intl/uconv/util/nsUCSupport.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/uconv/util/nsUCSupport.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,638 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsUCSupport.h"
    1.10 +#include "nsUnicodeDecodeHelper.h"
    1.11 +#include "nsUnicodeEncodeHelper.h"
    1.12 +#include <algorithm>
    1.13 +
    1.14 +#define DEFAULT_BUFFER_CAPACITY 16
    1.15 +
    1.16 +// XXX review the buffer growth limitation code
    1.17 +
    1.18 +//----------------------------------------------------------------------
    1.19 +// Class nsBasicDecoderSupport [implementation]
    1.20 +
    1.21 +nsBasicDecoderSupport::nsBasicDecoderSupport()
    1.22 +  : mErrBehavior(kOnError_Recover)
    1.23 +{
    1.24 +}
    1.25 +
    1.26 +nsBasicDecoderSupport::~nsBasicDecoderSupport()
    1.27 +{
    1.28 +}
    1.29 +
    1.30 +//----------------------------------------------------------------------
    1.31 +// Interface nsISupports [implementation]
    1.32 +
    1.33 +#ifdef DEBUG
    1.34 +NS_IMPL_ISUPPORTS(nsBasicDecoderSupport,
    1.35 +                  nsIUnicodeDecoder,
    1.36 +                  nsIBasicDecoder)
    1.37 +#else
    1.38 +NS_IMPL_ISUPPORTS(nsBasicDecoderSupport, nsIUnicodeDecoder)
    1.39 +#endif
    1.40 +
    1.41 +//----------------------------------------------------------------------
    1.42 +// Interface nsIUnicodeDecoder [implementation]
    1.43 +
    1.44 +void
    1.45 +nsBasicDecoderSupport::SetInputErrorBehavior(int32_t aBehavior)
    1.46 +{
    1.47 +  NS_ABORT_IF_FALSE(aBehavior == kOnError_Recover || aBehavior == kOnError_Signal,
    1.48 +                    "Unknown behavior for SetInputErrorBehavior");
    1.49 +  mErrBehavior = aBehavior;
    1.50 +}
    1.51 +
    1.52 +char16_t
    1.53 +nsBasicDecoderSupport::GetCharacterForUnMapped()
    1.54 +{
    1.55 +  return char16_t(0xfffd); // Unicode REPLACEMENT CHARACTER
    1.56 +}
    1.57 +
    1.58 +//----------------------------------------------------------------------
    1.59 +// Class nsBufferDecoderSupport [implementation]
    1.60 +
    1.61 +nsBufferDecoderSupport::nsBufferDecoderSupport(uint32_t aMaxLengthFactor)
    1.62 +  : nsBasicDecoderSupport(),
    1.63 +    mMaxLengthFactor(aMaxLengthFactor)
    1.64 +{
    1.65 +  mBufferCapacity = DEFAULT_BUFFER_CAPACITY;
    1.66 +  mBuffer = new char[mBufferCapacity];
    1.67 +
    1.68 +  Reset();
    1.69 +}
    1.70 +
    1.71 +nsBufferDecoderSupport::~nsBufferDecoderSupport()
    1.72 +{
    1.73 +  delete [] mBuffer;
    1.74 +}
    1.75 +
    1.76 +void nsBufferDecoderSupport::FillBuffer(const char ** aSrc, int32_t aSrcLength)
    1.77 +{
    1.78 +  int32_t bcr = std::min(mBufferCapacity - mBufferLength, aSrcLength);
    1.79 +  memcpy(mBuffer + mBufferLength, *aSrc, bcr);
    1.80 +  mBufferLength += bcr;
    1.81 +  (*aSrc) += bcr;
    1.82 +}
    1.83 +
    1.84 +//----------------------------------------------------------------------
    1.85 +// Subclassing of nsBasicDecoderSupport class [implementation]
    1.86 +
    1.87 +NS_IMETHODIMP nsBufferDecoderSupport::Convert(const char * aSrc,
    1.88 +                                              int32_t * aSrcLength,
    1.89 +                                              char16_t * aDest,
    1.90 +                                              int32_t * aDestLength)
    1.91 +{
    1.92 +  // we do all operations using pointers internally
    1.93 +  const char * src = aSrc;
    1.94 +  const char * srcEnd = aSrc + *aSrcLength;
    1.95 +  char16_t * dest = aDest;
    1.96 +  char16_t * destEnd = aDest + *aDestLength;
    1.97 +
    1.98 +  int32_t bcr, bcw; // byte counts for read & write;
    1.99 +  nsresult res = NS_OK;
   1.100 +
   1.101 +  // do we have some residual data from the last conversion?
   1.102 +  if (mBufferLength > 0) {
   1.103 +    if (dest == destEnd) {
   1.104 +      res = NS_OK_UDEC_MOREOUTPUT;
   1.105 +    } else {
   1.106 +      for (;;) {
   1.107 +        // we need new data to add to the buffer
   1.108 +        if (src == srcEnd) {
   1.109 +          res = NS_OK_UDEC_MOREINPUT;
   1.110 +          break;
   1.111 +        }
   1.112 +
   1.113 +        // fill that buffer
   1.114 +        int32_t buffLen = mBufferLength;  // initial buffer length
   1.115 +        FillBuffer(&src, srcEnd - src);
   1.116 +
   1.117 +        // convert that buffer
   1.118 +        bcr = mBufferLength;
   1.119 +        bcw = destEnd - dest;
   1.120 +        res = ConvertNoBuff(mBuffer, &bcr, dest, &bcw);
   1.121 +        dest += bcw;
   1.122 +
   1.123 +        // Detect invalid input character
   1.124 +        if (res == NS_ERROR_ILLEGAL_INPUT && mErrBehavior == kOnError_Signal) {
   1.125 +          break;
   1.126 +        }
   1.127 +
   1.128 +        if ((res == NS_OK_UDEC_MOREINPUT) && (bcw == 0)) {
   1.129 +          res = NS_ERROR_UNEXPECTED;
   1.130 +#if defined(DEBUG_yokoyama) || defined(DEBUG_ftang)
   1.131 +          NS_ERROR("This should not happen. Internal buffer may be corrupted.");
   1.132 +#endif
   1.133 +          break;
   1.134 +        } else {
   1.135 +          if (bcr < buffLen) {
   1.136 +            // we didn't convert that residual data - unfill the buffer
   1.137 +            src -= mBufferLength - buffLen;
   1.138 +            mBufferLength = buffLen;
   1.139 +#if defined(DEBUG_yokoyama) || defined(DEBUG_ftang)
   1.140 +            NS_ERROR("This should not happen. Internal buffer may be corrupted.");
   1.141 +#endif
   1.142 +          } else {
   1.143 +            // the buffer and some extra data was converted - unget the rest
   1.144 +            src -= mBufferLength - bcr;
   1.145 +            mBufferLength = 0;
   1.146 +            res = NS_OK;
   1.147 +          }
   1.148 +          break;
   1.149 +        }
   1.150 +      }
   1.151 +    }
   1.152 +  }
   1.153 +
   1.154 +  if (res == NS_OK) {
   1.155 +    bcr = srcEnd - src;
   1.156 +    bcw = destEnd - dest;
   1.157 +    res = ConvertNoBuff(src, &bcr, dest, &bcw);
   1.158 +    src += bcr;
   1.159 +    dest += bcw;
   1.160 +
   1.161 +    // if we have partial input, store it in our internal buffer.
   1.162 +    if (res == NS_OK_UDEC_MOREINPUT) {
   1.163 +      bcr = srcEnd - src;
   1.164 +      // make sure buffer is large enough
   1.165 +      if (bcr > mBufferCapacity) {
   1.166 +          // somehow we got into an error state and the buffer is growing out
   1.167 +          // of control
   1.168 +          res = NS_ERROR_UNEXPECTED;
   1.169 +      } else {
   1.170 +          FillBuffer(&src, bcr);
   1.171 +      }
   1.172 +    }
   1.173 +  }
   1.174 +
   1.175 +  *aSrcLength   -= srcEnd - src;
   1.176 +  *aDestLength  -= destEnd - dest;
   1.177 +  return res;
   1.178 +}
   1.179 +
   1.180 +NS_IMETHODIMP nsBufferDecoderSupport::Reset()
   1.181 +{
   1.182 +  mBufferLength = 0;
   1.183 +  return NS_OK;
   1.184 +}
   1.185 +
   1.186 +NS_IMETHODIMP nsBufferDecoderSupport::GetMaxLength(const char* aSrc,
   1.187 +                                                   int32_t aSrcLength,
   1.188 +                                                   int32_t* aDestLength)
   1.189 +{
   1.190 +  NS_ASSERTION(mMaxLengthFactor != 0, "Must override GetMaxLength!");
   1.191 +  *aDestLength = aSrcLength * mMaxLengthFactor;
   1.192 +  return NS_OK;
   1.193 +}
   1.194 +
   1.195 +//----------------------------------------------------------------------
   1.196 +// Class nsTableDecoderSupport [implementation]
   1.197 +
   1.198 +nsTableDecoderSupport::nsTableDecoderSupport(uScanClassID aScanClass,
   1.199 +                                             uShiftInTable * aShiftInTable,
   1.200 +                                             uMappingTable  * aMappingTable,
   1.201 +                                             uint32_t aMaxLengthFactor)
   1.202 +: nsBufferDecoderSupport(aMaxLengthFactor)
   1.203 +{
   1.204 +  mScanClass = aScanClass;
   1.205 +  mShiftInTable = aShiftInTable;
   1.206 +  mMappingTable = aMappingTable;
   1.207 +}
   1.208 +
   1.209 +nsTableDecoderSupport::~nsTableDecoderSupport()
   1.210 +{
   1.211 +}
   1.212 +
   1.213 +//----------------------------------------------------------------------
   1.214 +// Subclassing of nsBufferDecoderSupport class [implementation]
   1.215 +
   1.216 +NS_IMETHODIMP nsTableDecoderSupport::ConvertNoBuff(const char * aSrc,
   1.217 +                                                   int32_t * aSrcLength,
   1.218 +                                                   char16_t * aDest,
   1.219 +                                                   int32_t * aDestLength)
   1.220 +{
   1.221 +  return nsUnicodeDecodeHelper::ConvertByTable(aSrc, aSrcLength,
   1.222 +                                               aDest, aDestLength,
   1.223 +                                               mScanClass,
   1.224 +                                               mShiftInTable, mMappingTable,
   1.225 +                                               mErrBehavior == kOnError_Signal);
   1.226 +}
   1.227 +
   1.228 +//----------------------------------------------------------------------
   1.229 +// Class nsMultiTableDecoderSupport [implementation]
   1.230 +
   1.231 +nsMultiTableDecoderSupport::nsMultiTableDecoderSupport(
   1.232 +                            int32_t aTableCount,
   1.233 +                            const uRange * aRangeArray,
   1.234 +                            uScanClassID * aScanClassArray,
   1.235 +                            uMappingTable ** aMappingTable,
   1.236 +                            uint32_t aMaxLengthFactor)
   1.237 +: nsBufferDecoderSupport(aMaxLengthFactor)
   1.238 +{
   1.239 +  mTableCount = aTableCount;
   1.240 +  mRangeArray = aRangeArray;
   1.241 +  mScanClassArray = aScanClassArray;
   1.242 +  mMappingTable = aMappingTable;
   1.243 +}
   1.244 +
   1.245 +nsMultiTableDecoderSupport::~nsMultiTableDecoderSupport()
   1.246 +{
   1.247 +}
   1.248 +
   1.249 +//----------------------------------------------------------------------
   1.250 +// Subclassing of nsBufferDecoderSupport class [implementation]
   1.251 +
   1.252 +NS_IMETHODIMP nsMultiTableDecoderSupport::ConvertNoBuff(const char * aSrc,
   1.253 +                                                        int32_t * aSrcLength,
   1.254 +                                                        char16_t * aDest,
   1.255 +                                                        int32_t * aDestLength)
   1.256 +{
   1.257 +  return nsUnicodeDecodeHelper::ConvertByMultiTable(aSrc, aSrcLength,
   1.258 +                                                    aDest, aDestLength,
   1.259 +                                                    mTableCount, mRangeArray,
   1.260 +                                                    mScanClassArray,
   1.261 +                                                    mMappingTable,
   1.262 +                                                    mErrBehavior == kOnError_Signal);
   1.263 +}
   1.264 +
   1.265 +//----------------------------------------------------------------------
   1.266 +// Class nsOneByteDecoderSupport [implementation]
   1.267 +
   1.268 +nsOneByteDecoderSupport::nsOneByteDecoderSupport(
   1.269 +                         uMappingTable  * aMappingTable)
   1.270 +  : nsBasicDecoderSupport()
   1.271 +  , mMappingTable(aMappingTable)
   1.272 +  , mFastTableCreated(false)
   1.273 +  , mFastTableMutex("nsOneByteDecoderSupport mFastTableMutex")
   1.274 +{
   1.275 +}
   1.276 +
   1.277 +nsOneByteDecoderSupport::~nsOneByteDecoderSupport()
   1.278 +{
   1.279 +}
   1.280 +
   1.281 +//----------------------------------------------------------------------
   1.282 +// Subclassing of nsBasicDecoderSupport class [implementation]
   1.283 +
   1.284 +NS_IMETHODIMP nsOneByteDecoderSupport::Convert(const char * aSrc,
   1.285 +                                              int32_t * aSrcLength,
   1.286 +                                              char16_t * aDest,
   1.287 +                                              int32_t * aDestLength)
   1.288 +{
   1.289 +  if (!mFastTableCreated) {
   1.290 +    // Probably better to make this non-lazy and get rid of the mutex
   1.291 +    mozilla::MutexAutoLock autoLock(mFastTableMutex);
   1.292 +    if (!mFastTableCreated) {
   1.293 +      nsresult res = nsUnicodeDecodeHelper::CreateFastTable(
   1.294 +                         mMappingTable, mFastTable, ONE_BYTE_TABLE_SIZE);
   1.295 +      if (NS_FAILED(res)) return res;
   1.296 +      mFastTableCreated = true;
   1.297 +    }
   1.298 +  }
   1.299 +
   1.300 +  return nsUnicodeDecodeHelper::ConvertByFastTable(aSrc, aSrcLength,
   1.301 +                                                   aDest, aDestLength,
   1.302 +                                                   mFastTable,
   1.303 +                                                   ONE_BYTE_TABLE_SIZE,
   1.304 +                                                   mErrBehavior == kOnError_Signal);
   1.305 +}
   1.306 +
   1.307 +NS_IMETHODIMP nsOneByteDecoderSupport::GetMaxLength(const char * aSrc,
   1.308 +                                                    int32_t aSrcLength,
   1.309 +                                                    int32_t * aDestLength)
   1.310 +{
   1.311 +  // single byte to Unicode converter
   1.312 +  *aDestLength = aSrcLength;
   1.313 +  return NS_OK_UDEC_EXACTLENGTH;
   1.314 +}
   1.315 +
   1.316 +NS_IMETHODIMP nsOneByteDecoderSupport::Reset()
   1.317 +{
   1.318 +  // nothing to reset, no internal state in this case
   1.319 +  return NS_OK;
   1.320 +}
   1.321 +
   1.322 +//----------------------------------------------------------------------
   1.323 +// Class nsBasicEncoder [implementation]
   1.324 +nsBasicEncoder::nsBasicEncoder()
   1.325 +{
   1.326 +}
   1.327 +
   1.328 +nsBasicEncoder::~nsBasicEncoder()
   1.329 +{
   1.330 +}
   1.331 +
   1.332 +//----------------------------------------------------------------------
   1.333 +// Interface nsISupports [implementation]
   1.334 +
   1.335 +NS_IMPL_ADDREF(nsBasicEncoder)
   1.336 +NS_IMPL_RELEASE(nsBasicEncoder)
   1.337 +#ifdef DEBUG
   1.338 +NS_IMPL_QUERY_INTERFACE(nsBasicEncoder,
   1.339 +                        nsIUnicodeEncoder,
   1.340 +                        nsIBasicEncoder)
   1.341 +#else
   1.342 +NS_IMPL_QUERY_INTERFACE(nsBasicEncoder,
   1.343 +                        nsIUnicodeEncoder)
   1.344 +#endif
   1.345 +//----------------------------------------------------------------------
   1.346 +// Class nsEncoderSupport [implementation]
   1.347 +
   1.348 +nsEncoderSupport::nsEncoderSupport(uint32_t aMaxLengthFactor) :
   1.349 +  mMaxLengthFactor(aMaxLengthFactor)
   1.350 +{
   1.351 +  mBufferCapacity = DEFAULT_BUFFER_CAPACITY;
   1.352 +  mBuffer = new char[mBufferCapacity];
   1.353 +
   1.354 +  mErrBehavior = kOnError_Signal;
   1.355 +  mErrChar = 0;
   1.356 +
   1.357 +  Reset();
   1.358 +}
   1.359 +
   1.360 +nsEncoderSupport::~nsEncoderSupport()
   1.361 +{
   1.362 +  delete [] mBuffer;
   1.363 +}
   1.364 +
   1.365 +NS_IMETHODIMP nsEncoderSupport::ConvertNoBuff(const char16_t * aSrc,
   1.366 +                                              int32_t * aSrcLength,
   1.367 +                                              char * aDest,
   1.368 +                                              int32_t * aDestLength)
   1.369 +{
   1.370 +  // we do all operations using pointers internally
   1.371 +  const char16_t * src = aSrc;
   1.372 +  const char16_t * srcEnd = aSrc + *aSrcLength;
   1.373 +  char * dest = aDest;
   1.374 +  char * destEnd = aDest + *aDestLength;
   1.375 +
   1.376 +  int32_t bcr, bcw; // byte counts for read & write;
   1.377 +  nsresult res;
   1.378 +
   1.379 +  for (;;) {
   1.380 +    bcr = srcEnd - src;
   1.381 +    bcw = destEnd - dest;
   1.382 +    res = ConvertNoBuffNoErr(src, &bcr, dest, &bcw);
   1.383 +    src += bcr;
   1.384 +    dest += bcw;
   1.385 +
   1.386 +    if (res == NS_ERROR_UENC_NOMAPPING) {
   1.387 +      if (mErrBehavior == kOnError_Replace) {
   1.388 +        const char16_t buff[] = {mErrChar};
   1.389 +        bcr = 1;
   1.390 +        bcw = destEnd - dest;
   1.391 +        src--; // back the input: maybe the guy won't consume consume anything.
   1.392 +        res = ConvertNoBuffNoErr(buff, &bcr, dest, &bcw);
   1.393 +        src += bcr;
   1.394 +        dest += bcw;
   1.395 +        if (res != NS_OK) break;
   1.396 +      } else if (mErrBehavior == kOnError_CallBack) {
   1.397 +        bcw = destEnd - dest;
   1.398 +        src--;
   1.399 +        res = mErrEncoder->Convert(*src, dest, &bcw);
   1.400 +        dest += bcw;
   1.401 +        // if enought output space then the last char was used
   1.402 +        if (res != NS_OK_UENC_MOREOUTPUT) src++;
   1.403 +        if (res != NS_OK) break;
   1.404 +      } else break;
   1.405 +    }
   1.406 +    else break;
   1.407 +  }
   1.408 +
   1.409 +  *aSrcLength   -= srcEnd - src;
   1.410 +  *aDestLength  -= destEnd - dest;
   1.411 +  return res;
   1.412 +}
   1.413 +
   1.414 +NS_IMETHODIMP nsEncoderSupport::FinishNoBuff(char * aDest,
   1.415 +                                             int32_t * aDestLength)
   1.416 +{
   1.417 +  *aDestLength = 0;
   1.418 +  return NS_OK;
   1.419 +}
   1.420 +
   1.421 +nsresult nsEncoderSupport::FlushBuffer(char ** aDest, const char * aDestEnd)
   1.422 +{
   1.423 +  int32_t bcr, bcw; // byte counts for read & write;
   1.424 +  nsresult res = NS_OK;
   1.425 +  char * dest = *aDest;
   1.426 +
   1.427 +  if (mBufferStart < mBufferEnd) {
   1.428 +    bcr = mBufferEnd - mBufferStart;
   1.429 +    bcw = aDestEnd - dest;
   1.430 +    if (bcw < bcr) bcr = bcw;
   1.431 +    memcpy(dest, mBufferStart, bcr);
   1.432 +    dest += bcr;
   1.433 +    mBufferStart += bcr;
   1.434 +
   1.435 +    if (mBufferStart < mBufferEnd) res = NS_OK_UENC_MOREOUTPUT;
   1.436 +  }
   1.437 +
   1.438 +  *aDest = dest;
   1.439 +  return res;
   1.440 +}
   1.441 +
   1.442 +
   1.443 +//----------------------------------------------------------------------
   1.444 +// Interface nsIUnicodeEncoder [implementation]
   1.445 +
   1.446 +NS_IMETHODIMP nsEncoderSupport::Convert(const char16_t * aSrc,
   1.447 +                                        int32_t * aSrcLength,
   1.448 +                                        char * aDest,
   1.449 +                                        int32_t * aDestLength)
   1.450 +{
   1.451 +  // we do all operations using pointers internally
   1.452 +  const char16_t * src = aSrc;
   1.453 +  const char16_t * srcEnd = aSrc + *aSrcLength;
   1.454 +  char * dest = aDest;
   1.455 +  char * destEnd = aDest + *aDestLength;
   1.456 +
   1.457 +  int32_t bcr, bcw; // byte counts for read & write;
   1.458 +  nsresult res;
   1.459 +
   1.460 +  res = FlushBuffer(&dest, destEnd);
   1.461 +  if (res == NS_OK_UENC_MOREOUTPUT) goto final;
   1.462 +
   1.463 +  bcr = srcEnd - src;
   1.464 +  bcw = destEnd - dest;
   1.465 +  res = ConvertNoBuff(src, &bcr, dest, &bcw);
   1.466 +  src += bcr;
   1.467 +  dest += bcw;
   1.468 +  if ((res == NS_OK_UENC_MOREOUTPUT) && (dest < destEnd)) {
   1.469 +    // convert exactly one character into the internal buffer
   1.470 +    // at this point, there should be at least a char in the input
   1.471 +    for (;;) {
   1.472 +      bcr = 1;
   1.473 +      bcw = mBufferCapacity;
   1.474 +      res = ConvertNoBuff(src, &bcr, mBuffer, &bcw);
   1.475 +
   1.476 +      if (res == NS_OK_UENC_MOREOUTPUT) {
   1.477 +        delete [] mBuffer;
   1.478 +        mBufferCapacity *= 2;
   1.479 +        mBuffer = new char [mBufferCapacity];
   1.480 +      } else {
   1.481 +        src += bcr;
   1.482 +        mBufferStart = mBufferEnd = mBuffer;
   1.483 +        mBufferEnd += bcw;
   1.484 +        break;
   1.485 +      }
   1.486 +    }
   1.487 +
   1.488 +    res = FlushBuffer(&dest, destEnd);
   1.489 +  }
   1.490 +
   1.491 +final:
   1.492 +  *aSrcLength   -= srcEnd - src;
   1.493 +  *aDestLength  -= destEnd - dest;
   1.494 +  return res;
   1.495 +}
   1.496 +
   1.497 +NS_IMETHODIMP nsEncoderSupport::Finish(char * aDest, int32_t * aDestLength)
   1.498 +{
   1.499 +  // we do all operations using pointers internally
   1.500 +  char * dest = aDest;
   1.501 +  char * destEnd = aDest + *aDestLength;
   1.502 +
   1.503 +  int32_t bcw; // byte count for write;
   1.504 +  nsresult res;
   1.505 +
   1.506 +  res = FlushBuffer(&dest, destEnd);
   1.507 +  if (res == NS_OK_UENC_MOREOUTPUT) goto final;
   1.508 +
   1.509 +  // do the finish into the internal buffer.
   1.510 +  for (;;) {
   1.511 +    bcw = mBufferCapacity;
   1.512 +    res = FinishNoBuff(mBuffer, &bcw);
   1.513 +
   1.514 +    if (res == NS_OK_UENC_MOREOUTPUT) {
   1.515 +      delete [] mBuffer;
   1.516 +      mBufferCapacity *= 2;
   1.517 +      mBuffer = new char [mBufferCapacity];
   1.518 +    } else {
   1.519 +      mBufferStart = mBufferEnd = mBuffer;
   1.520 +      mBufferEnd += bcw;
   1.521 +      break;
   1.522 +    }
   1.523 +  }
   1.524 +
   1.525 +  res = FlushBuffer(&dest, destEnd);
   1.526 +
   1.527 +final:
   1.528 +  *aDestLength  -= destEnd - dest;
   1.529 +  return res;
   1.530 +}
   1.531 +
   1.532 +NS_IMETHODIMP nsEncoderSupport::Reset()
   1.533 +{
   1.534 +  mBufferStart = mBufferEnd = mBuffer;
   1.535 +  return NS_OK;
   1.536 +}
   1.537 +
   1.538 +NS_IMETHODIMP nsEncoderSupport::SetOutputErrorBehavior(
   1.539 +                                int32_t aBehavior,
   1.540 +                                nsIUnicharEncoder * aEncoder,
   1.541 +                                char16_t aChar)
   1.542 +{
   1.543 +  if (aBehavior == kOnError_CallBack && !aEncoder)
   1.544 +    return NS_ERROR_NULL_POINTER;
   1.545 +
   1.546 +  mErrEncoder = aEncoder;
   1.547 +  mErrBehavior = aBehavior;
   1.548 +  mErrChar = aChar;
   1.549 +  return NS_OK;
   1.550 +}
   1.551 +
   1.552 +NS_IMETHODIMP
   1.553 +nsEncoderSupport::GetMaxLength(const char16_t * aSrc,
   1.554 +                               int32_t aSrcLength,
   1.555 +                               int32_t * aDestLength)
   1.556 +{
   1.557 +  *aDestLength = aSrcLength * mMaxLengthFactor;
   1.558 +  return NS_OK;
   1.559 +}
   1.560 +
   1.561 +
   1.562 +//----------------------------------------------------------------------
   1.563 +// Class nsTableEncoderSupport [implementation]
   1.564 +
   1.565 +nsTableEncoderSupport::nsTableEncoderSupport(uScanClassID aScanClass,
   1.566 +                                             uShiftOutTable * aShiftOutTable,
   1.567 +                                             uMappingTable  * aMappingTable,
   1.568 +                                             uint32_t aMaxLengthFactor)
   1.569 +: nsEncoderSupport(aMaxLengthFactor)
   1.570 +{
   1.571 +  mScanClass = aScanClass;
   1.572 +  mShiftOutTable = aShiftOutTable,
   1.573 +  mMappingTable = aMappingTable;
   1.574 +}
   1.575 +
   1.576 +nsTableEncoderSupport::nsTableEncoderSupport(uScanClassID aScanClass,
   1.577 +                                             uMappingTable  * aMappingTable,
   1.578 +                                             uint32_t aMaxLengthFactor)
   1.579 +: nsEncoderSupport(aMaxLengthFactor)
   1.580 +{
   1.581 +  mScanClass = aScanClass;
   1.582 +  mShiftOutTable = nullptr;
   1.583 +  mMappingTable = aMappingTable;
   1.584 +}
   1.585 +
   1.586 +nsTableEncoderSupport::~nsTableEncoderSupport()
   1.587 +{
   1.588 +}
   1.589 +
   1.590 +//----------------------------------------------------------------------
   1.591 +// Subclassing of nsEncoderSupport class [implementation]
   1.592 +
   1.593 +NS_IMETHODIMP nsTableEncoderSupport::ConvertNoBuffNoErr(
   1.594 +                                     const char16_t * aSrc,
   1.595 +                                     int32_t * aSrcLength,
   1.596 +                                     char * aDest,
   1.597 +                                     int32_t * aDestLength)
   1.598 +{
   1.599 +  return nsUnicodeEncodeHelper::ConvertByTable(aSrc, aSrcLength,
   1.600 +                                               aDest, aDestLength,
   1.601 +                                               mScanClass,
   1.602 +                                               mShiftOutTable, mMappingTable);
   1.603 +}
   1.604 +
   1.605 +//----------------------------------------------------------------------
   1.606 +// Class nsMultiTableEncoderSupport [implementation]
   1.607 +
   1.608 +nsMultiTableEncoderSupport::nsMultiTableEncoderSupport(
   1.609 +                            int32_t aTableCount,
   1.610 +                            uScanClassID * aScanClassArray,
   1.611 +                            uShiftOutTable ** aShiftOutTable,
   1.612 +                            uMappingTable  ** aMappingTable,
   1.613 +                            uint32_t aMaxLengthFactor)
   1.614 +: nsEncoderSupport(aMaxLengthFactor)
   1.615 +{
   1.616 +  mTableCount = aTableCount;
   1.617 +  mScanClassArray = aScanClassArray;
   1.618 +  mShiftOutTable = aShiftOutTable;
   1.619 +  mMappingTable = aMappingTable;
   1.620 +}
   1.621 +
   1.622 +nsMultiTableEncoderSupport::~nsMultiTableEncoderSupport()
   1.623 +{
   1.624 +}
   1.625 +
   1.626 +//----------------------------------------------------------------------
   1.627 +// Subclassing of nsEncoderSupport class [implementation]
   1.628 +
   1.629 +NS_IMETHODIMP nsMultiTableEncoderSupport::ConvertNoBuffNoErr(
   1.630 +                                          const char16_t * aSrc,
   1.631 +                                          int32_t * aSrcLength,
   1.632 +                                          char * aDest,
   1.633 +                                          int32_t * aDestLength)
   1.634 +{
   1.635 +  return nsUnicodeEncodeHelper::ConvertByMultiTable(aSrc, aSrcLength,
   1.636 +                                                    aDest, aDestLength,
   1.637 +                                                    mTableCount,
   1.638 +                                                    mScanClassArray,
   1.639 +                                                    mShiftOutTable,
   1.640 +                                                    mMappingTable);
   1.641 +}

mercurial