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: 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 "nsUnicodeToGB2312V2.h" |
michael@0 | 7 | #include "gbku.h" |
michael@0 | 8 | |
michael@0 | 9 | //---------------------------------------------------------------------- |
michael@0 | 10 | // Class nsUnicodeToGB2312V2 [implementation] |
michael@0 | 11 | nsUnicodeToGB2312V2::nsUnicodeToGB2312V2() : |
michael@0 | 12 | nsEncoderSupport(2) |
michael@0 | 13 | { |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | NS_IMETHODIMP nsUnicodeToGB2312V2::ConvertNoBuff(const char16_t * aSrc, |
michael@0 | 17 | int32_t * aSrcLength, |
michael@0 | 18 | char * aDest, |
michael@0 | 19 | int32_t * aDestLength) |
michael@0 | 20 | { |
michael@0 | 21 | int32_t iSrcLength = 0; |
michael@0 | 22 | int32_t iDestLength = 0; |
michael@0 | 23 | nsresult res = NS_OK; |
michael@0 | 24 | |
michael@0 | 25 | while (iSrcLength < *aSrcLength) |
michael@0 | 26 | { |
michael@0 | 27 | //if unicode's hi byte has something, it is not ASCII, must be a GB |
michael@0 | 28 | if(IS_ASCII(*aSrc)) |
michael@0 | 29 | { |
michael@0 | 30 | // this is an ASCII |
michael@0 | 31 | *aDest = CAST_UNICHAR_TO_CHAR(*aSrc); |
michael@0 | 32 | aDest++; // increment 1 byte |
michael@0 | 33 | iDestLength +=1; |
michael@0 | 34 | } else { |
michael@0 | 35 | char byte1, byte2; |
michael@0 | 36 | if(mUtil.UnicodeToGBKChar(*aSrc, false, &byte1, &byte2)) |
michael@0 | 37 | { |
michael@0 | 38 | if(iDestLength+2 > *aDestLength) |
michael@0 | 39 | { |
michael@0 | 40 | res = NS_OK_UENC_MOREOUTPUT; |
michael@0 | 41 | break; |
michael@0 | 42 | } |
michael@0 | 43 | aDest[0]=byte1; |
michael@0 | 44 | aDest[1]=byte2; |
michael@0 | 45 | aDest += 2; // increment 2 bytes |
michael@0 | 46 | iDestLength +=2; // each GB char count as two in char* string |
michael@0 | 47 | } else { |
michael@0 | 48 | // cannot convert |
michael@0 | 49 | res= NS_ERROR_UENC_NOMAPPING; |
michael@0 | 50 | iSrcLength++; // include length of the unmapped character |
michael@0 | 51 | break; |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | iSrcLength++ ; // each unicode char just count as one in char16_t* string |
michael@0 | 55 | aSrc++; |
michael@0 | 56 | if ( iDestLength >= (*aDestLength) && (iSrcLength < *aSrcLength )) |
michael@0 | 57 | { |
michael@0 | 58 | res = NS_OK_UENC_MOREOUTPUT; |
michael@0 | 59 | break; |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | *aDestLength = iDestLength; |
michael@0 | 63 | *aSrcLength = iSrcLength; |
michael@0 | 64 | return res; |
michael@0 | 65 | } |