comparison src/util.c @ 8976:6f21aa413b18

[gaim-migrate @ 9750] "This patch fixes two relatively minor problems for the new gaim_mime_decode_field code: 1. Use g_return_val_if_fail to check for NULL. (Problem discovered by shx) 2. Added code to handle the boundary case =?charset?encoding??= (i.e., no encoded-text) Some comments also have been changed/added for clarification." --Ambrose C. LI committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Wed, 19 May 2004 04:02:08 +0000
parents 60a47725df97
children 38d022e5eb19
comparison
equal deleted inserted replaced
8975:574393ccd705 8976:6f21aa413b18
315 encoded_word_state_t state = state_start; 315 encoded_word_state_t state = state_start;
316 const char *cur, *mark; 316 const char *cur, *mark;
317 const char *charset0 = NULL, *encoding0 = NULL, *encoded_text0 = NULL; 317 const char *charset0 = NULL, *encoding0 = NULL, *encoded_text0 = NULL;
318 char *n, *new; 318 char *n, *new;
319 319
320 /* token can be any CHAR, not necessarily ASCII */ 320 /* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */
321 #define token_char_p(c) \ 321 #define token_char_p(c) \
322 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c)) 322 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c))
323 323
324 /* But encoded-text must be ASCII; alas, isascii() may not exist */ 324 /* But encoded-text must be ASCII; alas, isascii() may not exist */
325 #define encoded_text_char_p(c) \ 325 #define encoded_text_char_p(c) \
326 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c)) 326 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c))
327 327
328 #define RECOVER_MARKED_TEXT strncpy(n, mark, cur - mark + 1); \ 328 #define RECOVER_MARKED_TEXT strncpy(n, mark, cur - mark + 1); \
329 n += cur - mark + 1 329 n += cur - mark + 1
330 330
331 /* NOTE: Assuming that we need just strlen(str)+1 may be wrong */ 331 g_return_val_if_fail(str != NULL, NULL);
332
333 /* NOTE: Assuming that we need just strlen(str)+1 *may* be wrong.
334 * It would be wrong if one byte (in some unknown encoding) could
335 * expand to >=4 bytes of UTF-8; I don't know if there are such things.
336 */
332 n = new = g_malloc(strlen(str) + 1); 337 n = new = g_malloc(strlen(str) + 1);
333 338
334 /* Here we will be looking for encoded words and if they seem to be 339 /* Here we will be looking for encoded words and if they seem to be
335 * valid then decode them. 340 * valid then decode them.
336 * They are of this form: =?charset?encoding?text?= 341 * They are of this form: =?charset?encoding?text?=
356 } 361 }
357 break; 362 break;
358 case state_charset: 363 case state_charset:
359 if (*cur == '?') { 364 if (*cur == '?') {
360 state = state_question2; 365 state = state_question2;
361 } else if (!token_char_p(*cur)) { 366 } else if (!token_char_p(*cur)) { /* This should never happen */
362 RECOVER_MARKED_TEXT; 367 RECOVER_MARKED_TEXT;
363 state = state_start; 368 state = state_start;
364 } 369 }
365 break; 370 break;
366 case state_question2: 371 case state_question2:
373 } 378 }
374 break; 379 break;
375 case state_encoding: 380 case state_encoding:
376 if (*cur == '?') { 381 if (*cur == '?') {
377 state = state_question3; 382 state = state_question3;
378 } else if (!token_char_p(*cur)) { 383 } else if (!token_char_p(*cur)) { /* This should never happen */
379 RECOVER_MARKED_TEXT; 384 RECOVER_MARKED_TEXT;
380 state = state_start; 385 state = state_start;
381 } 386 }
382 break; 387 break;
383 case state_question3: 388 case state_question3:
384 if (encoded_text_char_p(*cur)) { 389 if (encoded_text_char_p(*cur)) {
385 encoded_text0 = cur; 390 encoded_text0 = cur;
386 state = state_encoded_text; 391 state = state_encoded_text;
392 } else if (*cur == '?') { /* empty string */
393 encoded_text0 = cur;
394 state = state_question4;
387 } else { /* This should never happen */ 395 } else { /* This should never happen */
388 RECOVER_MARKED_TEXT; 396 RECOVER_MARKED_TEXT;
389 state = state_start; 397 state = state_start;
390 } 398 }
391 break; 399 break;