changelog.html : Added entries about RICE2 residue coding method.
[platform/upstream/flac.git] / src / metaflac / utils.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002,2003,2004,2005,2006,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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "utils.h"
29 #include "FLAC/assert.h"
30 #include "share/alloc.h"
31 #include "share/safe_str.h"
32 #include "share/utf8.h"
33 #include "share/compat.h"
34
35 void die(const char *message)
36 {
37         FLAC__ASSERT(0 != message);
38         flac_fprintf(stderr, "ERROR: %s\n", message);
39         exit(1);
40 }
41
42 #ifdef FLAC__VALGRIND_TESTING
43 size_t local_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
44 {
45         size_t ret = fwrite(ptr, size, nmemb, stream);
46         if(!ferror(stream))
47                 fflush(stream);
48         return ret;
49 }
50 #endif
51
52 char *local_strdup(const char *source)
53 {
54         char *ret;
55         FLAC__ASSERT(0 != source);
56         if(0 == (ret = strdup(source)))
57                 die("out of memory during strdup()");
58         return ret;
59 }
60
61 void local_strcat(char **dest, const char *source)
62 {
63         size_t ndest, nsource;
64
65         FLAC__ASSERT(0 != dest);
66         FLAC__ASSERT(0 != source);
67
68         ndest = *dest? strlen(*dest) : 0;
69         nsource = strlen(source);
70
71         if(nsource == 0)
72                 return;
73
74         *dest = safe_realloc_add_3op_(*dest, ndest, /*+*/nsource, /*+*/1);
75         if(0 == *dest)
76                 die("out of memory growing string");
77         safe_strncpy((*dest)+ndest, source, ndest + nsource + 1);
78 }
79
80 static inline int local_isprint(int c)
81 {
82         if (c < 32) return 0;
83         return isprint(c);
84 }
85
86 void hexdump(const char *filename, const FLAC__byte *buf, unsigned bytes, const char *indent)
87 {
88         unsigned i, left = bytes;
89         const FLAC__byte *b = buf;
90
91         for(i = 0; i < bytes; i += 16) {
92                 flac_printf("%s%s", filename? filename:"", filename? ":":"");
93                 printf("%s%08X: "
94                         "%02X %02X %02X %02X %02X %02X %02X %02X "
95                         "%02X %02X %02X %02X %02X %02X %02X %02X "
96                         "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
97                         indent, i,
98                         left >  0? (unsigned char)b[ 0] : 0,
99                         left >  1? (unsigned char)b[ 1] : 0,
100                         left >  2? (unsigned char)b[ 2] : 0,
101                         left >  3? (unsigned char)b[ 3] : 0,
102                         left >  4? (unsigned char)b[ 4] : 0,
103                         left >  5? (unsigned char)b[ 5] : 0,
104                         left >  6? (unsigned char)b[ 6] : 0,
105                         left >  7? (unsigned char)b[ 7] : 0,
106                         left >  8? (unsigned char)b[ 8] : 0,
107                         left >  9? (unsigned char)b[ 9] : 0,
108                         left > 10? (unsigned char)b[10] : 0,
109                         left > 11? (unsigned char)b[11] : 0,
110                         left > 12? (unsigned char)b[12] : 0,
111                         left > 13? (unsigned char)b[13] : 0,
112                         left > 14? (unsigned char)b[14] : 0,
113                         left > 15? (unsigned char)b[15] : 0,
114                         (left >  0) ? (local_isprint(b[ 0]) ? b[ 0] : '.') : ' ',
115                         (left >  1) ? (local_isprint(b[ 1]) ? b[ 1] : '.') : ' ',
116                         (left >  2) ? (local_isprint(b[ 2]) ? b[ 2] : '.') : ' ',
117                         (left >  3) ? (local_isprint(b[ 3]) ? b[ 3] : '.') : ' ',
118                         (left >  4) ? (local_isprint(b[ 4]) ? b[ 4] : '.') : ' ',
119                         (left >  5) ? (local_isprint(b[ 5]) ? b[ 5] : '.') : ' ',
120                         (left >  6) ? (local_isprint(b[ 6]) ? b[ 6] : '.') : ' ',
121                         (left >  7) ? (local_isprint(b[ 7]) ? b[ 7] : '.') : ' ',
122                         (left >  8) ? (local_isprint(b[ 8]) ? b[ 8] : '.') : ' ',
123                         (left >  9) ? (local_isprint(b[ 9]) ? b[ 9] : '.') : ' ',
124                         (left > 10) ? (local_isprint(b[10]) ? b[10] : '.') : ' ',
125                         (left > 11) ? (local_isprint(b[11]) ? b[11] : '.') : ' ',
126                         (left > 12) ? (local_isprint(b[12]) ? b[12] : '.') : ' ',
127                         (left > 13) ? (local_isprint(b[13]) ? b[13] : '.') : ' ',
128                         (left > 14) ? (local_isprint(b[14]) ? b[14] : '.') : ' ',
129                         (left > 15) ? (local_isprint(b[15]) ? b[15] : '.') : ' '
130                 );
131                 left -= 16;
132                 b += 16;
133    }
134 }
135
136 void print_error_with_chain_status(FLAC__Metadata_Chain *chain, const char *format, ...)
137 {
138         const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status(chain);
139         va_list args;
140
141         FLAC__ASSERT(0 != format);
142
143         va_start(args, format);
144
145         (void) flac_vfprintf(stderr, format, args);
146
147         va_end(args);
148
149         flac_fprintf(stderr, ", status = \"%s\"\n", FLAC__Metadata_ChainStatusString[status]);
150
151         if(status == FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE) {
152                 flac_fprintf(stderr, "\n"
153                         "The FLAC file could not be opened.  Most likely the file does not exist\n"
154                         "or is not readable.\n"
155                 );
156         }
157         else if(status == FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE) {
158                 flac_fprintf(stderr, "\n"
159                         "The file does not appear to be a FLAC file.\n"
160                 );
161         }
162         else if(status == FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE) {
163                 flac_fprintf(stderr, "\n"
164                         "The FLAC file does not have write permissions.\n"
165                 );
166         }
167         else if(status == FLAC__METADATA_CHAIN_STATUS_BAD_METADATA) {
168                 flac_fprintf(stderr, "\n"
169                         "The metadata to be writted does not conform to the FLAC metadata\n"
170                         "specifications.\n"
171                 );
172         }
173         else if(status == FLAC__METADATA_CHAIN_STATUS_READ_ERROR) {
174                 flac_fprintf(stderr, "\n"
175                         "There was an error while reading the FLAC file.\n"
176                 );
177         }
178         else if(status == FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR) {
179                 flac_fprintf(stderr, "\n"
180                         "There was an error while writing FLAC file; most probably the disk is\n"
181                         "full.\n"
182                 );
183         }
184         else if(status == FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR) {
185                 flac_fprintf(stderr, "\n"
186                         "There was an error removing the temporary FLAC file.\n"
187                 );
188         }
189 }
190
191 FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
192 {
193         static const char * const violations[] = {
194                 "field name contains invalid character",
195                 "field contains no '=' character"
196         };
197
198         char *p, *q, *s;
199
200         if(0 != field)
201                 *field = local_strdup(field_ref);
202
203         s = local_strdup(field_ref);
204
205         if(0 == (p = strchr(s, '='))) {
206                 free(s);
207                 *violation = violations[1];
208                 return false;
209         }
210         *p++ = '\0';
211
212         for(q = s; *q; q++) {
213                 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
214                         free(s);
215                         *violation = violations[0];
216                         return false;
217                 }
218         }
219
220         *name = local_strdup(s);
221         *value = local_strdup(p);
222         *length = strlen(p);
223
224         free(s);
225         return true;
226 }
227
228 void write_vc_field(const char *filename, const FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__bool raw, FILE *f)
229 {
230         if(0 != entry->entry) {
231                 if(filename)
232                         flac_fprintf(f, "%s:", filename);
233
234                 if(!raw) {
235                         /*
236                          * WATCHOUT: comments that contain an embedded null will
237                          * be truncated by utf_decode().
238                          */
239 #ifdef _WIN32 /* if we are outputting to console, we need to use proper print functions to show unicode characters */
240                         if (f == stdout || f == stderr) {
241                                 flac_fprintf(f, "%s", entry->entry);
242                         } else {
243 #endif
244                         char *converted;
245
246                         if(utf8_decode((const char *)entry->entry, &converted) >= 0) {
247                                 (void) local_fwrite(converted, 1, strlen(converted), f);
248                                 free(converted);
249                         }
250                         else {
251                                 (void) local_fwrite(entry->entry, 1, entry->length, f);
252                         }
253 #ifdef _WIN32
254                         }
255 #endif
256                 }
257                 else {
258                         (void) local_fwrite(entry->entry, 1, entry->length, f);
259                 }
260         }
261
262         putc('\n', f);
263 }
264
265 void write_vc_fields(const char *filename, const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry entry[], unsigned num_entries, FLAC__bool raw, FILE *f)
266 {
267         unsigned i;
268         const unsigned field_name_length = (0 != field_name)? strlen(field_name) : 0;
269
270         for(i = 0; i < num_entries; i++) {
271                 if(0 == field_name || FLAC__metadata_object_vorbiscomment_entry_matches(entry[i], field_name, field_name_length))
272                         write_vc_field(filename, entry + i, raw, f);
273         }
274 }