1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: charset.c 1032 2008-04-11 00:30:04Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2008 University of Washington
8 * Copyright 2013-2015 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../pith/headers.h"
20 #include "../pith/charset.h"
21 #include "../pith/state.h"
22 #include "../pith/conf.h"
23 #include "../pith/escapes.h"
24 #include "../pith/mimedesc.h"
25 #include "../pith/filter.h"
26 #include "../pith/string.h"
27 #include "../pith/options.h"
33 int rfc1522_token(char *, int (*)(int), char *, char **);
34 int rfc1522_valtok(int);
35 int rfc1522_valenc(int);
36 int rfc1522_valid(char *, char **, char **, char **, char **);
37 void rfc1522_copy_and_transliterate(unsigned char *, unsigned char **, size_t,
38 unsigned char *, unsigned long, char *);
39 unsigned char *rfc1522_encoded_word(unsigned char *, int, char *);
40 char *rfc1522_8bit(void *, int);
41 char *rfc1522_binary(void *, int);
45 body_charset(MAILSTREAM
*stream
, long int msgno
, unsigned char *section
)
51 if((body
= mail_body(stream
, msgno
, section
)) && body
->type
== TYPETEXT
){
52 if(!(charset
= parameter_val(body
->parameter
, "charset")))
53 charset
= cpystr("US-ASCII");
63 * Copies the source string into allocated space with the 8-bit EUC codes
64 * (on Unix) or the Shift-JIS (on PC) converted into ISO-2022-JP.
65 * Caller is responsible for freeing the result.
68 trans_euc_to_2022_jp(unsigned char *src
)
71 unsigned char *rv
, *p
, *q
;
72 int inside_esc_seq
= 0;
73 int c1
= -1; /* remembers first of pair for Shift-JIS */
78 len
= strlen((char *) src
);
81 * Worst possible increase is every other character an 8-bit character.
82 * In that case, each of those gets 6 extra charactes for the escape
83 * sequences. We're not too concerned about the extra length because
84 * these are relatively short strings.
86 alloc
= len
+ 1 + ((len
+1)/2) * 6;
87 rv
= (unsigned char *) fs_get(alloc
* sizeof(char));
89 for(p
= src
, q
= rv
; *p
; p
++){
91 if(c1
>= 0){ /* second of a pair? */
92 int adjust
= *p
< 159;
93 int rowOffset
= c1
< 160 ? 112 : 176;
94 int cellOffset
= adjust
? (*p
> 127 ? 32 : 31) : 126;
96 *q
++ = ((c1
- rowOffset
) << 1) - adjust
;
97 *q
++ = *p
- cellOffset
;
139 * * * * * * * * * RFC 1522 support routines * * * * * * * *
141 * RFC 1522 support is *very* loosely based on code contributed
142 * by Lars-Erik Johansson <lej@cdg.chalmers.se>. Thanks to Lars-Erik,
143 * and appologies for taking such liberties with his code.
146 #define RFC1522_INIT "=?"
147 #define RFC1522_INIT_L 2
148 #define RFC1522_TERM "?="
149 #define RFC1522_TERM_L 2
150 #define RFC1522_DLIM "?"
151 #define RFC1522_DLIM_L 1
152 #define RFC1522_MAXW 256 /* RFC's say 75, but no senders seem to care*/
153 #define ESPECIALS "()<>@,;:\"/[]?.="
154 #define RFC1522_OVERHEAD(S) (RFC1522_INIT_L + RFC1522_TERM_L + \
155 (2 * RFC1522_DLIM_L) + strlen(S) + 1);
156 #define RFC1522_ENC_CHAR(C) (((C) & 0x80) || !rfc1522_valtok(C) \
160 * rfc1522_decode_to_utf8 - try to decode the given source string ala RFC 2047
161 * (obsoleted RFC 1522) into the given destination buffer,
164 * How large should d be? The decoded string of octets will fit in
165 * the same size string as the source string. However, because we're
166 * translating that into UTF-8 the result may expand. Currently the
167 * Thai character set has single octet characters which expand to
168 * three octets in UTF-8. So it would be safe to use 3 * strlen(s)
169 * for the size of d. One can imagine a currently non-existent
170 * character set that expanded to 4 octets instead, so use 4 to be
173 * Returns: pointer to either the destination buffer containing the
174 * decoded text, or a pointer to the source buffer if there was
175 * no valid 'encoded-word' found during scanning.
178 rfc1522_decode_to_utf8(unsigned char *d
, size_t len
, char *s
)
180 unsigned char *rv
= NULL
, *p
;
181 char *start
= s
, *sw
, *enc
, *txt
, *ew
, **q
, *lang
;
186 *d
= '\0'; /* init destination */
188 while(s
&& (sw
= strstr(s
, RFC1522_INIT
))){
189 if(!rv
) /* there's something to do, init it */
191 /* validate the rest of the encoded-word */
192 if(rfc1522_valid(sw
, &cset
, &enc
, &txt
, &ew
)){
194 * We may have been putting off copying the first part of the
195 * source while waiting to see if we have to copy at all.
197 if(rv
== d
&& s
!= start
){
198 rfc1522_copy_and_transliterate(rv
, &d
, len
, (unsigned char *) start
,
203 /* copy everything between s and sw to destination */
204 for(i
= 0; &s
[i
] < sw
; i
++)
205 if(!isspace((unsigned char)s
[i
])){ /* if some non-whitespace */
206 while(s
< sw
&& d
-rv
<len
-1)
207 *d
++ = (unsigned char) *s
++;
212 enc
[-1] = txt
[-1] = ew
[0] = '\0'; /* tie off token strings */
214 if((lang
= strchr(cset
, '*')) != NULL
)
217 /* based on encoding, write the encoded text to output buffer */
219 case 'Q' : /* 'Q' encoding */
221 /* special hocus-pocus to deal with '_' exception, too bad */
222 for(l
= 0L, i
= 0; txt
[l
]; l
++)
227 q
= (char **) fs_get((i
+ 1) * sizeof(char *));
228 for(l
= 0L, i
= 0; txt
[l
]; l
++)
239 if((p
= rfc822_qprint((unsigned char *)txt
, strlen(txt
), &l
)) != NULL
){
240 rfc1522_copy_and_transliterate(rv
, &d
, len
, p
, l
, cset
);
241 fs_give((void **)&p
); /* free encoded buf */
245 fs_give((void **) &q
);
250 if(q
){ /* restore underscores */
251 for(i
= 0; q
[i
]; i
++)
254 fs_give((void **)&q
);
259 case 'B' : /* 'B' encoding */
261 if((p
= rfc822_base64((unsigned char *) txt
, strlen(txt
), &l
)) != NULL
){
262 rfc1522_copy_and_transliterate(rv
, &d
, len
, p
, l
, cset
);
263 fs_give((void **)&p
); /* free encoded buf */
271 rfc1522_copy_and_transliterate(rv
, &d
, len
, (unsigned char *) txt
,
273 dprint((1, "RFC1522_decode: Unknown ENCODING: %s\n",
278 /* restore trompled source string */
279 enc
[-1] = txt
[-1] = '?';
280 ew
[0] = RFC1522_TERM
[0];
282 /* advance s to start of text after encoded-word */
283 s
= ew
+ RFC1522_TERM_L
;
290 * Found intro, but bogus data followed, treat it as normal text.
292 l
= (sw
- s
) + RFC1522_INIT_L
;
293 rfc1522_copy_and_transliterate(rv
, &d
, len
, (unsigned char *) s
, l
, NULL
);
294 for(; isspace((unsigned char) *(s
+l
)) && d
-rv
<len
-1;l
++)
295 *d
++ = *(s
+l
); /* copy any trailing space */
303 if(s
&& *s
){ /* copy remaining text */
304 rfc1522_copy_and_transliterate(rv
, &d
, len
, (unsigned char *) s
, strlen(s
), NULL
);
310 rfc1522_copy_and_transliterate(rv
, &d
, len
, (unsigned char *) s
, strlen(s
), NULL
);
314 return(rv
? rv
: (unsigned char *) start
);
317 dprint((1, "RFC1522_decode: BOGUS INPUT: -->%s<--\n",
318 start
? start
: "?"));
319 return((unsigned char *) start
);
324 * rfc1522_token - scan the given source line up to the end_str making
325 * sure all subsequent chars are "valid" leaving endp
326 * a the start of the end_str.
327 * Returns: TRUE if we got a valid token, FALSE otherwise
330 rfc1522_token(char *s
, int (*valid
) (int), char *end_str
, char **endp
)
333 if((char) *s
== *end_str
/* test for matching end_str */
335 ? !strncmp((char *)s
+ 1, end_str
+ 1, strlen(end_str
+ 1))
341 if(!(*valid
)(*s
++)) /* test for valid char */
350 * rfc1522_valtok - test for valid character in the RFC 1522 encoded
351 * word's charset and encoding fields.
354 rfc1522_valtok(int c
)
356 return(!(c
== SPACE
|| iscntrl(c
& 0x7f) || strindex(ESPECIALS
, c
)));
361 * rfc1522_valenc - test for valid character in the RFC 1522 encoded
362 * word's encoded-text field.
365 rfc1522_valenc(int c
)
367 return(!(c
== '?' || c
== SPACE
) && isprint((unsigned char)c
));
372 * rfc1522_valid - validate the given string as to it's rfc1522-ness
375 rfc1522_valid(char *s
, char **charset
, char **enc
, char **txt
, char **endp
)
380 rv
= rfc1522_token(c
= s
+RFC1522_INIT_L
, rfc1522_valtok
, RFC1522_DLIM
, &e
)
381 && rfc1522_token(++e
, rfc1522_valtok
, RFC1522_DLIM
, &t
)
382 && rfc1522_token(++t
, rfc1522_valenc
, RFC1522_TERM
, &p
)
383 && p
- s
<= RFC1522_MAXW
;
402 * rfc1522_copy_and_transliterate - copy given buf to destination buffer
403 * as UTF-8 characters
406 rfc1522_copy_and_transliterate(unsigned char *rv
,
418 memset(&xsrc
, 0, sizeof(SIZEDTEXT
));
420 /* transliterate decoded segment to utf-8 */
422 if(strucmp((char *) cset
, "us-ascii")
423 && strucmp((char *) cset
, "utf-8")){
424 if(utf8_charset(cset
)){
425 if(!utf8_text(&src
, cset
, &xsrc
, 0L)){
426 /* should not happen */
427 alpine_panic("c-client failed to transliterate recognized characterset");
431 /* non-xlatable charset */
432 for(i
= 0; i
< l
; i
++)
433 if(src
.data
[i
] & 0x80){
434 xsrc
.data
= (unsigned char *) fs_get((l
+1) * sizeof(unsigned char));
436 for(i
= 0; i
< l
; i
++)
437 xsrc
.data
[i
] = (src
.data
[i
] & 0x80) ? '?' : src
.data
[i
];
448 src
.size
= strlen((char *) s
);
450 if((cs
= utf8_infercharset(&src
))){
451 if(!(cs
->type
== CT_ASCII
|| cs
->type
== CT_UTF8
)){
452 if(!utf8_text_cs(&src
, cs
, &xsrc
, 0L, 0L)){
453 /* should not happen */
454 alpine_panic("c-client failed to transliterate recognized characterset");
458 else if((cset
=ps_global
->VAR_UNK_CHAR_SET
)
459 && strucmp((char *) cset
, "us-ascii")
460 && strucmp((char *) cset
, "utf-8")
461 && utf8_charset(cset
)){
462 if(!utf8_text(&src
, cset
, &xsrc
, 0L)){
463 /* should not happen */
464 alpine_panic("c-client failed to transliterate recognized character set");
468 /* unknown bytes - mask off high bit chars */
469 for(i
= 0; i
< l
; i
++)
470 if(src
.data
[i
] & 0x80){
471 xsrc
.data
= (unsigned char *) fs_get((l
+1) * sizeof(unsigned char));
473 for(i
= 0; i
< l
; i
++)
474 xsrc
.data
[i
] = (src
.data
[i
] & 0x80) ? '?' : src
.data
[i
];
486 i
= MIN(l
,len
-1-((*d
)-rv
));
487 strncpy((char *) (*d
), (char *) s
, i
);
489 *d
+= l
; /* advance dest ptr to EOL */
493 if(xsrc
.data
&& src
.data
!= xsrc
.data
)
494 fs_give((void **) &xsrc
.data
);
500 * rfc1522_encode - encode the given source string ala RFC 1522,
501 * IF NECESSARY, into the given destination buffer.
502 * Don't bother copying if it turns out encoding
505 * Returns: pointer to either the destination buffer containing the
506 * encoded text, or a pointer to the source buffer if we didn't
507 * have to encode anything.
510 rfc1522_encode(char *d
, size_t dlen
, unsigned char *s
, char *charset
)
512 unsigned char *p
, *q
;
519 charset
= UNKNOWN_CHARSET
;
521 /* look for a reason to encode */
522 for(p
= s
, n
= 0; *p
; p
++)
526 else if(*p
== RFC1522_INIT
[0]
527 && !strncmp((char *) p
, RFC1522_INIT
, RFC1522_INIT_L
)){
528 if(rfc1522_valid((char *) p
, NULL
, NULL
, NULL
, (char **) &q
))
529 p
= q
+ RFC1522_TERM_L
- 1; /* advance past encoded gunk */
531 else if(*p
== ESCAPE
&& match_escapes((char *)(p
+1))){
535 if(n
){ /* found, encoding to do */
537 enc
= (n
> (2 * (p
- s
)) / 3) ? 'B' : 'Q';
540 if(d
-rv
< dlen
-1-(RFC1522_INIT_L
+2*RFC1522_DLIM_L
+1)){
541 sstrncpy(&d
, RFC1522_INIT
, dlen
-(d
-rv
)); /* insert intro header, */
542 sstrncpy(&d
, charset
, dlen
-(d
-rv
)); /* character set tag, */
543 sstrncpy(&d
, RFC1522_DLIM
, dlen
-(d
-rv
)); /* and encoding flavor */
547 sstrncpy(&d
, RFC1522_DLIM
, dlen
-(d
-rv
));
551 * feed lines to encoder such that they're guaranteed
552 * less than RFC1522_MAXW.
554 p
= rfc1522_encoded_word(s
, enc
, charset
);
555 if(enc
== 'B') /* insert encoded data */
556 sstrncpy(&d
, t
= rfc1522_binary(s
, p
- s
), dlen
-1-(d
-rv
));
557 else /* 'Q' encoding */
558 sstrncpy(&d
, t
= rfc1522_8bit(s
, p
- s
), dlen
-1-(d
-rv
));
560 sstrncpy(&d
, RFC1522_TERM
, dlen
-1-(d
-rv
)); /* insert terminator */
561 fs_give((void **) &t
);
562 if(*p
) /* more src string follows */
563 sstrncpy(&d
, "\015\012 ", dlen
-1-(d
-rv
)); /* insert cont. line */
565 s
= p
; /* advance s */
572 return((char *) s
); /* no work for us here */
578 * rfc1522_encoded_word -- cut given string into max length encoded word
580 * Return: pointer into 's' such that the encoded 's' is no greater
583 * NOTE: this line break code is NOT cognizant of any SI/SO
584 * charset requirements nor similar strategies using escape
585 * codes. Hopefully this will matter little and such
586 * representation strategies don't also include 8bit chars.
589 rfc1522_encoded_word(unsigned char *s
, int enc
, char *charset
)
591 int goal
= RFC1522_MAXW
- RFC1522_OVERHEAD(charset
);
593 if(enc
== 'B') /* base64 encode */
594 for(goal
= ((goal
/ 4) * 3) - 2; goal
&& *s
; goal
--, s
++)
596 else /* special 'Q' encoding */
597 for(; goal
&& *s
; s
++)
598 if((goal
-= RFC1522_ENC_CHAR(*s
) ? 3 : 1) < 0)
607 * rfc1522_8bit -- apply RFC 1522 'Q' encoding to the given 8bit buffer
609 * Return: alloc'd buffer containing encoded string
612 rfc1522_8bit(void *src
, int slen
)
614 char *ret
= (char *) fs_get ((size_t) (3*slen
+ 2));
617 unsigned char *s
= (unsigned char *) src
;
619 while (slen
--) { /* for each character */
620 if (((c
= *s
++) == '\015') && (*s
== '\012') && slen
) {
621 *d
++ = '\015'; /* true line break */
625 else if(c
== SPACE
){ /* special encoding case */
628 else if(RFC1522_ENC_CHAR(c
)){
629 *d
++ = '='; /* quote character */
633 *d
++ = (char) c
; /* ordinary character */
636 *d
= '\0'; /* tie off destination */
642 * rfc1522_binary -- apply RFC 1522 'B' encoding to the given 8bit buffer
644 * Return: alloc'd buffer containing encoded string
647 rfc1522_binary (void *src
, int srcl
)
650 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
651 unsigned char *s
= (unsigned char *) src
;
654 d
= ret
= (char *) fs_get ((size_t) ((((srcl
+ 2) / 3) * 4) + 1));
655 for (; srcl
; s
+= 3) { /* process tuplets */
656 /* byte 1: high 6 bits (1) */
658 /* byte 2: low 2 bits (1), high 4 bits (2) */
659 *d
++ = v
[((s
[0] << 4) + (--srcl
? (s
[1] >> 4) : 0)) & 0x3f];
660 /* byte 3: low 4 bits (2), high 2 bits (3) */
661 *d
++ = srcl
? v
[((s
[1] << 2) + (--srcl
? (s
[2] >> 6) :0)) & 0x3f] :'=';
662 /* byte 4: low 6 bits (3) */
663 *d
++ = srcl
? v
[s
[2] & 0x3f] : '=';
665 srcl
--; /* count third character if processed */
668 *d
= '\0'; /* tie off string */
669 return(ret
); /* return the resulting string */
674 * Checks if charset conversion is possible and which quality could be achieved
676 * args: from_cs -- charset to convert from
677 * to_cs -- charset to convert to
680 * CONV_TABLE->table -- conversion table, NULL if conversion not needed
682 * CONV_TABLE->quality -- conversion quality (conversion not supported, not
683 * needed, loses special chars, or loses letters
685 * The other entries of CONV_TABLE are used inside this function only
686 * and may not be used outside unless this documentation is updated.
689 conversion_table(char *from_cs
, char *to_cs
)
692 unsigned char *p
= NULL
;
693 unsigned short *fromtab
, *totab
;
694 CONV_TABLE
*ct
= NULL
;
695 const CHARSET
*from
, *to
;
696 static CONV_TABLE null_tab
;
698 if(!(from_cs
&& *from_cs
&& to_cs
&& *to_cs
) || !strucmp(from_cs
, to_cs
)){
699 memset(&null_tab
, 0, sizeof(null_tab
));
700 null_tab
.quality
= CV_NO_TRANSLATE_NEEDED
;
705 * First check to see if we are already set up for this pair of charsets.
707 if((ct
= ps_global
->conv_table
) != NULL
708 && ct
->from_charset
&& ct
->to_charset
709 && !strucmp(ct
->from_charset
, from_cs
)
710 && !strucmp(ct
->to_charset
, to_cs
))
714 * No such luck. Get rid of the cache of the previous translation table
715 * and build a new one.
718 if(ct
->table
&& (ct
->convert
!= gf_convert_utf8_charset
))
719 fs_give((void **) &ct
->table
);
722 fs_give((void **) &ct
->from_charset
);
725 fs_give((void **) &ct
->to_charset
);
728 ct
= ps_global
->conv_table
= (CONV_TABLE
*) fs_get(sizeof(*ct
));
730 memset(ct
, 0, sizeof(*ct
));
732 ct
->from_charset
= cpystr(from_cs
);
733 ct
->to_charset
= cpystr(to_cs
);
734 ct
->quality
= CV_NO_TRANSLATE_POSSIBLE
;
737 * Check to see if a translation is feasible.
739 from
= utf8_charset(from_cs
);
740 to
= utf8_charset(to_cs
);
742 if(from
&& to
){ /* if both charsets found */
743 /* no mapping if same or from is ASCII */
744 if((from
->type
== to
->type
&& from
->tab
== to
->tab
)
745 || (from
->type
== CT_ASCII
))
746 ct
->quality
= CV_NO_TRANSLATE_NEEDED
;
747 else switch(from
->type
){
748 case CT_1BYTE0
: /* 1 byte no table */
749 case CT_1BYTE
: /* 1 byte ASCII + table 0x80-0xff */
750 case CT_1BYTE8
: /* 1 byte table 0x00 - 0xff */
752 case CT_1BYTE0
: /* 1 byte no table */
753 case CT_1BYTE
: /* 1 byte ASCII + table 0x80-0xff */
754 case CT_1BYTE8
: /* 1 byte table 0x00 - 0xff */
755 ct
->quality
= (from
->script
& to
->script
) ?
756 CV_LOSES_SOME_LETTERS
: CV_LOSES_SPECIAL_CHARS
;
760 case CT_UTF8
: /* variable UTF-8 encoded Unicode no table */
761 /* If source is UTF-8, see if destination charset has an 8 or 16 bit
762 * coded character set that we can translate to. By special
763 * dispensation, kludge ISO-2022-JP to EUC or Shift-JIS, but don't
764 * try to do any other ISO 2022 charsets or UTF-7.
767 case CT_SJIS
: /* 2 byte Shift-JIS */
768 /* only win if can get EUC-JP chartab */
769 if(utf8_charset("EUC-JP"))
770 ct
->quality
= CV_LOSES_SOME_LETTERS
;
772 case CT_ASCII
: /* 7-bit ASCII no table */
773 case CT_1BYTE0
: /* 1 byte no table */
774 case CT_1BYTE
: /* 1 byte ASCII + table 0x80-0xff */
775 case CT_1BYTE8
: /* 1 byte table 0x00 - 0xff */
776 case CT_EUC
: /* 2 byte ASCII + utf8_eucparam base/CS2/CS3 */
777 case CT_DBYTE
: /* 2 byte ASCII + utf8_eucparam */
778 case CT_DBYTE2
: /* 2 byte ASCII + utf8_eucparam plane1/2 */
779 ct
->quality
= CV_LOSES_SOME_LETTERS
;
785 switch (ct
->quality
) { /* need to map? */
786 case CV_NO_TRANSLATE_POSSIBLE
:
787 case CV_NO_TRANSLATE_NEEDED
:
788 break; /* no mapping needed */
789 default: /* do mapping */
790 switch (from
->type
) {
791 case CT_UTF8
: /* UTF-8 to legacy character set */
792 if((ct
->table
= utf8_rmap (to_cs
)) != NULL
)
793 ct
->convert
= gf_convert_utf8_charset
;
796 case CT_1BYTE0
: /* ISO 8859-1 */
797 case CT_1BYTE
: /* low part ASCII, high part other */
798 case CT_1BYTE8
: /* low part has some non-ASCII */
800 * The fromtab and totab tables are mappings from the 128 character
801 * positions 128-255 to their Unicode values (so unsigned shorts).
802 * The table we are creating is such that if
804 * from_char_value -> unicode_value
805 * to_char_value -> same_unicode_value
807 * then we want to map from_char_value -> to_char_value
809 * To simplify conversions we create the whole 256 element array,
810 * with the first 128 positions just the identity. If there is no
811 * conversion for a particular from_char_value (that is, no
812 * to_char_value maps to the same unicode character) then we put
813 * '?' in that character. We may want to output blob on the PC,
816 * If fromtab or totab are NULL, that means the mapping is simply
817 * the identity mapping. Since that is still useful to us, we
818 * create it on the fly.
820 fromtab
= (unsigned short *) from
->tab
;
821 totab
= (unsigned short *) to
->tab
;
823 ct
->convert
= gf_convert_8bit_charset
;
824 p
= ct
->table
= (unsigned char *)
825 fs_get(256 * sizeof(unsigned char));
826 for(i
= 0; i
< 256; i
++){
829 switch(from
->type
){ /* get "from" UCS-2 codepoint */
830 case CT_1BYTE0
: /* ISO 8859-1 */
833 case CT_1BYTE
: /* low part ASCII, high part other */
834 fc
= (i
< 128) ? i
: fromtab
[i
-128];
836 case CT_1BYTE8
: /* low part has some non-ASCII */
840 switch(to
->type
){ /* match against "to" UCS-2 codepoint */
841 case CT_1BYTE0
: /* identity match for ISO 8859-1*/
845 case CT_1BYTE
: /* ASCII is identity, search high part */
846 if(fc
< 128) p
[i
] = fc
;
847 else for(j
= 0; j
< 128; j
++){
854 case CT_1BYTE8
: /* search all codepoints */
855 for(j
= 0; j
< 256; j
++){
874 * Replace personal names in list of addresses with
875 * decoded personal names in UTF-8.
876 * Assumes we can free and reallocate the name.
879 decode_addr_names_to_utf8(struct mail_address
*a
)
881 for(; a
; a
= a
->next
)
883 convert_possibly_encoded_str_to_utf8(&a
->personal
);
888 * Strp is a pointer to an allocated string.
889 * This routine will convert the string to UTF-8, possibly
890 * freeing and re-allocating it.
891 * The source string may or may not have RFC1522 encoding
892 * which will be undone using rfc1522_decode.
893 * The string will have been converted on return.
896 convert_possibly_encoded_str_to_utf8(char **strp
)
898 size_t len
, lensrc
, lenresult
;
899 char *bufp
, *decoded
;
901 if(!strp
|| !*strp
|| **strp
== '\0')
904 len
= 4 * strlen(*strp
) + 1;
905 bufp
= (char *) fs_get(len
);
907 decoded
= (char *) rfc1522_decode_to_utf8((unsigned char *) bufp
, len
, *strp
);
908 if(decoded
!= (*strp
)){ /* unchanged */
909 if((lensrc
=strlen(*strp
)) >= (lenresult
=strlen(decoded
))){
910 strncpy(*strp
, decoded
, lensrc
);
911 (*strp
)[lensrc
] = '\0';
914 fs_give((void **) strp
);
915 if(decoded
== bufp
){ /* this will be true */
916 fs_resize((void **) &bufp
, lenresult
+1);
920 else{ /* this is unreachable */
921 *strp
= cpystr(decoded
);
925 /* else, already UTF-8 */
928 fs_give((void **) &bufp
);