Browse code

tiff: return meaningful error codes.

Anton Khirnov authored on 2012/11/17 21:40:37
Showing 1 changed files
... ...
@@ -112,7 +112,7 @@ static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
112 112
 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
113 113
                              const uint8_t *src, int size, int lines)
114 114
 {
115
-    int c, line, pixels, code;
115
+    int c, line, pixels, code, ret;
116 116
     const uint8_t *ssrc = src;
117 117
     int width = ((s->width * s->bpp) + 7) >> 3;
118 118
 
... ...
@@ -134,7 +134,7 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
134 134
                    "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
135 135
                    (unsigned long)width * lines, ret);
136 136
             av_free(zbuf);
137
-            return -1;
137
+            return AVERROR_UNKNOWN;
138 138
         }
139 139
         src = zbuf;
140 140
         for (line = 0; line < lines; line++) {
... ...
@@ -147,9 +147,9 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
147 147
     }
148 148
 #endif
149 149
     if (s->compr == TIFF_LZW) {
150
-        if (ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0) {
150
+        if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
151 151
             av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
152
-            return -1;
152
+            return ret;
153 153
         }
154 154
     }
155 155
     if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
... ...
@@ -167,7 +167,7 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
167 167
             av_log(s->avctx, AV_LOG_ERROR,
168 168
                    "Uncompressed fax mode is not supported (yet)\n");
169 169
             av_free(src2);
170
-            return -1;
170
+            return AVERROR_INVALIDDATA;
171 171
         }
172 172
         if (!s->fill_order) {
173 173
             memcpy(src2, src, size);
... ...
@@ -190,7 +190,7 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
190 190
     for (line = 0; line < lines; line++) {
191 191
         if (src - ssrc > size) {
192 192
             av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
193
-            return -1;
193
+            return AVERROR_INVALIDDATA;
194 194
         }
195 195
         switch (s->compr) {
196 196
         case TIFF_RAW:
... ...
@@ -213,7 +213,7 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
213 213
                     if (pixels + code > width) {
214 214
                         av_log(s->avctx, AV_LOG_ERROR,
215 215
                                "Copy went out of bounds\n");
216
-                        return -1;
216
+                        return AVERROR_INVALIDDATA;
217 217
                     }
218 218
                     memcpy(dst + pixels, src, code);
219 219
                     src += code;
... ...
@@ -223,7 +223,7 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
223 223
                     if (pixels + code > width) {
224 224
                         av_log(s->avctx, AV_LOG_ERROR,
225 225
                                "Run went out of bounds\n");
226
-                        return -1;
226
+                        return AVERROR_INVALIDDATA;
227 227
                     }
228 228
                     c = *src++;
229 229
                     memset(dst + pixels, c, code);
... ...
@@ -236,7 +236,7 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
236 236
             if (pixels < width) {
237 237
                 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
238 238
                        pixels, width);
239
-                return -1;
239
+                return AVERROR_INVALIDDATA;
240 240
             }
241 241
             break;
242 242
         }
... ...
@@ -308,7 +308,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
308 308
     const uint8_t *rp, *gp, *bp;
309 309
 
310 310
     if (end_buf - buf < 12)
311
-        return -1;
311
+        return AVERROR_INVALIDDATA;
312 312
     tag = tget_short(&buf, s->le);
313 313
     type = tget_short(&buf, s->le);
314 314
     count = tget_long(&buf, s->le);
... ...
@@ -352,7 +352,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
352 352
     if (buf && (buf < start || buf > end_buf)) {
353 353
         av_log(s->avctx, AV_LOG_ERROR,
354 354
                "Tag referencing position outside the image\n");
355
-        return -1;
355
+        return AVERROR_INVALIDDATA;
356 356
     }
357 357
 
358 358
     switch (tag) {
... ...
@@ -368,7 +368,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
368 368
             av_log(s->avctx, AV_LOG_ERROR,
369 369
                    "This format is not supported (bpp=%d, %d components)\n",
370 370
                    s->bpp, count);
371
-            return -1;
371
+            return AVERROR_INVALIDDATA;
372 372
         }
373 373
         if (count == 1)
374 374
             s->bpp = value;
... ...
@@ -418,17 +418,17 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
418 418
             break;
419 419
 #else
420 420
             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
421
-            return -1;
421
+            return AVERROR(ENOSYS);
422 422
 #endif
423 423
         case TIFF_JPEG:
424 424
         case TIFF_NEWJPEG:
425 425
             av_log(s->avctx, AV_LOG_ERROR,
426 426
                    "JPEG compression is not supported\n");
427
-            return -1;
427
+            return AVERROR_PATCHWELCOME;
428 428
         default:
429 429
             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
430 430
                    s->compr);
