1 /***************************************************************************
2 copyright : (C) 2002 - 2008 by Scott Wheeler
3 email : wheeler@kde.org
4 ***************************************************************************/
6 /***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
32 #include "id3v2header.h"
33 #include "id3v2footer.h"
34 #include "id3v2synchdata.h"
36 using namespace TagLib;
37 using namespace ID3v2;
39 class Header::HeaderPrivate
42 HeaderPrivate() : majorVersion(4),
44 unsynchronisation(false),
45 extendedHeader(false),
46 experimentalIndicator(false),
55 bool unsynchronisation;
57 bool experimentalIndicator;
62 static const uint size = 10;
65 ////////////////////////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////////////////////////
69 TagLib::uint Header::size()
71 return HeaderPrivate::size;
74 ByteVector Header::fileIdentifier()
76 return ByteVector::fromCString("ID3");
79 ////////////////////////////////////////////////////////////////////////////////
81 ////////////////////////////////////////////////////////////////////////////////
85 d = new HeaderPrivate;
88 Header::Header(const ByteVector &data)
90 d = new HeaderPrivate;
99 TagLib::uint Header::majorVersion() const
101 return d->majorVersion;
104 void Header::setMajorVersion(TagLib::uint version)
106 d->majorVersion = version;
109 TagLib::uint Header::revisionNumber() const
111 return d->revisionNumber;
114 bool Header::unsynchronisation() const
116 return d->unsynchronisation;
119 bool Header::extendedHeader() const
121 return d->extendedHeader;
124 bool Header::experimentalIndicator() const
126 return d->experimentalIndicator;
129 bool Header::footerPresent() const
131 return d->footerPresent;
134 TagLib::uint Header::tagSize() const
139 TagLib::uint Header::completeTagSize() const
142 return d->tagSize + d->size + Footer::size();
144 return d->tagSize + d->size;
147 void Header::setTagSize(uint s)
152 void Header::setData(const ByteVector &data)
157 ByteVector Header::render() const
161 // add the file identifier -- "ID3"
162 v.append(fileIdentifier());
164 // add the version number -- we always render a 2.4.0 tag regardless of what
165 // the tag originally was.
170 // Currently we don't actually support writing extended headers, footers or
171 // unsynchronized tags, make sure that the flags are set accordingly.
173 d->extendedHeader = false;
174 d->footerPresent = false;
175 d->unsynchronisation = false;
177 // render and add the flags
178 std::bitset<8> flags;
180 flags[7] = d->unsynchronisation;
181 flags[6] = d->extendedHeader;
182 flags[5] = d->experimentalIndicator;
183 flags[4] = d->footerPresent;
185 v.append(char(flags.to_ulong()));
188 v.append(SynchData::fromUInt(d->tagSize));
193 ////////////////////////////////////////////////////////////////////////////////
195 ////////////////////////////////////////////////////////////////////////////////
197 void Header::parse(const ByteVector &data)
199 if(data.size() < size())
203 // do some sanity checking -- even in ID3v2.3.0 and less the tag size is a
204 // synch-safe integer, so all bytes must be less than 128. If this is not
205 // true then this is an invalid tag.
207 // note that we're doing things a little out of order here -- the size is
208 // later in the bytestream than the version
210 ByteVector sizeData = data.mid(6, 4);
212 if(sizeData.size() != 4) {
214 debug("TagLib::ID3v2::Header::parse() - The tag size as read was 0 bytes!");
218 for(ByteVector::Iterator it = sizeData.begin(); it != sizeData.end(); it++) {
219 if(uchar(*it) >= 128) {
221 debug("TagLib::ID3v2::Header::parse() - One of the size bytes in the id3v2 header was greater than the allowed 128.");
226 // The first three bytes, data[0..2], are the File Identifier, "ID3". (structure 3.1 "file identifier")
228 // Read the version number from the fourth and fifth bytes.
229 d->majorVersion = data[3]; // (structure 3.1 "major version")
230 d->revisionNumber = data[4]; // (structure 3.1 "revision number")
232 // Read the flags, the first four bits of the sixth byte.
233 std::bitset<8> flags(data[5]);
235 d->unsynchronisation = flags[7]; // (structure 3.1.a)
236 d->extendedHeader = flags[6]; // (structure 3.1.b)
237 d->experimentalIndicator = flags[5]; // (structure 3.1.c)
238 d->footerPresent = flags[4]; // (structure 3.1.d)
240 // Get the size from the remaining four bytes (read above)
242 d->tagSize = SynchData::toUInt(sizeData); // (structure 3.1 "size")