int main(int argc, char* argv[]) { FILE * fpin; char szIn[1024]; DWORD dwInput[1024]; DWORD dwOutput[1024]; DWORD dwProhibitChar; int iInputSize = 0; int iOutputSize = 0; int counter = 0; int res; int i; /* Arg check */ if (argc < 2) { printf("usage: <unicode codepoints file>\n", argv[0] ); return 1; } /* Get file */ fpin = fopen(argv[1], "r"); if (fpin == NULL) { printf("Cannot open %s\n",argv[1]); return 1; } while ( !feof( fpin ) ) { memset( szIn, 0, sizeof(szIn) ); memset( dwInput, 0, sizeof(dwInput) ); memset( dwOutput, 0, sizeof(dwOutput) ); fgets( szIn, sizeof(szIn), fpin ); if ( szIn[0] == ' ' || szIn[0] == '#' || strlen( szIn ) < 2 ) { printf( szIn ); continue; } /* Clip off \n */ szIn[strlen(szIn)-1] = 0; Read32BitLine( szIn, dwInput, &iInputSize ); iOutputSize = 1024; res = Xcode_nameprepString32( dwInput, iInputSize, dwOutput, &iOutputSize, &dwProhibitChar ); counter++; if ( res != XCODE_SUCCESS ) { char szMsg[1024]; ConvertErrorCode( res, szMsg ); printf( "Fail: Line=%d '%25s' (%s)\n", counter, szMsg, szIn ); continue; } for( i = 0; i < iOutputSize; i++ ) { printf( "%05X ", dwOutput[i] ); } printf( "\n" ); } fclose(fpin); #ifdef WIN32 getchar(); #endif return 0; }
int Xcode_ToUnicode16( const UTF16CHAR * puzInputString, int iInputSize, UTF16CHAR * puzOutputString, int * piOutputSize ) { int res, i; XcodeBool bHigh = 0; int iConfirmSize = MAX_LABEL_SIZE_8; int iPrepInputSize = MAX_LABEL_SIZE_32; int iPrepOutputSize = MAX_LABEL_SIZE_32; int iCopySize; UCHAR8 pzConfirmString[MAX_LABEL_SIZE_8]; DWORD dwzPrepInputString[MAX_LABEL_SIZE_32]; DWORD dwzPrepOutputString[MAX_LABEL_SIZE_32]; UCHAR8 pzCopyString[MAX_LABEL_SIZE_8]; DWORD dwProhibitedChar = 0; UTF16CHAR suzDecoded[MAX_LABEL_SIZE_16]; int iDecodedSize = MAX_LABEL_SIZE_16; /* Basic input validity checks and buffer length checks */ if ( puzInputString == 0 || iInputSize <= 0 ) return XCODE_BAD_ARGUMENT_ERROR; if ( puzOutputString == 0 || *piOutputSize <= 0 ) return XCODE_BAD_ARGUMENT_ERROR; if ( iInputSize > MAX_LABEL_SIZE_16 ) return XCODE_BUFFER_OVERFLOW_ERROR; if ( *piOutputSize < MAX_LABEL_SIZE_16 ) return XCODE_BUFFER_OVERFLOW_ERROR; /* ToUnicode never fails. If any step fails, then the original input sequence is returned immediately in that step. */ for ( i = 0; i < iInputSize; i++ ) *(puzOutputString+i) = *(puzInputString+i); *piOutputSize = iInputSize; /* 1. If all code points in the sequence are in the ASCII range (0..7F) then skip to step 3. */ for ( i = 0; i < iInputSize; i++ ) { if ( *(puzInputString+i) > 0x7F ) { bHigh = 1; break; } } if ( bHigh == 1 ) { /* 2. Perform the steps specified in [NAMEPREP] and fail if there is an error. (If step 3 of ToAscii is also performed here, it will not affect the overall behavior of ToUnicode, but it is not necessary.) The AllowUnassigned flag is used in [NAMEPREP]. */ /* Author: This step maps some unicode codepoints to ascii, which is useful where in some languages "ascii" characters are emulated in higher unicode codepoints. (for example, Japanese half width and full width codepoints) Here in the 8-bit input routine, ASCII values greater that 7F are also mapped, so we convert over to 32-bit and run the input through nameprep. */ /* convert UTF16 to 32-bit */ res = Xcode_convertUTF16To32Bit( puzInputString, iInputSize, dwzPrepInputString, &iPrepInputSize ); if (res != XCODE_SUCCESS) { return res; } res = Xcode_nameprepString32( dwzPrepInputString, iPrepInputSize, dwzPrepOutputString, &iPrepOutputSize, &dwProhibitedChar ); if (res != XCODE_SUCCESS) { return res; } for ( i = 0; i < iPrepOutputSize; i++ ) pzCopyString[i] = (UCHAR8)(dwzPrepOutputString[i]); iCopySize = iPrepOutputSize; } else { for ( i = 0; i < iInputSize; i++ ) pzCopyString[i] = (UCHAR8)*(puzInputString+i); iCopySize = iInputSize; } /* 3. Verify that the sequence begins with the ACE prefix, and save a copy of the sequence. */ /* 4. Remove the ACE prefix. */ /* 5. Decode the sequence using the decoding algorithm in [PUNYCODE] and fail if there is an error. Save a copy of the result of this step. */ res = Xcode_puny_decodeString( pzCopyString, iCopySize, suzDecoded, &iDecodedSize ); if (res != XCODE_SUCCESS) { return res; } /* 6. Apply ToAscii. */ res = Xcode_ToASCII( suzDecoded, iDecodedSize, pzConfirmString, &iConfirmSize ); if (res != XCODE_SUCCESS) { return res; } /* 7. Verify that the result of step 6 matches the saved copy from step 3, using a case-insensitive ASCII comparison. */ if ( memcmp( pzCopyString, pzConfirmString, iConfirmSize ) != 0 ) { return XCODE_TOXXX_CIRCLECHECKFAILED; } /* 8. Return the saved copy from step 5. */ memcpy( puzOutputString, suzDecoded, iCopySize*2 ); *piOutputSize = iDecodedSize; return XCODE_SUCCESS; }