XRootD
XrdZipEOCD.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
3 // Author: Michal Simon <michal.simon@cern.ch>
4 //------------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 //------------------------------------------------------------------------------
24 
25 #ifndef SRC_XRDZIP_XRDZIPEOCD_HH_
26 #define SRC_XRDZIP_XRDZIPEOCD_HH_
27 
28 #include "XrdZip/XrdZipUtils.hh"
29 #include "XrdZip/XrdZipLFH.hh"
30 #include "XrdZip/XrdZipCDFH.hh"
31 #include <string>
32 #include <sstream>
33 
34 namespace XrdZip
35 {
36  //---------------------------------------------------------------------------
37  // A data structure representing the End of Central Directory record
38  //---------------------------------------------------------------------------
39  struct EOCD
40  {
41  inline static const char* Find( const char *buffer, uint64_t size )
42  {
43  for( ssize_t offset = size - eocdBaseSize; offset >= 0; --offset )
44  {
45  uint32_t signature = to<uint32_t>( buffer + offset );
46  if( signature == eocdSign ) return buffer + offset;
47  }
48  return 0;
49  }
50 
51  //-------------------------------------------------------------------------
53  //-------------------------------------------------------------------------
54  EOCD( const char *buffer, uint32_t maxSize = 0 )
55  {
56  nbDisk = to<uint16_t>(buffer + 4);
57  nbDiskCd = to<uint16_t>(buffer + 6);
58  nbCdRecD = to<uint16_t>(buffer + 8);
59  nbCdRec = to<uint16_t>(buffer + 10);
60  cdSize = to<uint32_t>(buffer + 12);
61  cdOffset = to<uint32_t>(buffer + 16);
62  commentLength = to<uint16_t>(buffer + 20);
63  if(maxSize > 0 && (uint32_t)(eocdBaseSize + commentLength) > maxSize)
64  throw bad_data();
65  comment = std::string( buffer + 22, commentLength );
66 
68  useZip64= false;
69  }
70 
71  //-------------------------------------------------------------------------
73  //-------------------------------------------------------------------------
74  EOCD( uint64_t cdoff, uint32_t cdcnt, uint32_t cdsize ):
75  nbDisk( 0 ),
76  nbDiskCd( 0 ),
77  commentLength( 0 ),
78  useZip64( false )
79  {
80  if( cdcnt >= ovrflw<uint16_t>::value )
81  {
84  }
85  else
86  {
87  nbCdRecD = cdcnt;
88  nbCdRec = cdcnt;
89  }
90 
91  cdSize = cdsize;
92 
93  if( cdoff >= ovrflw<uint32_t>::value )
94  {
96  useZip64 = true;
97  }
98  else
99  cdOffset = cdoff;
100 
102  }
103 
104  //-------------------------------------------------------------------------
106  //-------------------------------------------------------------------------
107  void Serialize( buffer_t &buffer )
108  {
109  copy_bytes( eocdSign, buffer );
110  copy_bytes( nbDisk, buffer );
111  copy_bytes( nbDiskCd, buffer );
112  copy_bytes( nbCdRecD, buffer );
113  copy_bytes( nbCdRec, buffer );
114  copy_bytes( cdSize, buffer );
115  copy_bytes( cdOffset, buffer );
116  copy_bytes( commentLength, buffer );
117 
118  std::copy( comment.begin(), comment.end(), std::back_inserter( buffer ) );
119  }
120 
121  //-------------------------------------------------------------------------
123  //-------------------------------------------------------------------------
124  std::string ToString()
125  {
126  std::stringstream ss;
127  ss << "{nbDisk=" << nbDisk;
128  ss << ";nbDiskCd=" << nbDiskCd;
129  ss << ";nbCdRecD=" << nbCdRecD;
130  ss << ";nbCdRec=" << nbCdRec;
131  ss << ";cdSize" << cdSize;
132  ss << ";cdOffset=" << cdOffset;
133  ss << ";commentLength=" << commentLength;
134  ss << ";comment=" << comment << '}';
135  return ss.str();
136  }
137 
138  uint16_t nbDisk; //< number of this disk
139  uint16_t nbDiskCd; //< number of the disk with the start of the central directory
140  uint16_t nbCdRecD; //< total number of entries in the central directory on this disk
141  uint16_t nbCdRec; //< total number of entries in the central directory
142  uint32_t cdSize; //< size of the central directory
143  uint32_t cdOffset; //< offset of start of central directory
144  uint16_t commentLength; //< comment length
145  std::string comment; //< user comment
146  uint16_t eocdSize; //< size of the record
147  bool useZip64; //< true if ZIP64 format is to be used, false otherwise
148 
149  //-------------------------------------------------------------------------
150  // the End of Central Directory signature
151  //-------------------------------------------------------------------------
152  static const uint32_t eocdSign = 0x06054b50;
153  static const uint16_t eocdBaseSize = 22;
154  static const uint16_t maxCommentLength = 65535;
155  };
156 
157 }
158 
159 #endif /* SRC_XRDZIP_XRDZIPEOCD_HH_ */
std::vector< char > buffer_t
Definition: XrdZipUtils.hh:56
static void copy_bytes(const INT value, buffer_t &buffer)
Definition: XrdZipUtils.hh:62
uint16_t nbDisk
Definition: XrdZipEOCD.hh:138
static const uint16_t eocdBaseSize
Definition: XrdZipEOCD.hh:153
uint16_t eocdSize
Definition: XrdZipEOCD.hh:146
std::string comment
Definition: XrdZipEOCD.hh:145
uint16_t commentLength
Definition: XrdZipEOCD.hh:144
static const uint16_t maxCommentLength
Definition: XrdZipEOCD.hh:154
uint16_t nbCdRecD
Definition: XrdZipEOCD.hh:140
std::string ToString()
Convert the EOCD into a string for logging purposes.
Definition: XrdZipEOCD.hh:124
EOCD(const char *buffer, uint32_t maxSize=0)
Constructor from buffer.
Definition: XrdZipEOCD.hh:54
uint16_t nbDiskCd
Definition: XrdZipEOCD.hh:139
EOCD(uint64_t cdoff, uint32_t cdcnt, uint32_t cdsize)
Constructor from last LFH + CDFH.
Definition: XrdZipEOCD.hh:74
uint32_t cdOffset
Definition: XrdZipEOCD.hh:143
static const uint32_t eocdSign
Definition: XrdZipEOCD.hh:152
static const char * Find(const char *buffer, uint64_t size)
Definition: XrdZipEOCD.hh:41
uint32_t cdSize
Definition: XrdZipEOCD.hh:142
uint16_t nbCdRec
Definition: XrdZipEOCD.hh:141
void Serialize(buffer_t &buffer)
Serialize the object into a buffer.
Definition: XrdZipEOCD.hh:107