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; }
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 ){
56 register unsigned char *p, *q;
58 p = ( unsigned char * ) bp;
87 void *getbytes( picoMemStream_t *fp, int size ){
90 if ( flen == FLEN_ERROR ) {
97 data = _pico_alloc( size );
102 if ( 1 != _pico_memstream_read( fp, data, size ) ) {
113 void skipbytes( picoMemStream_t *fp, int n ){
114 if ( flen == FLEN_ERROR ) {
117 if ( _pico_memstream_seek( fp, n, PICO_SEEK_CUR ) ) {
126 int getI1( picoMemStream_t *fp ){
129 if ( flen == FLEN_ERROR ) {
132 i = _pico_memstream_getc( fp );
145 short getI2( picoMemStream_t *fp ){
148 if ( flen == FLEN_ERROR ) {
151 if ( 1 != _pico_memstream_read( fp, &i, 2 ) ) {
155 revbytes( &i, 2, 1 );
161 int getI4( picoMemStream_t *fp ){
164 if ( flen == FLEN_ERROR ) {
167 if ( 1 != _pico_memstream_read( fp, &i, 4 ) ) {
171 revbytes( &i, 4, 1 );
177 unsigned char getU1( picoMemStream_t *fp ){
180 if ( flen == FLEN_ERROR ) {
183 i = _pico_memstream_getc( fp );
193 unsigned short getU2( picoMemStream_t *fp ){
196 if ( flen == FLEN_ERROR ) {
199 if ( 1 != _pico_memstream_read( fp, &i, 2 ) ) {
203 revbytes( &i, 2, 1 );
209 unsigned int getU4( picoMemStream_t *fp ){
212 if ( flen == FLEN_ERROR ) {
215 if ( 1 != _pico_memstream_read( fp, &i, 4 ) ) {
219 revbytes( &i, 4, 1 );
225 int getVX( picoMemStream_t *fp ){
228 if ( flen == FLEN_ERROR ) {
232 c = _pico_memstream_getc( fp );
235 c = _pico_memstream_getc( fp );
240 c = _pico_memstream_getc( fp );
242 c = _pico_memstream_getc( fp );
244 c = _pico_memstream_getc( fp );
249 if ( _pico_memstream_error( fp ) ) {
257 float getF4( picoMemStream_t *fp ){
260 if ( flen == FLEN_ERROR ) {
263 if ( 1 != _pico_memstream_read( fp, &f, 4 ) ) {
267 revbytes( &f, 4, 1 );
273 char *getS0( picoMemStream_t *fp ){
277 if ( flen == FLEN_ERROR ) {
281 pos = _pico_memstream_tell( fp );
282 for ( i = 1; ; i++ ) {
283 c = _pico_memstream_getc( fp );
294 if ( _pico_memstream_seek( fp, pos + 2, PICO_SEEK_SET ) ) {
304 s = _pico_alloc( len );
310 if ( _pico_memstream_seek( fp, pos, PICO_SEEK_SET ) ) {
314 if ( 1 != _pico_memstream_read( fp, s, len ) ) {
324 int sgetI1( unsigned char **bp ){
327 if ( flen == FLEN_ERROR ) {
340 short sgetI2( unsigned char **bp ){
343 if ( flen == FLEN_ERROR ) {
346 memcpy( &i, *bp, 2 );
347 revbytes( &i, 2, 1 );
354 int sgetI4( unsigned char **bp ){
357 if ( flen == FLEN_ERROR ) {
360 memcpy( &i, *bp, 4 );
361 revbytes( &i, 4, 1 );
368 unsigned char sgetU1( unsigned char **bp ){
371 if ( flen == FLEN_ERROR ) {
381 unsigned short sgetU2( unsigned char **bp ){
382 unsigned char *buf = *bp;
385 if ( flen == FLEN_ERROR ) {
388 i = ( buf[ 0 ] << 8 ) | buf[ 1 ];
395 unsigned int sgetU4( unsigned char **bp ){
398 if ( flen == FLEN_ERROR ) {
401 memcpy( &i, *bp, 4 );
402 revbytes( &i, 4, 1 );
409 int sgetVX( unsigned char **bp ){
410 unsigned char *buf = *bp;
413 if ( flen == FLEN_ERROR ) {
417 if ( buf[ 0 ] != 0xFF ) {
418 i = buf[ 0 ] << 8 | buf[ 1 ];
423 i = ( buf[ 1 ] << 16 ) | ( buf[ 2 ] << 8 ) | buf[ 3 ];
431 float sgetF4( unsigned char **bp ){
434 if ( flen == FLEN_ERROR ) {
437 memcpy( &f, *bp, 4 );
438 revbytes( &f, 4, 1 );
445 char *sgetS0( unsigned char **bp ){
447 unsigned char *buf = *bp;
450 if ( flen == FLEN_ERROR ) {
454 len = strlen( buf ) + 1;
461 s = _pico_alloc( len );
467 memcpy( s, buf, len );