intl/uconv/util/nsUnicodeEncodeHelper.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "unicpriv.h"
     7 #include "nsUnicodeEncodeHelper.h"
     8 #include "nsDebug.h"
    10 //----------------------------------------------------------------------
    11 // Class nsUnicodeEncodeHelper [implementation]
    12 nsresult nsUnicodeEncodeHelper::ConvertByTable(
    13                                      const char16_t * aSrc, 
    14                                      int32_t * aSrcLength, 
    15                                      char * aDest, 
    16                                      int32_t * aDestLength, 
    17                                      uScanClassID aScanClass,
    18                                      uShiftOutTable * aShiftOutTable,
    19                                      uMappingTable  * aMappingTable)
    20 {
    21   const char16_t * src = aSrc;
    22   const char16_t * srcEnd = aSrc + *aSrcLength;
    23   char * dest = aDest;
    24   int32_t destLen = *aDestLength;
    26   char16_t med;
    27   int32_t bcw; // byte count for write;
    28   nsresult res = NS_OK;
    30   while (src < srcEnd) {
    31     if (!uMapCode((uTable*) aMappingTable, static_cast<char16_t>(*(src++)), reinterpret_cast<uint16_t*>(&med))) {
    32       if (aScanClass == u1ByteCharset && *(src - 1) < 0x20) {
    33         // some tables are missing the 0x00 - 0x20 part
    34         med = *(src - 1);
    35       } else {
    36         res = NS_ERROR_UENC_NOMAPPING;
    37         break;
    38       }
    39     }
    41     bool charFound;
    42     if (aScanClass == uMultibytesCharset) {
    43       NS_ASSERTION(aShiftOutTable, "shift table missing");
    44       charFound = uGenerateShift(aShiftOutTable, 0, med,
    45                                  (uint8_t *)dest, destLen, 
    46                                  (uint32_t *)&bcw);
    47     } else {
    48       charFound = uGenerate(aScanClass, 0, med,
    49                             (uint8_t *)dest, destLen, 
    50                             (uint32_t *)&bcw);
    51     }
    52     if (!charFound) {
    53       src--;
    54       res = NS_OK_UENC_MOREOUTPUT;
    55       break;
    56     }
    58     dest += bcw;
    59     destLen -= bcw;
    60   }
    62   *aSrcLength = src - aSrc;
    63   *aDestLength  = dest - aDest;
    64   return res;
    65 }
    67 nsresult nsUnicodeEncodeHelper::ConvertByMultiTable(
    68                                      const char16_t * aSrc, 
    69                                      int32_t * aSrcLength, 
    70                                      char * aDest, 
    71                                      int32_t * aDestLength, 
    72                                      int32_t aTableCount, 
    73                                      uScanClassID * aScanClassArray,
    74                                      uShiftOutTable ** aShiftOutTable, 
    75                                      uMappingTable  ** aMappingTable)
    76 {
    77   const char16_t * src = aSrc;
    78   const char16_t * srcEnd = aSrc + *aSrcLength;
    79   char * dest = aDest;
    80   int32_t destLen = *aDestLength;
    82   char16_t med;
    83   int32_t bcw; // byte count for write;
    84   nsresult res = NS_OK;
    85   int32_t i;
    87   while (src < srcEnd) {
    88     for (i=0; i<aTableCount; i++) 
    89       if (uMapCode((uTable*) aMappingTable[i], static_cast<uint16_t>(*src), reinterpret_cast<uint16_t*>(&med))) break;
    91     src++;
    92     if (i == aTableCount) {
    93       res = NS_ERROR_UENC_NOMAPPING;
    94       break;
    95     }
    97     bool charFound;
    98     if (aScanClassArray[i] == uMultibytesCharset) {
    99       NS_ASSERTION(aShiftOutTable[i], "shift table missing");
   100       charFound = uGenerateShift(aShiftOutTable[i], 0, med,
   101                                  (uint8_t *)dest, destLen,
   102                                  (uint32_t *)&bcw);
   103     }
   104     else
   105       charFound = uGenerate(aScanClassArray[i], 0, med,
   106                             (uint8_t *)dest, destLen, 
   107                             (uint32_t *)&bcw);
   108     if (!charFound) { 
   109       src--;
   110       res = NS_OK_UENC_MOREOUTPUT;
   111       break;
   112     }
   114     dest += bcw;
   115     destLen -= bcw;
   116   }
   118   *aSrcLength = src - aSrc;
   119   *aDestLength  = dest - aDest;
   120   return res;
   121 }

mercurial