Exemplo n.º 1
0
int Xcode_nameprepString( const UTF16CHAR * puInputString, int iInputSize,
                          DWORD * pdwOutputString, int * piOutputSize,
                          DWORD * pdwProhibitedChar )
{
    int return_code;
    DWORD dwTemp[256];
    DWORD dwMapped[256];
    int iTempSize = 256;
    int iMappedSize = 256;

    memset( dwTemp, 0, sizeof( dwTemp ) );
    memset( dwMapped, 0, sizeof( dwMapped ) );

    *pdwProhibitedChar = 0;

    /* Input character string is UTF16, each character is 16 bits, type UTF16CHAR */

    return_code = Xcode_convertUTF16To32Bit( puInputString, iInputSize, dwTemp, &iTempSize );

    if ( return_code != XCODE_SUCCESS ) {
        return return_code;
    }

    return_code = Xcode_charmapString( dwTemp, iTempSize, dwMapped, &iMappedSize );

    if ( return_code != XCODE_SUCCESS ) {
        return return_code;
    }

    return_code = Xcode_normalizeString( dwMapped, iMappedSize, pdwOutputString, piOutputSize );

    if ( return_code != XCODE_SUCCESS ) {
        return return_code;
    }

    return_code = Xcode_prohibitString( pdwOutputString, *piOutputSize, pdwProhibitedChar );

    if ( return_code != XCODE_SUCCESS ) {
        return return_code;
    }

    return_code = Xcode_bidifilterString( pdwOutputString, *piOutputSize );

    if ( return_code != XCODE_SUCCESS ) {
        return return_code;
    }

    return XCODE_SUCCESS;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
	FILE * fpin;
	char szIn[1024];
	UTF16CHAR uInput[1024];
	DWORD  dwOutput[1024];
	UCHAR8 szData[1024];
	UTF16CHAR uData[1024];
	int iInputSize = 0;
	int iOutputSize = 0;
	int counter = 0;
	int res;
	int i;
	int encode = 0;

	/* Arg check */
	if (argc < 2) { printf("usage: [encode (to race)|decode (to unicode)] <file>\n", argv[0] ); return 1; }

	/* Get file */
	fpin = fopen(argv[2], "r");
	if (fpin == NULL) { printf("Cannot open %s\n",argv[2]); return 1; }

	if ( strcmp( argv[1], "encode" ) == 0 ) encode = 1;

	while ( !feof( fpin ) )
	{
		memset( szIn, 0, sizeof(szIn) );
		memset( uInput, 0, sizeof(uInput) );
		memset( dwOutput, 0, sizeof(dwOutput) );
		memset( uData, 0, sizeof(uData) );
		memset( szData, 0, sizeof(szData) );

		fgets( szIn, sizeof(szIn), fpin );
		if ( szIn[0] == ' ' || szIn[0] == '#' || strlen( szIn ) < 2 )
		{
			printf( szIn );
			continue;
		}

		/* Clip off \n */
		szIn[strlen(szIn)-1] = 0;

		if ( encode )
		Read16BitLine( szIn, uInput, &iInputSize );

		iOutputSize = 1024;

		if ( encode )
		{
			res = Xcode_race_encodeString( uInput, iInputSize, szData, &iOutputSize, "bq--", 4 );
		} else {
			iInputSize = strlen( szIn );
			res = Xcode_race_decodeString( szIn, iInputSize, uData, &iOutputSize, "bq--", 4 );
			if ( res != XCODE_SUCCESS ) goto error;
			res = Xcode_convertUTF16To32Bit( uData, iOutputSize, dwOutput, &iOutputSize );
		}

		counter++;

		error:
		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++ )
		{
			if ( encode )
			printf( "%c", szData[i] );
			else
			printf( "%x ", dwOutput[i] );
		}
		printf( "\n" );

	}

	fclose(fpin);
	#ifdef WIN32
	getchar();
	#endif
	return 0;

}