summaryrefslogtreecommitdiff
path: root/libs/taglib/taglib/ape/apefooter.cpp
blob: da6494b0b0d2df67c59f76fc5def740ffaa64d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/***************************************************************************
    copyright            : (C) 2004 by Allan Sandfeld Jensen
                           (C) 2002 - 2008 by Scott Wheeler (id3v2header.cpp)
    email                : kde@carewolf.org
 ***************************************************************************/

/***************************************************************************
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License version   *
 *   2.1 as published by the Free Software Foundation.                     *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful, but   *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
 *   USA                                                                   *
 *                                                                         *
 *   Alternatively, this file is available under the Mozilla Public        *
 *   License Version 1.1.  You may obtain a copy of the License at         *
 *   http://www.mozilla.org/MPL/                                           *
 ***************************************************************************/

#include <iostream>
#include <bitset>

#include <tstring.h>
#include <tdebug.h>

#include "apefooter.h"

using namespace TagLib;
using namespace APE;

class Footer::FooterPrivate
{
public:
  FooterPrivate() : version(0),
                    footerPresent(true),
                    headerPresent(false),
                    isHeader(false),
                    itemCount(0),
                    tagSize(0) {}

  ~FooterPrivate() {}

  uint version;

  bool footerPresent;
  bool headerPresent;

  bool isHeader;

  uint itemCount;
  uint tagSize;

  static const uint size = 32;
};

////////////////////////////////////////////////////////////////////////////////
// static members
////////////////////////////////////////////////////////////////////////////////

TagLib::uint Footer::size()
{
  return FooterPrivate::size;
}

ByteVector Footer::fileIdentifier()
{
  return ByteVector::fromCString("APETAGEX");
}

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

Footer::Footer()
{
  d = new FooterPrivate;
}

Footer::Footer(const ByteVector &data)
{
  d = new FooterPrivate;
  parse(data);
}

Footer::~Footer()
{
  delete d;
}

TagLib::uint Footer::version() const
{
  return d->version;
}

bool Footer::headerPresent() const
{
  return d->headerPresent;
}

bool Footer::footerPresent() const
{
  return d->footerPresent;
}

bool Footer::isHeader() const
{
  return d->isHeader;
}

void Footer::setHeaderPresent(bool b) const
{
  d->headerPresent = b;
}

TagLib::uint Footer::itemCount() const
{
  return d->itemCount;
}

void Footer::setItemCount(uint s)
{
  d->itemCount = s;
}

TagLib::uint Footer::tagSize() const
{
  return d->tagSize;
}

TagLib::uint Footer::completeTagSize() const
{
  if(d->headerPresent)
    return d->tagSize + d->size;
  else
    return d->tagSize;
}

void Footer::setTagSize(uint s)
{
  d->tagSize = s;
}

void Footer::setData(const ByteVector &data)
{
  parse(data);
}

ByteVector Footer::renderFooter() const
{
    return render(false);
}

ByteVector Footer::renderHeader() const
{
    if (!d->headerPresent) return ByteVector();

    return render(true);
}

////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

void Footer::parse(const ByteVector &data)
{
  if(data.size() < size())
    return;

  // The first eight bytes, data[0..7], are the File Identifier, "APETAGEX".

  // Read the version number

  d->version = data.mid(8, 4).toUInt(false);

  // Read the tag size

  d->tagSize = data.mid(12, 4).toUInt(false);

  // Read the item count

  d->itemCount = data.mid(16, 4).toUInt(false);

  // Read the flags

  std::bitset<32> flags(data.mid(20, 4).toUInt(false));

  d->headerPresent = flags[31];
  d->footerPresent = !flags[30];
  d->isHeader = flags[29];

}

ByteVector Footer::render(bool isHeader) const
{
  ByteVector v;

  // add the file identifier -- "APETAGEX"

  v.append(fileIdentifier());

  // add the version number -- we always render a 2.000 tag regardless of what
  // the tag originally was.

  v.append(ByteVector::fromUInt(2000, false));

  // add the tag size

  v.append(ByteVector::fromUInt(d->tagSize, false));

  // add the item count

  v.append(ByteVector::fromUInt(d->itemCount, false));

  // render and add the flags

  std::bitset<32> flags;

  flags[31] = d->headerPresent;
  flags[30] = false; // footer is always present
  flags[29] = isHeader;

  v.append(ByteVector::fromUInt(flags.to_ulong(), false));

  // add the reserved 64bit

  v.append(ByteVector::fromLongLong(0));

  return v;
}