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

« back to all changes in this revision

Viewing changes to mozilla/intl/uconv/ucvcn/nsHZToUnicode.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: 4; 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.org 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
 *
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the NPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
/**
 
39
 * A character set converter from HZ to Unicode.
 
40
 * 
 
41
 *
 
42
 * @created         08/Sept/1999
 
43
 * @author  Yueheng Xu, Yueheng.Xu@intel.com
 
44
 *
 
45
 * Note: in this HZ-GB-2312 converter, we accept a string composed of 7-bit HZ 
 
46
 *       encoded Chinese chars,as it is defined in RFC1843 available at 
 
47
 *       http://www.cis.ohio-state.edu/htbin/rfc/rfc1843.html
 
48
 *       and RFC1842 available at http://www.cis.ohio-state.edu/htbin/rfc/rfc1842.html.
 
49
 *        
 
50
 *       In an effort to match the similar extended capability of Microsoft Internet Explorer
 
51
 *       5.0. We also accept the 8-bit GB encoded chars mixed in a HZ string.
 
52
 *       But this should not be a recommendedd practice for HTML authors.
 
53
 *
 
54
 *       The priority of converting are as follows: first convert 8-bit GB code; then,
 
55
 *       consume HZ ESC sequences such as '~{', '~}', '~~'; then, depending on the current
 
56
 *       state ( default to ASCII state ) of the string, each 7-bit char is converted as an 
 
57
 *       ASCII, or two 7-bit chars are converted into a Chinese character.
 
58
 */
 
59
 
 
60
 
 
61
 
 
62
#include "nsUCvCnDll.h"
 
63
#include "nsHZToUnicode.h"
 
64
#include "gbku.h"
 
65
 
 
66
//----------------------------------------------------------------------
 
67
// Class nsHZToUnicode [implementation]
 
68
 
 
69
//----------------------------------------------------------------------
 
70
// Subclassing of nsTablesDecoderSupport class [implementation]
 
71
 
 
72
#define HZ_STATE_GB             1
 
73
#define HZ_STATE_ASCII  2
 
74
#define HZ_STATE_TILD   3
 
75
#define HZLEAD1 '~'
 
76
#define HZLEAD2 '{'
 
77
#define HZLEAD3 '}'
 
78
#define HZLEAD4 '\n'
 
79
 
 
80
nsHZToUnicode::nsHZToUnicode() : nsBufferDecoderSupport(1)
 
81
{
 
82
  mHZState = HZ_STATE_ASCII;    // per HZ spec, default to ASCII state 
 
83
}
 
84
//Overwriting the ConvertNoBuff() in nsUCvCnSupport.cpp.
 
85
NS_IMETHODIMP nsHZToUnicode::ConvertNoBuff(
 
86
  const char* aSrc, 
 
87
  PRInt32 * aSrcLength, 
 
88
  PRUnichar *aDest, 
 
89
  PRInt32 * aDestLength)
 
90
{
 
91
  PRInt32 i=0;
 
92
  PRInt32 iSrcLength = *aSrcLength;
 
93
  PRInt32 iDestlen = 0;
 
94
  PRUint8 ch1, ch2;
 
95
  nsresult res = NS_OK;
 
96
  *aSrcLength=0;
 
97
  for (i=0;i<iSrcLength;i++)
 
98
  {
 
99
    if ( iDestlen >= (*aDestLength) )
 
100
    {
 
101
      res = NS_OK_UDEC_MOREOUTPUT;
 
102
      break;
 
103
    }
 
104
    if ( *aSrc & 0x80 ) // if it is a 8-bit byte
 
105
    {
 
106
      // The source is a 8-bit GBCode
 
107
      *aDest = mUtil.GBKCharToUnicode(aSrc[0], aSrc[1]);
 
108
      aSrc += 2;
 
109
      i++;
 
110
      iDestlen++;
 
111
      aDest++;
 
112
      *aSrcLength = i+1;
 
113
      continue;
 
114
    }
 
115
    // otherwise, it is a 7-bit byte 
 
116
    // The source will be an ASCII or a 7-bit HZ code depending on ch1
 
117
    ch1 = *aSrc;
 
118
    ch2 = *(aSrc+1);
 
119
    if (ch1 == HZLEAD1 )  // if it is lead by '~'
 
120
    {
 
121
      switch (ch2)
 
122
      {
 
123
        case HZLEAD2: 
 
124
          // we got a '~{'
 
125
          // we are switching to HZ state
 
126
          mHZState = HZ_STATE_GB;
 
127
          aSrc += 2;
 
128
          i++;
 
129
          break;
 
130
        case HZLEAD3: 
 
131
          // we got a '~}'
 
132
          // we are switching to ASCII state
 
133
          mHZState = HZ_STATE_ASCII;
 
134
          aSrc += 2;
 
135
          i++;
 
136
          break;
 
137
        case HZLEAD1: 
 
138
          // we got a '~~', process like an ASCII, but no state change
 
139
          aSrc++;
 
140
          *aDest = CAST_CHAR_TO_UNICHAR(*aSrc);
 
141
          aSrc++;
 
142
          i++;
 
143
          iDestlen++;
 
144
          aDest++;
 
145
          break;
 
146
        case HZLEAD4:   
 
147
          // we got a "~\n", it means maintain double byte mode cross lines, ignore the '~' itself
 
148
          //  mHZState = HZ_STATE_GB; 
 
149
          // I find that "~\n" should interpreted as line continuation without mode change
 
150
          // It should not be interpreted as line continuation with double byte mode on
 
151
          aSrc++;
 
152
          break;
 
153
        default:
 
154
          // undefined ESC sequence '~X' are ignored since this is a illegal combination 
 
155
          aSrc += 2;
 
156
          break;
 
157
      };
 
158
      continue;// go for next loop
 
159
    }
 
160
    // ch1 != '~'
 
161
    switch (mHZState)
 
162
    {
 
163
      case HZ_STATE_GB:
 
164
        // the following chars are HZ
 
165
        *aDest = mUtil.GBKCharToUnicode(aSrc[0]|0x80, aSrc[1]|0x80);
 
166
        aSrc += 2;
 
167
        i++;
 
168
        iDestlen++;
 
169
        aDest++;
 
170
        break;
 
171
      case HZ_STATE_ASCII:
 
172
      default:
 
173
        // default behavior also like an ASCII
 
174
        // when the source is an ASCII
 
175
        *aDest = CAST_CHAR_TO_UNICHAR(*aSrc);
 
176
        aSrc++;
 
177
        iDestlen++;
 
178
        aDest++;
 
179
        break;
 
180
    }
 
181
    *aSrcLength = i+1;
 
182
  }// for loop
 
183
  *aDestLength = iDestlen;
 
184
  return NS_OK;
 
185
}
 
186
 
 
187