48 assert( integer == NULL ||
isWritePtr( integer, maxLength ) );
49 assert(
isWritePtr( integerLength,
sizeof(
int ) ) );
51 REQUIRES_S( minLength > 0 && minLength < maxLength && \
59 memset( integer, 0,
min( 16, maxLength ) );
66 length = readUint16( stream );
68 length = readUint32( stream );
94 if( length < minLength || length > maxLength + 2 )
101 if( lengthType ==
LENGTH_32 && ( sPeek( stream ) & 0x80 ) )
106 while( length > 0 && sPeek( stream ) == 0 )
110 status = sgetc( stream );
129 if( length < minLength || length > maxLength )
134 if( integer == NULL )
135 return( sSkip( stream, length ) );
136 return( sread( stream, integer, length ) );
158 value = ( ( int ) buffer[ 0 ] << 8 ) | buffer[ 1 ];
175 if( buffer[ 0 ] & 0x80 )
177 value = ( ( int ) buffer[ 0 ] << 24 ) | \
178 ( ( int ) buffer[ 1 ] << 16 ) | \
179 ( ( int ) buffer[ 2 ] << 8 ) | \
193 assert(
isWritePtr( value,
sizeof(
long ) ) );
203 status = localValue = readUint32( stream );
228 assert(
isWritePtr( timeVal,
sizeof( time_t ) ) );
237 status = readUint64( stream, &longValue );
239 value = ( int ) longValue;
242 status = value = readUint32( stream );
247 *timeVal = ( time_t ) value;
255 assert(
isWritePtr( timeVal,
sizeof( time_t ) ) );
260 return( readTime( stream, timeVal,
FALSE ) );
267 assert(
isWritePtr( timeVal,
sizeof( time_t ) ) );
272 return( readTime( stream, timeVal,
TRUE ) );
282 const BOOLEAN includeLengthField )
285 const int headerSize = includeLengthField ?
UINT32_SIZE : 0;
290 assert(
isWritePtr( dataLength,
sizeof(
int ) ) );
295 memset( data, 0,
min( 16, dataMaxLength ) );
298 length = readUint32( stream );
307 if( includeLengthField )
309 dataPtr[ 0 ] = ( length >> 24 ) & 0xFF;
310 dataPtr[ 1 ] = ( length >> 16 ) & 0xFF;
311 dataPtr[ 2 ] = ( length >> 8 ) & 0xFF;
312 dataPtr[ 3 ] = length & 0xFF;
314 *dataLength = headerSize +
length;
315 return( sread( stream, dataPtr + headerSize, length ) );
326 assert(
isWritePtr(
string, stringMaxLength ) );
327 assert(
isWritePtr( stringLength,
sizeof(
int ) ) );
332 return( readData32( stream,
string, stringMaxLength, stringLength,
FALSE ) );
341 const int bufferMaxLength,
345 assert(
isWritePtr( buffer, bufferMaxLength ) );
346 assert(
isWritePtr( bufferLength,
sizeof(
int ) ) );
352 return( readData32( stream, buffer, bufferMaxLength, bufferLength,
371 status = length = readUint16( stream );
373 status = length = readUint32( stream );
378 return( sSkip( stream, length ) );
384 return( readUniversal( stream,
LENGTH_16U ) );
390 return( readUniversal( stream,
LENGTH_32 ) );
398 *integerLength )
void *integer,
403 return(
readInteger( stream, integer, integerLength, minLength,
410 *integerLength )
void *integer,
415 return(
readInteger( stream, integer, integerLength, minLength,
422 *integerLength )
void *integer,
427 return(
readInteger( stream, integer, integerLength, minLength,
443 return(
readInteger( stream, integer, integerLength, minLength,
462 return(
readInteger( stream, integer, integerLength, minLength,
465 return(
readInteger( stream, integer, integerLength, minLength,
474 static
int readBignumInteger(
INOUT STREAM *stream,
490 REQUIRES_S( minLength > 0 && minLength < maxLength && \
497 status =
readInteger( stream, buffer, &length, minLength, maxLength,
498 lengthType, checkType );
506 status = importBignum( bignum, buffer, length, minLength, maxLength,
507 maxRange, checkType );
509 status = sSetError( stream, status );
521 return( readBignumInteger( stream, bignum, minLength, maxLength,
526 int readBignumInteger16Ubits(
INOUT STREAM *stream,
532 return( readBignumInteger( stream, bignum,
bitsToBytes( minBits ),
544 return( readBignumInteger( stream, bignum, minLength, maxLength,
553 int readBignumInteger16UChecked(
INOUT STREAM *stream,
558 return( readBignumInteger( stream, bignum, minLength, maxLength, NULL,
563 int readBignumInteger16UbitsChecked(
INOUT STREAM *stream,
568 return( readBignumInteger( stream, bignum,
bitsToBytes( minBits ),
574 int readBignumInteger32Checked(
INOUT STREAM *stream,
579 return( readBignumInteger( stream, bignum, minLength, maxLength,
594 IN_RANGE( 0, 0xFFFF )
const int value )
600 sputc( stream, ( value >> 8 ) & 0xFF );
601 return( sputc( stream, value & 0xFF ) );
613 buffer[ 0 ] = ( value >> 24 ) & 0xFF;
614 buffer[ 1 ] = ( value >> 16 ) & 0xFF;
615 buffer[ 2 ] = ( value >> 8 ) & 0xFF;
616 buffer[ 3 ] = value & 0xFF;
627 swrite( stream,
"\x00\x00\x00\x00",
UINT64_SIZE / 2 );
628 return( writeUint32( stream, value ) );
634 int writeUint32Time(
INOUT STREAM *stream,
const time_t timeVal )
638 return( writeUint32( stream, (
int ) timeVal ) );
642 int writeUint64Time(
INOUT STREAM *stream,
const time_t timeVal )
646 return( writeUint64( stream, (
int ) timeVal ) );
653 IN_BUFFER( stringLength )
const void *
string,
657 assert(
isReadPtr(
string, stringLength ) );
661 writeUint32( stream, stringLength );
662 return( swrite( stream,
string, stringLength ) );
669 IN_BUFFER( integerLength )
const void *integer,
677 assert(
isReadPtr( integer, integerLength ) );
685 while( length > 0 && *intPtr == 0 )
695 writeUint16( stream, length );
704 const BOOLEAN leadingOneBit = *intPtr & 0x80;
706 writeUint32( stream, length + ( leadingOneBit ? 1 : 0 ) );
715 return( swrite( stream, intPtr, length ) );
720 IN_BUFFER( integerLength )
const void *integer,
723 return( writeInteger( stream, integer, integerLength,
LENGTH_16U ) );
728 IN_BUFFER( integerLength )
const void *integer,
731 return( writeInteger( stream, integer, integerLength,
LENGTH_16U_BITS ) );
736 IN_BUFFER( integerLength )
const void *integer,
739 return( writeInteger( stream, integer, integerLength,
LENGTH_32 ) );
747 int sizeofBignumInteger32(
const void *bignum )
756 static
int writeBignumInteger(
INOUT STREAM *stream,
779 status = swrite( stream, buffer, bnLength );
782 status = writeInteger( stream, buffer, bnLength, lengthType );
791 return( writeBignumInteger( stream, bignum,
LENGTH_16U ) );
795 int writeBignumInteger16Ubits(
INOUT STREAM *stream,
805 return( writeBignumInteger( stream, bignum,
LENGTH_32 ) );