… | |
… | |
22 | #include <stdlib.h> |
22 | #include <stdlib.h> |
23 | #include <string.h> |
23 | #include <string.h> |
24 | |
24 | |
25 | #include "generate_routines.h" |
25 | #include "generate_routines.h" |
26 | #include "error.h" |
26 | #include "error.h" |
|
|
27 | |
|
|
28 | #ifdef DMALLOC |
|
|
29 | #include <dmalloc.h> |
|
|
30 | #endif |
27 | |
31 | |
28 | static int psiconv_write_layout_section(psiconv_buffer buf, |
32 | static int psiconv_write_layout_section(psiconv_buffer buf, |
29 | const psiconv_text_and_layout value, |
33 | const psiconv_text_and_layout value, |
30 | const psiconv_word_styles_section styles, |
34 | const psiconv_word_styles_section styles, |
31 | int with_styles); |
35 | int with_styles); |
… | |
… | |
62 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
66 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
63 | return -PSICONV_E_NOMEM; |
67 | return -PSICONV_E_NOMEM; |
64 | } |
68 | } |
65 | if ((res = psiconv_write_u32(buf,entry->id))) |
69 | if ((res = psiconv_write_u32(buf,entry->id))) |
66 | return res; |
70 | return res; |
67 | if ((res = psiconv_write_u32(buf,entry->offset))) |
71 | if ((res = psiconv_write_offset(buf,entry->offset))) |
68 | return res; |
72 | return res; |
69 | } |
73 | } |
70 | return -PSICONV_E_OK; |
74 | return -PSICONV_E_OK; |
71 | } |
75 | } |
72 | |
76 | |
… | |
… | |
90 | if (!value) { |
94 | if (!value) { |
91 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
95 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
92 | return -PSICONV_E_GENERATE; |
96 | return -PSICONV_E_GENERATE; |
93 | } |
97 | } |
94 | |
98 | |
|
|
99 | if (psiconv_list_length(value)) { |
95 | if (!(extra_buf = psiconv_buffer_new(0))) |
100 | if (!(extra_buf = psiconv_buffer_new())) |
96 | return -PSICONV_E_NOMEM; |
101 | return -PSICONV_E_NOMEM; |
97 | for (i = 0; i < psiconv_list_length(value); i++) { |
102 | for (i = 0; i < psiconv_list_length(value); i++) { |
98 | if (!(paragraph = psiconv_list_get(value,i))) { |
103 | if (!(paragraph = psiconv_list_get(value,i))) { |
99 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
104 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
100 | res = -PSICONV_E_OTHER; |
105 | res = -PSICONV_E_OTHER; |
|
|
106 | goto ERROR; |
|
|
107 | } |
|
|
108 | for (j = 0; j < strlen(paragraph->text); j++) |
|
|
109 | if ((res = psiconv_write_u8(extra_buf,paragraph->text[j]))) |
|
|
110 | goto ERROR; |
|
|
111 | psiconv_write_u8(extra_buf,0x06); |
|
|
112 | } |
|
|
113 | if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf)))) |
101 | goto ERROR; |
114 | goto ERROR; |
102 | } |
|
|
103 | for (j = 0; j < strlen(paragraph->text); j++) |
|
|
104 | if ((res = psiconv_write_u8(extra_buf,paragraph->text[j]))) |
|
|
105 | goto ERROR; |
|
|
106 | psiconv_write_u8(extra_buf,0x06); |
|
|
107 | } |
|
|
108 | if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf)))) |
|
|
109 | goto ERROR; |
|
|
110 | res = psiconv_buffer_concat(buf,extra_buf); |
115 | res = psiconv_buffer_concat(buf,extra_buf); |
|
|
116 | } else |
|
|
117 | /* Hack: empty text sections are just not allowed */ |
|
|
118 | return psiconv_write_u16(buf,0x0602); |
111 | |
119 | |
112 | ERROR: |
120 | ERROR: |
113 | psiconv_buffer_free(extra_buf); |
121 | psiconv_buffer_free(extra_buf); |
114 | return res; |
122 | return res; |
115 | } |
123 | } |
… | |
… | |
131 | struct psiconv_paragraph_type_list_s new_type; |
139 | struct psiconv_paragraph_type_list_s new_type; |
132 | psiconv_buffer buf_types,buf_elements,buf_inlines; |
140 | psiconv_buffer buf_types,buf_elements,buf_inlines; |
133 | psiconv_paragraph paragraph; |
141 | psiconv_paragraph paragraph; |
134 | psiconv_in_line_layout in_line; |
142 | psiconv_in_line_layout in_line; |
135 | psiconv_word_style style; |
143 | psiconv_word_style style; |
136 | int i,j,para_type,nr_of_inlines=0,res; |
144 | psiconv_character_layout para_charlayout; |
|
|
145 | int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen; |
137 | |
146 | |
138 | if (!value) { |
147 | if (!value) { |
139 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
148 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
140 | return -PSICONV_E_GENERATE; |
149 | return -PSICONV_E_GENERATE; |
141 | } |
150 | } |
… | |
… | |
143 | if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) { |
152 | if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) { |
144 | res = -PSICONV_E_NOMEM; |
153 | res = -PSICONV_E_NOMEM; |
145 | goto ERROR1; |
154 | goto ERROR1; |
146 | } |
155 | } |
147 | |
156 | |
148 | if (!(buf_types = psiconv_buffer_new(0))) { |
157 | if (!(buf_types = psiconv_buffer_new())) { |
149 | res = -PSICONV_E_NOMEM; |
158 | res = -PSICONV_E_NOMEM; |
150 | goto ERROR2; |
159 | goto ERROR2; |
151 | } |
160 | } |
152 | |
161 | |
153 | if (!(buf_elements = psiconv_buffer_new(0))) { |
162 | if (!(buf_elements = psiconv_buffer_new())) { |
154 | res = -PSICONV_E_NOMEM; |
163 | res = -PSICONV_E_NOMEM; |
155 | goto ERROR3; |
164 | goto ERROR3; |
156 | } |
165 | } |
157 | |
166 | |
158 | if (!(buf_inlines = psiconv_buffer_new(0))) { |
167 | if (!(buf_inlines = psiconv_buffer_new())) { |
159 | res = -PSICONV_E_NOMEM; |
168 | res = -PSICONV_E_NOMEM; |
160 | goto ERROR4; |
169 | goto ERROR4; |
161 | } |
170 | } |
162 | |
171 | |
163 | for (i = 0; i < psiconv_list_length(value); i++) { |
172 | for (i = 0; i < psiconv_list_length(value); i++) { |
… | |
… | |
167 | goto ERROR5; |
176 | goto ERROR5; |
168 | } |
177 | } |
169 | if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) |
178 | if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) |
170 | goto ERROR5; |
179 | goto ERROR5; |
171 | |
180 | |
172 | if (psiconv_list_length(paragraph->in_lines)) { |
181 | if (psiconv_list_length(paragraph->in_lines) > 1) { |
173 | /* Inline layouts, so we generate a paragraph element and inline |
182 | /* Inline layouts, so we generate a paragraph element and inline |
174 | elements */ |
183 | elements */ |
175 | if ((res = psiconv_write_u32(buf_elements,0x00))) |
184 | if ((res = psiconv_write_u8(buf_elements,0x00))) |
176 | goto ERROR5; |
185 | goto ERROR5; |
177 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
186 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
178 | psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); |
187 | psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); |
179 | res = -PSICONV_E_GENERATE; |
188 | res = -PSICONV_E_GENERATE; |
180 | goto ERROR5; |
189 | goto ERROR5; |
… | |
… | |
189 | if ((res = psiconv_write_u32(buf_elements, |
198 | if ((res = psiconv_write_u32(buf_elements, |
190 | psiconv_list_length(paragraph->in_lines)))) |
199 | psiconv_list_length(paragraph->in_lines)))) |
191 | goto ERROR5; |
200 | goto ERROR5; |
192 | |
201 | |
193 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
202 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
|
|
203 | paralen = 0; |
194 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
204 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
195 | nr_of_inlines ++; |
205 | nr_of_inlines ++; |
196 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
206 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
197 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
207 | psiconv_warn(0,psiconv_buffer_length(buf), |
|
|
208 | "Massive memory corruption"); |
198 | res = -PSICONV_E_OTHER; |
209 | res = -PSICONV_E_OTHER; |
199 | goto ERROR5; |
210 | goto ERROR5; |
200 | } |
211 | } |
201 | if ((res = psiconv_write_u8(buf_inlines,0x00))) |
212 | if ((res = psiconv_write_u8(buf_inlines,0x00))) |
202 | goto ERROR5; |
213 | goto ERROR5; |
203 | if ((res = psiconv_write_u32(buf_inlines,in_line->length))) |
214 | thislen = in_line->length; |
|
|
215 | paralen += thislen; |
|
|
216 | /* If this is the last in_line, we need to make sure that the |
|
|
217 | complete length of all inlines equals the text length */ |
|
|
218 | if (j == psiconv_list_length(paragraph->in_lines)-1) { |
|
|
219 | if (paralen > strlen(paragraph->text)+1) { |
|
|
220 | res = -PSICONV_E_GENERATE; |
204 | goto ERROR5; |
221 | goto ERROR5; |
|
|
222 | } |
|
|
223 | thislen += strlen(paragraph->text)+1-paralen; |
|
|
224 | } |
|
|
225 | if ((res = psiconv_write_u32(buf_inlines,thislen))) |
|
|
226 | goto ERROR5; |
205 | if ((res = psiconv_write_character_layout_list(buf_elements, |
227 | if ((res = psiconv_write_character_layout_list(buf_inlines, |
206 | in_line->layout, |
228 | in_line->layout, |
207 | style->character))) |
229 | style->character))) |
208 | goto ERROR5; |
230 | goto ERROR5; |
209 | } |
231 | } |
210 | } else { |
232 | } else { |
211 | /* No inline layouts, so we generate a paragraph type list */ |
233 | /* No inline layouts (or only 1), so we generate a paragraph type list */ |
212 | para_type = 0; |
234 | para_type = 0; |
|
|
235 | /* Set para_charlayout to the correct character-level layout */ |
|
|
236 | if (psiconv_list_length(paragraph->in_lines) == 0) |
|
|
237 | para_charlayout = paragraph->base_character; |
|
|
238 | else { |
|
|
239 | if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { |
|
|
240 | psiconv_warn(0,psiconv_buffer_length(buf), |
|
|
241 | "Massive memory corruption"); |
|
|
242 | res = -PSICONV_E_OTHER; |
|
|
243 | goto ERROR5; |
|
|
244 | } |
|
|
245 | para_charlayout = in_line->layout; |
|
|
246 | } |
213 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
247 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
214 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
248 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
215 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
249 | psiconv_warn(0,psiconv_buffer_length(buf), |
|
|
250 | "Massive memory corruption"); |
216 | res = -PSICONV_E_OTHER; |
251 | res = -PSICONV_E_OTHER; |
217 | goto ERROR5; |
252 | goto ERROR5; |
218 | } |
253 | } |
219 | if ((paragraph->base_style == paragraph_type->style) && |
254 | if ((paragraph->base_style == paragraph_type->style) && |
220 | !psiconv_compare_character_layout(paragraph->base_character, |
255 | !psiconv_compare_character_layout(para_charlayout, |
221 | paragraph_type->character) && |
256 | paragraph_type->character) && |
222 | !psiconv_compare_paragraph_layout(paragraph->base_paragraph, |
257 | !psiconv_compare_paragraph_layout(paragraph->base_paragraph, |
223 | paragraph_type->paragraph)) { |
258 | paragraph_type->paragraph)) { |
224 | para_type = paragraph_type->nr; |
259 | para_type = paragraph_type->nr; |
225 | break; |
260 | break; |
… | |
… | |
228 | if (!para_type) { |
263 | if (!para_type) { |
229 | /* We need to add a new entry */ |
264 | /* We need to add a new entry */ |
230 | para_type = new_type.nr = j+1; |
265 | para_type = new_type.nr = j+1; |
231 | /* No need to copy them, we won't change them anyway */ |
266 | /* No need to copy them, we won't change them anyway */ |
232 | new_type.paragraph = paragraph->base_paragraph; |
267 | new_type.paragraph = paragraph->base_paragraph; |
233 | new_type.character = paragraph->base_character; |
268 | new_type.character = para_charlayout; |
234 | new_type.style = paragraph->base_style; |
269 | new_type.style = paragraph->base_style; |
235 | paragraph_type = &new_type; |
270 | paragraph_type = &new_type; |
236 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
271 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
237 | goto ERROR5; |
272 | goto ERROR5; |
238 | if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) |
273 | if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) |
… | |
… | |
255 | if ((res = psiconv_write_u8(buf_elements,para_type))) |
290 | if ((res = psiconv_write_u8(buf_elements,para_type))) |
256 | goto ERROR5; |
291 | goto ERROR5; |
257 | } |
292 | } |
258 | } |
293 | } |
259 | |
294 | |
|
|
295 | /* HACK: special case: no paragraphs at all. We need to improvize. */ |
|
|
296 | if (!psiconv_list_length(value)) { |
|
|
297 | if ((res = psiconv_write_u32(buf_types,1))) |
|
|
298 | goto ERROR5; |
|
|
299 | if ((res = psiconv_write_u32(buf_types,0))) |
|
|
300 | goto ERROR5; |
|
|
301 | if (with_styles) |
|
|
302 | if ((res = psiconv_write_u8(buf_types,0))) |
|
|
303 | goto ERROR5; |
|
|
304 | if ((res = psiconv_write_u32(buf_types,0))) |
|
|
305 | goto ERROR5; |
|
|
306 | |
|
|
307 | if ((res = psiconv_write_u32(buf_elements,1))) |
|
|
308 | goto ERROR5; |
|
|
309 | if ((res = psiconv_write_u8(buf_elements,1))) |
|
|
310 | goto ERROR5; |
|
|
311 | pel_length = 1; |
|
|
312 | ptl_length = 1; |
|
|
313 | } else { |
|
|
314 | pel_length = psiconv_list_length(value); |
|
|
315 | ptl_length = psiconv_list_length(paragraph_type_list); |
|
|
316 | } |
|
|
317 | |
260 | /* Now append everything */ |
318 | /* Now append everything */ |
261 | if ((res = psiconv_write_u8(buf,with_styles?0x00:0x01))) |
319 | if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) |
262 | goto ERROR5; |
320 | goto ERROR5; |
263 | if ((res = psiconv_write_u8(buf,0x00))) |
321 | if ((res = psiconv_write_u8(buf, ptl_length))) |
264 | goto ERROR5; |
|
|
265 | if ((res = psiconv_write_u8(buf,psiconv_list_length(paragraph_type_list)))) |
|
|
266 | goto ERROR5; |
322 | goto ERROR5; |
267 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
323 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
268 | goto ERROR5; |
324 | goto ERROR5; |
269 | if ((res = psiconv_write_u32(buf,psiconv_list_length(value)))) |
325 | if ((res = psiconv_write_u32(buf,pel_length))) |
270 | goto ERROR5; |
326 | goto ERROR5; |
271 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
327 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
272 | goto ERROR5; |
328 | goto ERROR5; |
273 | if ((res = psiconv_write_u32(buf,nr_of_inlines))) |
329 | if ((res = psiconv_write_u32(buf,nr_of_inlines))) |
274 | goto ERROR5; |
330 | goto ERROR5; |