~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/intl/uconv/util/nsUCSupport.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is Mozilla Communicator client code.
 
16
 *
 
17
 * The Initial Developer of the Original Code is 
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Pierre Phaneuf <pp@ludusdesign.com>
 
24
 *
 
25
 *
 
26
 * Alternatively, the contents of this file may be used under the terms of
 
27
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
28
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
29
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
30
 * of those above. If you wish to allow use of your version of this file only
 
31
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
32
 * use your version of this file under the terms of the NPL, indicate your
 
33
 * decision by deleting the provisions above and replace them with the notice
 
34
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
35
 * the provisions above, a recipient may use your version of this file under
 
36
 * the terms of any one of the NPL, the GPL or the LGPL.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
#include "pratom.h"
 
41
#include "nsIComponentManager.h"
 
42
#include "nsICharRepresentable.h"
 
43
#include "nsUCSupport.h"
 
44
 
 
45
static NS_DEFINE_CID(kUnicodeEncodeHelperCID, NS_UNICODEENCODEHELPER_CID);
 
46
static NS_DEFINE_CID(kUnicodeDecodeHelperCID, NS_UNICODEDECODEHELPER_CID);
 
47
 
 
48
#define DEFAULT_BUFFER_CAPACITY 16
 
49
 
 
50
// XXX review the buffer growth limitation code
 
51
 
 
52
//----------------------------------------------------------------------
 
53
// Class nsBasicDecoderSupport [implementation]
 
54
 
 
55
nsBasicDecoderSupport::nsBasicDecoderSupport() 
 
56
{
 
57
}
 
58
 
 
59
nsBasicDecoderSupport::~nsBasicDecoderSupport() 
 
60
{
 
61
}
 
62
 
 
63
//----------------------------------------------------------------------
 
64
// Interface nsISupports [implementation]
 
65
 
 
66
NS_IMPL_ADDREF(nsBasicDecoderSupport)
 
67
NS_IMPL_RELEASE(nsBasicDecoderSupport)
 
68
#ifdef NS_DEBUG
 
69
NS_IMPL_QUERY_INTERFACE2(nsBasicDecoderSupport, nsIUnicodeDecoder, nsIBasicDecoder)
 
70
#else
 
71
NS_IMPL_QUERY_INTERFACE1(nsBasicDecoderSupport, nsIUnicodeDecoder)
 
72
#endif
 
73
 
 
74
//----------------------------------------------------------------------
 
75
// Interface nsIUnicodeDecoder [implementation]
 
76
 
 
77
//----------------------------------------------------------------------
 
78
// Class nsBufferDecoderSupport [implementation]
 
79
 
 
80
nsBufferDecoderSupport::nsBufferDecoderSupport(PRUint32 aMaxLengthFactor) 
 
81
  : nsBasicDecoderSupport(),
 
82
    mMaxLengthFactor(aMaxLengthFactor)
 
83
{
 
84
  mBufferCapacity = DEFAULT_BUFFER_CAPACITY;
 
85
  mBuffer = new char[mBufferCapacity];
 
86
 
 
87
  Reset();
 
88
}
 
89
 
 
90
nsBufferDecoderSupport::~nsBufferDecoderSupport() 
 
91
{
 
92
  delete [] mBuffer;
 
93
}
 
94
 
 
95
void nsBufferDecoderSupport::FillBuffer(const char ** aSrc, PRInt32 aSrcLength)
 
96
{
 
97
  PRInt32 bcr = PR_MIN(mBufferCapacity - mBufferLength, aSrcLength);
 
98
  memcpy(mBuffer + mBufferLength, *aSrc, bcr);
 
99
  mBufferLength += bcr;
 
100
  (*aSrc) += bcr;
 
101
}
 
102
 
 
103
void nsBufferDecoderSupport::DoubleBuffer()
 
