Esempio n. 1
0
/*********************************************************************************
*
* int Xcode_DomainToASCII( const UTF16CHAR * puzInputString, int iInputSize,
*              UCHAR8 * pzOutputString, int * piOutputSize )
* 
*  Applies IDNA spec ToASCII operation on a domain.
*
*  Returns XCODE_SUCCESS if successful, or an XCODE error constant on failure.
*
*  puzInputString  - [in] UTF16 input string.
*  iInputSize      - [in] Length of incoming UTF16 string.
*  pzOutputString  - [in,out] 8-bit output character string buffer.
*  piOutputSize    - [in,out] Length of incoming 8-bit buffer, and contains
*                    length of resulting encoded string on exit.
*
**********************************************************************************/
int Xcode_DomainToASCII( const UTF16CHAR *  puzInputString, 
                         int                iInputSize,
                         UCHAR8 *           pzOutputString, 
                         int *              piOutputSize )
{
  int i;
  UTF16CHAR suzLabel[MAX_LABEL_SIZE_16];
  UCHAR8 szDomain[MAX_DOMAIN_SIZE_8];
  UCHAR8 szLabel[MAX_LABEL_SIZE_8];
  int lindex = 0;
  int dindex = 0;
  int iOutputSize;
  int delimiterPresent;
  int res;
  /* Basic input validity checks */
  if ( puzInputString == 0 || iInputSize <= 0 ) return XCODE_BAD_ARGUMENT_ERROR;
  if ( pzOutputString == 0 || *piOutputSize <= 0 ) return XCODE_BAD_ARGUMENT_ERROR;
  if ( iInputSize > MAX_DOMAIN_SIZE_16 ) return XCODE_BUFFER_OVERFLOW_ERROR;
  if ( *piOutputSize < MAX_DOMAIN_SIZE_8 ) return XCODE_BUFFER_OVERFLOW_ERROR;
  for ( i = 0; i < iInputSize; i++ )
  {
    delimiterPresent = 0;
    
    /* www.ml.ml.com */
    
    if ( Xcode_IsIDNADomainDelimiter( puzInputString+i ) ) 
    {
      delimiterPresent = 1;
    } else {
      suzLabel[lindex] = *(puzInputString+i);
      lindex++;
    }
    
    if ( i == iInputSize - 1 || delimiterPresent == 1 )
    {
      /* encode the label and save the result in domain */
      suzLabel[lindex] = 0;
      
      if ( lindex == 0 ) goto skip;
      
      iOutputSize = MAX_LABEL_SIZE_8;
      if ( ( res = Xcode_ToASCII( suzLabel, lindex, szLabel, &iOutputSize ) ) != XCODE_SUCCESS ) return res;
      if ( dindex + iOutputSize > MAX_DOMAIN_SIZE_8 ) return XCODE_BUFFER_OVERFLOW_ERROR;
      memcpy( &szDomain[dindex], szLabel, iOutputSize );
      dindex = dindex + iOutputSize;
      lindex = 0;
      skip:
      if ( delimiterPresent == 1 )
      {
        szDomain[dindex] = LABEL_DELIMITER;
        dindex++;
      }
      continue;
    }
  }
  
  memcpy( pzOutputString, szDomain, dindex );
  *piOutputSize = dindex;
  return XCODE_SUCCESS;
}
Esempio 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;
}
Esempio n. 3
0
int main(int argc, char* argv[])
{
    FILE * fpin;
    char szIn[1024];
    char szOut[1024];
    DWORD dwInput[1024];
    DWORD dwOutput[1024];
    UCHAR8 szData[1024];
    UTF16CHAR uInput[1024];
    int iInputSize = 0;
    int iOutputSize = 0;
    int counter = 0;
    int res;

    /* Arg check */
    if (argc < 2) {
        printf("usage: <fullcircle 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( szOut, 0, sizeof(szOut) );
        memset( dwInput, 0, sizeof(dwInput) );
        memset( dwOutput, 0, sizeof(dwOutput) );
        memset( szData, 0, sizeof(szData) );


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

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

        if ( szIn[0] != 'i' ) {
            printf("Invalid input file format.\n");
            return 1;
        }
        if ( szOut[0] != 'o' ) {
            printf("Invalid input file format.\n");
            return 1;
        }

        if ( szOut[strlen(szOut)-1] == ' ' ) szOut[strlen(szOut)-1] = '\0';

        /*
        Read32BitLine( &szIn[2], dwInput, &iInputSize );
        iOutputSize = sizeof(dwOutput);
        res = Xcode_nameprepString32( dwInput, iInputSize, dwOutput, &iOutputSize, &dwProhibitChar );
        if ( res != XCODE_SUCCESS ) goto error;
        iInputSize = sizeof(szData);
        res = Xcode_puny_encodeString( dwOutput, iOutputSize, szData, &iInputSize );
        */

        iInputSize = sizeof( dwInput );
        Read32BitLine( &szIn[2], dwInput, &iInputSize );

        iOutputSize = sizeof(uInput);
        res = Xcode_convert32BitToUTF16( dwInput, iInputSize, uInput, &iOutputSize );

        iInputSize = iOutputSize;

        iOutputSize = sizeof(szData);
        res = Xcode_ToASCII( uInput, iInputSize, szData, &iOutputSize );

        if ( res != XCODE_SUCCESS )
        {
            char szMsg[1024];
            ConvertErrorCode( res, szMsg );
            printf( "Fail: Line=%d '%25s' (%s)(%s)\n", counter, szMsg, szIn, szOut );
            continue;
        }

        if ( stricmp( &szOut[2], szData ) != 0 )
        {
            //printf( "%s\no:%s\n", szIn, szData );
            printf( "Error  : Line=%d '%s' != '%s'\n", counter, &szOut[2], szData );
            continue;
        }

        printf( "Success: Line=%d '%s'\n", counter, &szOut[2] );
        //printf( "%s\no:%s\n", szIn, szData );
    }

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