summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2016-08-22 23:05:56 (GMT)
committer Vincent Sanders <vince@kyllikki.org>2016-08-22 23:05:56 (GMT)
commit110f27feea25760234b1bf3d1725108ece99d7ad (patch)
tree03a4936596230caa838a380ce741ed3b402a6191
parent9d21a4b86283aa5618f34988e50b5f6ef67406f1 (diff)
downloadlibnsbmp-110f27feea25760234b1bf3d1725108ece99d7ad.tar.gz
libnsbmp-110f27feea25760234b1bf3d1725108ece99d7ad.tar.bz2
update icon decoding test to allow for output file on commandline
-rw-r--r--test/decode_ico.c206
-rwxr-xr-xtest/runtest.sh6
2 files changed, 108 insertions, 104 deletions
diff --git a/test/decode_ico.c b/test/decode_ico.c
index 266b3bb..5dbc7a5 100644
--- a/test/decode_ico.c
+++ b/test/decode_ico.c
@@ -29,12 +29,92 @@
/* White with alpha masking. */
#define TRANSPARENT_COLOR 0xffffffff
-unsigned char *load_file(const char *path, size_t *data_size);
-void warning(const char *context, bmp_result code);
-void *bitmap_create(int width, int height, unsigned int state);
-unsigned char *bitmap_get_buffer(void *bitmap);
-size_t bitmap_get_bpp(void *bitmap);
-void bitmap_destroy(void *bitmap);
+static void *bitmap_create(int width, int height, unsigned int state)
+{
+ (void) state; /* unused */
+ return calloc(width * height, BYTES_PER_PIXEL);
+}
+
+
+static unsigned char *bitmap_get_buffer(void *bitmap)
+{
+ assert(bitmap);
+ return bitmap;
+}
+
+
+static size_t bitmap_get_bpp(void *bitmap)
+{
+ (void) bitmap; /* unused */
+ return BYTES_PER_PIXEL;
+}
+
+
+static void bitmap_destroy(void *bitmap)
+{
+ assert(bitmap);
+ free(bitmap);
+}
+
+static unsigned char *load_file(const char *path, size_t *data_size)
+{
+ FILE *fd;
+ struct stat sb;
+ unsigned char *buffer;
+ size_t size;
+ size_t n;
+
+ fd = fopen(path, "rb");
+ if (!fd) {
+ perror(path);
+ exit(EXIT_FAILURE);
+ }
+
+ if (stat(path, &sb)) {
+ perror(path);
+ exit(EXIT_FAILURE);
+ }
+ size = sb.st_size;
+
+ buffer = malloc(size);
+ if (!buffer) {
+ fprintf(stderr, "Unable to allocate %lld bytes\n",
+ (long long) size);
+ exit(EXIT_FAILURE);
+ }
+
+ n = fread(buffer, 1, size, fd);
+ if (n != size) {
+ perror(path);
+ exit(EXIT_FAILURE);
+ }
+
+ fclose(fd);
+
+ *data_size = size;
+ return buffer;
+}
+
+
+static void warning(const char *context, bmp_result code)
+{
+ fprintf(stderr, "%s failed: ", context);
+ switch (code) {
+ case BMP_INSUFFICIENT_MEMORY:
+ fprintf(stderr, "BMP_INSUFFICIENT_MEMORY");
+ break;
+ case BMP_INSUFFICIENT_DATA:
+ fprintf(stderr, "BMP_INSUFFICIENT_DATA");
+ break;
+ case BMP_DATA_ERROR:
+ fprintf(stderr, "BMP_DATA_ERROR");
+ break;
+ default:
+ fprintf(stderr, "unknown code %i", code);
+ break;
+ }
+ fprintf(stderr, "\n");
+}
static void write_pam(FILE* fh, const char *name, struct bmp_image *bmp)
@@ -104,13 +184,22 @@ int main(int argc, char *argv[])
size_t size;
unsigned short res = 0;
unsigned char *data;
+ FILE *outf = stdout;
- if ((argc < 2) || (argc > 4)) {
- fprintf(stderr, "Usage: %s collection.ico [width=255] [height=255]\n", argv[0]);
+ if ((argc < 2) || (argc > 5)) {
+ fprintf(stderr, "Usage: %s collection.ico [width=255] [height=255] [outfile]\n", argv[0]);
return 1;
}
width = (argc >= 3) ? atoi(argv[2]) : 255;
- height = (argc == 4) ? atoi(argv[3]) : 255;
+ height = (argc >= 4) ? atoi(argv[3]) : 255;
+
+ if (argc >= 5) {
+ outf = fopen(argv[4], "w+");
+ if (outf == NULL) {
+ fprintf(stderr, "Unable to open %s for writing\n", argv[2]);
+ return 2;
+ }
+ }
/* create our bmp image */
ico_collection_create(&ico, &bitmap_callbacks);
@@ -122,7 +211,7 @@ int main(int argc, char *argv[])
code = ico_analyse(&ico, size, data);
if (code != BMP_OK) {
warning("ico_analyse", code);
- res = 1;
+ res = 3;
goto cleanup;
}
@@ -149,9 +238,13 @@ int main(int argc, char *argv[])
}
if (bmp->opaque) {
- write_ppm(stdout, argv[1], bmp);
+ write_ppm(outf, argv[1], bmp);
} else {
- write_pam(stdout, argv[1], bmp);
+ write_pam(outf, argv[1], bmp);
+ }
+
+ if (argc >= 5) {
+ fclose(outf);
}
cleanup:
@@ -161,92 +254,3 @@ cleanup:
return res;
}
-
-
-unsigned char *load_file(const char *path, size_t *data_size)
-{
- FILE *fd;
- struct stat sb;
- unsigned char *buffer;
- size_t size;
- size_t n;
-
- fd = fopen(path, "rb");
- if (!fd) {
- perror(path);
- exit(EXIT_FAILURE);
- }
-
- if (stat(path, &sb)) {
- perror(path);
- exit(EXIT_FAILURE);
- }
- size = sb.st_size;
-
- buffer = malloc(size);
- if (!buffer) {
- fprintf(stderr, "Unable to allocate %lld bytes\n",
- (long long) size);
- exit(EXIT_FAILURE);
- }
-
- n = fread(buffer, 1, size, fd);
- if (n != size) {
- perror(path);
- exit(EXIT_FAILURE);
- }
-
- fclose(fd);
-
- *data_size = size;
- return buffer;
-}
-
-
-void warning(const char *context, bmp_result code)
-{
- fprintf(stderr, "%s failed: ", context);
- switch (code) {
- case BMP_INSUFFICIENT_MEMORY:
- fprintf(stderr, "BMP_INSUFFICIENT_MEMORY");
- break;
- case BMP_INSUFFICIENT_DATA:
- fprintf(stderr, "BMP_INSUFFICIENT_DATA");
- break;
- case BMP_DATA_ERROR:
- fprintf(stderr, "BMP_DATA_ERROR");
- break;
- default:
- fprintf(stderr, "unknown code %i", code);
- break;
- }
- fprintf(stderr, "\n");
-}
-
-
-void *bitmap_create(int width, int height, unsigned int state)
-{
- (void) state; /* unused */
- return calloc(width * height, BYTES_PER_PIXEL);
-}
-
-
-unsigned char *bitmap_get_buffer(void *bitmap)
-{
- assert(bitmap);
- return bitmap;
-}
-
-
-size_t bitmap_get_bpp(void *bitmap)
-{
- (void) bitmap; /* unused */
- return BYTES_PER_PIXEL;
-}
-
-
-void bitmap_destroy(void *bitmap)
-{
- assert(bitmap);
- free(bitmap);
-}
diff --git a/test/runtest.sh b/test/runtest.sh
index 8e2b33c..27df29c 100755
--- a/test/runtest.sh
+++ b/test/runtest.sh
@@ -24,14 +24,14 @@ BMPTESTS="${BMPTESTS} test/afl-bmp/*.bmp"
# icon test directories
ICOTESTS="test/icons/*.ico"
-# afl ico suite
+# afl ico demo suite
ICOTESTS="${ICOTESTS} test/afl-ico/*.ico"
bmpdecode()
{
OUTF=$(basename ${1} .bmp)
echo "Bitmap:${1}" >> ${TEST_LOG}
- ${TEST_PATH}/test_decode_bmp ${1} > ${TEST_OUT}/${OUTF}.ppm 2>> ${TEST_LOG}
+ ${TEST_PATH}/test_decode_bmp ${1} ${TEST_OUT}/${OUTF}.ppm 2>> ${TEST_LOG}
ECODE=$?
echo "Exit code:${ECODE}" >> ${TEST_LOG}
return ${ECODE}
@@ -42,7 +42,7 @@ icodecode()
OUTF=$(basename ${1} .ico)
CMPF=$(dirname ${1})/${OUTF}.ppm
echo "Icon:${1}" >> ${TEST_LOG}
- ${TEST_PATH}/test_decode_ico ${1} > ${TEST_OUT}/${OUTF}.ppm 2>> ${TEST_LOG}
+ ${TEST_PATH}/test_decode_ico ${1} 255 255 ${TEST_OUT}/${OUTF}.ppm 2>> ${TEST_LOG}
if [ -f "${CMPF}" ]; then
cmp ${CMPF} ${TEST_OUT}/${OUTF}.ppm >> ${TEST_LOG} 2>> ${TEST_LOG}
if [ "$?" -ne 0 ]; then