This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flac-image.c
402 lines (346 loc) · 12.2 KB
/
flac-image.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Cheesy hack to store and retrieve images (such as cover art) in APPLICATION
* blocks in FLAC files. Actually it turns out that you could store any
* arbitrary files and differentiate them by mime-type if you wanted. That's
* because this program doesn't understand anything in particular about image
* files; they are just treated as blobs of data.
*
* The application ID and data structure are defined in flac-image.h.
*
* Copyright (c) 2005 Michael A. Dickerson. All rights reserved. Copying,
* modification, redistribution, and use are permitted under the terms of a
* BSD-style license described in the file "COPYING".
*
* Written 17 Jan 2005 MAD
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <FLAC/metadata.h>
#include "flac-image.h"
#define VERSION "1.00"
extern char *optarg;
extern int optind, opterr, optopt;
FLAC__Metadata_Chain *chain;
FLAC__Metadata_Iterator *iter;
void search_images(char *mime_type, int extract);
void delete_images(char *mime_type);
void import_image(char *imgfile, char *mimetype);
void export_thumbnail(void);
void export_image_from_block(FLAC__StreamMetadata *block);
char *guess_mime_type(char *imgfile);
int block_is_image(FLAC__StreamMetadata *block);
void usage(void);
int main(int argc, char *argv[])
{
int o, oper = OP_UNDEFINED, i;
char *image_file = NULL, *mime_type = NULL;
/* parse options */
while ((o = getopt(argc, argv, "i:xt:dnl")) != -1) {
switch (o) {
case 'i':
oper = OP_IMPORT;
image_file = optarg;
break;
case 'x':
oper = OP_EXPORT;
break;
case 'd':
oper = OP_DELETE;
break;
case 'n':
oper = OP_THUMBNAIL;
break;
case 'l':
oper = OP_LIST;
break;
case 't':
mime_type = optarg;
break;
case '?':
usage();
}
}
/* everything after optind should be a flac file name; if not, or if
there is nothing left, that's an error. */
if (optind >= argc) usage();
for (i = optind; i < argc; ++i) {
/* allocate a FLAC metadata chain pointer */
chain = FLAC__metadata_chain_new();
iter = FLAC__metadata_iterator_new();
if (chain == NULL || iter == NULL) {
fputs("ERROR: out of memory allocating chain.\n", stderr);
return 1;
}
/* read blocks from flac file */
if (FLAC__metadata_chain_read(chain, argv[i]) == false) {
fprintf(stderr, "ERROR: can't read metadata from %s\n", argv[i]);
return false;
}
FLAC__metadata_iterator_init(iter, chain);
switch (oper) {
case OP_IMPORT:
import_image(image_file, mime_type);
break;
case OP_EXPORT:
search_images(mime_type, true);
break;
case OP_DELETE:
delete_images(mime_type);
break;
case OP_THUMBNAIL:
export_thumbnail();
break;
case OP_LIST:
search_images(mime_type, false);
break;
default:
usage();
}
/* commit whatever operations were done */
FLAC__metadata_iterator_delete(iter);
/* fputs("Sorting padding...", stderr); */
FLAC__metadata_chain_sort_padding(chain);
/* fputs("done.\nWriting chain...", stderr); */
if (FLAC__metadata_chain_write(chain, /*use_padding=*/true,
/*preserve_stat=*/false) == false) {
fputs("ERROR: can't write metadata chain\n", stderr);
exit(3);
}
/* fputs("done.\n", stderr); */
FLAC__metadata_chain_delete(chain);
}
return 0;
}
void delete_images(char *mime_type)
{
FLAC__bool ok = true;
FLAC__StreamMetadata *block;
int count = 0;
/* traverse the chain looking for only blocks of type APPLICATION with
an application ID we recognize. */
while (ok && FLAC__metadata_iterator_next(iter)) {
block = FLAC__metadata_iterator_get_block(iter);
if (!block_is_image(block)) continue;
/* if a mime type was passed in, skip any blocks that don't match */
if (mime_type) {
flac_image_header *head = (flac_image_header *)block->data.application.data;
if (strncmp(mime_type, head->mimetype, strlen(mime_type)) != 0) continue;
}
ok &= FLAC__metadata_iterator_delete_block(iter, /*replace_with_padding=*/true);
++count;
}
fprintf(stderr, "Deleted %d block(s).\n", count);
return;
}
void import_image(char *imgfile, char *mimetype)
{
struct stat img_stat;
int blocklen, f;
flac_image_header *img_header;
FLAC__byte *img_data;
FLAC__StreamMetadata *block;
/* go to last block */
while (FLAC__metadata_iterator_next(iter))
;
if (stat(imgfile, &img_stat) != 0) {
perror("ERROR: can't stat image file");
return;
}
/* allocate enough bytes for a flac_image_header struct plus the file itself */
blocklen = sizeof(flac_image_header) + img_stat.st_size;
if ((img_data = malloc(blocklen)) == NULL) {
fputs("ERROR: can't allocate memory for new image struct", stderr);
return;
}
/* populate the header fields */
img_header = (flac_image_header *)img_data;
memset(img_header, 0, blocklen);
img_header->vers = IMAGE_HEADER_VERSION;
if (mimetype == NULL) mimetype = guess_mime_type(imgfile);
strncpy(img_header->mimetype, mimetype, IMAGE_HEADER_MIMETYPE_MAXLEN);
img_header->mimetype[IMAGE_HEADER_MIMETYPE_MAXLEN - 1] = 0;
if (strrchr(imgfile, '/') != NULL) {
/* store only the part of the filename after the final / */
char *ptr = strrchr(imgfile, '/') + 1;
strncpy(img_header->filename, ptr, IMAGE_HEADER_FILENAME_MAXLEN);
} else {
strncpy(img_header->filename, imgfile, IMAGE_HEADER_FILENAME_MAXLEN);
}
img_header->filename[IMAGE_HEADER_FILENAME_MAXLEN - 1] = 0;
img_header->datasize = img_stat.st_size;
/* read the image data into the buffer */
if ((f = open(imgfile, O_RDONLY)) < 0) {
perror("ERROR: can't open image file");
return;
}
if (read(f, img_data + sizeof(flac_image_header), img_stat.st_size)
< img_stat.st_size) {
fputs("ERROR: short read trying to load image\n", stderr);
close(f);
return;
}
if (close(f)) {
perror("WARNING: can't close file");
}
/* prepare a new block */
if ((block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)) == NULL) {
fputs("ERROR: can't allocate memory for new block", stderr);
return;
}
memcpy(block->data.application.id, &APPLICATION_ID, ID_BYTES);
FLAC__metadata_object_application_set_data(block, (FLAC__byte *)img_data,
blocklen, /*copy=*/ false);
/* insert the new block to the stream */
printf("Inserting block with mime-type %s: %d bytes\n", mimetype, blocklen);
if (!FLAC__metadata_iterator_insert_block_after(iter, block))
fputs("WARNING: block insert failed\n", stderr);
return;
}
void search_images(char *mime_type, int extract)
{
FLAC__bool ok = true;
FLAC__StreamMetadata *block;
int count = 0;
while (ok && FLAC__metadata_iterator_next(iter)) {
block = FLAC__metadata_iterator_get_block(iter);
if (!block_is_image(block)) continue;
flac_image_header *head = (flac_image_header *)block->data.application.data;
/* if a mime type was passed in, skip any blocks that don't match */
if (mime_type) {
if (strncmp(mime_type, head->mimetype, strlen(mime_type)) != 0) continue;
}
fprintf(stdout, "Found image block: type %s, size %d\n",
head->mimetype, head->datasize);
if (extract) {
fprintf(stdout, "Extracting to file: %s\n", head->filename);
export_image_from_block(block);
} else {
fprintf(stdout, "Name: %s\n", head->filename);
}
++count;
}
fprintf(stderr, "Found %d recognized block(s).\n", count);
return;
}
/*
* export_thumbnail() walks the chain looking for the smallest block with
* the right application ID and a mime-type that starts with image/, and
* extracts only that image.
*/
void export_thumbnail(void)
{
FLAC__StreamMetadata *block;
FLAC__StreamMetadata *smallest_block = NULL;
flac_image_header *head;
int smallest_size = 10 * 1024 * 1024; /* start with a big value, 10 MB */
/* traverse the chain and find the smallest block with our application ID
and with a mime-type that matches 'image/' */
while (FLAC__metadata_iterator_next(iter)) {
block = FLAC__metadata_iterator_get_block(iter);
/* skip blocks that don't match our application ID */
if (!block_is_image(block)) continue;
/* skip blocks that don't match mime-type image/ */
head = (flac_image_header *)block->data.application.data;
if (strncmp(head->mimetype, "image/", 6) != 0) continue;
/* see whether this block is the smallest one so far */
if (head->datasize < smallest_size) {
smallest_block = block;
smallest_size = head->datasize;
}
}
if (smallest_block == NULL) {
fputs("ERROR: No image blocks found.\n", stderr);
} else {
head = (flac_image_header *)smallest_block->data.application.data;
fprintf(stdout, "Found image block: type %s, size %d\n",
head->mimetype, head->datasize);
fprintf(stdout, "Extracting to file: %s\n", head->filename);
export_image_from_block(smallest_block);
}
}
/*
* export_image_from_block() takes a pointer to a metadata block and
* dumps the image to a file using the name stored in the block. Does
* nothing if block_is_image(block) returns false.
*/
void export_image_from_block(FLAC__StreamMetadata *block)
{
flac_image_header *head;
int datasize, fd;
if (!block_is_image(block)) return;
head = (flac_image_header *)block->data.application.data;
datasize = head->datasize;
/* sanity check: this program won't put / characters in stored filenames,
but some joker could do it and trick you into overwriting e.g.
~/.ssh/authorized_keys */
if (strchr(head->filename, '/') != NULL) {
fputs("ERROR: won't extract to filename containing path characters!\n",
stderr);
return;
}
if ((fd = open(head->filename, O_WRONLY | O_CREAT, 00644)) < 0) {
perror("ERROR: can't open file");
return;
}
head++; /* note that head advances by sizeof(flac_image_header), so it now
points to the binary data that comes after the header struct. */
if (write(fd, head, datasize) < datasize) {
fputs("WARNING: short write to file\n", stderr);
}
if (close(fd)) { perror("WARNING: can't close file"); }
}
/*
* block_is_image() returns true iff the pointed-to block is an
* APPLICATION block that matches our application ID.
*/
int block_is_image(FLAC__StreamMetadata *block)
{
if (block == NULL) return false;
if (block->type != FLAC__METADATA_TYPE_APPLICATION) return false;
FLAC__byte *blockid = block->data.application.id;
if (memcmp(blockid, APPLICATION_ID, ID_BYTES) != 0) return false;
flac_image_header *head = (flac_image_header *)block->data.application.data;
if (head->vers != IMAGE_HEADER_VERSION) {
fputs("ERROR: Image block has mismatched version stamp.", stderr);
return false;
}
return true;
}
/*
* guess_mime_type() supplies mime-type strings for filenames that
* happen to end in .jpg, .png, or .gif. For anything else it returns
* the string "unknown".
*/
char *guess_mime_type(char *imgfilename)
{
char *end;
end = imgfilename;
while (*end != 0) ++end;
if (strcmp(end - 3, "jpg") == 0 || strcmp(end - 3, "JPG") == 0) {
return "image/jpeg";
} else if (strcmp(end - 4, "jpeg") == 0 || strcmp(end - 4, "JPEG") == 0) {
return "image/jpeg";
} else if (strcmp(end - 3, "png") == 0 || strcmp(end - 3, "PNG") == 0) {
return "image/png";
} else if (strcmp(end - 3, "gif") == 0 || strcmp(end - 3, "GIF") == 0) {
return "image/gif";
}
return "unknown";
}
void usage(void)
{
fprintf(stderr, "flac-image %s, Copyright (c) 2005 Michael A. Dickerson\n\n", VERSION);
fputs("Usage:\n", stderr);
fputs("flac-image (-x | -i <imagefile> | -d | -n | -l) [-t <mimetype>] flacfile ...\n", stderr);
fputs("\n", stderr);
fputs(" -x: eXtract images in flacfile to current directory\n", stderr);
fputs(" -i: Insert image <imagefile> in flacfile\n", stderr);
fputs(" -d: Delete image blocks in flacfile\n", stderr);
fputs(" -n: extract only the smallest available image block (thumbNail)\n", stderr);
fputs(" -l: List recognized image blocks\n\n", stderr);
fputs(" -t: with -i, use <mimetype> instead of guessing based on filename\n", stderr);
fputs(" with any other operation, process only the blocks that match <mimetype>\n\n", stderr);
exit(1);
}