OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
icnspack.c
Go to the documentation of this file.
1
19#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
20#define BigEndianToNative32(x) __builtin_bswap32(x)
21#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
22#define BigEndianToNative32(x) (x)
23#else
24#include <arpa/inet.h>
25#define BigEndianToNative32(x) ntohl(x)
26#endif
27
28#include <stdbool.h>
29#include <stdio.h>
30#include <stdint.h>
31#include <stdlib.h>
32#include <string.h>
33
34static int read_file(const char *filename, uint8_t **buffer, size_t *size) {
35 FILE *fh = fopen(filename, "rb");
36 if (!fh) {
37 fprintf(stderr, "Missing file %s!\n", filename);
38 return -1;
39 }
40
41 if (fseek(fh, 0, SEEK_END)) {
42 fprintf(stderr, "Failed to find end of %s!\n", filename);
43 fclose(fh);
44 return -1;
45 }
46
47 long pos = ftell(fh);
48
49 if (pos <= 0) {
50 fprintf(stderr, "Invalid file size (%ld) of %s!\n", pos, filename);
51 fclose(fh);
52 return -1;
53 }
54
55 if (fseek(fh, 0, SEEK_SET)) {
56 fprintf(stderr, "Failed to rewind %s!\n", filename);
57 fclose(fh);
58 return -1;
59 }
60
61 *size = (size_t)pos;
62 *buffer = (uint8_t *)malloc(*size);
63
64 if (!*buffer) {
65 fprintf(stderr, "Failed to allocate %zu bytes for %s!\n", *size, filename);
66 fclose(fh);
67 return -1;
68 }
69
70 if (fread(*buffer, *size, 1, fh) != 1) {
71 fprintf(stderr, "Failed to read %zu bytes from %s!\n", *size, filename);
72 fclose(fh);
73 free(*buffer);
74 return -1;
75 }
76
77 fclose(fh);
78 return 0;
79}
80
81int main(int argc, char *argv[]) {
82 if (argc != 4 ) {
83 fprintf(stderr,
84 "Usage:\n"
85 " icnspack image.icns 1x.png 2x.png\n");
86 return -1;
87 }
88
89 (void) remove(argv[1]);
90 uint8_t *buffer1x;
91 size_t size1x;
92 int r1 = read_file(argv[2], &buffer1x, &size1x);
93 uint8_t *buffer2x;
94 size_t size2x;
95 int r2 = read_file(argv[3], &buffer2x, &size2x);
96
97 FILE *fh = NULL;
98 if (r1 == 0 && r2 == 0) {
99 fh = fopen (argv[1], "wb");
100 if (fh != NULL) {
101 uint32_t size = BigEndianToNative32(size1x + size2x + sizeof(uint32_t)*6);
102 fwrite("icns", sizeof(uint32_t), 1, fh);
103 fwrite(&size, sizeof(uint32_t), 1, fh);
104
105 size = BigEndianToNative32(size1x + sizeof(uint32_t)*2);
106 fwrite("ic07", sizeof(uint32_t), 1, fh);
107 fwrite(&size, sizeof(uint32_t), 1, fh);
108 fwrite(buffer1x, size1x, 1, fh);
109
110 size = BigEndianToNative32(size2x + sizeof(uint32_t)*2);
111 fwrite("ic13", sizeof(uint32_t), 1, fh);
112 fwrite(&size, sizeof(uint32_t), 1, fh);
113 fwrite(buffer2x, size2x, 1, fh);
114
115 fclose(fh);
116 }
117 }
118
119 if (r1 == 0) free(buffer1x);
120 if (r2 == 0) free(buffer2x);
121
122 return fh != NULL ? 0 : -1;
123}
UINT32 size
#define size_t
Definition Ubsan.h:87
int main()
Definition acdtinfo.c:181
#define BigEndianToNative32(x)
Definition icnspack.c:25
UINT8 uint8_t
UINT32 uint32_t
#define malloc(Size)
Definition lzss.h:49
#define free(Ptr)
Definition lzss.h:50