BytesWriter.h
Go to the documentation of this file.
1 
2 // This source file is part of the ZipArchive Library Open Source distribution
3 // and is Copyrighted 2000 - 2022 by Artpol Software - Tadeusz Dracz
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // For the licensing details refer to the License.txt file.
11 //
12 // Web Site: https://www.artpol-software.com
14 
21 #if !defined(ZIPARCHIVE_BYTESWRITER_DOT_H)
22 #define ZIPARCHIVE_BYTESWRITER_DOT_H
23 
24 #if _MSC_VER > 1000
25  #pragma once
26 #endif
27 
28 #include "ZipCompatibility.h"
29 
30 namespace ZipArchiveLib
31 {
36  class ZIP_API CBytesWriter
37  {
38  public:
39 
40  #ifndef _ZIP_BIG_ENDIAN
41 
53  static void ReadBytes(WORD& uDestination, const char* pSource, int iCount = 2)
54  {
55  uDestination = 0;
56  memcpy(&uDestination, pSource, iCount);
57  }
58 
59  static void ReadBytes(DWORD& uDestination, const char* pSource, int iCount = 4)
60  {
61  uDestination = 0;
62  memcpy(&uDestination, pSource, iCount);
63  }
64 
65 
66  #ifndef _ZIP_STRICT_U16
67  static void ReadBytes(int& iDestination, const char* pSource, int iCount)
68  {
69  iDestination = 0;
70  memcpy(&iDestination, pSource, iCount);
71  }
72  #endif
73 
74 
75  static void WriteBytes(char* pDestination, WORD uSource)
76  {
77  memcpy(pDestination, &uSource, 2);
78  }
79 
92  static void WriteBytes(char* pDestination, DWORD uSource, int iCount = 4)
93  {
94  memcpy(pDestination, &uSource, iCount);
95  }
96 
97  #ifndef _ZIP_STRICT_U16
98  static void WriteBytes(char* pDestination, int uSource, int iCount)
99  {
100  memcpy(pDestination, &uSource, iCount);
101  }
102  #endif
103 
104  #else
105 
106  static void ReadBytes(char* pDestination, const char* pSource, int iDestSize, int iCount)
107  {
108  int i = iCount - iDestSize;
109  while (i < 0)
110  {
111  *pDestination++ = 0;
112  i++;
113  }
114  for (; i < iCount; i++)
115  (pDestination)[i] = pSource[iCount - i - 1];
116  }
117 
118  static void ReadBytes(WORD& uDestination, const char* pSource, int iCount = 2)
119  {
120  ReadBytes((char*)&uDestination, pSource, 2, iCount);
121  }
122 
123  static void ReadBytes(DWORD& uDestination, const char* pSource, int iCount = 4)
124  {
125  ReadBytes((char*)&uDestination, pSource, 4, iCount);
126  }
127 
128 
129  #ifndef _ZIP_STRICT_U16
130  static void ReadBytes(int& iDestination, const char* pSource, int iCount)
131  {
132  ReadBytes((char*)&iDestination, pSource, sizeof(int), iCount);
133  }
134  #endif
135 
136 
137  static void WriteBytes(char* pDestination, WORD uSource)
138  {
139  for (int i = 0; i < 2; i++)
140  pDestination[i] = ((char*)&uSource)[2 - i - 1];
141  }
142 
143  static void WriteBytes(char* pDestination, DWORD uSource, int iCount = 4)
144  {
145  for (int i = 0; i < iCount; i++)
146  pDestination[i] = ((char*)&uSource)[4 - i - 1];
147  }
148 
149  #ifndef _ZIP_STRICT_U16
150  static void WriteBytes(char* pDestination, int iSource, int iCount)
151  {
152  for (int i = 0; i < iCount; i++)
153  pDestination[i] = ((char*)&iSource)[sizeof(int) - i - 1];
154  }
155  #endif
156 
157  #endif
158 
159  static DWORD WriteSafeU32(DWORD uValue)
160  {
161  return uValue;
162  }
163 
164  #ifdef _ZIP_STRICT_U16
165  static WORD WriteSafeU16(WORD uValue)
166  {
167  return uValue;
168  }
169  #else
170  static WORD WriteSafeU16(int uValue)
171  {
172  return (WORD)uValue;
173  }
174  #endif
175 
176 
177  };
178 }
179 
180 #endif

The ZipArchive Library Copyright © 2000 - 2022 Artpol Software - Tadeusz Dracz. Generated at Sat Dec 17 2022 19:57:03.