Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C; tab-width: 4; 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 | * A character set converter from Unicode to HZ. |
michael@0 | 7 | * |
michael@0 | 8 | * |
michael@0 | 9 | * @created 08/Sept/1999 |
michael@0 | 10 | * @author Yueheng Xu, Yueheng.Xu@intel.com |
michael@0 | 11 | * Revision History |
michael@0 | 12 | * 04/Oct/1999. Yueheng Xu: Fixed line continuation problem when line |
michael@0 | 13 | * ended by '~'; |
michael@0 | 14 | * Used table UnicodeToGBK[] to speed up the mapping. |
michael@0 | 15 | */ |
michael@0 | 16 | #include "nsUnicodeToHZ.h" |
michael@0 | 17 | #include "gbku.h" |
michael@0 | 18 | //---------------------------------------------------------------------- |
michael@0 | 19 | // Class nsUnicodeToGBK [implementation] |
michael@0 | 20 | #define HZ_STATE_GB 1 |
michael@0 | 21 | #define HZ_STATE_ASCII 2 |
michael@0 | 22 | #define HZ_STATE_TILD 3 |
michael@0 | 23 | #define HZLEAD1 '~' |
michael@0 | 24 | #define HZLEAD2 '{' |
michael@0 | 25 | #define HZLEAD3 '}' |
michael@0 | 26 | #define UNICODE_TILD 0x007E |
michael@0 | 27 | nsUnicodeToHZ::nsUnicodeToHZ() : nsEncoderSupport(6) |
michael@0 | 28 | { |
michael@0 | 29 | mHZState = HZ_STATE_ASCII; // per HZ spec, default to HZ mode |
michael@0 | 30 | } |
michael@0 | 31 | NS_IMETHODIMP nsUnicodeToHZ::ConvertNoBuff( |
michael@0 | 32 | const char16_t * aSrc, |
michael@0 | 33 | int32_t * aSrcLength, |
michael@0 | 34 | char * aDest, |
michael@0 | 35 | int32_t * aDestLength) |
michael@0 | 36 | { |
michael@0 | 37 | int32_t i=0; |
michael@0 | 38 | int32_t iSrcLength = *aSrcLength; |
michael@0 | 39 | int32_t iDestLength = 0; |
michael@0 | 40 | |
michael@0 | 41 | for (i=0;i< iSrcLength;i++) |
michael@0 | 42 | { |
michael@0 | 43 | if(! IS_ASCII(*aSrc)) |
michael@0 | 44 | { |
michael@0 | 45 | // hi byte has something, it is not ASCII, process as a GB |
michael@0 | 46 | if ( mHZState != HZ_STATE_GB ) |
michael@0 | 47 | { |
michael@0 | 48 | // we are adding a '~{' ESC sequence to star a HZ string |
michael@0 | 49 | mHZState = HZ_STATE_GB; |
michael@0 | 50 | aDest[0] = '~'; |
michael@0 | 51 | aDest[1] = '{'; |
michael@0 | 52 | aDest += 2; // increment 2 bytes |
michael@0 | 53 | iDestLength +=2; |
michael@0 | 54 | } |
michael@0 | 55 | if(mUtil.UnicodeToGBKChar(*aSrc, true, &aDest[0], &aDest[1])) { |
michael@0 | 56 | aDest += 2; // increment 2 bytes |
michael@0 | 57 | iDestLength +=2; |
michael@0 | 58 | } else { |
michael@0 | 59 | // some thing that we cannot convert |
michael@0 | 60 | // xxx fix me ftang |
michael@0 | 61 | // error handling here |
michael@0 | 62 | } |
michael@0 | 63 | } else { |
michael@0 | 64 | // this is an ASCII |
michael@0 | 65 | |
michael@0 | 66 | // if we are in HZ mode, end it by adding a '~}' ESC sequence |
michael@0 | 67 | if ( mHZState == HZ_STATE_GB ) |
michael@0 | 68 | { |
michael@0 | 69 | mHZState = HZ_STATE_ASCII; |
michael@0 | 70 | aDest[0] = '~'; |
michael@0 | 71 | aDest[1] = '}'; |
michael@0 | 72 | aDest += 2; // increment 2 bytes |
michael@0 | 73 | iDestLength +=2; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // if this is a regular char '~' , convert it to two '~' |
michael@0 | 77 | if ( *aSrc == UNICODE_TILD ) |
michael@0 | 78 | { |
michael@0 | 79 | aDest[0] = '~'; |
michael@0 | 80 | aDest[1] = '~'; |
michael@0 | 81 | aDest += 2; // increment 2 bytes |
michael@0 | 82 | iDestLength +=2; |
michael@0 | 83 | } else { |
michael@0 | 84 | // other regular ASCII chars convert by normal ways |
michael@0 | 85 | |
michael@0 | 86 | // Is this works for both little endian and big endian machines ? |
michael@0 | 87 | *aDest = (char) ( (char16_t)(*aSrc) ); |
michael@0 | 88 | aDest++; // increment 1 byte |
michael@0 | 89 | iDestLength +=1; |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | aSrc++; // increment 2 bytes |
michael@0 | 93 | if ( iDestLength >= (*aDestLength) ) |
michael@0 | 94 | { |
michael@0 | 95 | break; |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | *aDestLength = iDestLength; |
michael@0 | 99 | *aSrcLength = i; |
michael@0 | 100 | return NS_OK; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | NS_IMETHODIMP nsUnicodeToHZ::FinishNoBuff(char * aDest, int32_t * aDestLength) |
michael@0 | 104 | { |
michael@0 | 105 | if ( mHZState == HZ_STATE_GB ) |
michael@0 | 106 | { |
michael@0 | 107 | // if we are in HZ mode, end it by adding a '~}' ESC sequence |
michael@0 | 108 | mHZState = HZ_STATE_ASCII; |
michael@0 | 109 | aDest[0] = '~'; |
michael@0 | 110 | aDest[1] = '}'; |
michael@0 | 111 | *aDestLength = 2; |
michael@0 | 112 | } else { |
michael@0 | 113 | *aDestLength = 0; |
michael@0 | 114 | } |
michael@0 | 115 | return NS_OK; |
michael@0 | 116 | } |