104
{
 
105
  mBufferCapacity *= 2;
 
106
  char * newBuffer = new char [mBufferCapacity];
 
107
  if (mBufferLength > 0) memcpy(newBuffer, mBuffer, mBufferLength);
 
108
  delete [] mBuffer;
 
109
  mBuffer = newBuffer;
 
110
}
 
111
 
 
112
//----------------------------------------------------------------------
 
113
// Subclassing of nsBasicDecoderSupport class [implementation]
 
114
 
 
115
NS_IMETHODIMP nsBufferDecoderSupport::Convert(const char * aSrc, 
 
116
                                              PRInt32 * aSrcLength,
 
117
                                              PRUnichar * aDest, 
 
118
                                              PRInt32 * aDestLength)
 
119
{
 
120
  // we do all operations using pointers internally
 
121
  const char * src = aSrc;
 
122
  const char * srcEnd = aSrc + *aSrcLength;
 
123
  PRUnichar * dest = aDest;
 
124
  PRUnichar * destEnd = aDest + *aDestLength;
 
125
 
 
126
  PRInt32 bcr, bcw; // byte counts for read & write;
 
127
  nsresult res = NS_OK;
 
128
 
 
129
  // do we have some residual data from the last conversion?
 
130
  if (mBufferLength > 0) if (dest == destEnd) {
 
131
    res = NS_OK_UDEC_MOREOUTPUT;
 
132
  } else for (;;) {
 
133
    // we need new data to add to the buffer
 
134
    if (src == srcEnd) {
 
135
      res = NS_OK_UDEC_MOREINPUT;
 
136
      break;
 
137
    }
 
138
 
 
139
    // fill that buffer
 
140
    PRInt32 buffLen = mBufferLength;  // initial buffer length
 
141
    FillBuffer(&src, srcEnd - src);
 
142
 
 
143
    // convert that buffer
 
144
    bcr = mBufferLength;
 
145
    bcw = destEnd - dest;
 
146
    res = ConvertNoBuff(mBuffer, &bcr, dest, &bcw);
 
147
    dest += bcw;
 
148
 
 
149
    if ((res == NS_OK_UDEC_MOREINPUT) && (bcw == 0)) {
 
150
        res = NS_ERROR_UNEXPECTED;
 
151
#if defined(DEBUG_yokoyama) || defined(DEBUG_ftang)
 
152
        NS_ASSERTION(0, "This should not happen. Internal buffer may be corrupted.");
 
153
#endif
 
154
        break;
 
155
    } else {
 
156
      if (bcr < buffLen) {
 
157
        // we didn't convert that residual data - unfill the buffer
 
158
        src -= mBufferLength - buffLen;
 
159
        mBufferLength = buffLen;
 
160
#if defined(DEBUG_yokoyama) || defined(DEBUG_ftang)
 
161
        NS_ASSERTION(0, "This should not happen. Internal buffer may be corrupted.");
 
162
#endif
 
163
      } else {
 
164
        // the buffer and some extra data was converted - unget the rest
 
165
        src -= mBufferLength - bcr;
 
166
        mBufferLength = 0;
 
167
        res = NS_OK;
 
168
      }
 
169
      break;
 
170
    }
 
171
  }
 
172
 
 
173
  if (res == NS_OK) {
 
174
    bcr = srcEnd - src;
 
175
    bcw = destEnd - dest;
 
176
    res = ConvertNoBuff(src, &bcr, dest, &bcw);
 
177
    src += bcr;
 
178
    dest += bcw;
 
179
 
 
180
    // if we have partial input, store it in our internal buffer.
 
181
    if (res == NS_OK_UDEC_MOREINPUT) {
 
182
      bcr = srcEnd - src;
 
183
      // make sure buffer is large enough
 
184
      if (bcr > mBufferCapacity) {
 
185
          // somehow we got into an error state and the buffer is growing out of control
 
186
          res = NS_ERROR_UNEXPECTED;
 
187
      } else {
 
188
          FillBuffer(&src, bcr);
 
189
      }
 
190
    }
 
191
  }
 
192
 
 
193
  *aSrcLength   -= srcEnd - src;
 
194
  *aDestLength  -= destEnd - dest;
 
195
  return res;
 
196
}
 
