static int tiff_decode_tag(TiffContext *s)

{

    unsigned tag, type, count, off, value = 0;

    int i, j, k, pos, start;

    int ret;

    uint32_t *pal;

    double *dp;



    tag   = tget_short(&s->gb, s->le);

    type  = tget_short(&s->gb, s->le);

    count = tget_long(&s->gb, s->le);

    off   = tget_long(&s->gb, s->le);

    start = bytestream2_tell(&s->gb);



    if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {

        av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",

               type);

        return 0;

    }



    if (count == 1) {

        switch (type) {

        case TIFF_BYTE:

        case TIFF_SHORT:

            bytestream2_seek(&s->gb, -4, SEEK_CUR);

            value = tget(&s->gb, type, s->le);

            break;

        case TIFF_LONG:

            value = off;

            break;

        case TIFF_STRING:

            if (count <= 4) {

                bytestream2_seek(&s->gb, -4, SEEK_CUR);

                break;

            }

        default:

            value = UINT_MAX;

            bytestream2_seek(&s->gb, off, SEEK_SET);

        }

    } else {

        if (count <= 4 && type_sizes[type] * count <= 4) {

            bytestream2_seek(&s->gb, -4, SEEK_CUR);

        } else {

            bytestream2_seek(&s->gb, off, SEEK_SET);

        }

    }



    switch (tag) {

    case TIFF_WIDTH:

        s->width = value;

        break;

    case TIFF_HEIGHT:

        s->height = value;

        break;

    case TIFF_BPP:

        s->bppcount = count;

        if (count > 4) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "This format is not supported (bpp=%d, %d components)\n",

                   s->bpp, count);

            return AVERROR_INVALIDDATA;

        }

        if (count == 1)

            s->bpp = value;

        else {

            switch (type) {

            case TIFF_BYTE:

                s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +

                         ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);

                break;

            case TIFF_SHORT:

            case TIFF_LONG:

                s->bpp = 0;

                if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)

                    return AVERROR_INVALIDDATA;

                for (i = 0; i < count; i++)

                    s->bpp += tget(&s->gb, type, s->le);

                break;

            default:

                s->bpp = -1;

            }

        }

        break;

    case TIFF_SAMPLES_PER_PIXEL:

        if (count != 1) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Samples per pixel requires a single value, many provided\n");

            return AVERROR_INVALIDDATA;

        }

        if (value > 4U) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Samples per pixel %d is too large\n", value);

            return AVERROR_INVALIDDATA;

        }

        if (s->bppcount == 1)

            s->bpp *= value;

        s->bppcount = value;

        break;

    case TIFF_COMPR:

        s->compr = value;

        s->predictor = 0;

        switch (s->compr) {

        case TIFF_RAW:

        case TIFF_PACKBITS:

        case TIFF_LZW:

        case TIFF_CCITT_RLE:

            break;

        case TIFF_G3:

        case TIFF_G4:

            s->fax_opts = 0;

            break;

        case TIFF_DEFLATE:

        case TIFF_ADOBE_DEFLATE:

#if CONFIG_ZLIB

            break;

#else

            av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");

            return AVERROR(ENOSYS);

#endif

        case TIFF_JPEG:

        case TIFF_NEWJPEG:

            av_log(s->avctx, AV_LOG_ERROR,

                   "JPEG compression is not supported\n");

            return AVERROR_PATCHWELCOME;

        default:

            av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",

                   s->compr);

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_ROWSPERSTRIP:

        if (type == TIFF_LONG && value == UINT_MAX)

            value = s->height;

        if (value < 1) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Incorrect value of rows per strip\n");

            return AVERROR_INVALIDDATA;

        }

        s->rps = value;

        break;

    case TIFF_STRIP_OFFS:

        if (count == 1) {

            s->strippos = 0;

            s->stripoff = value;

        } else

            s->strippos = off;

        s->strips = count;

        if (s->strips == 1)

            s->rps = s->height;

        s->sot = type;

        if (s->strippos > bytestream2_size(&s->gb)) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Tag referencing position outside the image\n");

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_STRIP_SIZE:

        if (count == 1) {

            s->stripsizesoff = 0;

            s->stripsize = value;

            s->strips = 1;

        } else {

            s->stripsizesoff = off;

        }

        s->strips = count;

        s->sstype = type;

        if (s->stripsizesoff > bytestream2_size(&s->gb)) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Tag referencing position outside the image\n");

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_TILE_BYTE_COUNTS:

    case TIFF_TILE_LENGTH:

    case TIFF_TILE_OFFSETS:

    case TIFF_TILE_WIDTH:

        av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");

        return AVERROR_PATCHWELCOME;

        break;

    case TIFF_PREDICTOR:

        s->predictor = value;

        break;

    case TIFF_INVERT:

        switch (value) {

        case 0:

            s->invert = 1;

            break;

        case 1:

            s->invert = 0;

            break;

        case 2:

        case 3:

            break;

        default:

            av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",

                   value);

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_FILL_ORDER:

        if (value < 1 || value > 2) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Unknown FillOrder value %d, trying default one\n", value);

            value = 1;

        }

        s->fill_order = value - 1;

        break;

    case TIFF_PAL:

        pal = (uint32_t *) s->palette;

        off = type_sizes[type];

        if (count / 3 > 256 || bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)

            return AVERROR_INVALIDDATA;

        off = (type_sizes[type] - 1) << 3;

        for (k = 2; k >= 0; k--) {

            for (i = 0; i < count / 3; i++) {

                if (k == 2)

                    pal[i] = 0xFFU << 24;

                j =  (tget(&s->gb, type, s->le) >> off) << (k * 8);

                pal[i] |= j;

            }

        }

        s->palette_is_set = 1;

        break;

    case TIFF_PLANAR:

        if (value == 2) {

            av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");

            return AVERROR_PATCHWELCOME;

        }

        break;

    case TIFF_T4OPTIONS:

        if (s->compr == TIFF_G3)

            s->fax_opts = value;

        break;

    case TIFF_T6OPTIONS:

        if (s->compr == TIFF_G4)

            s->fax_opts = value;

        break;

