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