Mercurial > msvpvf
diff src/common.c @ 79:8f90d5addda9 v2.0
*: refactor... basically everything!
The Win32 GUI version is now unicode-friendly. HOWEVER, ANSI still very
much works. you can configure which version to use through `-DUNICODE=0/1`
in CFLAGS.
the CLI is also friendlier and uses a more sane interface as well.
note: the command line flags (which were optional before) are now required.
Unicode filenames will not work on Windows because Windows sucks.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 20 Mar 2024 17:06:26 -0400 |
parents | fcd4b9fe957b |
children |
line wrap: on
line diff
--- a/src/common.c Sun Oct 01 03:03:29 2023 -0400 +++ b/src/common.c Wed Mar 20 17:06:26 2024 -0400 @@ -1,39 +1,51 @@ -#include <stdio.h> -#include <stdint.h> +#include "common.h" + +#include <string.h> + +/* hardcoded magic values; stored at 0x18... */ +static const uint8_t magic_veg[16] = {0xEF, 0x29, 0xC4, 0x46, 0x4A, 0x90, 0xD2, 0x11, 0x87, 0x22, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A}; +static const uint8_t magic_vf[16] = {0xF6, 0x1B, 0x3C, 0x53, 0x35, 0xD6, 0xF3, 0x43, 0x8A, 0x90, 0x64, 0xB8, 0x87, 0x23, 0x1F, 0x7F}; + +int set_file_information(FILE* target, uint8_t version, enum types type) { + const uint8_t* magic = (type == TYPES_VF) ? magic_vf : magic_veg; -void set_data(unsigned char* magic, uint16_t version, FILE* target) { - int i; - fseek(target, 0x46, SEEK_SET); - fputc(version, target); - for (i=0; i<=sizeof(*magic); ++i) { - fseek(target, 0x18+i, SEEK_SET); - fputc(magic[i], target); - } + if (fseek(target, 0x46, SEEK_SET)) + return -1; + + if (fputc(version, target) == EOF) + return -1; + + if (fseek(target, 0x18, SEEK_SET)) + return -1; + + if (fwrite(magic, sizeof(*magic), 16, target) < 16) + return -1; + + return 0; } -int copy_file(char* source_file, char* target_file) { - char ch[4096]; - FILE *source, *target; +int get_file_information(FILE* input, uint8_t* version, enum types* type) { + uint8_t magic[16] = {0}; - source = fopen(source_file, "rb"); + if (fseek(input, 0x46, SEEK_SET)) + return -1; + + *version = fgetc(input); + + if (fseek(input, 0x18, SEEK_SET)) + return -1; - if (source == NULL) - return 1; + /* read the WHOLE magic, then memcmp */ + if (fread(magic, sizeof(*magic), ARRAYSIZE(magic), input) < ARRAYSIZE(magic)) + return -1; - target = fopen(target_file, "wb"); - - if (target == NULL) { - fclose(source); - return 1; + if (!memcmp(magic, magic_veg, ARRAYSIZE(magic))) { + *type = TYPES_VEG; + } else if (!memcmp(magic, magic_vf, ARRAYSIZE(magic))) { + *type = TYPES_VF; + } else { + *type = TYPES_UNKNOWN; } - while (!feof(source)) { - size_t b = fread(ch, 1, sizeof(ch), source); - if (b) - fwrite(ch, 1, b, target); - } - - fclose(target); - fclose(source); return 0; }