int __stdcall CCP_WideCharToMultiByte(
    UINT CodePage,            // code page
    DWORD dwFlags,            // performance and mapping flags
    LPCWSTR lpWideCharStr,    // wide-character string
    int cchWideChar,          // number of chars in string
    LPSTR lpMultiByteStr,     // buffer for new string
    int cbMultiByte,          // size of buffer
    LPCSTR lpDefaultChar,     // default for unmappable chars
    LPBOOL lpUsedDefaultChar  // set when default char used
)
{
    codepage_t *pCodePage = FindCodePage(CodePage);
    if (pCodePage) {
        int cbOut = 0;
        if (cchWideChar<0)
            cchWideChar = wcslen(lpWideCharStr) + 1;
        if (!lpMultiByteStr || !cbMultiByte)
            return cchWideChar;
        for (; cchWideChar>0 && cbMultiByte>0; cchWideChar--, cbMultiByte--, cbOut++) {
            *(lpMultiByteStr++) = pCodePage->wide_to_ascii[*((unsigned short*)lpWideCharStr++)];
        }
        return cbOut;
    }
    return KERNEL_WideCharToMultiByte(CodePage, dwFlags,
                                      lpWideCharStr, cchWideChar,
                                      lpMultiByteStr, cbMultiByte,
                                      lpDefaultChar, lpUsedDefaultChar);
}
Ejemplo n.º 2
0
/**
**  Parses an IIS Unicode Map file and store in the supplied array.
**  
**  This routine allocates the necessary memory to store the array values
**  in, and parses the supplied filename.
**  
**  @param iis_unicode_map  double pointer so we can allocate the memory
**  @param filename         the name of the file to open and parse
**  @param iCodePage        the codpage number to read the mappings from
**  
**  @return integer
**  
**  @retval HI_INVALID ARG     invalid argument
**  @retval HI_MEM_ALLOC_FAIL  memory allocation failed
**  @retval HI_INVALID_FILE    Could not open the supplied filename
**  @retval HI_SUCCESS         function was successful
*/
int hi_ui_parse_iis_unicode_map(int **iis_unicode_map, char *filename,
                                int iCodePage)
{
    int  iRet;
    FILE *fFile;

    if(!filename || iCodePage < 0)
    {
        return HI_INVALID_ARG;
    }

    fFile = fopen(filename, "r");
    if(fFile == NULL)
    {
        /*
        **  Couldn't open the file
        */
        return HI_INVALID_FILE;
    }

    *iis_unicode_map = (int *)xmalloc(sizeof(int) * 65536);
    if(*iis_unicode_map == NULL)
    {   
        fclose(fFile);
        return HI_MEM_ALLOC_FAIL;
    }

    memset(*iis_unicode_map, HI_UI_NON_ASCII_CODEPOINT, (sizeof(int)*65536));

    /*
    **  Find the correct codepage
    */
    iRet = FindCodePage(fFile, iCodePage);
    if (iRet)
    {
        //printf("** Did not find codepage\n");
        fclose(fFile);
        return iRet;
    }

    iRet = MapCodePoints(fFile, *iis_unicode_map);
    if (iRet)
    {
        //printf("** Error while parsing codepage.\n");
        fclose(fFile);
        return iRet;
    }

    fclose(fFile);
    return HI_SUCCESS;
}
int __stdcall CCP_MultiByteToWideChar(
    UINT CodePage,         // code page
    DWORD dwFlags,         // character-type options
    LPCSTR lpMultiByteStr, // string to map
    int cbMultiByte,       // number of bytes in string
    LPWSTR lpWideCharStr,  // wide-character buffer
    int cchWideChar        // size of buffer
)
{
    codepage_t *pCodePage = FindCodePage(CodePage);
    if (pCodePage) {
        int cchOut = 0;
        if (cbMultiByte<0)
            cbMultiByte = strlen(lpMultiByteStr) + 1;
        if (!lpWideCharStr || !cchWideChar)
            return cbMultiByte;
        for (; cchWideChar>0 && cbMultiByte>0; cchWideChar--, cbMultiByte--, cchOut++)
            *(lpWideCharStr++) = pCodePage->ascii_to_wide[*((unsigned char *)lpMultiByteStr++)];
        return cchOut;
    }
    return KERNEL_MultiByteToWideChar(CodePage, dwFlags,
                                      lpMultiByteStr, cbMultiByte,
                                      lpWideCharStr, cchWideChar);
}