1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ String support routines.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #define n_FILE strings
38 #ifndef HAVE_AMALGAMATION
45 (savestr
)(char const *str n_MEMORY_DEBUG_ARGS
)
52 news
= (n_autorec_alloc_from_pool
)(NULL
, size
+1 n_MEMORY_DEBUG_ARGSCALL
);
54 memcpy(news
, str
, size
);
61 (savestrbuf
)(char const *sbuf
, size_t sbuf_len n_MEMORY_DEBUG_ARGS
)
66 news
= (n_autorec_alloc_from_pool
)(NULL
, sbuf_len
+1
67 n_MEMORY_DEBUG_ARGSCALL
);
69 memcpy(news
, sbuf
, sbuf_len
);
76 (savecatsep
)(char const *s1
, char sep
, char const *s2 n_MEMORY_DEBUG_ARGS
)
82 l1
= (s1
!= NULL
) ? strlen(s1
) : 0;
84 news
= (n_autorec_alloc_from_pool
)(NULL
, l1
+ (sep
!= '\0') + l2
+1
85 n_MEMORY_DEBUG_ARGSCALL
);
87 memcpy(news
+ 0, s1
, l1
);
92 memcpy(news
+ l1
, s2
, l2
);
99 * Support routines, auto-reclaimed storage
103 str_concat_csvl(struct str
*self
, ...) /* XXX onepass maybe better here */
111 for (l
= 0; (cs
= va_arg(vl
, char const*)) != NULL
;)
116 self
->s
= n_autorec_alloc(l
+1);
119 for (l
= 0; (cs
= va_arg(vl
, char const*)) != NULL
;) {
124 memcpy(self
->s
+ l
, cs
, i
);
135 (str_concat_cpa
)(struct str
*self
, char const * const *cpa
,
136 char const *sep_o_null n_MEMORY_DEBUG_ARGS
)
139 char const * const *xcpa
;
142 sonl
= (sep_o_null
!= NULL
) ? strlen(sep_o_null
) : 0;
144 for (l
= 0, xcpa
= cpa
; *xcpa
!= NULL
; ++xcpa
)
145 l
+= strlen(*xcpa
) + sonl
;
148 self
->s
= (n_autorec_alloc_from_pool
)(NULL
, l
+1 n_MEMORY_DEBUG_ARGSCALL
);
150 for (l
= 0, xcpa
= cpa
; *xcpa
!= NULL
; ++xcpa
) {
155 memcpy(self
->s
+ l
, *xcpa
, i
);
159 memcpy(self
->s
+ l
, sep_o_null
, sonl
);
169 * Routines that are not related to auto-reclaimed storage follow.
173 n_anyof_buf(char const *template, char const *dat
, size_t len
){
178 while((c
= *template++) != '\0')
179 if(strchr(dat
, c
) != NULL
)
182 while((c
= *template++) != '\0')
183 if(memchr(dat
, c
, len
) != NULL
)
192 n_strsep(char **iolist
, char sep
, bool_t ignore_empty
){
196 for(base
= *iolist
; base
!= NULL
; base
= *iolist
){
197 while(*base
!= '\0' && blankspacechar(*base
))
200 cp
= strchr(base
, sep
);
205 cp
= &base
[strlen(base
)];
207 while(cp
> base
&& blankspacechar(cp
[-1]))
210 if(*base
!= '\0' || !ignore_empty
)
218 n_strsep_esc(char **iolist
, char sep
, bool_t ignore_empty
){
220 bool_t isesc
, anyesc
;
223 for(base
= *iolist
; base
!= NULL
; base
= *iolist
){
224 while((c
= *base
) != '\0' && blankspacechar(c
))
227 for(isesc
= anyesc
= FAL0
, cp
= base
;; ++cp
){
228 if(n_UNLIKELY((c
= *cp
) == '\0')){
239 anyesc
|= (c
== sep
);
243 while(cp
> base
&& blankspacechar(cp
[-1]))
251 for(ins
= cp
= base
;; ++ins
)
252 if((c
= *cp
) == '\\' && cp
[1] == sep
){
255 }else if((*ins
= (++cp
, c
)) == '\0')
260 if(*base
!= '\0' || !ignore_empty
)
268 is_prefix(char const *as1
, char const *as2
) /* TODO arg order */
273 for (; (c
= *as1
) == *as2
&& c
!= '\0'; ++as1
, ++as2
)
281 string_quote(char const *v
) /* TODO too simpleminded (getrawlist(), +++ ..) */
288 for (i
= 0, cp
= v
; (c
= *cp
) != '\0'; ++i
, ++cp
)
289 if (c
== '"' || c
== '\\')
291 rv
= n_autorec_alloc(i
+1);
293 for (i
= 0, cp
= v
; (c
= *cp
) != '\0'; rv
[i
++] = c
, ++cp
)
294 if (c
== '"' || c
== '\\')
302 laststring(char *linebuf
, bool_t
*needs_list
, bool_t strip
)
304 char *cp
, *p
, quoted
;
307 /* Anything to do at all? */
308 if (*(cp
= linebuf
) == '\0')
310 cp
+= strlen(linebuf
) -1;
312 /* Strip away trailing blanks */
313 while (spacechar(*cp
) && cp
> linebuf
)
319 /* Now search for the BOS of the "last string" */
321 if (quoted
== '\'' || quoted
== '"') {
327 while (cp
> linebuf
) {
332 } else if (!spacechar(*cp
))
334 if (cp
== linebuf
|| cp
[-1] != '\\') {
335 /* When in whitespace mode, WS prefix doesn't belong */
340 /* Expand the escaped quote character */
341 for (p
= --cp
; (p
[0] = p
[1]) != '\0'; ++p
)
344 if (strip
&& quoted
!= ' ' && *cp
== quoted
)
345 for (p
= cp
; (p
[0] = p
[1]) != '\0'; ++p
)
348 /* The "last string" has been skipped over, but still, try to step backwards
349 * until we are at BOS or see whitespace, so as to make possible things like
350 * "? copy +'x y.mbox'" or even "? copy +x\ y.mbox" */
351 while (cp
> linebuf
) {
353 if (spacechar(*cp
)) {
356 /* We can furtherly release our callees if we now decide whether the
357 * remaining non-"last string" line content contains non-WS */
358 while (--p
>= linebuf
)
367 if (cp
!= NULL
&& *cp
== '\0')
369 *needs_list
= (cp
!= linebuf
&& *linebuf
!= '\0');
380 makelow(char *cp
) /* TODO isn't that crap? --> */
383 #ifdef HAVE_C90AMEND1
384 if (n_mb_cur_max
> 1) {
389 while (*cp
!= '\0') {
390 len
= mbtowc(&wc
, cp
, n_mb_cur_max
);
395 if (wctomb(tp
, wc
) == len
)
396 tp
+= len
, cp
+= len
;
398 *tp
++ = *cp
++; /* <-- at least here */
405 *cp
= tolower((uc_i
)*cp
);
406 while (*cp
++ != '\0');
412 substr(char const *str
, char const *sub
)
414 char const *cp
, *backup
;
419 while (*str
!= '\0' && *cp
!= '\0') {
420 #ifdef HAVE_C90AMEND1
421 if (n_mb_cur_max
> 1) {
425 if ((sz
= mbtowc(&c
, cp
, n_mb_cur_max
)) == -1)
428 if ((sz
= mbtowc(&c2
, str
, n_mb_cur_max
)) == -1)
434 if ((sz
= mbtowc(&c
, backup
, n_mb_cur_max
)) > 0) {
460 return (*cp
== '\0');
464 sstpcpy(char *dst
, char const *src
)
467 while ((*dst
= *src
++) != '\0')
474 (sstrdup
)(char const *cp n_MEMORY_DEBUG_ARGS
)
479 dp
= (cp
== NULL
) ? NULL
: (sbufdup
)(cp
, strlen(cp
) n_MEMORY_DEBUG_ARGSCALL
);
485 (sbufdup
)(char const *cp
, size_t len n_MEMORY_DEBUG_ARGS
)
490 dp
= (n_alloc
)(len
+1 n_MEMORY_DEBUG_ARGSCALL
);
499 n_strscpy(char *dst
, char const *src
, size_t dstsize
){
503 if(n_LIKELY(dstsize
> 0)){
506 if((dst
[rv
] = src
[rv
]) == '\0')
509 }while(--dstsize
> 0);
523 asccasecmp(char const *s1
, char const *s2
)
529 char c1
= *s1
++, c2
= *s2
++;
530 if ((cmp
= lowerconv(c1
) - lowerconv(c2
)) != 0 || c1
== '\0')
538 ascncasecmp(char const *s1
, char const *s2
, size_t sz
)
544 char c1
= *s1
++, c2
= *s2
++;
545 cmp
= (ui8_t
)lowerconv(c1
);
546 cmp
-= (ui8_t
)lowerconv(c2
);
547 if (cmp
!= 0 || c1
== '\0')
555 asccasestr(char const *s1
, char const *s2
)
560 for (c2
= *s2
++, c2
= lowerconv(c2
);;) {
561 if ((c1
= *s1
++) == '\0') {
565 if (lowerconv(c1
) == c2
&& is_asccaseprefix(s2
, s1
)) {
575 is_asccaseprefix(char const *as1
, char const *as2
) /* TODO arg order */
580 for(;; ++as1
, ++as2
){
586 if(c1
!= c2
|| c1
== '\0')
596 is_ascncaseprefix(char const *as1
, char const *as2
, size_t sz
)
602 for(rv
= TRU1
; sz
-- > 0; ++as1
, ++as2
){
608 if(!(rv
= (c1
== c2
)) || c1
== '\0')
619 (n_str_assign_buf
)(struct str
*self
, char const *buf
, uiz_t buflen
620 n_MEMORY_DEBUG_ARGS
){
622 if(buflen
== UIZ_MAX
)
623 buflen
= (buf
== NULL
) ? 0 : strlen(buf
);
625 assert(buflen
== 0 || buf
!= NULL
);
627 if(n_LIKELY(buflen
> 0)){
628 self
->s
= (n_realloc
)(self
->s
, (self
->l
= buflen
) +1
629 n_MEMORY_DEBUG_ARGSCALL
);
630 memcpy(self
->s
, buf
, buflen
);
631 self
->s
[buflen
] = '\0';
639 (n_str_add_buf
)(struct str
*self
, char const *buf
, uiz_t buflen
640 n_MEMORY_DEBUG_ARGS
){
642 if(buflen
== UIZ_MAX
)
643 buflen
= (buf
== NULL
) ? 0 : strlen(buf
);
645 assert(buflen
== 0 || buf
!= NULL
);
648 size_t osl
= self
->l
, nsl
= osl
+ buflen
;
650 self
->s
= (n_realloc
)(self
->s
, (self
->l
= nsl
) +1
651 n_MEMORY_DEBUG_ARGSCALL
);
652 memcpy(self
->s
+ osl
, buf
, buflen
);
660 n_str_trim(struct str
*self
, enum n_str_trim_flags stf
){
667 if((l
= self
->l
) > 0 && (stf
& n_STR_TRIM_FRONT
)){
668 while(spacechar(*cp
)){
673 self
->s
= n_UNCONST(cp
);
676 if(l
> 0 && (stf
& n_STR_TRIM_END
)){
677 for(cp
+= l
-1; spacechar(*cp
); --cp
)
688 n_str_trim_ifs(struct str
*self
, bool_t dodefaults
){
690 char const *ifs
, *cp
;
694 if((l
= self
->l
) == 0)
697 ifs
= ok_vlook(ifs_ws
);
701 /* Check whether we can go fast(er) path */
702 for(i
= 0; (c
= ifs
[i
]) != '\0'; ++i
){
704 case ' ': s
= c
; break;
705 case '\t': t
= c
; break;
706 case '\n': n
= c
; break;
708 /* Need to go the slow path */
709 while(strchr(ifs
, *cp
) != NULL
){
714 self
->s
= n_UNCONST(cp
);
717 for(cp
+= l
-1; strchr(ifs
, *cp
) != NULL
;){
720 /* An uneven number of reverse solidus escapes last WS! */
721 else if(*--cp
== '\\'){
724 for(j
= 1; l
- (uiz_t
)j
> 0 && cp
[-j
] == '\\'; ++j
)
743 /* No ifs-ws? No more data? No trimming */
744 if(l
== 0 || (i
== 0 && !dodefaults
))
754 while((c
= *cp
) != '\0' && (c
== s
|| c
== t
|| c
== n
)){
759 self
->s
= n_UNCONST(cp
);
763 for(cp
+= l
-1; (c
= *cp
) != '\0' && (c
== s
|| c
== t
|| c
== n
);){
766 /* An uneven number of reverse solidus escapes last WS! */
767 else if(*--cp
== '\\'){
770 for(j
= 1; l
- (uiz_t
)j
> 0 && cp
[-j
] == '\\'; ++j
)
786 * struct n_string TODO extend, optimize
790 (n_string_clear
)(struct n_string
*self n_MEMORY_DEBUG_ARGS
){
793 assert(self
!= NULL
);
795 if(self
->s_size
!= 0){
797 (n_free
)(self
->s_dat n_MEMORY_DEBUG_ARGSCALL
);
799 self
->s_len
= self
->s_auto
= self
->s_size
= 0;
807 (n_string_reserve
)(struct n_string
*self
, size_t noof n_MEMORY_DEBUG_ARGS
){
810 assert(self
!= NULL
);
814 if((size_t)SI32_MAX
- n_ALIGN(1) - l
<= noof
)
815 n_panic(_("Memory allocation too large"));
817 if((i
= s
- l
) <= ++noof
){
818 i
+= l
+ (ui32_t
)noof
;
823 self
->s_dat
= (n_realloc
)(self
->s_dat
, i n_MEMORY_DEBUG_ARGSCALL
);
825 char *ndat
= (n_autorec_alloc_from_pool
)(NULL
, i
826 n_MEMORY_DEBUG_ARGSCALL
);
829 memcpy(ndat
, self
->s_dat
, l
);
838 (n_string_resize
)(struct n_string
*self
, size_t nlen n_MEMORY_DEBUG_ARGS
){
840 assert(self
!= NULL
);
842 if(UICMP(z
, SI32_MAX
, <=, nlen
))
843 n_panic(_("Memory allocation too large"));
845 if(self
->s_len
< nlen
)
846 self
= (n_string_reserve
)(self
, nlen n_MEMORY_DEBUG_ARGSCALL
);
847 self
->s_len
= (ui32_t
)nlen
;
853 (n_string_push_buf
)(struct n_string
*self
, char const *buf
, size_t buflen
854 n_MEMORY_DEBUG_ARGS
){
857 assert(self
!= NULL
);
858 assert(buflen
== 0 || buf
!= NULL
);
860 if(buflen
== UIZ_MAX
)
861 buflen
= (buf
== NULL
) ? 0 : strlen(buf
);
866 self
= (n_string_reserve
)(self
, buflen n_MEMORY_DEBUG_ARGSCALL
);
867 memcpy(&self
->s_dat
[i
= self
->s_len
], buf
, buflen
);
868 self
->s_len
= (i
+= (ui32_t
)buflen
);
875 (n_string_push_c
)(struct n_string
*self
, char c n_MEMORY_DEBUG_ARGS
){
878 assert(self
!= NULL
);
880 if(self
->s_len
+ 1 >= self
->s_size
)
881 self
= (n_string_reserve
)(self
, 1 n_MEMORY_DEBUG_ARGSCALL
);
882 self
->s_dat
[self
->s_len
++] = c
;
888 (n_string_unshift_buf
)(struct n_string
*self
, char const *buf
, size_t buflen
889 n_MEMORY_DEBUG_ARGS
){
892 assert(self
!= NULL
);
893 assert(buflen
== 0 || buf
!= NULL
);
895 if(buflen
== UIZ_MAX
)
896 buflen
= (buf
== NULL
) ? 0 : strlen(buf
);
899 self
= (n_string_reserve
)(self
, buflen n_MEMORY_DEBUG_ARGSCALL
);
901 memmove(&self
->s_dat
[buflen
], self
->s_dat
, self
->s_len
);
902 memcpy(self
->s_dat
, buf
, buflen
);
903 self
->s_len
+= (ui32_t
)buflen
;
910 (n_string_unshift_c
)(struct n_string
*self
, char c n_MEMORY_DEBUG_ARGS
){
913 assert(self
!= NULL
);
915 if(self
->s_len
+ 1 >= self
->s_size
)
916 self
= (n_string_reserve
)(self
, 1 n_MEMORY_DEBUG_ARGSCALL
);
918 memmove(&self
->s_dat
[1], self
->s_dat
, self
->s_len
);
926 (n_string_insert_buf
)(struct n_string
*self
, size_t idx
,
927 char const *buf
, size_t buflen n_MEMORY_DEBUG_ARGS
){
930 assert(self
!= NULL
);
931 assert(buflen
== 0 || buf
!= NULL
);
932 assert(idx
<= self
->s_len
);
934 if(buflen
== UIZ_MAX
)
935 buflen
= (buf
== NULL
) ? 0 : strlen(buf
);
938 self
= (n_string_reserve
)(self
, buflen n_MEMORY_DEBUG_ARGSCALL
);
940 memmove(&self
->s_dat
[idx
+ buflen
], &self
->s_dat
[idx
],
942 memcpy(&self
->s_dat
[idx
], buf
, buflen
);
943 self
->s_len
+= (ui32_t
)buflen
;
950 (n_string_insert_c
)(struct n_string
*self
, size_t idx
,
951 char c n_MEMORY_DEBUG_ARGS
){
954 assert(self
!= NULL
);
955 assert(idx
<= self
->s_len
);
957 if(self
->s_len
+ 1 >= self
->s_size
)
958 self
= (n_string_reserve
)(self
, 1 n_MEMORY_DEBUG_ARGSCALL
);
960 memmove(&self
->s_dat
[idx
+ 1], &self
->s_dat
[idx
], self
->s_len
- idx
);
961 self
->s_dat
[idx
] = c
;
968 n_string_cut(struct n_string
*self
, size_t idx
, size_t len
){
971 assert(self
!= NULL
);
972 assert(UIZ_MAX
- idx
> len
);
973 assert(SI32_MAX
>= idx
+ len
);
974 assert(idx
+ len
<= self
->s_len
);
977 memmove(&self
->s_dat
[idx
], &self
->s_dat
[idx
+ len
],
978 (self
->s_len
-= len
) - idx
);
984 (n_string_cp
)(struct n_string
*self n_MEMORY_DEBUG_ARGS
){
988 assert(self
!= NULL
);
990 if(self
->s_size
== 0)
991 self
= (n_string_reserve
)(self
, 1 n_MEMORY_DEBUG_ARGSCALL
);
993 (rv
= self
->s_dat
)[self
->s_len
] = '\0';
999 n_string_cp_const(struct n_string
const *self
){
1003 assert(self
!= NULL
);
1005 if(self
->s_size
!= 0){
1006 ((struct n_string
*)n_UNCONST(self
))->s_dat
[self
->s_len
] = '\0';
1019 n_utf8_to_utf32(char const **bdat
, size_t *blen
){
1021 char const *cp
, *cpx
;
1026 x
= (ui8_t
)*(cp
= *bdat
);
1029 if(n_LIKELY(x
<= 0x7Fu
))
1031 /* 0xF8, but Unicode guarantees maximum of 0x10FFFFu -> F4 8F BF BF.
1032 * Unicode 9.0, 3.9, UTF-8, Table 3-7. Well-Formed UTF-8 Byte Sequences */
1033 else if(n_LIKELY(x
> 0xC0u
&& x
<= 0xF4u
)){
1034 if(n_LIKELY(x
< 0xE0u
)){
1035 if(n_UNLIKELY(l
< 1))
1040 }else if(n_LIKELY(x
< 0xF0u
)){
1041 if(n_UNLIKELY(l
< 2))
1048 /* Second byte constraints */
1052 if(n_UNLIKELY(x
< 0xA0u
|| x
> 0xBFu
))
1056 if(n_UNLIKELY(x
< 0x80u
|| x
> 0x9Fu
))
1060 if(n_UNLIKELY((x
& 0xC0u
) != 0x80u
))
1067 if(n_UNLIKELY(l
< 3))
1074 /* Second byte constraints */
1078 if(n_UNLIKELY(x
< 0x90u
|| x
> 0xBFu
))
1082 if(n_UNLIKELY((x
& 0xF0u
) != 0x80u
)) /* 80..8F */
1086 if(n_UNLIKELY((x
& 0xC0u
) != 0x80u
))
1094 if(n_UNLIKELY((x
& 0xC0u
) != 0x80u
))
1101 if(n_UNLIKELY((x
& 0xC0u
) != 0x80u
))
1122 n_utf32_to_utf8(ui32_t c
, char *buf
)
1129 ui8_t dec_leader_mask
;
1130 ui8_t dec_leader_val_mask
;
1131 ui8_t dec_bytes_togo
;
1135 {0x00000000, 0x00000000, 0x00, 0, 0x00, 0x00, 0, 0, {0,}},
1136 {0x00000000, 0x0000007F, 0x00, 1, 0x80, 0x7F, 1-1, 1, {0,}},
1137 {0x00000080, 0x000007FF, 0xC0, 2, 0xE0, 0xFF-0xE0, 2-1, 2, {0,}},
1138 /* We assume surrogates are U+D800 - U+DFFF, _cat index 3 */
1139 /* xxx _from_utf32() simply assumes magic code points for surrogates!
1140 * xxx (However, should we ever get yet another surrogate range we
1141 * xxx need to deal with that all over the place anyway? */
1142 {0x00000800, 0x0000FFFF, 0xE0, 3, 0xF0, 0xFF-0xF0, 3-1, 3, {0,}},
1143 {0x00010000, 0x0010FFFF, 0xF0, 4, 0xF8, 0xFF-0xF8, 4-1, 4, {0,}},
1147 if (c
<= _cat
[0].upper_bound
) { catp
+= 0; goto j0
; }
1148 if (c
<= _cat
[1].upper_bound
) { catp
+= 1; goto j1
; }
1149 if (c
<= _cat
[2].upper_bound
) { catp
+= 2; goto j2
; }
1150 if (c
<= _cat
[3].upper_bound
) {
1151 /* Surrogates may not be converted (Compatibility rule C10) */
1152 if (c
>= 0xD800u
&& c
<= 0xDFFFu
)
1157 if (c
<= _cat
[4].upper_bound
) { catp
+= 4; goto j4
; }
1159 c
= 0xFFFDu
; /* Unicode replacement character */
1163 buf
[3] = (char)0x80u
| (char)(c
& 0x3Fu
); c
>>= 6;
1165 buf
[2] = (char)0x80u
| (char)(c
& 0x3Fu
); c
>>= 6;
1167 buf
[1] = (char)0x80u
| (char)(c
& 0x3Fu
); c
>>= 6;
1169 buf
[0] = (char)catp
->enc_leader
| (char)(c
);
1171 buf
[catp
->enc_lval
] = '\0';
1178 * Our iconv(3) wrapper
1182 n_iconv_normalize_name(char const *cset
){
1183 char *cp
, c
, *tcp
, tc
;
1187 /* We need to strip //SUFFIXes off, we want to normalize to all lowercase,
1188 * and we perform some slight content testing, too */
1189 for(any
= FAL0
, cp
= n_UNCONST(cset
); (c
= *cp
) != '\0'; ++cp
){
1190 if(!alnumchar(c
) && !punctchar(c
)){
1191 n_err(_("Invalid character set name %s\n"),
1192 n_shexp_quote_cp(cset
, FAL0
));
1197 else if(upperchar(c
))
1201 if(any
|| c
!= '\0'){
1202 cp
= savestrbuf(cset
, PTR2SIZE(cp
- cset
));
1203 for(tcp
= cp
; (tc
= *tcp
) != '\0'; ++tcp
)
1204 *tcp
= lowerconv(tc
);
1206 if(c
!= '\0' && (n_poption
& n_PO_D_V
))
1207 n_err(_("Stripped off character set suffix: %s -> %s\n"),
1208 n_shexp_quote_cp(cset
, FAL0
), n_shexp_quote_cp(cp
, FAL0
));
1214 return n_UNCONST(cset
);
1218 n_iconv_name_is_ascii(char const *cset
){ /* TODO ctext/su */
1222 /* In MIME preference order */
1223 rv
= (!asccasecmp(cset
, "US-ASCII") || !asccasecmp(cset
, "ASCII") ||
1224 !asccasecmp(cset
, "ANSI_X3.4-1968") ||
1225 !asccasecmp(cset
, "iso-ir-6") ||
1226 !asccasecmp(cset
, "ANSI_X3.4-1986") ||
1227 !asccasecmp(cset
, "ISO_646.irv:1991") ||
1228 !asccasecmp(cset
, "ISO646-US") || !asccasecmp(cset
, "us") ||
1229 !asccasecmp(cset
, "IBM367") || !asccasecmp(cset
, "cp367") ||
1230 !asccasecmp(cset
, "csASCII"));
1237 n_iconv_open(char const *tocode
, char const *fromcode
){
1241 if((!asccasecmp(fromcode
, "unknown-8bit") ||
1242 !asccasecmp(fromcode
, "binary")) &&
1243 (fromcode
= ok_vlook(charset_unknown_8bit
)) == NULL
)
1244 fromcode
= ok_vlook(CHARSET_8BIT_OKEY
);
1246 id
= iconv_open(tocode
, fromcode
);
1248 /* If the encoding names are equal at this point, they are just not
1249 * understood by iconv(), and we cannot sensibly use it in any way. We do
1250 * not perform this as an optimization above since iconv() can otherwise be
1251 * used to check the validity of the input even with identical encoding
1253 if (id
== (iconv_t
)-1 && !asccasecmp(tocode
, fromcode
))
1254 n_err_no
= n_ERR_NONE
;
1260 n_iconv_close(iconv_t cd
){
1264 iconvd
= (iconv_t
)-1;
1269 n_iconv_reset(iconv_t cd
){
1271 iconv(cd
, NULL
, NULL
, NULL
, NULL
);
1275 /* (2012-09-24: export and use it exclusively to isolate prototype problems
1276 * (*inb* is 'char const **' except in POSIX) in a single place.
1277 * GNU libiconv even allows for configuration time const/non-const..
1278 * In the end it's an ugly guess, but we can't do better since make(1) doesn't
1279 * support compiler invocations which bail on error, so no -Werror */
1280 /* Citrus project? */
1281 # if defined _ICONV_H_ && defined __ICONV_F_HIDE_INVALID
1282 /* DragonFly 3.2.1 is special TODO newer DragonFly too, but different */
1284 # define __INBCAST(S) (char ** __restrict__)n_UNCONST(S)
1286 # define __INBCAST(S) (char const **)n_UNCONST(S)
1288 # elif n_OS_SUNOS || n_OS_SOLARIS
1289 # define __INBCAST(S) (char const ** __restrict__)n_UNCONST(S)
1292 # define __INBCAST(S) (char **)n_UNCONST(S)
1296 n_iconv_buf(iconv_t cd
, enum n_iconv_flags icf
,
1297 char const **inb
, size_t *inbleft
, char **outb
, size_t *outbleft
){
1301 if((icf
& n_ICONV_UNIREPL
) && !(n_psonce
& n_PSO_UNICODE
))
1302 icf
&= ~n_ICONV_UNIREPL
;
1307 if((sz
= iconv(cd
, __INBCAST(inb
), inbleft
, outb
, outbleft
)) == 0)
1309 if(sz
!= (size_t)-1){
1310 if(!(icf
& n_ICONV_IGN_NOREVERSE
)){
1317 if((err
= n_err_no
) == n_ERR_2BIG
)
1320 if(!(icf
& n_ICONV_IGN_ILSEQ
) || err
!= n_ERR_ILSEQ
)
1325 if(icf
& n_ICONV_UNIREPL
){
1326 if(*outbleft
>= sizeof(n_unirepl
) -1){
1327 memcpy(*outb
, n_unirepl
, sizeof(n_unirepl
) -1);
1328 *outb
+= sizeof(n_unirepl
) -1;
1329 *outbleft
-= sizeof(n_unirepl
) -1;
1332 }else if(*outbleft
> 0){
1339 }else if(*outbleft
> 0){
1346 n_iconv_err_no
= err
;
1353 n_iconv_str(iconv_t cd
, enum n_iconv_flags icf
,
1354 struct str
*out
, struct str
const *in
, struct str
*in_rest_or_null
){
1355 struct n_string s
, *sp
= &s
;
1362 if(!n_string_get_can_book(il
) || !n_string_get_can_book(out
->l
)){
1368 sp
= n_string_creat(sp
);
1369 sp
= n_string_take_ownership(sp
, out
->s
, out
->l
, 0);
1375 if((nol
= ol
= sp
->s_len
) < il
)
1377 assert(sizeof(sp
->s_len
) == sizeof(ui32_t
));
1383 xnol
= (ui64_t
)(nol
<< 1) - (nol
>> 4);
1384 if(!n_string_can_book(sp
, xnol
)){
1386 if(!n_string_can_book(sp
, xnol
)){
1393 sp
= n_string_resize(sp
, nol
);
1395 ob
= ob_base
= &sp
->s_dat
[ol
];
1397 err
= n_iconv_buf(cd
, icf
, &ib
, &il
, &ob
, &nol
);
1399 sp
= n_string_trunc(sp
, ol
+ PTR2SIZE(ob
- ob_base
));
1400 if(err
== 0 || err
!= n_ERR_2BIG
)
1404 if(in_rest_or_null
!= NULL
){
1405 in_rest_or_null
->s
= n_UNCONST(ib
);
1406 in_rest_or_null
->l
= il
;
1410 out
->s
= n_string_cp(sp
);
1412 sp
= n_string_drop_ownership(sp
);
1413 /* n_string_gut(sp)*/
1420 n_iconv_onetime_cp(enum n_iconv_flags icf
,
1421 char const *tocode
, char const *fromcode
, char const *input
){
1429 tocode
= ok_vlook(ttycharset
);
1430 if(fromcode
== NULL
)
1433 if((icd
= iconv_open(tocode
, fromcode
)) == (iconv_t
)-1)
1436 in
.l
= strlen(in
.s
= n_UNCONST(input
)); /* logical */
1437 out
.s
= NULL
, out
.l
= 0;
1438 if(!n_iconv_str(icd
, icf
, &out
, &in
, NULL
))
1439 rv
= savestrbuf(out
.s
, out
.l
);
1448 #endif /* HAVE_ICONV */