323225c231a06972832ca3858bc4431a5257f69a
6 imap_utf7_encode(uschar
*string
, const uschar
*charset
, uschar sep
,
7 uschar
*specials
, uschar
**error
)
9 static uschar encode_base64
[64] =
10 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
13 gstring
* yield
= NULL
;
14 int i
= 0, j
; /* compiler quietening */
15 uschar c
= 0; /* compiler quietening */
16 BOOL base64mode
= FALSE
;
22 uschar
*outptr
= outbuf
;
27 if (!specials
) specials
= US
"";
29 /* Pass over the string. If it consists entirely of "normal" characters
30 (possibly with leading seps), return it as is. */
31 for (s
= string
; *s
; s
++)
33 if (s
== string
&& *s
== sep
)
39 || Ustrchr(specials
, *s
)
48 slen
= Ustrlen(string
);
51 if ((icd
= iconv_open("UTF-16BE", CCS charset
)) == (iconv_t
)-1)
53 *error
= string_sprintf(
54 "imapfolder: iconv_open(\"UTF-16BE\", \"%s\") failed: %s%s",
55 charset
, strerror(errno
),
56 errno
== EINVAL
? " (maybe unsupported conversion)" : "");
64 size_t left
= sizeof(utf16buf
);
67 if ( iconv(icd
, (ICONV_ARG2_TYPE
)&sptr
, &slen
, CSS
&utf16ptr
, &left
)
72 *error
= string_sprintf("imapfolder: iconv() failed to convert from %s: %s",
73 charset
, strerror(errno
));
78 for (utf16ptr
= utf16buf
;
79 slen
> 0 && (utf16ptr
- utf16buf
) < sizeof(utf16buf
);
80 utf16ptr
+= 2, slen
--, sptr
++)
90 /* Now encode utf16buf as modified UTF-7 */
94 || (Ustrchr(specials
, s
[1]) && s
[1] != sep
)
98 /* Encode as modified BASE64 */
106 for (j
= 0; j
< 2; j
++, s
++) switch (i
++)
109 /* Top 6 bits of the first octet */
110 *outptr
++ = encode_base64
[(*s
>> 2) & 0x3F];
111 c
= (*s
& 0x03); break;
113 /* Bottom 2 bits of the first octet, and top 4 bits of the second */
114 *outptr
++ = encode_base64
[(c
<< 4) | ((*s
>> 4) & 0x0F)];
115 c
= (*s
& 0x0F); break;
117 /* Bottom 4 bits of the second octet and top 2 bits of the third */
118 *outptr
++ = encode_base64
[(c
<< 2) | ((*s
>> 6) & 0x03)];
119 /* Bottom 6 bits of the third octet */
120 *outptr
++ = encode_base64
[*s
& 0x3F];
125 else if ( (s
[1] != '.' && s
[1] != '/')
129 /* Encode as self (almost) */
135 /* Remaining bottom 2 bits of the last octet */
136 *outptr
++ = encode_base64
[c
<< 4];
139 /* Remaining bottom 4 bits of the last octet */
140 *outptr
++ = encode_base64
[c
<< 2];
166 *error
= string_sprintf("imapfolder: illegal character '%c'", s
[1]);
170 if (outptr
> outbuf
+ sizeof(outbuf
) - 3)
172 yield
= string_catn(yield
, outbuf
, outptr
- outbuf
);
177 } /* End of input string */
184 /* Remaining bottom 2 bits of the last octet */
185 *outptr
++ = encode_base64
[c
<< 4];
188 /* Remaining bottom 4 bits of the last octet */
189 *outptr
++ = encode_base64
[c
<< 2];
198 yield
= string_catn(yield
, outbuf
, outptr
- outbuf
);
200 if (yield
->s
[yield
->ptr
-1] == '.')
203 return string_from_gstring(yield
);
206 #endif /* whole file */