197
 
 
198
NS_IMETHODIMP nsBufferDecoderSupport::Reset()
 
199
{
 
200
  mBufferLength = 0;
 
201
  return NS_OK;
 
202
}
 
203
 
 
204
NS_IMETHODIMP nsBufferDecoderSupport::GetMaxLength(const char* aSrc,
 
205
                                                   PRInt32 aSrcLength,
 
206
                                                   PRInt32* aDestLength)
 
207
{
 
208
  NS_ASSERTION(mMaxLengthFactor != 0, "Must override GetMaxLength!");
 
209
  *aDestLength = aSrcLength * mMaxLengthFactor;
 
210
  return NS_OK;
 
211
}
 
212
 
 
213
//----------------------------------------------------------------------
 
214
// Class nsTableDecoderSupport [implementation]
 
215
 
 
216
nsTableDecoderSupport::nsTableDecoderSupport(uShiftTable * aShiftTable, 
 
217
                                             uMappingTable  * aMappingTable,
 
218
                                             PRUint32 aMaxLengthFactor) 
 
219
: nsBufferDecoderSupport(aMaxLengthFactor)
 
220
{
 
221
  mHelper = NULL;
 
222
  mShiftTable = aShiftTable;
 
223
  mMappingTable = aMappingTable;
 
224
}
 
225
 
 
226
nsTableDecoderSupport::~nsTableDecoderSupport() 
 
227
{
 
228
  NS_IF_RELEASE(mHelper);
 
229
}
 
230
 
 
231
//----------------------------------------------------------------------
 
232
// Subclassing of nsBufferDecoderSupport class [implementation]
 
233
 
 
234
NS_IMETHODIMP nsTableDecoderSupport::ConvertNoBuff(const char * aSrc, 
 
235
                                                   PRInt32 * aSrcLength, 
 
236
                                                   PRUnichar * aDest, 
 
237
                                                   PRInt32 * aDestLength)
 
238
{
 
239
  nsresult res;
 
240
 
 
241
  if (mHelper == nsnull) {
 
242
    res = nsComponentManager::CreateInstance(kUnicodeDecodeHelperCID, NULL, 
 
243
        NS_GET_IID(nsIUnicodeDecodeHelper), (void**) & mHelper);
 
244
    
 
245
    if (NS_FAILED(res)) return NS_ERROR_UDEC_NOHELPER;
 
246
  }
 
247
 
 
248
  res = mHelper->ConvertByTable(aSrc, aSrcLength, aDest, aDestLength, 
 
249
      mShiftTable, mMappingTable);
 
250
  return res;
 
251
}
 
252
 
 
253
//----------------------------------------------------------------------
 
254
// Class nsMultiTableDecoderSupport [implementation]
 
255
 
 
256
nsMultiTableDecoderSupport::nsMultiTableDecoderSupport(
 
257
                            PRInt32 aTableCount,
 
258
                            const uRange * aRangeArray, 
 
259
                            uShiftTable ** aShiftTable, 
 
260
                            uMappingTable ** aMappingTable,
 
261
                            PRUint32 aMaxLengthFactor) 
 
262
: nsBufferDecoderSupport(aMaxLengthFactor)
 
263
{
 
264
  mHelper = NULL;
 
265
  mTableCount = aTableCount;
 
266
  mRangeArray = aRangeArray;
 
267
  mShiftTable = aShiftTable;
 
268
  mMappingTable = aMappingTable;
 
269
}
 
270
 
 
271
nsMultiTableDecoderSupport::~nsMultiTableDecoderSupport() 
 
272
{
 
273
  NS_IF_RELEASE(mHelper);
 
274
}
 
275
 
 
276
//----------------------------------------------------------------------
 
277
// Subclassing of nsBufferDecoderSupport class [implementation]
 
