PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 0ms app.codeStats 0ms

/XADMaster.framework/Versions/A/Headers/CSInputBuffer.h

https://code.google.com/p/xee/
C++ Header | 195 lines | 135 code | 55 blank | 5 comment | 8 complexity | 769bbf096c3a90f4f2e78c1c7cd68a3b MD5 | raw file
  1#import <Foundation/Foundation.h>
  2#import "CSHandle.h"
  3
  4typedef struct CSInputBuffer
  5{
  6	CSHandle *parent;
  7	off_t startoffs;
  8	BOOL eof;
  9
 10	uint8_t *buffer;
 11	unsigned int bufsize,bufbytes,currbyte;
 12
 13	uint32_t bits;
 14	unsigned int numbits;
 15} CSInputBuffer;
 16
 17
 18
 19// Allocation and management
 20
 21CSInputBuffer *CSInputBufferAlloc(CSHandle *parent,int size);
 22CSInputBuffer *CSInputBufferAllocWithBuffer(const uint8_t *buffer,int length,off_t startoffs);
 23CSInputBuffer *CSInputBufferAllocEmpty();
 24void CSInputBufferFree(CSInputBuffer *self);
 25
 26void CSInputSetMemoryBuffer(CSInputBuffer *self,uint8_t *buffer,int length,off_t startoffs);
 27
 28
 29
 30// Buffer and file positioning
 31
 32void CSInputRestart(CSInputBuffer *self);
 33void CSInputFlush(CSInputBuffer *self);
 34
 35void CSInputSynchronizeFileOffset(CSInputBuffer *self);
 36void CSInputSeekToFileOffset(CSInputBuffer *self,off_t offset);
 37void CSInputSeekToBufferOffset(CSInputBuffer *self,off_t offset);
 38void CSInputSetStartOffset(CSInputBuffer *self,off_t offset);
 39off_t CSInputBufferOffset(CSInputBuffer *self);
 40off_t CSInputFileOffset(CSInputBuffer *self);
 41off_t CSInputBufferBitOffset(CSInputBuffer *self);
 42
 43void _CSInputFillBuffer(CSInputBuffer *self);
 44
 45
 46
 47
 48// Byte reading
 49
 50#define CSInputBufferLookAhead 4
 51
 52static inline void _CSInputBufferRaiseEOF(CSInputBuffer *self)
 53{
 54	[self->parent _raiseEOF];
 55}
 56
 57static inline int _CSInputBytesLeftInBuffer(CSInputBuffer *self)
 58{
 59	return self->bufbytes-self->currbyte;
 60}
 61
 62static inline void _CSInputCheckAndFillBuffer(CSInputBuffer *self)
 63{
 64	if(!self->eof&&_CSInputBytesLeftInBuffer(self)<=CSInputBufferLookAhead) _CSInputFillBuffer(self);
 65}
 66
 67static inline void CSInputSkipBytes(CSInputBuffer *self,int num)
 68{
 69	self->currbyte+=num;
 70}
 71
 72static inline int _CSInputPeekByteWithoutEOF(CSInputBuffer *self,int offs)
 73{
 74	return self->buffer[self->currbyte+offs];
 75}
 76
 77static inline int CSInputPeekByte(CSInputBuffer *self,int offs)
 78{
 79	_CSInputCheckAndFillBuffer(self);
 80	if(offs>=_CSInputBytesLeftInBuffer(self)) _CSInputBufferRaiseEOF(self);
 81	return _CSInputPeekByteWithoutEOF(self,offs);
 82}
 83
 84static inline int CSInputNextByte(CSInputBuffer *self)
 85{
 86	int byte=CSInputPeekByte(self,0);
 87	CSInputSkipBytes(self,1);
 88	return byte;
 89}
 90
 91static inline BOOL CSInputAtEOF(CSInputBuffer *self)
 92{
 93	_CSInputCheckAndFillBuffer(self);
 94	return _CSInputBytesLeftInBuffer(self)<=0;
 95}
 96
 97
 98
 99
