File: BytesWriter.h

package info (click to toggle)
tuxcmd-modules 0.6.70%2Bds-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,872 kB
  • sloc: cpp: 11,743; ansic: 8,064; makefile: 245
file content (180 lines) | stat: -rw-r--r-- 4,365 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2007 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////

/**
* \file BytesWriter.h
*	Includes the ZipArchiveLib::CBytesWriter class.
*
*/

#if !defined(ZIPARCHIVE_BYTESWRITER_DOT_H)
#define ZIPARCHIVE_BYTESWRITER_DOT_H

#if _MSC_VER > 1000
	#pragma once
#endif

#include "ZipCompatibility.h"

namespace ZipArchiveLib
{
	/**
		Provides implementation for various 
		buffer operations depending on the current platform and configuration.
	 */
	class ZIP_API CBytesWriter
	{
	public:
		
	#ifdef ZIP_ARCHIVE_LITTLE_ENDIAN
		/**
			Reads \a iCount bytes from \a pSource into \a pDestination.

			\param[out] pDestination
				The buffer to retrieve data with byte-ordering depending on the machine.

			\param[in] pSource
				The buffer with little-endian ordered data.

			\param iCount 
				The number of bytes to read.
		*/
		static void ReadBytes(WORD& uDestination, const char* pSource, int iCount = 2)
		{
			uDestination = 0;
			memcpy(&uDestination, pSource, iCount);
		}

		static void ReadBytes(DWORD& uDestination, const char* pSource, int iCount = 4)
		{
			uDestination = 0;
			memcpy(&uDestination, pSource, iCount);
		}


		#ifndef _ZIP_STRICT_U16
		static void ReadBytes(int& iDestination, const char* pSource, int iCount)
		{
			iDestination = 0;
			memcpy(&iDestination, pSource, iCount);
		}
		#endif


		/**
			Writes \a iCount bytes from \a pSource into \a pDestination.

			\param[out] pDestination
				The buffer to retrieve little-endian ordered data.

			\param[in] pSource
				The buffer with byte-ordering depending on the machine.

			\param iCount 
				The number of bytes to write.
		*/
		static void WriteBytes(char* pDestination, WORD uSource)
		{
			memcpy(pDestination, &uSource, 2);
		}

		static void WriteBytes(char* pDestination, DWORD uSource, int iCount = 4)
		{
			memcpy(pDestination, &uSource, iCount);
		}

		#ifndef _ZIP_STRICT_U16
		static void WriteBytes(char* pDestination, int uSource, int iCount)
		{
			memcpy(pDestination, &uSource, iCount);
		}
		#endif

	#else

		static void ReadBytes(char* pDestination, const char* pSource, int iDestSize, int iCount)
		{
			int i = iCount - iDestSize;
			while (i < 0)
			{
				*pDestination++ = 0;
				i++;
			}
			for (; i < iCount; i++)
				(pDestination)[i] = pSource[iCount - i - 1];
		}
		
		static void ReadBytes(WORD& uDestination, const char* pSource, int iCount = 2)
		{
			ReadBytes((char*)&uDestination, pSource, 2, iCount);
		}

		static void ReadBytes(DWORD& uDestination, const char* pSource, int iCount = 4)
		{
			ReadBytes((char*)&uDestination, pSource, 4, iCount);
		}


		#ifndef _ZIP_STRICT_U16
		static void ReadBytes(int& iDestination, const char* pSource, int iCount)
		{
			ReadBytes((char*)&iDestination, pSource, sizeof(int), iCount);
		}
		#endif


		static void WriteBytes(char* pDestination, WORD uSource)
		{
			for (int i = 0; i < 2; i++)
				pDestination[i] = ((char*)&uSource)[2 - i - 1];
		}

		static void WriteBytes(char* pDestination, DWORD uSource, int iCount = 4)
		{
			for (int i = 0; i < iCount; i++)
				pDestination[i] = ((char*)&uSource)[4 - i - 1];
		}

		#ifndef _ZIP_STRICT_U16
		static void WriteBytes(char* pDestination, int iSource, int iCount)
		{
			for (int i = 0; i < iCount; i++)
				pDestination[i] = ((char*)&iSource)[sizeof(int) - i - 1];
		}
		#endif

	#endif
		
		static DWORD WriteSafeU32(DWORD uValue)
		{
			return uValue;
		}

	#ifdef _ZIP_STRICT_U16
		static WORD WriteSafeU16(WORD uValue)
		{
			return uValue;
		}
	#else
		static WORD WriteSafeU16(int uValue)
		{
			return (WORD)uValue;
		}
	#endif


	};
}

#endif