Mercurial > libavcodec.hg
annotate bmpenc.c @ 6532:8aafb712389e libavcodec
Fix possible heap overflow caused by av_fast_realloc()
author | michael |
---|---|
date | Tue, 25 Mar 2008 10:15:17 +0000 |
parents | 48759bfbd073 |
children | 6eeb19edcee3 |
rev | line source |
---|---|
4477 | 1 /* |
2 * BMP image format encoder | |
3 * Copyright (c) 2006, 2007 Michel Bardiaux | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 #include "avcodec.h" | |
23 #include "bytestream.h" | |
24 #include "bmp.h" | |
25 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
4477
diff
changeset
|
26 static av_cold int bmp_encode_init(AVCodecContext *avctx){ |
4477 | 27 BMPContext *s = avctx->priv_data; |
28 | |
29 avcodec_get_frame_defaults((AVFrame*)&s->picture); | |
30 avctx->coded_frame = (AVFrame*)&s->picture; | |
31 | |
32 return 0; | |
33 } | |
34 | |
35 static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | |
36 BMPContext *s = avctx->priv_data; | |
37 AVFrame *pict = data; | |
38 AVFrame * const p= (AVFrame*)&s->picture; | |
39 int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize; | |
40 uint8_t *ptr; | |
41 unsigned char* buf0 = buf; | |
42 *p = *pict; | |
43 p->pict_type= FF_I_TYPE; | |
44 p->key_frame= 1; | |
45 n_bytes_per_row = (avctx->width*3 + 3) & ~3; | |
46 n_bytes_image = avctx->height*n_bytes_per_row; | |
47 | |
48 // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER | |
49 // and related pages. | |
50 #define SIZE_BITMAPFILEHEADER 14 | |
51 #define SIZE_BITMAPINFOHEADER 40 | |
52 hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER; | |
53 n_bytes = n_bytes_image + hsize; | |
54 if(n_bytes>buf_size) { | |
55 av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", n_bytes, buf_size); | |
56 return -1; | |
57 } | |
58 bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType | |
59 bytestream_put_byte(&buf, 'M'); // do. | |
60 bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize | |
61 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1 | |
62 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2 | |
63 bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits | |
64 bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize | |
65 bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth | |
66 bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight | |
67 bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes | |
68 bytestream_put_le16(&buf, 24); // BITMAPINFOHEADER.biBitCount | |
69 bytestream_put_le32(&buf, BMP_RGB); // BITMAPINFOHEADER.biCompression | |
70 bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage | |
71 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter | |
72 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter | |
73 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed | |
74 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant | |
75 // BMP files are bottom-to-top so we start from the end... | |
76 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0]; | |
77 buf = buf0 + hsize; | |
78 for(i = 0; i < avctx->height; i++) { | |
79 n = 3*avctx->width; | |
80 memcpy(buf, ptr, n); | |
81 buf += n; | |
82 memset(buf, 0, n_bytes_per_row-n); | |
83 buf += n_bytes_per_row-n; | |
84 ptr -= p->linesize[0]; // ... and go back | |
85 } | |
86 return n_bytes; | |
87 } | |
88 | |
89 AVCodec bmp_encoder = { | |
90 "bmp", | |
91 CODEC_TYPE_VIDEO, | |
92 CODEC_ID_BMP, | |
93 sizeof(BMPContext), | |
94 bmp_encode_init, | |
95 bmp_encode_frame, | |
96 NULL, //encode_end, | |
97 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, -1}, | |
98 }; |