[FFmpeg-devel] [PATCH] Add FITS Encoder

Reimar Döffinger Reimar.Doeffinger at gmx.de
Sun Jul 16 20:00:35 EEST 2017


On 16.07.2017, at 14:31, Paras Chadha <paraschadha18 at gmail.com> wrote:
> +static int write_keyword_value(uint8_t **bytestream, const char * keyword, int value)
> +{
> +    int len, ret;
> +    uint8_t * header = * bytestream;
> +    len = strlen(keyword);
> +
> +    memcpy(header, keyword, len);
> +    memset(header + len, ' ', 8 - len);
> +    header[8] = '=';
> +    header[9] = ' ';
> +    header += 10;
> +
> +    ret = snprintf(header, 70, "%d", value);
> +    memset(header + ret, ' ', 70 - ret);

You are unnecessarily complicating this.
Start with memset(header, ' ', 80); and remove all the other memsets (also applies to code further down).
Then only fix up the snprintf 0-termination character.

> +        memcpy(bytestream, "XTENSION= 'IMAGE   '", 20);

Not really sure it is better, so at your discretion, but there are 2 alternatives to doing this (the variable names are probably bad and just for demonstration):
// explicit size dropping 0-termination
static const image_ext_str[20] = "XTENSION= 'IMAGE   '";
memcpy(bytestream, image_ext_str, sizeof(image_ext_str));

or:

static const image_ext_str[] = "XTENSION= 'IMAGE   '";
// -1 to drop 0-termination
memcpy(bytestream, image_ext_str, sizeof(image_ext_str) - 1);

The advantage is that it avoids the "magic" numbers which are easy to get wrong.


More information about the ffmpeg-devel mailing list