#define ADD_METADATA(count, name, sep)\

    if ((ret = add_metadata(count, type, name, sep, s)) < 0) {\

        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\

        return ret;\

    }

    case TIFF_MODEL_PIXEL_SCALE:

        ADD_METADATA(count, "ModelPixelScaleTag", NULL);

        break;

    case TIFF_MODEL_TRANSFORMATION:

        ADD_METADATA(count, "ModelTransformationTag", NULL);

        break;

    case TIFF_MODEL_TIEPOINT:

        ADD_METADATA(count, "ModelTiepointTag", NULL);

        break;

    case TIFF_GEO_KEY_DIRECTORY:

        ADD_METADATA(1, "GeoTIFF_Version", NULL);

        ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");

        s->geotag_count   = tget_short(&s->gb, s->le);

        if (s->geotag_count > count / 4 - 1) {

            s->geotag_count = count / 4 - 1;

            av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");

        }

        if (bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4) {

            s->geotag_count = 0;

            return -1;

        }

        s->geotags = av_mallocz(sizeof(TiffGeoTag) * s->geotag_count);

        if (!s->geotags) {

            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            s->geotag_count = 0;

            return AVERROR(ENOMEM);

        }

        for (i = 0; i < s->geotag_count; i++) {

            s->geotags[i].key    = tget_short(&s->gb, s->le);

            s->geotags[i].type   = tget_short(&s->gb, s->le);

            s->geotags[i].count  = tget_short(&s->gb, s->le);



            if (!s->geotags[i].type)

                s->geotags[i].val  = get_geokey_val(s->geotags[i].key, tget_short(&s->gb, s->le));

            else

                s->geotags[i].offset = tget_short(&s->gb, s->le);

        }

        break;

    case TIFF_GEO_DOUBLE_PARAMS:

        if (count >= INT_MAX / sizeof(int64_t))

            return AVERROR_INVALIDDATA;

        if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))

            return AVERROR_INVALIDDATA;

        dp = av_malloc(count * sizeof(double));

        if (!dp) {

            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            return AVERROR(ENOMEM);

        }

        for (i = 0; i < count; i++)

            dp[i] = tget_double(&s->gb, s->le);

        for (i = 0; i < s->geotag_count; i++) {

            if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {

                if (s->geotags[i].count == 0

                    || s->geotags[i].offset + s->geotags[i].count > count) {

                    av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);

                } else {

                    char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");

                    if (!ap) {

                        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

                        av_freep(&dp);

                        return AVERROR(ENOMEM);

                    }

                    s->geotags[i].val = ap;

                }

            }

        }

        av_freep(&dp);

        break;

    case TIFF_GEO_ASCII_PARAMS:

        pos = bytestream2_tell(&s->gb);

        for (i = 0; i < s->geotag_count; i++) {

            if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {

                if (s->geotags[i].count == 0

                    || s->geotags[i].offset +  s->geotags[i].count > count) {

                    av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);

                } else {

                    char *ap;



                    bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);

                    if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)

                        return AVERROR_INVALIDDATA;

                    ap = av_malloc(s->geotags[i].count);

                    if (!ap) {

                        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

                        return AVERROR(ENOMEM);

                    }

                    bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);

                    ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte

                    s->geotags[i].val = ap;

                }

            }

        }

        break;

    case TIFF_ARTIST:

        ADD_METADATA(count, "artist", NULL);

        break;

    case TIFF_COPYRIGHT:

        ADD_METADATA(count, "copyright", NULL);

        break;

    case TIFF_DATE:

        ADD_METADATA(count, "date", NULL);

        break;

    case TIFF_DOCUMENT_NAME:

        ADD_METADATA(count, "document_name", NULL);

        break;

    case TIFF_HOST_COMPUTER:

        ADD_METADATA(count, "computer", NULL);

        break;

    case TIFF_IMAGE_DESCRIPTION:

        ADD_METADATA(count, "description", NULL);

        break;

    case TIFF_MAKE:

        ADD_METADATA(count, "make", NULL);

        break;

    case TIFF_MODEL:

        ADD_METADATA(count, "model", NULL);

        break;

    case TIFF_PAGE_NAME:

        ADD_METADATA(count, "page_name", NULL);

        break;

    case TIFF_PAGE_NUMBER:

        ADD_METADATA(count, "page_number", " / ");

        break;

    case TIFF_SOFTWARE_NAME:

        ADD_METADATA(count, "software", NULL);

        break;

    default:

        av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",

               tag, tag);

    }

    bytestream2_seek(&s->gb, start, SEEK_SET);

    return 0;

}

static int tiff_decode_tag(TiffContext *s)

