This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pi-nbt.c
97 lines (82 loc) · 2.72 KB
/
pi-nbt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <byteswap.h>
// Read Bytes From File And Then Reset Cursor Position
static int fread_and_reset(FILE *stream, unsigned char *buff, int size) {
size = fread(buff, 1, size, stream);
fseek(stream, -size, SEEK_CUR);
return size;
}
// Copy File Contents
static void copy_file(FILE *input, FILE *output) {
int c;
while ((c = fgetc(input)) != EOF) {
fputc(c, output);
}
}
// Get File Size
static long int get_file_size(FILE *stream) {
fseek(stream, 0, SEEK_END);
long int size = ftell(stream);
fseek(stream, 0, SEEK_SET);
return size;
}
// Write Integer To File
static void write_int(FILE *stream, uint32_t x) {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
// Integer Must Be Little-Endian
x = __bswap_32(x);
#endif
fwrite(&x, 4, 1, stream);
}
#define STORAGE_VERSION 3
#define INVALID_ARGS() fprintf(stderr, "Invalid Arguments\n"); exit(1);
int main(int argc, char *argv[]) {
if (argc == 4) {
int add_header;
if (strcmp(argv[1], "add-header") == 0) {
add_header = 1;
} else if (strcmp(argv[1], "remove-header") == 0) {
add_header = 0;
} else {
INVALID_ARGS();
}
FILE *input = fopen(argv[2], "rb");
FILE *output = fopen(argv[3], "wb");
if (input == NULL || output == NULL) {
fprintf(stderr, "Unable To Open Input/Output File(s)\n");
exit(1);
}
uint32_t first_byte = 0;
int bytes_read = fread_and_reset(input, (unsigned char *) &first_byte, 4);
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
// Convert Little-Endian To Big-Endian
first_byte = __bswap_32(first_byte);
#endif
int input_has_header = bytes_read == 4 && first_byte == STORAGE_VERSION;
if ((add_header && input_has_header) || (!add_header && !input_has_header)) {
fprintf(stderr, add_header ? "Header Already Exists\n" : "Header Is Already Removed\n");
copy_file(input, output);
} else if (add_header) {
fprintf(stderr, "Adding Header...\n");
write_int(output, STORAGE_VERSION);
uint32_t size = get_file_size(input);
write_int(output, size);
copy_file(input, output);
} else {
fprintf(stderr, "Removing Header...\n");
fseek(input, 8, SEEK_CUR);
copy_file(input, output);
}
fprintf(stderr, "Done\n");
fclose(input);
fclose(output);
} else if (argc == 1) {
fprintf(stderr, "HELP:\n %s <add-header|remove-header> <input-file> <output-file>\n", argv[0]);
exit(1);
} else {
INVALID_ARGS();
}
}