41 if( length <= 0xFFFFL )
43 return( 1 + ( ( length > 0xFFFFFFL ) ? 4 : 3 ) );
52 const int noLengthOctets = ( length <= 0xFF ) ? 1 : \
53 ( length <= 0xFFFFL ) ? 2 : \
54 ( length <= 0xFFFFFFL ) ? 3 : 4;
63 return( sputc( stream, length & 0xFF ) );
66 buffer[ 0 ] = 0x80 | noLengthOctets;
67 if( noLengthOctets > 3 )
68 buffer[ bufPos++ ] = ( length >> 24 ) & 0xFF;
69 if( noLengthOctets > 2 )
70 buffer[ bufPos++ ] = ( length >> 16 ) & 0xFF;
71 if( noLengthOctets > 1 )
72 buffer[ bufPos++ ] = ( length >> 8 ) & 0xFF;
73 buffer[ bufPos++ ] = length & 0xFF;
74 return( swrite( stream, buffer, bufPos ) );
86 int length = 0, i, iterationCount;
94 return( swrite( stream,
"\x01\x00", 2 ) );
99 for( iterationCount = 0;
103 buffer[ length++ ] = intValue & 0xFF;
106 ENSURES( iterationCount < FAILSAFE_ITERATIONS_SMALL );
110 if( buffer[ length - 1 ] & 0x80 )
111 buffer[ length++ ] = 0x00;
122 buffer[ length++ ] = intValue & 0xFF;
125 while( intValue != -1 && length <
sizeof(
int ) && \
126 iterationCount++ < FAILSAFE_ITERATIONS_SMALL );
127 ENSURES( iterationCount < FAILSAFE_ITERATIONS_SMALL );
131 if( !( buffer[ length - 1 ] & 0x80 ) )
132 buffer[ length++ ] = 0xFF;
134 ENSURES( length > 0 && length <= 8 );
137 sputc( stream, length );
138 for( i = length - 1; i > 0; i-- )
139 sputc( stream, buffer[ i ] );
140 return( sputc( stream, buffer[ 0 ] ) );
160 long sizeofObject(
IN_LENGTH const long length )
168 DEBUG_DIAG((
"Error code was passed to sizeof() function" ));
177 DEBUG_DIAG((
"Length exceeds maximum safe length value" ));
182 return(
sizeof(
BYTE ) + calculateLengthSize( length ) + length );
220 return( writeNumeric( stream, integer ) );
229 const BOOLEAN leadingZero = ( integerLength > 0 && \
230 ( *integer & 0x80 ) ) ? 1 : 0;
233 assert(
isReadPtr( integer, integerLength ) );
240 writeLength( stream, integerLength + leadingZero );
243 return( swrite( stream, integer, integerLength ) );
265 if( sIsNullStream( stream ) )
271 status = writeInteger( stream, buffer, length, tag );
281 IN_RANGE( 0, 999 )
const int enumerated,
286 REQUIRES_S( enumerated >= 0 && enumerated < 1000 );
291 return( writeNumeric( stream, (
long ) enumerated ) );
299 BYTE buffer[ 8 + 8 ];
308 return( swrite( stream, buffer, 2 ) );
317 BYTE buffer[ 8 + 8 ];
326 buffer[ 2 ] =
boolean ? 0xFF : 0;
327 return( swrite( stream, buffer, 3 ) );
346 writeLength( stream, length );
347 return( swrite( stream,
string, length ) );
371 writeLength( stream, length );
372 return( swrite( stream,
string, length ) );
381 BYTE buffer[ 16 + 8 ];
382 unsigned int value = 0;
387 REQUIRES_S( bitString >= 0 && bitString < INT_MAX );
392 for( i = 0; i < ( sizeof( int ) > 2 ? 32 : 16 ); i++ )
413 buffer[ 1 ] = 1 + ( ( noBits + 7 ) >> 3 );
414 buffer[ 2 ] = ~( ( noBits - 1 ) & 7 ) & 7;
415 #if UINT_MAX > 0xFFFF
416 buffer[ 3 ] = ( value >> 24 ) & 0xFF;
417 buffer[ 4 ] = ( value >> 16 ) & 0xFF;
418 buffer[ 5 ] = ( value >> 8 ) & 0xFF;
419 buffer[ 6 ] = value & 0xFF;
421 buffer[ 3 ] = ( value >> 8 ) & 0xFF;
422 buffer[ 4 ] = value & 0xFF;
424 return( swrite( stream, buffer, 3 + ( ( noBits + 7 ) >> 3 ) ) );
433 struct tm timeInfo, *timeInfoPtr = &timeInfo;
434 char buffer[ 20 + 8 ];
435 const int length = isUTCTime ? 13 : 15;
442 timeInfoPtr =
gmTime_s( &timeVal, timeInfoPtr );
443 ENSURES_S( timeInfoPtr != NULL && timeInfoPtr->tm_year > 90 );
449 sprintf_s( buffer + 2, 16,
"%02d%02d%02d%02d%02d%02dZ",
450 timeInfoPtr->tm_year % 100, timeInfoPtr->tm_mon + 1,
451 timeInfoPtr->tm_mday, timeInfoPtr->tm_hour,
452 timeInfoPtr->tm_min, timeInfoPtr->tm_sec );
456 sprintf_s( buffer + 2, 16,
"%04d%02d%02d%02d%02d%02dZ",
457 timeInfoPtr->tm_year + 1900, timeInfoPtr->tm_mon + 1,
458 timeInfoPtr->tm_mday, timeInfoPtr->tm_hour,
459 timeInfoPtr->tm_min, timeInfoPtr->tm_sec );
461 return( swrite( stream, buffer, length + 2 ) );
465 int writeUTCTime(
INOUT STREAM *stream,
const time_t timeVal,
473 return( writeTime( stream, timeVal, tag,
TRUE ) );
477 int writeGeneralizedTime(
INOUT STREAM *stream,
const time_t timeVal,
485 return( writeTime( stream, timeVal, tag,
FALSE) );
508 return( writeLength( stream, length ) );
519 return( writeLength( stream, length ) );
533 return( writeLength( stream, length ) );
548 return( writeLength( stream, length ) );
562 writeLength( stream, length + 1 );
563 return( sputc( stream, 0 ) );
576 return( writeLength( stream, length ) );