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