f5678df22c3f726c81231d58e168891e22fac6de
[flac.git] / src / libFLAC++ / metadata.cpp
1 /* libFLAC++ - Free Lossless Audio Codec library
2  * Copyright (C) 2002,2003,2004  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "FLAC++/metadata.h"
33 #include "FLAC/assert.h"
34 #include <stdlib.h> // for malloc(), free()
35 #include <string.h> // for memcpy() etc.
36
37 #ifdef _MSC_VER
38 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
39 #pragma warning ( disable : 4800 )
40 #endif
41
42 namespace FLAC {
43         namespace Metadata {
44
45                 // local utility routines
46
47                 namespace local {
48
49                         Prototype *construct_block(::FLAC__StreamMetadata *object)
50                         {
51                                 Prototype *ret = 0;
52                                 switch(object->type) {
53                                         case FLAC__METADATA_TYPE_STREAMINFO:
54                                                 ret = new StreamInfo(object, /*copy=*/false);
55                                                 break;
56                                         case FLAC__METADATA_TYPE_PADDING:
57                                                 ret = new Padding(object, /*copy=*/false);
58                                                 break;
59                                         case FLAC__METADATA_TYPE_APPLICATION:
60                                                 ret = new Application(object, /*copy=*/false);
61                                                 break;
62                                         case FLAC__METADATA_TYPE_SEEKTABLE:
63                                                 ret = new SeekTable(object, /*copy=*/false);
64                                                 break;
65                                         case FLAC__METADATA_TYPE_VORBIS_COMMENT:
66                                                 ret = new VorbisComment(object, /*copy=*/false);
67                                                 break;
68                                         case FLAC__METADATA_TYPE_CUESHEET:
69                                                 ret = new CueSheet(object, /*copy=*/false);
70                                                 break;
71                                         default:
72                                                 ret = new Unknown(object, /*copy=*/false);
73                                                 break;
74                                 }
75                                 return ret;
76                         }
77
78                 };
79
80                 FLACPP_API Prototype *clone(const Prototype *object)
81                 {
82                         FLAC__ASSERT(0 != object);
83
84                         const StreamInfo *streaminfo = dynamic_cast<const StreamInfo *>(object);
85                         const Padding *padding = dynamic_cast<const Padding *>(object);
86                         const Application *application = dynamic_cast<const Application *>(object);
87                         const SeekTable *seektable = dynamic_cast<const SeekTable *>(object);
88                         const VorbisComment *vorbiscomment = dynamic_cast<const VorbisComment *>(object);
89                         const CueSheet *cuesheet = dynamic_cast<const CueSheet *>(object);
90                         const Unknown *unknown = dynamic_cast<const Unknown *>(object);
91
92                         if(0 != streaminfo)
93                                 return new StreamInfo(*streaminfo);
94                         else if(0 != padding)
95                                 return new Padding(*padding);
96                         else if(0 != application)
97                                 return new Application(*application);
98                         else if(0 != seektable)
99                                 return new SeekTable(*seektable);
100                         else if(0 != vorbiscomment)
101                                 return new VorbisComment(*vorbiscomment);
102                         else if(0 != cuesheet)
103                                 return new CueSheet(*cuesheet);
104                         else if(0 != unknown)
105                                 return new Unknown(*unknown);
106                         else {
107                                 FLAC__ASSERT(0);
108                                 return 0;
109                         }
110                 }
111
112                 //
113                 // Prototype
114                 //
115
116                 Prototype::Prototype(const Prototype &object):
117                 object_(::FLAC__metadata_object_clone(object.object_)),
118                 is_reference_(false)
119                 {
120                         FLAC__ASSERT(object.is_valid());
121                 }
122
123                 Prototype::Prototype(const ::FLAC__StreamMetadata &object):
124                 object_(::FLAC__metadata_object_clone(&object)),
125                 is_reference_(false)
126                 {
127                 }
128
129                 Prototype::Prototype(const ::FLAC__StreamMetadata *object):
130                 object_(::FLAC__metadata_object_clone(object)),
131                 is_reference_(false)
132                 {
133                         FLAC__ASSERT(0 != object);
134                 }
135
136                 Prototype::Prototype(::FLAC__StreamMetadata *object, bool copy):
137                 object_(copy? ::FLAC__metadata_object_clone(object) : object),
138                 is_reference_(false)
139                 {
140                         FLAC__ASSERT(0 != object);
141                 }
142
143                 Prototype::~Prototype()
144                 {
145                         clear();
146                 }
147
148                 void Prototype::clear()
149                 {
150                         if(0 != object_ && !is_reference_)
151                                 FLAC__metadata_object_delete(object_);
152                         object_ = 0;
153                 }
154
155                 void Prototype::operator=(const Prototype &object)
156                 {
157                         FLAC__ASSERT(object.is_valid());
158                         clear();
159                         is_reference_ = false;
160                         object_ = ::FLAC__metadata_object_clone(object.object_);
161                 }
162
163                 void Prototype::operator=(const ::FLAC__StreamMetadata &object)
164                 {
165                         clear();
166                         is_reference_ = false;
167                         object_ = ::FLAC__metadata_object_clone(&object);
168                 }
169
170                 void Prototype::operator=(const ::FLAC__StreamMetadata *object)
171                 {
172                         FLAC__ASSERT(0 != object);
173                         clear();
174                         is_reference_ = false;
175                         object_ = ::FLAC__metadata_object_clone(object);
176                 }
177
178                 bool Prototype::get_is_last() const
179                 {
180                         FLAC__ASSERT(is_valid());
181                         return (bool)object_->is_last;
182                 }
183
184                 FLAC__MetadataType Prototype::get_type() const
185                 {
186                         FLAC__ASSERT(is_valid());
187                         return object_->type;
188                 }
189
190                 unsigned Prototype::get_length() const
191                 {
192                         FLAC__ASSERT(is_valid());
193                         return object_->length;
194                 }
195
196                 void Prototype::set_is_last(bool value)
197                 {
198                         FLAC__ASSERT(is_valid());
199                         object_->is_last = value;
200                 }
201
202
203                 //
204                 // StreamInfo
205                 //
206
207                 StreamInfo::StreamInfo():
208                 Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO), /*copy=*/false)
209                 { }
210
211                 StreamInfo::~StreamInfo()
212                 { }
213
214                 unsigned StreamInfo::get_min_blocksize() const
215                 {
216                         FLAC__ASSERT(is_valid());
217                         return object_->data.stream_info.min_blocksize;
218                 }
219
220                 unsigned StreamInfo::get_max_blocksize() const
221                 {
222                         FLAC__ASSERT(is_valid());
223                         return object_->data.stream_info.max_blocksize;
224                 }
225
226                 unsigned StreamInfo::get_min_framesize() const
227                 {
228                         FLAC__ASSERT(is_valid());
229                         return object_->data.stream_info.min_framesize;
230                 }
231
232                 unsigned StreamInfo::get_max_framesize() const
233                 {
234                         FLAC__ASSERT(is_valid());
235                         return object_->data.stream_info.max_framesize;
236                 }
237
238                 unsigned StreamInfo::get_sample_rate() const
239                 {
240                         FLAC__ASSERT(is_valid());
241                         return object_->data.stream_info.sample_rate;
242                 }
243
244                 unsigned StreamInfo::get_channels() const
245                 {
246                         FLAC__ASSERT(is_valid());
247                         return object_->data.stream_info.channels;
248                 }
249
250                 unsigned StreamInfo::get_bits_per_sample() const
251                 {
252                         FLAC__ASSERT(is_valid());
253                         return object_->data.stream_info.bits_per_sample;
254                 }
255
256                 FLAC__uint64 StreamInfo::get_total_samples() const
257                 {
258                         FLAC__ASSERT(is_valid());
259                         return object_->data.stream_info.total_samples;
260                 }
261
262                 const FLAC__byte *StreamInfo::get_md5sum() const
263                 {
264                         FLAC__ASSERT(is_valid());
265                         return object_->data.stream_info.md5sum;
266                 }
267
268                 void StreamInfo::set_min_blocksize(unsigned value)
269                 {
270                         FLAC__ASSERT(is_valid());
271                         FLAC__ASSERT(value >= FLAC__MIN_BLOCK_SIZE);
272                         FLAC__ASSERT(value <= FLAC__MAX_BLOCK_SIZE);
273                         object_->data.stream_info.min_blocksize = value;
274                 }
275
276                 void StreamInfo::set_max_blocksize(unsigned value)
277                 {
278                         FLAC__ASSERT(is_valid());
279                         FLAC__ASSERT(value >= FLAC__MIN_BLOCK_SIZE);
280                         FLAC__ASSERT(value <= FLAC__MAX_BLOCK_SIZE);
281                         object_->data.stream_info.max_blocksize = value;
282                 }
283
284                 void StreamInfo::set_min_framesize(unsigned value)
285                 {
286                         FLAC__ASSERT(is_valid());
287                         FLAC__ASSERT(value < (1u < FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
288                         object_->data.stream_info.min_framesize = value;
289                 }
290
291                 void StreamInfo::set_max_framesize(unsigned value)
292                 {
293                         FLAC__ASSERT(is_valid());
294                         FLAC__ASSERT(value < (1u < FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
295                         object_->data.stream_info.max_framesize = value;
296                 }
297
298                 void StreamInfo::set_sample_rate(unsigned value)
299                 {
300                         FLAC__ASSERT(is_valid());
301                         FLAC__ASSERT(FLAC__format_sample_rate_is_valid(value));
302                         object_->data.stream_info.sample_rate = value;
303                 }
304
305                 void StreamInfo::set_channels(unsigned value)
306                 {
307                         FLAC__ASSERT(is_valid());
308                         FLAC__ASSERT(value > 0);
309                         FLAC__ASSERT(value <= FLAC__MAX_CHANNELS);
310                         object_->data.stream_info.channels = value;
311                 }
312
313                 void StreamInfo::set_bits_per_sample(unsigned value)
314                 {
315                         FLAC__ASSERT(is_valid());
316                         FLAC__ASSERT(value >= FLAC__MIN_BITS_PER_SAMPLE);
317                         FLAC__ASSERT(value <= FLAC__MAX_BITS_PER_SAMPLE);
318                         object_->data.stream_info.bits_per_sample = value;
319                 }
320
321                 void StreamInfo::set_total_samples(FLAC__uint64 value)
322                 {
323                         FLAC__ASSERT(is_valid());
324                         FLAC__ASSERT(value < (1u << FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN));
325                         object_->data.stream_info.total_samples = value;
326                 }
327
328                 void StreamInfo::set_md5sum(const FLAC__byte value[16])
329                 {
330                         FLAC__ASSERT(is_valid());
331                         FLAC__ASSERT(0 != value);
332                         memcpy(object_->data.stream_info.md5sum, value, 16);
333                 }
334
335
336                 //
337                 // Padding
338                 //
339
340                 Padding::Padding():
341                 Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING), /*copy=*/false)
342                 { }
343
344                 Padding::~Padding()
345                 { }
346
347                 void Padding::set_length(unsigned length)
348                 {
349                         FLAC__ASSERT(is_valid());
350                         object_->length = length;
351                 }
352
353
354                 //
355                 // Application
356                 //
357
358                 Application::Application():
359                 Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION), /*copy=*/false)
360                 { }
361
362                 Application::~Application()
363                 { }
364
365                 const FLAC__byte *Application::get_id() const
366                 {
367                         FLAC__ASSERT(is_valid());
368                         return object_->data.application.id;
369                 }
370
371                 const FLAC__byte *Application::get_data() const
372                 {
373                         FLAC__ASSERT(is_valid());
374                         return object_->data.application.data;
375                 }
376
377                 void Application::set_id(const FLAC__byte value[4])
378                 {
379                         FLAC__ASSERT(is_valid());
380                         FLAC__ASSERT(0 != value);
381                         memcpy(object_->data.application.id, value, 4);
382                 }
383
384                 bool Application::set_data(const FLAC__byte *data, unsigned length)
385                 {
386                         FLAC__ASSERT(is_valid());
387                         return (bool)::FLAC__metadata_object_application_set_data(object_, (FLAC__byte*)data, length, true);
388                 }
389
390                 bool Application::set_data(FLAC__byte *data, unsigned length, bool copy)
391                 {
392                         FLAC__ASSERT(is_valid());
393                         return (bool)::FLAC__metadata_object_application_set_data(object_, data, length, copy);
394                 }
395
396
397                 //
398                 // SeekTable
399                 //
400
401                 SeekTable::SeekTable():
402                 Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE), /*copy=*/false)
403                 { }
404
405                 SeekTable::~SeekTable()
406                 { }
407
408                 unsigned SeekTable::get_num_points() const
409                 {
410                         FLAC__ASSERT(is_valid());
411                         return object_->data.seek_table.num_points;
412                 }
413
414                 ::FLAC__StreamMetadata_SeekPoint SeekTable::get_point(unsigned index) const
415                 {
416                         FLAC__ASSERT(is_valid());
417                         FLAC__ASSERT(index < object_->data.seek_table.num_points);
418                         return object_->data.seek_table.points[index];
419                 }
420
421                 void SeekTable::set_point(unsigned index, const ::FLAC__StreamMetadata_SeekPoint &point)
422                 {
423                         FLAC__ASSERT(is_valid());
424                         FLAC__ASSERT(index < object_->data.seek_table.num_points);
425                         ::FLAC__metadata_object_seektable_set_point(object_, index, point);
426                 }
427
428                 bool SeekTable::insert_point(unsigned index, const ::FLAC__StreamMetadata_SeekPoint &point)
429                 {
430                         FLAC__ASSERT(is_valid());
431                         FLAC__ASSERT(index <= object_->data.seek_table.num_points);
432                         return (bool)::FLAC__metadata_object_seektable_insert_point(object_, index, point);
433                 }
434
435                 bool SeekTable::delete_point(unsigned index)
436                 {
437                         FLAC__ASSERT(is_valid());
438                         FLAC__ASSERT(index < object_->data.seek_table.num_points);
439                         return (bool)::FLAC__metadata_object_seektable_delete_point(object_, index);
440                 }
441
442                 bool SeekTable::is_legal() const
443                 {
444                         FLAC__ASSERT(is_valid());
445                         return (bool)::FLAC__metadata_object_seektable_is_legal(object_);
446                 }
447
448
449                 //
450                 // VorbisComment::Entry
451                 //
452
453                 VorbisComment::Entry::Entry()
454                 {
455                         zero();
456                 }
457
458                 VorbisComment::Entry::Entry(const char *field, unsigned field_length)
459                 {
460                         zero();
461                         construct(field, field_length);
462                 }
463
464                 VorbisComment::Entry::Entry(const char *field)
465                 {
466                         zero();
467                         construct(field);
468                 }
469
470                 VorbisComment::Entry::Entry(const char *field_name, const char *field_value, unsigned field_value_length)
471                 {
472                         zero();
473                         construct(field_name, field_value, field_value_length);
474                 }
475
476                 VorbisComment::Entry::Entry(const char *field_name, const char *field_value)
477                 {
478                         zero();
479                         construct(field_name, field_value);
480                 }
481
482                 VorbisComment::Entry::Entry(const Entry &entry)
483                 {
484                         FLAC__ASSERT(entry.is_valid());
485                         zero();
486                         construct((const char *)entry.entry_.entry, entry.entry_.length);
487                 }
488
489                 void VorbisComment::Entry::operator=(const Entry &entry)
490                 {
491                         FLAC__ASSERT(entry.is_valid());
492                         clear();
493                         construct((const char *)entry.entry_.entry, entry.entry_.length);
494                 }
495
496                 VorbisComment::Entry::~Entry()
497                 {
498                         clear();
499                 }
500
501                 bool VorbisComment::Entry::is_valid() const
502                 {
503                         return is_valid_;
504                 }
505
506                 unsigned VorbisComment::Entry::get_field_length() const
507                 {
508                         FLAC__ASSERT(is_valid());
509                         return entry_.length;
510                 }
511
512                 unsigned VorbisComment::Entry::get_field_name_length() const
513                 {
514                         FLAC__ASSERT(is_valid());
515                         return field_name_length_;
516                 }
517
518                 unsigned VorbisComment::Entry::get_field_value_length() const
519                 {
520                         FLAC__ASSERT(is_valid());
521                         return field_value_length_;
522                 }
523
524                 ::FLAC__StreamMetadata_VorbisComment_Entry VorbisComment::Entry::get_entry() const
525                 {
526                         FLAC__ASSERT(is_valid());
527                         return entry_;
528                 }
529
530                 const char *VorbisComment::Entry::get_field() const
531                 {
532                         FLAC__ASSERT(is_valid());
533                         return (const char *)entry_.entry;
534                 }
535
536                 const char *VorbisComment::Entry::get_field_name() const
537                 {
538                         FLAC__ASSERT(is_valid());
539                         return field_name_;
540                 }
541
542                 const char *VorbisComment::Entry::get_field_value() const
543                 {
544                         FLAC__ASSERT(is_valid());
545                         return field_value_;
546                 }
547
548                 bool VorbisComment::Entry::set_field(const char *field, unsigned field_length)
549                 {
550                         FLAC__ASSERT(is_valid());
551                         FLAC__ASSERT(0 != field);
552
553                         if(!::FLAC__format_vorbiscomment_entry_is_legal((const ::FLAC__byte*)field, field_length))
554                                 return is_valid_ = false;
555
556                         clear_entry();
557
558                         if(0 == (entry_.entry = (FLAC__byte*)malloc(field_length+1))) {
559                                 is_valid_ = false;
560                         }
561                         else {
562                                 entry_.length = field_length;
563                                 memcpy(entry_.entry, field, field_length);
564                                 entry_.entry[field_length] = '\0';
565                                 (void) parse_field();
566                         }
567
568                         return is_valid_;
569                 }
570
571                 bool VorbisComment::Entry::set_field(const char *field)
572                 {
573                         return set_field(field, strlen(field));
574                 }
575
576                 bool VorbisComment::Entry::set_field_name(const char *field_name)
577                 {
578                         FLAC__ASSERT(is_valid());
579                         FLAC__ASSERT(0 != field_name);
580
581                         if(!::FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
582                                 return is_valid_ = false;
583
584                         clear_field_name();
585
586                         if(0 == (field_name_ = strdup(field_name))) {
587                                 is_valid_ = false;
588                         }
589                         else {
590                                 field_name_length_ = strlen(field_name_);
591                                 compose_field();
592                         }
593
594                         return is_valid_;
595                 }
596
597                 bool VorbisComment::Entry::set_field_value(const char *field_value, unsigned field_value_length)
598                 {
599                         FLAC__ASSERT(is_valid());
600                         FLAC__ASSERT(0 != field_value);
601
602                         if(!::FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte*)field_value, field_value_length))
603                                 return is_valid_ = false;
604
605                         clear_field_value();
606
607                         if(0 == (field_value_ = (char *)malloc(field_value_length+1))) {
608                                 is_valid_ = false;
609                         }
610                         else {
611                                 field_value_length_ = field_value_length;
612                                 memcpy(field_value_, field_value, field_value_length);
613                                 field_value_[field_value_length] = '\0';
614                                 compose_field();
615                         }
616
617                         return is_valid_;
618                 }
619
620                 bool VorbisComment::Entry::set_field_value(const char *field_value)
621                 {
622                         return set_field_value(field_value, strlen(field_value));
623                 }
624
625                 void VorbisComment::Entry::zero()
626                 {
627                         is_valid_ = true;
628                         entry_.length = 0;
629                         entry_.entry = 0;
630                         field_name_ = 0;
631                         field_name_length_ = 0;
632                         field_value_ = 0;
633                         field_value_length_ = 0;
634                 }
635
636                 void VorbisComment::Entry::clear()
637                 {
638                         clear_entry();
639                         clear_field_name();
640                         clear_field_value();
641                         is_valid_ = true;
642                 }
643
644                 void VorbisComment::Entry::clear_entry()
645                 {
646                         if(0 != entry_.entry) {
647                                 free(entry_.entry);
648                                 entry_.entry = 0;
649                                 entry_.length = 0;
650                         }
651                 }
652
653                 void VorbisComment::Entry::clear_field_name()
654                 {
655                         if(0 != field_name_) {
656                                 free(field_name_);
657                                 field_name_ = 0;
658                                 field_name_length_ = 0;
659                         }
660                 }
661
662                 void VorbisComment::Entry::clear_field_value()
663                 {
664                         if(0 != field_value_) {
665                                 free(field_value_);
666                                 field_value_ = 0;
667                                 field_value_length_ = 0;
668                         }
669                 }
670
671                 void VorbisComment::Entry::construct(const char *field, unsigned field_length)
672                 {
673                         if(set_field(field, field_length))
674                                 parse_field();
675                 }
676
677                 void VorbisComment::Entry::construct(const char *field)
678                 {
679                         construct(field, strlen(field));
680                 }
681
682                 void VorbisComment::Entry::construct(const char *field_name, const char *field_value, unsigned field_value_length)
683                 {
684                         if(set_field_name(field_name) && set_field_value(field_value, field_value_length))
685                                 compose_field();
686                 }
687
688                 void VorbisComment::Entry::construct(const char *field_name, const char *field_value)
689                 {
690                         construct(field_name, field_value, strlen(field_value));
691                 }
692
693                 void VorbisComment::Entry::compose_field()
694                 {
695                         clear_entry();
696
697                         if(0 == (entry_.entry = (FLAC__byte*)malloc(field_name_length_ + 1 + field_value_length_ + 1))) {
698                                 is_valid_ = false;
699                         }
700                         else {
701                                 memcpy(entry_.entry, field_name_, field_name_length_);
702                                 entry_.length += field_name_length_;
703                                 memcpy(entry_.entry + entry_.length, "=", 1);
704                                 entry_.length += 1;
705                                 memcpy(entry_.entry + entry_.length, field_value_, field_value_length_);
706                                 entry_.length += field_value_length_;
707                                 entry_.entry[entry_.length] = '\0';
708                                 is_valid_ = true;
709                         }
710                 }
711
712                 void VorbisComment::Entry::parse_field()
713                 {
714                         clear_field_name();
715                         clear_field_value();
716
717                         const char *p = (const char *)memchr(entry_.entry, '=', entry_.length);
718
719                         if(0 == p)
720                                 p = (const char *)entry_.entry + entry_.length;
721
722                         field_name_length_ = p - (const char *)entry_.entry;
723                         if(0 == (field_name_ = (char *)malloc(field_name_length_ + 1))) { // +1 for the trailing \0
724                                 is_valid_ = false;
725                                 return;
726                         }
727                         memcpy(field_name_, entry_.entry, field_name_length_);
728                         field_name_[field_name_length_] = '\0';
729
730                         if(entry_.length - field_name_length_ == 0) {
731                                 field_value_length_ = 0;
732                                 if(0 == (field_value_ = (char *)malloc(0))) {
733                                         is_valid_ = false;
734                                         return;
735                                 }
736                         }
737                         else {
738                                 field_value_length_ = entry_.length - field_name_length_ - 1;
739                                 if(0 == (field_value_ = (char *)malloc(field_value_length_ + 1))) { // +1 for the trailing \0
740                                         is_valid_ = false;
741                                         return;
742                                 }
743                                 memcpy(field_value_, ++p, field_value_length_);
744                                 field_value_[field_value_length_] = '\0';
745                         }
746
747                         is_valid_ = true;
748                 }
749
750
751                 //
752                 // VorbisComment
753                 //
754
755                 VorbisComment::VorbisComment():
756                 Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT), /*copy=*/false)
757                 { }
758
759                 VorbisComment::~VorbisComment()
760                 { }
761
762                 unsigned VorbisComment::get_num_comments() const
763                 {
764                         FLAC__ASSERT(is_valid());
765                         return object_->data.vorbis_comment.num_comments;
766                 }
767
768                 const FLAC__byte *VorbisComment::get_vendor_string() const
769                 {
770                         FLAC__ASSERT(is_valid());
771                         return object_->data.vorbis_comment.vendor_string.entry;
772                 }
773
774                 VorbisComment::Entry VorbisComment::get_comment(unsigned index) const
775                 {
776                         FLAC__ASSERT(is_valid());
777                         FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
778                         return Entry((const char *)object_->data.vorbis_comment.comments[index].entry, object_->data.vorbis_comment.comments[index].length);
779                 }
780
781                 bool VorbisComment::set_vendor_string(const FLAC__byte *string)
782                 {
783                         FLAC__ASSERT(is_valid());
784                         // vendor_string is a special kind of entry
785                         const ::FLAC__StreamMetadata_VorbisComment_Entry vendor_string = { strlen((const char *)string), (FLAC__byte*)string }; // we can cheat on const-ness because we make a copy below:
786                         return (bool)::FLAC__metadata_object_vorbiscomment_set_vendor_string(object_, vendor_string, /*copy=*/true);
787                 }
788
789                 bool VorbisComment::set_comment(unsigned index, const VorbisComment::Entry &entry)
790                 {
791                         FLAC__ASSERT(is_valid());
792                         FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
793                         return (bool)::FLAC__metadata_object_vorbiscomment_set_comment(object_, index, entry.get_entry(), /*copy=*/true);
794                 }
795
796                 bool VorbisComment::insert_comment(unsigned index, const VorbisComment::Entry &entry)
797                 {
798                         FLAC__ASSERT(is_valid());
799                         FLAC__ASSERT(index <= object_->data.vorbis_comment.num_comments);
800                         return (bool)::FLAC__metadata_object_vorbiscomment_insert_comment(object_, index, entry.get_entry(), /*copy=*/true);
801                 }
802
803                 bool VorbisComment::append_comment(const VorbisComment::Entry &entry)
804                 {
805                         FLAC__ASSERT(is_valid());
806                         return (bool)::FLAC__metadata_object_vorbiscomment_append_comment(object_, entry.get_entry(), /*copy=*/true);
807                 }
808
809                 bool VorbisComment::delete_comment(unsigned index)
810                 {
811                         FLAC__ASSERT(is_valid());
812                         FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
813                         return (bool)::FLAC__metadata_object_vorbiscomment_delete_comment(object_, index);
814                 }
815
816
817                 //
818                 // CueSheet::Track
819                 //
820
821                 CueSheet::Track::Track():
822                 object_(::FLAC__metadata_object_cuesheet_track_new())
823                 { }
824
825                 CueSheet::Track::Track(const ::FLAC__StreamMetadata_CueSheet_Track *track):
826                 object_(::FLAC__metadata_object_cuesheet_track_clone(track))
827                 { }
828
829                 CueSheet::Track::Track(const Track &track):
830                 object_(::FLAC__metadata_object_cuesheet_track_clone(track.object_))
831                 { }
832
833                 void CueSheet::Track::operator=(const Track &track)
834                 {
835                         if(0 != object_)
836                                 ::FLAC__metadata_object_cuesheet_track_delete(object_);
837                         object_ = ::FLAC__metadata_object_cuesheet_track_clone(track.object_);
838                 }
839
840                 CueSheet::Track::~Track()
841                 {
842                         if(0 != object_)
843                                 ::FLAC__metadata_object_cuesheet_track_delete(object_);
844                 }
845
846                 bool CueSheet::Track::is_valid() const
847                 {
848                         return(0 != object_);
849                 }
850
851                 ::FLAC__StreamMetadata_CueSheet_Index CueSheet::Track::get_index(unsigned i) const
852                 {
853                         FLAC__ASSERT(is_valid());
854                         FLAC__ASSERT(i < object_->num_indices);
855                         return object_->indices[i];
856                 }
857
858                 void CueSheet::Track::set_isrc(const char value[12])
859                 {
860                         FLAC__ASSERT(is_valid());
861                         FLAC__ASSERT(0 != value);
862                         memcpy(object_->isrc, value, 12);
863                         object_->isrc[12] = '\0';
864                 }
865
866                 void CueSheet::Track::set_type(unsigned value)
867                 {
868                         FLAC__ASSERT(is_valid());
869                         FLAC__ASSERT(value <= 1);
870                         object_->type = value;
871                 }
872
873                 void CueSheet::Track::set_index(unsigned i, const ::FLAC__StreamMetadata_CueSheet_Index &index)
874                 {
875                         FLAC__ASSERT(is_valid());
876                         FLAC__ASSERT(i < object_->num_indices);
877                         object_->indices[i] = index;
878                 }
879
880
881                 //
882                 // CueSheet
883                 //
884
885                 CueSheet::CueSheet():
886                 Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET), /*copy=*/false)
887                 { }
888
889                 CueSheet::~CueSheet()
890                 { }
891
892                 const char *CueSheet::get_media_catalog_number() const
893                 {
894                         FLAC__ASSERT(is_valid());
895                         return object_->data.cue_sheet.media_catalog_number;
896                 }
897
898                 FLAC__uint64 CueSheet::get_lead_in() const
899                 {
900                         FLAC__ASSERT(is_valid());
901                         return object_->data.cue_sheet.lead_in;
902                 }
903
904                 bool CueSheet::get_is_cd() const
905                 {
906                         FLAC__ASSERT(is_valid());
907                         return object_->data.cue_sheet.is_cd? true : false;
908                 }
909
910                 unsigned CueSheet::get_num_tracks() const
911                 {
912                         FLAC__ASSERT(is_valid());
913                         return object_->data.cue_sheet.num_tracks;
914                 }
915
916                 CueSheet::Track CueSheet::get_track(unsigned i) const
917                 {
918                         FLAC__ASSERT(is_valid());
919                         FLAC__ASSERT(i < object_->data.cue_sheet.num_tracks);
920                         return Track(object_->data.cue_sheet.tracks + i);
921                 }
922
923                 void CueSheet::set_media_catalog_number(const char value[128])
924                 {
925                         FLAC__ASSERT(is_valid());
926                         FLAC__ASSERT(0 != value);
927                         memcpy(object_->data.cue_sheet.media_catalog_number, value, 128);
928                         object_->data.cue_sheet.media_catalog_number[128] = '\0';
929                 }
930
931                 void CueSheet::set_lead_in(FLAC__uint64 value)
932                 {
933                         FLAC__ASSERT(is_valid());
934                         object_->data.cue_sheet.lead_in = value;
935                 }
936
937                 void CueSheet::set_is_cd(bool value)
938                 {
939                         FLAC__ASSERT(is_valid());
940                         object_->data.cue_sheet.is_cd = value;
941                 }
942
943                 void CueSheet::set_index(unsigned track_num, unsigned index_num, const ::FLAC__StreamMetadata_CueSheet_Index &index)
944                 {
945                         FLAC__ASSERT(is_valid());
946                         FLAC__ASSERT(track_num < object_->data.cue_sheet.num_tracks);
947                         FLAC__ASSERT(index_num < object_->data.cue_sheet.tracks[track_num].num_indices);
948                         object_->data.cue_sheet.tracks[track_num].indices[index_num] = index;
949                 }
950
951                 bool CueSheet::insert_index(unsigned track_num, unsigned index_num, const ::FLAC__StreamMetadata_CueSheet_Index &index)
952                 {
953                         FLAC__ASSERT(is_valid());
954                         FLAC__ASSERT(track_num < object_->data.cue_sheet.num_tracks);
955                         FLAC__ASSERT(index_num <= object_->data.cue_sheet.tracks[track_num].num_indices);
956                         return (bool)::FLAC__metadata_object_cuesheet_track_insert_index(object_, track_num, index_num, index);
957                 }
958
959                 bool CueSheet::delete_index(unsigned track_num, unsigned index_num)
960                 {
961                         FLAC__ASSERT(is_valid());
962                         FLAC__ASSERT(track_num < object_->data.cue_sheet.num_tracks);
963                         FLAC__ASSERT(index_num < object_->data.cue_sheet.tracks[track_num].num_indices);
964                         return (bool)::FLAC__metadata_object_cuesheet_track_delete_index(object_, track_num, index_num);
965                 }
966
967                 bool CueSheet::set_track(unsigned i, const CueSheet::Track &track)
968                 {
969                         FLAC__ASSERT(is_valid());
970                         FLAC__ASSERT(i < object_->data.cue_sheet.num_tracks);
971                         // We can safely const_cast since copy=true
972                         return (bool)::FLAC__metadata_object_cuesheet_set_track(object_, i, const_cast< ::FLAC__StreamMetadata_CueSheet_Track*>(track.get_track()), /*copy=*/true);
973                 }
974
975                 bool CueSheet::insert_track(unsigned i, const CueSheet::Track &track)
976                 {
977                         FLAC__ASSERT(is_valid());
978                         FLAC__ASSERT(i <= object_->data.cue_sheet.num_tracks);
979                         // We can safely const_cast since copy=true
980                         return (bool)::FLAC__metadata_object_cuesheet_insert_track(object_, i, const_cast< ::FLAC__StreamMetadata_CueSheet_Track*>(track.get_track()), /*copy=*/true);
981                 }
982
983                 bool CueSheet::delete_track(unsigned i)
984                 {
985                         FLAC__ASSERT(is_valid());
986                         FLAC__ASSERT(i < object_->data.cue_sheet.num_tracks);
987                         return (bool)::FLAC__metadata_object_cuesheet_delete_track(object_, i);
988                 }
989
990                 bool CueSheet::is_legal(bool check_cd_da_subset, const char **violation) const
991                 {
992                         FLAC__ASSERT(is_valid());
993                         return (bool)::FLAC__metadata_object_cuesheet_is_legal(object_, check_cd_da_subset, violation);
994                 }
995
996
997                 //
998                 // Unknown
999                 //
1000
1001                 Unknown::Unknown():
1002                 Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION), /*copy=*/false)
1003                 { }
1004
1005                 Unknown::~Unknown()
1006                 { }
1007
1008                 const FLAC__byte *Unknown::get_data() const
1009                 {
1010                         FLAC__ASSERT(is_valid());
1011                         return object_->data.application.data;
1012                 }
1013
1014                 bool Unknown::set_data(const FLAC__byte *data, unsigned length)
1015                 {
1016                         FLAC__ASSERT(is_valid());
1017                         return (bool)::FLAC__metadata_object_application_set_data(object_, (FLAC__byte*)data, length, true);
1018                 }
1019
1020                 bool Unknown::set_data(FLAC__byte *data, unsigned length, bool copy)
1021                 {
1022                         FLAC__ASSERT(is_valid());
1023                         return (bool)::FLAC__metadata_object_application_set_data(object_, data, length, copy);
1024                 }
1025
1026
1027                 // ============================================================
1028                 //
1029                 //  Level 0
1030                 //
1031                 // ============================================================
1032
1033                 FLACPP_API bool get_streaminfo(const char *filename, StreamInfo &streaminfo)
1034                 {
1035                         FLAC__ASSERT(0 != filename);
1036
1037                         ::FLAC__StreamMetadata object;
1038
1039                         if(::FLAC__metadata_get_streaminfo(filename, &object)) {
1040                                 streaminfo = object;
1041                                 return true;
1042                         }
1043                         else
1044                                 return false;
1045                 }
1046
1047                 FLACPP_API bool get_tags(const char *filename, VorbisComment *&tags)
1048                 {
1049                         FLAC__ASSERT(0 != filename);
1050
1051                         ::FLAC__StreamMetadata *object;
1052
1053                         tags = 0;
1054
1055                         if(::FLAC__metadata_get_tags(filename, &object)) {
1056                                 tags = new VorbisComment(object, /*copy=*/false);
1057                                 return true;
1058                         }
1059                         else
1060                                 return false;
1061                 }
1062
1063
1064                 // ============================================================
1065                 //
1066                 //  Level 1
1067                 //
1068                 // ============================================================
1069
1070                 SimpleIterator::SimpleIterator():
1071                 iterator_(::FLAC__metadata_simple_iterator_new())
1072                 { }
1073
1074                 SimpleIterator::~SimpleIterator()
1075                 {
1076                         clear();
1077                 }
1078
1079                 void SimpleIterator::clear()
1080                 {
1081                         if(0 != iterator_)
1082                                 FLAC__metadata_simple_iterator_delete(iterator_);
1083                         iterator_ = 0;
1084                 }
1085
1086                 bool SimpleIterator::init(const char *filename, bool read_only, bool preserve_file_stats)
1087                 {
1088                         FLAC__ASSERT(0 != filename);
1089                         FLAC__ASSERT(is_valid());
1090                         return (bool)::FLAC__metadata_simple_iterator_init(iterator_, filename, read_only, preserve_file_stats);
1091                 }
1092
1093                 bool SimpleIterator::is_valid() const
1094                 {
1095                         return 0 != iterator_;
1096                 }
1097
1098                 SimpleIterator::Status SimpleIterator::status()
1099                 {
1100                         FLAC__ASSERT(is_valid());
1101                         return Status(::FLAC__metadata_simple_iterator_status(iterator_));
1102                 }
1103
1104                 bool SimpleIterator::is_writable() const
1105                 {
1106                         FLAC__ASSERT(is_valid());
1107                         return (bool)::FLAC__metadata_simple_iterator_is_writable(iterator_);
1108                 }
1109
1110                 bool SimpleIterator::next()
1111                 {
1112                         FLAC__ASSERT(is_valid());
1113                         return (bool)::FLAC__metadata_simple_iterator_next(iterator_);
1114                 }
1115
1116                 bool SimpleIterator::prev()
1117                 {
1118                         FLAC__ASSERT(is_valid());
1119                         return (bool)::FLAC__metadata_simple_iterator_prev(iterator_);
1120                 }
1121
1122                 ::FLAC__MetadataType SimpleIterator::get_block_type() const
1123                 {
1124                         FLAC__ASSERT(is_valid());
1125                         return ::FLAC__metadata_simple_iterator_get_block_type(iterator_);
1126                 }
1127
1128                 Prototype *SimpleIterator::get_block()
1129                 {
1130                         FLAC__ASSERT(is_valid());
1131                         return local::construct_block(::FLAC__metadata_simple_iterator_get_block(iterator_));
1132                 }
1133
1134                 bool SimpleIterator::set_block(Prototype *block, bool use_padding)
1135                 {
1136                         FLAC__ASSERT(0 != block);
1137                         FLAC__ASSERT(is_valid());
1138                         return (bool)::FLAC__metadata_simple_iterator_set_block(iterator_, block->object_, use_padding);
1139                 }
1140
1141                 bool SimpleIterator::insert_block_after(Prototype *block, bool use_padding)
1142                 {
1143                         FLAC__ASSERT(0 != block);
1144                         FLAC__ASSERT(is_valid());
1145                         return (bool)::FLAC__metadata_simple_iterator_insert_block_after(iterator_, block->object_, use_padding);
1146                 }
1147
1148                 bool SimpleIterator::delete_block(bool use_padding)
1149                 {
1150                         FLAC__ASSERT(is_valid());
1151                         return (bool)::FLAC__metadata_simple_iterator_delete_block(iterator_, use_padding);
1152                 }
1153
1154
1155                 // ============================================================
1156                 //
1157                 //  Level 2
1158                 //
1159                 // ============================================================
1160
1161                 Chain::Chain():
1162                 chain_(::FLAC__metadata_chain_new())
1163                 { }
1164
1165                 Chain::~Chain()
1166                 {
1167                         clear();
1168                 }
1169
1170                 void Chain::clear()
1171                 {
1172                         if(0 != chain_)
1173                                 FLAC__metadata_chain_delete(chain_);
1174                         chain_ = 0;
1175                 }
1176
1177                 bool Chain::is_valid() const
1178                 {
1179                         return 0 != chain_;
1180                 }
1181
1182                 Chain::Status Chain::status()
1183                 {
1184                         FLAC__ASSERT(is_valid());
1185                         return Status(::FLAC__metadata_chain_status(chain_));
1186                 }
1187
1188                 bool Chain::read(const char *filename)
1189                 {
1190                         FLAC__ASSERT(0 != filename);
1191                         FLAC__ASSERT(is_valid());
1192                         return (bool)::FLAC__metadata_chain_read(chain_, filename);
1193                 }
1194
1195                 bool Chain::read(FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks)
1196                 {
1197                         FLAC__ASSERT(is_valid());
1198                         return (bool)::FLAC__metadata_chain_read_with_callbacks(chain_, handle, callbacks);
1199                 }
1200
1201                 bool Chain::check_if_tempfile_needed(bool use_padding)
1202                 {
1203                         FLAC__ASSERT(is_valid());
1204                         return (bool)::FLAC__metadata_chain_check_if_tempfile_needed(chain_, use_padding);
1205                 }
1206
1207                 bool Chain::write(bool use_padding, bool preserve_file_stats)
1208                 {
1209                         FLAC__ASSERT(is_valid());
1210                         return (bool)::FLAC__metadata_chain_write(chain_, use_padding, preserve_file_stats);
1211                 }
1212
1213                 bool Chain::write(bool use_padding, ::FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks)
1214                 {
1215                         FLAC__ASSERT(is_valid());
1216                         return (bool)::FLAC__metadata_chain_write_with_callbacks(chain_, use_padding, handle, callbacks);
1217                 }
1218
1219                 bool Chain::write(bool use_padding, ::FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks, ::FLAC__IOHandle temp_handle, ::FLAC__IOCallbacks temp_callbacks)
1220                 {
1221                         FLAC__ASSERT(is_valid());
1222                         return (bool)::FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain_, use_padding, handle, callbacks, temp_handle, temp_callbacks);
1223                 }
1224
1225                 void Chain::merge_padding()
1226                 {
1227                         FLAC__ASSERT(is_valid());
1228                         ::FLAC__metadata_chain_merge_padding(chain_);
1229                 }
1230
1231                 void Chain::sort_padding()
1232                 {
1233                         FLAC__ASSERT(is_valid());
1234                         ::FLAC__metadata_chain_sort_padding(chain_);
1235                 }
1236
1237
1238                 Iterator::Iterator():
1239                 iterator_(::FLAC__metadata_iterator_new())
1240                 { }
1241
1242                 Iterator::~Iterator()
1243                 {
1244                         clear();
1245                 }
1246
1247                 void Iterator::clear()
1248                 {
1249                         if(0 != iterator_)
1250                                 FLAC__metadata_iterator_delete(iterator_);
1251                         iterator_ = 0;
1252                 }
1253
1254                 bool Iterator::is_valid() const
1255                 {
1256                         return 0 != iterator_;
1257                 }
1258
1259                 void Iterator::init(Chain &chain)
1260                 {
1261                         FLAC__ASSERT(is_valid());
1262                         FLAC__ASSERT(chain.is_valid());
1263                         ::FLAC__metadata_iterator_init(iterator_, chain.chain_);
1264                 }
1265
1266                 bool Iterator::next()
1267                 {
1268                         FLAC__ASSERT(is_valid());
1269                         return (bool)::FLAC__metadata_iterator_next(iterator_);
1270                 }
1271
1272                 bool Iterator::prev()
1273                 {
1274                         FLAC__ASSERT(is_valid());
1275                         return (bool)::FLAC__metadata_iterator_prev(iterator_);
1276                 }
1277
1278                 ::FLAC__MetadataType Iterator::get_block_type() const
1279                 {
1280                         FLAC__ASSERT(is_valid());
1281                         return ::FLAC__metadata_iterator_get_block_type(iterator_);
1282                 }
1283
1284                 Prototype *Iterator::get_block()
1285                 {
1286                         FLAC__ASSERT(is_valid());
1287                         Prototype *block = local::construct_block(::FLAC__metadata_iterator_get_block(iterator_));
1288                         if(0 != block)
1289                                 block->set_reference(true);
1290                         return block;
1291                 }
1292
1293                 bool Iterator::set_block(Prototype *block)
1294                 {
1295                         FLAC__ASSERT(0 != block);
1296                         FLAC__ASSERT(is_valid());
1297                         bool ret = (bool)::FLAC__metadata_iterator_set_block(iterator_, block->object_);
1298                         if(ret) {
1299                                 block->set_reference(true);
1300                                 delete block;
1301                         }
1302                         return ret;
1303                 }
1304
1305                 bool Iterator::delete_block(bool replace_with_padding)
1306                 {
1307                         FLAC__ASSERT(is_valid());
1308                         return (bool)::FLAC__metadata_iterator_delete_block(iterator_, replace_with_padding);
1309                 }
1310
1311                 bool Iterator::insert_block_before(Prototype *block)
1312                 {
1313                         FLAC__ASSERT(0 != block);
1314                         FLAC__ASSERT(is_valid());
1315                         bool ret = (bool)::FLAC__metadata_iterator_insert_block_before(iterator_, block->object_);
1316                         if(ret) {
1317                                 block->set_reference(true);
1318                                 delete block;
1319                         }
1320                         return ret;
1321                 }
1322
1323                 bool Iterator::insert_block_after(Prototype *block)
1324                 {
1325                         FLAC__ASSERT(0 != block);
1326                         FLAC__ASSERT(is_valid());
1327                         bool ret = (bool)::FLAC__metadata_iterator_insert_block_after(iterator_, block->object_);
1328                         if(ret) {
1329                                 block->set_reference(true);
1330                                 delete block;
1331                         }
1332                         return ret;
1333                 }
1334
1335         };
1336 };