-
Notifications
You must be signed in to change notification settings - Fork 2
/
blank.c
115 lines (103 loc) · 3.04 KB
/
blank.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
/*
* Copyright (c) 2018 Tristan Le Guern <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <arpa/inet.h>
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <zlib.h>
#include "libravatar.h"
#include "lgpng.h"
#define PNGBLANK_MAX_SIZE 255
static size_t
write_tRNS(uint8_t *buf)
{
uint32_t crc, length;
size_t bufw;
uint8_t type[4] = "tRNS";
uint8_t trns[2];
(void)memset(trns, 0, sizeof(trns));
length = htonl(sizeof(trns));
crc = crc32(0, Z_NULL, 0);
crc = crc32(crc, type, sizeof(type));
crc = crc32(crc, (Bytef *)trns, sizeof(trns));
crc = htonl(crc);
bufw = 0;
(void)memcpy(buf + bufw, &length, sizeof(length));
bufw += sizeof(length);
(void)memcpy(buf + bufw, type, sizeof(type));
bufw += sizeof(type);
(void)memcpy(buf + bufw, &trns, sizeof(trns));
bufw += sizeof(trns);
(void)memcpy(buf + bufw, &crc, sizeof(crc));
bufw += sizeof(crc);
return(bufw);
}
static size_t
write_IDAT(uint8_t *buf, size_t width)
{
size_t dataz, deflatez;
uint32_t crc, length;
size_t bufw;
uint8_t type[4] = "IDAT";
uint8_t *data, *deflate;
/* TODO: Directly write a compressed stream to avoid allocations */
dataz = (width / (8 / 1) + \
(width % (8 / 1) != 0 ? 1 : 0) + 1) * width;
if (NULL == (data = calloc(dataz, 1))) {
return(-1);
}
deflatez = compressBound(dataz);
if (NULL == (deflate = calloc(deflatez, 1))) {
free(data);
return(-1);
}
if (Z_OK != compress(deflate, &deflatez, data, dataz)) {
return(-1);
}
free(data);
length = htonl(deflatez);
crc = crc32(0, Z_NULL, 0);
crc = crc32(crc, type, sizeof(type));
crc = crc32(crc, deflate, deflatez);
crc = htonl(crc);
bufw = 0;
(void)memcpy(buf + bufw, &length, sizeof(length));
bufw += sizeof(length);
(void)memcpy(buf + bufw, type, sizeof(type));
bufw += sizeof(type);
(void)memcpy(buf + bufw, deflate, deflatez);
bufw += deflatez;
(void)memcpy(buf + bufw, &crc, sizeof(crc));
bufw += sizeof(crc);
free(deflate);
return(bufw);
}
int
blank(size_t width, uint8_t **buf, size_t *bufz)
{
if (NULL == ((*buf) = calloc(PNGBLANK_MAX_SIZE, 1)))
return(-1);
*bufz = 0;
*bufz += write_png_sig(*buf);
*bufz += write_IHDR(*buf + *bufz, width, 1, COLOUR_TYPE_GREYSCALE);
*bufz += write_tRNS(*buf + *bufz);
*bufz += write_IDAT(*buf + *bufz, width);
*bufz += write_IEND(*buf + *bufz);
return(0);
}