-
Notifications
You must be signed in to change notification settings - Fork 1
/
makehtml.c
225 lines (205 loc) · 4.86 KB
/
makehtml.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <ctype.h>
#include <inttypes.h>
#include <iso646.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <unistd.h>
#include "base64.h"
#include "dprintf.h"
#include "endian.h"
#include "err.h"
#include "hexdump.h"
#include "makehtml.h"
#include "sfo.h"
#include "version.h"
const char paramsfo_name[] = "PSP_GAME/PARAM.SFO";
const char icon_name[] = "PSP_GAME/ICON0.PNG";
const char html_header[] =
R"(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="generator" content="%s"/>
<title>PSP Master Disc - %s</title>
<style type="text/css">
body {
font-family: sans-serif;
}
@media (min-width: 1200px) {
body {
max-width: 1000px;
margin-left: auto;
margin-right: auto;
}
}
table {border: 1px solid lightgrey; border-collapse: collapse;table-layout:fixed;margin-bottom:1em;}
caption {background-color: grey; color: white;font: italic bold 1.5em sans-serif;padding:10px;}
td {border: 1px solid lightgrey; font: 1em monospace;padding:10px;}
th {font: 1em monospace;}
th:nth-child(1),th:nth-child(2) {width:150px;}
.note {color: gray; border-bottom: 1px dotted;}
.keydesc {color: gray; font-style: italic;}
</style>
</head>
<body>
<h1>PSP Master Disc</h1>
<table id="params">
<caption>%s%s</caption>
<thead>
<tr>
<th scope="col">key</th>
<th scope="col">value</th>
</tr>
</thead>
<tbody>
)";
void z(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
char *make_icontag(uint8_t *png, size_t pnglen)
{
const char template[] = "<img src=\"data:image/png;base64,%s\" alt=\"game icon\"/><br />";
char *b64;
char *out;
size_t outlen;
b64 = base64_encode(png, pnglen);
if (!b64) {
return NULL;
}
outlen = strlen(b64);
outlen += strlen(template);
outlen -= strlen("%s");
outlen += 1;
out = malloc(outlen);
if (!out) {
free(b64);
return NULL;
}
sprintf(out, template, b64);
free(b64);
out[outlen - 1] = '\0';
return out;
}
int make_html(iso9660_t *ctx, int outfd)
{
size_t paramsfo_bytes;
size_t paramsfo_secsize;
size_t paramsfo_lsn;
size_t icon_bytes;
size_t icon_secsize;
size_t icon_lsn;
uint8_t *paramsfo_buf = NULL;
uint8_t *icon_buf = NULL;
char *icontag = NULL;
iso9660_stat_t *sb;
struct sfo_ctx sfoctx;
long int rc;
// 'z' is a utility function for appending string to outfd
void z(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vdprintf(outfd, fmt, ap);
va_end(ap);
}
sb = iso9660_ifs_stat_translate(ctx, paramsfo_name);
if (!sb) return -1;
paramsfo_bytes = sb->size;
paramsfo_secsize = sb->secsize;
paramsfo_lsn = sb->lsn;
iso9660_stat_free(sb);
sb = NULL;
sb = iso9660_ifs_stat_translate(ctx, icon_name);
if (sb) {
icon_bytes = sb->size;
icon_secsize = sb->secsize;
icon_lsn = sb->lsn;
iso9660_stat_free(sb);
sb = NULL;
} else {
icon_bytes = 0;
}
paramsfo_buf = calloc(ISO_BLOCKSIZE, paramsfo_secsize);
if (!paramsfo_buf) return -2;
rc = iso9660_iso_seek_read(ctx, paramsfo_buf, paramsfo_lsn, paramsfo_secsize);
if (!rc) {
free(paramsfo_buf);
return -3;
}
if (icon_bytes > 0) {
icon_buf = calloc(ISO_BLOCKSIZE, icon_secsize);
}
if (icon_buf) {
rc = iso9660_iso_seek_read(ctx, icon_buf, icon_lsn, icon_secsize);
if (!rc) {
free(icon_buf);
icon_buf = NULL;
}
}
if (icon_buf) {
icontag = make_icontag(icon_buf, icon_bytes);
free(icon_buf);
icon_buf = NULL;
}
rc = sfo_init(&sfoctx, paramsfo_buf);
if (rc < 0) {
free(icon_buf);
free(paramsfo_buf);
return -4;
}
char *title = escape_xml(sfo_get_string_for_key(&sfoctx, "TITLE"), false);
char *title_nobreaks = escape_xml(sfo_get_string_for_key(&sfoctx, "TITLE"), true);
z(html_header,
PSP2MASTER_GENERATOR_STRING,
title,
icontag?:"",
title_nobreaks
);
free(title);
title = NULL;
free(icontag);
icontag = NULL;
free(title_nobreaks);
title_nobreaks = NULL;
// print param table
for (int i = 0; i < sfo_numrows(&sfoctx); i++) {
struct sfo_row row = sfo_get_row_by_index(&sfoctx, i);
z("<tr>\n");
// key
z("\t<td class=\"key\">%s", row.key);
z("</td>\n");
// value
z("\t<td class=\"value\">");
switch (row.type) {
case KT_UINT32:
z("%u", *(uint32_t *)row.val);
break;
case KT_STRING:
char *s = escape_xml((char *)row.val, false);
z("%s", s);
free(s);
break;
case KT_BINARY:
z("(%d bytes)\n", row.len);
break;
default:
break;
}
z("</td>\n");
z("</tr>\n");
}
z("</tbody>\n</table>\n");
z("<p>\nGenerated by %s.<br />\n", PSP2MASTER_PROGRAM_NAME);
z("<a href=\"%s\">%s</a></p>\n</body>\n</html>\n", PSP2MASTER_URL, PSP2MASTER_URL);
sfo_free(&sfoctx);
free(paramsfo_buf);
return 0;
}