Debian Codegraph @

converter/other/pngtxt.c

raw symbols SCIP
1 #define HAVE_PNGLIB_WITH_ITXT 0
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6
7 #include <png.h>
8
9 #include "mallocvar.h"
10 #include "nstring.h"
11 #include "pngx.h"
12 #include "pngtxt.h"
13 #include "pm.h"
14
15
16
17 static void
18 readToken(char const textline[],
19 unsigned int const lineLength,
20 unsigned int * const cursorP,
21 const char ** const tokenP) {
22 /*----------------------------------------------------------------------------
23 Read a token from 'textline' (whose length is 'lineLength'), assuming the
24 cursor is positioned to it now, leaving the cursor positioned after it.
25
26 Tokens are delimited by white space. We don't skip any white space before
27 or after the token. Ergo, if we are positioned to white space right now,
28 the token we read is a null string.
29 -----------------------------------------------------------------------------*/
30 char * tokenBuffer;
31 char * cp;
32 unsigned int cursor;
33
34 cursor = *cursorP;
35
36 MALLOCARRAY(tokenBuffer, lineLength + 1);
37 /* leave room for terminating NUL */
38 if (tokenBuffer == NULL)
39 pm_error("Unable to allocate memory for a %u-character "
40 "text string file line", lineLength);
41
42 cp = &tokenBuffer[0]; /* initial value */
43
44 if (textline[0] == '"') {
45 ++cursor; /* skip past opening quotation mark */
46 while (textline[cursor] != '"') {
47 if (cursor >= lineLength)
48 pm_error("Invalid text string file format. Line ends in "
49 "the middle of a quoted token. Text at the end of "
50 "the line is '%s'", tokenBuffer);
51 if (textline[cursor] == '\0')
52 pm_error("Invalid text string file format: Token contains "
53 "a NUL character. Text leading up to the NUL "
54 "character is '%s'", tokenBuffer);
55 *(cp++) = textline[cursor++];
56 }
57 ++cursor; /* skip past closing quotation mark */
58 } else {
59 while ((cursor < lineLength) &&
60 (textline[cursor] != ' ') && (textline[cursor] != '\t')) {
61
62 if (textline[cursor] == '\0')
63 pm_error("Invalid text string file format: Token contains "
64 "a NUL character. Text leading up to the NUL "
65 "character is '%s'", tokenBuffer);
66 *(cp++) = textline[cursor++];
67 }
68 }
69 *cp++ = '\0';
70
71 *cursorP = cursor;
72
73 *tokenP = tokenBuffer;
74 }
75
76
77
78 static void
79 skipWhiteSpace(char const textline[],
80 unsigned int const lineLength,
81 unsigned int * const cursorP) {
82 /*----------------------------------------------------------------------------
83 Move *cursorP past white space (or, for some reason, NUL characters),
84 in 'textline', which is 'lineLength' long.
85 -----------------------------------------------------------------------------*/
86 unsigned int cursor;
87
88 cursor = *cursorP; /* initial value */
89
90 while (cursor < lineLength &&
91 (textline[cursor] == ' ' || textline[cursor] == '\t' ||
92 textline[cursor] == '\0'))
93 ++cursor;
94
95 *cursorP = cursor;
96 }
97
98
99
100 static void
101 readTextString(char const textline[],
102 unsigned int const lineLength,
103 unsigned int const startPos,
104 png_charp * const textStringP,
105 png_size_t * const textStringLengthP) {
106 /*----------------------------------------------------------------------------
107 Extract the text string at 'startPos' in the buffer 'textline', whose
108 length is 'lineLength'. Return it in newly malloced storage with a
109 pointer to that storage as 'textString' and the size of the text as
110 *textStringLengthP.
111 -----------------------------------------------------------------------------*/
112 char * cp;
113
114 MALLOCARRAY(cp, lineLength + 1); /* incl '\0' */
115 if (!cp)
116 pm_error("Unable to allocate memory for text chunks");
117
118 memcpy(cp, textline + startPos, lineLength - startPos);
119 cp[lineLength - startPos] = '\0'; /* for safety - not part of text */
120 *textStringP = cp;
121 *textStringLengthP = lineLength - startPos;
122 }
123
124
125
126 static void
127 startTextChunkEngl(png_text * const textChunkP,
128 char const textline[],
129 unsigned int const lineLength,
130 bool const isCompressed,
131 bool const verbose) {
132 /*----------------------------------------------------------------------------
133 Assuming 'textline' is the first line of an entry in an English text
134 string file, put the information from it in the comment record *textChunkP.
135 Use the text on this line as the comment text, even though the true text
136 string may include text from subsequent continuation lines as well.
137
138 'textline' is not NUL-terminated. Its length is 'lineLength', and it is at
139 least one character long. 'textline' does not contain a newline character.
140
141 'isCompressed' means it is a compressed text chunk.
142 -----------------------------------------------------------------------------*/
143 unsigned int cursor;
144
145 cursor = 0;
146
147 {
148 const char * key;
149
150 readToken(textline, lineLength, &cursor, &key);
151
152 pngx_setTextKey(textChunkP, key);
153
154 pm_strfree(key);
155 }
156
157 skipWhiteSpace(textline, lineLength, &cursor);
158
159 pngx_setTextLang(textChunkP, NULL);
160
161 readTextString(textline, lineLength, cursor, &textChunkP->text,
162 &textChunkP->text_length);
163
164 textChunkP->compression =
165 isCompressed ? PNG_TEXT_COMPRESSION_zTXt : PNG_TEXT_COMPRESSION_NONE;
166 }
167
168
169
170 static void
171 startTextChunkIntl(png_text * const textChunkP,
172 char const textline[],
173 unsigned int const lineLength,
174 bool const isCompressed,
175 bool const verbose) {
176 /*----------------------------------------------------------------------------
177 Assuming 'textline' is the first line of an entry in an international (not
178 English) text string file, put the information from it in the text chunk
179 *textChunkP. Use the text on this line as the text string, even though the
180 true text string may include text from subsequent continuation lines as
181 well.
182
183 'textline' is not NUL-terminated. Its length is 'lineLength', and it is at
184 least one character long. 'textline' does not contain a newline character.
185
186 'isInternational' means it is an international (i.e. non-English) text
187 chunk.
188
189 Leave the language attribute (textChunkP->lang) unset.
190 -----------------------------------------------------------------------------*/
191 unsigned int cursor;
192
193 cursor = 0; /* Initial value */
194
195 {
196 const char * key;
197
198 readToken(textline, lineLength, &cursor, &key);
199
200 pngx_setTextKey(textChunkP, key);
201
202 pm_strfree(key);
203 }
204
205 skipWhiteSpace(textline, lineLength, &cursor);
206
207 {
208 const char * isoLang;
209
210 readToken(textline, lineLength, &cursor, &isoLang);
211
212 pngx_setTextLang(textChunkP, isoLang);
213
214 pm_strfree(isoLang);
215 }
216
217 skipWhiteSpace(textline, lineLength, &cursor);
218
219 {
220 const char * langKey;
221
222 readToken(textline, lineLength, &cursor, &langKey);
223
224 pngx_setTextLangKey(textChunkP, langKey);
225
226 pm_strfree(langKey);
227 }
228
229 skipWhiteSpace(textline, lineLength, &cursor);
230
231 /* Beginning of text string (continuation lines may follow) */
232 readTextString(textline, lineLength, cursor, &textChunkP->text,
233 &textChunkP->text_length);
234
235 textChunkP->compression =
236 isCompressed ? PNG_ITXT_COMPRESSION_zTXt :PNG_ITXT_COMPRESSION_NONE;
237 }
238
239
240
241 static void
242 continueTextString(png_text * const textChunkP,
243 char const textline[],
244 unsigned int const lineLength) {
245 /*----------------------------------------------------------------------------
246 Update the text chunk *textChunkP by adding to it the text from
247 textline[], which is a continuation line from a text string file.
248
249 'textline' is not NUL-terminated. Its length is 'lineLength', and
250 it is at least one character long. 'textline' does not contain a
251 newline character.
252 -----------------------------------------------------------------------------*/
253 unsigned int cursor; /* cursor into textline[] */
254
255 unsigned int const newTextLength =
256 textChunkP->text_length + lineLength + 1 + 1;
257
258 REALLOCARRAY(textChunkP->text, newTextLength);
259
260 if (textChunkP->text == NULL)
261 pm_error("Unable to allocate %u bytes of memory for text string",
262 newTextLength);
263
264 textChunkP->text[textChunkP->text_length++] = '\n';
265
266 cursor = 0;
267
268 skipWhiteSpace(textline, lineLength, &cursor);
269
270 memcpy(textChunkP->text + textChunkP->text_length,
271 textline + cursor,
272 lineLength - cursor);
273
274 textChunkP->text_length += lineLength - cursor;
275
276 textChunkP->text[textChunkP->text_length] = '\0'; /* for safety */
277 }
278
279
280
281 static void
282 getFileLine(FILE * const fileP,
283 const char ** const textP,
284 unsigned int * const lengthP) {
285 /*----------------------------------------------------------------------------
286 Read the next line (characters from current position through the first
287 newline character) and return it. Put the text in newly malloc'ed
288 storage.
289
290 Do not include the newline.
291
292 Add a terminating NUL for safety, but note that you can't rely on this
293 as the end of line marker because the line may contain a NUL. *lengthP
294 does not include the NUL that we add.
295
296 If there are no more characters in the file, return NULL.
297 -----------------------------------------------------------------------------*/
298 char * textline; /* malloc'ed */
299 unsigned int cursor; /* cursor into textline[] */
300 unsigned int allocatedSz;
301 /* The number of characters of space that are allocated for
302 'textline'
303 */
304 bool eol;
305 bool gotSomething;
306
307 allocatedSz = 128; /* initial value */
308
309 MALLOCARRAY(textline, allocatedSz);
310 if (textline == NULL)
311 pm_error("Unable to allocate buffer to read a line of a file.");
312
313 cursor = 0;
314 eol = FALSE;
315 gotSomething = FALSE;
316
317 while (!eol) {
318 int const c = getc(fileP);
319
320 if (c != EOF)
321 gotSomething = TRUE;
322
323 if (c == '\n' || c == EOF)
324 eol = TRUE;
325 else {
326 /* leave space for safety NUL */
327 if (cursor > allocatedSz - 1 - 1) {
328 allocatedSz *= 2;
329 REALLOCARRAY(textline, allocatedSz);
330 if (textline == NULL)
331 pm_error("Unable to allocate buffer to read a line of "
332 "a file.");
333 }
334 textline[cursor++] = c;
335 }
336 }
337 textline[cursor] = '\0'; /* For safety; not part of line */
338
339 if (gotSomething) {
340 *textP = textline;
341 *lengthP = cursor;
342 } else {
343 free(textline);
344 *textP = NULL;
345 }
346 }
347
348
349
350 static void
351 handleArrayAllocation(png_text ** const arrayP,
352 unsigned int * const allocatedChunkCtP,
353 unsigned int const chunkIdx) {
354
355 if (chunkIdx >= *allocatedChunkCtP) {
356 *allocatedChunkCtP *= 2;
357 REALLOCARRAY(*arrayP, *allocatedChunkCtP);
358 if (*arrayP == NULL)
359 pm_error("unable to allocate memory for %u text chunks",
360 *allocatedChunkCtP);
361 }
362 }
363
364
365
366 static bool
367 isContinuationLine(const char * const line) {
368 /*----------------------------------------------------------------------------
369 Text line 'line', if it is from a text string file, is a continuation of a
370 text string started in an earlier line.
371
372 What identifies a line as a continuation line is that it starts with
373 a space or tab.
374 -----------------------------------------------------------------------------*/
375 return line[0] == ' ' || line[0] == '\t';
376 }
377
378
379
380 static void
381 reportChunkCt(bool const ztxt,
382 bool const itxt,
383 unsigned int const chunkCt) {
384
385 const char * chunkType;
386
387 if (itxt)
388 chunkType = "iTXt";
389 else {
390 if (ztxt)
391 chunkType = "zTXt";
392 else
393 chunkType = "tEXt";
394 }
395
396 pm_message("Writing %u %s chunks", chunkCt, chunkType);
397 }
398
399
400
401 /******************************************************************************
402 EXTERNAL SUBROUTINES
403 ******************************************************************************/
404
405
406 void
407 pngtxt_addChunk(struct pngx * const pngxP,
408 FILE * const tfP,
409 bool const ztxt,
410 bool const itxt,
411 bool const verbose) {
412 /*----------------------------------------------------------------------------
413 Add text chunks (tEXt, zTXt, or iTXt) to the PNG image represented by
414 *pngxP as directed by file *tfP.
415
416 'itxt' means to make them international language (iTXt) chunks. Otherwise
417 they are either tEXt or zTXt chunks, depending upon 'ztxt'.
418
419 'ztxt' means to make the text compressed. If the chunks are not
420 international (i.e. 'itxt' is false), this means the chunks are zTXt chunks
421 instead of 'tEXt' chunks.
422 -----------------------------------------------------------------------------*/
423 bool noChunksYet;
424 bool eof;
425 png_textp text; /* An array; one chunk per element */
426 unsigned int chunkCt;
427 /* Number of chunks we have completed in the 'text' array */
428 unsigned int allocatedChunkCt;
429 /* Number of entries currently allocated for the PNG text array */
430
431 /* In an international text string file, the first entry tells the
432 language of all of the chunks, by having key 'Language'.
433 */
434
435 allocatedChunkCt = 256; /* initial value */
436
437 MALLOCARRAY(text, allocatedChunkCt);
438 if (text == NULL)
439 pm_error("unable to allocate memory for text chunk array");
440
441 for (chunkCt = 0, noChunksYet = true, eof = false; !eof; ) {
442 const char * textline;
443 unsigned int lineLength;
444
445 getFileLine(tfP, &textline, &lineLength);
446 if (textline == NULL)
447 eof = true;
448 else {
449 if (lineLength == 0) {
450 /* skip this empty line */
451 } else {
452 handleArrayAllocation(&text, &allocatedChunkCt, chunkCt);
453
454 if (!isContinuationLine(textline)) {
455 png_text * textChunkP;
456
457 if (noChunksYet) {
458 /* No previous chunk to move past */
459 } else
460 ++chunkCt;
461 noChunksYet = false;
462
463 textChunkP = &text[chunkCt];
464
465 if (itxt)
466 startTextChunkIntl(textChunkP,
467 textline, lineLength, ztxt,
468 verbose);
469 else
470 startTextChunkEngl(textChunkP,
471 textline, lineLength, ztxt,
472 verbose);
473 } else {
474 png_text * const textChunkP = &text[chunkCt];
475
476 /* Line starts with whitespace, which means it is
477 a continuation of the current text string.
478 */
479 if (noChunksYet)
480 pm_error("Invalid text string file format: "
481 "first line is a continuation line! "
482 "(It starts with whitespace)");
483 continueTextString(textChunkP, textline, lineLength);
484 }
485 }
486 pm_strfree(textline);
487 }
488 }
489 if (!noChunksYet)
490 ++chunkCt;
491
492 if (verbose)
493 reportChunkCt(ztxt, itxt, chunkCt);
494
495 if (chunkCt > 0)
496 pngx_setText(pngxP, text, chunkCt);
497
498 free(text);
499 }
500
501
502