{

    unsigned tag, type, count, off, value = 0;

    int i, j, k, pos, start;

    int ret;

    uint32_t *pal;

    double *dp;



    tag   = tget_short(&s->gb, s->le);

    type  = tget_short(&s->gb, s->le);

    count = tget_long(&s->gb, s->le);

    off   = tget_long(&s->gb, s->le);

    start = bytestream2_tell(&s->gb);



    if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {

        av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",

               type);

        return 0;

    }



    if (count == 1) {

        switch (type) {

        case TIFF_BYTE:

        case TIFF_SHORT:

            bytestream2_seek(&s->gb, -4, SEEK_CUR);

            value = tget(&s->gb, type, s->le);

            break;

        case TIFF_LONG:

            value = off;

            break;

        case TIFF_STRING:

            if (count <= 4) {

                bytestream2_seek(&s->gb, -4, SEEK_CUR);

                break;

            }

        default:

            value = UINT_MAX;

            bytestream2_seek(&s->gb, off, SEEK_SET);

        }

    } else {

        if (count <= 4 && type_sizes[type] * count <= 4) {

            bytestream2_seek(&s->gb, -4, SEEK_CUR);

        } else {

            bytestream2_seek(&s->gb, off, SEEK_SET);

        }

    }



    switch (tag) {

    case TIFF_WIDTH:

        s->width = value;

        break;

    case TIFF_HEIGHT:

        s->height = value;

        break;

    case TIFF_BPP:

        s->bppcount = count;

        if (count > 4) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "This format is not supported (bpp=%d, %d components)\n",

                   s->bpp, count);

            return AVERROR_INVALIDDATA;

        }

        if (count == 1)

            s->bpp = value;

        else {

            switch (type) {

            case TIFF_BYTE:

                s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +

                         ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);

                break;

            case TIFF_SHORT:

            case TIFF_LONG:

                s->bpp = 0;

                if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)

                    return AVERROR_INVALIDDATA;

                for (i = 0; i < count; i++)

                    s->bpp += tget(&s->gb, type, s->le);

                break;

            default:

                s->bpp = -1;

            }

        }

        break;

    case TIFF_SAMPLES_PER_PIXEL:

        if (count != 1) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Samples per pixel requires a single value, many provided\n");

            return AVERROR_INVALIDDATA;

        }

        if (value > 4U) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Samples per pixel %d is too large\n", value);

            return AVERROR_INVALIDDATA;

        }

        if (s->bppcount == 1)

            s->bpp *= value;

        s->bppcount = value;

        break;

    case TIFF_COMPR:

        s->compr = value;

        s->predictor = 0;

        switch (s->compr) {

        case TIFF_RAW:

        case TIFF_PACKBITS:

        case TIFF_LZW:

        case TIFF_CCITT_RLE:

            break;

        case TIFF_G3:

        case TIFF_G4:

            s->fax_opts = 0;

            break;

        case TIFF_DEFLATE:

        case TIFF_ADOBE_DEFLATE:

#if CONFIG_ZLIB

            break;

#else

            av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");

            return AVERROR(ENOSYS);

#endif

        case TIFF_JPEG:

        case TIFF_NEWJPEG:

            av_log(s->avctx, AV_LOG_ERROR,

                   "JPEG compression is not supported\n");

            return AVERROR_PATCHWELCOME;

        default:

            av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",

                   s->compr);

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_ROWSPERSTRIP:

        if (type == TIFF_LONG && value == UINT_MAX)

            value = s->height;

        if (value < 1) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Incorrect value of rows per strip\n");

            return AVERROR_INVALIDDATA;

        }

        s->rps = value;

        break;

    case TIFF_STRIP_OFFS:

        if (count == 1) {

            s->strippos = 0;

            s->stripoff = value;

        } else

            s->strippos = off;

        s->strips = count;

        if (s->strips == 1)

            s->rps = s->height;

        s->sot = type;

        if (s->strippos > bytestream2_size(&s->gb)) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Tag referencing position outside the image\n");

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_STRIP_SIZE:

        if (count == 1) {

            s->stripsizesoff = 0;

            s->stripsize = value;

            s->strips = 1;

        } else {

            s->stripsizesoff = off;

        }

        s->strips = count;

        s->sstype = type;

        if (s->stripsizesoff > bytestream2_size(&s->gb)) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Tag referencing position outside the image\n");

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_TILE_BYTE_COUNTS:

    case TIFF_TILE_LENGTH:

    case TIFF_TILE_OFFSETS:

    case TIFF_TILE_WIDTH:

        av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");

        return AVERROR_PATCHWELCOME;

        break;

    case TIFF_PREDICTOR:

        s->predictor = value;

        break;

    case TIFF_INVERT:

        switch (value) {

        case 0:

            s->invert = 1;

            break;

        case 1:

            s->invert = 0;

            break;

        case 2:

        case 3:

            break;

        default:

            av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",

                   value);

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_FILL_ORDER:

        if (value < 1 || value > 2) {

            av_log(s->avctx, AV_LOG_ERROR,

                   "Unknown FillOrder value %d, trying default one\n", value);

            value = 1;

        }

        s->fill_order = value - 1;

        break;

    case TIFF_PAL:

        pal = (uint32_t *) s->palette;

        off = type_sizes[type];

        if (count / 3 > 256 || bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)

            return AVERROR_INVALIDDATA;

        off = (type_sizes[type] - 1) << 3;

        for (k = 2; k >= 0; k--) {

            for (i = 0; i < count / 3; i++) {

                if (k == 2)

                    pal[i] = 0xFFU << 24;

                j =  (tget(&s->gb, type, s->le) >> off) << (k * 8);

                pal[i] |= j;

            }

        }

        s->palette_is_set = 1;

        break;

    case TIFF_PLANAR:

        if (value == 2) {

            av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");

            return AVERROR_PATCHWELCOME;

        }

        break;

    case TIFF_T4OPTIONS:

        if (s->compr == TIFF_G3)

            s->fax_opts = value;

        break;

    case TIFF_T6OPTIONS:

        if (s->compr == TIFF_G4)

            s->fax_opts = value;

        break;

#define ADD_METADATA(count, name, sep)\

    if ((ret = add_metadata(count, type, name, sep, s)) < 0) {\

        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\

        return ret;\

    }

    case TIFF_MODEL_PIXEL_SCALE:

        ADD_METADATA(count, "ModelPixelScaleTag", NULL);

        break;

    case TIFF_MODEL_TRANSFORMATION:

        ADD_METADATA(count, "ModelTransformationTag", NULL);

        break;

    case TIFF_MODEL_TIEPOINT:

        ADD_METADATA(count, "ModelTiepointTag", NULL);

        break;

    case TIFF_GEO_KEY_DIRECTORY:

        ADD_METADATA(1, "GeoTIFF_Version", NULL);

        ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");

        s->geotag_count   = tget_short(&s->gb, s->le);

        if (s->geotag_count > count / 4 - 1) {

            s->geotag_count = count / 4 - 1;

            av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");

        }

        if (bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4) {

            s->geotag_count = 0;

            return -1;

        }

        s->geotags = av_mallocz(sizeof(TiffGeoTag) * s->geotag_count);

        if (!s->geotags) {

            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            s->geotag_count = 0;

            return AVERROR(ENOMEM);

        }

        for (i = 0; i < s->geotag_count; i++) {

            s->geotags[i].key    = tget_short(&s->gb, s->le);

            s->geotags[i].type   = tget_short(&s->gb, s->le);

            s->geotags[i].count  = tget_short(&s->gb, s->le);



            if (!s->geotags[i].type)

                s->geotags[i].val  = get_geokey_val(s->geotags[i].key, tget_short(&s->gb, s->le));

            else

                s->geotags[i].offset = tget_short(&s->gb, s->le);

        }

        break;

    case TIFF_GEO_DOUBLE_PARAMS:

        if (count >= INT_MAX / sizeof(int64_t))

            return AVERROR_INVALIDDATA;

        if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))

            return AVERROR_INVALIDDATA;

        dp = av_malloc(count * sizeof(double));

        if (!dp) {

            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            return AVERROR(ENOMEM);

        }

        for (i = 0; i < count; i++)

            dp[i] = tget_double(&s->gb, s->le);

        for (i = 0; i < s->geotag_count; i++) {

            if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {

                if (s->geotags[i].count == 0

                    || s->geotags[i].offset + s->geotags[i].count > count) {

                    av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);

                } else {

                    char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");

                    if (!ap) {

                        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

                        av_freep(&dp);

                        return AVERROR(ENOMEM);

                    }

                    s->geotags[i].val = ap;

                }

            }

        }

        av_freep(&dp);

        break;

    case TIFF_GEO_ASCII_PARAMS:

        pos = bytestream2_tell(&s->gb);

        for (i = 0; i < s->geotag_count; i++) {

            if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {

                if (s->geotags[i].count == 0

                    || s->geotags[i].offset +  s->geotags[i].count > count) {

                    av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);

                } else {

                    char *ap;



                    bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);

                    if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)

                        return AVERROR_INVALIDDATA;

                    ap = av_malloc(s->geotags[i].count);

                    if (!ap) {

                        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

                        return AVERROR(ENOMEM);

                    }

                    bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);

                    ap[s->geotags[i].count - 1] = '\0'; 

                    s->geotags[i].val = ap;

                }

            }

        }

        break;

    case TIFF_ARTIST:

        ADD_METADATA(count, "artist", NULL);

        break;

    case TIFF_COPYRIGHT:

        ADD_METADATA(count, "copyright", NULL);

        break;

    case TIFF_DATE:

        ADD_METADATA(count, "date", NULL);

        break;

    case TIFF_DOCUMENT_NAME:

        ADD_METADATA(count, "document_name", NULL);

        break;

    case TIFF_HOST_COMPUTER:

        ADD_METADATA(count, "computer", NULL);

        break;

    case TIFF_IMAGE_DESCRIPTION:

        ADD_METADATA(count, "description", NULL);

        break;

    case TIFF_MAKE:

        ADD_METADATA(count, "make", NULL);

        break;

    case TIFF_MODEL:

        ADD_METADATA(count, "model", NULL);

        break;

    case TIFF_PAGE_NAME:

        ADD_METADATA(count, "page_name", NULL);

        break;

    case TIFF_PAGE_NUMBER:

        ADD_METADATA(count, "page_number", " / ");

        break;

    case TIFF_SOFTWARE_NAME:

        ADD_METADATA(count, "software", NULL);

        break;

    default:

        av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",

               tag, tag);

    }

    bytestream2_seek(&s->gb, start, SEEK_SET);

    return 0;

}

