Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this
3 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 : */
5 :
6 : #ifndef _nsStreamFunctions_h_
7 : #define _nsStreamFunctions_h_
8 :
9 : #include "nscore.h"
10 : #include "nsIInputStream.h"
11 : #include "nsIOutputStream.h"
12 :
13 : /*
14 : * ZIP file data is stored little-endian. These are helper functions to read and
15 : * write little endian data to/from a char buffer.
16 : * The off argument, where present, is incremented according to the number of
17 : * bytes consumed from the buffer.
18 : */
19 0 : inline void WRITE8(uint8_t* buf, uint32_t* off, uint8_t val)
20 : {
21 0 : buf[(*off)++] = val;
22 0 : }
23 :
24 0 : inline void WRITE16(uint8_t* buf, uint32_t* off, uint16_t val)
25 : {
26 0 : WRITE8(buf, off, val & 0xff);
27 0 : WRITE8(buf, off, (val >> 8) & 0xff);
28 0 : }
29 :
30 0 : inline void WRITE32(uint8_t* buf, uint32_t* off, uint32_t val)
31 : {
32 0 : WRITE16(buf, off, val & 0xffff);
33 0 : WRITE16(buf, off, (val >> 16) & 0xffff);
34 0 : }
35 :
36 0 : inline uint8_t READ8(const uint8_t* buf, uint32_t* off)
37 : {
38 0 : return buf[(*off)++];
39 : }
40 :
41 0 : inline uint16_t READ16(const uint8_t* buf, uint32_t* off)
42 : {
43 0 : uint16_t val = READ8(buf, off);
44 0 : val |= READ8(buf, off) << 8;
45 0 : return val;
46 : }
47 :
48 0 : inline uint32_t READ32(const uint8_t* buf, uint32_t* off)
49 : {
50 0 : uint32_t val = READ16(buf, off);
51 0 : val |= READ16(buf, off) << 16;
52 0 : return val;
53 : }
54 :
55 0 : inline uint32_t PEEK32(const uint8_t* buf)
56 : {
57 0 : return (uint32_t)( (buf [0] ) |
58 0 : (buf [1] << 8) |
59 0 : (buf [2] << 16) |
60 0 : (buf [3] << 24) );
61 : }
62 :
63 : nsresult ZW_ReadData(nsIInputStream *aStream, char *aBuffer, uint32_t aCount);
64 :
65 : nsresult ZW_WriteData(nsIOutputStream *aStream, const char *aBuffer,
66 : uint32_t aCount);
67 :
68 : #endif
|