1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsUTF7ToUnicode.h"
#define ENC_DIRECT 0
#define ENC_BASE64 1
//----------------------------------------------------------------------
// Class nsBasicUTF7Decoder [implementation]
nsBasicUTF7Decoder::nsBasicUTF7Decoder(char aLastChar, char aEscChar)
: nsBufferDecoderSupport(1)
{
mLastChar = aLastChar;
mEscChar = aEscChar;
Reset();
}
nsresult nsBasicUTF7Decoder::DecodeDirect(
const char * aSrc,
int32_t * aSrcLength,
char16_t * aDest,
int32_t * aDestLength)
{
const char * srcEnd = aSrc + *aSrcLength;
const char * src = aSrc;
char16_t * destEnd = aDest + *aDestLength;
char16_t * dest = aDest;
nsresult res = NS_OK;
char ch;
while (src < srcEnd) {
ch = *src;
// stop when we meet other chars or end of direct encoded seq.
// if (!(DirectEncodable(ch)) || (ch == mEscChar)) {
// but we are decoding; so we should be lax; pass everything until escchar
if (ch == mEscChar) {
res = NS_ERROR_UDEC_ILLEGALINPUT;
break;
}
if (dest >= destEnd) {
res = NS_OK_UDEC_MOREOUTPUT;
break;
} else {
*dest++ = ch;
src++;
}
}
*aSrcLength = src - aSrc;
*aDestLength = dest - aDest;
return res;
}
nsresult nsBasicUTF7Decoder::DecodeBase64(
const char * aSrc,
int32_t * aSrcLength,
char16_t * aDest,
int32_t * aDestLength)
{
const char * srcEnd = aSrc + *aSrcLength;
const char * src = aSrc;
char16_t * destEnd = aDest + *aDestLength;
char16_t * dest = aDest;
nsresult res = NS_OK;
char ch;
uint32_t value;
while (src < srcEnd) {
ch = *src;
// stop when we meet other chars or end of direct encoded seq.
value = CharToValue(ch);
if (value > 0xff) {
res = NS_ERROR_UDEC_ILLEGALINPUT;
break;
}
switch (mEncStep) {
case 0:
mEncBits = value << 10;
break;
case 1:
mEncBits += value << 4;
break;
case 2:
if (dest >= destEnd) {
res = NS_OK_UDEC_MOREOUTPUT;
break;
}
mEncBits += value >> 2;
*(dest++) = (char16_t) mEncBits;
mEncBits = (value & 0x03) << 14;
break;
case 3:
mEncBits += value << 8;
break;
case 4:
mEncBits += value << 2;
break;
case 5:
if (dest >= destEnd) {
res = NS_OK_UDEC_MOREOUTPUT;
break;
}
mEncBits += value >> 4;
*(dest++) = (char16_t) mEncBits;
mEncBits = (value & 0x0f) << 12;
break;
case 6:
mEncBits += value << 6;
break;
case 7:
if (dest >= destEnd) {
res = NS_OK_UDEC_MOREOUTPUT;
break;
}
mEncBits += value;
*(dest++) = (char16_t) mEncBits;
mEncBits = 0;
break;
}
if (res != NS_OK) break;
src++;
(++mEncStep)%=8;
}
*aSrcLength = src - aSrc;
*aDestLength = dest - aDest;
return res;
}
uint32_t nsBasicUTF7Decoder::CharToValue(char aChar) {
if ((aChar>='A')&&(aChar<='Z'))
return (uint8_t)(aChar-'A');
else if ((aChar>='a')&&(aChar<='z'))
return (uint8_t)(26+aChar-'a');
else if ((aChar>='0')&&(aChar<='9'))
return (uint8_t)(26+26+aChar-'0');
else if (aChar=='+')
return (uint8_t)(26+26+10);
else if (aChar==mLastChar)
return (uint8_t)(26+26+10+1);
else
return 0xffff;
}
//----------------------------------------------------------------------
// Subclassing of nsBufferDecoderSupport class [implementation]
NS_IMETHODIMP nsBasicUTF7Decoder::ConvertNoBuff(const char * aSrc,
int32_t * aSrcLength,
char16_t * aDest,
int32_t * aDestLength)
{
const char * srcEnd = aSrc + *aSrcLength;
const char * src = aSrc;
char16_t * destEnd = aDest + *aDestLength;
char16_t * dest = aDest;
int32_t bcr,bcw;
nsresult res = NS_OK;
while (src < srcEnd) {
// fist, attept to decode in the current mode
bcr = srcEnd - src;
bcw = destEnd - dest;
if (mEncoding == ENC_DIRECT)
res = DecodeDirect(src, &bcr, dest, &bcw);
else if ((mFreshBase64) && (*src == '-')) {
*dest = mEscChar;
bcr = 0;
bcw = 1;
res = NS_ERROR_UDEC_ILLEGALINPUT;
} else {
mFreshBase64 = false;
res = DecodeBase64(src, &bcr, dest, &bcw);
}
src += bcr;
dest += bcw;
// if an illegal char was encountered, test if it is an escape seq.
if (res == NS_ERROR_UDEC_ILLEGALINPUT) {
if (mEncoding == ENC_DIRECT) {
if (*src == mEscChar) {
mEncoding = ENC_BASE64;
mFreshBase64 = true;
mEncBits = 0;
mEncStep = 0;
src++;
res = NS_OK;
} else break;
} else {
mEncoding = ENC_DIRECT;
res = NS_OK;
// absorbe end of escape sequence
if (*src == '-') src++;
}
} else if (res != NS_OK) break;
}
*aSrcLength = src - aSrc;
*aDestLength = dest - aDest;
return res;
}
NS_IMETHODIMP nsBasicUTF7Decoder::Reset()
{
mEncoding = ENC_DIRECT;
mEncBits = 0;
mEncStep = 0;
return nsBufferDecoderSupport::Reset();
}
//----------------------------------------------------------------------
// Class nsUTF7ToUnicode [implementation]
nsUTF7ToUnicode::nsUTF7ToUnicode()
: nsBasicUTF7Decoder('/', '+')
{
}
|