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