xrootd
Loading...
Searching...
No Matches
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
36namespace 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 );
83 from_buffer( timestmp, buffer );
84 from_buffer( ZCRC32, buffer );
85 from_buffer( compressedSize, buffer );
87 from_buffer( filenameLength, buffer );
88 from_buffer( extraLength, buffer );
89
90 if(bufferSize > 0 && (uint64_t)(lfhBaseSize + filenameLength + extraLength) > bufferSize)
91 throw bad_data();
92 // parse the filename
93 filename.assign( buffer, filenameLength );
94 buffer += filenameLength;
95 // parse the extra record
96 if( extraLength > 0 )
97 ParseExtra( buffer, extraLength );
98
100 }
101
102 //-------------------------------------------------------------------------
104 //-------------------------------------------------------------------------
105 void Serialize( buffer_t &buffer )
106 {
107 copy_bytes( lfhSign, buffer );
108 copy_bytes( minZipVersion, buffer );
109 copy_bytes( generalBitFlag, buffer );
110 copy_bytes( compressionMethod, buffer );
111 copy_bytes( timestmp.time, buffer );
112 copy_bytes( timestmp.date, buffer );
113 copy_bytes( ZCRC32, buffer );
114 copy_bytes( compressedSize, buffer );
115 copy_bytes( uncompressedSize, buffer );
116 copy_bytes( filenameLength, buffer );
117 copy_bytes( extraLength , buffer );
118 std::copy( filename.begin(), filename.end(), std::back_inserter( buffer ) );
119 extra->Serialize( buffer );
120 }
121
122 //-------------------------------------------------------------------------
123 // Parse the extensible data fields
124 //-------------------------------------------------------------------------
125 void ParseExtra( const char *buffer, uint16_t length)
126 {
127 uint8_t ovrflws = Extra::NONE;
128 uint16_t exsize = 0;
129
130 // check if compressed size is overflown
132 {
133 ovrflws |= Extra::CPMSIZE;
134 exsize += sizeof( uint64_t );
135 }
136
137 // check if original size is overflown
139 {
140 ovrflws |= Extra::UCMPSIZE;
141 exsize += sizeof( uint64_t );
142 }
143
144 // if the expected size of ZIP64 extension is 0 we
145 // can skip parsing of 'extra'
146 if( exsize == 0 ) return;
147
148 extra.reset( new Extra() );
149
150 // Parse the extra part
151 buffer = Extra::Find( buffer, length );
152 if( buffer )
153 extra->FromBuffer( buffer, exsize, ovrflws );
154 }
155
156 uint16_t minZipVersion; //< minimum ZIP version required to read the file
157 uint16_t generalBitFlag; //< flags
158 uint16_t compressionMethod; //< compression method
159 dos_timestmp timestmp; //< DOS time stamp
160 uint32_t ZCRC32; //< crc32 value
161 uint32_t compressedSize; //< compressed data size
162 uint32_t uncompressedSize; //< uncompressed data size
163 uint16_t filenameLength; //< file name length
164 uint16_t extraLength; //< size of the ZIP64 extra field
165 std::string filename; //< file name
166 std::unique_ptr<Extra> extra; //< the ZIP64 extra field
167 uint16_t lfhSize; //< size of the Local File Header
168
169 //-------------------------------------------------------------------------
171 //-------------------------------------------------------------------------
172 static const uint32_t lfhSign = 0x04034b50;
173 static const uint16_t lfhBaseSize = 30;
174 };
175}
176
177#endif /* SRC_XRDZIP_XRDZIPLFH_HH_ */
Definition: XrdZipCDFH.hh:40
static void from_buffer(INT &var, const char *&buffer)
Definition: XrdZipUtils.hh:72
std::vector< char > buffer_t
Definition: XrdZipUtils.hh:54
static void copy_bytes(const INT value, buffer_t &buffer)
Definition: XrdZipUtils.hh:60
Definition: XrdZipExtra.hh:36
static const char * Find(const char *buffer, uint16_t length)
Definition: XrdZipExtra.hh:98
@ CPMSIZE
Definition: XrdZipExtra.hh:161
@ UCMPSIZE
Definition: XrdZipExtra.hh:160
@ NONE
Definition: XrdZipExtra.hh:159
A data structure representing ZIP Local File Header.
Definition: XrdZipLFH.hh:42
uint32_t ZCRC32
Definition: XrdZipLFH.hh:160
void Serialize(buffer_t &buffer)
Serialize the object into a buffer.
Definition: XrdZipLFH.hh:105
static const uint16_t lfhBaseSize
Definition: XrdZipLFH.hh:173
dos_timestmp timestmp
Definition: XrdZipLFH.hh:159
uint16_t extraLength
Definition: XrdZipLFH.hh:164
uint32_t compressedSize
Definition: XrdZipLFH.hh:161
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:158
uint16_t generalBitFlag
Definition: XrdZipLFH.hh:157
void ParseExtra(const char *buffer, uint16_t length)
Definition: XrdZipLFH.hh:125
uint16_t lfhSize
Definition: XrdZipLFH.hh:167
std::unique_ptr< Extra > extra
Definition: XrdZipLFH.hh:166
uint32_t uncompressedSize
Definition: XrdZipLFH.hh:162
uint16_t minZipVersion
Definition: XrdZipLFH.hh:156
std::string filename
Definition: XrdZipLFH.hh:165
uint16_t filenameLength
Definition: XrdZipLFH.hh:163
static const uint32_t lfhSign
Local File Header signature.
Definition: XrdZipLFH.hh:172
Definition: XrdZipUtils.hh:40
Definition: XrdZipUtils.hh:93
uint16_t time
Definition: XrdZipUtils.hh:130
uint16_t date
Definition: XrdZipUtils.hh:143
Definition: XrdZipUtils.hh:47