100// Bitstream reading
101
102void _CSInputFillBits(CSInputBuffer *self);
103void _CSInputFillBitsLE(CSInputBuffer *self);
104
105unsigned int CSInputNextBit(CSInputBuffer *self);
106unsigned int CSInputNextBitLE(CSInputBuffer *self);
107unsigned int CSInputNextBitString(CSInputBuffer *self,int numbits);
108unsigned int CSInputNextBitStringLE(CSInputBuffer *self,int numbits);
109unsigned int CSInputNextLongBitString(CSInputBuffer *self,int numbits);
110unsigned int CSInputNextLongBitStringLE(CSInputBuffer *self,int numbits);
111
112void CSInputSkipBits(CSInputBuffer *self,int numbits);
113void CSInputSkipBitsLE(CSInputBuffer *self,int numbits);
114BOOL CSInputOnByteBoundary(CSInputBuffer *self);
115void CSInputSkipToByteBoundary(CSInputBuffer *self);
116void CSInputSkipTo16BitBoundary(CSInputBuffer *self);
117
118static inline unsigned int CSInputBitsLeftInBuffer(CSInputBuffer *self)
119{
120	_CSInputCheckAndFillBuffer(self);
121	return _CSInputBytesLeftInBuffer(self)*8+(self->numbits&7);
122}
123
124static inline void _CSInputCheckAndFillBits(CSInputBuffer *self,int numbits)
125{
126	if(numbits>self->numbits) _CSInputFillBits(self);
127}
128
129static inline void _CSInputCheckAndFillBitsLE(CSInputBuffer *self,int numbits)
130{
131	if(numbits>self->numbits) _CSInputFillBitsLE(self);
132}
133
134static inline unsigned int CSInputPeekBitString(CSInputBuffer *self,int numbits)
135{
136	if(numbits==0) return 0;
137	_CSInputCheckAndFillBits(self,numbits);
138	return self->bits>>(32-numbits);
139}
140
141static inline unsigned int CSInputPeekBitStringLE(CSInputBuffer *self,int numbits)
142{
143	if(numbits==0) return 0;
144	_CSInputCheckAndFillBitsLE(self,numbits);
145	return self->bits&((1<<numbits)-1);
146}
147
148static inline void CSInputSkipPeekedBits(CSInputBuffer *self,int numbits)
149{
150	int numbytes=(numbits-(self->numbits&7)+7)>>3;
151	CSInputSkipBytes(self,numbytes);
152
153	if(_CSInputBytesLeftInBuffer(self)<0) _CSInputBufferRaiseEOF(self);
154
155	self->bits<<=numbits;
156	self->numbits-=numbits;
157}
158
159static inline void CSInputSkipPeekedBitsLE(CSInputBuffer *self,int numbits)
160{
161	int numbytes=(numbits-(self->numbits&7)+7)>>3;
162	CSInputSkipBytes(self,numbytes);
163
164	if(_CSInputBytesLeftInBuffer(self)<0) _CSInputBufferRaiseEOF(self);
165
166	self->bits>>=numbits;
167	self->numbits-=numbits;
168}
169
170
171
172
173// Multibyte reading
174
175#define CSInputNextValueImpl(type,name,conv) \
176static inline type name(CSInputBuffer *self) \
177{ \
178	_CSInputCheckAndFillBuffer(self); \
179	type val=conv(self->buffer+self->currbyte); \
180	CSInputSkipBytes(self,sizeof(type)); \
181	return val; \
182}
183
184CSInputNextValueImpl(int16_t,CSInputNextInt16LE,CSInt16LE)
185CSInputNextValueImpl(int32_t,CSInputNextInt32LE,CSInt32LE)
186CSInputNextValueImpl(uint16_t,CSInputNextUInt16LE,CSUInt16LE)
187CSInputNextValueImpl(uint32_t,CSInputNextUInt32LE,CSUInt32LE)
188CSInputNextValueImpl(int16_t,CSInputNextInt16BE,CSInt16BE)
189CSInputNextValueImpl(int32_t,CSInputNextInt32BE,CSInt32BE)
190CSInputNextValueImpl(uint16_t,CSInputNextUInt16BE,CSUInt16BE)
191CSInputNextValueImpl(uint32_t,CSInputNextUInt32BE,CSUInt32BE)
192
193
194
195