comparison src/libpst.c @ 211:94bde95d7e18

the shared library interface should now be thread safe
author Carl Byington <carl@five-ten-sg.com>
date Mon, 08 Jun 2009 11:49:39 -0700
parents 268458c79e9b
children fef2214083a4
comparison
equal deleted inserted replaced
210:2d1111fd70cf 211:94bde95d7e18
4141 DEBUG_RET(); 4141 DEBUG_RET();
4142 return x; 4142 return x;
4143 } 4143 }
4144 4144
4145 4145
4146 char* pst_rfc2426_escape(char *str) { 4146 char* pst_rfc2426_escape(char* str, char **buf, size_t* buflen) {
4147 static char* buf = NULL; 4147 //static char* buf = NULL;
4148 static size_t buflen = 0; 4148 //static size_t buflen = 0;
4149 char *ret, *a, *b; 4149 char *ret, *a, *b;
4150 size_t x = 0; 4150 size_t x = 0;
4151 int y, z; 4151 int y, z;
4152 if (!str) return NULL; 4152 if (!str) return NULL;
4153 DEBUG_ENT("rfc2426_escape"); 4153 DEBUG_ENT("rfc2426_escape");
4160 if (y == 0 && z == 0) 4160 if (y == 0 && z == 0)
4161 // there isn't any extra space required 4161 // there isn't any extra space required
4162 ret = str; 4162 ret = str;
4163 else { 4163 else {
4164 x = strlen(str) + y - z + 1; // don't forget room for the NUL 4164 x = strlen(str) + y - z + 1; // don't forget room for the NUL
4165 if (x > buflen) { 4165 if (x > *buflen) {
4166 buf = (char*) realloc(buf, x); 4166 *buf = (char*) realloc(*buf, x);
4167 buflen = x; 4167 *buflen = x;
4168 } 4168 }
4169 a = str; 4169 a = str;
4170 b = buf; 4170 b = *buf;
4171 while (*a != '\0') { 4171 while (*a != '\0') {
4172 switch (*a) { 4172 switch (*a) {
4173 case ',' : 4173 case ',' :
4174 case '\\': 4174 case '\\':
4175 case ';' : 4175 case ';' :
4188 } 4188 }
4189 b++; 4189 b++;
4190 a++; 4190 a++;
4191 } 4191 }
4192 *b = '\0'; // NUL-terminate the string (buf) 4192 *b = '\0'; // NUL-terminate the string (buf)
4193 ret = buf; 4193 ret = *buf;
4194 } 4194 }
4195 DEBUG_RET(); 4195 DEBUG_RET();
4196 return ret; 4196 return ret;
4197 } 4197 }
4198 4198
4245 4245
4246 4246
4247 /** Convert a code page integer into a string suitable for iconv() 4247 /** Convert a code page integer into a string suitable for iconv()
4248 * 4248 *
4249 * @param cp the code page integer used in the pst file 4249 * @param cp the code page integer used in the pst file
4250 * @param[in] buflen length of the output buffer
4251 * @param[out] result pointer to output buffer, must be at least 30 bytes
4250 * @return pointer to a static buffer holding the string representation of the 4252 * @return pointer to a static buffer holding the string representation of the
4251 * equivalent iconv character set 4253 * equivalent iconv character set
4252 */ 4254 */
4253 static const char* codepage(int cp); 4255 static const char* codepage(int cp, int buflen, char* result);
4254 static const char* codepage(int cp) { 4256 static const char* codepage(int cp, int buflen, char* result) {
4255 static char buffer[20];
4256 switch (cp) { 4257 switch (cp) {
4257 case 932 : return "iso-2022-jp"; 4258 case 932 : return "iso-2022-jp";
4258 case 936 : return "gb2313"; 4259 case 936 : return "gb2313";
4259 case 950 : return "big5"; 4260 case 950 : return "big5";
4260 case 20127 : return "us-ascii"; 4261 case 20127 : return "us-ascii";
4281 case 51932 : return "euc-jp"; 4282 case 51932 : return "euc-jp";
4282 case 51949 : return "euc-kr"; 4283 case 51949 : return "euc-kr";
4283 case 65000 : return "utf-7"; 4284 case 65000 : return "utf-7";
4284 case 65001 : return "utf-8"; 4285 case 65001 : return "utf-8";
4285 default : 4286 default :
4286 snprintf(buffer, sizeof(buffer), "windows-%d", cp); 4287 snprintf(result, buflen, "windows-%d", cp);
4287 return buffer; 4288 return result;
4288 } 4289 }
4289 return NULL; 4290 return NULL;
4290 } 4291 }
4291 4292
4292 4293
4293 /** Get the default character set for this item. This is used to find 4294 /** Get the default character set for this item. This is used to find
4294 * the charset for pst_string elements that are not already in utf8 encoding. 4295 * the charset for pst_string elements that are not already in utf8 encoding.
4295 * @param item pointer to the mapi item of interest 4296 * @param item pointer to the mapi item of interest
4297 * @param[in] buflen length of the output buffer
4298 * @param[out] result pointer to output buffer, must be at least 30 bytes
4296 * @return default character set as a string useable by iconv() 4299 * @return default character set as a string useable by iconv()
4297 */ 4300 */
4298 const char* pst_default_charset(pst_item *item) { 4301 const char* pst_default_charset(pst_item *item, int buflen, char* result) {
4299 return (item->body_charset.str) ? item->body_charset.str : 4302 return (item->body_charset.str) ? item->body_charset.str :
4300 (item->message_codepage) ? codepage(item->message_codepage) : 4303 (item->message_codepage) ? codepage(item->message_codepage, buflen, result) :
4301 (item->internet_cpid) ? codepage(item->internet_cpid) : 4304 (item->internet_cpid) ? codepage(item->internet_cpid, buflen, result) :
4302 "utf-8"; 4305 "utf-8";
4303 } 4306 }
4304 4307
4305 4308
4306 /** Convert str to utf8 if possible; null strings are preserved. 4309 /** Convert str to utf8 if possible; null strings are preserved.
4318 * 4321 *
4319 * @param item pointer to the containing mapi item 4322 * @param item pointer to the containing mapi item
4320 * @param str pointer to the mapi string of interest 4323 * @param str pointer to the mapi string of interest
4321 */ 4324 */
4322 void pst_convert_utf8(pst_item *item, pst_string *str) { 4325 void pst_convert_utf8(pst_item *item, pst_string *str) {
4326 char buffer[30];
4323 if (str->is_utf8) return; 4327 if (str->is_utf8) return;
4324 if (!str->str) { 4328 if (!str->str) {
4325 str->str = strdup(""); 4329 str->str = strdup("");
4326 return; 4330 return;
4327 } 4331 }
4328 const char *charset = pst_default_charset(item); 4332 const char *charset = pst_default_charset(item, sizeof(buffer), buffer);
4329 if (!strcasecmp("utf-8", charset)) return; // already utf8 4333 if (!strcasecmp("utf-8", charset)) return; // already utf8
4330 DEBUG_ENT("pst_convert_utf8"); 4334 DEBUG_ENT("pst_convert_utf8");
4331 pst_vbuf *newer = pst_vballoc(2); 4335 pst_vbuf *newer = pst_vballoc(2);
4332 size_t rc = pst_vb_8bit2utf8(newer, str->str, strlen(str->str) + 1, charset); 4336 size_t rc = pst_vb_8bit2utf8(newer, str->str, strlen(str->str) + 1, charset);
4333 if (rc == (size_t)-1) { 4337 if (rc == (size_t)-1) {