XRootD
XrdZipLFH.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_XRDZIPLFH_HH_
26 #define SRC_XRDZIP_XRDZIPLFH_HH_
27 
28 #include "XrdZip/XrdZipUtils.hh"
29 #include "XrdZip/XrdZipExtra.hh"
30 
31 #include <string>
32 #include <memory>
33 #include <algorithm>
34 #include <iterator>
35 
36 namespace XrdZip
37 {
38  //---------------------------------------------------------------------------
40  //---------------------------------------------------------------------------
41  struct LFH
42  {
43  //-------------------------------------------------------------------------
45  //-------------------------------------------------------------------------
46  inline static uint32_t initSize( const off_t &fileSize )
47  {
48  return fileSize >= ovrflw<uint32_t>::value ?
49  ovrflw<uint32_t>::value : fileSize;
50  }
51 
52  //-------------------------------------------------------------------------
54  //-------------------------------------------------------------------------
55  LFH( const std::string &filename, uint32_t crc, off_t fileSize, time_t time ) :
56  generalBitFlag( 0 ), compressionMethod( 0 ), timestmp( time ), ZCRC32( crc ),
57  compressedSize( initSize( fileSize ) ), uncompressedSize( initSize( fileSize ) ),
58  filenameLength( filename.size() ), filename( filename ), extra( new Extra( fileSize ) )
59  {
60  extraLength = extra->totalSize;
61  if ( extraLength == 0 )
62  minZipVersion = 10;
63  else
64  minZipVersion = 45;
66  }
67 
68  //-------------------------------------------------------------------------
70  //-------------------------------------------------------------------------
71  LFH( const char *buffer, const uint64_t bufferSize = 0 )
72  {
73  if(bufferSize > 0 && bufferSize < (uint64_t)lfhBaseSize)
74  throw bad_data();
75  // check if the buffer contains a LFH record
76  uint32_t signature = 0;
77  from_buffer( signature, buffer );
78  if( signature != lfhSign ) throw bad_data();
79  // parse LFH filds
80  from_buffer( minZipVersion, buffer );
81  from_buffer( generalBitFlag, buffer );
82  from_buffer( compressionMethod, buffer );
83  from_buffer( timestmp.time, buffer );
84  from_buffer( timestmp.date, buffer );
85  from_buffer( ZCRC32, buffer );
86  from_buffer( compressedSize, buffer );
87  from_buffer( uncompressedSize, buffer );
88  from_buffer( filenameLength, buffer );
89  from_buffer( extraLength, buffer );
90 
91  if(bufferSize > 0 && (uint64_t)(lfhBaseSize + filenameLength + extraLength) > bufferSize)
92  throw bad_data();
93  // parse the filename
94  filename.assign( buffer, filenameLength );
95  buffer += filenameLength;
96  // parse the extra record
97  if( extraLength > 0 )
98  ParseExtra( buffer, extraLength );
99 
101  }
102 
103  //-------------------------------------------------------------------------
105  //-------------------------------------------------------------------------
106  void Serialize( buffer_t &buffer )
107  {
108  copy_bytes( lfhSign, buffer );
109  copy_bytes( minZipVersion, buffer );
110  copy_bytes( generalBitFlag, buffer );
111  copy_bytes( compressionMethod, buffer );
112  copy_bytes( timestmp.time, buffer );
113  copy_bytes( timestmp.date, buffer );
114  copy_bytes( ZCRC32, buffer );
115  copy_bytes( compressedSize, buffer );
116  copy_bytes( uncompressedSize, buffer );
117  copy_bytes( filenameLength, buffer );
118  copy_bytes( extraLength , buffer );
119  std::copy( filename.begin(), filename.end(), std::back_inserter( buffer ) );
120  extra->Serialize( buffer );
121  }
122 
123  //-------------------------------------------------------------------------
124  // Parse the extensible data fields
125  //-------------------------------------------------------------------------
126  void ParseExtra( const char *buffer, uint16_t length)
127  {
128  uint8_t ovrflws = Extra::NONE;
129  uint16_t exsize = 0;
130 
131  // check if compressed size is overflown
133  {
134  ovrflws |= Extra::CPMSIZE;
135  exsize += sizeof( uint64_t );
136  }
137 
138  // check if original size is overflown
140  {
141  ovrflws |= Extra::UCMPSIZE;
142  exsize += sizeof( uint64_t );
143  }
144 
145  // if the expected size of ZIP64 extension is 0 we
146  // can skip parsing of 'extra'
147  if( exsize == 0 ) return;
148 
149  extra.reset( new Extra() );
150 
151  // Parse the extra part
152  buffer = Extra::Find( buffer, length );
153  if( buffer )
154  extra->FromBuffer( buffer, exsize, ovrflws );
155  }
156 
157  uint16_t minZipVersion; //< minimum ZIP version required to read the file
158  uint16_t generalBitFlag; //< flags
159  uint16_t compressionMethod; //< compression method
160  dos_timestmp timestmp; //< DOS time stamp
161  uint32_t ZCRC32; //< crc32 value
162  uint32_t compressedSize; //< compressed data size
163  uint32_t uncompressedSize; //< uncompressed data size
164  uint16_t filenameLength; //< file name length
165  uint16_t extraLength; //< size of the ZIP64 extra field
166  std::string filename; //< file name
167  std::unique_ptr<Extra> extra; //< the ZIP64 extra field
168  uint16_t lfhSize; //< size of the Local File Header
169 
170  //-------------------------------------------------------------------------
172  //-------------------------------------------------------------------------
173  static const uint32_t lfhSign = 0x04034b50;
174  static const uint16_t lfhBaseSize = 30;
175  };
176 }
177 
178 #endif /* SRC_XRDZIP_XRDZIPLFH_HH_ */
static void from_buffer(INT &var, const char *&buffer)
Definition: XrdZipUtils.hh:78
std::vector< char > buffer_t
Definition: XrdZipUtils.hh:56
static void copy_bytes(const INT value, buffer_t &buffer)
Definition: XrdZipUtils.hh:62
static const char * Find(const char *buffer, uint16_t length)
Definition: XrdZipExtra.hh:101
A data structure representing ZIP Local File Header.
Definition: XrdZipLFH.hh:42
uint32_t ZCRC32
Definition: XrdZipLFH.hh:161
void Serialize(buffer_t &buffer)
Serialize the object into a buffer.
Definition: XrdZipLFH.hh:106
static const uint16_t lfhBaseSize
Definition: XrdZipLFH.hh:174
dos_timestmp timestmp
Definition: XrdZipLFH.hh:160
uint16_t extraLength
Definition: XrdZipLFH.hh:165
uint32_t compressedSize
Definition: XrdZipLFH.hh:162
LFH(const char *buffer, const uint64_t bufferSize=0)
Constructor from buffer.
Definition: XrdZipLFH.hh:71
LFH(const std::string &filename, uint32_t crc, off_t fileSize, time_t time)
Constructor.
Definition: XrdZipLFH.hh:55
static uint32_t initSize(const off_t &fileSize)
Convenience function for initializing compressed/uncompressed size.
Definition: XrdZipLFH.hh:46
uint16_t compressionMethod
Definition: XrdZipLFH.hh:159
uint16_t generalBitFlag
Definition: XrdZipLFH.hh:158
void ParseExtra(const char *buffer, uint16_t length)
Definition: XrdZipLFH.hh:126
uint16_t lfhSize
Definition: XrdZipLFH.hh:168
std::unique_ptr< Extra > extra
Definition: XrdZipLFH.hh:167
uint32_t uncompressedSize
Definition: XrdZipLFH.hh:163
uint16_t minZipVersion
Definition: XrdZipLFH.hh:157
std::string filename
Definition: XrdZipLFH.hh:166
uint16_t filenameLength
Definition: XrdZipLFH.hh:164
static const uint32_t lfhSign
Local File Header signature.
Definition: XrdZipLFH.hh:173