예제 #1
0
// ---------------------------------------------------------------------------
//  IconvGNUTranscoder: Implementation of the virtual transcoder API
// ---------------------------------------------------------------------------
unsigned int    IconvGNUTranscoder::transcodeFrom
(
    const   XMLByte* const          srcData
    , const unsigned int            srcCount
    ,       XMLCh* const            toFill
    , const unsigned int            maxChars
    ,       unsigned int&           bytesEaten
    ,       unsigned char* const    charSizes )
{
    // Transcode TO XMLCh
    const char*  startSrc = (const char*) srcData;
    const char*  endSrc = (const char*) srcData + srcCount;

    char    tmpWBuff[gTempBuffArraySize];
    char    *startTarget = 0;
    char    *wBufPtr = 0;
    size_t    len = maxChars * uChSize();

    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {
        if (len > gTempBuffArraySize) {
            wBufPtr = (char*) getMemoryManager()->allocate
            (
                len * sizeof(char)
            );//new char[len];
            if (wBufPtr == NULL)
                return 0;
            startTarget = wBufPtr;
        } else
            startTarget = tmpWBuff;
    } else
    startTarget = (char *) toFill;

    // Do character-by-character transcoding
    char    *orgTarget = startTarget;
    size_t    srcLen = srcCount;
    size_t    prevSrcLen = srcLen;
    unsigned int toReturn = 0;
    bytesEaten = 0;
    for (size_t cnt = 0; cnt < maxChars && srcLen; cnt++) {
        size_t    rc = iconvFrom(startSrc, &srcLen, &orgTarget, uChSize());
        if (rc == (size_t)-1) {
            if (errno != E2BIG || prevSrcLen == srcLen) {
                if (wBufPtr)
                    getMemoryManager()->deallocate(wBufPtr);//delete [] wBufPtr;
                ThrowXMLwithMemMgr(TranscodingException, XMLExcepts::Trans_BadSrcSeq, getMemoryManager());
            }
        }
        charSizes[cnt] = prevSrcLen - srcLen;
        prevSrcLen = srcLen;
        bytesEaten += charSizes[cnt];
        startSrc = endSrc - srcLen;
        toReturn++;
    }
    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER)
        mbsToXML (startTarget, toReturn, toFill, toReturn);
    if (wBufPtr)
        getMemoryManager()->deallocate(wBufPtr);//delete [] wBufPtr;
    return toReturn;
}
예제 #2
0
bool IconvGNULCPTranscoder::transcode(const   char* const    toTranscode
                       ,       XMLCh* const    toFill
                       , const unsigned int    maxChars
                       , MemoryManager* const  manager)
{
    // Check for a couple of psycho corner cases
    if (!toTranscode || !maxChars)
    {
        toFill[0] = 0;
        return true;
    }

    if (!*toTranscode)
    {
        toFill[0] = 0;
        return true;
    }

    size_t wLent = calcRequiredSize(toTranscode);
    if (wLent > maxChars)
        wLent = maxChars;

    char    tmpWBuff[gTempBuffArraySize];
    char    *wideCharBuf = 0;
    char    *wBufPtr = 0;
    size_t    len = wLent * uChSize();

    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {
        if (len > gTempBuffArraySize) {
            wBufPtr = (char*) manager->allocate
            (
                len * sizeof(char)
            );//new char[len];
            if (wBufPtr == NULL)
                return 0;
            wideCharBuf = wBufPtr;
        } else
            wideCharBuf = tmpWBuff;
    } else
        wideCharBuf = (char *) toFill;

    size_t    flen = strlen(toTranscode); // wLent;
    char    *ptr = wideCharBuf;
    size_t    rc = iconvFrom(toTranscode, &flen, &ptr, len);
    if (rc == (size_t)-1) {
        if (wBufPtr)
            manager->deallocate(wBufPtr);//delete [] wBufPtr;
        return false;
    }

    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER)
        mbsToXML (wideCharBuf, wLent, toFill, wLent);
    if (wBufPtr)
        manager->deallocate(wBufPtr);//delete [] wBufPtr;

    toFill[wLent] = 0x00;
    return true;
}
예제 #3
0
bool IconvGNULCPTranscoder::transcode(const   char* const    toTranscode
                       ,       XMLCh* const    toFill
                       , const XMLSize_t       maxChars
                       , MemoryManager* const  manager)
{
    // Check for a couple of psycho corner cases
    if (!toTranscode || !maxChars)
    {
        toFill[0] = 0;
        return true;
    }

    if (!*toTranscode)
    {
        toFill[0] = 0;
        return true;
    }

    XMLSize_t wLent = calcRequiredSize(toTranscode);
    if (wLent > maxChars)
        wLent = maxChars;

    char    tmpWBuff[gTempBuffArraySize];
    char    *wideCharBuf = 0;
    char    *wBufPtr = 0;
    ArrayJanitor<char>  janBuf(wBufPtr, manager);
    size_t    len = wLent * uChSize();

    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {
        if (len > gTempBuffArraySize) {
            wBufPtr = (char*) manager->allocate(len * sizeof(char));//new char[len];
            janBuf.reset(wBufPtr, manager);
            wideCharBuf = wBufPtr;
        } else
            wideCharBuf = tmpWBuff;
    } else
        wideCharBuf = (char *) toFill;

    size_t    flen = strlen(toTranscode); // wLent;
    char    *ptr = wideCharBuf;
    size_t rc;

    {
      XMLMutexLock lockConverter(&fMutex);
      rc = iconvFrom(toTranscode, &flen, &ptr, len);
    }

    if (rc == (size_t)-1) {
        return false;
    }

    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER)
        mbsToXML (wideCharBuf, toFill, wLent);

    toFill[wLent] = 0x00;
    return true;
}
예제 #4
0
XMLCh* IconvGNULCPTranscoder::transcode(const char* const toTranscode,
                                        MemoryManager* const manager)
{
    if (!toTranscode)
        return 0;

    XMLCh* retVal = 0;
    if (*toTranscode) {
        const unsigned int wLent = calcRequiredSize(toTranscode, manager);
        if (wLent == 0) {
            retVal = (XMLCh*) manager->allocate(sizeof(XMLCh));//new XMLCh[1];
            retVal[0] = 0;
            return retVal;
        }

        char    tmpWBuff[gTempBuffArraySize];
        char    *wideCharBuf = 0;
        char    *wBufPtr = 0;
        size_t  len = wLent * uChSize();

        retVal = (XMLCh*) manager->allocate((wLent + 1) * sizeof(XMLCh));//new XMLCh[wLent + 1];
        if (retVal == NULL)
            return NULL;
        if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {
            if (len > gTempBuffArraySize) {
                wBufPtr = (char*) manager->allocate(len * sizeof(char));//new char[len];
                if (wBufPtr == NULL)
                    return 0;
                wideCharBuf = wBufPtr;
            } else
                wideCharBuf = tmpWBuff;
        } else
            wideCharBuf = (char *) retVal;

        size_t    flen = strlen(toTranscode);
        char    *ptr = wideCharBuf;
        size_t    rc = iconvFrom(toTranscode, &flen, &ptr, len);
        if (rc == (size_t) -1) {
            if (wBufPtr)
            manager->deallocate(wBufPtr);//delete [] wBufPtr;
            return NULL;
        }
        if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER)
            mbsToXML (wideCharBuf, wLent, retVal, wLent);
        if (wBufPtr)
            manager->deallocate(wBufPtr);//delete [] wBufPtr;
        retVal[wLent] = 0x00;
    }
    else {
        retVal = (XMLCh*) manager->allocate(sizeof(XMLCh));//new XMLCh[1];
        if (retVal == NULL )
            return 0;
        retVal[0] = 0;
    }
    return retVal;
}
예제 #5
0
XMLCh* IconvGNULCPTranscoder::transcode(const char* const toTranscode,
                                        MemoryManager* const manager)
{
    if (!toTranscode)
        return 0;

    XMLCh* retVal = 0;
    if (!*toTranscode) {
        retVal = (XMLCh*) manager->allocate(sizeof(XMLCh));//new XMLCh[1];
        retVal[0] = 0;
        return retVal;
    }

    XMLSize_t wLent = calcRequiredSize(toTranscode, manager);
    if (wLent == 0) {
        retVal = (XMLCh*) manager->allocate(sizeof(XMLCh));//new XMLCh[1];
        retVal[0] = 0;
        return retVal;
    }

    char    tmpWBuff[gTempBuffArraySize];
    char    *wideCharBuf = 0;
    char    *wBufPtr = 0;
    ArrayJanitor<char>  janBuf(wBufPtr, manager);
    size_t  len = wLent * uChSize();

    retVal = (XMLCh*) manager->allocate((wLent + 1) * sizeof(XMLCh));//new XMLCh[wLent + 1];
    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {
        if (len > gTempBuffArraySize) {
            wBufPtr = (char*) manager->allocate(len * sizeof(char));//new char[len];
            janBuf.reset(wBufPtr, manager);
            wideCharBuf = wBufPtr;
        } else
            wideCharBuf = tmpWBuff;
    } else
        wideCharBuf = (char *) retVal;

    size_t    flen = strlen(toTranscode);
    char    *ptr = wideCharBuf;
    size_t rc;

    {
      XMLMutexLock lockConverter(&fMutex);
      rc = iconvFrom(toTranscode, &flen, &ptr, len);
    }

    if (rc == (size_t) -1) {
        return NULL;
    }
    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER)
        mbsToXML (wideCharBuf, retVal, wLent);
    retVal[wLent] = 0x00;

    return retVal;
}