about summary refs log tree commit diff homepage
path: root/image.c
blob: 80e256775157a57085d8092bf2b7bb9342da0080 (plain) (blame)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* Copyright (C) 2018, 2019  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <err.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sysexits.h>
#include <unistd.h>
#include <zlib.h>

#ifdef __FreeBSD__
#include <sys/capsicum.h>
#endif

#ifdef HAVE_KCGI
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
#endif

// XXX: Include this after kcgi.h to avoid conflicts.
// <https://github.com/kristapsdz/kcgi/pull/58>
#include <stdnoreturn.h>

#include "png.h"
#include "torus.h"

static const uint8_t Palette[16][3] = {
	{ 0x00, 0x00, 0x00 },
	{ 0xAA, 0x00, 0x00 },
	{ 0x00, 0xAA, 0x00 },
	{ 0xAA, 0x55, 0x00 },
	{ 0x00, 0x00, 0xAA },
	{ 0xAA, 0x00, 0xAA },
	{ 0x00, 0xAA, 0xAA },
	{ 0xAA, 0xAA, 0xAA },
	{ 0x55, 0x55, 0x55 },
	{ 0xFF, 0x55, 0x55 },
	{ 0x55, 0xFF, 0x55 },
	{ 0xFF, 0xFF, 0x55 },
	{ 0x55, 0x55, 0xFF },
	{ 0xFF, 0x55, 0xFF },
	{ 0x55, 0xFF, 0xFF },
	{ 0xFF, 0xFF, 0xFF },
};

static struct {
	uint32_t magic;
	uint32_t version;
	uint32_t size;
	uint32_t flags;
	struct {
		uint32_t len;
		uint32_t size;
		uint32_t height;
		uint32_t width;
	} glyph;
} font;

static uint8_t *glyphs;

static void fontLoad(const char *path) {
	FILE *file = fopen(path, "r");
	if (!file) err(EX_NOINPUT, "%s", path);

	size_t len = fread(&font, sizeof(font), 1, file);
	if (ferror(file)) err(EX_IOERR, "%s", path);
	if (len < 1) errx(EX_DATAERR, "%s: truncated header", path);

	if (font.magic != 0x864AB572 || font.size != sizeof(font)) {
		errx(EX_DATAERR, "%s: invalid header", path);
	}

	glyphs = calloc(font.glyph.len, font.glyph.size);
	if (!glyphs) err(EX_OSERR, "calloc");

	len = fread(glyphs, font.glyph.size, font.glyph.len, file);
	if (ferror(file)) err(EX_IOERR, "%s", path);
	if (len < font.glyph.len) errx(EX_DATAERR, "%s: truncated glyphs", path);
	fclose(file);
}

static struct Tile (*tiles)[TileRows][TileCols];

static void tilesMap(const char *path) {
	int fd = open(path, O_RDONLY);
	if (fd < 0) err(EX_NOINPUT, "%s", path);

	struct stat stat;
	int error = fstat(fd, &stat);
	if (error) err(EX_IOERR, "%s", path);

	if ((size_t)stat.st_size < TilesSize) {
		errx(EX_DATAERR, "%s: truncated tiles", path);
	}

	tiles = mmap(NULL, TilesSize, PROT_READ, MAP_SHARED, fd, 0);
	if (tiles == MAP_FAILED) err(EX_OSERR, "mmap");
	close(fd);

	error = madvise(tiles, TilesSize, MADV_RANDOM);
	if (error) err(EX_OSERR, "madvise");

#ifdef MADV_NOCORE
	error = madvise(tiles, TilesSize, MADV_NOCORE);
	if (error) err(EX_OSERR, "madvise");
#endif
}

static void render(FILE *stream, uint32_t tileX, uint32_t tileY) {
	uint32_t width = CellCols * font.glyph.width;
	uint32_t height = CellRows * font.glyph.height;

	pngHead(stream, width, height, 8, PNGIndexed);
	pngPalette(stream, (uint8_t *)Palette, sizeof(Palette));

	uint8_t data[height][1 + width];
	memset(data, PNGNone, sizeof(data));

	uint32_t widthBytes = (font.glyph.width + 7) / 8;
	uint8_t (*bits)[font.glyph.len][font.glyph.height][widthBytes];
	bits = (void *)glyphs;

	struct Tile *tile = &(*tiles)[tileY][tileX];
	for (uint32_t cellY = 0; cellY < CellRows; ++cellY) {
		for (uint32_t cellX = 0; cellX < CellCols; ++cellX) {
			uint8_t cell = tile->cells[cellY][cellX];
			uint8_t fg = tile->colors[cellY][cellX] & 0x0F;
			uint8_t bg = tile->colors[cellY][cellX] >> 4;

			uint32_t glyphX = font.glyph.width * cellX;
			uint32_t glyphY = font.glyph.height * cellY;
			for (uint32_t y = 0; y < font.glyph.height; ++y) {
				for (uint32_t x = 0; x < font.glyph.width; ++x) {
					uint8_t bit = (*bits)[cell][y][x / 8] >> (7 - x % 8) & 1;
					data[glyphY + y][1 + glyphX + x] = (bit ? fg : bg);
				}
			}
		}
	}

	uLong zlen = compressBound(sizeof(data));
	uint8_t zdata[zlen];
	int error = compress(zdata, &zlen, (uint8_t *)data, sizeof(data));
	if (error) errx(EX_SOFTWARE, "compress: %d", error);

	pngDeflated(stream, zdata, (size_t)zlen);
	pngTail(stream);
}

