comparison 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
comparison
equal deleted inserted replaced
78:fae1d67d8cfd 79:8f90d5addda9
1 #include <stdio.h> 1 #include "common.h"
2 #include <stdint.h>
3 2
4 void set_data(unsigned char* magic, uint16_t version, FILE* target) { 3 #include <string.h>
5 int i; 4
6 fseek(target, 0x46, SEEK_SET); 5 /* hardcoded magic values; stored at 0x18... */
7 fputc(version, target); 6 static const uint8_t magic_veg[16] = {0xEF, 0x29, 0xC4, 0x46, 0x4A, 0x90, 0xD2, 0x11, 0x87, 0x22, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A};
8 for (i=0; i<=sizeof(*magic); ++i) { 7 static const uint8_t magic_vf[16] = {0xF6, 0x1B, 0x3C, 0x53, 0x35, 0xD6, 0xF3, 0x43, 0x8A, 0x90, 0x64, 0xB8, 0x87, 0x23, 0x1F, 0x7F};
9 fseek(target, 0x18+i, SEEK_SET); 8
10 fputc(magic[i], target); 9 int set_file_information(FILE* target, uint8_t version, enum types type) {
11 } 10 const uint8_t* magic = (type == TYPES_VF) ? magic_vf : magic_veg;
11
12 if (fseek(target, 0x46, SEEK_SET))
13 return -1;
14
15 if (fputc(version, target) == EOF)
16 return -1;
17
18 if (fseek(target, 0x18, SEEK_SET))
19 return -1;
20
21 if (fwrite(magic, sizeof(*magic), 16, target) < 16)
22 return -1;
23
24 return 0;
12 } 25 }
13 26
14 int copy_file(char* source_file, char* target_file) { 27 int get_file_information(FILE* input, uint8_t* version, enum types* type) {
15 char ch[4096]; 28 uint8_t magic[16] = {0};
16 FILE *source, *target;
17 29
18 source = fopen(source_file, "rb"); 30 if (fseek(input, 0x46, SEEK_SET))
31 return -1;
19 32
20 if (source == NULL) 33 *version = fgetc(input);
21 return 1;
22 34
23 target = fopen(target_file, "wb"); 35 if (fseek(input, 0x18, SEEK_SET))
36 return -1;
24 37
25 if (target == NULL) { 38 /* read the WHOLE magic, then memcmp */
26 fclose(source); 39 if (fread(magic, sizeof(*magic), ARRAYSIZE(magic), input) < ARRAYSIZE(magic))
27 return 1; 40 return -1;
41
42 if (!memcmp(magic, magic_veg, ARRAYSIZE(magic))) {
43 *type = TYPES_VEG;
44 } else if (!memcmp(magic, magic_vf, ARRAYSIZE(magic))) {
45 *type = TYPES_VF;
46 } else {
47 *type = TYPES_UNKNOWN;
28 } 48 }
29 49
30 while (!feof(source)) {
31 size_t b = fread(ch, 1, sizeof(ch), source);
32 if (b)
33 fwrite(ch, 1, b, target);
34 }
35
36 fclose(target);
37 fclose(source);
38 return 0; 50 return 0;
39 } 51 }