278
 
 
279
NS_IMETHODIMP nsMultiTableDecoderSupport::ConvertNoBuff(const char * aSrc, 
 
280
                                                        PRInt32 * aSrcLength, 
 
281
                                                        PRUnichar * aDest, 
 
282
                                                        PRInt32 * aDestLength)
 
283
{
 
284
  nsresult res;
 
285
 
 
286
  if (mHelper == nsnull) {
 
287
    res = nsComponentManager::CreateInstance(kUnicodeDecodeHelperCID, NULL, 
 
288
        NS_GET_IID(nsIUnicodeDecodeHelper), (void**) &mHelper);
 
289
    
 
290
    if (NS_FAILED(res)) return NS_ERROR_UDEC_NOHELPER;
 
291
  }
 
292
 
 
293
  res = mHelper->ConvertByMultiTable(aSrc, aSrcLength, aDest, aDestLength, 
 
294
      mTableCount, mRangeArray, mShiftTable, mMappingTable);
 
295
  return res;
 
296
}
 
297
 
 
298
//----------------------------------------------------------------------
 
299
// Class nsOneByteDecoderSupport [implementation]
 
300
 
 
301
nsOneByteDecoderSupport::nsOneByteDecoderSupport(
 
302
                         uShiftTable * aShiftTable, 
 
303
                         uMappingTable  * aMappingTable) 
 
304
: nsBasicDecoderSupport()
 
305
{
 
306
  mHelper = NULL;
 
307
  mShiftTable = aShiftTable;
 
308
  mMappingTable = aMappingTable;
 
309
}
 
310
 
 
311
nsOneByteDecoderSupport::~nsOneByteDecoderSupport() 
 
312
{
 
313
  NS_IF_RELEASE(mHelper);
 
314
}
 
315
 
 
316
//----------------------------------------------------------------------
 
317
// Subclassing of nsBasicDecoderSupport class [implementation]
 
318
 
 
319
NS_IMETHODIMP nsOneByteDecoderSupport::Convert(const char * aSrc, 
 
320
                                              PRInt32 * aSrcLength, 
 
321
                                              PRUnichar * aDest, 
 
322
                                              PRInt32 * aDestLength)
 
323
{
 
324
  nsresult res;
 
325
 
 
326
  if (mHelper == nsnull) {
 
327
    res = nsComponentManager::CreateInstance(kUnicodeDecodeHelperCID, NULL, 
 
328
        NS_GET_IID(nsIUnicodeDecodeHelper), (void**) &mHelper);
 
329
    if (NS_FAILED(res)) return NS_ERROR_UDEC_NOHELPER;
 
330
 
 
331
    res = mHelper -> CreateFastTable(mShiftTable, mMappingTable, mFastTable, 
 
332
        ONE_BYTE_TABLE_SIZE);
 
333
    if (NS_FAILED(res)) return res;
 
334
  }
 
335
 
 
336
  res = mHelper->ConvertByFastTable(aSrc, aSrcLength, aDest, aDestLength, 
 
337
      mFastTable, ONE_BYTE_TABLE_SIZE);
 
338
  return res;
 
339
}
 
340
 
 
341
NS_IMETHODIMP nsOneByteDecoderSupport::GetMaxLength(const char * aSrc, 
 
342
                                                    PRInt32 aSrcLength, 
 
343
                                                    PRInt32 * aDestLength)
 
344
{
 
345
  // single byte to Unicode converter
 
346
  *aDestLength = aSrcLength;
 
347
  return NS_OK_UDEC_EXACTLENGTH;
 
348
}
 
349
 
 
350
NS_IMETHODIMP nsOneByteDecoderSupport::Reset()
 
351
{
 
352
  // nothing to reset, no internal state in this case
 
353
  return NS_OK;
 
354
}
 
355
 
 
356
//----------------------------------------------------------------------
 
357
// Class nsBasicEncoder [implementation]
 
358
nsBasicEncoder::nsBasicEncoder() 
 
359
{
 
360
}
 
