| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
char * tokenBuffer; |
| 31 |
char * cp; |
| 32 |
unsigned int cursor; |
| 33 |
|
| 34 |
cursor = *cursorP; |
| 35 |
|
| 36 |
MALLOCARRAY(tokenBuffer, lineLength + 1); |
| 37 |
|
| 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]; |
| 43 |
|
| 44 |
if (textline[0] == '"') { |
| 45 |
++cursor; |
| 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; |
| 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 |
|
| 84 |
|
| 85 |
|
| 86 |
unsigned int cursor; |
| 87 |
|
| 88 |
cursor = *cursorP; |
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
char * cp; |
| 113 |
|
| 114 |
MALLOCARRAY(cp, lineLength + 1); |
| 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'; |
| 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 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
unsigned int cursor; |
| 192 |
|
| 193 |
cursor = 0; |
| 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 |
|
| 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 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
unsigned int cursor; |
| 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'; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
static void |
| 282 |
getFileLine(FILE * const fileP, |
| 283 |
const char ** const textP, |
| 284 |
unsigned int * const lengthP) { |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
char * textline; |
| 299 |
unsigned int cursor; |
| 300 |
unsigned int allocatedSz; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
bool eol; |
| 305 |
bool gotSomething; |
| 306 |
|
| 307 |
allocatedSz = 128; |
| 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 |
|
| 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'; |
| 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 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 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 |
|
| 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 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
bool noChunksYet; |
| 424 |
bool eof; |
| 425 |
png_textp text; |
| 426 |
unsigned int chunkCt; |
| 427 |
|
| 428 |
unsigned int allocatedChunkCt; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
allocatedChunkCt = 256; |
| 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 |
|
| 451 |
} else { |
| 452 |
handleArrayAllocation(&text, &allocatedChunkCt, chunkCt); |
| 453 |
|
| 454 |
if (!isContinuationLine(textline)) { |
| 455 |
png_text * textChunkP; |
| 456 |
|
| 457 |
if (noChunksYet) { |
| 458 |
|
| 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 |
|
| 477 |
|
| 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 |
|