id3lib 3.8.3

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
35 using namespace dami;
36
37 const char* const ID3_TagHeader::ID = "ID3";
38
39 bool ID3_TagHeader::SetSpec(ID3_V2Spec spec)
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
53 size_t ID3_TagHeader::Size() const
54 {
55 size_t bytesUsed = ID3_TagHeader::SIZE;
56
57 if (_info->is_extended)
58 {
59 bytesUsed += _info->extended_bytes;
60 }
61
62 return bytesUsed;
63 }
64
65
66 void ID3_TagHeader::Render(ID3_Writer& writer) const
67 {
68 writer.writeChars((uchar *) ID, strlen(ID));
69
70 writer.writeChar(ID3_V2SpecToVer(ID3V2_LATEST));
71 writer.writeChar(ID3_V2SpecToRev(ID3V2_LATEST));
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
78 if (_flags.test(HEADER_FLAG_EXTENDED))
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
101 bool ID3_TagHeader::Parse(ID3_Reader& reader)
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 // Get the flags at the appropriate offset
118 _flags.set(static_cast<ID3_Flags::TYPE>(reader.readChar()));
119
120 // set the data size
121 this->SetDataSize(io::readUInt28(reader));
122
123 if (_flags.test(HEADER_FLAG_EXTENDED) && this->GetSpec() == ID3V2_2_1)
124 {
125 //couldn't find anything about this in the draft specifying 2.2.1 -> http://www.id3.org/pipermail/id3v2/2000-April/000126.html
126 _flags.set(HEADER_FLAG_EXTENDED, false);
127 _info->extended_bytes = 0;
128 // rest is checked at ParseExtended()
129 }
130 et.setExitPos(reader.getCur());
131 return true;
132 }
133
134 void ID3_TagHeader::ParseExtended(ID3_Reader& reader)
135 {
136 if (this->GetSpec() == ID3V2_3_0)
137 {
138 /*
139 Extended header size $xx xx xx xx
140 Extended Flags $xx xx
141 Size of padding $xx xx xx xx
142 */
143 // skip over header size, we are not using it anyway, we calculate it
144 reader.setCur(reader.getCur()+4); //Extended header size
145 //io::readBENumber(reader, 4); //Extended header size
146 uint16 tmpval = io::readBENumber(reader, 2); //Extended Flags
147 // skip over padding size, we are not using it anyway
148 reader.setCur(reader.getCur()+4); //Size of padding
149 // io::readBENumber(reader, 4); //Size of padding
150 if (tmpval != 0) //there is only one flag defined in ID3V2_3_0: crc
151 {
152 //skip over crc data, we are not using it anyway
153 reader.setCur(reader.getCur()+4); //Crc
154 //io::readBENumber(reader, 4); //Crc
155 _info->extended_bytes = 14;
156 }
157 else
158 _info->extended_bytes = 10;
159 }
160 if (this->GetSpec() == ID3V2_4_0)
161 {
162 /*
163 Extended header size 4 * %0xxxxxxx
164 Number of flag bytes $01
165 Extended Flags $xx
166 */
167 uint16 i;
168 uint16 extrabytes;
169
170 io::readUInt28(reader);
171 const int extflagbytes = reader.readChar(); //Number of flag bytes
172 ID3_Flags* extflags[1]; // ID3V2_4_0 has 1 flag byte, extflagbytes should be equal to 1
173 for (i = 0; i < extflagbytes; ++i)
174 {
175 extflags[i] = new ID3_Flags;
176 extflags[i]->set(reader.readChar()); //flags
177 }
178 extrabytes = 0;
179 //extflags[0]->test(EXT_HEADER_FLAG_BIT1); // ID3V2_4_0 ext header flag bit 1 *should* be 0
180 if (extflags[0]->test(EXT_HEADER_FLAG_BIT2))
181 {
182 // ID3V2_4_0 ext header flag bit 2 = Tag is an update
183 // read size
184 extrabytes += 1; // add a byte for the char containing the extflagdatasize
185 const int extheaderflagdatasize = reader.readChar();
186 extrabytes += extheaderflagdatasize;
187 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
188 reader.setCur(reader.getCur() + extheaderflagdatasize);
189 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
190 }
191 if (extflags[0]->test(EXT_HEADER_FLAG_BIT3))
192 {
193 // ID3V2_4_0 ext header flag bit 3 = CRC data present
194 // read size
195 extrabytes += 1; // add a byte for the char containing the extflagdatasize
196 const int extheaderflagdatasize = reader.readChar();
197 extrabytes += extheaderflagdatasize;
198 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
199 reader.setCur(reader.getCur() + extheaderflagdatasize);
200 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
201 }
202 if (extflags[0]->test(EXT_HEADER_FLAG_BIT4))
203 {
204 // ID3V2_4_0 ext header flag bit 4 = Tag restrictions
205 // read size
206 extrabytes += 1; // add a byte for the char containing the extflagdatasize
207 const int extheaderflagdatasize = reader.readChar();
208 extrabytes += extheaderflagdatasize;
209 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
210 reader.setCur(reader.getCur() + extheaderflagdatasize);
211 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
212 }
213 _info->extended_bytes = 5 + extflagbytes + extrabytes;
214 }
215 // a bit unorthodox, but since we are not using any of the extended header, but were merely
216 // parsing it to get the cursor right, we delete it. Be Gone !
217 _flags.set(HEADER_FLAG_EXTENDED, false);
218 if (_info)
219 {
220 _data_size -= _info->extended_bytes;
221 _info->extended_bytes = 0;
222 }//else there is a tag with a higher or lower version than supported
223 }
224
tag.h
header_tag.h
ID3_Tag::IsV2Tag
static size_t IsV2Tag(const uchar *)
Analyses a buffer to determine if we have a valid ID3v2 tag header.
Definition: tag.cpp:955
ID3_Writer::writeChars
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.
ID3_TagHeader::ID
static const char *const ID
Definition: header_tag.h:100
ID3_Writer::END_OF_WRITER
static const int_type END_OF_WRITER
Definition: writer.h:41
ID3_V2SpecToVer
uchar ID3_V2SpecToVer(ID3_V2Spec spec)
Definition: spec.cpp:66
ID3_Reader::getCur
virtual pos_type getCur()=0
Return the current position in the reader.
ID3_Reader::setCur
virtual pos_type setCur(pos_type pos)=0
Set the value of the current position for reading.
ID3_TagHeader::SIZE
Definition: header_tag.h:108
ID3_Flags::set
bool set(TYPE f)
Definition: flags.h:43
ID3_Reader::readChars
virtual size_type readChars(char_type buf[], size_type len)=0
Read up to len characters into buf and advance the internal position accordingly. ...
uchar
unsigned char uchar
Definition: globals.h:114
utils.h
MASK8
#define MASK8
Definition: globals.h:705
ID3V2_4_0
Definition: globals.h:167
ID3_TagHeader::ParseExtended
void ParseExtended(ID3_Reader &)
Definition: header_tag.cpp:134
ID3_Reader::readChar
virtual int_type readChar()
Read a single character and advance the internal position.
Definition: reader.h:65
ID3_Reader
Definition: reader.h:33
ID3_VerRevToV2Spec
ID3_V2Spec ID3_VerRevToV2Spec(uchar ver, uchar rev)
Definition: spec.cpp:34
ID3_V2Spec
ID3_V2Spec
Definition: globals.h:161
ID3_V2SpecToRev
uchar ID3_V2SpecToRev(ID3_V2Spec spec)
Definition: spec.cpp:87
ID3_Header::SetSpec
virtual bool SetSpec(ID3_V2Spec)
Definition: header.cpp:34
ID3_TagHeader::Parse
bool Parse(ID3_Reader &)
Definition: header_tag.cpp:101
spec.h
ID3V2_3_0
Definition: globals.h:166
ID3_TagHeader::SetSpec
bool SetSpec(ID3_V2Spec)
Definition: header_tag.cpp:39
ID3V2_2_1
Definition: globals.h:165
ID3_Writer
Definition: writer.h:33
ID3_TagHeader::Render
void Render(ID3_Writer &) const
Definition: header_tag.cpp:66
ID3_Writer::writeChar
virtual int_type writeChar(char_type ch)
Write a single character and advance the internal position.
Definition: writer.h:71
ID3_TagHeader::Size
size_t Size() const
Definition: header_tag.cpp:53
ID3_Flags
Definition: flags.h:33
ID3V2_LATEST
Definition: globals.h:169