2 * string.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.
44 #define xmalloc malloc
45 #define xrealloc realloc
46 #define xfree_null(p) if (!(p)) ; else free (p)
52 static const wchar_t w_line_delim[] = L"\r\n";
53 static const char line_delim[] = "\r\n";
54 static const unsigned int line_delim_len = 2;
56 static const wchar_t w_line_delim[] = L"\n";
57 static const char line_delim[] = "\n";
58 static const unsigned int line_delim_len = 2;
61 typedef struct string_t {
62 char *sz; /* standard null-terminated string */
63 unsigned int len; /* number of chars in the allocated buffer */
64 unsigned int used; /* number of used chars */
67 #ifdef STRING_MODULE_DEBUG
69 #define assert_valid_string(str) \
70 assert (((str) != NULL) \
71 && ((str)->sz != NULL) \
72 && ((str)->used + 1 <= (str)->len));
75 string_dump (struct string_t *str, FILE *out)
77 assert_valid_string (str);
80 fprintf (out, "string_dump: str->sz = %s (%p)\n", str->sz, str->sz);
81 fprintf (out, "string_dump: *(str->sz) = %d\n", *(str->sz));
82 fprintf (out, "string_dump: str->len = %u\n", str->len);
83 fprintf (out, "string_dump: str->used = %u\n", str->used);
86 #define DEBUG_PRINTF(x) printf x
88 #else /* not defined STRING_MODULE_DEBUG */
90 #define assert_valid_string(str) do {} while (0);
91 #define string_dump(str, out) do {} while (0);
92 #define DEBUG_PRINTF(x) do {} while (0);
98 string_init (struct string_t *s, unsigned int len)
102 /* no need to check that len > 0, since the len == 0 case is ok */
106 * for the moment we try to perform a reasonable allocation by rounding up
107 * the number of requested chars (including the trailing zero) to the
108 * closest multiple of 256, but we should probably find a better allocation
109 * policy or completely leave the optimization of memory allocation to malloc
111 to_alloc = ((len + 1 + 256) & (~0xFF));
113 s->sz = (char *) xmalloc (to_alloc * sizeof (char));
118 string_dump (s, stdout);
122 string_copy (struct string_t *dst, const void *src, unsigned int len)
124 assert_valid_string (dst);
125 assert (src != NULL);
127 /* no need to do anything */
128 if (len == 0) return;
130 if (dst->sz == NULL) {
131 string_init (dst, len);
134 strncpy (dst->sz, (const char *) src, len);
141 string_cat (struct string_t *dst, const void *src, unsigned int len)
143 assert_valid_string (dst);
144 assert (src != NULL);
146 /* no need to do anything */
147 if (len == 0) return;
149 if (dst->sz == NULL) {
150 string_init (dst, len);
153 strncpy (dst->sz + dst->used, (const char *) src, len);
154 dst->sz[dst->used + len] = '\0';
160 string_ready (struct string_t *str, unsigned int len)
162 assert_valid_string (str);
164 /* no need to do anything */
165 if (len == 0) return;
167 if (str->len - str->used < len)
169 DEBUG_PRINTF (("calling xrealloc"));
170 str->sz = xrealloc (str->sz, str->len + len);
177 string_destroy (struct string_t *str)
179 assert_valid_string (str);
181 xfree_null (str->sz);
182 memset (str, 0, sizeof (*str));
186 string_append_delim (struct string_t *dst)
188 assert_valid_string (dst);
189 string_cat (dst, line_delim, line_delim_len);
193 is_line_delim (const wchar_t *wsz)
195 assert (wsz != NULL);
197 if (*wsz == L'\r' && *(wsz + 1) == L'\n') {
199 } else if (*wsz == L'\r' || *wsz == L'\n') {
207 * DEST is the string to which the multibyte stuff will be added
208 * TO_ESC is the null wide char string to add
211 string_append_multibyte (struct string_t *dest, const wchar_t *wstr, unsigned int len, mbstate_t *state)
215 assert_valid_string (dest);
216 assert (wstr != NULL);
217 assert (state != NULL);
220 if (len == 0) return;
222 string_ready (dest, 4 * MB_CUR_MAX * (len + 1));
224 DEBUG_PRINTF (("string_append_multibyte: len = %u\n", len));
225 string_dump (dest, stdout);
227 for (i = 0; len > 0; ++i, --len) {
228 size_t copied = wcrtomb (dest->sz + dest->used, *(wstr + i), state);
230 DEBUG_PRINTF (("string_append_multibyte (loop): i = %d\n", i));
231 DEBUG_PRINTF (("string_append_multibyte (loop): copied = %u\n", copied));
232 string_dump (dest, stdout);
234 if (copied == (size_t)(-1)) {
238 dest->used += copied;
239 *(dest->sz + dest->used) = '\0';
241 DEBUG_PRINTF (("string_append_multibyte (loop): processed %s\n", dest->sz + dest->used - copied));
246 string_append_multibyte_newline (struct string_t *dest, mbstate_t *state)
248 assert_valid_string (dest);
249 string_append_multibyte(dest, w_line_delim, line_delim_len, state);
253 string_append_multibyte_terminator (struct string_t *dest, mbstate_t *state)
255 const wchar_t terminator = L'\0';
257 assert_valid_string (dest);
258 string_append_multibyte(dest, &terminator, 1, state);
262 * DEST is the string to which the escape code will be added
263 * TO_ESC is the (not necessarily null terminated) string to escape
264 * LEN is the length of the string to escape
267 do_escape (struct string_t *dest, const char *to_esc, unsigned int len, mbstate_t *state)
269 /* we only need to allocate 5 chars for byte:
270 * - one for the leading backslash
271 * - three for the octal representation
272 * - one for the trailing zero */
273 wchar_t buf[8] = L"";
276 assert_valid_string (dest);
277 assert (to_esc != NULL);
278 assert (state != NULL);
281 if (len == 0) return;
283 DEBUG_PRINTF (("do_escape: len = %d\n", len));
284 string_dump (dest, stdout);
286 for (i = 0; len > 0; ++i, --len) {
287 int j = (unsigned char)*(to_esc + i);
288 swprintf (buf, sizeof(buf), L"\\%03o", j);
289 DEBUG_PRINTF (("do_escape (loop): escaping \\%03o\n", j));
290 buf[sizeof(buf) - 1] = L'\0';
291 assert (wcslen(buf) == 4);
292 string_append_multibyte (dest, buf, 4, state);
297 string_escape (struct string_t *str)
301 mbstate_t state1, state2;
303 unsigned int to_read;
307 assert_valid_string (str);
309 memset (&state1, '\0', sizeof (state1));
310 memset (&state2, '\0', sizeof (state2));
313 to_read = src.used + 1;
315 /* this value is completely arbitrary */
316 string_init (str, 4 * to_read);
318 DEBUG_PRINTF (("string_escape: dumping string src"));
319 string_dump (&src, stdout);
320 DEBUG_PRINTF (("string_escape: dumping string str"));
321 string_dump (str, stdout);
324 while ((ret = mbrtowc (&c, src.sz + i, to_read, &state1)) != 0) {
325 DEBUG_PRINTF (("string_escape (loop): ret = %d\n", ret));
326 if (ret == (size_t)(-2)) {
327 DEBUG_PRINTF (("string_escape (loop): handling ret == -2"));
328 /* mauro: should we just return the portion of the string already
329 * processed and print an error message? */
332 } else if (ret == (size_t)(-1)) {
333 DEBUG_PRINTF (("string_escape (loop): handling ret == -1"));
334 do_escape (str, src.sz + i, 1, &state2);
336 } else if ((delim_size = is_line_delim(&c))) {
337 DEBUG_PRINTF (("string_escape (loop): handling ret == line_delim"));
339 string_append_multibyte_newline (str, &state2);
340 } else if (iswprint(c) || iswblank(c)) {
341 DEBUG_PRINTF (("string_escape (loop): handling ret == blank | printable"));
342 string_append_multibyte (str, &c, 1, &state2);
345 DEBUG_PRINTF (("string_escape (loop): handling ret == toescape"));
346 do_escape (str, src.sz + i, ret, &state2);
351 string_append_multibyte_terminator (str, &state2);
353 string_destroy (&src);
357 * BUF must be a null-terminated dynamically allocated string
358 * LEN is the size of the string BUF
361 escape_buffer (char **buf, size_t len)
365 assert (buf != NULL && *buf != NULL);
368 if (len == 0) return;
370 DEBUG_PRINTF (("escape_buffer processing: %s (len %u)\n", *buf, len));
385 const size_t buflen = 512;
386 buf = (char *) xmalloc(buflen);
387 assert (buf != NULL);
389 puts ("--------------------------------------------------------------------------------");
391 while (fgets (buf, buflen - 1, stdin) != NULL)
393 /* just in case... */
394 buf[buflen - 1] = '\0';
395 printf ("before escape: %s", buf);
396 escape_buffer (&buf, strlen(buf));
397 printf ("after escape: %s", buf);
400 puts ("--------------------------------------------------------------------------------");