/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running cc -Wall -s -O2 -o ./id3pic ./id3pic.c */ #include #include #include #include const char* info = "" "id3pic [options...] [file...]\n" "\n" "Extract picture/thumbnail bytes from ID3/MP3 metadata, if available.\n" "\n" "All (optional) leading options start with either single or double-dash:\n" "\n" " -h show this help message\n" " -help show this help message\n" ""; typedef struct bufreader { // buf is the buffer, (re)filled periodically as needed unsigned char* buf; // len is how many buffer bytes are being used, out of its max capacity size_t len; // cap is the buffer's capacity, or the most bytes it can hold at once size_t cap; // pos is the current position, up to the current buffer length size_t pos; // src is the data source used to fill the buffer FILE* src; } bufreader; // init_bufreader is the constructor for type bufreader void init_bufreader(bufreader* r, FILE* src, unsigned char* buf, size_t cap) { r->buf = buf; r->len = 0; r->cap = cap; r->pos = 0; r->src = src; } // read_byte does as it says: check its return for the value EOF, before // using it as the next byte int read_byte(bufreader* r) { if (r->pos < r->len) { // inside current chunk const unsigned char b = r->buf[r->pos]; r->pos++; return b; } // need to read the next block r->pos = 0; r->len = fread(r->buf, sizeof(unsigned char), r->cap, r->src); if (r->len > 0) { const unsigned char b = r->buf[r->pos]; r->pos++; return b; } // reached the end of data return EOF; } void copy_bytes(FILE* w, bufreader* r, int64_t n) { // potentially slow, but it works for now for (; n > 0; n--) { int b = read_byte(r); if (b == EOF) { return; } putc(b, w); } } int64_t discard_bytes(bufreader* r, size_t n) { if (r->pos + n < r->len) { r->pos += n; return n; } int64_t discarded = 0; for (; n > 0; n--, discarded++) { if (read_byte(r) == EOF) { break; } } return discarded; } int64_t discard_until(bufreader* r, unsigned char what) { for (int64_t n = 0; true; n++) { int b = read_byte(r); if (b == EOF) { return -(n + 1); } if (b == what) { return n + 1; } } } bool match(bufreader* r, const char* what) { for (size_t i = 0; what[i] != 0; i++) { if (read_byte(r) != what[i]) { return false; } } return true; } bool any(const int* data, size_t n, int what) { for (size_t i = 0; i < n; i++) { if (data[i] == what) { return true; } } return false; } void showError(const char* msg) { fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); } int64_t skip_thumbnail_type_APIC(bufreader* r) { // https://id3.org/id3v2.3.0 int64_t skipped = 0; int64_t n = 0; n = discard_bytes(r, 2); if (n != 2) { showError("failed to sync to APIC flags"); return false; } skipped += n; n = discard_bytes(r, 1); if (n != 1) { showError("failed to sync to APIC text-encoding"); return false; } skipped += n; n = discard_until(r, 0); if (n < 0) { showError("failed to sync to APIC thumbnail MIME-type"); return -1; } skipped += n; n = discard_bytes(r, 1); if (n != 1) { showError("failed to sync to APIC picture type"); return false; } skipped += n; n = discard_until(r, 0); if (n < 0) { showError("failed to sync to APIC thumbnail description"); return -1; } skipped += n; return skipped; } int64_t decode_big_endian_int64(const int v[4]) { return (v[0] << 24) + (v[1] << 16) + (v[2] << 8) + (v[3] << 0); } bool handle_apic(FILE* w, bufreader* r) { // section-size seems stored as 4 big-endian bytes int ss[4]; ss[0] = read_byte(r); ss[1] = read_byte(r); ss[2] = read_byte(r); ss[3] = read_byte(r); if (any(ss, sizeof(ss), EOF)) { return false; } const int64_t section_size = decode_big_endian_int64(ss); const int64_t header_size = skip_thumbnail_type_APIC(r); if (header_size < 0 || header_size > section_size) { return false; } copy_bytes(w, r, section_size - header_size); return true; } bool handle_pic(FILE* w, bufreader* r) { // http://www.unixgods.org/Ruby/ID3/docs/id3v2-00.html#PIC // thumbnail-payload-size seems stored as 3 big-endian bytes int tps[4]; tps[0] = read_byte(r); tps[1] = read_byte(r); tps[2] = read_byte(r); if (any(tps, sizeof(tps), EOF)) { return false; } const int64_t size = (tps[0] << 16) + (tps[1] << 8) + (tps[2] << 0); if (size < 0) { return false; } // skip the text encoding if (discard_bytes(r, 5) != 5) { return false; } // skip a null-delimited string if (discard_until(r, 0) < 0) { return false; } copy_bytes(w, r, size); return true; } bool id3pic(FILE* w, bufreader* r) { while (true) { int b = read_byte(r); if (b == EOF) { fprintf(stderr, "no thumbnail found\n"); return false; } // handle APIC-type chunks if (b == 'A' && match(r, "PIC")) { return handle_apic(w, r); } // handle PIC-type chunks if (b == 'P' && match(r, "IC")) { return handle_pic(w, r); } } } // handle_file handles data from the filename given; returns false only when // the file can't be opened bool handle_file(FILE* w, const char* path, bufreader* r) { FILE* f = fopen(path, "rb"); if (f == NULL) { fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", path); return false; } r->src = f; const bool ok = id3pic(w, r); fclose(f); return ok; } // is_help_option simplifies control-flow for func main bool is_help_option(const char* s) { return (s[0] == '-') && ( strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 || strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0 ); } bool run(const char* name) { unsigned char buf[32 * 1024]; bufreader r; init_bufreader(&r, stdin, buf, sizeof(buf)); if (name[0] == '-' && name[1] == 0) { r.src = stdin; return id3pic(stdout, &r); } return handle_file(stdout, name, &r); } int main(int argc, char** argv) { // enable full buffering for stdout setvbuf(stdout, NULL, _IOFBF, 0); // handle any of the help options, if given if (argc > 1 && is_help_option(argv[1])) { puts(info); return 0; } if (argc > 2) { fprintf(stderr, "\x1b[31mcan only handle 1 file\x1b[0m\n"); return 1; } return run(argc == 2 ? argv[1] : "-") ? 0 : 1; }