comparison src/libpst.c @ 257:c947b8812120

rfc2047 and rfc2231 encoding for non-ascii headers and attachment filenames
author Carl Byington <carl@five-ten-sg.com>
date Fri, 24 Dec 2010 19:26:05 -0800
parents 4573b536177f
children 156cf548c764
comparison
equal deleted inserted replaced
256:a863de65e5b8 257:c947b8812120
4383 (item->internet_cpid) ? codepage(item->internet_cpid, buflen, result) : 4383 (item->internet_cpid) ? codepage(item->internet_cpid, buflen, result) :
4384 "utf-8"; 4384 "utf-8";
4385 } 4385 }
4386 4386
4387 4387
4388 /** Convert str to rfc2231 encoding of str
4389 *
4390 * @param item pointer to the containing mapi item
4391 * @param str pointer to the mapi string of interest
4392 */
4393 void pst_rfc2231(pst_string *str) {
4394 int needs = 0;
4395 const int8_t *x = (int8_t *)str->str;
4396 while (*x) {
4397 if (*x <= 32) needs++;
4398 x++;
4399 }
4400 int n = strlen(str->str) + 2*needs + 15;
4401 char *buffer = pst_malloc(n);
4402 strcpy(buffer, "utf-8''");
4403 x = (int8_t *)str->str;
4404 const uint8_t *y = (uint8_t *)str->str;
4405 uint8_t *z = (uint8_t *)buffer;
4406 z += strlen(buffer); // skip the utf8 prefix
4407 while (*y) {
4408 if (*x <= 32) {
4409 *(z++) = (uint8_t)'%';
4410 snprintf(z, 3, "%2x", *y);
4411 z += 2;
4412 }
4413 else {
4414 *(z++) = *y;
4415 }
4416 x++;
4417 y++;
4418 }
4419 *z = '\0';
4420 free(str->str);
4421 str->str = buffer;
4422 }
4423
4424
4425 /** Convert str to rfc2047 encoding of str, possibly enclosed in quotes if it contains spaces
4426 *
4427 * @param item pointer to the containing mapi item
4428 * @param str pointer to the mapi string of interest
4429 */
4430 void pst_rfc2047(pst_item *item, pst_string *str, int needs_quote) {
4431 int has_space = 0;
4432 int needs_coding = 0;
4433 pst_convert_utf8(item, str);
4434 const int8_t *x = (int8_t *)str->str;
4435 while (*x) {
4436 if (*x == 32) has_space = 1;
4437 if (*x < 32) needs_coding = 1;
4438 x++;
4439 }
4440 if (needs_coding) {
4441 char *enc = pst_base64_encode_single(str->str, strlen(str->str));
4442 free(str->str);
4443 int n = strlen(enc) + 20;
4444 str->str = pst_malloc(n);
4445 snprintf(str->str, n, "=?utf-8?B?%s?=", enc);
4446 free(enc);
4447 }
4448 else if (has_space && needs_quote) {
4449 int n = strlen(str->str) + 10;
4450 char *buffer = pst_malloc(n);
4451 snprintf(buffer, n, "\"%s\"", str->str);
4452 free(str->str);
4453 str->str = buffer;
4454 }
4455 }
4456
4457
4388 /** Convert str to utf8 if possible; null strings are preserved. 4458 /** Convert str to utf8 if possible; null strings are preserved.
4389 * 4459 *
4390 * @param item pointer to the containing mapi item 4460 * @param item pointer to the containing mapi item
4391 * @param str pointer to the mapi string of interest 4461 * @param str pointer to the mapi string of interest
4392 */ 4462 */