about summary refs log tree commit diff
path: root/converter/other/pngtxt.c
blob: e02ee227e19ea1ca1d475fb21ba2b00b0db7d40a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#define HAVE_PNGLIB_WITH_ITXT 0

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <png.h>

#include "mallocvar.h"
#include "nstring.h"
#include "pngx.h"
#include "pngtxt.h"
#include "pm.h"



static void
readToken(char           const textline[],
          unsigned int   const lineLength,
          unsigned int * const cursorP,
          const char **  const tokenP) {
/*----------------------------------------------------------------------------
   Read a token from 'textline' (whose length is 'lineLength'), assuming the
   cursor is positioned to it now, leaving the cursor positioned after it.

   Tokens are delimited by white space.  We don't skip any white space before
   or after the token.  Ergo, if we are positioned to white space right now,
   the token we read is a null string.
-----------------------------------------------------------------------------*/
    char * tokenBuffer;
    char * cp;
    unsigned int cursor;

    cursor = *cursorP;
    
    MALLOCARRAY(tokenBuffer, lineLength + 1);
    /* leave room for terminating NUL */
    if (tokenBuffer == NULL) 
        pm_error("Unable to allocate memory for a %u-character "
                 "text string file line", lineLength);

    cp = &tokenBuffer[0];  /* initial value */
    
    if (textline[0] == '"') {
        ++cursor;  /* skip past opening quotation mark */
        while (textline[cursor] != '"') {
            if (cursor >= lineLength)
                pm_error("Invalid text string file format.  Line ends in "
                         "the middle of a quoted token.  Text at the end of "
                         "the line is '%s'", tokenBuffer);
            if (textline[cursor] == '\0')
                pm_error("Invalid text string file format:  Token contains "
                         "a NUL character.  Text leading up to the NUL "
                         "character is '%s'", tokenBuffer);
            *(cp++) = textline[cursor++];
        }
        ++cursor;  /* skip past closing quotation mark */
    } else {
        while ((cursor < lineLength) && 
               (textline[cursor] != ' ') && (textline[cursor] != '\t')) {
            
            if (textline[cursor] == '\0')
                pm_error("Invalid text string file format:  Token contains "
                         "a NUL character.  Text leading up to the NUL "
                         "character is '%s'", tokenBuffer);
            *(cp++) = textline[cursor++];
        }
    }
    *cp++ = '\0';

    *cursorP = cursor;

    *tokenP = tokenBuffer;
}




static void
skipWhiteSpace(char           const textline[],
               unsigned int   const lineLength,
               unsigned int * const cursorP) {
/*----------------------------------------------------------------------------
   Move *cursorP past white space (or, for some reason, NUL characters),
   in 'textline', which is 'lineLength' long.
-----------------------------------------------------------------------------*/
    unsigned int cursor;

    cursor = *cursorP;  /* initial value */

    while (cursor < lineLength && 
           (textline[cursor] == ' ' || textline[cursor] == '\t' ||
            textline[cursor] == '\0'))
        ++cursor;

    *cursorP = cursor;
}



static void
readTextString(char          const textline[],
               unsigned int  const lineLength,
               unsigned int  const startPos,
               png_charp *   const textStringP,
               png_size_t *  const textStringLengthP) {
/*----------------------------------------------------------------------------
  Extract the text string at 'startPos' in the buffer 'textline', whose
  length is 'lineLength'.  Return it in newly malloced storage with a
  pointer to that storage as 'textString' and the size of the text as
  *textStringLengthP.
-----------------------------------------------------------------------------*/
    char * cp;

    MALLOCARRAY(cp, lineLength + 1);  /* incl '\0' */
    if (!cp) 
        pm_error("Unable to allocate memory for text chunks");

    memcpy(cp, textline + startPos, lineLength - startPos);
    cp[lineLength - startPos] = '\0';  /* for safety - not part of text */
    *textStringP = cp;
    *textStringLengthP = lineLength - startPos;
}



static void
startTextChunkEngl(png_text *   const textChunkP, 
                   char         const textline[],
                   unsigned int const lineLength,
                   bool         const isCompressed,
                   bool         const verbose) {
/*----------------------------------------------------------------------------
   Assuming 'textline' is the first line of an entry in an English text
   string file, put the information from it in the comment record *textChunkP.
   Use the text on this line as the comment text, even though the true text
   string may include text from subsequent continuation lines as well.

   'textline' is not NUL-terminated.  Its length is 'lineLength', and it is at
   least one character long.  'textline' does not contain a newline character.

   'isCompressed' means it is a compressed text chunk.
-----------------------------------------------------------------------------*/
    unsigned int cursor;

    cursor = 0;

    {
        const char * key;

        readToken(textline, lineLength, &cursor, &key);

        pngx_setTextKey(textChunkP, key);

        pm_strfree(key);
    }

    skipWhiteSpace(textline, lineLength, &cursor);

    pngx_setTextLang(textChunkP, NULL);

    readTextString(textline, lineLength, cursor, &textChunkP->text,
                   &textChunkP->text_length);

    textChunkP->compression =
        isCompressed ? PNG_TEXT_COMPRESSION_zTXt : PNG_TEXT_COMPRESSION_NONE;
}