361
 
 
362
nsBasicEncoder::~nsBasicEncoder() 
 
363
{
 
364
}
 
365
 
 
366
//----------------------------------------------------------------------
 
367
// Interface nsISupports [implementation]
 
368
 
 
369
NS_IMPL_ADDREF(nsBasicEncoder)
 
370
NS_IMPL_RELEASE(nsBasicEncoder)
 
371
#ifdef NS_DEBUG
 
372
NS_IMPL_QUERY_INTERFACE3(nsBasicEncoder,
 
373
                         nsIUnicodeEncoder,
 
374
                         nsICharRepresentable, nsIBasicEncoder)
 
375
#else
 
376
NS_IMPL_QUERY_INTERFACE2(nsBasicEncoder,
 
377
                         nsIUnicodeEncoder,
 
378
                         nsICharRepresentable)
 
379
#endif
 
380
//----------------------------------------------------------------------
 
381
// Class nsEncoderSupport [implementation]
 
382
 
 
383
nsEncoderSupport::nsEncoderSupport(PRUint32 aMaxLengthFactor) :
 
384
  mMaxLengthFactor(aMaxLengthFactor)
 
385
{
 
386
  mBufferCapacity = DEFAULT_BUFFER_CAPACITY;
 
387
  mBuffer = new char[mBufferCapacity];
 
388
 
 
389
  mErrBehavior = kOnError_Signal;
 
390
  mErrChar = 0;
 
391
  mErrEncoder = NULL;
 
392
 
 
393
  Reset();
 
394
}
 
395
 
 
396
nsEncoderSupport::~nsEncoderSupport() 
 
397
{
 
398
  delete [] mBuffer;
 
399
  NS_IF_RELEASE(mErrEncoder);
 
400
}
 
401
 
 
402
NS_IMETHODIMP nsEncoderSupport::ConvertNoBuff(const PRUnichar * aSrc, 
 
403
                                              PRInt32 * aSrcLength, 
 
404
                                              char * aDest, 
 
405
                                              PRInt32 * aDestLength)
 
406
{
 
407
  // we do all operations using pointers internally
 
408
  const PRUnichar * src = aSrc;
 
409
  const PRUnichar * srcEnd = aSrc + *aSrcLength;
 
410
  char * dest = aDest;
 
411
  char * destEnd = aDest + *aDestLength;
 
412
 
 
413
  PRInt32 bcr, bcw; // byte counts for read & write;
 
414
  nsresult res;
 
415
 
 
416
  for (;;) {
 
417
    bcr = srcEnd - src;
 
418
    bcw = destEnd - dest;
 
419
    res = ConvertNoBuffNoErr(src, &bcr, dest, &bcw);
 
420
    src += bcr;
 
421
    dest += bcw;
 
422
 
 
423
    if (res == NS_ERROR_UENC_NOMAPPING) {
 
424
      if (mErrBehavior == kOnError_Replace) {
 
425
        const PRUnichar buff[] = {mErrChar};
 
426
        bcr = 1;
 
427
        bcw = destEnd - dest;
 
428
        src--; // back the input: maybe the guy won't consume consume anything.
 
429
        res = ConvertNoBuffNoErr(buff, &bcr, dest, &bcw);
 
430
        src += bcr;
 
431
        dest += bcw;
 
432
        if (res != NS_OK) break;
 
433
      } else if (mErrBehavior == kOnError_CallBack) {
 
434
        bcw = destEnd - dest;
 
435
        src--;
 
436
        res = mErrEncoder->Convert(*src, dest, &bcw);
 
437
        dest += bcw;
 
438
        // if enought output space then the last char was used
 
439
        if (res != NS_OK_UENC_MOREOUTPUT) src++;
 
440
        if (res != NS_OK) break;
 
441
      } else break;
 
442
    }
 
443
    else break;
 
444
  }
 
445
 
 
446
  *aSrcLength   -= srcEnd - src;
 
447
  *aDestLength  -= destEnd - dest;
 
448
  return res;
 
449
}
 
