aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/png_extractor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/png_extractor.c')
-rw-r--r--src/plugins/png_extractor.c225
1 files changed, 118 insertions, 107 deletions
diff --git a/src/plugins/png_extractor.c b/src/plugins/png_extractor.c
index add1fde..fec6c17 100644
--- a/src/plugins/png_extractor.c
+++ b/src/plugins/png_extractor.c
@@ -43,7 +43,7 @@
43 */ 43 */
44static char * 44static char *
45stndup (const char *str, 45stndup (const char *str,
46 size_t n) 46 size_t n)
47{ 47{
48 char *tmp; 48 char *tmp;
49 49
@@ -67,13 +67,13 @@ stndup (const char *str,
67 */ 67 */
68static size_t 68static size_t
69stnlen (const char *str, 69stnlen (const char *str,
70 size_t maxlen) 70 size_t maxlen)
71{ 71{
72 size_t ret; 72 size_t ret;
73 73
74 ret = 0; 74 ret = 0;
75 while ( (ret < maxlen) && 75 while ( (ret < maxlen) &&
76 ('\0' != str[ret]) ) 76 ('\0' != str[ret]) )
77 ret++; 77 ret++;
78 return ret; 78 return ret;
79} 79}
@@ -111,8 +111,7 @@ static struct
111 * Corresponding LE type. 111 * Corresponding LE type.
112 */ 112 */
113 enum EXTRACTOR_MetaType type; 113 enum EXTRACTOR_MetaType type;
114} tagmap[] = 114} tagmap[] = {
115{
116 { "Author", EXTRACTOR_METATYPE_AUTHOR_NAME }, 115 { "Author", EXTRACTOR_METATYPE_AUTHOR_NAME },
117 { "Description", EXTRACTOR_METATYPE_DESCRIPTION }, 116 { "Description", EXTRACTOR_METATYPE_DESCRIPTION },
118 { "Comment", EXTRACTOR_METATYPE_COMMENT }, 117 { "Comment", EXTRACTOR_METATYPE_COMMENT },
@@ -135,7 +134,11 @@ static struct
135 * @param t type of the metadata 134 * @param t type of the metadata
136 * @param s utf8 string with the metadata 135 * @param s utf8 string with the metadata
137 */ 136 */
138#define ADD(t,s) do { if (0 != (ret = ec->proc (ec->cls, "png", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen (s) + 1))) goto FINISH; } while (0) 137#define ADD(t,s) do { if (0 != (ret = ec->proc (ec->cls, "png", t, \
138 EXTRACTOR_METAFORMAT_UTF8, \
139 "text/plain", s, strlen (s) \
140 + 1))) goto FINISH; \
141} while (0)
139 142
140 143
141/** 144/**
@@ -145,7 +148,14 @@ static struct
145 * @param t type of the metadata 148 * @param t type of the metadata
146 * @param s utf8 string with the metadata, to be freed afterwards 149 * @param s utf8 string with the metadata, to be freed afterwards
147 */ 150 */
148#define ADDF(t,s) do { if ( (NULL != s) && (0 != (ret = ec->proc (ec->cls, "png", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen (s) + 1))) ) { free (s); goto FINISH; } if (NULL != s) free (s); } while (0) 151#define ADDF(t,s) do { if ( (NULL != s) && (0 != (ret = ec->proc (ec->cls, \
152 "png", t, \
153 EXTRACTOR_METAFORMAT_UTF8, \
154 "text/plain", \
155 s, strlen (s) \
156 + 1))) ) { \
157 free (s); goto FINISH; } if (NULL != s) free (s); \
158} while (0)
149 159
150 160
151/** 161/**
@@ -173,16 +183,16 @@ processtEXt (struct EXTRACTOR_ExtractContext *ec,
173 if (off >= length) 183 if (off >= length)
174 return 0; /* failed to find '\0' */ 184 return 0; /* failed to find '\0' */
175 if (NULL == (keyword = EXTRACTOR_common_convert_to_utf8 ((char*) &data[off], 185 if (NULL == (keyword = EXTRACTOR_common_convert_to_utf8 ((char*) &data[off],
176 length - off, 186 length - off,
177 "ISO-8859-1"))) 187 "ISO-8859-1")))
178 return 0; 188 return 0;
179 ret = 0; 189 ret = 0;
180 for (i = 0; NULL != tagmap[i].name; i++) 190 for (i = 0; NULL != tagmap[i].name; i++)
181 if (0 == strcmp (tagmap[i].name, (char*) data)) 191 if (0 == strcmp (tagmap[i].name, (char*) data))
182 { 192 {
183 ADDF (tagmap[i].type, keyword); 193 ADDF (tagmap[i].type, keyword);
184 return 0; 194 return 0;
185 } 195 }
186 ADDF (EXTRACTOR_METATYPE_KEYWORDS, keyword); 196 ADDF (EXTRACTOR_METATYPE_KEYWORDS, keyword);
187FINISH: 197FINISH:
188 return ret; 198 return ret;
@@ -242,50 +252,50 @@ processiTXt (struct EXTRACTOR_ExtractContext *ec,
242 return 0; 252 return 0;
243 253
244 if (compressed) 254 if (compressed)
255 {
256 bufLen = 1024 + 2 * (length - pos);
257 while (1)
245 { 258 {
246 bufLen = 1024 + 2 * (length - pos); 259 if (bufLen * 2 < bufLen)
247 while (1) 260 return 0;
248 { 261 bufLen *= 2;
249 if (bufLen * 2 < bufLen) 262 if (bufLen > 50 * (length - pos))
250 return 0; 263 {
251 bufLen *= 2; 264 /* printf("zlib problem"); */
252 if (bufLen > 50 * (length - pos)) 265 return 0;
253 { 266 }
254 /* printf("zlib problem"); */ 267 if (NULL == (buf = malloc (bufLen)))
255 return 0; 268 {
256 } 269 /* printf("out of memory"); */
257 if (NULL == (buf = malloc (bufLen))) 270 return 0; /* out of memory */
258 { 271 }
259 /* printf("out of memory"); */ 272 if (Z_OK ==
260 return 0; /* out of memory */ 273 (zret = uncompress ((Bytef *) buf,
261 } 274 &bufLen,
262 if (Z_OK == 275 (const Bytef *) &data[pos], length - pos)))
263 (zret = uncompress ((Bytef *) buf, 276 {
264 &bufLen, 277 /* printf("zlib ok"); */
265 (const Bytef *) &data[pos], length - pos))) 278 break;
266 { 279 }
267 /* printf("zlib ok"); */
268 break;
269 }
270 free (buf);
271 if (Z_BUF_ERROR != zret)
272 return 0; /* unknown error, abort */
273 }
274 keyword = stndup (buf, bufLen);
275 free (buf); 280 free (buf);
281 if (Z_BUF_ERROR != zret)
282 return 0; /* unknown error, abort */
276 } 283 }
284 keyword = stndup (buf, bufLen);
285 free (buf);
286 }
277 else 287 else
278 { 288 {
279 keyword = stndup ((char *) &data[pos], length - pos); 289 keyword = stndup ((char *) &data[pos], length - pos);
280 } 290 }
281 if (NULL == keyword) 291 if (NULL == keyword)
282 return ret; 292 return ret;
283 for (i = 0; NULL != tagmap[i].name; i++) 293 for (i = 0; NULL != tagmap[i].name; i++)
284 if (0 == strcmp (tagmap[i].name, (char*) data)) 294 if (0 == strcmp (tagmap[i].name, (char*) data))
285 { 295 {
286 ADDF (tagmap[i].type, keyword /* already in utf8 */); 296 ADDF (tagmap[i].type, keyword /* already in utf8 */);
287 return 0; 297 return 0;
288 } 298 }
289 ADDF (EXTRACTOR_METATYPE_COMMENT, keyword); 299 ADDF (EXTRACTOR_METATYPE_COMMENT, keyword);
290FINISH: 300FINISH:
291 return ret; 301 return ret;
@@ -357,43 +367,43 @@ processzTXt (struct EXTRACTOR_ExtractContext *ec,
357 ret = 0; 367 ret = 0;
358 bufLen = 1024 + 2 * (length - off); 368 bufLen = 1024 + 2 * (length - off);
359 while (1) 369 while (1)
370 {
371 if (bufLen * 2 < bufLen)
372 return 0;
373 bufLen *= 2;
374 if (bufLen > 50 * (length - off))
360 { 375 {
361 if (bufLen * 2 < bufLen) 376 /* printf("zlib problem"); */
362 return 0; 377 return 0;
363 bufLen *= 2; 378 }
364 if (bufLen > 50 * (length - off)) 379 if (NULL == (buf = malloc (bufLen)))
365 { 380 {
366 /* printf("zlib problem"); */ 381 /* printf("out of memory"); */
367 return 0; 382 return 0; /* out of memory */
368 } 383 }
369 if (NULL == (buf = malloc (bufLen))) 384 if (Z_OK ==
370 { 385 (zret = uncompress ((Bytef *) buf,
371 /* printf("out of memory"); */ 386 &bufLen,
372 return 0; /* out of memory */ 387 (const Bytef *) &data[off],
373 } 388 length - off)))
374 if (Z_OK == 389 {
375 (zret = uncompress ((Bytef *) buf, 390 /* printf("zlib ok"); */
376 &bufLen, 391 break;
377 (const Bytef *) &data[off],
378 length - off)))
379 {
380 /* printf("zlib ok"); */
381 break;
382 }
383 free (buf);
384 if (Z_BUF_ERROR != zret)
385 return 0; /* unknown error, abort */
386 } 392 }
393 free (buf);
394 if (Z_BUF_ERROR != zret)
395 return 0; /* unknown error, abort */
396 }
387 keyword = EXTRACTOR_common_convert_to_utf8 (buf, 397 keyword = EXTRACTOR_common_convert_to_utf8 (buf,
388 bufLen, 398 bufLen,
389 "ISO-8859-1"); 399 "ISO-8859-1");
390 free (buf); 400 free (buf);
391 for (i = 0; NULL != tagmap[i].name; i++) 401 for (i = 0; NULL != tagmap[i].name; i++)
392 if (0 == strcmp (tagmap[i].name, (char*) data)) 402 if (0 == strcmp (tagmap[i].name, (char*) data))
393 { 403 {
394 ADDF (tagmap[i].type, keyword); 404 ADDF (tagmap[i].type, keyword);
395 return 0; 405 return 0;
396 } 406 }
397 ADDF (EXTRACTOR_METATYPE_COMMENT, keyword); 407 ADDF (EXTRACTOR_METATYPE_COMMENT, keyword);
398FINISH: 408FINISH:
399 return ret; 409 return ret;
@@ -437,9 +447,9 @@ processtIME (struct EXTRACTOR_ExtractContext *ec,
437 m = (unsigned char) data[9]; 447 m = (unsigned char) data[9];
438 s = (unsigned char) data[10]; 448 s = (unsigned char) data[10];
439 snprintf (val, 449 snprintf (val,
440 sizeof (val), 450 sizeof (val),
441 "%04u-%02u-%02u %02d:%02d:%02d", 451 "%04u-%02u-%02u %02d:%02d:%02d",
442 year, mo, day, h, m, s); 452 year, mo, day, h, m, s);
443 ADD (EXTRACTOR_METATYPE_MODIFICATION_DATE, val); 453 ADD (EXTRACTOR_METATYPE_MODIFICATION_DATE, val);
444FINISH: 454FINISH:
445 return ret; 455 return ret;
@@ -468,32 +478,33 @@ EXTRACTOR_png_extract_method (struct EXTRACTOR_ExtractContext *ec)
468 ADD (EXTRACTOR_METATYPE_MIMETYPE, "image/png"); 478 ADD (EXTRACTOR_METATYPE_MIMETYPE, "image/png");
469 ret = 0; 479 ret = 0;
470 while (0 == ret) 480 while (0 == ret)
471 { 481 {
472 if (sizeof (uint32_t) + 4 != ec->read (ec->cls, 482 if (sizeof (uint32_t) + 4 != ec->read (ec->cls,
473 &data, 483 &data,
474 sizeof (uint32_t) + 4)) 484 sizeof (uint32_t) + 4))
475 break; 485 break;
476 length = get_int_at (data); 486 length = get_int_at (data);
477 if (0 > (pos = ec->seek (ec->cls, 0, SEEK_CUR))) 487 if (0 > (pos = ec->seek (ec->cls, 0, SEEK_CUR)))
478 break; 488 break;
479 pos += length + 4; /* Chunk type, data, crc */ 489 pos += length + 4; /* Chunk type, data, crc */
480 if (0 == strncmp ((char*) data + sizeof (uint32_t), "IHDR", 4)) 490 if (0 == strncmp ((char*) data + sizeof (uint32_t), "IHDR", 4))
481 ret = processIHDR (ec, length); 491 ret = processIHDR (ec, length);
482 if (0 == strncmp ((char*) data + sizeof (uint32_t), "iTXt", 4)) 492 if (0 == strncmp ((char*) data + sizeof (uint32_t), "iTXt", 4))
483 ret = processiTXt (ec, length); 493 ret = processiTXt (ec, length);
484 if (0 == strncmp ((char*) data + sizeof (uint32_t), "tEXt", 4)) 494 if (0 == strncmp ((char*) data + sizeof (uint32_t), "tEXt", 4))
485 ret = processtEXt (ec, length); 495 ret = processtEXt (ec, length);
486 if (0 == strncmp ((char*) data + sizeof (uint32_t), "zTXt", 4)) 496 if (0 == strncmp ((char*) data + sizeof (uint32_t), "zTXt", 4))
487 ret = processzTXt (ec, length); 497 ret = processzTXt (ec, length);
488 if (0 == strncmp ((char*) data + sizeof (uint32_t), "tIME", 4)) 498 if (0 == strncmp ((char*) data + sizeof (uint32_t), "tIME", 4))
489 ret = processtIME (ec, length); 499 ret = processtIME (ec, length);
490 if (ret != 0) 500 if (ret != 0)
491 break; 501 break;
492 if (pos != ec->seek (ec->cls, pos, SEEK_SET)) 502 if (pos != ec->seek (ec->cls, pos, SEEK_SET))
493 break; 503 break;
494 } 504 }
495FINISH: 505FINISH:
496 return; 506 return;
497} 507}
498 508
509
499/* end of png_extractor.c */ 510/* end of png_extractor.c */