id3lib 3.8.3
header_tag.cpp
Go to the documentation of this file.
1// $Id: header_tag.cpp,v 1.25 2003/03/02 14:30:46 t1mpy Exp $
2
3// id3lib: a C++ library for creating and manipulating id3v1/v2 tags
4// Copyright 1999, 2000 Scott Thomas Haug
5// Copyright 2002 Thijmen Klok (thijmen@id3lib.org)
6
7// This library is free software; you can redistribute it and/or modify it
8// under the terms of the GNU Library General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// This library is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15// License for more details.
16//
17// You should have received a copy of the GNU Library General Public License
18// along with this library; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21// The id3lib authors encourage improvements and optimisations to be sent to
22// the id3lib coordinator. Please see the README file for details on where to
23// send such submissions. See the AUTHORS file for a list of people who have
24// contributed to id3lib. See the ChangeLog file for a list of changes to
25// id3lib. These files are distributed with id3lib at
26// http://download.sourceforge.net/id3lib/
27
28
29#include "header_tag.h"
30#include "id3/utils.h" // has <config.h> "id3/id3lib_streams.h" "id3/globals.h" "id3/id3lib_strings.h"
31#include "tag.h"
32#include "io_helpers.h"
33#include "spec.h"
34
35using namespace dami;
36
37const char* const ID3_TagHeader::ID = "ID3";
38
40{
41 bool changed = this->ID3_Header::SetSpec(spec);
42 if (changed)
43 {
44 if (_info)
45 {
46 _flags.set(HEADER_FLAG_EXPERIMENTAL, _info->is_experimental);
47 _flags.set(HEADER_FLAG_EXTENDED, _info->is_extended);
48 }
49 }
50 return changed;
51}
52
53size_t ID3_TagHeader::Size() const
54{
55 size_t bytesUsed = ID3_TagHeader::SIZE;
56
57 if (_info && _info->is_extended)
58 {
59 bytesUsed += _info->extended_bytes;
60 }
61
62 return bytesUsed;
63}
64
65
67{
68 writer.writeChars((uchar *) ID, strlen(ID));
69
72
73 // set the flags byte in the header
74 writer.writeChar(static_cast<uchar>(_flags.get() & MASK8));
75 io::writeUInt28(writer, this->GetDataSize()); //now includes the extended header
76
77 // now we render the extended header
79 {
80 if (this->GetSpec() == ID3V2_4_0)
81 {
82 io::writeUInt28(writer, 6); //write 4 bytes of v2.4.0 ext header containing size '6'
83 io::writeBENumber(writer, 1, 1); //write that it has only one flag byte (value '1')
84 io::writeBENumber(writer, 0, 1); //write flag byte with value '0'
85 }
86 else if (this->GetSpec() == ID3V2_3_0)
87 {
88 io::writeBENumber(writer, 6, sizeof(uint32));
89 for (size_t i = 0; i < 6; ++i)
90 {
91 if (writer.writeChar('\0') == ID3_Writer::END_OF_WRITER)
92 {
93 break;
94 }
95 }
96 }
97 // else //not implemented
98 }
99}
100
102{
103 io::ExitTrigger et(reader);
104 if (!ID3_Tag::IsV2Tag(reader))
105 {
106 ID3D_NOTICE( "ID3_TagHeader::Parse(): not an id3v2 header" );
107 return false;
108 }
109
110 uchar id[3];
111 reader.readChars(id, 3);
112 // The spec version is determined with the MAJOR and MINOR OFFSETs
113 uchar major = reader.readChar();
114 uchar minor = reader.readChar();
115 this->SetSpec(ID3_VerRevToV2Spec(major, minor));
116
117 // id3lib only fully supports up to ID3v2.3.0 (ID3V2_LATEST). A higher or
118 // unrecognised v2 version (e.g. ID3v2.4) cannot be parsed correctly and was
119 // previously dropped without any indication (Debian bug #449186). Emit a
120 // single notice so this case is no longer entirely silent. Parsing
121 // behaviour is intentionally left unchanged.
122 if (this->GetSpec() == ID3V2_UNKNOWN || this->GetSpec() > ID3V2_LATEST)
123 {
124 std::clog << "id3lib: ID3v2." << static_cast<int>(major) << "."
125 << static_cast<int>(minor)
126 << " tag found, but only ID3v2.3.0 and earlier are supported; "
127 << "this tag may not be read correctly" << std::endl;
128 }
129
130 // Get the flags at the appropriate offset
131 _flags.set(static_cast<ID3_Flags::TYPE>(reader.readChar()));
132
133 // set the data size
134 this->SetDataSize(io::readUInt28(reader));
135
136 if (_flags.test(HEADER_FLAG_EXTENDED) && this->GetSpec() == ID3V2_2_1)
137 {
138 //couldn't find anything about this in the draft specifying 2.2.1 -> http://www.id3.org/pipermail/id3v2/2000-April/000126.html
139 _flags.set(HEADER_FLAG_EXTENDED, false);
140 _info->extended_bytes = 0;
141 // rest is checked at ParseExtended()
142 }
143 et.setExitPos(reader.getCur());
144 return true;
145}
146
148{
149 if (this->GetSpec() == ID3V2_3_0)
150 {
151/*
152 Extended header size $xx xx xx xx
153 Extended Flags $xx xx
154 Size of padding $xx xx xx xx
155*/
156 // skip over header size, we are not using it anyway, we calculate it
157 reader.setCur(reader.getCur()+4); //Extended header size
158 //io::readBENumber(reader, 4); //Extended header size
159 uint16 tmpval = io::readBENumber(reader, 2); //Extended Flags
160 // skip over padding size, we are not using it anyway
161 reader.setCur(reader.getCur()+4); //Size of padding
162 // io::readBENumber(reader, 4); //Size of padding
163 if (tmpval != 0) //there is only one flag defined in ID3V2_3_0: crc
164 {
165 //skip over crc data, we are not using it anyway
166 reader.setCur(reader.getCur()+4); //Crc
167 //io::readBENumber(reader, 4); //Crc
168 _info->extended_bytes = 14;
169 }
170 else
171 _info->extended_bytes = 10;
172 }
173 if (this->GetSpec() == ID3V2_4_0)
174 {
175/*
176 Extended header size 4 * %0xxxxxxx
177 Number of flag bytes $01
178 Extended Flags $xx
179*/
180 uint16 i;
181 uint16 extrabytes;
182
183 io::readUInt28(reader);
184 const int extflagbytes = reader.readChar(); //Number of flag bytes
185 ID3_Flags* extflags[1]; // ID3V2_4_0 has 1 flag byte, extflagbytes should be equal to 1
186 for (i = 0; i < extflagbytes; ++i)
187 {
188 extflags[i] = new ID3_Flags;
189 extflags[i]->set(reader.readChar()); //flags
190 }
191 extrabytes = 0;
192 //extflags[0]->test(EXT_HEADER_FLAG_BIT1); // ID3V2_4_0 ext header flag bit 1 *should* be 0
193 if (extflags[0]->test(EXT_HEADER_FLAG_BIT2))
194 {
195 // ID3V2_4_0 ext header flag bit 2 = Tag is an update
196 // read size
197 extrabytes += 1; // add a byte for the char containing the extflagdatasize
198 const int extheaderflagdatasize = reader.readChar();
199 extrabytes += extheaderflagdatasize;
200 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
201 reader.setCur(reader.getCur() + extheaderflagdatasize);
202 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
203 }
204 if (extflags[0]->test(EXT_HEADER_FLAG_BIT3))
205 {
206 // ID3V2_4_0 ext header flag bit 3 = CRC data present
207 // read size
208 extrabytes += 1; // add a byte for the char containing the extflagdatasize
209 const int extheaderflagdatasize = reader.readChar();
210 extrabytes += extheaderflagdatasize;
211 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
212 reader.setCur(reader.getCur() + extheaderflagdatasize);
213 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
214 }
215 if (extflags[0]->test(EXT_HEADER_FLAG_BIT4))
216 {
217 // ID3V2_4_0 ext header flag bit 4 = Tag restrictions
218 // read size
219 extrabytes += 1; // add a byte for the char containing the extflagdatasize
220 const int extheaderflagdatasize = reader.readChar();
221 extrabytes += extheaderflagdatasize;
222 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
223 reader.setCur(reader.getCur() + extheaderflagdatasize);
224 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
225 }
226 _info->extended_bytes = 5 + extflagbytes + extrabytes;
227 }
228 // a bit unorthodox, but since we are not using any of the extended header, but were merely
229 // parsing it to get the cursor right, we delete it. Be Gone !
230 _flags.set(HEADER_FLAG_EXTENDED, false);
231 if (_info)
232 {
233 _data_size -= _info->extended_bytes;
234 _info->extended_bytes = 0;
235 }//else there is a tag with a higher or lower version than supported
236}
237
flags_t TYPE
Definition flags.h:36
bool set(TYPE f)
Definition flags.h:43
bool SetDataSize(size_t size)
Definition header.h:64
ID3_V2Spec GetSpec() const
Definition header.h:62
size_t _data_size
Definition header.h:103
Info * _info
Definition header.h:105
size_t GetDataSize() const
Definition header.h:71
virtual bool SetSpec(ID3_V2Spec)
Definition header.cpp:34
ID3_Flags _flags
Definition header.h:104
virtual pos_type setCur(pos_type pos)=0
Set the value of the current position for reading.
virtual pos_type getCur()=0
Return the current position in the reader.
virtual size_type readChars(char_type buf[], size_type len)=0
Read up to len characters into buf and advance the internal position accordingly.
virtual int_type readChar()
Read a single character and advance the internal position.
Definition reader.h:65
@ HEADER_FLAG_EXPERIMENTAL
Definition header_tag.h:42
void Render(ID3_Writer &) const
bool Parse(ID3_Reader &)
void ParseExtended(ID3_Reader &)
static const char *const ID
Definition header_tag.h:100
size_t Size() const
bool SetSpec(ID3_V2Spec)
static size_t IsV2Tag(const uchar *)
Analyses a buffer to determine if we have a valid ID3v2 tag header.
Definition tag.cpp:955
virtual size_type writeChars(const char_type buf[], size_type len)=0
Write up to len characters into buf and advance the internal position accordingly.
virtual int_type writeChar(char_type ch)
Write a single character and advance the internal position.
Definition writer.h:71
static const int_type END_OF_WRITER
Definition writer.h:41
#define MASK8
Definition globals.h:713
ID3_V2Spec
Definition globals.h:170
@ ID3V2_LATEST
Definition globals.h:177
@ ID3V2_4_0
Definition globals.h:175
@ ID3V2_2_1
Definition globals.h:173
@ ID3V2_UNKNOWN
Definition globals.h:171
@ ID3V2_3_0
Definition globals.h:174
unsigned char uchar
Definition globals.h:122
uchar ID3_V2SpecToRev(ID3_V2Spec spec)
Definition spec.cpp:87
ID3_V2Spec ID3_VerRevToV2Spec(uchar ver, uchar rev)
Definition spec.cpp:34
uchar ID3_V2SpecToVer(ID3_V2Spec spec)
Definition spec.cpp:66