450
 
 
451
NS_IMETHODIMP nsEncoderSupport::FinishNoBuff(char * aDest, 
 
452
                                             PRInt32 * aDestLength)
 
453
{
 
454
  *aDestLength = 0;
 
455
  return NS_OK;
 
456
}
 
457
 
 
458
nsresult nsEncoderSupport::FlushBuffer(char ** aDest, const char * aDestEnd)
 
459
{
 
460
  PRInt32 bcr, bcw; // byte counts for read & write;
 
461
  nsresult res = NS_OK;
 
462
  char * dest = *aDest;
 
463
 
 
464
  if (mBufferStart < mBufferEnd) {
 
465
    bcr = mBufferEnd - mBufferStart;
 
466
    bcw = aDestEnd - dest;
 
467
    if (bcw < bcr) bcr = bcw;
 
468
    memcpy(dest, mBufferStart, bcr);
 
469
    dest += bcr;
 
470
    mBufferStart += bcr;
 
471
 
 
472
    if (mBufferStart < mBufferEnd) res = NS_OK_UENC_MOREOUTPUT;
 
473
  }
 
474
 
 
475
  *aDest = dest;
 
476
  return res;
 
477
}
 
478
 
 
479
 
 
480
//----------------------------------------------------------------------
 
481
// Interface nsIUnicodeEncoder [implementation]
 
482
 
 
483
NS_IMETHODIMP nsEncoderSupport::Convert(const PRUnichar * aSrc, 
 
484
                                        PRInt32 * aSrcLength, 
 
485
                                        char * aDest, 
 
486
                                        PRInt32 * aDestLength)
 
487
{
 
488
  // we do all operations using pointers internally
 
489
  const PRUnichar * src = aSrc;
 
490
  const PRUnichar * srcEnd = aSrc + *aSrcLength;
 
491
  char * dest = aDest;
 
492
  char * destEnd = aDest + *aDestLength;
 
493
 
 
494
  PRInt32 bcr, bcw; // byte counts for read & write;
 
495
  nsresult res;
 
496
 
 
497
  res = FlushBuffer(&dest, destEnd);
 
498
  if (res == NS_OK_UENC_MOREOUTPUT) goto final;
 
499
 
 
500
  bcr = srcEnd - src;
 
501
  bcw = destEnd - dest;
 
502
  res = ConvertNoBuff(src, &bcr, dest, &bcw);
 
503
  src += bcr;
 
504
  dest += bcw;
 
505
  if ((res == NS_OK_UENC_MOREOUTPUT) && (dest < destEnd)) {
 
506
    // convert exactly one character into the internal buffer
 
507
    // at this point, there should be at least a char in the input
 
508
    for (;;) {
 
509
      bcr = 1;
 
510
      bcw = mBufferCapacity;
 
511
      res = ConvertNoBuff(src, &bcr, mBuffer, &bcw);
 
512
 
 
513
      if (res == NS_OK_UENC_MOREOUTPUT) {
 
514
        delete [] mBuffer;
 
515
        mBufferCapacity *= 2;
 
516
        mBuffer = new char [mBufferCapacity];
 
517
      } else {
 
518
        src += bcr;
 
519
        mBufferStart = mBufferEnd = mBuffer;
 
520
        mBufferEnd += bcw;
 
521
        break;
 
522
      }
 
523
    }
 
524
 
 
525
    res = FlushBuffer(&dest, destEnd);
 
526
  }
 
527
 
 
528
final:
 
529
  *aSrcLength   -= srcEnd - src;
 
530
  *aDestLength  -= destEnd - dest;
 
531
  return res;
 
532
}
 
533
 
 
534
NS_IMETHODIMP nsEncoderSupport::Finish(char * aDest, PRInt32 * aDestLength)
 
