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