From 1a1b7bf4e55bd377b663434fadc7d04da40ce235 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Tue, 15 Mar 2022 14:47:40 -0400 Subject: Add daticns utility Idea from . Used to extract icns files from the .dat file in /System/Library/Keyboard Layouts/AppleKeyboardLayouts.bundle. --- etc/daticns.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 etc/daticns.c diff --git a/etc/daticns.c b/etc/daticns.c new file mode 100644 index 00000000..63f1649d --- /dev/null +++ b/etc/daticns.c @@ -0,0 +1,62 @@ +/* Copyright (C) 2022 June McEnroe + * + * 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 . + */ + +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc < 2) return EX_USAGE; + + FILE *file = fopen(argv[1], "r"); + if (!file) err(EX_NOINPUT, "%s", argv[1]); + + size_t cap = 0x10000; + uint8_t *buf = malloc(cap); + if (!buf) err(EX_OSERR, "malloc"); + + size_t len = 0; + for (size_t n; 0 < (n = fread(&buf[len], 1, cap - len, file)); len += n) { + buf = realloc(buf, (cap *= 2)); + if (!buf) err(EX_OSERR, "realloc"); + } + + unsigned nr = 0; + for ( + uint8_t *ptr = buf; + NULL != (ptr = memmem(ptr, &buf[len] - ptr, "icns", 4)); + ptr += 4 + ) { + if (&ptr[8] > &buf[len]) break; + size_t len = ptr[4] << 24 | ptr[5] << 16 | ptr[6] << 8 | ptr[7]; + + char path[64]; + snprintf(path, sizeof(path), "icon%04u.icns", ++nr); + printf("%s\n", path); + + FILE *icon = fopen(path, "w"); + if (!icon) err(EX_CANTCREAT, "%s", path); + + size_t n = fwrite(ptr, 8 + len, 1, icon); + if (!n) err(EX_IOERR, "%s", path); + + int error = fclose(icon); + if (error) err(EX_IOERR, "%s", path); + } +} -- cgit 1.4.1