michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "unicpriv.h" michael@0: #include "nsUnicodeEncodeHelper.h" michael@0: #include "nsDebug.h" michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Class nsUnicodeEncodeHelper [implementation] michael@0: nsresult nsUnicodeEncodeHelper::ConvertByTable( michael@0: const char16_t * aSrc, michael@0: int32_t * aSrcLength, michael@0: char * aDest, michael@0: int32_t * aDestLength, michael@0: uScanClassID aScanClass, michael@0: uShiftOutTable * aShiftOutTable, michael@0: uMappingTable * aMappingTable) michael@0: { michael@0: const char16_t * src = aSrc; michael@0: const char16_t * srcEnd = aSrc + *aSrcLength; michael@0: char * dest = aDest; michael@0: int32_t destLen = *aDestLength; michael@0: michael@0: char16_t med; michael@0: int32_t bcw; // byte count for write; michael@0: nsresult res = NS_OK; michael@0: michael@0: while (src < srcEnd) { michael@0: if (!uMapCode((uTable*) aMappingTable, static_cast(*(src++)), reinterpret_cast(&med))) { michael@0: if (aScanClass == u1ByteCharset && *(src - 1) < 0x20) { michael@0: // some tables are missing the 0x00 - 0x20 part michael@0: med = *(src - 1); michael@0: } else { michael@0: res = NS_ERROR_UENC_NOMAPPING; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: bool charFound; michael@0: if (aScanClass == uMultibytesCharset) { michael@0: NS_ASSERTION(aShiftOutTable, "shift table missing"); michael@0: charFound = uGenerateShift(aShiftOutTable, 0, med, michael@0: (uint8_t *)dest, destLen, michael@0: (uint32_t *)&bcw); michael@0: } else { michael@0: charFound = uGenerate(aScanClass, 0, med, michael@0: (uint8_t *)dest, destLen, michael@0: (uint32_t *)&bcw); michael@0: } michael@0: if (!charFound) { michael@0: src--; michael@0: res = NS_OK_UENC_MOREOUTPUT; michael@0: break; michael@0: } michael@0: michael@0: dest += bcw; michael@0: destLen -= bcw; michael@0: } michael@0: michael@0: *aSrcLength = src - aSrc; michael@0: *aDestLength = dest - aDest; michael@0: return res; michael@0: } michael@0: michael@0: nsresult nsUnicodeEncodeHelper::ConvertByMultiTable( michael@0: const char16_t * aSrc, michael@0: int32_t * aSrcLength, michael@0: char * aDest, michael@0: int32_t * aDestLength, michael@0: int32_t aTableCount, michael@0: uScanClassID * aScanClassArray, michael@0: uShiftOutTable ** aShiftOutTable, michael@0: uMappingTable ** aMappingTable) michael@0: { michael@0: const char16_t * src = aSrc; michael@0: const char16_t * srcEnd = aSrc + *aSrcLength; michael@0: char * dest = aDest; michael@0: int32_t destLen = *aDestLength; michael@0: michael@0: char16_t med; michael@0: int32_t bcw; // byte count for write; michael@0: nsresult res = NS_OK; michael@0: int32_t i; michael@0: michael@0: while (src < srcEnd) { michael@0: for (i=0; i(*src), reinterpret_cast(&med))) break; michael@0: michael@0: src++; michael@0: if (i == aTableCount) { michael@0: res = NS_ERROR_UENC_NOMAPPING; michael@0: break; michael@0: } michael@0: michael@0: bool charFound; michael@0: if (aScanClassArray[i] == uMultibytesCharset) { michael@0: NS_ASSERTION(aShiftOutTable[i], "shift table missing"); michael@0: charFound = uGenerateShift(aShiftOutTable[i], 0, med, michael@0: (uint8_t *)dest, destLen, michael@0: (uint32_t *)&bcw); michael@0: } michael@0: else michael@0: charFound = uGenerate(aScanClassArray[i], 0, med, michael@0: (uint8_t *)dest, destLen, michael@0: (uint32_t *)&bcw); michael@0: if (!charFound) { michael@0: src--; michael@0: res = NS_OK_UENC_MOREOUTPUT; michael@0: break; michael@0: } michael@0: michael@0: dest += bcw; michael@0: destLen -= bcw; michael@0: } michael@0: michael@0: *aSrcLength = src - aSrc; michael@0: *aDestLength = dest - aDest; michael@0: return res; michael@0: }