2 * string_t.c - dynamic string handling module
4 * Copyright (C) 2005 Free Software Foundation, Inc.
6 * This file is part of GNU Wget.
8 * GNU Wget is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * GNU Wget is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * In addition, as a special exception, the Free Software Foundation
22 * gives permission to link the code of its release of Wget with the
23 * OpenSSL project's "OpenSSL" library (or with modified versions of it
24 * that use the same license as the "OpenSSL" library), and distribute
25 * the linked executables. You must obey the GNU General Public License
26 * in all respects for all of the code used other than "OpenSSL". If you
27 * modify this file, you may extend this exception to your version of the
28 * file, but you are not obligated to do so. If you do not wish to do
29 * so, delete this exception statement from your version.
34 #define _GNU_SOURCE /* to get iswblank */
49 #define xmalloc malloc
50 #define xrealloc realloc
51 #define xfree_null(p) if (!(p)) ; else free (p)
57 static const wchar_t w_line_delim[] = L"\r\n";
58 static const char line_delim[] = "\r\n";
59 static const unsigned int line_delim_len = 2;
61 static const wchar_t w_line_delim[] = L"\n";
62 static const char line_delim[] = "\n";
63 static const unsigned int line_delim_len = 2;
66 typedef struct string_t {
67 char *sz; /* standard null-terminated string */
68 unsigned int len; /* number of chars in the allocated buffer */
69 unsigned int used; /* number of used chars */
72 #ifdef STRING_MODULE_DEBUG
74 #define assert_valid_string(str) \
75 assert (((str) != NULL) \
76 && ((str)->sz != NULL) \
77 && ((str)->used + 1 <= (str)->len));
80 string_dump (struct string_t *str, FILE *out)
82 assert_valid_string (str);
85 fprintf (out, "string_dump: str->sz = %s (%p)\n", str->sz, str->sz);
86 fprintf (out, "string_dump: *(str->sz) = %d\n", *(str->sz));
87 fprintf (out, "string_dump: str->len = %u\n", str->len);
88 fprintf (out, "string_dump: str->used = %u\n", str->used);
91 #define DEBUG_PRINTF(x) printf x
93 #else /* not defined STRING_MODULE_DEBUG */
95 #define assert_valid_string(str) do {} while (0);
96 #define string_dump(str, out) do {} while (0);
97 #define DEBUG_PRINTF(x) do {} while (0);
103 string_init (struct string_t *s, unsigned int len)
107 /* no need to check that len > 0, since the len == 0 case is ok */
111 * for the moment we try to perform a reasonable allocation by rounding up
112 * the number of requested chars (including the trailing zero) to the
113 * closest multiple of 256, but we should probably find a better allocation
114 * policy or completely leave the optimization of memory allocation to malloc
116 to_alloc = ((len + 1 + 256) & (~0xFF));
118 s->sz = (char *) xmalloc (to_alloc * sizeof (char));
123 string_dump (s, stdout);
127 string_copy (struct string_t *dst, const void *src, unsigned int len)
129 assert_valid_string (dst);
130 assert (src != NULL);
132 /* no need to do anything */
133 if (len == 0) return;
135 if (dst->sz == NULL) {
136 string_init (dst, len);
139 strncpy (dst->sz, (const char *) src, len);
146 string_cat (struct string_t *dst, const void *src, unsigned int len)
148 assert_valid_string (dst);
149 assert (src != NULL);
151 /* no need to do anything */
152 if (len == 0) return;
154 if (dst->sz == NULL) {
155 string_init (dst, len);
158 strncpy (dst->sz + dst->used, (const char *) src, len);
159 dst->sz[dst->used + len] = '\0';
165 string_ready (struct string_t *str, unsigned int len)
167 assert_valid_string (str);
169 /* no need to do anything */
170 if (len == 0) return;
172 if (str->len - str->used < len)
174 DEBUG_PRINTF (("calling xrealloc"));
175 str->sz = xrealloc (str->sz, str->len + len);
182 string_destroy (struct string_t *str)
184 assert_valid_string (str);
186 xfree_null (str->sz);
187 memset (str, 0, sizeof (*str));
192 string_append_delim (struct string_t *dst)
194 assert_valid_string (dst);
195 string_cat (dst, line_delim, line_delim_len);
200 is_line_delim (const wchar_t *wsz)
202 assert (wsz != NULL);
204 if (*wsz == L'\r' && *(wsz + 1) == L'\n') {
206 } else if (*wsz == L'\r' || *wsz == L'\n') {
214 * DEST is the string to which the multibyte stuff will be added
215 * TO_ESC is the null wide char string to add
218 string_append_multibyte (struct string_t *dest, const wchar_t *wstr, unsigned int len, mbstate_t *state)
222 assert_valid_string (dest);
223 assert (wstr != NULL);
224 assert (state != NULL);
227 if (len == 0) return;
229 string_ready (dest, 4 * MB_CUR_MAX * (len + 1));
231 DEBUG_PRINTF (("string_append_multibyte: len = %u\n", len));
232 string_dump (dest, stdout);
234 for (i = 0; len > 0; ++i, --len) {
235 size_t copied = wcrtomb (dest->sz + dest->used, *(wstr + i), state);
237 DEBUG_PRINTF (("string_append_multibyte (loop): i = %d\n", i));
238 DEBUG_PRINTF (("string_append_multibyte (loop): copied = %u\n", copied));
239 string_dump (dest, stdout);
241 if (copied == (size_t)(-1)) {
245 dest->used += copied;
246 *(dest->sz + dest->used) = '\0';
248 DEBUG_PRINTF (("string_append_multibyte (loop): processed %s\n", dest->sz + dest->used - copied));
253 string_append_multibyte_newline (struct string_t *dest, mbstate_t *state)
255 assert_valid_string (dest);
256 string_append_multibyte(dest, w_line_delim, line_delim_len, state);
260 string_append_multibyte_terminator (struct string_t *dest, mbstate_t *state)
262 const wchar_t terminator = L'\0';
264 assert_valid_string (dest);
265 string_append_multibyte(dest, &terminator, 1, state);
269 * DEST is the string to which the escape code will be added
270 * TO_ESC is the (not necessarily null terminated) string to escape
271 * LEN is the length of the string to escape
274 do_escape (struct string_t *dest, const char *to_esc, unsigned int len, mbstate_t *state)
276 /* we only need to allocate 5 chars for byte:
277 * - one for the leading backslash
278 * - three for the octal representation
279 * - one for the trailing zero */
280 wchar_t buf[8] = L"";
281 size_t buf_elems = sizeof(buf)/sizeof(buf[0]);
284 assert_valid_string (dest);
285 assert (to_esc != NULL);
286 assert (state != NULL);
289 if (len == 0) return;
291 DEBUG_PRINTF (("do_escape: len = %d\n", len));
292 string_dump (dest, stdout);
294 for (i = 0; len > 0; ++i, --len) {
295 int j = (unsigned char)*(to_esc + i);
296 int cc = swprintf (buf, buf_elems, L"\\%03o", j);
297 assert(cc > 0 && cc < buf_elems);
298 DEBUG_PRINTF (("do_escape (loop): escaping \\%03o\n", j));
299 buf[buf_elems - 1] = L'\0';
300 assert (wcslen(buf) == 4);
301 string_append_multibyte (dest, buf, 4, state);
306 string_escape (struct string_t *str)
310 mbstate_t state1, state2;
312 unsigned int to_read;
316 assert_valid_string (str);
318 memset (&state1, '\0', sizeof (state1));
319 memset (&state2, '\0', sizeof (state2));
322 to_read = src.used + 1;
324 /* this value is completely arbitrary */
325 string_init (str, 4 * to_read);
327 DEBUG_PRINTF (("string_escape: dumping string src"));
328 string_dump (&src, stdout);
329 DEBUG_PRINTF (("string_escape: dumping string str"));
330 string_dump (str, stdout);
333 while ((ret = mbrtowc (&c, src.sz + i, to_read, &state1)) != 0) {
334 DEBUG_PRINTF (("string_escape (loop): ret = %d\n", ret));
335 if (ret == (size_t)(-2)) {
336 DEBUG_PRINTF (("string_escape (loop): handling ret == -2"));
337 /* mauro: should we just return the portion of the string already
338 * processed and print an error message? */
341 } else if (ret == (size_t)(-1)) {
342 DEBUG_PRINTF (("string_escape (loop): handling ret == -1"));
343 do_escape (str, src.sz + i, 1, &state2);
345 } else if ((delim_size = is_line_delim(&c))) {
346 DEBUG_PRINTF (("string_escape (loop): handling ret == line_delim"));
348 string_append_multibyte_newline (str, &state2);
349 } else if (iswprint(c) || iswblank(c)) {
350 DEBUG_PRINTF (("string_escape (loop): handling ret == blank | printable"));
351 string_append_multibyte (str, &c, 1, &state2);
353 /* since the backslash character is used to escape unprintable data,
354 * in order to avoid ambiguities in the escaped string we have to
355 * escape backslashes as well */
356 string_append_multibyte (str, &c, 1, &state2);
360 DEBUG_PRINTF (("string_escape (loop): handling ret == toescape"));
361 do_escape (str, src.sz + i, ret, &state2);
366 string_append_multibyte_terminator (str, &state2);
368 string_destroy (&src);
372 * BUF must be a null-terminated dynamically allocated string
373 * LEN is the size of the string BUF
376 escape_buffer (char **buf, size_t len)
380 assert (buf != NULL && *buf != NULL);
383 if (len == 0) return;
385 DEBUG_PRINTF (("escape_buffer processing: %s (len %u)\n", *buf, len));
400 const size_t buflen = 512;
401 buf = (char *) xmalloc(buflen);
402 assert (buf != NULL);
404 puts ("--------------------------------------------------------------------------------");
406 while (fgets (buf, buflen - 1, stdin) != NULL)
408 /* just in case... */
409 buf[buflen - 1] = '\0';
410 printf ("before escape: %s", buf);
411 escape_buffer (&buf, strlen(buf));
412 printf ("after escape: %s", buf);
415 puts ("--------------------------------------------------------------------------------");