-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreadicns.c
274 lines (231 loc) · 7.3 KB
/
readicns.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
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
// -*- Mode: c; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
//
// Copyright (c) 2017, Arjan van Leeuwen
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// This is a converter for the Apple Icon Image Format (.icns) which outputs
// PNGs without changing them.
//
// It's run like this: 'readicns x.icns' and outputs a directory 'x.iconset'.
//
// This tool is similar to running 'iconutil -c iconset x.icns', except it
// doesn't change the PNG images in any way.
#include <arpa/inet.h>
#include <dirent.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
// Magic values for headers were found at
// https://en.wikipedia.org/wiki/Apple_Icon_Image_format.
enum kBufferSize { kBufferSize = 1024 };
static const char kIconsetExtension[] = ".iconset";
static const char kIcnsExtension[] = ".icns";
static const char kUnknownFormatFilename[] = "icon_data_";
static const uint32_t kMagicHeader = 'icns';
typedef struct {
char path[MAXPATHLEN];
} Path;
struct {
const char* icon_filename;
uint32_t icon_type;
} static const kIconTypes[] = {
{"icon_16x16.png", 'icp4'},
{"[email protected]", 'ic11'},
{"icon_32x32.png", 'icp5'},
{"[email protected]", 'ic12'},
{"icon_64x64.png", 'icp6'},
{"icon_128x128.png", 'ic07'},
{"[email protected]", 'ic13'},
{"icon_256x256.png", 'ic08'},
{"[email protected]", 'ic14'},
{"icon_512x512.png", 'ic09'},
{"[email protected]", 'ic10'}
};
bool IsEmpty(Path path) {
return path.path[0] == '\0';
}
void PrintError(const char* error) {
fprintf(stderr, "Error: %s\n", error);
}
void PrintSystemError() {
perror("Error");
}
void PrintUsage(const char* own_path) {
fprintf(stderr, "Usage: %s [file.icns]\n", own_path);
}
char* Basename(const char* path, char* basename) {
if (!path || *path == '\0') {
strlcpy(basename, ".", sizeof("."));
return basename;
}
size_t length = strlen(path);
const char* end;
for (end = path + length - 1; end > path && *end == '/'; end--) {}
const char* begin;
for (begin = end; begin > path && *(begin - 1) != '/'; begin--) {}
size_t base_length = (end - begin) + 1;
if (base_length >= MAXPATHLEN)
return NULL;
strlcpy(basename, begin, base_length + 1);
return basename;
}
const char* IcnsFromArguments(int argc, char* argv[]) {
if (argc < 2) {
PrintError("No path given to icns file.");
PrintUsage(argv[0]);
return NULL;
} else if (argc > 2) {
PrintError("Too many arguments.");
PrintUsage(argv[0]);
return NULL;
}
return argv[1];
}
uint32_t ReadUint32(FILE* file) {
uint32_t read;
if (fread(&read, sizeof(read), 1, file) != 1)
return 0;
return ntohl(read);
}
FILE* OpenIcnsFileForReading(const char* icns_path) {
FILE* icns = fopen(icns_path, "r");
if (!icns) {
PrintSystemError();
return NULL;
}
uint32_t header = ReadUint32(icns);
if (header != kMagicHeader) {
PrintError("This doesn't look like an Apple .icns file.");
fclose(icns);
return NULL;
}
uint32_t size = ReadUint32(icns);
if (!size) {
PrintError("This looks like an empty .icns file.");
fclose(icns);
return NULL;
}
return icns;
}
Path GetIconsetPath(const char* icns_path) {
Path path = {0};
if (!Basename(icns_path, path.path)) {
PrintError("Can't determine name of icns file");
return path;
}
char* extension = strstr(path.path, kIcnsExtension);
if (!extension) {
PrintError("Can't find .icns extension on input file");
path.path[0] = '\0';
return path;
}
strlcpy(extension, kIconsetExtension, MAXPATHLEN - (extension - path.path));
return path;
}
const char* GetFilenameFromType(uint32_t type) {
for (size_t i = 0; i < (sizeof(kIconTypes) / sizeof(*kIconTypes)); i++) {
if (kIconTypes[i].icon_type == type)
return kIconTypes[i].icon_filename;
}
return NULL;
}
bool CopyIconToIconset(FILE* icns, Path iconset_path) {
uint32_t header = ReadUint32(icns);
if (!header && feof(icns))
return true;
uint32_t size = ReadUint32(icns);
if (size <= 8) {
PrintError("Invalid size in .icns file");
return false;
}
size -= 8;
const char* icon_filename = GetFilenameFromType(header);
char* iconset_path_end = iconset_path.path + strlen(iconset_path.path);
*iconset_path_end++ = '/';
if (icon_filename) {
strlcpy(iconset_path_end, icon_filename,
MAXPATHLEN - (iconset_path_end - iconset_path.path));
} else {
char unknown_format[] = {(header >> 24) & 0xff, (header >> 16) & 0xff,
(header >> 8) & 0xff, header & 0xff, '\0'};
iconset_path_end +=
strlcpy(iconset_path_end, kUnknownFormatFilename,
MAXPATHLEN - (iconset_path_end - iconset_path.path));
strlcpy(iconset_path_end, unknown_format,
MAXPATHLEN - (iconset_path_end - iconset_path.path));
}
FILE* target = fopen(iconset_path.path, "w");
if (!target) {
PrintSystemError();
return false;
}
uint8_t buffer[kBufferSize];
while (size > 0) {
size_t to_read = size > kBufferSize ? kBufferSize : size;
if (fread(buffer, 1, to_read, icns) != to_read ||
fwrite(buffer, 1, to_read, target) != to_read) {
PrintError("Error copying from .icns file to iconset");
fclose(target);
return false;
}
size -= to_read;
}
fclose(target);
return true;
}
bool CreateIconsetFromIcns(const char* icns_path) {
FILE* icns = OpenIcnsFileForReading(icns_path);
if (!icns)
return false;
Path iconset_path = GetIconsetPath(icns_path);
if (IsEmpty(iconset_path)) {
fclose(icns);
return false;
}
if (mkdir(iconset_path.path, 0777)) {
PrintSystemError();
fclose(icns);
return false;
}
while (!feof(icns)) {
if (!CopyIconToIconset(icns, iconset_path)) {
fclose(icns);
return false;
}
}
fclose(icns);
return true;
}
int main(int argc, char* argv[]) {
const char* icns_path = IcnsFromArguments(argc, argv);
if (!icns_path)
return -1;
if (!CreateIconsetFromIcns(icns_path))
return -1;
return 0;
}