535
{
 
536
  // we do all operations using pointers internally
 
537
  char * dest = aDest;
 
538
  char * destEnd = aDest + *aDestLength;
 
539
 
 
540
  PRInt32 bcw; // byte count for write;
 
541
  nsresult res;
 
542
 
 
543
  res = FlushBuffer(&dest, destEnd);
 
544
  if (res == NS_OK_UENC_MOREOUTPUT) goto final;
 
545
 
 
546
  // do the finish into the internal buffer.
 
547
  for (;;) {
 
548
    bcw = mBufferCapacity;
 
549
    res = FinishNoBuff(mBuffer, &bcw);
 
550
 
 
551
    if (res == NS_OK_UENC_MOREOUTPUT) {
 
552
      delete [] mBuffer;
 
553
      mBufferCapacity *= 2;
 
554
      mBuffer = new char [mBufferCapacity];
 
555
    } else {
 
556
      mBufferStart = mBufferEnd = mBuffer;
 
557
      mBufferEnd += bcw;
 
558
      break;
 
559
    }
 
560
  }
 
561
 
 
562
  res = FlushBuffer(&dest, destEnd);
 
563
 
 
564
final:
 
565
  *aDestLength  -= destEnd - dest;
 
566
  return res;
 
567
}
 
568
 
 
569
NS_IMETHODIMP nsEncoderSupport::Reset()
 
570
{
 
571
  mBufferStart = mBufferEnd = mBuffer;
 
572
  return NS_OK;
 
573
}
 
574
 
 
575
NS_IMETHODIMP nsEncoderSupport::SetOutputErrorBehavior(
 
576
                                PRInt32 aBehavior, 
 
577
                                nsIUnicharEncoder * aEncoder, 
 
578
                                PRUnichar aChar)
 
579
{
 
580
  if ((aBehavior == kOnError_CallBack) && (aEncoder == NULL)) 
 
581
    return NS_ERROR_NULL_POINTER;
 
582
 
 
583
  NS_IF_RELEASE(aEncoder);
 
584
  mErrEncoder = aEncoder;
 
585
  NS_IF_ADDREF(aEncoder);
 
586
 
 
587
  mErrBehavior = aBehavior;
 
588
  mErrChar = aChar;
 
589
  return NS_OK;
 
590
}
 
591
 
 
592
NS_IMETHODIMP
 
593
nsEncoderSupport::GetMaxLength(const PRUnichar * aSrc, 
 
594
                               PRInt32 aSrcLength, 
 
595
                               PRInt32 * aDestLength)
 
596
{
 
597
  *aDestLength = aSrcLength * mMaxLengthFactor;
 
598
  return NS_OK;
 
599
}
 
600
 
 
601
 
 
602
//----------------------------------------------------------------------
 
603
// Class nsTableEncoderSupport [implementation]
 
604
 
 
605
nsTableEncoderSupport::nsTableEncoderSupport(uShiftTable * aShiftTable, 
 
606
                                             uMappingTable  * aMappingTable,
 
607
                                             PRUint32 aMaxLengthFactor) 
 
608
: nsEncoderSupport(aMaxLengthFactor)
 
609
{
 
610
  mHelper = NULL;
 
611
  mShiftTable = aShiftTable;
 
612
  mMappingTable = aMappingTable;
 
613
}
 
614
 
 
615
nsTableEncoderSupport::~nsTableEncoderSupport() 
 
616
{
 
617
  NS_IF_RELEASE(mHelper);
 
618
}
 
619
 
 
620
NS_IMETHODIMP nsTableEncoderSupport::FillInfo(PRUint32 *aInfo) 
 
621
{
 
622
  nsresult res;
 
623
 
 
624
  if (mHelper == nsnull) {
 
625
    res = nsComponentManager::CreateInstance(kUnicodeEncodeHelperCID, NULL, 
 
626
        NS_GET_IID(nsIUnicodeEncodeHelper), (void**) & mHelper);
 
627
    
 
628
    if (NS_FAILED(res)) return NS_ERROR_UENC_NOHELPER;
 
629
  }
 
630
 
 
631
  res = mHelper->FillInfo(aInfo, mMappingTable);
 
632
  return res;
 
633
}
 
