2 ======================================================================
5 Functions for reading basic LWO2 data types.
8 ====================================================================== */
10 #include "../picointernal.h"
15 ======================================================================
18 This accumulates a count of the number of bytes read. Callers can set
19 it at the beginning of a sequence of reads and then retrieve it to get
20 the number of bytes actually read. If one of the I/O functions fails,
21 flen is set to an error code, after which the I/O functions ignore
22 read requests until flen is reset.
23 ====================================================================== */
25 #define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */
26 #define FLEN_ERROR INT_MIN
30 void set_flen( int i ) { flen = i; }
32 int get_flen( void ) { return flen; }
35 #ifndef __BIG_ENDIAN__
37 =====================================================================
40 Reverses byte order in place.
44 elsize size of the underlying data type
45 elcount number of elements to swap
48 Reverses the byte order in each of elcount elements.
50 This only needs to be defined on little-endian platforms, most
51 notably Windows. lwo2.h replaces this with a #define on big-endian
53 ===================================================================== */
55 void revbytes( void *bp, int elsize, int elcount )
57 register unsigned char *p, *q;
59 p = ( unsigned char * ) bp;
88 void *getbytes( picoMemStream_t *fp, int size )
92 if ( flen == FLEN_ERROR ) return NULL;
97 data = _pico_alloc( size );
102 if ( 1 != _pico_memstream_read( fp, data, size )) {
113 void skipbytes( picoMemStream_t *fp, int n )
115 if ( flen == FLEN_ERROR ) return;
116 if ( _pico_memstream_seek( fp, n, PICO_SEEK_CUR ))
123 int getI1( picoMemStream_t *fp )
127 if ( flen == FLEN_ERROR ) return 0;
128 i = _pico_memstream_getc( fp );
133 if ( i > 127 ) i -= 256;
139 short getI2( picoMemStream_t *fp )
143 if ( flen == FLEN_ERROR ) return 0;
144 if ( 1 != _pico_memstream_read( fp, &i, 2 )) {
148 revbytes( &i, 2, 1 );
154 int getI4( picoMemStream_t *fp )
158 if ( flen == FLEN_ERROR ) return 0;
159 if ( 1 != _pico_memstream_read( fp, &i, 4 )) {
163 revbytes( &i, 4, 1 );
169 unsigned char getU1( picoMemStream_t *fp )
173 if ( flen == FLEN_ERROR ) return 0;
174 i = _pico_memstream_getc( fp );
184 unsigned short getU2( picoMemStream_t *fp )
188 if ( flen == FLEN_ERROR ) return 0;
189 if ( 1 != _pico_memstream_read( fp, &i, 2 )) {
193 revbytes( &i, 2, 1 );
199 unsigned int getU4( picoMemStream_t *fp )
203 if ( flen == FLEN_ERROR ) return 0;
204 if ( 1 != _pico_memstream_read( fp, &i, 4 )) {
208 revbytes( &i, 4, 1 );
214 int getVX( picoMemStream_t *fp )
218 if ( flen == FLEN_ERROR ) return 0;
220 c = _pico_memstream_getc( fp );
223 c = _pico_memstream_getc( fp );
228 c = _pico_memstream_getc( fp );
230 c = _pico_memstream_getc( fp );
232 c = _pico_memstream_getc( fp );
237 if ( _pico_memstream_error( fp )) {
245 float getF4( picoMemStream_t *fp )
249 if ( flen == FLEN_ERROR ) return 0.0f;
250 if ( 1 != _pico_memstream_read( fp, &f, 4 )) {
254 revbytes( &f, 4, 1 );
260 char *getS0( picoMemStream_t *fp )
265 if ( flen == FLEN_ERROR ) return NULL;
267 pos = _pico_memstream_tell( fp );
268 for ( i = 1; ; i++ ) {
269 c = _pico_memstream_getc( fp );
278 if ( _pico_memstream_seek( fp, pos + 2, PICO_SEEK_SET ))
286 s = _pico_alloc( len );
292 if ( _pico_memstream_seek( fp, pos, PICO_SEEK_SET )) {
296 if ( 1 != _pico_memstream_read( fp, s, len )) {
306 int sgetI1( unsigned char **bp )
310 if ( flen == FLEN_ERROR ) return 0;
312 if ( i > 127 ) i -= 256;
319 short sgetI2( unsigned char **bp )
323 if ( flen == FLEN_ERROR ) return 0;
324 memcpy( &i, *bp, 2 );
325 revbytes( &i, 2, 1 );
332 int sgetI4( unsigned char **bp )
336 if ( flen == FLEN_ERROR ) return 0;
337 memcpy( &i, *bp, 4 );
338 revbytes( &i, 4, 1 );
345 unsigned char sgetU1( unsigned char **bp )
349 if ( flen == FLEN_ERROR ) return 0;
357 unsigned short sgetU2( unsigned char **bp )
359 unsigned char *buf = *bp;
362 if ( flen == FLEN_ERROR ) return 0;
363 i = ( buf[ 0 ] << 8 ) | buf[ 1 ];
370 unsigned int sgetU4( unsigned char **bp )
374 if ( flen == FLEN_ERROR ) return 0;
375 memcpy( &i, *bp, 4 );
376 revbytes( &i, 4, 1 );
383 int sgetVX( unsigned char **bp )
385 unsigned char *buf = *bp;
388 if ( flen == FLEN_ERROR ) return 0;
390 if ( buf[ 0 ] != 0xFF ) {
391 i = buf[ 0 ] << 8 | buf[ 1 ];
396 i = ( buf[ 1 ] << 16 ) | ( buf[ 2 ] << 8 ) | buf[ 3 ];
404 float sgetF4( unsigned char **bp )
408 if ( flen == FLEN_ERROR ) return 0.0f;
409 memcpy( &f, *bp, 4 );
410 revbytes( &f, 4, 1 );
417 char *sgetS0( unsigned char **bp )
420 unsigned char *buf = *bp;
423 if ( flen == FLEN_ERROR ) return NULL;
425 len = strlen( buf ) + 1;
432 s = _pico_alloc( len );
438 memcpy( s, buf, len );