Browse Source

Compress PNG data in image

kcgi never enables compression for FastCGI.
Causal Agent 4 months ago
parent
commit
b9551af817
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
4 changed files with 17 additions and 4 deletions
  1. 1
    1
      Makefile
  2. 8
    1
      image.c
  3. 1
    1
      kcgi.mk
  4. 7
    1
      png.h

+ 1
- 1
Makefile View File

@@ -3,7 +3,7 @@ CHROOT_GROUP = $(CHROOT_USER)
3 3
 
4 4
 CFLAGS += -std=c11 -Wall -Wextra -Wpedantic
5 5
 LDFLAGS = -static
6
-LDLIBS = -lcursesw -lutil
6
+LDLIBS = -lcursesw -lutil -lz
7 7
 
8 8
 -include config.mk
9 9
 

+ 8
- 1
image.c View File

@@ -25,6 +25,7 @@
25 25
 #include <sys/stat.h>
26 26
 #include <sysexits.h>
27 27
 #include <unistd.h>
28
+#include <zlib.h>
28 29
 
29 30
 #ifdef __FreeBSD__
30 31
 #include <sys/capsicum.h>
@@ -158,7 +159,12 @@ static void render(FILE *stream, uint32_t tileX, uint32_t tileY) {
158 159
 		}
159 160
 	}
160 161
 
161
-	pngData(stream, (uint8_t *)data, sizeof(data));
162
+	uLong zlen = compressBound(sizeof(data));
163
+	uint8_t zdata[zlen];
164
+	int error = compress(zdata, &zlen, (uint8_t *)data, sizeof(data));
165
+	if (error) errx(EX_SOFTWARE, "compress: %d", error);
166
+
167
+	pngDeflated(stream, zdata, (size_t)zlen);
162 168
 	pngTail(stream);
163 169
 }
164 170
 
@@ -229,6 +235,7 @@ static void worker(void) {
229 235
 		if (error == KCGI_HUP) goto next;
230 236
 		if (error) errkcgi(EX_IOERR, error, "khttp_head");
231 237
 
238
+		// XXX: kcgi never enables compression for FastCGI.
232 239
 		error = khttp_body(&req);
233 240
 		if (error == KCGI_HUP) goto next;
234 241
 		if (error) errkcgi(EX_IOERR, error, "khttp_body");

+ 1
- 1
kcgi.mk View File

@@ -1,3 +1,3 @@
1 1
 CFLAGS += -DHAVE_KCGI -I/usr/local/include
2 2
 LDFLAGS += -L/usr/local/lib
3
-LDLIBS += -lkcgi -lz
3
+LDLIBS := -lkcgi $(LDLIBS)

+ 7
- 1
png.h View File

@@ -1,4 +1,4 @@
1
-/* Copyright (C) 2018  Curtis McEnroe <june@causal.agency>
1
+/* Copyright (C) 2018, 2019  C. McEnroe <june@causal.agency>
2 2
  *
3 3
  * This program is free software: you can redistribute it and/or modify
4 4
  * it under the terms of the GNU Affero General Public License as published by
@@ -102,6 +102,12 @@ static inline void pngData(FILE *file, const uint8_t *data, uint32_t len) {
102 102
 	pngInt32(file, ~pngCRC);
103 103
 }
104 104
 
105
+static inline void pngDeflated(FILE *file, const uint8_t *data, uint32_t len) {
106
+	pngChunk(file, "IDAT", len);
107
+	pngWrite(file, data, len);
108
+	pngInt32(file, ~pngCRC);
109
+}
110
+
105 111
 static inline void pngTail(FILE *file) {
106 112
 	pngChunk(file, "IEND", 0);
107 113
 	pngInt32(file, ~pngCRC);

Loading…
Cancel
Save