xrootd
XrdZipUtils.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_XRDZIPUTILS_HH_
26 #define SRC_XRDZIP_XRDZIPUTILS_HH_
27 
28 #include <algorithm>
29 #include <cstdint>
30 #include <cstdlib>
31 #include <cstring>
32 #include <ctime>
33 #include <iterator>
34 #include <vector>
35 
36 #if defined(__APPLE__)
37 #include <libkern/OSByteOrder.h>
38 #else
39 #include <byteswap.h>
40 #endif
41 
42 #if defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN__) || \
43  defined(__IEEE_BIG_ENDIAN) || \
44  (defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN)
45 #define XrdZip_Big_Endian
46 #endif
47 
48 namespace XrdZip
49 {
50 #if defined(__APPLE__)
51  inline static uint16_t BSwap(uint16_t x) { return OSSwapInt16(x); }
52  inline static uint32_t BSwap(uint32_t x) { return OSSwapInt32(x); }
53  inline static uint64_t BSwap(uint64_t x) { return OSSwapInt64(x); }
54 #else
55  inline static uint16_t BSwap(uint16_t x) { return bswap_16(x); }
56  inline static uint32_t BSwap(uint32_t x) { return bswap_32(x); }
57  inline static uint64_t BSwap(uint64_t x) { return bswap_64(x); }
58 #endif
59 
60  //---------------------------------------------------------------------------
61  // Exception indicating corrupted data
62  //---------------------------------------------------------------------------
63  struct bad_data : public std::exception { };
64 
65  //---------------------------------------------------------------------------
66  // Provides overflow value for unsigned int types
67  //---------------------------------------------------------------------------
68  template<typename UINT>
69  struct ovrflw
70  {
71  static const UINT value = -1;
72  };
73 
74  //---------------------------------------------------------------------------
75  // Buffer type (a typedef for std::vector<char>)
76  //---------------------------------------------------------------------------
77  typedef std::vector<char> buffer_t;
78 
79  //---------------------------------------------------------------------------
80  // Copies integer byte by byte into a buffer
81  //---------------------------------------------------------------------------
82  template<typename INT>
83  inline static void copy_bytes( const INT value, buffer_t &buffer)
84  {
85  const char *begin = reinterpret_cast<const char*>( &value );
86  const char *end = begin + sizeof( INT );
87 #ifdef XrdZip_Big_Endian
88  std::reverse_copy( begin, end, std::back_inserter( buffer ) );
89 #else
90  std::copy( begin, end, std::back_inserter( buffer ) );
91 #endif
92  }
93 
94  //---------------------------------------------------------------------------
95  // Copies bytes into an integer type and advances the buffer by the number
96  // of bytes read.
97  //---------------------------------------------------------------------------
98  template<typename INT>
99  inline static void from_buffer( INT &var, const char *&buffer )
100  {
101  memcpy( &var, buffer, sizeof( INT ) );
102 #ifdef XrdZip_Big_Endian
103  var = BSwap(var);
104 #endif
105  buffer += sizeof( INT );
106  }
107 
108  //---------------------------------------------------------------------------
109  // Converts bytes into an integer type
110  //---------------------------------------------------------------------------
111  template<typename INT>
112  inline static INT to( const char *buffer )
113  {
114  INT value;
115  memcpy( &value, buffer, sizeof( INT ) );
116 #ifdef XrdZip_Big_Endian
117  value = BSwap(value);
118 #endif
119  return value;
120  }
121 
122  //---------------------------------------------------------------------------
123  // Generate a DOS timestamp (time/date)
124  //---------------------------------------------------------------------------
126  {
127  //-------------------------------------------------------------------------
128  // Default constructor (creates a timestamp for current time)
129  //-------------------------------------------------------------------------
130  inline dos_timestmp() : time( 0 ), date( 0 )
131  {
132  const std::time_t now = std::time( nullptr );
133  const std::tm calendar_time = *std::localtime( std::addressof( now ) );
134 
135  time |= ( hour_mask & uint16_t( calendar_time.tm_hour ) ) << hour_shift;
136  time |= ( min_mask & uint16_t( calendar_time.tm_min ) ) << min_shift;
137  time |= ( sec_mask & uint16_t( calendar_time.tm_sec / 2 ) ) << sec_shift;
138 
139  date |= ( year_mask & uint16_t( calendar_time.tm_year - 1980 ) ) << year_shift;
140  date |= ( mon_mask & uint16_t( calendar_time.tm_mon ) ) << mon_shift;
141  date |= ( day_mask & uint16_t( calendar_time.tm_mday ) ) << day_shift;
142  }
143 
144  //-------------------------------------------------------------------------
145  // Constructs a DOS timestamp from time_t value
146  //-------------------------------------------------------------------------
147  inline dos_timestmp( time_t timestmp ) : time( 0 ), date( 0 )
148  {
149  const std::tm calendar_time = *std::localtime( std::addressof( timestmp ) );
150 
151  time |= ( hour_mask & uint16_t( calendar_time.tm_hour ) ) << hour_shift;
152  time |= ( min_mask & uint16_t( calendar_time.tm_min ) ) << min_shift;
153  time |= ( sec_mask & uint16_t( calendar_time.tm_sec / 2 ) ) << sec_shift;
154 
155  date |= ( year_mask & uint16_t( calendar_time.tm_year - 1980 ) ) << year_shift;
156  date |= ( mon_mask & uint16_t( calendar_time.tm_mon ) ) << mon_shift;
157  date |= ( day_mask & uint16_t( calendar_time.tm_mday ) ) << day_shift;
158  }
159 
160  //-------------------------------------------------------------------------
161  // The time part of the DOS timestamp
162  //-------------------------------------------------------------------------
163  uint16_t time;
164 
165  static const uint16_t sec_mask = 0x1f; //< seconds mask
166  static const uint16_t min_mask = 0x3f; //< minutes mask
167  static const uint16_t hour_mask = 0x1f; //< hour mask
168 
169  static const uint8_t sec_shift = 0; //< seconds shift
170  static const uint8_t min_shift = 5; //< minutes shift
171  static const uint8_t hour_shift = 11; //< hour shift
172 
173  //-------------------------------------------------------------------------
174  // The date part of the DOS timestamp
175  //-------------------------------------------------------------------------
176  uint16_t date;
177 
178  static const uint16_t day_mask = 0x1f; //< day mask
179  static const uint16_t mon_mask = 0x0f; //< month mask
180  static const uint16_t year_mask = 0x7f; //< year mask
181 
182  static const uint8_t day_shift = 0; //< day shift
183  static const uint8_t mon_shift = 5; //< month shift
184  static const uint8_t year_shift = 9; //< year shift
185  };
186 }
187 
188 #endif /* SRC_XRDZIP_XRDZIPUTILS_HH_ */
static const uint8_t year_shift
Definition: XrdZipUtils.hh:184
std::vector< char > buffer_t
Definition: XrdZipUtils.hh:77
uint16_t time
Definition: XrdZipUtils.hh:163
static void copy_bytes(const INT value, buffer_t &buffer)
Definition: XrdZipUtils.hh:83
static const uint16_t year_mask
Definition: XrdZipUtils.hh:180
dos_timestmp(time_t timestmp)
Definition: XrdZipUtils.hh:147
static const uint8_t mon_shift
Definition: XrdZipUtils.hh:183
static const uint8_t hour_shift
Definition: XrdZipUtils.hh:171
static const uint8_t day_shift
Definition: XrdZipUtils.hh:182
static const uint16_t day_mask
Definition: XrdZipUtils.hh:178
Definition: XrdZipCDFH.hh:39
static INT to(const char *buffer)
Definition: XrdZipUtils.hh:112
static uint16_t BSwap(uint16_t x)
Definition: XrdZipUtils.hh:55
dos_timestmp()
Definition: XrdZipUtils.hh:130
static const uint16_t sec_mask
Definition: XrdZipUtils.hh:165
Definition: XrdZipUtils.hh:69
static const uint16_t mon_mask
Definition: XrdZipUtils.hh:179
Definition: XrdZipUtils.hh:63
Definition: XrdZipUtils.hh:125
static const uint8_t min_shift
Definition: XrdZipUtils.hh:170
static const uint16_t hour_mask
Definition: XrdZipUtils.hh:167
uint16_t date
Definition: XrdZipUtils.hh:176
static void from_buffer(INT &var, const char *&buffer)
Definition: XrdZipUtils.hh:99
static const UINT value
Definition: XrdZipUtils.hh:71
static const uint16_t min_mask
Definition: XrdZipUtils.hh:166
static const uint8_t sec_shift
Definition: XrdZipUtils.hh:169