2 ======================================================================
5 Functions for reading basic LWO2 data types.
8 ====================================================================== */
10 #include "../picointernal.h"
13 #include "globaldefs.h"
17 ======================================================================
20 This accumulates a count of the number of bytes read. Callers can set
21 it at the beginning of a sequence of reads and then retrieve it to get
22 the number of bytes actually read. If one of the I/O functions fails,
23 flen is set to an error code, after which the I/O functions ignore
24 read requests until flen is reset.
25 ====================================================================== */
27 const int FLEN_ERROR = INT_MIN;
31 void set_flen( int i ) { flen = i; }
33 int get_flen( void ) { return flen; }
36 #if !GDEF_ARCH_ENDIAN_BIG
38 =====================================================================
41 Reverses byte order in place.
45 elsize size of the underlying data type
46 elcount number of elements to swap
49 Reverses the byte order in each of elcount elements.
51 This only needs to be defined on little-endian platforms, most
52 notably Windows. lwo2.h replaces this with a #define on big-endian
54 ===================================================================== */
56 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 ){
91 if ( flen == FLEN_ERROR ) {
98 data = _pico_alloc( size );
103 if ( 1 != _pico_memstream_read( fp, data, size ) ) {
114 void skipbytes( picoMemStream_t *fp, int n ){
115 if ( flen == FLEN_ERROR ) {
118 if ( _pico_memstream_seek( fp, n, PICO_SEEK_CUR ) ) {
127 int getI1( picoMemStream_t *fp ){
130 if ( flen == FLEN_ERROR ) {
133 i = _pico_memstream_getc( fp );
146 short getI2( picoMemStream_t *fp ){
149 if ( flen == FLEN_ERROR ) {
152 if ( 1 != _pico_memstream_read( fp, &i, 2 ) ) {
156 revbytes( &i, 2, 1 );
162 int getI4( picoMemStream_t *fp ){
165 if ( flen == FLEN_ERROR ) {
168 if ( 1 != _pico_memstream_read( fp, &i, 4 ) ) {
172 revbytes( &i, 4, 1 );
178 unsigned char getU1( picoMemStream_t *fp ){
181 if ( flen == FLEN_ERROR ) {
184 i = _pico_memstream_getc( fp );
194 unsigned short getU2( picoMemStream_t *fp ){
197 if ( flen == FLEN_ERROR ) {
200 if ( 1 != _pico_memstream_read( fp, &i, 2 ) ) {
204 revbytes( &i, 2, 1 );
210 unsigned int getU4( picoMemStream_t *fp ){
213 if ( flen == FLEN_ERROR ) {
216 if ( 1 != _pico_memstream_read( fp, &i, 4 ) ) {
220 revbytes( &i, 4, 1 );
226 int getVX( picoMemStream_t *fp ){
229 if ( flen == FLEN_ERROR ) {
233 c = _pico_memstream_getc( fp );
236 c = _pico_memstream_getc( fp );
241 c = _pico_memstream_getc( fp );
243 c = _pico_memstream_getc( fp );
245 c = _pico_memstream_getc( fp );
250 if ( _pico_memstream_error( fp ) ) {
258 float getF4( picoMemStream_t *fp ){
261 if ( flen == FLEN_ERROR ) {
264 if ( 1 != _pico_memstream_read( fp, &f, 4 ) ) {
268 revbytes( &f, 4, 1 );
274 char *getS0( picoMemStream_t *fp ){
278 if ( flen == FLEN_ERROR ) {
282 pos = _pico_memstream_tell( fp );
283 for ( i = 1; ; i++ ) {
284 c = _pico_memstream_getc( fp );
295 if ( _pico_memstream_seek( fp, pos + 2, PICO_SEEK_SET ) ) {
305 s = _pico_alloc( len );
311 if ( _pico_memstream_seek( fp, pos, PICO_SEEK_SET ) ) {
315 if ( 1 != _pico_memstream_read( fp, s, len ) ) {
325 int sgetI1( unsigned char **bp ){
328 if ( flen == FLEN_ERROR ) {
341 short sgetI2( unsigned char **bp ){
344 if ( flen == FLEN_ERROR ) {
347 memcpy( &i, *bp, 2 );
348 revbytes( &i, 2, 1 );
355 int sgetI4( unsigned char **bp ){
358 if ( flen == FLEN_ERROR ) {
361 memcpy( &i, *bp, 4 );
362 revbytes( &i, 4, 1 );
369 unsigned char sgetU1( unsigned char **bp ){
372 if ( flen == FLEN_ERROR ) {
382 unsigned short sgetU2( unsigned char **bp ){
383 unsigned char *buf = *bp;
386 if ( flen == FLEN_ERROR ) {
389 i = ( buf[ 0 ] << 8 ) | buf[ 1 ];
396 unsigned int sgetU4( unsigned char **bp ){
399 if ( flen == FLEN_ERROR ) {
402 memcpy( &i, *bp, 4 );
403 revbytes( &i, 4, 1 );
410 int sgetVX( unsigned char **bp ){
411 unsigned char *buf = *bp;
414 if ( flen == FLEN_ERROR ) {
418 if ( buf[ 0 ] != 0xFF ) {
419 i = buf[ 0 ] << 8 | buf[ 1 ];
424 i = ( buf[ 1 ] << 16 ) | ( buf[ 2 ] << 8 ) | buf[ 3 ];
432 float sgetF4( unsigned char **bp ){
435 if ( flen == FLEN_ERROR ) {
438 memcpy( &f, *bp, 4 );
439 revbytes( &f, 4, 1 );
446 char *sgetS0( unsigned char **bp ){
448 unsigned char *buf = *bp;
451 if ( flen == FLEN_ERROR ) {
455 len = strlen( (const char *) buf ) + 1;
462 s = _pico_alloc( len );
468 memcpy( s, buf, len );