Mercurial > libpst
comparison src/msg.cpp @ 309:4fd5197aacc2
fix charset issue with iconv return value
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 14 Dec 2009 22:19:50 -0800 |
parents | 97c53c6868ab |
children | a6df6ffc3ff5 |
comparison
equal
deleted
inserted
replaced
308:97c53c6868ab | 309:4fd5197aacc2 |
---|---|
30 /** Convert str to an 8 bit charset if it is utf8, null strings are preserved. | 30 /** Convert str to an 8 bit charset if it is utf8, null strings are preserved. |
31 * | 31 * |
32 * @param str reference to the mapi string of interest | 32 * @param str reference to the mapi string of interest |
33 * @param charset pointer to the 8 bit charset to use | 33 * @param charset pointer to the 8 bit charset to use |
34 */ | 34 */ |
35 static void convert_8bit(pst_string &str, const char *charset); | |
35 static void convert_8bit(pst_string &str, const char *charset) { | 36 static void convert_8bit(pst_string &str, const char *charset) { |
36 if (!str.str) return; // null | 37 if (!str.str) return; // null |
37 if (!str.is_utf8) return; // not utf8 | 38 if (!str.is_utf8) return; // not utf8 |
38 | 39 |
40 DEBUG_ENT("convert_8bit"); | |
39 pst_vbuf *newer = pst_vballoc(2); | 41 pst_vbuf *newer = pst_vballoc(2); |
40 size_t rc = pst_vb_utf8to8bit(newer, str.str, strlen(str.str), charset); | 42 size_t strsize = strlen(str.str); |
43 size_t rc = pst_vb_utf8to8bit(newer, str.str, strsize, charset); | |
41 if (rc == (size_t)-1) { | 44 if (rc == (size_t)-1) { |
42 // unable to convert, change the charset to utf8 | 45 // unable to convert, change the charset to utf8 |
43 free(newer->b); | 46 free(newer->b); |
44 DEBUG_INFO(("Failed to convert utf-8 to %s\n", charset)); | 47 DEBUG_INFO(("Failed to convert utf-8 to %s\n", charset)); |
48 DEBUG_HEXDUMPC(str.str, strsize, 0x10); | |
45 } | 49 } |
46 else { | 50 else { |
47 // null terminate the output string | 51 // null terminate the output string |
48 pst_vbgrow(newer, 1); | 52 pst_vbgrow(newer, 1); |
49 newer->b[newer->dlen] = '\0'; | 53 newer->b[newer->dlen] = '\0'; |
50 free(str.str); | 54 free(str.str); |
51 str.str = newer->b; | 55 str.str = newer->b; |
52 } | 56 } |
53 free(newer); | 57 free(newer); |
54 } | 58 DEBUG_RET(); |
55 | 59 } |
56 | 60 |
61 | |
62 static void empty_property(GsfOutfile *out, uint32_t tag); | |
57 static void empty_property(GsfOutfile *out, uint32_t tag) { | 63 static void empty_property(GsfOutfile *out, uint32_t tag) { |
58 vector<char> n(50); | 64 vector<char> n(50); |
59 snprintf(&n[0], n.size(), "__substg1.0_%08X", tag); | 65 snprintf(&n[0], n.size(), "__substg1.0_%08X", tag); |
60 GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false); | 66 GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false); |
61 gsf_output_close(dst); | 67 gsf_output_close(dst); |
62 g_object_unref(G_OBJECT(dst)); | 68 g_object_unref(G_OBJECT(dst)); |
63 } | 69 } |
64 | 70 |
65 | 71 |
72 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char *contents, size_t size); | |
66 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char *contents, size_t size) { | 73 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char *contents, size_t size) { |
67 if (!contents) return; | 74 if (!contents) return; |
68 vector<char> n(50); | 75 vector<char> n(50); |
69 snprintf(&n[0], n.size(), "__substg1.0_%08X", tag); | 76 snprintf(&n[0], n.size(), "__substg1.0_%08X", tag); |
70 GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false); | 77 GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false); |
80 p.reserved = 0; | 87 p.reserved = 0; |
81 prop.push_back(p); | 88 prop.push_back(p); |
82 } | 89 } |
83 | 90 |
84 | 91 |
92 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, FILE *fp); | |
85 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, FILE *fp) { | 93 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, FILE *fp) { |
86 vector<char> n(50); | 94 vector<char> n(50); |
87 snprintf(&n[0], n.size(), "__substg1.0_%08X", tag); | 95 snprintf(&n[0], n.size(), "__substg1.0_%08X", tag); |
88 GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false); | 96 GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false); |
89 | 97 |
107 p.reserved = 0; | 115 p.reserved = 0; |
108 prop.push_back(p); | 116 prop.push_back(p); |
109 } | 117 } |
110 | 118 |
111 | 119 |
120 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char* charset, pst_string &contents); | |
112 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char* charset, pst_string &contents) { | 121 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char* charset, pst_string &contents) { |
113 if (contents.str) { | 122 if (contents.str) { |
114 convert_8bit(contents, charset); | 123 convert_8bit(contents, charset); |
115 string_property(out, prop, tag, contents.str, strlen(contents.str)); | 124 string_property(out, prop, tag, contents.str, strlen(contents.str)); |
116 } | 125 } |
117 } | 126 } |
118 | 127 |
119 | 128 |
129 static void strin0_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char* charset, pst_string &contents); | |
120 static void strin0_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char* charset, pst_string &contents) { | 130 static void strin0_property(GsfOutfile *out, property_list &prop, uint32_t tag, const char* charset, pst_string &contents) { |
121 if (contents.str) { | 131 if (contents.str) { |
122 convert_8bit(contents, charset); | 132 convert_8bit(contents, charset); |
123 string_property(out, prop, tag, contents.str, strlen(contents.str)+1); | 133 string_property(out, prop, tag, contents.str, strlen(contents.str)+1); |
124 } | 134 } |
125 } | 135 } |
126 | 136 |
127 | 137 |
138 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const string &contents); | |
128 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const string &contents) { | 139 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, const string &contents) { |
129 string_property(out, prop, tag, contents.c_str(), contents.size()); | 140 string_property(out, prop, tag, contents.c_str(), contents.size()); |
130 } | 141 } |
131 | 142 |
132 | 143 |
144 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, pst_binary &contents); | |
133 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, pst_binary &contents) { | 145 static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, pst_binary &contents) { |
134 if (contents.size) string_property(out, prop, tag, contents.data, contents.size); | 146 if (contents.size) string_property(out, prop, tag, contents.data, contents.size); |
135 } | 147 } |
136 | 148 |
137 | 149 |
150 static void write_properties(GsfOutfile *out, property_list &prop, const guint8* header, size_t hlen); | |
138 static void write_properties(GsfOutfile *out, property_list &prop, const guint8* header, size_t hlen) { | 151 static void write_properties(GsfOutfile *out, property_list &prop, const guint8* header, size_t hlen) { |
139 GsfOutput* dst = gsf_outfile_new_child(out, "__properties_version1.0", false); | 152 GsfOutput* dst = gsf_outfile_new_child(out, "__properties_version1.0", false); |
140 gsf_output_write(dst, hlen, header); | 153 gsf_output_write(dst, hlen, header); |
141 for (property_list::iterator i=prop.begin(); i!=prop.end(); i++) { | 154 for (property_list::iterator i=prop.begin(); i!=prop.end(); i++) { |
142 property &p = *i; | 155 property &p = *i; |
145 gsf_output_close(dst); | 158 gsf_output_close(dst); |
146 g_object_unref(G_OBJECT(dst)); | 159 g_object_unref(G_OBJECT(dst)); |
147 } | 160 } |
148 | 161 |
149 | 162 |
163 static void int_property(property_list &prop_list, uint32_t tag, uint32_t flags, uint32_t value); | |
150 static void int_property(property_list &prop_list, uint32_t tag, uint32_t flags, uint32_t value) { | 164 static void int_property(property_list &prop_list, uint32_t tag, uint32_t flags, uint32_t value) { |
151 property p; | 165 property p; |
152 p.tag = tag; | 166 p.tag = tag; |
153 p.flags = flags; | 167 p.flags = flags; |
154 p.length = value; | 168 p.length = value; |
155 p.reserved = 0; | 169 p.reserved = 0; |
156 prop_list.push_back(p); | 170 prop_list.push_back(p); |
157 } | 171 } |
158 | 172 |
159 | 173 |
174 static void nzi_property(property_list &prop_list, uint32_t tag, uint32_t flags, uint32_t value); | |
160 static void nzi_property(property_list &prop_list, uint32_t tag, uint32_t flags, uint32_t value) { | 175 static void nzi_property(property_list &prop_list, uint32_t tag, uint32_t flags, uint32_t value) { |
161 if (value) int_property(prop_list, tag, flags, value); | 176 if (value) int_property(prop_list, tag, flags, value); |
162 } | 177 } |
163 | 178 |
164 | 179 |
165 void write_msg_email(char *fname, pst_item* item, pst_file* pst) { | 180 void write_msg_email(char *fname, pst_item* item, pst_file* pst) { |
166 // this is not an email item | 181 // this is not an email item |
167 if (!item->email) return; | 182 if (!item->email) return; |
183 DEBUG_ENT("write_msg_email"); | |
184 | |
168 pst_item_email &email = *(item->email); | 185 pst_item_email &email = *(item->email); |
169 | 186 |
170 char charset[30]; | 187 char charset[30]; |
171 const char* body_charset = pst_default_charset(item, sizeof(charset), charset); | 188 const char* body_charset = pst_default_charset(item, sizeof(charset), charset); |
189 DEBUG_INFO(("%s body charset seems to be %s\n", fname, body_charset)); | |
190 body_charset = "iso-8859-1//TRANSLIT//IGNORE"; | |
172 | 191 |
173 gsf_init(); | 192 gsf_init(); |
174 | 193 |
175 DEBUG_ENT("write_msg_email"); | |
176 GsfOutfile *outfile; | 194 GsfOutfile *outfile; |
177 GsfOutput *output; | 195 GsfOutput *output; |
178 GError *err = NULL; | 196 GError *err = NULL; |
179 | 197 |
180 output = gsf_output_stdio_new(fname, &err); | 198 output = gsf_output_stdio_new(fname, &err); |