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