fix bug printing application id
[platform/upstream/flac.git] / src / metaflac / main.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 /*
20  * WATCHOUT - this is meant to be very lightweight an not even dependent
21  * on libFLAC, so there are a couple places where FLAC__* variables are
22  * duplicated here.  Look for 'DUPLICATE:' in comments.
23  */
24
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 /* we only use some typedefs, #defines, and enums from here: */
31 #include "FLAC/all.h"
32
33 static const char *sync_string_ = "fLaC"; /* DUPLICATE:FLAC__STREAM_SYNC_STRING */
34 static const char *metadata_type_string_[] = { /* DUPLICATE:FLAC__MetaDataTypeString */
35         "STREAMINFO",
36         "PADDING",
37         "APPLICATION",
38         "SEEKTABLE"
39 };
40 static const unsigned SEEKPOINT_LEN_ = 18; /* DUPLICATE:FLAC__STREAM_METADATA_SEEKPOINT_LENGTH */
41
42 static int usage(const char *message, ...);
43 static FLAC__bool list(FILE *f, FLAC__bool verbose);
44 static FLAC__uint32 unpack_uint32(FLAC__byte *b, unsigned bytes);
45 static FLAC__uint64 unpack_uint64(FLAC__byte *b, unsigned bytes);
46 static void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent);
47
48 int main(int argc, char *argv[])
49 {
50         int i, first_file, retval = 0;
51         FLAC__bool verbose = false, list_mode = true;
52
53         if(argc <= 1)
54                 return usage(0);
55
56         /* get the options */
57         for(i = 1; i < argc; i++) {
58                 if(argv[i][0] != '-' || argv[i][1] == 0)
59                         break;
60                 if(0 == strcmp(argv[i], "-l"))
61                         list_mode = true;
62                 else if(0 == strcmp(argv[i], "-v"))
63                         verbose = true;
64                 else if(0 == strcmp(argv[i], "-v-"))
65                         verbose = false;
66                 else {
67                         return usage("ERROR: invalid option '%s'\n", argv[i]);
68                 }
69         }
70         if(i == argc)
71                 return usage(0);
72
73         if(list_mode) {
74                 for(first_file = i; i < argc; i++) {
75                         FILE *f = fopen(argv[i], "rb");
76
77                         if(0 == f) {
78                                 fprintf(stderr, "ERROR opening %s\n", argv[i]);
79                                 retval = 1;
80                         }
81                         else {
82                                 printf("%sfile: %s\n", i==first_file? "" : "\n", argv[i]);
83
84                                 if(!list(f, verbose))
85                                         retval = 1;
86
87                                 fclose(f);
88                         }
89                 }
90         }
91
92         return retval;
93 }
94
95 int usage(const char *message, ...)
96 {
97         va_list args;
98
99         if(message) {
100                 va_start(args, message);
101
102                 (void) vfprintf(stderr, message, args);
103
104                 va_end(args);
105
106         }
107         printf("==============================================================================\n");
108         printf("metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
109         printf("Copyright (C) 2001,2002  Josh Coalson\n");
110         printf("\n");
111         printf("This program is free software; you can redistribute it and/or\n");
112         printf("modify it under the terms of the GNU General Public License\n");
113         printf("as published by the Free Software Foundation; either version 2\n");
114         printf("of the License, or (at your option) any later version.\n");
115         printf("\n");
116         printf("This program is distributed in the hope that it will be useful,\n");
117         printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
118         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
119         printf("GNU General Public License for more details.\n");
120         printf("\n");
121         printf("You should have received a copy of the GNU General Public License\n");
122         printf("along with this program; if not, write to the Free Software\n");
123         printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
124         printf("==============================================================================\n");
125         printf("Usage:\n");
126         printf("  metaflac [options] flacfile [...]\n");
127         printf("\n");
128         printf("options:\n");
129         printf("  -l : list metadata blocks\n");
130         printf("  -v : verbose\n");
131
132         return message? 1 : 0;
133 }
134
135 FLAC__bool list(FILE *f, FLAC__bool verbose)
136 {
137         FLAC__byte buf[65536];
138         FLAC__byte *b = buf;
139         FLAC__StreamMetaData metadata;
140         unsigned blocknum = 0, byte_offset = 0, i;
141
142         /* skip any id3v2 tag */
143         if(fread(buf, 1, 4, f) < 4) {
144                 fprintf(stderr, "ERROR: not a FLAC file\n");
145                 return false;
146         }
147         if(0 == memcmp(buf, "ID3", 3)) {
148                 unsigned tag_length = 0;
149
150                 /* skip to the tag length */
151                 if(fseek(f, 2, SEEK_CUR) < 0) {
152                         fprintf(stderr, "ERROR: bad ID3v2 tag\n");
153                         return false;
154                 }
155
156                 /* read the length */
157                 for(i = 0; i < 4; i++) {
158                         if(fread(buf, 1, 1, f) < 1 || buf[0] & 0x80) {
159                                 fprintf(stderr, "ERROR: bad ID3v2 tag\n");
160                                 return false;
161                         }
162                         tag_length <<= 7;
163                         tag_length |= (buf[0] & 0x7f);
164                 }
165
166                 /* skip the rest of the tag */
167                 if(fseek(f, tag_length, SEEK_CUR) < 0) {
168                         fprintf(stderr, "ERROR: bad ID3v2 tag\n");
169                         return false;
170                 }
171
172                 /* read the stream sync code */
173                 if(fread(buf, 1, 4, f) < 4) {
174                         fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
175                         return false;
176                 }
177         }
178
179         /* check the stream sync code */
180         if(memcmp(buf, sync_string_, 4)) {
181                 fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
182                 return false;
183         }
184         byte_offset += 4;
185
186         /* read the metadata blocks */
187         do {
188                 /* read the metadata block header */
189                 if(fread(buf, 1, 4, f) < 4) {
190                         fprintf(stderr, "ERROR: short count reading metadata block header\n");
191                         return false;
192                 }
193                 metadata.is_last = (buf[0] & 0x80)? true:false;
194                 metadata.type = (FLAC__MetaDataType)(buf[0] & 0x7f);
195                 metadata.length = unpack_uint32(buf+1, 3);
196
197                 /* print header */
198                 printf("METADATA block #%u\n", blocknum);
199                 printf("  byte offset: %u\n", byte_offset);
200                 printf("  type: %u (%s)\n", (unsigned)metadata.type, metadata.type<=FLAC__METADATA_TYPE_SEEKTABLE? metadata_type_string_[metadata.type] : "UNKNOWN");
201                 printf("  is last: %s\n", metadata.is_last? "true":"false");
202                 printf("  length: %u\n", metadata.length);
203
204                 if(metadata.length > sizeof(buf)) {
205                         printf("  SKIPPING large block\n");
206                         if(fseek(f, metadata.length, SEEK_CUR) < 0) {
207                                 fprintf(stderr, "ERROR: short count skipping metadata block data\n");
208                                 return false;
209                         }
210                         continue;
211                 }
212
213                 /* read the metadata block data */
214                 if(fread(buf, 1, metadata.length, f) < metadata.length) {
215                         fprintf(stderr, "ERROR: short count reading metadata block data\n");
216                         return false;
217                 }
218                 switch(metadata.type) {
219                         case FLAC__METADATA_TYPE_STREAMINFO:
220                                 b = buf;
221                                 metadata.data.stream_info.min_blocksize = unpack_uint32(b, 2); b += 2;
222                                 metadata.data.stream_info.max_blocksize = unpack_uint32(b, 2); b += 2;
223                                 metadata.data.stream_info.min_framesize = unpack_uint32(b, 3); b += 3;
224                                 metadata.data.stream_info.max_framesize = unpack_uint32(b, 3); b += 3;
225                                 metadata.data.stream_info.sample_rate = (unpack_uint32(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
226                                 metadata.data.stream_info.channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
227                                 metadata.data.stream_info.bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 1) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
228                                 metadata.data.stream_info.total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64(b+4, 4);
229                                 memcpy(metadata.data.stream_info.md5sum, b+8, 16);
230                                 break;
231                         case FLAC__METADATA_TYPE_PADDING:
232                                 if(verbose) {
233                                         /* dump contents */
234                                 }
235                                 break;
236                         case FLAC__METADATA_TYPE_APPLICATION:
237                                 memcpy(metadata.data.application.id, buf, 4);
238                                 metadata.data.application.data = buf+4;
239                                 break;
240                         case FLAC__METADATA_TYPE_SEEKTABLE:
241                                 metadata.data.seek_table.num_points = metadata.length / SEEKPOINT_LEN_;
242                                 b = buf; /* we leave the points in buf for printing later */
243                                 break;
244                         default:
245                                 printf("SKIPPING block of unknown type\n");
246                                 continue;
247                 }
248
249                 /* print data */
250                 switch(metadata.type) {
251                         case FLAC__METADATA_TYPE_STREAMINFO:
252                                 printf("  minumum blocksize: %u samples\n", metadata.data.stream_info.min_blocksize);
253                                 printf("  maximum blocksize: %u samples\n", metadata.data.stream_info.max_blocksize);
254                                 printf("  minimum framesize: %u bytes\n", metadata.data.stream_info.min_framesize);
255                                 printf("  maximum framesize: %u bytes\n", metadata.data.stream_info.max_framesize);
256                                 printf("  sample_rate: %u Hz\n", metadata.data.stream_info.sample_rate);
257                                 printf("  channels: %u\n", metadata.data.stream_info.channels);
258                                 printf("  bits-per-sample: %u\n", metadata.data.stream_info.bits_per_sample);
259                                 printf("  total samples: %llu\n", metadata.data.stream_info.total_samples);
260                                 printf("  MD5 signature: ");
261                                 for(i = 0; i < 16; i++)
262                                         printf("%02x", metadata.data.stream_info.md5sum[i]);
263                                 printf("\n");
264                                 break;
265                         case FLAC__METADATA_TYPE_PADDING:
266                                 if(verbose) {
267                                         printf("  pad contents:\n");
268                                         hexdump(buf, metadata.length, "    ");
269                                 }
270                                 break;
271                         case FLAC__METADATA_TYPE_APPLICATION:
272                                 printf("  application ID: ");
273                                 for(i = 0; i < 4; i++)
274                                         printf("%02x", metadata.data.application.id[i]);
275                                 printf("\n");
276                                 if(verbose) {
277                                         printf("  data contents:\n");
278                                         hexdump(metadata.data.application.data, metadata.length, "    ");
279                                 }
280                                 break;
281                         case FLAC__METADATA_TYPE_SEEKTABLE:
282                                 printf("  seek points: %u\n", metadata.data.seek_table.num_points);
283                                 if(verbose) {
284                                         for(i = 0; i < metadata.data.seek_table.num_points; i++, b += SEEKPOINT_LEN_)
285                                                 printf("    point %d: sample_number=%llu, stream_offset=%llu, frame_samples=%u\n", i, unpack_uint64(b, 8), unpack_uint64(b+8, 8), unpack_uint32(b+16, 2));
286                                 }
287                                 break;
288                         default:
289                                 FLAC__ASSERT(0);
290                 }
291
292                 blocknum++;
293                 byte_offset += (4 + metadata.length);
294         } while (!metadata.is_last);
295
296         return true;
297 }
298
299 FLAC__uint32 unpack_uint32(FLAC__byte *b, unsigned bytes)
300 {
301         FLAC__uint32 ret = 0;
302         unsigned i;
303
304         for(i = 0; i < bytes; i++)
305                 ret = (ret << 8) | (FLAC__uint32)(*b++);
306
307         return ret;
308 }
309
310 FLAC__uint64 unpack_uint64(FLAC__byte *b, unsigned bytes)
311 {
312         FLAC__uint64 ret = 0;
313         unsigned i;
314
315         for(i = 0; i < bytes; i++)
316                 ret = (ret << 8) | (FLAC__uint64)(*b++);
317
318         return ret;
319 }
320
321 void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent)
322 {
323         unsigned i, left = bytes;
324         const FLAC__byte *b = buf;
325
326         for(i = 0; i < bytes; i += 16) {
327                 printf("%s%08X: "
328                         "%02X %02X %02X %02X %02X %02X %02X %02X "
329                         "%02X %02X %02X %02X %02X %02X %02X %02X "
330                         "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
331                         indent, i,
332                         left >  0? (unsigned char)b[ 0] : 0,
333                         left >  1? (unsigned char)b[ 1] : 0,
334                         left >  2? (unsigned char)b[ 2] : 0,
335                         left >  3? (unsigned char)b[ 3] : 0,
336                         left >  4? (unsigned char)b[ 4] : 0,
337                         left >  5? (unsigned char)b[ 5] : 0,
338                         left >  6? (unsigned char)b[ 6] : 0,
339                         left >  7? (unsigned char)b[ 7] : 0,
340                         left >  8? (unsigned char)b[ 8] : 0,
341                         left >  9? (unsigned char)b[ 9] : 0,
342                         left > 10? (unsigned char)b[10] : 0,
343                         left > 11? (unsigned char)b[11] : 0,
344                         left > 12? (unsigned char)b[12] : 0,
345                         left > 13? (unsigned char)b[13] : 0,
346                         left > 14? (unsigned char)b[14] : 0,
347                         left > 15? (unsigned char)b[15] : 0,
348                         (left >  0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
349                         (left >  1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
350                         (left >  2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
351                         (left >  3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
352                         (left >  4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
353                         (left >  5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
354                         (left >  6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
355                         (left >  7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
356                         (left >  8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
357                         (left >  9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
358                         (left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
359                         (left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
360                         (left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
361                         (left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
362                         (left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
363                         (left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '
364                 );
365                 left -= 16;
366                 b += 16;
367    }
368 }