gif

read/write GIFs
git clone git://bvnf.space/gm.git
Log | Files | Refs | README

commit 5080ee84b8890c63119bab1e49536cd45fc7e5f6
parent 6c8797ad5b3a3caef409000333e6671a26d73b5e
Author: phoebos <ben@bvnf.space>
Date:   Fri, 27 Aug 2021 04:02:01 +0100

basic version check and [!,;]

Diffstat:
Mgif.c | 55++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 54 insertions(+), 1 deletion(-)

diff --git a/gif.c b/gif.c @@ -8,13 +8,54 @@ #include <fcntl.h> #include <stdio.h> #include <stdlib.h> -#include <strings.h> +#include <string.h> #include <unistd.h> #define BUF_SIZE 100 #define FAIL_GIF 1 #define FAIL_SYS 2 +typedef char rgb[3]; +struct Gif { + char version[3]; /* 87a or 89a */ + rgb colormap[255]; + unsigned char *pixels; /* array of indices to colors */ +}; + + +int +gif_decode(unsigned char *buf, size_t len){ + struct Gif g = {0 }; + int cur_block; + enum { HEAD = 1, SPEC = 2, IMGE = 4 }; + cur_block = HEAD; + + if (strncmp((char *)buf, "GIF", 3)) return FAIL_GIF; + buf += 3; + len -= 3; + strncpy(g.version, (char *)buf, 3); + fprintf(stderr, "GIF version %s\n", g.version); + buf += 3; + len -= 3; + + while (--len) { + ++buf; + switch (*buf) { + case '!': + cur_block = SPEC; + break; + case ',': + cur_block = IMGE; + break; + case ';': + /* last char of image section */ + return 0; + } + } + + return 0; +} + int main(int argc, char **argv){ int fd; @@ -73,6 +114,18 @@ main(int argc, char **argv){ return FAIL_SYS; } + int ret = gif_decode(buf, used); + switch (ret) { + case FAIL_GIF: + fprintf(stderr, "error: invalid GIF format\n"); + return FAIL_GIF; + case 0: + break; + default: + return ret; + } + + free(buf); return 0; }