diff --git a/include/level.h b/include/level.h index 835cf16..c5abc00 100644 --- a/include/level.h +++ b/include/level.h @@ -3,10 +3,11 @@ #pragma once +typedef unsigned int tile_t; struct Level { int width; int height; - int *data; + tile_t *data; }; void level_read(struct Level*, char *path); diff --git a/src/level.c b/src/level.c index d040571..5a42756 100644 --- a/src/level.c +++ b/src/level.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2021 KikooDX */ -#include #include #include #include @@ -10,18 +9,18 @@ static const int kble_fmt_version = 0; -static int read_byte(FILE *file); -static int read_byte_group(FILE *file, int size); -static void write_byte(FILE *file, unsigned char byte); -static void write_byte_group(FILE *file, int value, int size); +static char read_byte(FILE *file); +static tile_t read_byte_group(FILE *file, int size); +static void write_byte(FILE *file, char byte); +static void write_byte_group(FILE *file, tile_t value, int size); void level_read(struct Level *level, char *path) { FILE *file = NULL; - int byte = 0; - int tile_size = 0; + char byte = 0; + unsigned int tile_size = 0; int level_size = 0; - int tile = 0; int i = 0; + tile_t tile = 0; /* open file in read mode */ file = fopen(path, "rb"); @@ -40,6 +39,12 @@ void level_read(struct Level *level, char *path) { /* get tile size (in bytes) */ tile_size = read_byte(file); + /* check than tile size is in boundaries */ + if (tile_size > sizeof(tile_t) / sizeof(char)) { + fprintf(stderr, "ERROR: tile size is too big ; " + "maximum is %ld, found %d", sizeof(tile_t), tile_size); + exit(EXIT_FAILURE); + } /* get width */ level->width = read_byte_group(file, 2); /* get height */ @@ -48,9 +53,9 @@ void level_read(struct Level *level, char *path) { /* allocate memory for data */ level_size = level->width * level->height; if (level->data == NULL) { - level->data = malloc(level_size * sizeof(int)); + level->data = malloc(level_size * sizeof(tile_t)); } else { - level->data = realloc(level->data, level_size * sizeof(int)); + level->data = realloc(level->data, level_size * sizeof(tile_t)); } /* check for allocation failure */ if (level->data == NULL) { @@ -116,32 +121,30 @@ void level_write(struct Level level, char *path) { } /* Read a single byte safely (handle EOF). */ -static int read_byte(FILE *file) { +static char read_byte(FILE *file) { const int byte = fgetc(file); if (byte == EOF) { fprintf(stderr, "ERROR: unexpected EOF\n"); exit(EXIT_FAILURE); } - return byte; + return (char)byte; } /* Read multiple bytes and "merge" them into one integer. */ -static int read_byte_group(FILE *file, int size) { +static tile_t read_byte_group(FILE *file, int size) { int group = 0; - uint8_t *byte_ptr = (uint8_t*)&group; + char *byte_ptr = (char*)&group; int i = 0; byte_ptr += size; for (i = 0; i < size; i += 1) { - byte_ptr -= 1; + byte_ptr -= 1, *byte_ptr = read_byte(file); - printf("%d ", *byte_ptr); } - printf("%d\n", group); return group; } /* Write a single byte safely (handle EOF). */ -static void write_byte(FILE *file, unsigned char byte) { +static void write_byte(FILE *file, char byte) { const int result = fputc(byte, file); if (result == EOF) { fprintf(stderr, "ERROR: file write error\n"); @@ -150,14 +153,12 @@ static void write_byte(FILE *file, unsigned char byte) { } /* Write an integer as multiple bytes. */ -static void write_byte_group(FILE *file, int value, int size) { - uint8_t *value_ptr = (uint8_t*)&value; +static void write_byte_group(FILE *file, tile_t value, int size) { + char *value_ptr = (char*)&value; value_ptr += size; int i = 0; for (i = 0; i < size; i += 1) { value_ptr -= 1; - printf("%d ", *value_ptr); write_byte(file, *value_ptr); } - printf("\n"); }