static int tiff_decode_tag(TiffContext *s)     if ((ret = add_metadata(count, type, name, sep, s)) < 0) {\
static int FUNC_0(TiffContext *VAR_0)

{

    unsigned VAR_1, VAR_2, VAR_3, VAR_4, VAR_5 = 0;

    int VAR_6, VAR_7, VAR_8, VAR_9, VAR_10;

    int VAR_11;

    uint32_t *pal;

    double *VAR_12;



    VAR_1   = tget_short(&VAR_0->gb, VAR_0->le);

    VAR_2  = tget_short(&VAR_0->gb, VAR_0->le);

    VAR_3 = tget_long(&VAR_0->gb, VAR_0->le);

    VAR_4   = tget_long(&VAR_0->gb, VAR_0->le);

    VAR_10 = bytestream2_tell(&VAR_0->gb);



    if (VAR_2 == 0 || VAR_2 >= FF_ARRAY_ELEMS(type_sizes)) {

        av_log(VAR_0->avctx, AV_LOG_DEBUG, "Unknown tiff VAR_2 (%u) encountered\n",

               VAR_2);

        return 0;

    }



    if (VAR_3 == 1) {

        switch (VAR_2) {

        case TIFF_BYTE:

        case TIFF_SHORT:

            bytestream2_seek(&VAR_0->gb, -4, SEEK_CUR);

            VAR_5 = tget(&VAR_0->gb, VAR_2, VAR_0->le);

            break;

        case TIFF_LONG:

            VAR_5 = VAR_4;

            break;

        case TIFF_STRING:

            if (VAR_3 <= 4) {

                bytestream2_seek(&VAR_0->gb, -4, SEEK_CUR);

                break;

            }

        default:

            VAR_5 = UINT_MAX;

            bytestream2_seek(&VAR_0->gb, VAR_4, SEEK_SET);

        }

    } else {

        if (VAR_3 <= 4 && type_sizes[VAR_2] * VAR_3 <= 4) {

            bytestream2_seek(&VAR_0->gb, -4, SEEK_CUR);

        } else {

            bytestream2_seek(&VAR_0->gb, VAR_4, SEEK_SET);

        }

    }



    switch (VAR_1) {

    case TIFF_WIDTH:

        VAR_0->width = VAR_5;

        break;

    case TIFF_HEIGHT:

        VAR_0->height = VAR_5;

        break;

    case TIFF_BPP:

        VAR_0->bppcount = VAR_3;

        if (VAR_3 > 4) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "This format is not supported (bpp=%d, %d components)\n",

                   VAR_0->bpp, VAR_3);

            return AVERROR_INVALIDDATA;

        }

        if (VAR_3 == 1)

            VAR_0->bpp = VAR_5;

        else {

            switch (VAR_2) {

            case TIFF_BYTE:

                VAR_0->bpp = (VAR_4 & 0xFF) + ((VAR_4 >> 8) & 0xFF) +

                         ((VAR_4 >> 16) & 0xFF) + ((VAR_4 >> 24) & 0xFF);

                break;

            case TIFF_SHORT:

            case TIFF_LONG:

                VAR_0->bpp = 0;

                if (bytestream2_get_bytes_left(&VAR_0->gb) < type_sizes[VAR_2] * VAR_3)

                    return AVERROR_INVALIDDATA;

                for (VAR_6 = 0; VAR_6 < VAR_3; VAR_6++)

                    VAR_0->bpp += tget(&VAR_0->gb, VAR_2, VAR_0->le);

                break;

            default:

                VAR_0->bpp = -1;

            }

        }

        break;

    case TIFF_SAMPLES_PER_PIXEL:

        if (VAR_3 != 1) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Samples per pixel requires a single VAR_5, many provided\n");

            return AVERROR_INVALIDDATA;

        }

        if (VAR_5 > 4U) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Samples per pixel %d is too large\n", VAR_5);

            return AVERROR_INVALIDDATA;

        }

        if (VAR_0->bppcount == 1)

            VAR_0->bpp *= VAR_5;

        VAR_0->bppcount = VAR_5;

        break;

    case TIFF_COMPR:

        VAR_0->compr = VAR_5;

        VAR_0->predictor = 0;

        switch (VAR_0->compr) {

        case TIFF_RAW:

        case TIFF_PACKBITS:

        case TIFF_LZW:

        case TIFF_CCITT_RLE:

            break;

        case TIFF_G3:

        case TIFF_G4:

            VAR_0->fax_opts = 0;

            break;

        case TIFF_DEFLATE:

        case TIFF_ADOBE_DEFLATE:

#if CONFIG_ZLIB

            break;

#else

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");

            return AVERROR(ENOSYS);

#endif

        case TIFF_JPEG:

        case TIFF_NEWJPEG:

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "JPEG compression is not supported\n");

            return AVERROR_PATCHWELCOME;

        default:

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Unknown compression method %VAR_6\n",

                   VAR_0->compr);

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_ROWSPERSTRIP:

        if (VAR_2 == TIFF_LONG && VAR_5 == UINT_MAX)

            VAR_5 = VAR_0->height;

        if (VAR_5 < 1) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Incorrect VAR_5 of rows per strip\n");

            return AVERROR_INVALIDDATA;

        }

        VAR_0->rps = VAR_5;

        break;

    case TIFF_STRIP_OFFS:

        if (VAR_3 == 1) {

            VAR_0->strippos = 0;

            VAR_0->stripoff = VAR_5;

        } else

            VAR_0->strippos = VAR_4;

        VAR_0->strips = VAR_3;

        if (VAR_0->strips == 1)

            VAR_0->rps = VAR_0->height;

        VAR_0->sot = VAR_2;

        if (VAR_0->strippos > bytestream2_size(&VAR_0->gb)) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Tag referencing position outside the image\n");

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_STRIP_SIZE:

        if (VAR_3 == 1) {

            VAR_0->stripsizesoff = 0;

            VAR_0->stripsize = VAR_5;

            VAR_0->strips = 1;

        } else {

            VAR_0->stripsizesoff = VAR_4;

        }

        VAR_0->strips = VAR_3;

        VAR_0->sstype = VAR_2;

        if (VAR_0->stripsizesoff > bytestream2_size(&VAR_0->gb)) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Tag referencing position outside the image\n");

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_TILE_BYTE_COUNTS:

    case TIFF_TILE_LENGTH:

    case TIFF_TILE_OFFSETS:

    case TIFF_TILE_WIDTH:

        av_log(VAR_0->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");

        return AVERROR_PATCHWELCOME;

        break;

    case TIFF_PREDICTOR:

        VAR_0->predictor = VAR_5;

        break;

    case TIFF_INVERT:

        switch (VAR_5) {

        case 0:

            VAR_0->invert = 1;

            break;

        case 1:

            VAR_0->invert = 0;

            break;

        case 2:

        case 3:

            break;

        default:

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",

                   VAR_5);

            return AVERROR_INVALIDDATA;

        }

        break;

    case TIFF_FILL_ORDER:

        if (VAR_5 < 1 || VAR_5 > 2) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Unknown FillOrder VAR_5 %d, trying default one\n", VAR_5);

            VAR_5 = 1;

        }

        VAR_0->fill_order = VAR_5 - 1;

        break;

    case TIFF_PAL:

        pal = (uint32_t *) VAR_0->palette;

        VAR_4 = type_sizes[VAR_2];

        if (VAR_3 / 3 > 256 || bytestream2_get_bytes_left(&VAR_0->gb) < VAR_3 / 3 * VAR_4 * 3)

            return AVERROR_INVALIDDATA;

        VAR_4 = (type_sizes[VAR_2] - 1) << 3;

        for (VAR_8 = 2; VAR_8 >= 0; VAR_8--) {

            for (VAR_6 = 0; VAR_6 < VAR_3 / 3; VAR_6++) {

                if (VAR_8 == 2)

                    pal[VAR_6] = 0xFFU << 24;

                VAR_7 =  (tget(&VAR_0->gb, VAR_2, VAR_0->le) >> VAR_4) << (VAR_8 * 8);

                pal[VAR_6] |= VAR_7;

            }

        }

        VAR_0->palette_is_set = 1;

        break;

    case TIFF_PLANAR:

        if (VAR_5 == 2) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Planar format is not supported\n");

            return AVERROR_PATCHWELCOME;

        }

        break;

    case TIFF_T4OPTIONS:

        if (VAR_0->compr == TIFF_G3)

            VAR_0->fax_opts = VAR_5;

        break;

    case TIFF_T6OPTIONS:

        if (VAR_0->compr == TIFF_G4)

            VAR_0->fax_opts = VAR_5;

        break;