#ifdef HAVE_KCGI

enum { KeyX, KeyY, KeysLen };
static const struct kvalid Keys[KeysLen] = {
	[KeyX] = { .name = "x", .valid = kvalid_int },
	[KeyY] = { .name = "y", .valid = kvalid_int },
};

enum { PageTile, PagesLen };
static const char *Pages[PagesLen] = {
	[PageTile] = "tile",
};

static noreturn void errkcgi(int eval, enum kcgi_err code, const char *str) {
	errx(eval, "%s: %s", str, kcgi_strerror(code));
}

struct Stream {
	struct kreq *req;
	bool hup;
};

// XXX: Swallow writes after the connection is closed.
static int streamWrite(void *cookie, const char *buf, int len) {
	struct Stream *stream = cookie;
	if (stream->hup) return len;
	enum kcgi_err error = khttp_write(stream->req, buf, (size_t)len);
	if (error) {
		if (error != KCGI_HUP) errkcgi(EX_IOERR, error, "khttp_write");
		stream->hup = true;
	}
	return len;
}

static void worker(void) {
	struct kfcgi *fcgi;
	enum kcgi_err error = khttp_fcgi_init(
		&fcgi, Keys, KeysLen, Pages, PagesLen, PageTile
	);
	if (error) errkcgi(EX_CONFIG, error, "khttp_fcgi_init");

	for (;;) {
		struct kreq req;
		error = khttp_fcgi_parse(fcgi, &req);
		if (error) errkcgi(EX_DATAERR, error, "khttp_fcgi_parse");

		uint32_t tileX = TileInitX;
		uint32_t tileY = TileInitY;
		if (req.fieldmap[KeyX]) {
			tileX = (uint32_t)req.fieldmap[KeyX]->parsed.i % TileCols;
		}
		if (req.fieldmap[KeyY]) {
			tileY = (uint32_t)req.fieldmap[KeyY]->parsed.i % TileRows;
		}

		error = khttp_head(
			&req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]
		);
		if (error == KCGI_HUP) goto next;
		if (error) errkcgi(EX_IOERR, error, "khttp_head");

		error = khttp_head(
			&req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_IMAGE_PNG]
		);
		if (error == KCGI_HUP) goto next;
		if (error) errkcgi(EX_IOERR, error, "khttp_head");

		// XXX: kcgi never enables compression for FastCGI.
		error = khttp_body(&req);
		if (error == KCGI_HUP) goto next;
		if (error) errkcgi(EX_IOERR, error, "khttp_body");

		struct Stream cookie = { .req = &req };
		FILE *stream = fwopen(&cookie, streamWrite);
		if (!stream) err(EX_OSERR, "fwopen");

		render(stream, tileX, tileY);
		fclose(stream);

next:
		khttp_free(&req);
	}
}

#endif /* HAVE_KCGI */

int main(int argc, char *argv[]) {
	bool kcgi = false;
	const char *fontPath = DefaultFontPath;
	const char *dataPath = DefaultDataPath;
	uint32_t tileX = TileInitX;
	uint32_t tileY = TileInitY;

	int opt;
	while (0 < (opt = getopt(argc, argv, "d:f:kx:y:"))) {
		switch (opt) {
			break; case 'd': dataPath = optarg;
			break; case 'f': fontPath = optarg;
			break; case 'k': kcgi = true;
			break; case 'x': tileX = strtoul(optarg, NULL, 0) % TileCols;
			break; case 'y': tileY = strtoul(optarg, NULL, 0) % TileRows;
			break; default:  return EX_USAGE;
		}
	}

	fontLoad(fontPath);
	tilesMap(dataPath);

#ifdef __FreeBSD__
	int error = cap_enter();
	if (error) err(EX_OSERR, "cap_enter");
#endif

#ifdef HAVE_KCGI
	if (kcgi) worker();
#endif

	render(stdout, tileX, tileY);
}