aboutsummaryrefslogtreecommitdiffstats
path: root/minzip/Bits.h
blob: f96e6c44369a677f28f17828559d08b021d8dc60 (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * Copyright 2006 The Android Open Source Project
 *
 * Some handy functions for manipulating bits and bytes.
 */
#ifndef _MINZIP_BITS
#define _MINZIP_BITS

#include "inline_magic.h"

#include <stdlib.h>
#include <string.h>

/*
 * Get 1 byte.  (Included to make the code more legible.)
 */
INLINE unsigned char get1(unsigned const char* pSrc)
{
    return *pSrc;
}

/*
 * Get 2 big-endian bytes.
 */
INLINE unsigned short get2BE(unsigned char const* pSrc)
{
    unsigned short result;

    result = *pSrc++ << 8;
    result |= *pSrc++;

    return result;
}

/*
 * Get 4 big-endian bytes.
 */
INLINE unsigned int get4BE(unsigned char const* pSrc)
{
    unsigned int result;

    result = *pSrc++ << 24;
    result |= *pSrc++ << 16;
    result |= *pSrc++ << 8;
    result |= *pSrc++;

    return result;
}

/*
 * Get 8 big-endian bytes.
 */
INLINE unsigned long long get8BE(unsigned char const* pSrc)
{
    unsigned long long result;

    result = (unsigned long long) *pSrc++ << 56;
    result |= (unsigned long long) *pSrc++ << 48;
    result |= (unsigned long long) *pSrc++ << 40;
    result |= (unsigned long long) *pSrc++ << 32;
    result |= (unsigned long long) *pSrc++ << 24;
    result |= (unsigned long long) *pSrc++ << 16;
    result |= (unsigned long long) *pSrc++ << 8;
    result |= (unsigned long long) *pSrc++;

    return result;
}

/*
 * Get 2 little-endian bytes.
 */
INLINE unsigned short get2LE(unsigned char const* pSrc)
{
    unsigned short result;

    result = *pSrc++;
    result |= *pSrc++ << 8;

    return result;
}

/*
 * Get 4 little-endian bytes.
 */
INLINE unsigned int get4LE(unsigned char const* pSrc)
{
    unsigned int result;

    result = *pSrc++;
    result |= *pSrc++ << 8;
    result |= *pSrc++ << 16;
    result |= *pSrc++ << 24;

    return result;
}

/*
 * Get 8 little-endian bytes.
 */
INLINE unsigned long long get8LE(unsigned char const* pSrc)
{
    unsigned long long result;

    result = (unsigned long long) *pSrc++;
    result |= (unsigned long long) *pSrc++ << 8;
    result |= (unsigned long long) *pSrc++ << 16;
    result |= (unsigned long long) *pSrc++ << 24;
    result |= (unsigned long long) *pSrc++ << 32;
    result |= (unsigned long long) *pSrc++ << 40;
    result |= (unsigned long long) *pSrc++ << 48;
    result |= (unsigned long long) *pSrc++ << 56;

    return result;
}

/*
 * Grab 1 byte and advance the data pointer.
 */
INLINE unsigned char read1(unsigned const char** ppSrc)
{
    return *(*ppSrc)++;
}

/*
 * Grab 2 big-endian bytes and advance the data pointer.
 */
INLINE unsigned short read2BE(unsigned char const** ppSrc)
{
    unsigned short result;

    result = *(*ppSrc)++ << 8;
    result |= *(*ppSrc)++;

    return result;
}

/*
 * Grab 4 big-endian bytes and advance the data pointer.
 */
INLINE unsigned int read4BE(unsigned char const** ppSrc)
{
    unsigned int result;

    result = *(*ppSrc)++ << 24;
    result |= *(*ppSrc)++ << 16;
    result |= *(*ppSrc)++ << 8;
    result |= *(*ppSrc)++;

    return result;
}

/*
 * Get 8 big-endian bytes.
 */
INLINE unsigned long long read8BE(unsigned char const** ppSrc)
{
    unsigned long long result;

    result = (unsigned long long) *(*ppSrc)++ << 56;
    result |= (unsigned long long) *(*ppSrc)++ << 48;
    result |= (unsigned long long) *(*ppSrc)++ << 40;
    result |= (unsigned long long) *(*ppSrc)++ << 32;
    result |= (unsigned long long) *(*ppSrc)++ << 24;
    result |= (unsigned long long) *(*ppSrc)++ << 16;
    result |= (unsigned long long) *(*ppSrc)++ << 8;
    result |= (unsigned long long) *(*ppSrc)++;

    return result;
}

/*
 * Grab 2 little-endian bytes and advance the data pointer.
 */
INLINE unsigned short read2LE(unsigned char const** ppSrc)
{
    unsigned short result;

    result = *(*ppSrc)++;
    result |= *(*ppSrc)++ << 8;

    return result;
}

/*
 * Grab 4 little-endian bytes and advance the data pointer.
 */
INLINE unsigned int read4LE(unsigned char const** ppSrc)
{
    unsigned int result;

    result = *(*ppSrc)++;
    result |= *(*ppSrc)++ << 8;
    result |= *(*ppSrc)++ << 16;
    result |= *(*ppSrc)++ << 24;

    return result;
}

/*
 * Get 8 little-endian bytes.
 */
INLINE unsigned long long read8LE(unsigned char const** ppSrc)
{
    unsigned long long result;

    result = (unsigned long long) *(*ppSrc)++;
    result |= (unsigned long long) *(*ppSrc)++ << 8;
    result |= (unsigned long long) *(*ppSrc)++ << 16;
    result |= (unsigned long long) *(*ppSrc)++ << 24;
    result |= (unsigned long long) *(*ppSrc)++ << 32;
    result |= (unsigned long long) *(*ppSrc)++ << 40;
    result |= (unsigned long long) *(*ppSrc)++ << 48;
    result |= (unsigned long long) *(*ppSrc)++ << 56;

    return result;
}

/*
 * Skip over a UTF-8 string.
 */
INLINE void skipUtf8String(unsigned char const** ppSrc)
{
    unsigned int length = read4BE(ppSrc);

    (*ppSrc) += length;
}

/*
 * Read a UTF-8 string into a fixed-size buffer, and null-terminate it.
 *
 * Returns the length of the original string.
 */
INLINE int readUtf8String(unsigned char const** ppSrc, char* buf, size_t bufLen)
{
    unsigned int length = read4BE(ppSrc);
    size_t copyLen = (length < bufLen) ? length : bufLen-1;

    memcpy(buf, *ppSrc, copyLen);
    buf[copyLen] = '\0';

    (*ppSrc) += length;
    return length;
}

/*
 * Read a UTF-8 string into newly-allocated storage, and null-terminate it.
 *
 * Returns the string and its length.  (The latter is probably unnecessary
 * for the way we're using UTF8.)
 */
INLINE char* readNewUtf8String(unsigned char const** ppSrc, size_t* pLength)
{
    unsigned int length = read4BE(ppSrc);
    char* buf;

    buf = (char*) malloc(length+1);

    memcpy(buf, *ppSrc, length);
    buf[length] = '\0';

    (*ppSrc) += length;

    *pLength = length;
    return buf;
}


/*
 * Set 1 byte.  (Included to make the code more legible.)
 */
INLINE void set1(unsigned char* buf, unsigned char val)
{
    *buf = (unsigned char)(val);
}

/*
 * Set 2 big-endian bytes.
 */
INLINE void set2BE(unsigned char* buf, unsigned short val)
{
    *buf++ = (unsigned char)(val >> 8);
    *buf = (unsigned char)(val);
}

/*
 * Set 4 big-endian bytes.
 */
INLINE void set4BE(unsigned char* buf, unsigned int val)
{
    *buf++ = (unsigned char)(val >> 24);
    *buf++ = (unsigned char)(val >> 16);
    *buf++ = (unsigned char)(val >> 8);
    *buf = (unsigned char)(val);
}

/*
 * Set 8 big-endian bytes.
 */
INLINE void set8BE(unsigned char* buf, unsigned long long val)
{
    *buf++ = (unsigned char)(val >> 56);
    *buf++ = (unsigned char)(val >> 48);
    *buf++ = (unsigned char)(val >> 40);
    *buf++ = (unsigned char)(val >> 32);
    *buf++ = (unsigned char)(val >> 24);
    *buf++ = (unsigned char)(val >> 16);
    *buf++ = (unsigned char)(val >> 8);
    *buf = (unsigned char)(val);
}

/*
 * Set 2 little-endian bytes.
 */
INLINE void set2LE(unsigned char* buf, unsigned short val)
{
    *buf++ = (unsigned char)(val);
    *buf = (unsigned char)(val >> 8);
}

/*
 * Set 4 little-endian bytes.
 */
INLINE void set4LE(unsigned char* buf, unsigned int val)
{
    *buf++ = (unsigned char)(val);
    *buf++ = (unsigned char)(val >> 8);
    *buf++ = (unsigned char)(val >> 16);
    *buf = (unsigned char)(val >> 24);
}

/*
 * Set 8 little-endian bytes.
 */
INLINE void set8LE(unsigned char* buf, unsigned long long val)
{
    *buf++ = (unsigned char)(val);
    *buf++ = (unsigned char)(val >> 8);
    *buf++ = (unsigned char)(val >> 16);
    *buf++ = (unsigned char)(val >> 24);
    *buf++ = (unsigned char)(val >> 32);
    *buf++ = (unsigned char)(val >> 40);
    *buf++ = (unsigned char)(val >> 48);
    *buf = (unsigned char)(val >> 56);
}

/*
 * Stuff a UTF-8 string into the buffer.
 */
INLINE void setUtf8String(unsigned char* buf, const unsigned char* str)
{
    unsigned int strLen = strlen((const char*)str);

    set4BE(buf, strLen);
    memcpy(buf + sizeof(unsigned int), str, strLen);
}

#endif /*_MINZIP_BITS*/