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