- switch (ctx->section_type & 0xF0) {
- case HAP_COMP_NONE:
- /* Only DXTC texture compression */
- ctx->tex_data = gbc->buffer;
- ctx->tex_size = length;
- compressorstr = "none";
- break;
- case HAP_COMP_SNAPPY:
- snappy_size = ff_snappy_peek_uncompressed_length(gbc);
- ret = av_reallocp(&ctx->snappied, snappy_size);
- if (ret < 0) {
- return ret;
+ switch (section_type & 0xF0) {
+ case HAP_COMP_NONE:
+ case HAP_COMP_SNAPPY:
+ ret = ff_hap_set_chunk_count(ctx, 1, 1);
+ if (ret == 0) {
+ ctx->chunks[0].compressor = section_type & 0xF0;
+ ctx->chunks[0].compressed_offset = 0;
+ ctx->chunks[0].compressed_size = section_size;
+ }
+ if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
+ compressorstr = "none";
+ } else {
+ compressorstr = "snappy";
+ }
+ break;
+ case HAP_COMP_COMPLEX:
+ ret = parse_section_header(gbc, §ion_size, §ion_type);
+ if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
+ ret = AVERROR_INVALIDDATA;
+ if (ret == 0)
+ ret = hap_parse_decode_instructions(ctx, section_size);
+ compressorstr = "complex";
+ break;
+ default:
+ ret = AVERROR_INVALIDDATA;
+ break;
+ }
+
+ if (ret != 0)
+ return ret;
+
+ /* Check the frame is valid and read the uncompressed chunk sizes */
+ ctx->tex_size = 0;
+ for (i = 0; i < ctx->chunk_count; i++) {
+ HapChunk *chunk = &ctx->chunks[i];
+
+ /* Check the compressed buffer is valid */
+ if (chunk->compressed_offset + chunk->compressed_size > bytestream2_get_bytes_left(gbc))
+ return AVERROR_INVALIDDATA;
+
+ /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
+ * size thus far */
+ chunk->uncompressed_offset = ctx->tex_size;
+
+ /* Fill out uncompressed size */
+ if (chunk->compressor == HAP_COMP_SNAPPY) {
+ GetByteContext gbc_tmp;
+ int64_t uncompressed_size;
+ bytestream2_init(&gbc_tmp, gbc->buffer + chunk->compressed_offset,
+ chunk->compressed_size);
+ uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
+ if (uncompressed_size < 0) {
+ return uncompressed_size;
+ }
+ chunk->uncompressed_size = uncompressed_size;
+ } else if (chunk->compressor == HAP_COMP_NONE) {
+ chunk->uncompressed_size = chunk->compressed_size;
+ } else {
+ return AVERROR_INVALIDDATA;