static void
startTextChunkIntl(png_text *   const textChunkP, 
                   char         const textline[],
                   unsigned int const lineLength,
                   bool         const isCompressed,
                   bool         const verbose) {
/*----------------------------------------------------------------------------
  Assuming 'textline' is the first line of an entry in an international (not
  English) text string file, put the information from it in the text chunk
  *textChunkP.  Use the text on this line as the text string, even though the
  true text string may include text from subsequent continuation lines as
  well.

  'textline' is not NUL-terminated.  Its length is 'lineLength', and it is at
  least one character long.  'textline' does not contain a newline character.

  'isInternational' means it is an international (i.e. non-English) text
  chunk.

  Leave the language attribute (textChunkP->lang) unset.
-----------------------------------------------------------------------------*/
    unsigned int cursor;

    cursor = 0;  /* Initial value */

    {
        const char * key;

        readToken(textline, lineLength, &cursor, &key);

        pngx_setTextKey(textChunkP, key);

        pm_strfree(key);
    }

    skipWhiteSpace(textline, lineLength, &cursor);

    {
        const char * isoLang;

        readToken(textline, lineLength, &cursor, &isoLang);

        pngx_setTextLang(textChunkP, isoLang);

        pm_strfree(isoLang);
    }

    skipWhiteSpace(textline, lineLength, &cursor);

    {
        const char * langKey;
    
        readToken(textline, lineLength, &cursor, &langKey);

        pngx_setTextLangKey(textChunkP, langKey);

        pm_strfree(langKey);
    }

    skipWhiteSpace(textline, lineLength, &cursor);

    /* Beginning of text string (continuation lines may follow) */
    readTextString(textline, lineLength, cursor, &textChunkP->text,
                   &textChunkP->text_length);

    textChunkP->compression = 
        isCompressed ? PNG_ITXT_COMPRESSION_zTXt :PNG_ITXT_COMPRESSION_NONE;
}



static void
continueTextString(png_text *   const textChunkP, 
                   char         const textline[],
                   unsigned int const lineLength) {
/*----------------------------------------------------------------------------
   Update the text chunk *textChunkP by adding to it the text from
   textline[], which is a continuation line from a text string file.

   'textline' is not NUL-terminated.  Its length is 'lineLength', and
   it is at least one character long.  'textline' does not contain a
   newline character.
-----------------------------------------------------------------------------*/
    unsigned int cursor;  /* cursor into textline[] */

    unsigned int const newTextLength =
        textChunkP->text_length + lineLength + 1 + 1;

    REALLOCARRAY(textChunkP->text, newTextLength);

    if (textChunkP->text == NULL)
        pm_error("Unable to allocate %u bytes of memory for text string",
                 newTextLength);

    textChunkP->text[textChunkP->text_length++] = '\n';

    cursor = 0; 

    skipWhiteSpace(textline, lineLength, &cursor);

    memcpy(textChunkP->text + textChunkP->text_length,
           textline + cursor,
           lineLength - cursor);

    textChunkP->text_length += lineLength - cursor;

    textChunkP->text[textChunkP->text_length] = '\0';  /* for safety */
}



static void
getFileLine(FILE *         const fileP, 
            const char **  const textP, 
            unsigned int * const lengthP) {
/*----------------------------------------------------------------------------
   Read the next line (characters from current position through the first
   newline character) and return it.  Put the text in newly malloc'ed 
   storage.

   Do not include the newline.

   Add a terminating NUL for safety, but note that you can't rely on this
   as the end of line marker because the line may contain a NUL.  *lengthP
   does not include the NUL that we add.

   If there are no more characters in the file, return NULL.
-----------------------------------------------------------------------------*/
    char * textline;  /* malloc'ed */
    unsigned int cursor;  /* cursor into textline[] */
    unsigned int allocatedSz;
        /* The number of characters of space that are allocated for
           'textline' 
        */
    bool eol;
    bool gotSomething;

    allocatedSz = 128;  /* initial value */

    MALLOCARRAY(textline, allocatedSz);
    if (textline == NULL)
        pm_error("Unable to allocate buffer to read a line of a file.");
    
    cursor = 0;
    eol = FALSE;
    gotSomething = FALSE;

    while (!eol) {
        int const c = getc(fileP);
        
        if (c != EOF)
            gotSomething = TRUE;

        if (c == '\n' || c == EOF)
            eol = TRUE;
        else {
            /* leave space for safety NUL */
            if (cursor > allocatedSz - 1 - 1) {
                allocatedSz *= 2;
                REALLOCARRAY(textline, allocatedSz);
                if (textline == NULL)
                    pm_error("Unable to allocate buffer to read a line of "
                             "a file.");
            }
            textline[cursor++] = c;
        }
    }
    textline[cursor] = '\0';  /* For safety; not part of line */

    if (gotSomething) {
        *textP = textline;
        *lengthP = cursor;
    } else {
        free(textline);
        *textP = NULL;
    }
}



