intl/uconv/util/nsUnicodeDecodeHelper.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "unicpriv.h"
michael@0 7 #include "nsUnicodeDecodeHelper.h"
michael@0 8 #include "nsAutoPtr.h"
michael@0 9
michael@0 10 //----------------------------------------------------------------------
michael@0 11 // Class nsUnicodeDecodeHelper [implementation]
michael@0 12 nsresult nsUnicodeDecodeHelper::ConvertByTable(
michael@0 13 const char * aSrc,
michael@0 14 int32_t * aSrcLength,
michael@0 15 char16_t * aDest,
michael@0 16 int32_t * aDestLength,
michael@0 17 uScanClassID aScanClass,
michael@0 18 uShiftInTable * aShiftInTable,
michael@0 19 uMappingTable * aMappingTable,
michael@0 20 bool aErrorSignal)
michael@0 21 {
michael@0 22 const char * src = aSrc;
michael@0 23 int32_t srcLen = *aSrcLength;
michael@0 24 char16_t * dest = aDest;
michael@0 25 char16_t * destEnd = aDest + *aDestLength;
michael@0 26
michael@0 27 char16_t med;
michael@0 28 int32_t bcr; // byte count for read
michael@0 29 nsresult res = NS_OK;
michael@0 30
michael@0 31 while ((srcLen > 0) && (dest < destEnd)) {
michael@0 32 bool charFound;
michael@0 33 if (aScanClass == uMultibytesCharset) {
michael@0 34 NS_ASSERTION(aShiftInTable, "shift table missing");
michael@0 35 charFound = uScanShift(aShiftInTable, nullptr, (uint8_t *)src,
michael@0 36 reinterpret_cast<uint16_t*>(&med), srcLen,
michael@0 37 (uint32_t *)&bcr);
michael@0 38 } else {
michael@0 39 charFound = uScan(aScanClass, nullptr, (uint8_t *)src,
michael@0 40 reinterpret_cast<uint16_t*>(&med),
michael@0 41 srcLen, (uint32_t *)&bcr);
michael@0 42 }
michael@0 43 if (!charFound) {
michael@0 44 res = NS_OK_UDEC_MOREINPUT;
michael@0 45 break;
michael@0 46 }
michael@0 47
michael@0 48 if (!uMapCode((uTable*) aMappingTable, static_cast<uint16_t>(med), reinterpret_cast<uint16_t*>(dest))) {
michael@0 49 if (med < 0x20) {
michael@0 50 // somehow some table miss the 0x00 - 0x20 part
michael@0 51 *dest = med;
michael@0 52 } else {
michael@0 53 if (aErrorSignal) {
michael@0 54 res = NS_ERROR_ILLEGAL_INPUT;
michael@0 55 break;
michael@0 56 }
michael@0 57 // Unicode replacement value for unmappable chars
michael@0 58 *dest = 0xfffd;
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 src += bcr;
michael@0 63 srcLen -= bcr;
michael@0 64 dest++;
michael@0 65 }
michael@0 66
michael@0 67 if ((srcLen > 0) && (res == NS_OK)) res = NS_OK_UDEC_MOREOUTPUT;
michael@0 68
michael@0 69 *aSrcLength = src - aSrc;
michael@0 70 *aDestLength = dest - aDest;
michael@0 71 return res;
michael@0 72 }
michael@0 73
michael@0 74 nsresult nsUnicodeDecodeHelper::ConvertByMultiTable(
michael@0 75 const char * aSrc,
michael@0 76 int32_t * aSrcLength,
michael@0 77 char16_t * aDest,
michael@0 78 int32_t * aDestLength,
michael@0 79 int32_t aTableCount,
michael@0 80 const uRange * aRangeArray,
michael@0 81 uScanClassID * aScanClassArray,
michael@0 82 uMappingTable ** aMappingTable,
michael@0 83 bool aErrorSignal)
michael@0 84 {
michael@0 85 uint8_t * src = (uint8_t *)aSrc;
michael@0 86 int32_t srcLen = *aSrcLength;
michael@0 87 char16_t * dest = aDest;
michael@0 88 char16_t * destEnd = aDest + *aDestLength;
michael@0 89
michael@0 90 char16_t med;
michael@0 91 int32_t bcr; // byte count for read
michael@0 92 nsresult res = NS_OK;
michael@0 93 int32_t i;
michael@0 94
michael@0 95 while ((srcLen > 0) && (dest < destEnd))
michael@0 96 {
michael@0 97 bool done= false;
michael@0 98 bool passRangeCheck = false;
michael@0 99 bool passScan = false;
michael@0 100 for (i=0; (!done) && (i<aTableCount); i++)
michael@0 101 {
michael@0 102 if ((aRangeArray[i].min <= *src) && (*src <= aRangeArray[i].max))
michael@0 103 {
michael@0 104 passRangeCheck = true;
michael@0 105 if (uScan(aScanClassArray[i], nullptr, src,
michael@0 106 reinterpret_cast<uint16_t*>(&med), srcLen,
michael@0 107 (uint32_t *)&bcr))
michael@0 108 {
michael@0 109 passScan = true;
michael@0 110 done = uMapCode((uTable*) aMappingTable[i],
michael@0 111 static_cast<uint16_t>(med),
michael@0 112 reinterpret_cast<uint16_t*>(dest));
michael@0 113 } // if (uScan ... )
michael@0 114 } // if Range
michael@0 115 } // for loop
michael@0 116
michael@0 117 if(passRangeCheck && (! passScan))
michael@0 118 {
michael@0 119 if (res != NS_ERROR_ILLEGAL_INPUT)
michael@0 120 res = NS_OK_UDEC_MOREINPUT;
michael@0 121 break;
michael@0 122 }
michael@0 123 if(! done)
michael@0 124 {
michael@0 125 bcr = 1;
michael@0 126 if ((uint8_t)*src < 0x20) {
michael@0 127 // somehow some table miss the 0x00 - 0x20 part
michael@0 128 *dest = *src;
michael@0 129 } else if(*src == (uint8_t) 0xa0) {
michael@0 130 // handle nbsp
michael@0 131 *dest = 0x00a0;
michael@0 132 } else {
michael@0 133 // we need to decide how many byte we skip. We can use uScan to do this
michael@0 134 for (i=0; i<aTableCount; i++)
michael@0 135 {
michael@0 136 if ((aRangeArray[i].min <= *src) && (*src <= aRangeArray[i].max))
michael@0 137 {
michael@0 138 if (uScan(aScanClassArray[i], nullptr, src,
michael@0 139 reinterpret_cast<uint16_t*>(&med), srcLen,
michael@0 140 (uint32_t*)&bcr))
michael@0 141 {
michael@0 142 // match the patten
michael@0 143
michael@0 144 int32_t k;
michael@0 145 for(k = 1; k < bcr; k++)
michael@0 146 {
michael@0 147 if(0 == (src[k] & 0x80))
michael@0 148 { // only skip if all bytes > 0x80
michael@0 149 // if we hit bytes <= 0x80, skip only one byte
michael@0 150 bcr = 1;
michael@0 151 break;
michael@0 152 }
michael@0 153 }
michael@0 154 break;
michael@0 155 }
michael@0 156 }
michael@0 157 }
michael@0 158 // treat it as NSBR if bcr == 1 and it is 0xa0
michael@0 159 if ((1==bcr)&&(*src == (uint8_t)0xa0 )) {
michael@0 160 *dest = 0x00a0;
michael@0 161 } else {
michael@0 162 if (aErrorSignal) {
michael@0 163 res = NS_ERROR_ILLEGAL_INPUT;
michael@0 164 break;
michael@0 165 }
michael@0 166 *dest = 0xfffd;
michael@0 167 }
michael@0 168 }
michael@0 169 }
michael@0 170
michael@0 171 src += bcr;
michael@0 172 srcLen -= bcr;
michael@0 173 dest++;
michael@0 174 } // while
michael@0 175
michael@0 176 if ((srcLen > 0) && (res == NS_OK)) res = NS_OK_UDEC_MOREOUTPUT;
michael@0 177
michael@0 178 *aSrcLength = src - (uint8_t *)aSrc;
michael@0 179 *aDestLength = dest - aDest;
michael@0 180 return res;
michael@0 181 }
michael@0 182
michael@0 183 nsresult nsUnicodeDecodeHelper::ConvertByFastTable(
michael@0 184 const char * aSrc,
michael@0 185 int32_t * aSrcLength,
michael@0 186 char16_t * aDest,
michael@0 187 int32_t * aDestLength,
michael@0 188 const char16_t * aFastTable,
michael@0 189 int32_t aTableSize,
michael@0 190 bool aErrorSignal)
michael@0 191 {
michael@0 192 uint8_t * src = (uint8_t *)aSrc;
michael@0 193 uint8_t * srcEnd = src;
michael@0 194 char16_t * dest = aDest;
michael@0 195
michael@0 196 nsresult res;
michael@0 197 if (*aSrcLength > *aDestLength) {
michael@0 198 srcEnd += (*aDestLength);
michael@0 199 res = NS_PARTIAL_MORE_OUTPUT;
michael@0 200 } else {
michael@0 201 srcEnd += (*aSrcLength);
michael@0 202 res = NS_OK;
michael@0 203 }
michael@0 204
michael@0 205 for (; src<srcEnd;) {
michael@0 206 *dest = aFastTable[*src];
michael@0 207 if (*dest == 0xfffd && aErrorSignal) {
michael@0 208 res = NS_ERROR_ILLEGAL_INPUT;
michael@0 209 break;
michael@0 210 }
michael@0 211 src++;
michael@0 212 dest++;
michael@0 213 }
michael@0 214
michael@0 215 *aSrcLength = src - (uint8_t *)aSrc;
michael@0 216 *aDestLength = dest - aDest;
michael@0 217 return res;
michael@0 218 }
michael@0 219
michael@0 220 nsresult nsUnicodeDecodeHelper::CreateFastTable(
michael@0 221 uMappingTable * aMappingTable,
michael@0 222 char16_t * aFastTable,
michael@0 223 int32_t aTableSize)
michael@0 224 {
michael@0 225 int32_t tableSize = aTableSize;
michael@0 226 int32_t buffSize = aTableSize;
michael@0 227 nsAutoArrayPtr<char> buff(new char [buffSize]);
michael@0 228
michael@0 229 char * p = buff;
michael@0 230 for (int32_t i=0; i<aTableSize; i++) *(p++) = i;
michael@0 231 return ConvertByTable(buff, &buffSize, aFastTable, &tableSize,
michael@0 232 u1ByteCharset, nullptr, aMappingTable);
michael@0 233 }
michael@0 234

mercurial