1
0
Fork 0

Add parsing of magic number

This commit is contained in:
Hans-Joerg Schurr 2020-02-21 20:34:29 +01:00
parent 8f6d3f6897
commit e79d365498
4 changed files with 89 additions and 28 deletions

View File

@ -1,21 +1,11 @@
NULL =
bin_PROGRAMS = hvif
bin_PROGRAMS = hvif
# veriT configuration
hvif_CPPFLAGS = \
$(AM_CPPFLAGS) \
-I$(top_builddir)/src \
$(NULL)
hvif_LDADD = \
$(AM_LDADD) \
-lm \
$(NULL)
hvif_SOURCES = \
$(BUILT_SOURCES) \
src/main.c \
src/hvif-light.c \
src/hvif-light.h \
$(NULL)
#veriT configuration
hvif_CPPFLAGS = $(AM_CPPFLAGS) - I$(top_builddir) / src $(NULL) hvif_LDADD =
$(AM_LDADD) -
lm $(NULL)
hvif_SOURCES = $(BUILT_SOURCES) src / main.c src / hvif -
light.c src / hvif - light.h $(NULL)

View File

@ -1,5 +1,44 @@
#include "hvif-light.h"
unsigned test(unsigned x) {
return 2*x;
#include <stdint.h>
#include <stdlib.h>
char const* const eof_error = "File ended prematurely.";
#define le_read(V, BUFFER) V = _Generic((V), uint32_t : le_read_uint32)(BUFFER)
static inline uint32_t
le_read_uint32(char buffer[static 1])
{
return (buffer[0] << 0) | (buffer[1] << 8) | (buffer[2] << 16) |
(buffer[3] << 24);
}
#define ERROR_RESULT(M) \
(hvif_result) { .success = false, .error = M }
struct hvif_image
{};
hvif_result
hvif_from_file(FILE* file)
{
uint32_t const magic = 0x6669636e; /* 'finc' (on disk little endian 'cnif') */
char magic_buffer[4];
if (fread(magic_buffer, 1, 4, file) != 4) { return ERROR_RESULT(eof_error); }
uint32_t read_magic;
le_read(read_magic, magic_buffer);
if (read_magic != magic) {
printf("%04x %04x\n", magic, read_magic);
return ERROR_RESULT("Wrong magic number.");
}
hvif_image* image = malloc(sizeof(hvif_image));
return (hvif_result){ .success = true, .image = image };
}
void
hvif_free(hvif_image* image)
{
free(image);
}

View File

@ -1,7 +1,23 @@
#ifndef HVIF_LIGHT_H
#define HVIF_LIGHT_H
unsigned test(unsigned x);
#include <stdbool.h>
#include <stdio.h>
typedef struct hvif_image hvif_image;
typedef struct hvif_result hvif_result;
struct hvif_result
{
bool success;
union
{
char const* const error;
hvif_image* image;
};
};
hvif_result hvif_from_file(FILE* file);
void hvif_free(hvif_image* image);
#endif // HVIF_LIGHT_H

View File

@ -1,13 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "hvif-light.h"
int main(void) {
unsigned x = 12;
unsigned y = test(x);
printf("This is " PACKAGE_STRING " and 2*%u = %u.\n", x, y);
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
puts("This is " PACKAGE_STRING ".");
FILE* imagefile = fopen("test.hvif", "r");
if (!imagefile) {
fputs("fopen failed.\n", stderr);
return EXIT_FAILURE;
}
hvif_result result = hvif_from_file(imagefile);
if (!result.success) {
fputs("Reading image failed.\n", stderr);
fputs(result.error, stderr);
fputc('\n', stderr);
return EXIT_FAILURE;
}
puts("Reading image succeeded.");
hvif_free(result.image);
return EXIT_SUCCESS;
}