aboutsummaryrefslogtreecommitdiff
path: root/lib/file/decode/fxi.c.draft
blob: eb7b4e0e04639cfb1ef464b57fd163c7ad85bc4d (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* ****************************************************************************
 * file/decode/fxi.c -- decode a fx-Interface file.
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of libcasio.
 * libcasio is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3.0 of the License,
 * or (at your option) any later version.
 *
 * libcasio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libcasio; if not, see <http://www.gnu.org/licenses/>.
 *
 * FXI is the official fx-Interface format. It is an obfuscated, binary format.
 * It is partially described in the Casetta project documentation:
 * https://casetta.tuxfamily.org/formats/fxi
 * ************************************************************************* */
#include "decode.h"
#define FREAD(TO, SZ) { \
	READ(TO, SZ) \
	deobf(TO, SZ); }
#define FDREAD(NAM, TYPE) { \
	DREAD(NAM, TYPE) \
	obf(&NAM, sizeof(struct TYPE)); }

/* ---
 * Obfuscation-related functions.
 * --- */

/**
 *	obf:
 *	Obfuscate AND de-obfuscate a string.
 *
 *	The formula was found by Lephenixnoir, from Planète Casio, out of an array
 *	found by Alexis Soulard, from Casioland (with a 14-years time lapse, yay!).
 *
 *	The formula is an involution, which means the obfuscation and
 *	de-obfuscation is done using the same formula.
 *
 *	@arg	s	the string to deobfuscate.
 *	@arg	n	the string length.
 */

#define deobf(S, N) obf(S, N)
CASIO_LOCAL void obf(unsigned char *s, size_t n)
{
	while (n--)
		*s++ ^= ~24;
}

/* ---
 * File header.
 * --- */

static unsigned char header[] =
	"\xD5\xD7\x1F" "f" "FX-INTERFACE - YELLOW COMPUTING" /* 35 */

	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" /* 20 */
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\x06"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\x07" /* entries total? */
	"\0\xFF\xFF" "\0\0\x0F\0"
	"CY" "CFXListItem"
	
	"NP\0\0"       "\x00" "\0\0\0" "\x00\0" "\x03\0"
	"\x15\x18" "\0\0\x00" "\0\x00" "\xA4\xA0"
	"\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

	"\x01\x80\0\0" "\x01" "\0\0\0" "\x01\0" "\x03\0"
	"\x55\x1A" "\0\0\x04" "\0\x00" "\x5B\x80"
	"\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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\0\0"

	"\x01\x80\0\0" "\x06" "\0\0\0" "\x01\0" "\x00\0"
	"\x08\x11" "\0\0\x1E" "\0\x00" "\x83\x80"
	"\0\0\0\0\0\0\0\0"  "\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

	/* from here, it is a real file, SpeedSnk.fxi */

	"\x01" /* number of records? */ "\x00"

	"\x01\x80\0\0" "\x00" "\0\0\0" "\x02\0" "\x01\0"
	"\xBA\x1E" "\0\0\x22" "\0\x08" /* >program name size */
	"SpeedSnk" "\x83\x80" "\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
	"\0\0\0\0\0\0\0\0\0\0\0\0" "SpeedSnk"
	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

	/* subheader? */
	"\0\0\0\0\0\0\0" "\0\1\0\1" "\0" "\xAF\x12"

	/* text file header */
	"TXT\0PG\0\0\x12\x81" "SpeedSnk"
	"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
	"NL" "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"

	/* ... program content */
	"\xFF" "\0\0\0\0\0\0"
;