Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/xrootd/private/XrdZip/XrdZipExtra.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //------------------------------------------------------------------------------
0002 // Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
0003 // Author: Michal Simon <michal.simon@cern.ch>
0004 //------------------------------------------------------------------------------
0005 // This file is part of the XRootD software suite.
0006 //
0007 // XRootD is free software: you can redistribute it and/or modify
0008 // it under the terms of the GNU Lesser General Public License as published by
0009 // the Free Software Foundation, either version 3 of the License, or
0010 // (at your option) any later version.
0011 //
0012 // XRootD is distributed in the hope that it will be useful,
0013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015 // GNU General Public License for more details.
0016 //
0017 // You should have received a copy of the GNU Lesser General Public License
0018 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0019 //
0020 // In applying this licence, CERN does not waive the privileges and immunities
0021 // granted to it by virtue of its status as an Intergovernmental Organization
0022 // or submit itself to any jurisdiction.
0023 //------------------------------------------------------------------------------
0024 
0025 #ifndef SRC_XRDZIP_XRDZIPEXTRA_HH_
0026 #define SRC_XRDZIP_XRDZIPEXTRA_HH_
0027 
0028 #include "XrdZip/XrdZipUtils.hh"
0029 
0030 #include <cstdint>
0031 #include <sys/types.h>
0032 
0033 namespace XrdZip
0034 {
0035   //---------------------------------------------------------------------------
0036   // A data structure for the ZIP64 extra field
0037   //---------------------------------------------------------------------------
0038   struct Extra
0039   {
0040     //-------------------------------------------------------------------------
0041     //! Constructor from file size
0042     //-------------------------------------------------------------------------
0043     Extra( uint64_t fileSize )
0044     {
0045       offset = 0;
0046       nbDisk = 0;
0047       if ( fileSize >= ovrflw<uint32_t>::value )
0048       {
0049         dataSize = 16;
0050         uncompressedSize = fileSize;
0051         compressedSize = fileSize;
0052         totalSize = dataSize + 4;
0053       }
0054       else
0055       {
0056         dataSize = 0;
0057         uncompressedSize = 0;
0058         compressedSize = 0;
0059         totalSize = 0;
0060       }
0061     }
0062 
0063     //-------------------------------------------------------------------------
0064     //! Constructor from another extra + offset
0065     //-------------------------------------------------------------------------
0066     Extra( Extra *extra, uint64_t offset )
0067     {
0068       nbDisk = 0;
0069       uncompressedSize = extra->uncompressedSize;
0070       compressedSize = extra->compressedSize;
0071       dataSize = extra->dataSize;
0072       totalSize = extra->totalSize;
0073       if ( offset >= ovrflw<uint32_t>::value )
0074       {
0075         this->offset = offset;
0076         dataSize += 8;
0077         totalSize = dataSize + 4;
0078       }
0079       else
0080         this->offset = 0;
0081     }
0082 
0083     //-------------------------------------------------------------------------
0084     //! Default constructor
0085     //-------------------------------------------------------------------------
0086     Extra() : dataSize( 0 ),
0087               uncompressedSize( 0 ),
0088               compressedSize( 0 ),
0089               offset( 0 ),
0090               nbDisk( 0 ),
0091               totalSize( 0 )
0092     {
0093     }
0094 
0095     //-------------------------------------------------------------------------
0096     //! Finds the Zip64 extended information extra field
0097     //!
0098     //! @return : pointer to the buffer holding ZIP64 extra field, nullptr
0099     //!           on failure
0100     //-------------------------------------------------------------------------
0101     inline static const char* Find( const char *buffer, uint16_t length )
0102     {
0103       const char *end = buffer + length;
0104       while( buffer < end )
0105       {
0106         uint16_t signature = to<uint16_t>( buffer );
0107         uint16_t datasize  = to<uint16_t>( buffer + 2 );
0108         if( signature == headerID ) return buffer;
0109         buffer += 2 * sizeof( uint16_t ) + datasize;
0110       }
0111       return nullptr;
0112     }
0113 
0114     //-------------------------------------------------------------------------
0115     //! Constructor from buffer
0116     //-------------------------------------------------------------------------
0117     void FromBuffer( const char *&buffer, uint16_t exsize, uint8_t flags )
0118     {
0119       uint16_t signature = 0;
0120       from_buffer( signature, buffer );
0121       if( signature != headerID ) throw bad_data();
0122 
0123       from_buffer( dataSize, buffer );
0124       if( dataSize != exsize ) throw bad_data();
0125 
0126       if( UCMPSIZE & flags )
0127         from_buffer( uncompressedSize, buffer );
0128 
0129       if( CPMSIZE & flags )
0130         from_buffer( compressedSize, buffer );
0131 
0132       if( OFFSET & flags )
0133         from_buffer( offset, buffer );
0134 
0135       if( NBDISK & flags )
0136         from_buffer( nbDisk, buffer );
0137     }
0138 
0139     //-------------------------------------------------------------------------
0140     //! Serialize the extra field into a buffer
0141     //-------------------------------------------------------------------------
0142     void Serialize( buffer_t &buffer )
0143     {
0144       if( totalSize > 0 )
0145       {
0146         copy_bytes( headerID, buffer );
0147         copy_bytes( dataSize, buffer );
0148         if ( uncompressedSize > 0)
0149         {
0150           copy_bytes( uncompressedSize, buffer );
0151           copy_bytes( compressedSize,   buffer );
0152           if ( offset > 0 )
0153             copy_bytes( offset, buffer );
0154         }
0155         else if( offset > 0 )
0156           copy_bytes( offset, buffer );
0157       }
0158     }
0159 
0160     enum Ovrflw
0161     {
0162       NONE     = 0,
0163       UCMPSIZE = 1,
0164       CPMSIZE  = 2,
0165       OFFSET   = 4,
0166       NBDISK   = 8
0167     };
0168 
0169     //-------------------------------------------------------------------------
0170     //! The extra field marker
0171     //-------------------------------------------------------------------------
0172     static const uint16_t headerID = 0x0001;
0173 
0174     uint16_t dataSize;         //< size of the extra block
0175     uint64_t uncompressedSize; //< size of the uncompressed data
0176     uint64_t compressedSize;   //< size of the compressed data
0177     uint64_t offset;           //< offset of local header record
0178     uint32_t nbDisk;           //< number of disk where file starts
0179     uint16_t totalSize;        //< total size in buffer
0180   };
0181 }
0182 
0183 #endif /* SRC_XRDZIP_XRDZIPEXTRA_HH_ */