Patch from JonY to suppress redefinition warnings with mingw-w64 lfs64 warnings.
[platform/upstream/flac.git] / src / flac / iffscan.c
1 /* iffscan - Simple AIFF/RIFF chunk scanner
2  * Copyright (C) 2007,2008,2009  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 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #if defined _MSC_VER || defined __MINGW32__
27 #include <sys/types.h> /* for off_t */
28 #if _MSC_VER <= 1600 /* @@@ [2G limit] */
29 #ifndef fseeko
30 #define fseeko fseek
31 #endif
32 #ifndef ftello
33 #define ftello ftell
34 #endif
35 #endif
36 #endif
37 #include "foreign_metadata.h"
38
39 static FLAC__uint32 unpack32be_(const FLAC__byte *b)
40 {
41         return ((FLAC__uint32)b[0]<<24) + ((FLAC__uint32)b[1]<<16) + ((FLAC__uint32)b[2]<<8) + (FLAC__uint32)b[3];
42 }
43
44 static FLAC__uint32 unpack32le_(const FLAC__byte *b)
45 {
46         return (FLAC__uint32)b[0] + ((FLAC__uint32)b[1]<<8) + ((FLAC__uint32)b[2]<<16) + ((FLAC__uint32)b[3]<<24);
47 }
48
49 static FLAC__uint64 unpack64le_(const FLAC__byte *b)
50 {
51         return (FLAC__uint64)b[0] + ((FLAC__uint64)b[1]<<8) + ((FLAC__uint64)b[2]<<16) + ((FLAC__uint64)b[3]<<24) + ((FLAC__uint64)b[4]<<32) + ((FLAC__uint64)b[5]<<40) + ((FLAC__uint64)b[6]<<48) + ((FLAC__uint64)b[7]<<56);
52 }
53
54 static FLAC__uint32 unpack32_(const FLAC__byte *b, foreign_block_type_t type)
55 {
56         if(type == FOREIGN_BLOCK_TYPE__AIFF)
57                 return unpack32be_(b);
58         else
59                 return unpack32le_(b);
60 }
61
62 int main(int argc, char *argv[])
63 {
64         FILE *f;
65         char buf[36];
66         foreign_metadata_t *fm;
67         const char *fn, *error;
68         size_t i;
69         FLAC__uint32 size;
70
71         if(argc != 2) {
72                 fprintf(stderr, "usage: %s { file.wav | file.aif }\n", argv[0]);
73                 return 1;
74         }
75         fn = argv[1];
76         if(0 == (f = fopen(fn, "rb")) || fread(buf, 1, 4, f) != 4) {
77                 fprintf(stderr, "ERROR opening %s for reading\n", fn);
78                 return 1;
79         }
80         fclose(f);
81         if(0 == (fm = flac__foreign_metadata_new(memcmp(buf, "RIFF", 4) && memcmp(buf, "RF64", 4)? FOREIGN_BLOCK_TYPE__AIFF : FOREIGN_BLOCK_TYPE__RIFF))) {
82                 fprintf(stderr, "ERROR: out of memory\n");
83                 return 1;
84         }
85         if(fm->type == FOREIGN_BLOCK_TYPE__AIFF) {
86                 if(!flac__foreign_metadata_read_from_aiff(fm, fn, &error)) {
87                         fprintf(stderr, "ERROR reading chunks from %s: %s\n", fn, error);
88                         return 1;
89                 }
90         }
91         else {
92                 if(!flac__foreign_metadata_read_from_wave(fm, fn, &error)) {
93                         fprintf(stderr, "ERROR reading chunks from %s: %s\n", fn, error);
94                         return 1;
95                 }
96         }
97         if(0 == (f = fopen(fn, "rb"))) {
98                 fprintf(stderr, "ERROR opening %s for reading\n", fn);
99                 return 1;
100         }
101         for(i = 0; i < fm->num_blocks; i++) {
102                 if(fseeko(f, fm->blocks[i].offset, SEEK_SET) < 0) {
103                         fprintf(stderr, "ERROR seeking in %s\n", fn);
104                         return 1;
105                 }
106                 if(fread(buf, 1, i==0?12:8, f) != (i==0?12:8)) {
107                         fprintf(stderr, "ERROR reading %s\n", fn);
108                         return 1;
109                 }
110                 size = unpack32_((const FLAC__byte*)buf+4, fm->type);
111                 printf("block:[%c%c%c%c] size=%08x=(%10u)", buf[0], buf[1], buf[2], buf[3], size, size);
112                 if(i == 0)
113                         printf(" type:[%c%c%c%c]", buf[8], buf[9], buf[10], buf[11]);
114                 else if(fm->type == FOREIGN_BLOCK_TYPE__AIFF && i == fm->audio_block)
115                         printf(" offset size=%08x=(%10u)", fm->ssnd_offset_size, fm->ssnd_offset_size);
116                 else if(fm->type == FOREIGN_BLOCK_TYPE__RIFF && i == 1 && !memcmp(buf, "ds64", 4)) {
117                         if(fread(buf+8, 1, 36-8, f) != 36-8) {
118                                 fprintf(stderr, "ERROR reading %s\n", fn);
119                                 return 1;
120                         }
121                         printf(" RIFF size=%016" PRIx64 "=(" PRIu64 ")", unpack64le_(buf+8), unpack64le_(buf+8));
122                         printf(" data size=%016" PRIx64 "=(" PRIu64 ")", unpack64le_(buf+16), unpack64le_(buf+16));
123                         printf(" sample count=%016" PRIx64 "=(" PRIu64 ")", unpack64le_(buf+24), unpack64le_(buf+24));
124                         printf(" table size=%08x=(%u)", unpack32le_(buf+32), unpack32le_(buf+32));
125                 }
126                 printf("\n");
127         }
128         fclose(f);
129         flac__foreign_metadata_delete(fm);
130         return 0;
131 }