StreamFunctions.h (1670B)
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 inline void WRITE8(uint8_t* buf, uint32_t* off, uint8_t val) { 20 buf[(*off)++] = val; 21 } 22 23 inline void WRITE16(uint8_t* buf, uint32_t* off, uint16_t val) { 24 WRITE8(buf, off, val & 0xff); 25 WRITE8(buf, off, (val >> 8) & 0xff); 26 } 27 28 inline void WRITE32(uint8_t* buf, uint32_t* off, uint32_t val) { 29 WRITE16(buf, off, val & 0xffff); 30 WRITE16(buf, off, (val >> 16) & 0xffff); 31 } 32 33 inline uint8_t READ8(const uint8_t* buf, uint32_t* off) { 34 return buf[(*off)++]; 35 } 36 37 inline uint16_t READ16(const uint8_t* buf, uint32_t* off) { 38 uint16_t val = READ8(buf, off); 39 val |= READ8(buf, off) << 8; 40 return val; 41 } 42 43 inline uint32_t READ32(const uint8_t* buf, uint32_t* off) { 44 uint32_t val = READ16(buf, off); 45 val |= READ16(buf, off) << 16; 46 return val; 47 } 48 49 inline uint32_t PEEK32(const uint8_t* buf) { 50 return (uint32_t)((buf[0]) | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)); 51 } 52 53 nsresult ZW_ReadData(nsIInputStream* aStream, char* aBuffer, uint32_t aCount); 54 55 nsresult ZW_WriteData(nsIOutputStream* aStream, const char* aBuffer, 56 uint32_t aCount); 57 58 #endif