/
utf7.c
508 lines (466 loc) · 14 KB
/
utf7.c
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*++
Module Name:
utf7.c
Abstract:
This file contains functions that convert UTF-7 strings to UTF-16
strings and UTF-16 string to UTF-7 strings.
--*/
#include "utf.h"
#define SHIFT_IN '+' // beginning of a shift sequence
#define SHIFT_OUT '-' // end of a shift sequence
#define ASCII 0x7f
/////////////////////////
// //
// Unicode -> UTF-7 //
// //
/////////////////////////
//
// Convert one Unicode to 2 2/3 Base64 chars in a shifted sequence.
// Each char represents a 6-bit portion of the 16-bit Unicode char.
//
CONST char cBase64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" // A : 000000 .... 011001 ( 0 - 25)
"abcdefghijklmnopqrstuvwxyz" // a : 011010 .... 110011 (26 - 51)
"0123456789" // 0 : 110100 .... 111101 (52 - 61)
"+/"; // + : 111110, / : 111111 (62 - 63)
//
// To determine if an ASCII char needs to be shifted.
// 1 : to be shifted
// 0 : not to be shifted
//
CONST BOOLEAN fShiftChar[] = {
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // Null, Tab, LF, CR
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, // Space '() +,-./
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, // 0123456789: ?
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ABCDEFGHIJKLMNO
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, // PQRSTUVWXYZ
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // abcdefghijklmno
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 // pqrstuvwxyz
};
/////////////////////////
// //
// UTF-7 -> Unicode //
// //
/////////////////////////
//
// Convert a Base64 char in a shifted sequence to a 6-bit portion of a
// Unicode char.
// -1 means it is not a Base64
//
CONST char nBitBase64[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // + /
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 0123456789
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // ABCDEFGHIJKLMNO
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // PQRSTUVWXYZ
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // abcdefghijklmno
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 // pqrstuvwxyz
};
//-------------------------------------------------------------------------//
// INTERNAL ROUTINES //
//-------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////
//
// UTF7ToUnicode
//
// Maps a UTF-7 character string to its wide character string counterpart.
//
////////////////////////////////////////////////////////////////////////////
int API UTF7ToUTF16(
LPCSTR lpSrcStr,
int cchSrc,
LPWSTR lpDestStr,
int cchDest)
{
const unsigned char *pUTF7 = (const unsigned char *)lpSrcStr;
BOOL fShift = FALSE;
DWORD dwBit = 0; // 32-bit buffer to hold temporary bits
int iPos = 0; // 6-bit position pointer in the buffer
int cchWC = 0; // # of Unicode code points generated
if ( (cchSrc == 0) || (cchDest < 0) || (lpSrcStr == NULL) ||
((cchDest != 0) && ((lpDestStr == NULL) || (lpSrcStr == (LPSTR)lpDestStr))) )
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (cchSrc <= -1)
{
cchSrc = strlen(lpSrcStr) + 1;
}
while ((cchSrc--) && ((cchDest == 0) || (cchWC < cchDest)))
{
if (*pUTF7 > ASCII)
{
//
// Error - non ASCII char, so zero extend it.
//
if (cchDest)
{
lpDestStr[cchWC] = (WCHAR)*pUTF7;
}
cchWC++;
}
else if (!fShift)
{
//
// Not in shifted sequence.
//
if (*pUTF7 == SHIFT_IN)
{
if (cchSrc && (pUTF7[1] == SHIFT_OUT))
{
//
// "+-" means "+"
//
if (cchDest)
{
lpDestStr[cchWC] = (WCHAR)*pUTF7;
}
pUTF7++;
cchSrc--;
cchWC++;
}
else
{
//
// Start a new shift sequence.
//
fShift = TRUE;
}
}
else
{
//
// No need to shift.
//
if (cchDest)
{
lpDestStr[cchWC] = (WCHAR)*pUTF7;
}
cchWC++;
}
}
else
{
//
// Already in shifted sequence.
//
if (nBitBase64[*pUTF7] == -1)
{
//
// Any non Base64 char also ends shift state.
//
if (*pUTF7 != SHIFT_OUT)
{
//
// Not "-", so write it to the buffer.
//
if (cchDest)
{
lpDestStr[cchWC] = (WCHAR)*pUTF7;
}
cchWC++;
}
//
// Reset bits.
//
fShift = FALSE;
dwBit = 0;
iPos = 0;
}
else
{
//
// Store the bits in the 6-bit buffer and adjust the
// position pointer.
//
dwBit |= ((DWORD)nBitBase64[*pUTF7]) << (26 - iPos);
iPos += 6;
}
//
// Output the 16-bit Unicode value.
//
while (iPos >= 16)
{
if (cchDest)
{
if (cchWC < cchDest)
{
lpDestStr[cchWC] = (WCHAR)(dwBit >> 16);
}
else
{
break;
}
}
cchWC++;
dwBit <<= 16;
iPos -= 16;
}
if (iPos >= 16)
{
//
// Error - buffer too small.
//
cchSrc++;
break;
}
}
pUTF7++;
}
//
// Make sure the destination buffer was large enough.
//
if (cchDest && (cchSrc >= 0))
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return (0);
}
//
// Return the number of Unicode characters written.
//
return (cchWC);
}
////////////////////////////////////////////////////////////////////////////
//
// UnicodeToUTF7
//
// Maps a Unicode character string to its UTF-7 string counterpart.
//
////////////////////////////////////////////////////////////////////////////
int API UTF16ToUTF7(
LPCWSTR lpSrcStr,
int cchSrc,
LPSTR lpDestStr,
int cchDest)
{
LPCWSTR lpWC = lpSrcStr;
BOOL fShift = FALSE;
DWORD dwBit = 0; // 32-bit buffer
int iPos = 0; // 6-bit position in buffer
int cchU7 = 0; // # of UTF7 chars generated
if ( (cchSrc == 0) || (cchDest < 0) || (lpSrcStr == NULL) ||
((cchDest != 0) && ((lpDestStr == NULL) || (lpSrcStr == (LPWSTR)lpDestStr))) )
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (cchSrc <= -1)
{
cchSrc = wcslen(lpSrcStr) + 1;
}
while ((cchSrc--) && ((cchDest == 0) || (cchU7 < cchDest)))
{
if ((*lpWC > ASCII) || (fShiftChar[*lpWC]))
{
//
// Need shift. Store 16 bits in buffer.
//
dwBit |= ((DWORD)*lpWC) << (16 - iPos);
iPos += 16;
if (!fShift)
{
//
// Not in shift state, so add "+".
//
if (cchDest)
{
lpDestStr[cchU7] = SHIFT_IN;
}
cchU7++;
//
// Go into shift state.
//
fShift = TRUE;
}
//
// Output 6 bits at a time as Base64 chars.
//
while (iPos >= 6)
{
if (cchDest)
{
if (cchU7 < cchDest)
{
//
// 26 = 32 - 6
//
lpDestStr[cchU7] = cBase64[(int)(dwBit >> 26)];
}
else
{
break;
}
}
cchU7++;
dwBit <<= 6; // remove from bit buffer
iPos -= 6; // adjust position pointer
}
if (iPos >= 6)
{
//
// Error - buffer too small.
//
cchSrc++;
break;
}
}
else
{
//
// No need to shift.
//
if (fShift)
{
//
// End the shift sequence.
//
fShift = FALSE;
if (iPos != 0)
{
//
// Some bits left in dwBit.
//
if (cchDest)
{
if ((cchU7 + 1) < cchDest)
{
lpDestStr[cchU7++] = cBase64[(int)(dwBit >> 26)];
lpDestStr[cchU7++] = SHIFT_OUT;
}
else
{
//
// Error - buffer too small.
//
cchSrc++;
break;
}
}
else
{
cchU7 += 2;
}
dwBit = 0; // reset bit buffer
iPos = 0; // reset postion pointer
}
else
{
//
// Simply end the shift sequence.
//
if (cchDest)
{
lpDestStr[cchU7++] = SHIFT_OUT;
}
else
{
cchU7++;
}
}
}
//
// Write the character to the buffer.
// If the character is "+", then write "+-".
//
if (cchDest)
{
if (cchU7 < cchDest)
{
lpDestStr[cchU7++] = (char)*lpWC;
if (*lpWC == SHIFT_IN)
{
if (cchU7 < cchDest)
{
lpDestStr[cchU7++] = SHIFT_OUT;
}
else
{
//
// Error - buffer too small.
//
cchSrc++;
break;
}
}
}
else
{
//
// Error - buffer too small.
//
cchSrc++;
break;
}
}
else
{
cchU7++;
if (*lpWC == SHIFT_IN)
{
cchU7++;
}
}
}
lpWC++;
}
//
// See if we're still in the shift state.
//
if (fShift)
{
if (iPos != 0)
{
//
// Some bits left in dwBit.
//
if (cchDest)
{
if ((cchU7 + 1) < cchDest)
{
lpDestStr[cchU7++] = cBase64[(int)(dwBit >> 26)];
lpDestStr[cchU7++] = SHIFT_OUT;
}
else
{
//
// Error - buffer too small.
//
cchSrc++;
}
}
else
{
cchU7 += 2;
}
}
else
{
//
// Simply end the shift sequence.
//
if (cchDest)
{
lpDestStr[cchU7++] = SHIFT_OUT;
}
else
{
cchU7++;
}
}
}
//
// Make sure the destination buffer was large enough.
//
if (cchDest && (cchSrc >= 0))
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return (0);
}
//
// Return the number of UTF-7 characters written.
//
return (cchU7);
}