static void
handleArrayAllocation(png_text **    const arrayP,
                      unsigned int * const allocatedChunkCtP,
                      unsigned int   const chunkIdx) {

    if (chunkIdx >= *allocatedChunkCtP) {
        *allocatedChunkCtP *= 2;
        REALLOCARRAY(*arrayP, *allocatedChunkCtP);
        if (*arrayP == NULL) 
            pm_error("unable to allocate memory for %u text chunks",
                *allocatedChunkCtP);
    }
}



static bool
isContinuationLine(const char * const line) {
/*----------------------------------------------------------------------------
   Text line 'line', if it is from a text string file, is a continuation of a
   text string started in an earlier line.

   What identifies a line as a continuation line is that it starts with
   a space or tab.
-----------------------------------------------------------------------------*/
    return line[0] == ' ' || line[0] == '\t';
}



static void
reportChunkCt(bool         const ztxt,
              bool         const itxt,
              unsigned int const chunkCt) {

    const char * chunkType;

    if (itxt)
        chunkType = "iTXt";
    else {
        if (ztxt)
            chunkType = "zTXt";
        else
            chunkType = "tEXt";
    }

    pm_message("Writing %u %s chunks", chunkCt, chunkType);
}



/******************************************************************************
                            EXTERNAL SUBROUTINES
******************************************************************************/


void 
pngtxt_addChunk(struct pngx * const pngxP,
                FILE *        const tfP, 
                bool          const ztxt,
                bool          const itxt,
                bool          const verbose) {
/*----------------------------------------------------------------------------
   Add text chunks (tEXt, zTXt, or iTXt) to the PNG image represented by
   *pngxP as directed by file *tfP.

   'itxt' means to make them international language (iTXt) chunks.  Otherwise
   they are either tEXt or zTXt chunks, depending upon 'ztxt'.

   'ztxt' means to make the text compressed.  If the chunks are not
   international (i.e. 'itxt' is false), this means the chunks are zTXt chunks
   instead of 'tEXt' chunks.
-----------------------------------------------------------------------------*/
    bool noChunksYet;
    bool eof;
    png_textp text;  /* An array; one chunk per element */
    unsigned int chunkCt;
        /* Number of chunks we have completed in the 'text' array */
    unsigned int allocatedChunkCt;
        /* Number of entries currently allocated for the PNG text array */

    /* In an international text string file, the first entry tells the
       language of all of the chunks, by having key 'Language'.
    */

    allocatedChunkCt = 256;  /* initial value */

    MALLOCARRAY(text, allocatedChunkCt);
    if (text == NULL) 
        pm_error("unable to allocate memory for text chunk array");

    for (chunkCt = 0, noChunksYet = true, eof = false; !eof; ) {
        const char * textline;
        unsigned int lineLength;

        getFileLine(tfP, &textline, &lineLength);
        if (textline == NULL)
            eof = true;
        else {
            if (lineLength == 0) {
                /* skip this empty line */
            } else {
                handleArrayAllocation(&text, &allocatedChunkCt, chunkCt);

                if (!isContinuationLine(textline)) {
                    png_text * textChunkP;

                    if (noChunksYet) {
                        /* No previous chunk to move past */
                    } else
                        ++chunkCt;
                    noChunksYet = false;
                    
                    textChunkP = &text[chunkCt];
                
                    if (itxt)
                        startTextChunkIntl(textChunkP, 
                                           textline, lineLength, ztxt,
                                           verbose);
                    else
                        startTextChunkEngl(textChunkP, 
                                           textline, lineLength, ztxt,
                                           verbose);
                } else {
                    png_text * const textChunkP = &text[chunkCt];

                    /* Line starts with whitespace, which means it is
                       a continuation of the current text string.
                    */
                    if (noChunksYet)
                        pm_error("Invalid text string file format: "
                                 "first line is a continuation line! "
                                 "(It starts with whitespace)");
                    continueTextString(textChunkP, textline, lineLength);
                }
            }
            pm_strfree(textline);
        }
    }
    if (!noChunksYet)
        ++chunkCt;

    if (verbose)
        reportChunkCt(ztxt, itxt, chunkCt);

    if (chunkCt > 0)
        pngx_setText(pngxP, text, chunkCt);

    free(text);
}