431
-            return -1;
431
+            return AVERROR_INVALIDDATA;
432 432
         }
433 433
         break;
434 434
     case TIFF_ROWSPERSTRIP:
... ...
@@ -437,7 +437,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
437 437
         if (value < 1) {
438 438
             av_log(s->avctx, AV_LOG_ERROR,
439 439
                    "Incorrect value of rows per strip\n");
440
-            return -1;
440
+            return AVERROR_INVALIDDATA;
441 441
         }
442 442
         s->rps = value;
443 443
         break;
... ...
@@ -454,7 +454,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
454 454
         if (s->stripdata > end_buf) {
455 455
             av_log(s->avctx, AV_LOG_ERROR,
456 456
                    "Tag referencing position outside the image\n");
457
-            return -1;
457
+            return AVERROR_INVALIDDATA;
458 458
         }
459 459
         break;
460 460
     case TIFF_STRIP_SIZE:
... ...
@@ -470,7 +470,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
470 470
         if (s->stripsizes > end_buf) {
471 471
             av_log(s->avctx, AV_LOG_ERROR,
472 472
                    "Tag referencing position outside the image\n");
473
-            return -1;
473
+            return AVERROR_INVALIDDATA;
474 474
         }
475 475
         break;
476 476
     case TIFF_PREDICTOR:
... ...
@@ -490,7 +490,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
490 490
         default:
491 491
             av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
492 492
                    value);
493
-            return -1;
493
+            return AVERROR_INVALIDDATA;
494 494
         }
495 495
         break;
496 496
     case TIFF_FILL_ORDER:
... ...
@@ -505,7 +505,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
505 505
         pal = (uint32_t *) s->palette;
506 506
         off = type_sizes[type];
507 507
         if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
508
-            return -1;
508
+            return AVERROR_INVALIDDATA;
509 509
         rp = buf;
510 510
         gp = buf + count / 3 * off;
511 511
         bp = buf + count / 3 * off * 2;
... ...
@@ -521,7 +521,7 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
521 521
     case TIFF_PLANAR:
522 522
         if (value == 2) {
523 523
             av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
524
-            return -1;
524
+            return AVERROR_PATCHWELCOME;
525 525
         }
526 526
         break;
527 527
     case TIFF_T4OPTIONS:
... ...
@@ -566,7 +566,7 @@ static int decode_frame(AVCodecContext *avctx,
566 566
         le = 0;
567 567
     else {
568 568
         av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
569
-        return -1;
569
+        return AVERROR_INVALIDDATA;
570 570
     }
571 571
     s->le = le;
572 572
     s->invert = 0;
... ...
@@ -577,7 +577,7 @@ static int decode_frame(AVCodecContext *avctx,
577 577
     if (tget_short(&buf, le) != 42) {
578 578
         av_log(avctx, AV_LOG_ERROR,
579 579
                "The answer to life, universe and everything is not correct!\n");
580
-        return -1;
580
+        return AVERROR_INVALIDDATA;
581 581
     }
582 582
     // Reset these pointers so we can tell if they were set this frame
583 583
     s->stripsizes = s->stripdata = NULL;
... ...
@@ -590,13 +590,13 @@ static int decode_frame(AVCodecContext *avctx,
590 590
     buf = orig_buf + off;
591 591
     entries = tget_short(&buf, le);
592 592
     for (i = 0; i < entries; i++) {
593
-        if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
594
-            return -1;
593
+        if ((ret = tiff_decode_tag(s, orig_buf, buf, end_buf)) < 0)
594
+            return ret;
595 595
         buf += 12;
596 596
     }
597 597
     if (!s->stripdata && !s->stripoff) {
598 598
         av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
599
-        return -1;
599
+        return AVERROR_INVALIDDATA;
600 600
     }
601 601
     /* now we have the data and may start decoding */
602 602
     if ((ret = init_image(s)) < 0)
... ...
@@ -625,7 +625,7 @@ static int decode_frame(AVCodecContext *avctx,
625 625
 
626 626
         if (soff > buf_size || ssize > buf_size - soff) {
627 627
             av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
628
-            return -1;
628
+            return AVERROR_INVALIDDATA;
629 629
         }
630 630
         if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
631 631
                               FFMIN(s->rps, s->height - i)) < 0)