-
Notifications
You must be signed in to change notification settings - Fork 0
/
g1a_utils.c
154 lines (116 loc) · 5.24 KB
/
g1a_utils.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
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
143
144
145
146
147
148
149
150
151
152
153
154
/**************************************************************************
This file is part of Minimalist Casio Assembler (MCA).
MCA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MCA 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with MCA. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
#include "g1a_utils.h"
#include "g1a_header.h"
#ifndef TESTING_PC
#include <fxstdio.h>
#else
#include <stdio.h>
#endif
#include <string.h>
static unsigned char default_icon[68] = {0x00, 0x00, 0x00, 0x04, 0x13, 0x03, 0x24, 0x34, 0x2A, 0x84, 0x54, 0x44, 0x2A, 0xB4, 0x74, 0x44, 0x12, 0x83, 0x57, 0x34, 0x00, 0x00, 0x00, 0x04, 0x80, 0x06, 0x34, 0x44, 0xC0, 0x09, 0x46, 0xC4, 0xE0, 0x0F, 0x25, 0x44, 0xF0, 0x09, 0x14, 0x44, 0xF8, 0x09, 0x64, 0x44, 0xDC, 0x00, 0x00, 0x04, 0x8E, 0x00, 0x00, 0xFC, 0xAF, 0x00, 0x00, 0xFC, 0xAF, 0x80, 0x00, 0xFC, 0x8F, 0xC0, 0x00, 0xFC, 0xDF, 0xE0, 0x00, 0xFC};
int writeG1AFromFile(const FONTCHARACTER *bin, const FONTCHARACTER *outputname, const unsigned char *icon, const char *name) {
#ifndef TESTING_PC
struct G1A_Header header;
int i;
int binaryHandle, outputHandle;
// Open the binary file and return an error if the file can't be opened
binaryHandle = Bfile_OpenFile(bin, _OPENMODE_READ);
if(binaryHandle < 0) return -1;
fillG1AHeader(&header, icon, name, Bfile_GetFileSize(binaryHandle));
// Delete and/or create the G1A file and open it in write mode
Bfile_DeleteFile(outputname);
Bfile_CreateFile(outputname, Bfile_GetFileSize(binaryHandle) + 0x200);
outputHandle = Bfile_OpenFile(outputname, _OPENMODE_WRITE);
if(outputHandle < 0) return -3;
// Write the header
if(Bfile_WriteFile(outputHandle, (void*)(&header), 0x200) < 0) return -4;
// Copy data
#warning Using Bfile_ReadFile and Bfile_WriteFile in a loop is not safe.
//@TODO do the copy more safe ;)
char readbuffer[16384]; // High size because the Bfile_*** fail after some call O_o
int toRead, read;
for(toRead = Bfile_GetFileSize(binaryHandle); toRead>0; toRead -= 16384) {
if((read = Bfile_ReadFile(binaryHandle, readbuffer, 16384, -1)) < 0) return -2;
if (Bfile_WriteFile(outputHandle, readbuffer, read) < 0) return -4;
}
Bfile_CloseFile(binaryHandle);
Bfile_CloseFile(outputHandle);
return 0;
#else
return -1;
#endif
}
int writeG1AFromBuffer(unsigned char *bin, unsigned int binsize, const FONTCHARACTER *outputname, const unsigned char *icon, const char *name) {
struct G1A_Header header;
int outputHandle;
fillG1AHeader(&header, icon, name, binsize);
#ifndef TESTING_PC
// Delete and/or create the G1A file and open it in write mode
Bfile_DeleteFile(outputname);
Bfile_CreateFile(outputname, binsize + 0x200);
outputHandle = Bfile_OpenFile(outputname, _OPENMODE_WRITE);
if(outputHandle < 0) return -1;
// Write the header and copy data
if(Bfile_WriteFile(outputHandle, (void*)(&header), 0x200) < 0) return -2;
if(Bfile_WriteFile(outputHandle, bin, binsize) < 0) return -2;
Bfile_CloseFile(outputHandle);
#else
FILE *outfile = NULL;
outfile = fopen(outputname, "wb+");
if(outfile == NULL) return -1;
fwrite((void*)(&header), sizeof(char), 0x200, outfile);
fwrite(bin, sizeof(char), binsize, outfile);
fflush(outfile);
fclose(outfile);
#endif
return 0;
}
void fillG1AHeader(struct G1A_Header *header, const unsigned char *icon, const char *name, unsigned int binsize) {
int i, tmp;
/* read data into the struct */
memset(header, 0, sizeof(struct G1A_Header)); /* initialize */
memcpy(header->magic,"\xAA\xAC\xBD\xAF\x90\x88\x9A\x8D\x0C\xFF\xEF\xFF\xEF\xFF", sizeof(header->magic));
header->stuff1 = 0xFE;
#ifndef TESTING_PC
header->inverted_filesize = ~(binsize + 0x200); /* NOT'ed */
#else
tmp = ~(binsize + 0x200);
header->inverted_filesize = (tmp<<24) + ((tmp&0x0000FF00)<<8) + ((tmp&0x00FF0000)>>8) + (tmp>>24);
#endif
/* write the checkbytes */
char lowbyte = (char)(header->inverted_filesize & 0x000000FF); /* last (LSB) byte in the long */
header->checkbyte1 = lowbyte - 0x41;
header->checkbyte2 = lowbyte - 0xB8;
header->name_start = '@';
// Copy both header->filename and header->name here
int copy = 1;
for(i=0; i<sizeof(header->filename); i++)
if(copy) if((header->filename[i]=header->name[i]=name[i]) == 0) copy=1;
header->estrip_count = 0;
memcpy(header->version, "Open!.0000", 10);
// Write a static date because the calculator's RTC isn't trusted
memcpy(header->date, "1981.0511.1200", sizeof(header->date));
// The icon bitmap's array size must be 68!
memcpy(header->menu_icon, icon, sizeof(header->menu_icon));
#ifndef TESTING_PC
header->filesize = (binsize + 0x200); /* include size of header */
#else
tmp = binsize + 0x200;
header->filesize = (tmp<<24) + ((tmp&0x0000FF00)<<8) + ((tmp&0x00FF0000)>>8) + (tmp>>24);
#endif
}
const unsigned char *defaultIconG1A() {
return default_icon;
}