#define ADD_METADATA(VAR_3, name, sep)\

    if ((VAR_11 = add_metadata(VAR_3, VAR_2, name, sep, VAR_0)) < 0) {\

        av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\

        return VAR_11;\

    }

    case TIFF_MODEL_PIXEL_SCALE:

        ADD_METADATA(VAR_3, "ModelPixelScaleTag", NULL);

        break;

    case TIFF_MODEL_TRANSFORMATION:

        ADD_METADATA(VAR_3, "ModelTransformationTag", NULL);

        break;

    case TIFF_MODEL_TIEPOINT:

        ADD_METADATA(VAR_3, "ModelTiepointTag", NULL);

        break;

    case TIFF_GEO_KEY_DIRECTORY:

        ADD_METADATA(1, "GeoTIFF_Version", NULL);

        ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");

        VAR_0->geotag_count   = tget_short(&VAR_0->gb, VAR_0->le);

        if (VAR_0->geotag_count > VAR_3 / 4 - 1) {

            VAR_0->geotag_count = VAR_3 / 4 - 1;

            av_log(VAR_0->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");

        }

        if (bytestream2_get_bytes_left(&VAR_0->gb) < VAR_0->geotag_count * sizeof(int16_t) * 4) {

            VAR_0->geotag_count = 0;

            return -1;

        }

        VAR_0->geotags = av_mallocz(sizeof(TiffGeoTag) * VAR_0->geotag_count);

        if (!VAR_0->geotags) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            VAR_0->geotag_count = 0;

            return AVERROR(ENOMEM);

        }

        for (VAR_6 = 0; VAR_6 < VAR_0->geotag_count; VAR_6++) {

            VAR_0->geotags[VAR_6].key    = tget_short(&VAR_0->gb, VAR_0->le);

            VAR_0->geotags[VAR_6].VAR_2   = tget_short(&VAR_0->gb, VAR_0->le);

            VAR_0->geotags[VAR_6].VAR_3  = tget_short(&VAR_0->gb, VAR_0->le);



            if (!VAR_0->geotags[VAR_6].VAR_2)

                VAR_0->geotags[VAR_6].val  = get_geokey_val(VAR_0->geotags[VAR_6].key, tget_short(&VAR_0->gb, VAR_0->le));

            else

                VAR_0->geotags[VAR_6].offset = tget_short(&VAR_0->gb, VAR_0->le);

        }

        break;

    case TIFF_GEO_DOUBLE_PARAMS:

        if (VAR_3 >= INT_MAX / sizeof(int64_t))

            return AVERROR_INVALIDDATA;

        if (bytestream2_get_bytes_left(&VAR_0->gb) < VAR_3 * sizeof(int64_t))

            return AVERROR_INVALIDDATA;

        VAR_12 = av_malloc(VAR_3 * sizeof(double));

        if (!VAR_12) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            return AVERROR(ENOMEM);

        }

        for (VAR_6 = 0; VAR_6 < VAR_3; VAR_6++)

            VAR_12[VAR_6] = tget_double(&VAR_0->gb, VAR_0->le);

        for (VAR_6 = 0; VAR_6 < VAR_0->geotag_count; VAR_6++) {

            if (VAR_0->geotags[VAR_6].VAR_2 == TIFF_GEO_DOUBLE_PARAMS) {

                if (VAR_0->geotags[VAR_6].VAR_3 == 0

                    || VAR_0->geotags[VAR_6].offset + VAR_0->geotags[VAR_6].VAR_3 > VAR_3) {

                    av_log(VAR_0->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", VAR_0->geotags[VAR_6].key);

                } else {

                    char *ap = doubles2str(&VAR_12[VAR_0->geotags[VAR_6].offset], VAR_0->geotags[VAR_6].VAR_3, ", ");

                    if (!ap) {

                        av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

                        av_freep(&VAR_12);

                        return AVERROR(ENOMEM);

                    }

                    VAR_0->geotags[VAR_6].val = ap;

                }

            }

        }

        av_freep(&VAR_12);

        break;

    case TIFF_GEO_ASCII_PARAMS:

        VAR_9 = bytestream2_tell(&VAR_0->gb);

        for (VAR_6 = 0; VAR_6 < VAR_0->geotag_count; VAR_6++) {

            if (VAR_0->geotags[VAR_6].VAR_2 == TIFF_GEO_ASCII_PARAMS) {

                if (VAR_0->geotags[VAR_6].VAR_3 == 0

                    || VAR_0->geotags[VAR_6].offset +  VAR_0->geotags[VAR_6].VAR_3 > VAR_3) {

                    av_log(VAR_0->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", VAR_0->geotags[VAR_6].key);

                } else {

                    char *ap;



                    bytestream2_seek(&VAR_0->gb, VAR_9 + VAR_0->geotags[VAR_6].offset, SEEK_SET);

                    if (bytestream2_get_bytes_left(&VAR_0->gb) < VAR_0->geotags[VAR_6].VAR_3)

                        return AVERROR_INVALIDDATA;

                    ap = av_malloc(VAR_0->geotags[VAR_6].VAR_3);

                    if (!ap) {

                        av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

                        return AVERROR(ENOMEM);

                    }

                    bytestream2_get_bufferu(&VAR_0->gb, ap, VAR_0->geotags[VAR_6].VAR_3);

                    ap[VAR_0->geotags[VAR_6].VAR_3 - 1] = '\0'; 

                    VAR_0->geotags[VAR_6].val = ap;

                }

            }

        }

        break;

    case TIFF_ARTIST:

        ADD_METADATA(VAR_3, "artist", NULL);

        break;

    case TIFF_COPYRIGHT:

        ADD_METADATA(VAR_3, "copyright", NULL);

        break;

    case TIFF_DATE:

        ADD_METADATA(VAR_3, "date", NULL);

        break;

    case TIFF_DOCUMENT_NAME:

        ADD_METADATA(VAR_3, "document_name", NULL);

        break;

    case TIFF_HOST_COMPUTER:

        ADD_METADATA(VAR_3, "computer", NULL);

        break;

    case TIFF_IMAGE_DESCRIPTION:

        ADD_METADATA(VAR_3, "description", NULL);

        break;

    case TIFF_MAKE:

        ADD_METADATA(VAR_3, "make", NULL);

        break;

    case TIFF_MODEL:

        ADD_METADATA(VAR_3, "model", NULL);

        break;

    case TIFF_PAGE_NAME:

        ADD_METADATA(VAR_3, "page_name", NULL);

        break;

    case TIFF_PAGE_NUMBER:

        ADD_METADATA(VAR_3, "page_number", " / ");

        break;

    case TIFF_SOFTWARE_NAME:

        ADD_METADATA(VAR_3, "software", NULL);

        break;

    default:

        av_log(VAR_0->avctx, AV_LOG_DEBUG, "Unknown or unsupported VAR_1 %d/0X%0X\n",

               VAR_1, VAR_1);

    }

    bytestream2_seek(&VAR_0->gb, VAR_10, SEEK_SET);

    return 0;

}

