annotate src/common.c @ 80:719570851563

Added tag v2.0 for changeset 8f90d5addda9
author Paper <paper@paper.us.eu.org>
date Wed, 20 Mar 2024 17:09:43 -0400
parents 8f90d5addda9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
79
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
1 #include "common.h"
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
2
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
3 #include <string.h>
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
4
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
5 /* hardcoded magic values; stored at 0x18... */
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
6 static const uint8_t magic_veg[16] = {0xEF, 0x29, 0xC4, 0x46, 0x4A, 0x90, 0xD2, 0x11, 0x87, 0x22, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A};
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
7 static const uint8_t magic_vf[16] = {0xF6, 0x1B, 0x3C, 0x53, 0x35, 0xD6, 0xF3, 0x43, 0x8A, 0x90, 0x64, 0xB8, 0x87, 0x23, 0x1F, 0x7F};
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
8
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
9 int set_file_information(FILE* target, uint8_t version, enum types type) {
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
10 const uint8_t* magic = (type == TYPES_VF) ? magic_vf : magic_veg;
47
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
79
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
12 if (fseek(target, 0x46, SEEK_SET))
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
13 return -1;
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
14
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
15 if (fputc(version, target) == EOF)
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
16 return -1;
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
17
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
18 if (fseek(target, 0x18, SEEK_SET))
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
19 return -1;
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
20
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
21 if (fwrite(magic, sizeof(*magic), 16, target) < 16)
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
22 return -1;
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
23
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
24 return 0;
47
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 }
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26
79
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
27 int get_file_information(FILE* input, uint8_t* version, enum types* type) {
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
28 uint8_t magic[16] = {0};
47
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29
79
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
30 if (fseek(input, 0x46, SEEK_SET))
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
31 return -1;
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
32
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
33 *version = fgetc(input);
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
34
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
35 if (fseek(input, 0x18, SEEK_SET))
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
36 return -1;
47
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37
79
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
38 /* read the WHOLE magic, then memcmp */
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
39 if (fread(magic, sizeof(*magic), ARRAYSIZE(magic), input) < ARRAYSIZE(magic))
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
40 return -1;
47
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41
79
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
42 if (!memcmp(magic, magic_veg, ARRAYSIZE(magic))) {
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
43 *type = TYPES_VEG;
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
44 } else if (!memcmp(magic, magic_vf, ARRAYSIZE(magic))) {
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
45 *type = TYPES_VF;
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
46 } else {
8f90d5addda9 *: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents: 58
diff changeset
47 *type = TYPES_UNKNOWN;
47
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 }
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 return 0;
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 }