72 #ifdef USE_CERTIFICATES
94 static const int FAR_BSS asn1CharFlags[] = {
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100 PI,
I,
I,
I,
I,
I,
I,
PI,
PI,
PI,
I,
PI,
PI,
PI,
PI,
PI,
102 PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
I,
I,
PI,
I,
PI,
104 I,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
106 PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
I,
I,
I,
I,
I,
108 I,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
110 PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
PI,
I,
I,
I,
I, 0,
114 #define nativeCharFlags asn1CharFlags
128 #ifdef DATA_LITTLEENDIAN
138 for( i = 0; i <
WCSIZE; i++ )
140 #ifdef DATA_LITTLEENDIAN
141 ch |=
string[ i ] << shiftAmt;
144 ch = ( ch << 8 ) |
string[ i ];
173 int firstChar, secondChar,
status;
178 status = firstChar = sgetc( stream );
181 if( !( firstChar & 0x80 ) )
184 return( firstChar & 0x7F );
186 if( ( firstChar & 0xC0 ) == 0x80 )
190 status = secondChar = sgetc( stream );
193 if( ( firstChar & 0xE0 ) == 0xC0 )
196 if( ( secondChar & 0xC0 ) != 0x80 )
198 largeCh = ( ( firstChar & 0x1F ) << 6 ) | \
199 ( secondChar & 0x3F );
200 if( largeCh < 0x80 || largeCh > 0x7FF )
202 return( largeCh & 0x7FF );
206 if( ( firstChar & 0xF0 ) == 0xE0 )
210 status = thirdChar = sgetc( stream );
215 if( ( secondChar & 0xC0 ) != 0x80 || \
216 ( thirdChar & 0xC0 ) != 0x80 )
218 largeCh = ( ( firstChar & 0x1F ) << 12 ) | \
219 ( ( secondChar & 0x3F ) << 6 ) | \
220 ( thirdChar & 0x3F );
221 if( largeCh < 0x800 || largeCh > 0xFFFF )
223 return( largeCh & 0xFFFF );
236 IN_RANGE( 0, 0xFFFF )
const long largeCh )
240 REQUIRES_S( largeCh >= 0 && largeCh <= 0xFFFF );
243 return( sputc( stream, (
BYTE ) largeCh );
244 if( largeCh < 0x0800 )
246 sputc( stream, (
BYTE )( 0xC0 | ( ( largeCh >> 6 ) & 0x1F ) ) );
247 return( sputc( stream, (
BYTE )( 0x80 | ( largeCh & 0x3F ) ) ) );
249 sputc( stream, (
BYTE )( 0xE0 | ( ( largeCh >> 12 ) & 0x0F ) ) );
250 sputc( stream, (
BYTE )( 0x80 | ( ( largeCh >> 6 ) & 0x3F ) ) );
251 return( sputc( stream, (
BYTE )( 0x80 | largeCh & 0x3F ) ) );
265 assert(
isReadPtr(
string, stringLen ) );
266 assert(
isWritePtr( targetStringLength,
sizeof(
int ) ) );
271 *targetStringLength = 0;
273 sMemNullOpen( &stream );
276 const wchar_t *wcStrPtr = (
wchar_t * )
string;
277 const int wcStrLen = stringLen /
WCSIZE;
280 REQUIRES( stringLen % WCSIZE == 0 );
284 status = putUtf8Char( &stream, wcStrPtr[ i ] );
288 ENSURES( i < FAILSAFE_ITERATIONS_LARGE );
290 *targetStringLength = stell( &stream );
296 status = putUtf8Char( &stream,
string[ i ] );
300 ENSURES( i < FAILSAFE_ITERATIONS_LARGE );
302 *targetStringLength = stell( &stream );
304 sMemClose( &stream );
313 static
int checkUtf8String(
IN_BUFFER( stringLen )
const void *
string,
319 assert(
isReadPtr(
string, stringLen ) );
324 sMemConnect( &stream,
string, stringLen );
325 for( iterationCount = 0;
326 stell( &stream ) < stringLen && \
330 const long longStatus = getUTF8Char( &stream );
333 sMemDisconnect( &stream );
337 ENSURES( iterationCount < FAILSAFE_ITERATIONS_LARGE );
338 sMemDisconnect( &stream );
355 wchar_t *wcDestPtr =
dest;
357 int noChars = 0, iterationCount;
360 assert(
isWritePtr( destLen,
sizeof(
int ) ) );
361 assert(
isWritePtr( destStringType,
sizeof(
int ) ) );
362 assert(
isReadPtr( source, sourceLen ) );
368 memset( dest, 0,
min( 16, destMaxLen ) );
378 sMemConnect( &stream, source, sourceLen );
379 for( iterationCount = 0;
380 stell( &stream ) < sourceLen && \
384 const long largeCh = getUTF8Char( &stream );
387 sMemDisconnect( &stream );
408 stringType = ( asn1CharFlags[ largeCh & 0x7F ] &
P ) ? \
412 ENSURES( iterationCount < FAILSAFE_ITERATIONS_LARGE );
413 sMemDisconnect( &stream );
419 if( *destLen < 0 || *destLen > destMaxLen )
421 *destStringType = stringType;
424 sMemConnect( &stream, source, sourceLen );
425 for( iterationCount = 0;
426 stell( &stream ) < sourceLen && \
430 const long largeCh = getUTF8Char( &stream );
436 *wcDestPtr++ = (
wchar_t ) largeCh;
438 *destPtr++ = (
BYTE ) largeCh;
440 ENSURES( iterationCount < FAILSAFE_ITERATIONS_LARGE );
441 sMemDisconnect( &stream );
449 static
int copyToUtf8String(
OUT_BUFFER( destMaxLen, *destLen )
void *dest,
452 IN_BUFFER( sourceLen )
const void *source,
459 assert(
isWritePtr( destLen,
sizeof(
int ) ) );
460 assert(
isReadPtr( source, sourceLen ) );
466 memset( dest, 0,
min( 16, destMaxLen ) );
469 sMemOpen( &stream, dest, destMaxLen );
472 const wchar_t *wcStrPtr = (
wchar_t * )
string;
473 const int wcStrLen = stringLen /
WCSIZE;
476 REQUIRES( stringLen % WCSIZE == 0 );
480 if( wcStrPtr[ i ] < 0 || wcStrPtr[ i ] > 0xFFFF )
485 status = putUtf8Char( &stream, wcStrPtr[ i ] );
489 ENSURES( i < FAILSAFE_ITERATIONS_LARGE );
491 *destLen = stell( &stream );
497 status = putUtf8Char( &stream,
string[ i ] );
501 ENSURES( i < FAILSAFE_ITERATIONS_LARGE );
503 *destLen = stell( &stream );
505 sMemDisconnect( &stream );
526 assert(
isReadPtr(
string, stringLen ) );
532 wCh = getWidechar(
string );
542 return( ( wCh > 0xFFFF ) ?
FALSE :
TRUE );
548 #if INT_MAX < 0xFFFFL
559 if( stringLen <= ( WCSIZE * 3 ) && wCh > 0xFF )
561 if( stringLen == WCSIZE )
563 const int ch1 =
string[ 0 ];
564 const int ch2 =
string[ 1 ];
568 if( ch1 > 0 && ch1 <= 0x7F &&
isPrint( ch1 ) && \
569 ch2 > 0 && ch2 <= 0x7F &&
isPrint( ch2 ) )
574 const int hi1 = wCh >> 8;
575 const int hi2 = getWidechar(
string + WCSIZE ) >> 8;
576 const int hi3 = ( stringLen > WCSIZE * 2 ) ? \
577 getWidechar(
string + ( WCSIZE * 2 ) ) >> 8 : hi1;
579 ENSURES_B( stringLen == ( WCSIZE * 2 ) || \
580 stringLen == ( WCSIZE * 3 ) );
584 hi1 == hi2 && hi2 == hi3 )
598 wCh = getWidechar( &
string[ i ] );
601 const int wChHi = wCh >> 8;
612 if( wChHi != hiByte )
617 ENSURES_B( i < FAILSAFE_ITERATIONS_LARGE );
645 assert(
isReadPtr(
string, stringLen ) );
656 const BYTE ch =
string[ i ];
662 if( asn1CharFlags[ ch & 0x7F ] )
678 if( !( asn1CharFlags[ ch ] &
P ) )
682 if( asn1CharFlags[ ch ] )
712 assert(
isReadPtr(
string, stringLen ) );
722 return( get8bitStringType(
string, stringLen, stringTag ) );
733 if(
string[ 0 ] !=
'\0' )
740 sMemConnect( &stream,
string, stringLen );
745 status = ch = readUint16( &stream );
748 sMemDisconnect( &stream );
767 if( !( asn1CharFlags[ ch ] & P ) )
770 sMemDisconnect( &stream );
786 assert(
isReadPtr(
string, stringLen ) );
800 if( stringLen < WCSIZE || ( stringLen % WCSIZE ) != 0 || \
801 !isNativeWidecharString(
string, stringLen ) )
808 const wchar_t wCh = getWidechar( &
string[ i ] );
814 if( wCh < 0 || ( wCh & 0xFFFF8000L ) )
829 if( nativeCharFlags[ wCh & 0x7F ] )
845 if( !( nativeCharFlags[ wCh ] & P ) )
870 const int charTypeMask = isPrintableString ? P :
I;
873 assert(
isReadPtr(
string, stringLen ) );
881 if( ch <= 0 || ch > 0x7F || !
isPrint( ch ) )
883 if( !( nativeCharFlags[ ch ] & charTypeMask ) )
886 ENSURES_B( i < FAILSAFE_ITERATIONS_LARGE );
895 int copyFromAsn1String(
OUT_BUFFER( destMaxLen, *destLen )
void *dest,
899 IN_BUFFER( sourceLen )
const void *source,
904 const ASN1_STRINGTYPE stringType = getAsn1StringType( source, sourceLen,
906 int iterationCount,
status;
909 assert(
isWritePtr( destLen,
sizeof(
int ) ) );
910 assert(
isWritePtr( destStringType,
sizeof(
int ) ) );
911 assert(
isReadPtr( source, sourceLen ) );
918 memset( dest, 0,
min( 16, destMaxLen ) );
929 wchar_t *wcDestPtr = (
wchar_t * ) dest;
930 const int newLen = ( sourceLen /
UCSIZE ) * WCSIZE;
932 if( newLen <= 0 || newLen > destMaxLen )
939 sMemConnect( &stream, source, sourceLen );
940 for( iterationCount = 0;
941 stell( &stream ) < sourceLen && \
947 status = wCh = readUint16( &stream );
950 sMemDisconnect( &stream );
953 *wcDestPtr++ = (
wchar_t ) wCh;
955 ENSURES( iterationCount < FAILSAFE_ITERATIONS_LARGE );
956 sMemDisconnect( &stream );
964 return( copyFromUtf8String( dest, destMaxLen, destLen,
965 destStringType, source, sourceLen ) );
975 const int newLen = sourceLen /
UCSIZE;
977 if( newLen <= 0 || newLen > destMaxLen )
980 sMemConnect( &stream, source, sourceLen );
981 for( iterationCount = 0;
982 stell( &stream ) < sourceLen && \
988 status = wCh = readUint16( &stream );
997 sMemDisconnect( &stream );
1002 ENSURES( iterationCount < FAILSAFE_ITERATIONS_LARGE );
1003 sMemDisconnect( &stream );
1014 if( sourceLen <= 0 || sourceLen > destMaxLen )
1016 memcpy( dest, source, sourceLen );
1018 *destStringType = stringType;
1036 if( destPtr[ i ] == 0xC8 )
1043 if( ch == 0x61 || ch == 0x41 ||
1044 ch == 0x6F || ch == 0x4F ||
1045 ch == 0x75 || ch == 0x55 )
1047 typedef struct {
int src,
dest; } CHARMAP_INFO;
1048 static const CHARMAP_INFO charMap[] = {
1049 { 0x61, 0xE4 }, { 0x41, 0xC4 },
1050 { 0x6F, 0xF6 }, { 0x4F, 0xD6 },
1051 { 0x75, 0xFC }, { 0x55, 0xDC },
1052 { 0x00,
'?' }, { 0x00,
'?' }
1057 charMap[ charIndex ].src && \
1058 charMap[ charIndex ].src != ch && \
1062 destPtr[ i ] = charMap[ charIndex ].dest;
1063 if( length - i > 2 )
1064 memmove( destPtr + i + 1, destPtr + i + 2,
1065 length - ( i + 2 ) );
1070 ENSURES( i < FAILSAFE_ITERATIONS_LARGE );
1084 int getAsn1StringInfo(
IN_BUFFER( stringLen )
const void *
string,
1091 getNativeStringType(
string, stringLen );
1093 assert(
isReadPtr(
string, stringLen ) );
1094 assert(
isWritePtr( stringType,
sizeof(
int ) ) );
1095 assert(
isWritePtr( asn1StringType,
sizeof(
int ) ) );
1096 assert(
isWritePtr( asn1StringLen,
sizeof(
int ) ) );
1102 *asn1StringType = 0;
1109 *stringType = localStringType;
1110 switch( localStringType )
1115 *asn1StringLen = ( stringLen /
WCSIZE ) * UCSIZE;
1124 *asn1StringLen = stringLen /
WCSIZE;
1139 status = utf8TargetStringLen(
string, stringLen, asn1StringLen,
1165 int copyToAsn1String(
OUT_BUFFER( destMaxLen, *destLen )
void *dest,
1168 IN_BUFFER( sourceLen )
const void *source,
1170 IN_RANGE( 1, 20 )
const int stringType )
1179 assert(
isWritePtr( destLen,
sizeof(
int ) ) );
1180 assert(
isReadPtr( source, sourceLen ) );
1189 memset( dest, 0,
min( 16, destMaxLen ) );
1199 if( sourceLen > destMaxLen )
1201 memcpy( dest, source, sourceLen );
1209 sMemOpen( &stream, dest, destMaxLen );
1214 const wchar_t wCh = getWidechar( &srcPtr[ i ] );
1216 status = writeUint16( &stream, wCh );
1218 status = sputc( &stream, wCh );
1220 ENSURES( i < FAILSAFE_ITERATIONS_LARGE );
1222 *destLen = stell( &stream );
1223 sMemDisconnect( &stream );