static int FUNC_0(TiffContext *VAR_0)
{ unsigned VAR_1, VAR_2, VAR_3, VAR_4, VAR_5 = 0; int VAR_6, VAR_7, VAR_8, VAR_9, VAR_10; int VAR_11; uint32_t *pal; double *VAR_12; VAR_1   = tget_short(&VAR_0->gb, VAR_0->le); VAR_2  = tget_short(&VAR_0->gb, VAR_0->le); VAR_3 = tget_long(&VAR_0->gb, VAR_0->le); VAR_4   = tget_long(&VAR_0->gb, VAR_0->le); VAR_10 = bytestream2_tell(&VAR_0->gb); if (VAR_2 == 0 || VAR_2 >= FF_ARRAY_ELEMS(type_sizes)) { av_log(VAR_0->avctx, AV_LOG_DEBUG, "Unknown tiff VAR_2 (%u) encountered\n",
VAR_2); return 0; } if (VAR_3 == 1) { switch (VAR_2) { case TIFF_BYTE:
case TIFF_SHORT:
bytestream2_seek(&VAR_0->gb, -4, SEEK_CUR); VAR_5 = tget(&VAR_0->gb, VAR_2, VAR_0->le); break; case TIFF_LONG:
VAR_5 = VAR_4; break; case TIFF_STRING:
if (VAR_3 <= 4) { bytestream2_seek(&VAR_0->gb, -4, SEEK_CUR); break; } default:
VAR_5 = UINT_MAX; bytestream2_seek(&VAR_0->gb, VAR_4, SEEK_SET); } } else { if (VAR_3 <= 4 && type_sizes[VAR_2] * VAR_3 <= 4) { bytestream2_seek(&VAR_0->gb, -4, SEEK_CUR); } else { bytestream2_seek(&VAR_0->gb, VAR_4, SEEK_SET); } } switch (VAR_1) { case TIFF_WIDTH:
VAR_0->width = VAR_5; break; case TIFF_HEIGHT:
VAR_0->height = VAR_5; break; case TIFF_BPP:
VAR_0->bppcount = VAR_3; if (VAR_3 > 4) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"This format is not supported (bpp=%d, %d components)\n",
VAR_0->bpp, VAR_3); return AVERROR_INVALIDDATA; } if (VAR_3 == 1)
VAR_0->bpp = VAR_5; else { switch (VAR_2) { case TIFF_BYTE:
VAR_0->bpp = (VAR_4 & 0xFF) + ((VAR_4 >> 8) & 0xFF) +
((VAR_4 >> 16) & 0xFF) + ((VAR_4 >> 24) & 0xFF); break; case TIFF_SHORT:
case TIFF_LONG:
VAR_0->bpp = 0; if (bytestream2_get_bytes_left(&VAR_0->gb) < type_sizes[VAR_2] * VAR_3)
return AVERROR_INVALIDDATA; for (VAR_6 = 0; VAR_6 < VAR_3; VAR_6++) VAR_0->bpp += tget(&VAR_0->gb, VAR_2, VAR_0->le); break; default:
VAR_0->bpp = -1; } } break; case TIFF_SAMPLES_PER_PIXEL:
if (VAR_3 != 1) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Samples per pixel requires a single VAR_5, many provided\n"); return AVERROR_INVALIDDATA; } if (VAR_5 > 4U) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Samples per pixel %d is too large\n", VAR_5); return AVERROR_INVALIDDATA; } if (VAR_0->bppcount == 1)
VAR_0->bpp *= VAR_5; VAR_0->bppcount = VAR_5; break; case TIFF_COMPR:
VAR_0->compr = VAR_5; VAR_0->predictor = 0; switch (VAR_0->compr) { case TIFF_RAW:
case TIFF_PACKBITS:
case TIFF_LZW:
case TIFF_CCITT_RLE:
break; case TIFF_G3:
case TIFF_G4:
VAR_0->fax_opts = 0; break; case TIFF_DEFLATE:
case TIFF_ADOBE_DEFLATE:
#if CONFIG_ZLIB
break; #else
av_log(VAR_0->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n"); return AVERROR(ENOSYS); #endif
case TIFF_JPEG:
case TIFF_NEWJPEG:
av_log(VAR_0->avctx, AV_LOG_ERROR,
"JPEG compression is not supported\n"); return AVERROR_PATCHWELCOME; default:
av_log(VAR_0->avctx, AV_LOG_ERROR, "Unknown compression method %VAR_6\n",
VAR_0->compr); return AVERROR_INVALIDDATA; } break; case TIFF_ROWSPERSTRIP:
if (VAR_2 == TIFF_LONG && VAR_5 == UINT_MAX)
VAR_5 = VAR_0->height; if (VAR_5 < 1) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Incorrect VAR_5 of rows per strip\n"); return AVERROR_INVALIDDATA; } VAR_0->rps = VAR_5; break; case TIFF_STRIP_OFFS:
if (VAR_3 == 1) { VAR_0->strippos = 0; VAR_0->stripoff = VAR_5; } else VAR_0->strippos = VAR_4; VAR_0->strips = VAR_3; if (VAR_0->strips == 1)
VAR_0->rps = VAR_0->height; VAR_0->sot = VAR_2; if (VAR_0->strippos > bytestream2_size(&VAR_0->gb)) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Tag referencing position outside the image\n"); return AVERROR_INVALIDDATA; } break; case TIFF_STRIP_SIZE:
if (VAR_3 == 1) { VAR_0->stripsizesoff = 0; VAR_0->stripsize = VAR_5; VAR_0->strips = 1; } else { VAR_0->stripsizesoff = VAR_4; } VAR_0->strips = VAR_3; VAR_0->sstype = VAR_2; if (VAR_0->stripsizesoff > bytestream2_size(&VAR_0->gb)) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Tag referencing position outside the image\n"); return AVERROR_INVALIDDATA; } break; case TIFF_TILE_BYTE_COUNTS:
case TIFF_TILE_LENGTH:
case TIFF_TILE_OFFSETS:
case TIFF_TILE_WIDTH:
av_log(VAR_0->avctx, AV_LOG_ERROR, "Tiled images are not supported\n"); return AVERROR_PATCHWELCOME; break; case TIFF_PREDICTOR:
VAR_0->predictor = VAR_5; break; case TIFF_INVERT:
switch (VAR_5) { case 0:
VAR_0->invert = 1; break; case 1:
VAR_0->invert = 0; break; case 2:
case 3:
break; default:
av_log(VAR_0->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
VAR_5); return AVERROR_INVALIDDATA; } break; case TIFF_FILL_ORDER:
if (VAR_5 < 1 || VAR_5 > 2) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Unknown FillOrder VAR_5 %d, trying default one\n", VAR_5); VAR_5 = 1; } VAR_0->fill_order = VAR_5 - 1; break; case TIFF_PAL:
pal = (uint32_t *) VAR_0->palette; VAR_4 = type_sizes[VAR_2]; if (VAR_3 / 3 > 256 || bytestream2_get_bytes_left(&VAR_0->gb) < VAR_3 / 3 * VAR_4 * 3)
return AVERROR_INVALIDDATA; VAR_4 = (type_sizes[VAR_2] - 1) << 3; for (VAR_8 = 2; VAR_8 >= 0; VAR_8--) { for (VAR_6 = 0; VAR_6 < VAR_3 / 3; VAR_6++) { if (VAR_8 == 2)
pal[VAR_6] = 0xFFU << 24; VAR_7 =  (tget(&VAR_0->gb, VAR_2, VAR_0->le) >> VAR_4) << (VAR_8 * 8); pal[VAR_6] |= VAR_7; } } VAR_0->palette_is_set = 1; break; case TIFF_PLANAR:
if (VAR_5 == 2) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Planar format is not supported\n"); return AVERROR_PATCHWELCOME; } break; case TIFF_T4OPTIONS:
if (VAR_0->compr == TIFF_G3)
VAR_0->fax_opts = VAR_5; break; case TIFF_T6OPTIONS:
if (VAR_0->compr == TIFF_G4)
VAR_0->fax_opts = VAR_5; break; #define ADD_METADATA(VAR_3, name, sep)\
if ((VAR_11 = add_metadata(VAR_3, VAR_2, name, sep, VAR_0)) < 0) {\ av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\ return VAR_11;\ } case TIFF_MODEL_PIXEL_SCALE:
ADD_METADATA(VAR_3, "ModelPixelScaleTag", NULL); break; case TIFF_MODEL_TRANSFORMATION:
ADD_METADATA(VAR_3, "ModelTransformationTag", NULL); break; case TIFF_MODEL_TIEPOINT:
ADD_METADATA(VAR_3, "ModelTiepointTag", NULL); break; case TIFF_GEO_KEY_DIRECTORY:
ADD_METADATA(1, "GeoTIFF_Version", NULL); ADD_METADATA(2, "GeoTIFF_Key_Revision", "."); VAR_0->geotag_count   = tget_short(&VAR_0->gb, VAR_0->le); if (VAR_0->geotag_count > VAR_3 / 4 - 1) { VAR_0->geotag_count = VAR_3 / 4 - 1; av_log(VAR_0->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n"); } if (bytestream2_get_bytes_left(&VAR_0->gb) < VAR_0->geotag_count * sizeof(int16_t) * 4) { VAR_0->geotag_count = 0; return -1; } VAR_0->geotags = av_mallocz(sizeof(TiffGeoTag) * VAR_0->geotag_count); if (!VAR_0->geotags) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); VAR_0->geotag_count = 0; return AVERROR(ENOMEM); } for (VAR_6 = 0; VAR_6 < VAR_0->geotag_count; VAR_6++) { VAR_0->geotags[VAR_6].key    = tget_short(&VAR_0->gb, VAR_0->le); VAR_0->geotags[VAR_6].VAR_2   = tget_short(&VAR_0->gb, VAR_0->le); VAR_0->geotags[VAR_6].VAR_3  = tget_short(&VAR_0->gb, VAR_0->le); if (!VAR_0->geotags[VAR_6].VAR_2)
VAR_0->geotags[VAR_6].val  = get_geokey_val(VAR_0->geotags[VAR_6].key, tget_short(&VAR_0->gb, VAR_0->le)); else
VAR_0->geotags[VAR_6].offset = tget_short(&VAR_0->gb, VAR_0->le); } break; case TIFF_GEO_DOUBLE_PARAMS:
if (VAR_3 >= INT_MAX / sizeof(int64_t))
return AVERROR_INVALIDDATA; if (bytestream2_get_bytes_left(&VAR_0->gb) < VAR_3 * sizeof(int64_t))
return AVERROR_INVALIDDATA; VAR_12 = av_malloc(VAR_3 * sizeof(double)); if (!VAR_12) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); return AVERROR(ENOMEM); } for (VAR_6 = 0; VAR_6 < VAR_3; VAR_6++) VAR_12[VAR_6] = tget_double(&VAR_0->gb, VAR_0->le); for (VAR_6 = 0; VAR_6 < VAR_0->geotag_count; VAR_6++) { if (VAR_0->geotags[VAR_6].VAR_2 == TIFF_GEO_DOUBLE_PARAMS) { if (VAR_0->geotags[VAR_6].VAR_3 == 0
|| VAR_0->geotags[VAR_6].offset + VAR_0->geotags[VAR_6].VAR_3 > VAR_3) { av_log(VAR_0->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", VAR_0->geotags[VAR_6].key); } else { char *ap = doubles2str(&VAR_12[VAR_0->geotags[VAR_6].offset], VAR_0->geotags[VAR_6].VAR_3, ", "); if (!ap) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); av_freep(&VAR_12); return AVERROR(ENOMEM); } VAR_0->geotags[VAR_6].val = ap; } } } av_freep(&VAR_12); break; case TIFF_GEO_ASCII_PARAMS:
VAR_9 = bytestream2_tell(&VAR_0->gb); for (VAR_6 = 0; VAR_6 < VAR_0->geotag_count; VAR_6++) { if (VAR_0->geotags[VAR_6].VAR_2 == TIFF_GEO_ASCII_PARAMS) { if (VAR_0->geotags[VAR_6].VAR_3 == 0
|| VAR_0->geotags[VAR_6].offset +  VAR_0->geotags[VAR_6].VAR_3 > VAR_3) { av_log(VAR_0->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", VAR_0->geotags[VAR_6].key); } else { char *ap; bytestream2_seek(&VAR_0->gb, VAR_9 + VAR_0->geotags[VAR_6].offset, SEEK_SET); if (bytestream2_get_bytes_left(&VAR_0->gb) < VAR_0->geotags[VAR_6].VAR_3)
return AVERROR_INVALIDDATA; ap = av_malloc(VAR_0->geotags[VAR_6].VAR_3); if (!ap) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); return AVERROR(ENOMEM); } bytestream2_get_bufferu(&VAR_0->gb, ap, VAR_0->geotags[VAR_6].VAR_3); ap[VAR_0->geotags[VAR_6].VAR_3 - 1] = '\0'; VAR_0->geotags[VAR_6].val = ap; } } } break; case TIFF_ARTIST:
ADD_METADATA(VAR_3, "artist", NULL); break; case TIFF_COPYRIGHT:
ADD_METADATA(VAR_3, "copyright", NULL); break; case TIFF_DATE:
ADD_METADATA(VAR_3, "date", NULL); break; case TIFF_DOCUMENT_NAME:
ADD_METADATA(VAR_3, "document_name", NULL); break; case TIFF_HOST_COMPUTER:
ADD_METADATA(VAR_3, "computer", NULL); break; case TIFF_IMAGE_DESCRIPTION:
ADD_METADATA(VAR_3, "description", NULL); break; case TIFF_MAKE:
ADD_METADATA(VAR_3, "make", NULL); break; case TIFF_MODEL:
ADD_METADATA(VAR_3, "model", NULL); break; case TIFF_PAGE_NAME:
ADD_METADATA(VAR_3, "page_name", NULL); break; case TIFF_PAGE_NUMBER:
ADD_METADATA(VAR_3, "page_number", " / "); break; case TIFF_SOFTWARE_NAME:
ADD_METADATA(VAR_3, "software", NULL); break; default:
av_log(VAR_0->avctx, AV_LOG_DEBUG, "Unknown or unsupported VAR_1 %d/0X%0X\n",
VAR_1, VAR_1); } bytestream2_seek(&VAR_0->gb, VAR_10, SEEK_SET); return 0; }