bump product version to 5.0.4.1
[LibreOffice.git] / sal / textenc / tcvtutf7.cxx
blob8d4ae60045f2eaaf91add2b8774528b208be3431
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
22 #include "rtl/textcvt.h"
24 #include "tenchelp.hxx"
25 #include "unichars.hxx"
27 /* ======================================================================= */
29 static unsigned char const aImplBase64Tab[64] =
31 /* A-Z */
32 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
33 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
34 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
35 0x58, 0x59, 0x5A,
36 /* a-z */
37 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
38 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
39 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
40 0x78, 0x79, 0x7A,
41 /* 0-9,+,/ */
42 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
43 0x38, 0x39, 0x2B, 0x2F
46 /* Index in Base64Tab or 0xFF, when is a invalid character */
47 static unsigned char const aImplBase64IndexTab[128] =
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x00-0x07 */
50 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x08-0x0F */
51 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x10-0x17 */
52 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x18-0x1F */
53 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x20-0x27 !"#$%&' */
54 0xFF, 0xFF, 0xFF, 62, 0xFF, 0xFF, 0xFF, 63, /* 0x28-0x2F ()*+,-./ */
55 52, 53, 54, 55, 56, 57, 58, 59, /* 0x30-0x37 01234567 */
56 60, 61, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x38-0x3F 89:;<=>? */
57 0xFF, 0, 1, 2, 3, 4, 5, 6, /* 0x40-0x47 @ABCDEFG */
58 7, 8, 9, 10, 11, 12, 13, 14, /* 0x48-0x4F HIJKLMNO */
59 15, 16, 17, 18, 19, 20, 21, 22, /* 0x50-0x57 PQRSTUVW */
60 23, 24, 25, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x58-0x5F XYZ[\]^_ */
61 0xFF, 26, 27, 28, 29, 30, 31, 32, /* 0x60-0x67 `abcdefg */
62 33, 34, 35, 36, 37, 38, 39, 40, /* 0x68-0x6F hijklmno */
63 41, 42, 43, 44, 45, 46, 47, 48, /* 0x70-0x77 pqrstuvw */
64 49, 50, 51, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF /* 0x78-0x7F xyz{|}~ */
67 static unsigned char const aImplMustShiftTab[128] =
69 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00-0x07 */
70 1, 0, 0, 1, 0, 1, 1, 1, /* 0x08-0x0F 0x09 == HTAB, 0x0A == LF 0x0C == CR */
71 1, 1, 1, 1, 1, 1, 1, 1, /* 0x10-0x17 */
72 1, 1, 1, 1, 1, 1, 1, 1, /* 0x18-0x1F */
73 0, 1, 1, 1, 1, 1, 1, 0, /* 0x20-0x27 !"#$%&' */
74 0, 0, 1, 1, 0, 1, 0, 0, /* 0x28-0x2F ()*+,-./ */
75 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30-0x37 01234567 */
76 0, 0, 0, 1, 1, 1, 1, 0, /* 0x38-0x3F 89:;<=>? */
77 1, 0, 0, 0, 0, 0, 0, 0, /* 0x40-0x47 @ABCDEFG */
78 0, 0, 0, 0, 0, 0, 0, 0, /* 0x48-0x4F HIJKLMNO */
79 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50-0x57 PQRSTUVW */
80 0, 0, 0, 1, 1, 1, 1, 1, /* 0x58-0x5F XYZ[\]^_ */
81 1, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 `abcdefg */
82 0, 0, 0, 0, 0, 0, 0, 0, /* 0x68-0x6F hijklmno */
83 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77 pqrstuvw */
84 0, 0, 0, 1, 1, 1, 1, 1 /* 0x78-0x7F xyz{|}~ */
87 /* + */
88 #define IMPL_SHIFT_IN_CHAR 0x2B
89 /* - */
90 #define IMPL_SHIFT_OUT_CHAR 0x2D
92 /* ----------------------------------------------------------------------- */
94 struct ImplUTF7ToUCContextData
96 bool mbShifted;
97 bool mbFirst;
98 bool mbWroteOne;
99 sal_uInt32 mnBitBuffer;
100 sal_uInt32 mnBufferBits;
103 /* ----------------------------------------------------------------------- */
105 void* ImplUTF7CreateUTF7TextToUnicodeContext()
107 ImplUTF7ToUCContextData* pContextData = new ImplUTF7ToUCContextData;
108 pContextData->mbShifted = false;
109 pContextData->mbFirst = false;
110 pContextData->mbWroteOne = false;
111 pContextData->mnBitBuffer = 0;
112 pContextData->mnBufferBits = 0;
113 return pContextData;
116 /* ----------------------------------------------------------------------- */
118 void ImplUTF7DestroyTextToUnicodeContext( void* pContext )
120 delete static_cast< ImplUTF7ToUCContextData * >(pContext);
123 /* ----------------------------------------------------------------------- */
125 void ImplUTF7ResetTextToUnicodeContext( void* pContext )
127 ImplUTF7ToUCContextData* pContextData = static_cast<ImplUTF7ToUCContextData*>(pContext);
128 pContextData->mbShifted = false;
129 pContextData->mbFirst = false;
130 pContextData->mbWroteOne = false;
131 pContextData->mnBitBuffer = 0;
132 pContextData->mnBufferBits = 0;
135 /* ----------------------------------------------------------------------- */
137 sal_Size ImplUTF7ToUnicode( SAL_UNUSED_PARAMETER const void*, void* pContext,
138 const char* pSrcBuf, sal_Size nSrcBytes,
139 sal_Unicode* pDestBuf, sal_Size nDestChars,
140 sal_uInt32 nFlags, sal_uInt32* pInfo,
141 sal_Size* pSrcCvtBytes )
143 ImplUTF7ToUCContextData* pContextData = static_cast<ImplUTF7ToUCContextData*>(pContext);
144 unsigned char c ='\0';
145 unsigned char nBase64Value = 0;
146 bool bEnd = false;
147 bool bShifted;
148 bool bFirst;
149 bool bWroteOne;
150 bool bBase64End;
151 sal_uInt32 nBitBuffer;
152 sal_uInt32 nBitBufferTemp;
153 sal_uInt32 nBufferBits;
154 sal_Unicode* pEndDestBuf;
155 const char* pEndSrcBuf;
157 /* !!! Implementation not finnished !!!
158 if ( pContextData )
160 bShifted = pContextData->mbShifted;
161 bFirst = pContextData->mbFirst;
162 bWroteOne = pContextData->mbWroteOne;
163 nBitBuffer = pContextData->mnBitBuffer;
164 nBufferBits = pContextData->mnBufferBits;
166 else
169 bShifted = false;
170 bFirst = false;
171 bWroteOne = false;
172 nBitBuffer = 0;
173 nBufferBits = 0;
176 *pInfo = 0;
177 pEndDestBuf = pDestBuf+nDestChars;
178 pEndSrcBuf = pSrcBuf+nSrcBytes;
181 if ( pSrcBuf < pEndSrcBuf )
183 c = (unsigned char)*pSrcBuf;
185 /* End, when not a base64 character */
186 bBase64End = false;
187 if ( c <= 0x7F )
189 nBase64Value = aImplBase64IndexTab[c];
190 if ( nBase64Value == 0xFF )
191 bBase64End = true;
194 else
196 bEnd = true;
197 bBase64End = true;
200 if ( bShifted )
202 if ( bBase64End )
204 bShifted = false;
206 /* If the character causing us to drop out was SHIFT_IN */
207 /* or SHIFT_OUT, it may be a special escape for SHIFT_IN. */
208 /* The test for SHIFT_IN is not necessary, but allows */
209 /* an alternate form of UTF-7 where SHIFT_IN is escaped */
210 /* by SHIFT_IN. This only works for some values of */
211 /* SHIFT_IN. It is so implemented, because this comes */
212 /* from the officel unicode book (The Unicode Standard, */
213 /* Version 2.0) and so I think, that someone of the */
214 /* world has used this feature. */
215 if ( !bEnd )
217 if ( (c == IMPL_SHIFT_IN_CHAR) || (c == IMPL_SHIFT_OUT_CHAR) )
219 /* If no base64 character, and the terminating */
220 /* character of the shift sequence was the */
221 /* SHIFT_OUT_CHAR, then it't a special escape */
222 /* for SHIFT_IN_CHAR. */
223 if ( bFirst && (c == IMPL_SHIFT_OUT_CHAR) )
225 if ( pDestBuf >= pEndDestBuf )
227 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
228 break;
230 *pDestBuf = IMPL_SHIFT_IN_CHAR;
231 pDestBuf++;
232 bWroteOne = true;
235 /* Skip character */
236 pSrcBuf++;
237 if ( pSrcBuf < pEndSrcBuf )
238 c = (unsigned char)*pSrcBuf;
239 else
240 bEnd = true;
244 /* Empty sequence not allowed, so when we don't write one */
245 /* valid char, then the sequence is corrupt */
246 if ( !bWroteOne )
248 /* When no more bytes in the source buffer, then */
249 /* this buffer may be to small */
250 if ( bEnd )
251 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOSMALL;
252 else
254 *pInfo |= RTL_TEXTTOUNICODE_INFO_INVALID;
255 if ( (nFlags & RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK) == RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR )
257 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR;
258 break;
260 /* We insert here no default char, because I think */
261 /* this is better to ignore this */
265 else
267 /* Add 6 Bits from character to the bit buffer */
268 nBufferBits += 6;
269 nBitBuffer |= ((sal_uInt32)(nBase64Value & 0x3F)) << (32-nBufferBits);
270 bFirst = false;
273 /* Extract as many full 16 bit characters as possible from the */
274 /* bit buffer. */
275 while ( (pDestBuf < pEndDestBuf) && (nBufferBits >= 16) )
277 nBitBufferTemp = nBitBuffer >> (32-16);
278 *pDestBuf = (sal_Unicode)((nBitBufferTemp) & 0xFFFF);
279 pDestBuf++;
280 nBitBuffer <<= 16;
281 nBufferBits -= 16;
282 bWroteOne = true;
285 if ( nBufferBits >= 16 )
287 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
288 break;
291 if ( bBase64End )
293 /* Sequence ended and we have some bits, then the */
294 /* sequence is corrupted */
295 if ( nBufferBits && nBitBuffer )
297 /* When no more bytes in the source buffer, then */
298 /* this buffer may be to small */
299 if ( bEnd )
300 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOSMALL;
301 else
303 *pInfo |= RTL_TEXTTOUNICODE_INFO_INVALID;
304 if ( (nFlags & RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK) == RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR )
306 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR;
307 break;
309 else if ( (nFlags & RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK) != RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE )
311 if ( pDestBuf >= pEndDestBuf )
313 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
314 break;
316 *pDestBuf++
317 = RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER;
323 nBitBuffer = 0;
324 nBufferBits = 0;
328 if ( !bEnd )
330 if ( !bShifted )
332 if ( c == IMPL_SHIFT_IN_CHAR )
334 bShifted = true;
335 bFirst = true;
336 bWroteOne = false;
338 else
340 /* No direct encoded charcater, then the buffer is */
341 /* corrupt */
342 if ( c > 0x7F )
344 *pInfo |= RTL_TEXTTOUNICODE_INFO_INVALID;
345 if ( (nFlags & RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK) == RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR )
347 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR;
348 break;
350 else if ( (nFlags & RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK) != RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE )
352 if ( pDestBuf >= pEndDestBuf )
354 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
355 break;
357 *pDestBuf++
358 = RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER;
362 /* Write char to unicode buffer */
363 if ( pDestBuf >= pEndDestBuf )
365 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
366 break;
368 *pDestBuf = c;
369 pDestBuf++;
373 pSrcBuf++;
376 while ( !bEnd );
378 if ( pContextData )
380 pContextData->mbShifted = bShifted;
381 pContextData->mbFirst = bFirst;
382 pContextData->mbWroteOne = bWroteOne;
383 pContextData->mnBitBuffer = nBitBuffer;
384 pContextData->mnBufferBits = nBufferBits;
387 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
388 return (nDestChars - (pEndDestBuf-pDestBuf));
391 /* ======================================================================= */
393 struct ImplUTF7FromUCContextData
395 bool mbShifted;
396 sal_uInt32 mnBitBuffer;
397 sal_uInt32 mnBufferBits;
400 /* ----------------------------------------------------------------------- */
402 void* ImplUTF7CreateUnicodeToTextContext()
404 ImplUTF7FromUCContextData* pContextData = new ImplUTF7FromUCContextData;
405 pContextData->mbShifted = false;
406 pContextData->mnBitBuffer = 0;
407 pContextData->mnBufferBits = 0;
408 return pContextData;
411 /* ----------------------------------------------------------------------- */
413 void ImplUTF7DestroyUnicodeToTextContext( void* pContext )
415 delete static_cast< ImplUTF7FromUCContextData * >(pContext);
418 /* ----------------------------------------------------------------------- */
420 void ImplUTF7ResetUnicodeToTextContext( void* pContext )
422 ImplUTF7FromUCContextData* pContextData = static_cast<ImplUTF7FromUCContextData*>(pContext);
423 pContextData->mbShifted = false;
424 pContextData->mnBitBuffer = 0;
425 pContextData->mnBufferBits = 0;
428 /* ----------------------------------------------------------------------- */
430 sal_Size ImplUnicodeToUTF7( SAL_UNUSED_PARAMETER const void*, void* pContext,
431 const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
432 char* pDestBuf, sal_Size nDestBytes,
433 SAL_UNUSED_PARAMETER sal_uInt32, sal_uInt32* pInfo,
434 sal_Size* pSrcCvtChars )
436 ImplUTF7FromUCContextData* pContextData = static_cast<ImplUTF7FromUCContextData*>(pContext);
437 sal_Unicode c = '\0';
438 bool bEnd = false;
439 bool bShifted;
440 bool bNeedShift;
441 sal_uInt32 nBitBuffer;
442 sal_uInt32 nBitBufferTemp;
443 sal_uInt32 nBufferBits;
444 char* pEndDestBuf;
445 const sal_Unicode* pEndSrcBuf;
447 /* !!! Implementation not finnished !!!
448 if ( pContextData )
450 bShifted = pContextData->mbShifted;
451 nBitBuffer = pContextData->mnBitBuffer;
452 nBufferBits = pContextData->mnBufferBits;
454 else
457 bShifted = false;
458 nBitBuffer = 0;
459 nBufferBits = 0;
462 *pInfo = 0;
463 pEndDestBuf = pDestBuf+nDestBytes;
464 pEndSrcBuf = pSrcBuf+nSrcChars;
467 if ( pSrcBuf < pEndSrcBuf )
469 c = *pSrcBuf;
471 bNeedShift = (c > 0x7F) || aImplMustShiftTab[c];
472 if ( bNeedShift && !bShifted )
474 if ( pDestBuf >= pEndDestBuf )
476 *pInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
477 break;
479 *pDestBuf = IMPL_SHIFT_IN_CHAR;
480 pDestBuf++;
481 /* Special case handling for SHIFT_IN_CHAR */
482 if ( c == IMPL_SHIFT_IN_CHAR )
484 if ( pDestBuf >= pEndDestBuf )
486 *pInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
487 break;
489 *pDestBuf = IMPL_SHIFT_OUT_CHAR;
490 pDestBuf++;
492 else
493 bShifted = true;
496 else
498 bEnd = true;
499 bNeedShift = false;
502 if ( bShifted )
504 /* Write the character to the bit buffer, or pad the bit */
505 /* buffer out to a full base64 character */
506 if ( bNeedShift )
508 nBufferBits += 16;
509 nBitBuffer |= ((sal_uInt32)c) << (32-nBufferBits);
511 else
512 nBufferBits += (6-(nBufferBits%6))%6;
514 /* Flush out as many full base64 characters as possible */
515 while ( (pDestBuf < pEndDestBuf) && (nBufferBits >= 6) )
517 nBitBufferTemp = nBitBuffer >> (32-6);
518 *pDestBuf = aImplBase64Tab[nBitBufferTemp];
519 pDestBuf++;
520 nBitBuffer <<= 6;
521 nBufferBits -= 6;
524 if ( nBufferBits >= 6 )
526 *pInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
527 break;
530 /* Write SHIFT_OUT_CHAR, when needed */
531 if ( !bNeedShift )
533 if ( pDestBuf >= pEndDestBuf )
535 *pInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
536 break;
538 *pDestBuf = IMPL_SHIFT_OUT_CHAR;
539 pDestBuf++;
540 bShifted = false;
544 if ( !bEnd )
546 /* Character can be directly endcoded */
547 if ( !bNeedShift )
549 if ( pDestBuf >= pEndDestBuf )
551 *pInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
552 break;
554 *pDestBuf = static_cast< char >(static_cast< unsigned char >(c));
555 pDestBuf++;
558 pSrcBuf++;
561 while ( !bEnd );
563 if ( pContextData )
565 pContextData->mbShifted = bShifted;
566 pContextData->mnBitBuffer = nBitBuffer;
567 pContextData->mnBufferBits = nBufferBits;
570 *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
571 return (nDestBytes - (pEndDestBuf-pDestBuf));
574 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */