Mercurial > libpst
comparison src/libpst.c @ 298:201464dd356e
add default character set for items where the pst file does not specify a character set
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 02 Aug 2011 17:02:39 -0700 |
parents | 8b3a827b71f4 |
children | 5338d93889aa |
comparison
equal
deleted
inserted
replaced
297:8b3a827b71f4 | 298:201464dd356e |
---|---|
288 static int pst_strincmp(char *a, char *b, size_t x); | 288 static int pst_strincmp(char *a, char *b, size_t x); |
289 static char* pst_wide_to_single(char *wt, size_t size); | 289 static char* pst_wide_to_single(char *wt, size_t size); |
290 | 290 |
291 | 291 |
292 | 292 |
293 int pst_open(pst_file *pf, const char *name) { | 293 int pst_open(pst_file *pf, const char *name, const char *charset) { |
294 int32_t sig; | 294 int32_t sig; |
295 | 295 |
296 pst_unicode_init(); | 296 pst_unicode_init(); |
297 | 297 |
298 DEBUG_ENT("pst_open"); | 298 DEBUG_ENT("pst_open"); |
301 WARN (("cannot be passed a NULL pst_file\n")); | 301 WARN (("cannot be passed a NULL pst_file\n")); |
302 DEBUG_RET(); | 302 DEBUG_RET(); |
303 return -1; | 303 return -1; |
304 } | 304 } |
305 memset(pf, 0, sizeof(*pf)); | 305 memset(pf, 0, sizeof(*pf)); |
306 pf->charset = charset; | |
306 | 307 |
307 if ((pf->fp = fopen(name, "rb")) == NULL) { | 308 if ((pf->fp = fopen(name, "rb")) == NULL) { |
308 perror("Error opening PST file"); | 309 perror("Error opening PST file"); |
309 DEBUG_RET(); | 310 DEBUG_RET(); |
310 return -1; | 311 return -1; |
1243 return NULL; | 1244 return NULL; |
1244 } | 1245 } |
1245 | 1246 |
1246 item = (pst_item*) pst_malloc(sizeof(pst_item)); | 1247 item = (pst_item*) pst_malloc(sizeof(pst_item)); |
1247 memset(item, 0, sizeof(pst_item)); | 1248 memset(item, 0, sizeof(pst_item)); |
1249 item->pf = pf; | |
1248 | 1250 |
1249 if (pst_process(d_ptr->desc->i_id, list, item, NULL)) { | 1251 if (pst_process(d_ptr->desc->i_id, list, item, NULL)) { |
1250 DEBUG_WARN(("pst_process() returned non-zero value. That is an error\n")); | 1252 DEBUG_WARN(("pst_process() returned non-zero value. That is an error\n")); |
1251 pst_freeItem(item); | 1253 pst_freeItem(item); |
1252 pst_free_list(list); | 1254 pst_free_list(list); |
4355 * @param[in] buflen length of the output buffer | 4357 * @param[in] buflen length of the output buffer |
4356 * @param[out] result pointer to output buffer, must be at least 30 bytes | 4358 * @param[out] result pointer to output buffer, must be at least 30 bytes |
4357 * @return default character set as a string useable by iconv() | 4359 * @return default character set as a string useable by iconv() |
4358 */ | 4360 */ |
4359 const char* pst_default_charset(pst_item *item, int buflen, char* result) { | 4361 const char* pst_default_charset(pst_item *item, int buflen, char* result) { |
4360 return (item->body_charset.str) ? item->body_charset.str : | 4362 return (item->body_charset.str) ? item->body_charset.str : |
4361 (item->message_codepage) ? codepage(item->message_codepage, buflen, result) : | 4363 (item->message_codepage) ? codepage(item->message_codepage, buflen, result) : |
4362 (item->internet_cpid) ? codepage(item->internet_cpid, buflen, result) : | 4364 (item->internet_cpid) ? codepage(item->internet_cpid, buflen, result) : |
4363 "utf-8"; | 4365 (item->pf && item->pf->charset) ? item->pf->charset : |
4366 "iso-8859-1"; | |
4364 } | 4367 } |
4365 | 4368 |
4366 | 4369 |
4367 /** Convert str to rfc2231 encoding of str | 4370 /** Convert str to rfc2231 encoding of str |
4368 * | 4371 * |
4449 * | 4452 * |
4450 * @param item pointer to the containing mapi item | 4453 * @param item pointer to the containing mapi item |
4451 * @param str pointer to the mapi string of interest | 4454 * @param str pointer to the mapi string of interest |
4452 */ | 4455 */ |
4453 void pst_convert_utf8(pst_item *item, pst_string *str) { | 4456 void pst_convert_utf8(pst_item *item, pst_string *str) { |
4457 DEBUG_ENT("pst_convert_utf8"); | |
4454 char buffer[30]; | 4458 char buffer[30]; |
4455 if (str->is_utf8) return; | 4459 if (str->is_utf8) { |
4460 DEBUG_WARN(("Already utf8\n")); | |
4461 DEBUG_RET(); | |
4462 return; | |
4463 } | |
4456 if (!str->str) { | 4464 if (!str->str) { |
4457 str->str = strdup(""); | 4465 str->str = strdup(""); |
4466 DEBUG_WARN(("null to empty string\n")); | |
4467 DEBUG_RET(); | |
4458 return; | 4468 return; |
4459 } | 4469 } |
4460 const char *charset = pst_default_charset(item, sizeof(buffer), buffer); | 4470 const char *charset = pst_default_charset(item, sizeof(buffer), buffer); |
4461 if (!strcasecmp("utf-8", charset)) return; // already utf8 | 4471 DEBUG_WARN(("default charset is %s\n", charset)); |
4462 DEBUG_ENT("pst_convert_utf8"); | 4472 if (!strcasecmp("utf-8", charset)) { |
4473 DEBUG_RET(); | |
4474 return; | |
4475 } | |
4463 pst_vbuf *newer = pst_vballoc(2); | 4476 pst_vbuf *newer = pst_vballoc(2); |
4464 size_t rc = pst_vb_8bit2utf8(newer, str->str, strlen(str->str) + 1, charset); | 4477 size_t rc = pst_vb_8bit2utf8(newer, str->str, strlen(str->str) + 1, charset); |
4465 if (rc == (size_t)-1) { | 4478 if (rc == (size_t)-1) { |
4466 free(newer->b); | 4479 free(newer->b); |
4467 DEBUG_WARN(("Failed to convert %s to utf-8 - %s\n", charset, str->str)); | 4480 DEBUG_WARN(("Failed to convert %s to utf-8 - %s\n", charset, str->str)); |