634
//----------------------------------------------------------------------
 
635
// Subclassing of nsEncoderSupport class [implementation]
 
636
 
 
637
NS_IMETHODIMP nsTableEncoderSupport::ConvertNoBuffNoErr(
 
638
                                     const PRUnichar * aSrc, 
 
639
                                     PRInt32 * aSrcLength, 
 
640
                                     char * aDest, 
 
641
                                     PRInt32 * aDestLength)
 
642
{
 
643
  nsresult res;
 
644
 
 
645
  if (mHelper == nsnull) {
 
646
    res = nsComponentManager::CreateInstance(kUnicodeEncodeHelperCID, NULL, 
 
647
        NS_GET_IID(nsIUnicodeEncodeHelper), (void**) & mHelper);
 
648
    
 
649
    if (NS_FAILED(res)) return NS_ERROR_UENC_NOHELPER;
 
650
  }
 
651
 
 
652
  res = mHelper->ConvertByTable(aSrc, aSrcLength, aDest, aDestLength, 
 
653
      mShiftTable, mMappingTable);
 
654
  return res;
 
655
}
 
656
 
 
657
//----------------------------------------------------------------------
 
658
// Class nsMultiTableEncoderSupport [implementation]
 
659
 
 
660
nsMultiTableEncoderSupport::nsMultiTableEncoderSupport(
 
661
                            PRInt32 aTableCount,
 
662
                            uShiftTable ** aShiftTable, 
 
663
                            uMappingTable  ** aMappingTable,
 
664
                            PRUint32 aMaxLengthFactor) 
 
665
: nsEncoderSupport(aMaxLengthFactor)
 
666
{
 
667
  mHelper = NULL;
 
668
  mTableCount = aTableCount;
 
669
  mShiftTable = aShiftTable;
 
670
  mMappingTable = aMappingTable;
 
671
}
 
672
 
 
673
nsMultiTableEncoderSupport::~nsMultiTableEncoderSupport() 
 
674
{
 
675
  NS_IF_RELEASE(mHelper);
 
676
}
 
677
 
 
678
NS_IMETHODIMP nsMultiTableEncoderSupport::FillInfo(PRUint32 *aInfo) 
 
679
{
 
680
  nsresult res;
 
681
 
 
682
  if (mHelper == nsnull) {
 
683
    res = nsComponentManager::CreateInstance(kUnicodeEncodeHelperCID, NULL, 
 
684
        NS_GET_IID(nsIUnicodeEncodeHelper), (void**) & mHelper);
 
685
    
 
686
    if (NS_FAILED(res)) return NS_ERROR_UENC_NOHELPER;
 
687
  }
 
688
 
 
689
  res = mHelper->FillInfo(aInfo,mTableCount, mMappingTable);
 
690
  return res;
 
691
}
 
692
//----------------------------------------------------------------------
 
693
// Subclassing of nsEncoderSupport class [implementation]
 
694
 
 
695
NS_IMETHODIMP nsMultiTableEncoderSupport::ConvertNoBuffNoErr(
 
696
                                          const PRUnichar * aSrc, 
 
697
                                          PRInt32 * aSrcLength, 
 
698
                                          char * aDest, 
 
699
                                          PRInt32 * aDestLength)
 
700
{
 
701
  nsresult res;
 
702
 
 
703
  if (mHelper == nsnull) {
 
704
    res = nsComponentManager::CreateInstance(kUnicodeEncodeHelperCID, NULL, 
 
705
        NS_GET_IID(nsIUnicodeEncodeHelper), (void**) & mHelper);
 
706
    
 
707
    if (NS_FAILED(res)) return NS_ERROR_UENC_NOHELPER;
 
708
  }
 
709
 
 
710
  res = mHelper->ConvertByMultiTable(aSrc, aSrcLength, aDest, aDestLength, 
 
711
      mTableCount, mShiftTable, mMappingTable);
 
712
  return res;
 
713
}