1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Auto-reclaimed string allocation and support routines that build on top of
3 *@ them. Strings handed out by those are reclaimed at the top of the command
4 *@ loop each time, so they need not be freed.
5 *@ And below this series we do collect all other plain string support routines
6 *@ in here, including those which use normal heap memory.
8 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
9 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
12 * Copyright (c) 1980, 1993
13 * The Regents of the University of California. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #ifndef HAVE_AMALGAMATION
51 * Allocate SBUFFER_SIZE chunks and keep them in a singly linked list, but
52 * release all except the first two in sreset(), because other allocations are
53 * performed and the underlaying allocator should have the possibility to
54 * reorder stuff and possibly even madvise(2), so that S-nail(1) integrates
55 * neatly into the system.
56 * To relax stuff further, especially in non-interactive, i.e., send mode, do
57 * not even allocate the first buffer, but let that be a builtin DATA section
58 * one that is rather small, yet sufficient for send mode to *never* even
59 * perform a single dynamic allocation (from our stringdope point of view).
67 #define SALIGN (sizeof(union __align__) - 1)
69 CTA(ISPOW2(SALIGN
+ 1));
73 char *_bot
; /* For spreserve() */
74 char *_relax
; /* If !NULL, used by srelax() instead of ._bot */
75 char *_max
; /* Max usable byte */
76 char *_caster
; /* NULL if full */
79 /* Single instance builtin buffer, DATA */
82 char b_buf
[SBUFFER_BUILTIN
- sizeof(struct b_base
)];
84 #define SBLTIN_SIZE SIZEOF_FIELD(struct b_bltin, b_buf)
86 /* Dynamically allocated buffers */
89 char b_buf
[SBUFFER_SIZE
- sizeof(struct b_base
)];
91 #define SDYN_SIZE SIZEOF_FIELD(struct b_dyn, b_buf)
95 char b_buf
[VFIELD_SIZE(SALIGN
+ 1)];
98 static struct b_bltin _builtin_buf
;
99 static struct buffer
*_buf_head
, *_buf_list
, *_buf_server
, *_buf_relax
;
102 size_t _all_cnt
, _all_cycnt
, _all_cycnt_max
,
103 _all_size
, _all_cysize
, _all_cysize_max
, _all_min
, _all_max
, _all_wast
,
104 _all_bufcnt
, _all_cybufcnt
, _all_cybufcnt_max
,
105 _all_resetreqs
, _all_resets
;
112 size_t orig_size
= size
;
114 union {struct buffer
*b
; char *cp
;} u
;
125 _all_cycnt_max
= MAX(_all_cycnt_max
, _all_cycnt
);
128 _all_cysize_max
= MAX(_all_cysize_max
, _all_cysize
);
129 _all_min
= (_all_max
== 0) ? size
: MIN(_all_min
, size
);
130 _all_max
= MAX(_all_max
, size
);
131 _all_wast
+= size
- orig_size
;
134 if ((u
.b
= _buf_server
) != NULL
)
137 for (u
.b
= _buf_head
; u
.b
!= NULL
; u
.b
= u
.b
->b
._next
) {
141 if (u
.b
== _buf_server
) {
142 if (u
.b
== _buf_head
&& (u
.b
= _buf_head
->b
._next
) != NULL
) {
154 /* Alignment is the one thing, the other is what is
155 * usually allocated, and here about 40 bytes seems to
156 * be a good cut to avoid non-usable non-NULL casters */
157 u
.b
->b
._caster
= PTRCMP(y
+ 42 + 16, >=, z
) ? NULL
: y
;
163 if (_buf_head
== NULL
) {
164 struct b_bltin
*b
= &_builtin_buf
;
165 b
->b_base
._max
= b
->b_buf
+ sizeof(b
->b_buf
) - 1;
166 _buf_head
= (struct buffer
*)b
;
172 _all_cybufcnt_max
= MAX(_all_cybufcnt_max
, _all_cybufcnt
);
174 u
.b
= smalloc(sizeof(struct b_dyn
));
175 u
.b
->b
._max
= u
.b
->b_buf
+ SDYN_SIZE
- 1;
177 if (_buf_list
!= NULL
)
178 _buf_list
->b
._next
= u
.b
;
179 _buf_server
= _buf_list
= u
.b
;
181 u
.b
->b
._caster
= (u
.b
->b
._bot
= u
.b
->b_buf
) + size
;
182 u
.b
->b
._relax
= NULL
;
189 csalloc(size_t nmemb
, size_t size
)
200 sreset(bool_t only_if_relaxed
)
207 if (noreset
|| (only_if_relaxed
&& _buf_relax
== NULL
))
211 _all_cycnt
= _all_cysize
= 0;
212 _all_cybufcnt
= (_buf_head
!= NULL
&& _buf_head
->b
._next
!= NULL
);
216 if ((bh
= _buf_head
) != NULL
) {
217 struct buffer
*b
= bh
;
218 b
->b
._caster
= b
->b
._bot
;
221 memset(b
->b
._caster
, 0377, PTR2SIZE(b
->b
._max
- b
->b
._caster
));
224 if ((bh
= bh
->b
._next
) != NULL
) {
226 b
->b
._caster
= b
->b
._bot
;
229 memset(b
->b
._caster
, 0377, PTR2SIZE(b
->b
._max
- b
->b
._caster
));
231 for (bh
= bh
->b
._next
; bh
!= NULL
;) {
232 struct buffer
*b2
= bh
->b
._next
;
254 for (b
= _buf_head
; b
!= NULL
; b
= b
->b
._next
)
255 b
->b
._bot
= b
->b
._caster
;
263 assert(_buf_relax
== NULL
);
265 for (b
= _buf_head
; b
!= NULL
; b
= b
->b
._next
)
266 b
->b
._relax
= b
->b
._caster
;
267 _buf_relax
= _buf_server
;
268 assert(_buf_relax
!= NULL
);
276 assert(_buf_relax
!= NULL
);
278 for (b
= _buf_relax
; b
!= NULL
; b
= b
->b
._next
) {
279 b
->b
._caster
= (b
->b
._relax
!= NULL
) ? b
->b
._relax
: b
->b
._bot
;
288 /* The purpose of relaxation is only that it is possible to reset the
289 * casters, *not* to give back memory to the system. We are presumably in
290 * an iteration over all messages of a mailbox, and it'd be quite
291 * counterproductive to give the system allocator a chance to waste time */
294 assert(_buf_relax
!= NULL
);
296 for (b
= _buf_relax
; b
!= NULL
; b
= b
->b
._next
) {
297 b
->b
._caster
= (b
->b
._relax
!= NULL
) ? b
->b
._relax
: b
->b
._bot
;
299 memset(b
->b
._caster
, 0377, PTR2SIZE(b
->b
._max
- b
->b
._caster
));
311 excess
= (_all_cybufcnt_max
* SDYN_SIZE
) + SBLTIN_SIZE
;
312 excess
= (excess
>= _all_cysize_max
) ? 0 : _all_cysize_max
- excess
;
314 printf("String usage statistics (cycle means one sreset() cycle):\n"
315 " Buffer allocs ever/max simultan. : %lu/%lu\n"
316 " Buffer size of builtin(1)/dynamic: %lu/%lu\n"
317 " Overall alloc count/bytes : %lu/%lu\n"
318 " Alloc bytes min/max/align wastage: %lu/%lu/%lu\n"
319 " sreset() cycles : %lu (%lu performed)\n"
320 " Cycle maximums: alloc count/bytes: %lu/%lu+%lu\n",
321 (ul_it
)_all_bufcnt
, (ul_it
)_all_cybufcnt_max
,
322 (ul_it
)SBLTIN_SIZE
, (ul_it
)SDYN_SIZE
,
323 (ul_it
)_all_cnt
, (ul_it
)_all_size
,
324 (ul_it
)_all_min
, (ul_it
)_all_max
, (ul_it
)_all_wast
,
325 (ul_it
)_all_resetreqs
, (ul_it
)_all_resets
,
326 (ul_it
)_all_cycnt_max
, (ul_it
)_all_cysize_max
, (ul_it
)excess
);
332 * Return a pointer to a dynamic copy of the argument.
335 savestr(const char *str
)
337 size_t size
= strlen(str
) + 1;
338 char *news
= salloc(size
);
339 memcpy(news
, str
, size
);
344 * Return new string copy of a non-terminated argument.
347 savestrbuf(const char *sbuf
, size_t sbuf_len
)
349 char *news
= salloc(sbuf_len
+ 1);
350 memcpy(news
, sbuf
, sbuf_len
);
356 * Make a copy of new argument incorporating old one.
359 save2str(const char *str
, const char *old
)
361 size_t newsize
= strlen(str
) + 1, oldsize
= old
? strlen(old
) + 1 : 0;
362 char *news
= salloc(newsize
+ oldsize
);
364 memcpy(news
, old
, oldsize
);
365 news
[oldsize
- 1] = ' ';
367 memcpy(news
+ oldsize
, str
, newsize
);
372 savecat(char const *s1
, char const *s2
)
374 size_t l1
= strlen(s1
), l2
= strlen(s2
);
375 char *news
= salloc(l1
+ l2
+ 1);
376 memcpy(news
+ 0, s1
, l1
);
377 memcpy(news
+ l1
, s2
, l2
);
378 news
[l1
+ l2
] = '\0';
383 * Support routines, auto-reclaimed storage
387 i_strdup(char const *src
)
392 sz
= strlen(src
) + 1;
394 i_strcpy(dest
, src
, sz
);
399 protbase(char const *cp
)
401 char *n
= salloc(strlen(cp
) + 1), *np
= n
;
403 /* Just ignore the `is-system-mailbox' prefix XXX */
404 if (cp
[0] == '%' && cp
[1] == ':')
408 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
412 } else if (cp
[0] == '/')
422 urlxenc(char const *cp
) /* XXX */
426 np
= n
= salloc(strlen(cp
) * 3 + 1);
428 if (alnumchar(*cp
) || *cp
== '_' || *cp
== '@' ||
429 (np
> n
&& (*cp
== '.' || *cp
== '-' ||
434 *np
++ = Hexchar((*cp
&0xf0) >> 4);
435 *np
++ = Hexchar(*cp
&0x0f);
444 urlxdec(char const *cp
) /* XXX */
448 np
= n
= salloc(strlen(cp
) + 1);
450 if (cp
[0] == '%' && cp
[1] && cp
[2]) {
451 *np
= (int)(cp
[1]>'9'?cp
[1]-'A'+10:cp
[1]-'0') << 4;
452 *np
++ |= cp
[2]>'9'?cp
[2]-'A'+10:cp
[2]-'0';
462 str_concat_csvl(struct str
*self
, ...) /* XXX onepass maybe better here */
469 for (l
= 0; (cs
= va_arg(vl
, char const*)) != NULL
;)
474 self
->s
= salloc(l
+ 1);
477 for (l
= 0; (cs
= va_arg(vl
, char const*)) != NULL
;) {
478 size_t i
= strlen(cs
);
479 memcpy(self
->s
+ l
, cs
, i
);
488 str_concat_cpa(struct str
*self
, char const *const*cpa
, char const *sep_o_null
)
491 char const *const*xcpa
;
493 sonl
= (sep_o_null
!= NULL
) ? strlen(sep_o_null
) : 0;
495 for (l
= 0, xcpa
= cpa
; *xcpa
!= NULL
; ++xcpa
)
496 l
+= strlen(*xcpa
) + sonl
;
499 self
->s
= salloc(l
+ 1);
501 for (l
= 0, xcpa
= cpa
; *xcpa
!= NULL
; ++xcpa
) {
502 size_t i
= strlen(*xcpa
);
503 memcpy(self
->s
+ l
, *xcpa
, i
);
506 memcpy(self
->s
+ l
, sep_o_null
, sonl
);
515 * Routines that are not related to auto-reclaimed storage follow.
518 uc_it
const class_char
[] = {
519 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
520 C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,
521 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
522 C_CNTRL
,C_BLANK
,C_WHITE
,C_SPACE
,C_SPACE
,C_SPACE
,C_CNTRL
,C_CNTRL
,
523 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
524 C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,
525 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
526 C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,C_CNTRL
,
527 /* 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' */
528 C_BLANK
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,
529 /* 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / */
530 C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,
531 /* 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 */
532 C_OCTAL
,C_OCTAL
,C_OCTAL
,C_OCTAL
,C_OCTAL
,C_OCTAL
,C_OCTAL
,C_OCTAL
,
533 /* 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? */
534 C_DIGIT
,C_DIGIT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,
535 /* 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G */
536 C_PUNCT
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,
537 /* 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O */
538 C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,
539 /* 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W */
540 C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,C_UPPER
,
541 /* 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ */
542 C_UPPER
,C_UPPER
,C_UPPER
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,
543 /* 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g */
544 C_PUNCT
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,
545 /* 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o */
546 C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,
547 /* 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w */
548 C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,C_LOWER
,
549 /* 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del */
550 C_LOWER
,C_LOWER
,C_LOWER
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_PUNCT
,C_CNTRL
554 anyof(char const *s1
, char const *s2
)
556 for (; *s1
!= '\0'; ++s1
)
559 return (*s1
!= '\0');
563 strhash(char const *name
)
567 while (*name
!= '\0') {
575 strcomma(char **iolist
, int ignore_empty
)
579 for (base
= *iolist
; base
!= NULL
; base
= *iolist
) {
580 while (*base
!= '\0' && blankspacechar(*base
))
582 cp
= strchr(base
, ',');
587 cp
= base
+ strlen(base
);
589 while (cp
> base
&& blankspacechar(cp
[-1]))
592 if (*base
!= '\0' || ! ignore_empty
)
599 i_strcpy(char *dest
, const char *src
, size_t size
)
602 for (;; ++dest
, ++src
)
603 if ((*dest
= lowerconv(*src
)) == '\0') {
605 } else if (--size
== 0) {
613 is_prefix(char const *as1
, char const *as2
)
616 for (; (c
= *as1
) == *as2
&& c
!= '\0'; ++as1
, ++as2
)
623 last_at_before_slash(char const *sp
)
628 for (cp
= sp
; (c
= *cp
) != '\0'; ++cp
)
631 while (cp
> sp
&& *--cp
!= '@')
633 return (*cp
== '@' ? cp
: NULL
);
637 laststring(char *linebuf
, bool_t
*needs_list
, bool_t strip
)
639 char *cp
, *p
, quoted
;
641 /* Anything to do at all? */
642 if (*(cp
= linebuf
) == '\0')
644 cp
+= strlen(linebuf
) - 1;
646 /* Strip away trailing blanks */
647 while (whitechar(*cp
) && PTRCMP(cp
, >, linebuf
))
653 /* Now search for the BOS of the "last string" */
655 if (quoted
== '\'' || quoted
== '"') {
661 while (PTRCMP(cp
, >, linebuf
)) {
666 } else if (!whitechar(*cp
))
668 if (PTRCMP(cp
, ==, linebuf
) || cp
[-1] != '\\') {
669 /* When in whitespace mode, WS prefix doesn't belong */
674 /* Expand the escaped quote character */
675 for (p
= --cp
; (p
[0] = p
[1]) != '\0'; ++p
)
678 if (strip
&& quoted
!= ' ' && *cp
== quoted
)
679 for (p
= cp
; (p
[0] = p
[1]) != '\0'; ++p
)
682 /* The "last string" has been skipped over, but still, try to step backwards
683 * until we are at BOS or see whitespace, so as to make possible things like
684 * "? copy +'x y.mbox'" or even "? copy +x\ y.mbox" */
685 while (PTRCMP(cp
, >, linebuf
)) {
687 if (whitechar(*cp
)) {
690 /* We can furtherly release our callees if we now decide wether the
691 * remaining non-"last string" line content contains non-WS */
692 while (PTRCMP(--p
, >=, linebuf
))
701 if (cp
!= NULL
&& *cp
== '\0')
703 *needs_list
= (cp
!= linebuf
&& *linebuf
!= '\0');
713 makelow(char *cp
) /* TODO isn't that crap? --> */
715 #ifdef HAVE_C90AMEND1
716 if (mb_cur_max
> 1) {
722 len
= mbtowc(&wc
, cp
, mb_cur_max
);
727 if (wctomb(tp
, wc
) == len
)
728 tp
+= len
, cp
+= len
;
730 *tp
++ = *cp
++; /* <-- at least here */
737 *cp
= tolower((uc_it
)*cp
);
743 substr(char const *str
, char const *sub
)
745 char const *cp
, *backup
;
749 while (*str
&& *cp
) {
750 #ifdef HAVE_C90AMEND1
751 if (mb_cur_max
> 1) {
755 if ((sz
= mbtowc(&c
, cp
, mb_cur_max
)) < 0)
758 if ((sz
= mbtowc(&c2
, str
, mb_cur_max
)) < 0)
764 if ((sz
= mbtowc(&c
, backup
, mb_cur_max
)) > 0) {
776 #ifdef HAVE_C90AMEND1
794 #ifndef HAVE_SNPRINTF
796 snprintf(char *str
, size_t size
, const char *format
, ...) /* XXX DANGER! */
801 va_start(ap
, format
);
802 ret
= vsprintf(str
, format
, ap
);
811 sstpcpy(char *dst
, char const *src
)
813 while ((*dst
= *src
++) != '\0')
819 (sstrdup
)(char const *cp SMALLOC_DEBUG_ARGS
)
824 size_t l
= strlen(cp
) + 1;
825 dp
= (smalloc
)(l SMALLOC_DEBUG_ARGSCALL
);
833 (sbufdup
)(char const *cp
, size_t len SMALLOC_DEBUG_ARGS
)
837 dp
= (smalloc
)(len
+ 1 SMALLOC_DEBUG_ARGSCALL
);
846 n_strlcpy(char *dst
, const char *src
, size_t len
)
850 dst
= strncpy(dst
, src
, len
);
856 asccasecmp(char const *s1
, char const *s2
)
861 char c1
= *s1
++, c2
= *s2
++;
862 if ((cmp
= lowerconv(c1
) - lowerconv(c2
)) != 0 || c1
== '\0')
869 ascncasecmp(char const *s1
, char const *s2
, size_t sz
)
874 char c1
= *s1
++, c2
= *s2
++;
875 cmp
= (ui8_t
)lowerconv(c1
);
876 cmp
-= (ui8_t
)lowerconv(c2
);
877 if (cmp
!= 0 || c1
== '\0')
884 asccasestr(char const *haystack
, char const *xneedle
)
886 char *needle
= NULL
, *NEEDLE
;
889 sz
= strlen(xneedle
);
893 needle
= ac_alloc(sz
);
894 NEEDLE
= ac_alloc(sz
);
895 for (i
= 0; i
< sz
; i
++) {
896 needle
[i
] = lowerconv(xneedle
[i
]);
897 NEEDLE
[i
] = upperconv(xneedle
[i
]);
901 if (*haystack
== *needle
|| *haystack
== *NEEDLE
) {
902 for (i
= 1; i
< sz
; i
++)
903 if (haystack
[i
] != needle
[i
] &&
904 haystack
[i
] != NEEDLE
[i
])
913 if (needle
!= NULL
) {
921 is_asccaseprefix(char const *as1
, char const *as2
)
925 for (;; ++as1
, ++as2
) {
926 char c1
= lowerconv(*as1
), c2
= lowerconv(*as2
);
927 if ((rv
= (c1
== '\0')))
929 if (c1
!= c2
|| c2
== '\0')
936 (n_str_dup
)(struct str
*self
, struct str
const *t SMALLOC_DEBUG_ARGS
)
938 if (t
!= NULL
&& t
->l
> 0) {
940 self
->s
= (srealloc
)(self
->s
, t
->l
+ 1 SMALLOC_DEBUG_ARGSCALL
);
941 memcpy(self
->s
, t
->s
, t
->l
);
948 (n_str_add_buf
)(struct str
*self
, char const *buf
, size_t buflen
953 self
->l
= sl
+ buflen
;
954 self
->s
= (srealloc
)(self
->s
, self
->l
+1 SMALLOC_DEBUG_ARGSCALL
);
955 memcpy(self
->s
+ sl
, buf
, buflen
);
961 static void _ic_toupper(char *dest
, char const *src
);
962 static void _ic_stripdash(char *p
);
965 _ic_toupper(char *dest
, const char *src
)
968 *dest
++ = upperconv(*src
);
973 _ic_stripdash(char *p
)
984 n_iconv_open(char const *tocode
, char const *fromcode
)
989 if ((id
= iconv_open(tocode
, fromcode
)) != (iconv_t
)-1)
993 * Remove the "iso-" prefixes for Solaris.
995 if (ascncasecmp(tocode
, "iso-", 4) == 0)
997 else if (ascncasecmp(tocode
, "iso", 3) == 0)
999 if (ascncasecmp(fromcode
, "iso-", 4) == 0)
1001 else if (ascncasecmp(fromcode
, "iso", 3) == 0)
1003 if (*tocode
== '\0' || *fromcode
== '\0')
1004 return (iconv_t
) -1;
1005 if ((id
= iconv_open(tocode
, fromcode
)) != (iconv_t
)-1)
1008 * Solaris prefers upper-case charset names. Don't ask...
1010 t
= salloc(strlen(tocode
) + 1);
1011 _ic_toupper(t
, tocode
);
1012 f
= salloc(strlen(fromcode
) + 1);
1013 _ic_toupper(f
, fromcode
);
1014 if ((id
= iconv_open(t
, f
)) != (iconv_t
)-1)
1017 * Strip dashes for UnixWare.
1021 if ((id
= iconv_open(t
, f
)) != (iconv_t
)-1)
1024 * Add your vendor's sillynesses here.
1028 * If the encoding names are equal at this point, they
1029 * are just not understood by iconv(), and we cannot
1030 * sensibly use it in any way. We do not perform this
1031 * as an optimization above since iconv() can otherwise
1032 * be used to check the validity of the input even with
1033 * identical encoding names.
1035 if (strcmp(t
, f
) == 0)
1041 n_iconv_close(iconv_t cd
)
1045 iconvd
= (iconv_t
)-1;
1050 n_iconv_reset(iconv_t cd
)
1052 (void)iconv(cd
, NULL
, NULL
, NULL
, NULL
);
1057 * (2012-09-24: export and use it exclusively to isolate prototype problems
1058 * (*inb* is 'char const **' except in POSIX) in a single place.
1059 * GNU libiconv even allows for configuration time const/non-const..
1060 * In the end it's an ugly guess, but we can't do better since make(1) doesn't
1061 * support compiler invocations which bail on error, so no -Werror.
1063 /* Citrus project? */
1064 # if defined _ICONV_H_ && defined __ICONV_F_HIDE_INVALID
1065 /* DragonFly 3.2.1 is special */
1066 # ifdef __DragonFly__
1067 # define __INBCAST(S) (char ** __restrict__)UNCONST(S)
1069 # define __INBCAST(S) (char const **)UNCONST(S)
1073 # define __INBCAST(S) (char **)UNCONST(S)
1077 n_iconv_buf(iconv_t cd
, char const **inb
, size_t *inbleft
,/*XXX redo iconv use*/
1078 char **outb
, size_t *outbleft
, bool_t skipilseq
)
1083 size_t sz
= iconv(cd
, __INBCAST(inb
), inbleft
, outb
, outbleft
);
1084 if (sz
!= (size_t)-1)
1087 if (! skipilseq
|| err
!= EILSEQ
)
1092 } else if (*outbleft
> 0) {
1096 if (*outbleft
> 0/* TODO 0xFFFD 2*/) {
1097 /* TODO 0xFFFD (*outb)[0] = '[';
1098 * TODO (*outb)[1] = '?';
1099 * TODO 0xFFFD (*outb)[2] = ']';
1100 * TODO (*outb) += 3;
1101 * TODO (*outbleft) -= 3; */
1115 n_iconv_str(iconv_t cd
, struct str
*out
, struct str
const *in
,
1116 struct str
*in_rest_or_null
, bool_t skipilseq
)
1119 char *obb
= out
->s
, *ob
;
1121 size_t olb
= out
->l
, ol
, il
;
1124 ol
= (ol
<< 1) - (ol
>> 4);
1135 err
= n_iconv_buf(cd
, &ib
, &il
, &ob
, &ol
, skipilseq
);
1136 if (err
== 0 || err
!= E2BIG
)
1140 jrealloc
: obb
= srealloc(obb
, olb
);
1143 if (in_rest_or_null
!= NULL
) {
1144 in_rest_or_null
->s
= UNCONST(ib
);
1145 in_rest_or_null
->l
= il
;
1151 #endif /* HAVE_ICONV */
1153 /* vim:set fenc=utf-8:s-it-mode (TODO only partial true) */