Mercurial > libavcodec.hg
annotate flashsv.c @ 4883:9055ed00a295 libavcodec
fix exploitable buffer overflow
| author | michael |
|---|---|
| date | Fri, 27 Apr 2007 12:41:55 +0000 |
| parents | b3ee9a1526b0 |
| children | f99e40a7155b |
| rev | line source |
|---|---|
| 3329 | 1 /* |
| 2 * Flash Screen Video decoder | |
| 3 * Copyright (C) 2004 Alex Beregszaszi | |
| 4 * Copyright (C) 2006 Benjamin Larsson | |
| 5 * | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3329
diff
changeset
|
6 * This file is part of FFmpeg. |
|
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3329
diff
changeset
|
7 * |
|
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3329
diff
changeset
|
8 * FFmpeg is free software; you can redistribute it and/or |
| 3329 | 9 * modify it under the terms of the GNU Lesser General Public |
| 10 * License as published by the Free Software Foundation; either | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3329
diff
changeset
|
11 * version 2.1 of the License, or (at your option) any later version. |
| 3329 | 12 * |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3329
diff
changeset
|
13 * FFmpeg is distributed in the hope that it will be useful, |
| 3329 | 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 16 * Lesser General Public License for more details. | |
| 17 * | |
| 18 * You should have received a copy of the GNU Lesser General Public | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3329
diff
changeset
|
19 * License along with FFmpeg; if not, write to the Free Software |
| 3329 | 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 */ | |
| 22 | |
| 23 /** | |
| 24 * @file flashsv.c | |
| 25 * Flash Screen Video decoder | |
| 26 * @author Alex Beregszaszi | |
| 27 * @author Benjamin Larsson | |
| 28 */ | |
| 29 | |
| 30 /* Bitstream description | |
| 31 * The picture is divided into blocks that are zlib compressed. | |
| 32 * | |
| 33 * The decoder is fed complete frames, the frameheader contains: | |
| 34 * 4bits of block width | |
| 35 * 12bits of frame width | |
| 36 * 4bits of block height | |
| 37 * 12bits of frame height | |
| 38 * | |
| 39 * Directly after the header are the compressed blocks. The blocks | |
| 40 * have their compressed size represented with 16bits in the beginnig. | |
| 41 * If the size = 0 then the block is unchanged from the previous frame. | |
| 42 * All blocks are decompressed until the buffer is consumed. | |
| 43 * | |
| 44 * Encoding ideas, a basic encoder would just use a fixed block size. | |
| 45 * Block sizes can be multipels of 16, from 16 to 256. The blocks don't | |
| 46 * have to be quadratic. A brute force search with a set of diffrent | |
| 47 * block sizes should give a better result then to just use a fixed size. | |
| 48 */ | |
| 49 | |
| 50 #include <stdio.h> | |
| 51 #include <stdlib.h> | |
| 52 | |
| 53 #include "common.h" | |
| 54 #include "avcodec.h" | |
| 55 #include "bitstream.h" | |
| 56 | |
| 4372 | 57 #include <zlib.h> |
| 58 | |
| 3329 | 59 typedef struct FlashSVContext { |
| 60 AVCodecContext *avctx; | |
| 61 AVFrame frame; | |
| 62 int image_width, image_height; | |
| 63 int block_width, block_height; | |
| 64 uint8_t* tmpblock; | |
| 65 int block_size; | |
| 66 z_stream zstream; | |
| 67 } FlashSVContext; | |
| 68 | |
| 69 | |
| 70 static void copy_region(uint8_t *sptr, uint8_t *dptr, | |
| 71 int dx, int dy, int h, int w, int stride) | |
| 72 { | |
| 73 int i; | |
| 74 | |
| 75 for (i = dx+h; i > dx; i--) | |
| 76 { | |
| 77 memcpy(dptr+(i*stride)+dy*3, sptr, w*3); | |
| 78 sptr += w*3; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 | |
| 83 static int flashsv_decode_init(AVCodecContext *avctx) | |
| 84 { | |
| 4827 | 85 FlashSVContext *s = avctx->priv_data; |
| 3329 | 86 int zret; // Zlib return code |
| 87 | |
| 88 s->avctx = avctx; | |
| 89 s->zstream.zalloc = Z_NULL; | |
| 90 s->zstream.zfree = Z_NULL; | |
| 91 s->zstream.opaque = Z_NULL; | |
| 92 zret = inflateInit(&(s->zstream)); | |
| 93 if (zret != Z_OK) { | |
| 94 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); | |
| 95 return 1; | |
| 96 } | |
| 97 avctx->pix_fmt = PIX_FMT_BGR24; | |
| 98 s->frame.data[0] = NULL; | |
| 99 | |
| 100 return 0; | |
| 101 } | |
| 102 | |
| 103 | |
| 104 static int flashsv_decode_frame(AVCodecContext *avctx, | |
| 105 void *data, int *data_size, | |
| 106 uint8_t *buf, int buf_size) | |
| 107 { | |
| 4827 | 108 FlashSVContext *s = avctx->priv_data; |
| 3329 | 109 int h_blocks, v_blocks, h_part, v_part, i, j; |
| 110 GetBitContext gb; | |
| 111 | |
| 112 /* no supplementary picture */ | |
| 113 if (buf_size == 0) | |
| 114 return 0; | |
| 115 | |
| 116 if(s->frame.data[0]) | |
| 117 avctx->release_buffer(avctx, &s->frame); | |
| 118 | |
| 119 init_get_bits(&gb, buf, buf_size * 8); | |
| 120 | |
| 121 /* start to parse the bitstream */ | |
| 122 s->block_width = 16* (get_bits(&gb, 4)+1); | |
| 123 s->image_width = get_bits(&gb,12); | |
| 124 s->block_height= 16* (get_bits(&gb, 4)+1); | |
| 125 s->image_height= get_bits(&gb,12); | |
| 126 | |
| 127 /* calculate amount of blocks and the size of the border blocks */ | |
| 128 h_blocks = s->image_width / s->block_width; | |
| 129 h_part = s->image_width % s->block_width; | |
| 130 v_blocks = s->image_height / s->block_height; | |
| 131 v_part = s->image_height % s->block_height; | |
| 132 | |
| 133 /* the block size could change between frames, make sure the buffer | |
| 134 * is large enough, if not, get a larger one */ | |
| 135 if(s->block_size < s->block_width*s->block_height) { | |
| 136 if (s->tmpblock != NULL) | |
| 137 av_free(s->tmpblock); | |
|
4370
e6eb67453d94
The block_size might be used incorrectly if it is not updated.
banan
parents:
4367
diff
changeset
|
138 if ((s->tmpblock = av_malloc(3*s->block_width*s->block_height)) == NULL) { |
| 3329 | 139 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); |
| 140 return -1; | |
| 141 } | |
| 142 } | |
|
4370
e6eb67453d94
The block_size might be used incorrectly if it is not updated.
banan
parents:
4367
diff
changeset
|
143 s->block_size = s->block_width*s->block_height; |
| 3329 | 144 |
| 145 /* init the image size once */ | |
| 146 if((avctx->width==0) && (avctx->height==0)){ | |
| 147 avctx->width = s->image_width; | |
| 148 avctx->height = s->image_height; | |
| 149 } | |
| 150 | |
| 151 /* check for changes of image width and image height */ | |
| 152 if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) { | |
| 153 av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n"); | |
| 154 av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",avctx->height, | |
| 155 avctx->width,s->image_height,s->image_width); | |
| 156 return -1; | |
| 157 } | |
| 158 | |
| 159 av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n", | |
| 160 s->image_width, s->image_height, s->block_width, s->block_height, | |
| 161 h_blocks, v_blocks, h_part, v_part); | |
| 162 | |
| 163 s->frame.reference = 1; | |
| 164 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID; | |
| 165 if (avctx->get_buffer(avctx, &s->frame) < 0) { | |
| 166 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
| 167 return -1; | |
| 168 } | |
| 169 | |
| 170 /* loop over all block columns */ | |
| 171 for (j = 0; j < v_blocks + (v_part?1:0); j++) | |
| 172 { | |
| 173 | |
| 174 int hp = j*s->block_height; // horiz position in frame | |
| 175 int hs = (j<v_blocks)?s->block_height:v_part; // size of block | |
| 176 | |
| 177 | |
| 178 /* loop over all block rows */ | |
| 179 for (i = 0; i < h_blocks + (h_part?1:0); i++) | |
| 180 { | |
| 181 int wp = i*s->block_width; // vert position in frame | |
| 182 int ws = (i<h_blocks)?s->block_width:h_part; // size of block | |
| 183 | |
| 184 /* get the size of the compressed zlib chunk */ | |
| 185 int size = get_bits(&gb, 16); | |
| 186 | |
| 187 if (size == 0) { | |
| 188 /* no change, don't do anything */ | |
| 189 } else { | |
| 190 /* decompress block */ | |
| 191 int ret = inflateReset(&(s->zstream)); | |
| 192 if (ret != Z_OK) | |
| 193 { | |
| 194 av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j); | |
| 195 /* return -1; */ | |
| 196 } | |
| 197 s->zstream.next_in = buf+(get_bits_count(&gb)/8); | |
| 198 s->zstream.avail_in = size; | |
| 199 s->zstream.next_out = s->tmpblock; | |
| 200 s->zstream.avail_out = s->block_size*3; | |
| 201 ret = inflate(&(s->zstream), Z_FINISH); | |
| 202 if (ret == Z_DATA_ERROR) | |
| 203 { | |
| 204 av_log(avctx, AV_LOG_ERROR, "Zlib resync occured\n"); | |
| 205 inflateSync(&(s->zstream)); | |
| 206 ret = inflate(&(s->zstream), Z_FINISH); | |
| 207 } | |
| 208 | |
| 209 if ((ret != Z_OK) && (ret != Z_STREAM_END)) | |
| 210 { | |
| 211 av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret); | |
| 212 /* return -1; */ | |
| 213 } | |
| 214 copy_region(s->tmpblock, s->frame.data[0], s->image_height-(hp+hs+1), wp, hs, ws, s->frame.linesize[0]); | |
| 215 skip_bits(&gb, 8*size); /* skip the consumed bits */ | |
| 216 } | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 *data_size = sizeof(AVFrame); | |
| 221 *(AVFrame*)data = s->frame; | |
| 222 | |
| 223 if ((get_bits_count(&gb)/8) != buf_size) | |
| 224 av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n", | |
| 225 buf_size, (get_bits_count(&gb)/8)); | |
| 226 | |
| 227 /* report that the buffer was completely consumed */ | |
| 228 return buf_size; | |
| 229 } | |
| 230 | |
| 231 | |
| 232 static int flashsv_decode_end(AVCodecContext *avctx) | |
| 233 { | |
| 4827 | 234 FlashSVContext *s = avctx->priv_data; |
| 3329 | 235 inflateEnd(&(s->zstream)); |
| 236 /* release the frame if needed */ | |
| 237 if (s->frame.data[0]) | |
| 238 avctx->release_buffer(avctx, &s->frame); | |
| 239 | |
| 240 /* free the tmpblock */ | |
| 241 if (s->tmpblock != NULL) | |
| 242 av_free(s->tmpblock); | |
| 243 | |
| 244 return 0; | |
| 245 } | |
| 246 | |
| 247 | |
| 248 AVCodec flashsv_decoder = { | |
| 249 "flashsv", | |
| 250 CODEC_TYPE_VIDEO, | |
| 251 CODEC_ID_FLASHSV, | |
| 252 sizeof(FlashSVContext), | |
| 253 flashsv_decode_init, | |
| 254 NULL, | |
| 255 flashsv_decode_end, | |
| 256 flashsv_decode_frame, | |
| 257 CODEC_CAP_DR1, | |
| 258 .pix_fmts = (enum PixelFormat[]){PIX_FMT_BGR24, -1}, | |
| 259 }; |
