XRootD
XrdZipExtra.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_XRDZIPEXTRA_HH_
26 #define SRC_XRDZIP_XRDZIPEXTRA_HH_
27 
28 #include "XrdZip/XrdZipUtils.hh"
29 
30 #include <cstdint>
31 #include <sys/types.h>
32 
33 namespace XrdZip
34 {
35  //---------------------------------------------------------------------------
36  // A data structure for the ZIP64 extra field
37  //---------------------------------------------------------------------------
38  struct Extra
39  {
40  //-------------------------------------------------------------------------
42  //-------------------------------------------------------------------------
43  Extra( uint64_t fileSize )
44  {
45  offset = 0;
46  nbDisk = 0;
47  if ( fileSize >= ovrflw<uint32_t>::value )
48  {
49  dataSize = 16;
50  uncompressedSize = fileSize;
51  compressedSize = fileSize;
52  totalSize = dataSize + 4;
53  }
54  else
55  {
56  dataSize = 0;
57  uncompressedSize = 0;
58  compressedSize = 0;
59  totalSize = 0;
60  }
61  }
62 
63  //-------------------------------------------------------------------------
65  //-------------------------------------------------------------------------
66  Extra( Extra *extra, uint64_t offset )
67  {
68  nbDisk = 0;
69  uncompressedSize = extra->uncompressedSize;
70  compressedSize = extra->compressedSize;
71  dataSize = extra->dataSize;
72  totalSize = extra->totalSize;
74  {
75  this->offset = offset;
76  dataSize += 8;
77  totalSize = dataSize + 4;
78  }
79  else
80  this->offset = 0;
81  }
82 
83  //-------------------------------------------------------------------------
85  //-------------------------------------------------------------------------
86  Extra() : dataSize( 0 ),
87  uncompressedSize( 0 ),
88  compressedSize( 0 ),
89  offset( 0 ),
90  nbDisk( 0 ),
91  totalSize( 0 )
92  {
93  }
94 
95  //-------------------------------------------------------------------------
100  //-------------------------------------------------------------------------
101  inline static const char* Find( const char *buffer, uint16_t length )
102  {
103  const char *end = buffer + length;
104  while( buffer < end )
105  {
106  uint16_t signature = to<uint16_t>( buffer );
107  uint16_t datasize = to<uint16_t>( buffer + 2 );
108  if( signature == headerID ) return buffer;
109  buffer += 2 * sizeof( uint16_t ) + datasize;
110  }
111  return nullptr;
112  }
113 
114  //-------------------------------------------------------------------------
116  //-------------------------------------------------------------------------
117  void FromBuffer( const char *&buffer, uint16_t exsize, uint8_t flags )
118  {
119  uint16_t signature = 0;
120  from_buffer( signature, buffer );
121  if( signature != headerID ) throw bad_data();
122 
123  from_buffer( dataSize, buffer );
124  if( dataSize != exsize ) throw bad_data();
125 
126  if( UCMPSIZE & flags )
127  from_buffer( uncompressedSize, buffer );
128 
129  if( CPMSIZE & flags )
130  from_buffer( compressedSize, buffer );
131 
132  if( OFFSET & flags )
133  from_buffer( offset, buffer );
134 
135  if( NBDISK & flags )
136  from_buffer( nbDisk, buffer );
137  }
138 
139  //-------------------------------------------------------------------------
141  //-------------------------------------------------------------------------
142  void Serialize( buffer_t &buffer )
143  {
144  if( totalSize > 0 )
145  {
146  copy_bytes( headerID, buffer );
147  copy_bytes( dataSize, buffer );
148  if ( uncompressedSize > 0)
149  {
150  copy_bytes( uncompressedSize, buffer );
151  copy_bytes( compressedSize, buffer );
152  if ( offset > 0 )
153  copy_bytes( offset, buffer );
154  }
155  else if( offset > 0 )
156  copy_bytes( offset, buffer );
157  }
158  }
159 
160  enum Ovrflw
161  {
162  NONE = 0,
163  UCMPSIZE = 1,
164  CPMSIZE = 2,
165  OFFSET = 4,
166  NBDISK = 8
167  };
168 
169  //-------------------------------------------------------------------------
171  //-------------------------------------------------------------------------
172  static const uint16_t headerID = 0x0001;
173 
174  uint16_t dataSize; //< size of the extra block
175  uint64_t uncompressedSize; //< size of the uncompressed data
176  uint64_t compressedSize; //< size of the compressed data
177  uint64_t offset; //< offset of local header record
178  uint32_t nbDisk; //< number of disk where file starts
179  uint16_t totalSize; //< total size in buffer
180  };
181 }
182 
183 #endif /* SRC_XRDZIP_XRDZIPEXTRA_HH_ */
int extra
Definition: XrdAccTest.cc:63
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
Extra(uint64_t fileSize)
Constructor from file size.
Definition: XrdZipExtra.hh:43
static const uint16_t headerID
The extra field marker.
Definition: XrdZipExtra.hh:172
uint16_t totalSize
Definition: XrdZipExtra.hh:179
uint64_t offset
Definition: XrdZipExtra.hh:177
Extra(Extra *extra, uint64_t offset)
Constructor from another extra + offset.
Definition: XrdZipExtra.hh:66
uint64_t uncompressedSize
Definition: XrdZipExtra.hh:175
Extra()
Default constructor.
Definition: XrdZipExtra.hh:86
uint16_t dataSize
Definition: XrdZipExtra.hh:174
uint64_t compressedSize
Definition: XrdZipExtra.hh:176
uint32_t nbDisk
Definition: XrdZipExtra.hh:178
static const char * Find(const char *buffer, uint16_t length)
Definition: XrdZipExtra.hh:101
void Serialize(buffer_t &buffer)
Serialize the extra field into a buffer.
Definition: XrdZipExtra.hh:142
void FromBuffer(const char *&buffer, uint16_t exsize, uint8_t flags)
Constructor from buffer.
Definition: XrdZipExtra.hh:117