src/libFLAC/stream_decoder.c : Fix buffer read overflow.
[platform/upstream/flac.git] / src / metaflac / operations_shorthand_streaminfo.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001-2009  Josh Coalson
3  * Copyright (C) 2011-2013  Xiph.Org Foundation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #if HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "options.h"
25 #include "utils.h"
26 #include "FLAC/assert.h"
27 #include "FLAC/metadata.h"
28 #include "share/compat.h"
29 #include <string.h>
30 #include "operations_shorthand.h"
31
32 FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
33 {
34         unsigned i;
35         FLAC__bool ok = true;
36         FLAC__StreamMetadata *block;
37         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
38
39         if(0 == iterator)
40                 die("out of memory allocating iterator");
41
42         FLAC__metadata_iterator_init(iterator, chain);
43
44         block = FLAC__metadata_iterator_get_block(iterator);
45
46         FLAC__ASSERT(0 != block);
47         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_STREAMINFO);
48
49         if(prefix_with_filename)
50                 flac_printf("%s:", filename);
51
52         switch(operation->type) {
53                 case OP__SHOW_MD5SUM:
54                         for(i = 0; i < 16; i++)
55                                 printf("%02x", block->data.stream_info.md5sum[i]);
56                         printf("\n");
57                         break;
58                 case OP__SHOW_MIN_BLOCKSIZE:
59                         printf("%u\n", block->data.stream_info.min_blocksize);
60                         break;
61                 case OP__SHOW_MAX_BLOCKSIZE:
62                         printf("%u\n", block->data.stream_info.max_blocksize);
63                         break;
64                 case OP__SHOW_MIN_FRAMESIZE:
65                         printf("%u\n", block->data.stream_info.min_framesize);
66                         break;
67                 case OP__SHOW_MAX_FRAMESIZE:
68                         printf("%u\n", block->data.stream_info.max_framesize);
69                         break;
70                 case OP__SHOW_SAMPLE_RATE:
71                         printf("%u\n", block->data.stream_info.sample_rate);
72                         break;
73                 case OP__SHOW_CHANNELS:
74                         printf("%u\n", block->data.stream_info.channels);
75                         break;
76                 case OP__SHOW_BPS:
77                         printf("%u\n", block->data.stream_info.bits_per_sample);
78                         break;
79                 case OP__SHOW_TOTAL_SAMPLES:
80                         printf("%" PRIu64 "\n", block->data.stream_info.total_samples);
81                         break;
82                 case OP__SET_MD5SUM:
83                         memcpy(block->data.stream_info.md5sum, operation->argument.streaminfo_md5.value, 16);
84                         *needs_write = true;
85                         break;
86                 case OP__SET_MIN_BLOCKSIZE:
87                         block->data.stream_info.min_blocksize = operation->argument.streaminfo_uint32.value;
88                         *needs_write = true;
89                         break;
90                 case OP__SET_MAX_BLOCKSIZE:
91                         block->data.stream_info.max_blocksize = operation->argument.streaminfo_uint32.value;
92                         *needs_write = true;
93                         break;
94                 case OP__SET_MIN_FRAMESIZE:
95                         block->data.stream_info.min_framesize = operation->argument.streaminfo_uint32.value;
96                         *needs_write = true;
97                         break;
98                 case OP__SET_MAX_FRAMESIZE:
99                         block->data.stream_info.max_framesize = operation->argument.streaminfo_uint32.value;
100                         *needs_write = true;
101                         break;
102                 case OP__SET_SAMPLE_RATE:
103                         block->data.stream_info.sample_rate = operation->argument.streaminfo_uint32.value;
104                         *needs_write = true;
105                         break;
106                 case OP__SET_CHANNELS:
107                         block->data.stream_info.channels = operation->argument.streaminfo_uint32.value;
108                         *needs_write = true;
109                         break;
110                 case OP__SET_BPS:
111                         block->data.stream_info.bits_per_sample = operation->argument.streaminfo_uint32.value;
112                         *needs_write = true;
113                         break;
114                 case OP__SET_TOTAL_SAMPLES:
115                         block->data.stream_info.total_samples = operation->argument.streaminfo_uint64.value;
116                         *needs_write = true;
117                         break;
118                 default:
119                         ok = false;
120                         FLAC__ASSERT(0);
121                         break;
122         };
123
124         FLAC__metadata_iterator_delete(iterator);
125
126         return ok;
127 }