xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 )
55  {
56  nbDisk = *reinterpret_cast<const uint16_t*>( buffer + 4 );
57  nbDiskCd = *reinterpret_cast<const uint16_t*>( buffer + 6 );
58  nbCdRecD = *reinterpret_cast<const uint16_t*>( buffer + 8 );
59  nbCdRec = *reinterpret_cast<const uint16_t*>( buffer + 10 );
60  cdSize = *reinterpret_cast<const uint32_t*>( buffer + 12 );
61  cdOffset = *reinterpret_cast<const uint32_t*>( buffer + 16 );
62  commentLength = *reinterpret_cast<const uint16_t*>( buffer + 20 );
63  comment = std::string( buffer + 22, commentLength );
64 
66  useZip64= false;
67  }
68 
69  //-------------------------------------------------------------------------
71  //-------------------------------------------------------------------------
72  EOCD( uint64_t cdoff, uint32_t cdcnt, uint32_t cdsize ):
73  nbDisk( 0 ),
74  nbDiskCd( 0 ),
75  commentLength( 0 ),
76  useZip64( false )
77  {
78  if( cdcnt >= ovrflw<uint16_t>::value )
79  {
82  }
83  else
84  {
85  nbCdRecD = cdcnt;
86  nbCdRec = cdcnt;
87  }
88 
89  cdSize = cdsize;
90 
91  if( cdoff >= ovrflw<uint32_t>::value )
92  {
94  useZip64 = true;
95  }
96  else
97  cdOffset = cdoff;
98 
100  }
101 
102  //-------------------------------------------------------------------------
104  //-------------------------------------------------------------------------
105  void Serialize( buffer_t &buffer )
106  {
107  copy_bytes( eocdSign, buffer );
108  copy_bytes( nbDisk, buffer );
109  copy_bytes( nbDiskCd, buffer );
110  copy_bytes( nbCdRecD, buffer );
111  copy_bytes( nbCdRec, buffer );
112  copy_bytes( cdSize, buffer );
113  copy_bytes( cdOffset, buffer );
114  copy_bytes( commentLength, buffer );
115 
116  std::copy( comment.begin(), comment.end(), std::back_inserter( buffer ) );
117  }
118 
119  //-------------------------------------------------------------------------
121  //-------------------------------------------------------------------------
122  std::string ToString()
123  {
124  std::stringstream ss;
125  ss << "{nbDisk=" << nbDisk;
126  ss << ";nbDiskCd=" << nbDiskCd;
127  ss << ";nbCdRecD=" << nbCdRecD;
128  ss << ";nbCdRec=" << nbCdRec;
129  ss << ";cdSize" << cdSize;
130  ss << ";cdOffset=" << cdOffset;
131  ss << ";commentLength=" << commentLength;
132  ss << ";comment=" << comment << '}';
133  return ss.str();
134  }
135 
136  uint16_t nbDisk; //< number of this disk
137  uint16_t nbDiskCd; //< number of the disk with the start of the central directory
138  uint16_t nbCdRecD; //< total number of entries in the central directory on this disk
139  uint16_t nbCdRec; //< total number of entries in the central directory
140  uint32_t cdSize; //< size of the central directory
141  uint32_t cdOffset; //< offset of start of central directory
142  uint16_t commentLength; //< comment length
143  std::string comment; //< user comment
144  uint16_t eocdSize; //< size of the record
145  bool useZip64; //< true if ZIP64 format is to be used, false otherwise
146 
147  //-------------------------------------------------------------------------
148  // the End of Central Directory signature
149  //-------------------------------------------------------------------------
150  static const uint32_t eocdSign = 0x06054b50;
151  static const uint16_t eocdBaseSize = 22;
152  static const uint16_t maxCommentLength = 65535;
153  };
154 
155 }
156 
157 #endif /* SRC_XRDZIP_XRDZIPEOCD_HH_ */
EOCD(uint64_t cdoff, uint32_t cdcnt, uint32_t cdsize)
Constructor from last LFH + CDFH.
Definition: XrdZipEOCD.hh:72
static const uint16_t eocdBaseSize
Definition: XrdZipEOCD.hh:151
static const uint32_t eocdSign
Definition: XrdZipEOCD.hh:150
uint16_t commentLength
Definition: XrdZipEOCD.hh:142
std::vector< char > buffer_t
Definition: XrdZipUtils.hh:54
uint16_t nbDiskCd
Definition: XrdZipEOCD.hh:137
uint16_t nbCdRec
Definition: XrdZipEOCD.hh:139
std::string ToString()
Convert the EOCD into a string for logging purposes.
Definition: XrdZipEOCD.hh:122
EOCD(const char *buffer)
Constructor from buffer.
Definition: XrdZipEOCD.hh:54
void Serialize(buffer_t &buffer)
Serialize the object into a buffer.
Definition: XrdZipEOCD.hh:105
static const uint16_t maxCommentLength
Definition: XrdZipEOCD.hh:152
static void copy_bytes(const INT value, buffer_t &buffer)
Definition: XrdZipUtils.hh:60
std::string comment
Definition: XrdZipEOCD.hh:143
uint32_t cdSize
Definition: XrdZipEOCD.hh:140
uint32_t cdOffset
Definition: XrdZipEOCD.hh:141
Definition: XrdZipUtils.hh:46
uint16_t nbCdRecD
Definition: XrdZipEOCD.hh:138
Definition: XrdZipEOCD.hh:39
uint16_t nbDisk
Definition: XrdZipEOCD.hh:136
uint16_t eocdSize
Definition: XrdZipEOCD.hh:144
static const char * Find(const char *buffer, uint64_t size)
Definition: XrdZipEOCD.hh:41
bool useZip64
Definition: XrdZipEOCD.hh:145