xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 )
72  {
73  // check if the buffer contains a LFH record
74  uint32_t signature = 0;
75  from_buffer( signature, buffer );
76  if( signature != lfhSign ) throw bad_data();
77  // parse LFH filds
78  from_buffer( minZipVersion, buffer );
79  from_buffer( generalBitFlag, buffer );
80  from_buffer( compressionMethod, buffer );
81  from_buffer( timestmp, buffer );
82  from_buffer( ZCRC32, buffer );
83  from_buffer( compressedSize, buffer );
84  from_buffer( uncompressedSize, buffer );
85  from_buffer( filenameLength, buffer );
86  from_buffer( extraLength, buffer );
87  // parse the filename
88  filename.assign( buffer, filenameLength );
89  buffer += filenameLength;
90  // parse the extra record
91  if( extraLength > 0 )
92  ParseExtra( buffer, extraLength );
93 
95  }
96 
97  //-------------------------------------------------------------------------
99  //-------------------------------------------------------------------------
100  void Serialize( buffer_t &buffer )
101  {
102  copy_bytes( lfhSign, buffer );
103  copy_bytes( minZipVersion, buffer );
104  copy_bytes( generalBitFlag, buffer );
105  copy_bytes( compressionMethod, buffer );
106  copy_bytes( timestmp.time, buffer );
107  copy_bytes( timestmp.date, buffer );
108  copy_bytes( ZCRC32, buffer );
109  copy_bytes( compressedSize, buffer );
110  copy_bytes( uncompressedSize, buffer );
111  copy_bytes( filenameLength, buffer );
112  copy_bytes( extraLength , buffer );
113  std::copy( filename.begin(), filename.end(), std::back_inserter( buffer ) );
114  extra->Serialize( buffer );
115  }
116 
117  //-------------------------------------------------------------------------
118  // Parse the extensible data fields
119  //-------------------------------------------------------------------------
120  void ParseExtra( const char *buffer, uint16_t length)
121  {
122  uint8_t ovrflws = Extra::NONE;
123  uint16_t exsize = 0;
124 
125  // check if compressed size is overflown
127  {
128  ovrflws |= Extra::CPMSIZE;
129  exsize += sizeof( uint64_t );
130  }
131 
132  // check if original size is overflown
134  {
135  ovrflws |= Extra::UCMPSIZE;
136  exsize += sizeof( uint64_t );
137  }
138 
139  // if the expected size of ZIP64 extension is 0 we
140  // can skip parsing of 'extra'
141  if( exsize == 0 ) return;
142 
143  extra.reset( new Extra() );
144 
145  // Parse the extra part
146  buffer = Extra::Find( buffer, length );
147  if( buffer )
148  extra->FromBuffer( buffer, exsize, ovrflws );
149  }
150 
151  uint16_t minZipVersion; //< minimum ZIP version required to read the file
152  uint16_t generalBitFlag; //< flags
153  uint16_t compressionMethod; //< compression method
154  dos_timestmp timestmp; //< DOS time stamp
155  uint32_t ZCRC32; //< crc32 value
156  uint32_t compressedSize; //< compressed data size
157  uint32_t uncompressedSize; //< uncompressed data size
158  uint16_t filenameLength; //< file name length
159  uint16_t extraLength; //< size of the ZIP64 extra field
160  std::string filename; //< file name
161  std::unique_ptr<Extra> extra; //< the ZIP64 extra field
162  uint16_t lfhSize; //< size of the Local File Header
163 
164  //-------------------------------------------------------------------------
166  //-------------------------------------------------------------------------
167  static const uint32_t lfhSign = 0x04034b50;
168  static const uint16_t lfhBaseSize = 30;
169  };
170 }
171 
172 #endif /* SRC_XRDZIP_XRDZIPLFH_HH_ */
uint32_t compressedSize
Definition: XrdZipLFH.hh:156
void ParseExtra(const char *buffer, uint16_t length)
Definition: XrdZipLFH.hh:120
LFH(const char *buffer)
Constructor from buffer.
Definition: XrdZipLFH.hh:71
std::vector< char > buffer_t
Definition: XrdZipUtils.hh:54
uint16_t time
Definition: XrdZipUtils.hh:130
uint16_t compressionMethod
Definition: XrdZipLFH.hh:153
static void copy_bytes(const INT value, buffer_t &buffer)
Definition: XrdZipUtils.hh:60
uint16_t generalBitFlag
Definition: XrdZipLFH.hh:152
static const char * Find(const char *buffer, uint16_t length)
Definition: XrdZipExtra.hh:98
LFH(const std::string &filename, uint32_t crc, off_t fileSize, time_t time)
Constructor.
Definition: XrdZipLFH.hh:55
uint16_t lfhSize
Definition: XrdZipLFH.hh:162
uint16_t extraLength
Definition: XrdZipLFH.hh:159
Definition: XrdZipExtra.hh:159
static uint32_t initSize(const off_t &fileSize)
Convenience function for initializing compressed/uncompressed size.
Definition: XrdZipLFH.hh:46
A data structure representing ZIP Local File Header.
Definition: XrdZipLFH.hh:41
static const uint32_t lfhSign
Local File Header signature.
Definition: XrdZipLFH.hh:167
Definition: XrdZipUtils.hh:46
std::string filename
Definition: XrdZipLFH.hh:160
dos_timestmp timestmp
Definition: XrdZipLFH.hh:154
Definition: XrdZipUtils.hh:40
uint32_t ZCRC32
Definition: XrdZipLFH.hh:155
Definition: XrdZipUtils.hh:92
void Serialize(buffer_t &buffer)
Serialize the object into a buffer.
Definition: XrdZipLFH.hh:100
uint16_t date
Definition: XrdZipUtils.hh:143
uint16_t filenameLength
Definition: XrdZipLFH.hh:158
Definition: XrdZipExtra.hh:160
static void from_buffer(INT &var, const char *&buffer)
Definition: XrdZipUtils.hh:72
Definition: XrdZipExtra.hh:161
static const uint16_t lfhBaseSize
Definition: XrdZipLFH.hh:168
Definition: XrdZipExtra.hh:35
std::unique_ptr< Extra > extra
Definition: XrdZipLFH.hh:161
uint32_t uncompressedSize
Definition: XrdZipLFH.hh:157
uint16_t minZipVersion
Definition: XrdZipLFH.hh:151