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
***************************************************************************/
26
#ifndef TAGLIB_BYTEVECTOR_H
27
#define TAGLIB_BYTEVECTOR_H
30
#include "taglib_export.h"
40
* This class provides a byte vector with some methods that are useful for
41
* tagging purposes. Many of the search functions are tailored to what is
42
* useful for finding tag related paterns in a data array.
45
class TAGLIB_EXPORT ByteVector
48
#ifndef DO_NOT_DOCUMENT
49
typedef std::vector<char>::iterator Iterator;
50
typedef std::vector<char>::const_iterator ConstIterator;
54
* Constructs an empty byte vector.
59
* Construct a vector of size \a size with all values set to \a value by
62
ByteVector(uint size, char value = 0);
65
* Contructs a byte vector that is a copy of \a v.
67
ByteVector(const ByteVector &v);
70
* Contructs a byte vector that contains \a c.
75
* Constructs a byte vector that copies \a data for up to \a length bytes.
77
ByteVector(const char *data, uint length);
80
* Constructs a byte vector that copies \a data up to the first null
81
* byte. The behavior is undefined if \a data is not null terminated.
82
* This is particularly useful for constructing byte arrays from string
85
ByteVector(const char *data);
88
* Destroys this ByteVector instance.
90
virtual ~ByteVector();
93
* Sets the data for the byte array using the first \a length bytes of \a data
95
ByteVector &setData(const char *data, uint length);
98
* Sets the data for the byte array copies \a data up to the first null
99
* byte. The behavior is undefined if \a data is not null terminated.
101
ByteVector &setData(const char *data);
104
* Returns a pointer to the internal data structure.
106
* \warning Care should be taken when modifying this data structure as it is
107
* easy to corrupt the ByteVector when doing so. Specifically, while the
108
* data may be changed, its length may not be.
113
* Returns a pointer to the internal data structure which may not be modified.
115
const char *data() const;
118
* Returns a byte vector made up of the bytes starting at \a index and
119
* for \a length bytes. If \a length is not specified it will return the bytes
120
* from \a index to the end of the vector.
122
ByteVector mid(uint index, uint length = 0xffffffff) const;
125
* This essentially performs the same as operator[](), but instead of causing
126
* a runtime error if the index is out of bounds, it will return a null byte.
128
char at(uint index) const;
131
* Searches the ByteVector for \a pattern starting at \a offset and returns
132
* the offset. Returns -1 if the pattern was not found. If \a byteAlign is
133
* specified the pattern will only be matched if it starts on a byte divisible
134
* by \a byteAlign (starting from \a offset).
136
int find(const ByteVector &pattern, uint offset = 0, int byteAlign = 1) const;
139
* Searches the ByteVector for \a pattern starting from either the end of the
140
* vector or \a offset and returns the offset. Returns -1 if the pattern was
141
* not found. If \a byteAlign is specified the pattern will only be matched
142
* if it starts on a byte divisible by \a byteAlign (starting from \a offset).
144
int rfind(const ByteVector &pattern, uint offset = 0, int byteAlign = 1) const;
147
* Checks to see if the vector contains the \a pattern starting at position
148
* \a offset. Optionally, if you only want to search for part of the pattern
149
* you can specify an offset within the pattern to start from. Also, you can
150
* specify to only check for the first \a patternLength bytes of \a pattern with
151
* the \a patternLength argument.
153
bool containsAt(const ByteVector &pattern, uint offset, uint patternOffset = 0, uint patternLength = 0xffffffff) const;
156
* Returns true if the vector starts with \a pattern.
158
bool startsWith(const ByteVector &pattern) const;
161
* Returns true if the vector ends with \a pattern.
163
bool endsWith(const ByteVector &pattern) const;
166
* Replaces \a pattern with \a with and returns a reference to the ByteVector
167
* after the operation. This \e does modify the vector.
169
ByteVector &replace(const ByteVector &pattern, const ByteVector &with);
172
* Checks for a partial match of \a pattern at the end of the vector. It
173
* returns the offset of the partial match within the vector, or -1 if the
174
* pattern is not found. This method is particularly useful when searching for
175
* patterns that start in one vector and end in another. When combined with
176
* startsWith() it can be used to find a pattern that overlaps two buffers.
178
* \note This will not match the complete pattern at the end of the string; use
179
* endsWith() for that.
181
int endsWithPartialMatch(const ByteVector &pattern) const;
184
* Appends \a v to the end of the ByteVector.
186
ByteVector &append(const ByteVector &v);
194
* Returns the size of the array.
199
* Resize the vector to \a size. If the vector is currently less than
200
* \a size, pad the remaining spaces with \a padding. Returns a reference
201
* to the resized vector.
203
ByteVector &resize(uint size, char padding = 0);
206
* Returns an Iterator that points to the front of the vector.
211
* Returns a ConstIterator that points to the front of the vector.
213
ConstIterator begin() const;
216
* Returns an Iterator that points to the back of the vector.
221
* Returns a ConstIterator that points to the back of the vector.
223
ConstIterator end() const;
226
* Returns true if the vector is null.
228
* \note A vector may be empty without being null.
234
* Returns true if the ByteVector is empty.
239
bool isEmpty() const;
242
* Returns a CRC checksum of the byte vector's data.
244
uint checksum() const;
247
* Converts the first 4 bytes of the vector to an unsigned integer.
249
* If \a mostSignificantByteFirst is true this will operate left to right
250
* evaluating the integer. For example if \a mostSignificantByteFirst is
251
* true then $00 $00 $00 $01 == 0x00000001 == 1, if false, $01 00 00 00 ==
256
uint toUInt(bool mostSignificantByteFirst = true) const;
259
* Converts the first 2 bytes of the vector to a short.
261
* If \a mostSignificantByteFirst is true this will operate left to right
262
* evaluating the integer. For example if \a mostSignificantByteFirst is
263
* true then $00 $01 == 0x0001 == 1, if false, $01 00 == 0x01000000 == 1.
267
short toShort(bool mostSignificantByteFirst = true) const;
270
* Converts the first 8 bytes of the vector to a (signed) long long.
272
* If \a mostSignificantByteFirst is true this will operate left to right
273
* evaluating the integer. For example if \a mostSignificantByteFirst is
274
* true then $00 00 00 00 00 00 00 01 == 0x0000000000000001 == 1,
275
* if false, $01 00 00 00 00 00 00 00 == 0x0100000000000000 == 1.
279
long long toLongLong(bool mostSignificantByteFirst = true) const;
282
* Creates a 4 byte ByteVector based on \a value. If
283
* \a mostSignificantByteFirst is true, then this will operate left to right
284
* in building the ByteVector. For example if \a mostSignificantByteFirst is
285
* true then $00 00 00 01 == 0x00000001 == 1, if false, $01 00 00 00 ==
290
static ByteVector fromUInt(uint value, bool mostSignificantByteFirst = true);
293
* Creates a 2 byte ByteVector based on \a value. If
294
* \a mostSignificantByteFirst is true, then this will operate left to right
295
* in building the ByteVector. For example if \a mostSignificantByteFirst is
296
* true then $00 01 == 0x0001 == 1, if false, $01 00 == 0x0100 == 1.
300
static ByteVector fromShort(short value, bool mostSignificantByteFirst = true);
303
* Creates a 8 byte ByteVector based on \a value. If
304
* \a mostSignificantByteFirst is true, then this will operate left to right
305
* in building the ByteVector. For example if \a mostSignificantByteFirst is
306
* true then $00 00 00 01 == 0x0000000000000001 == 1, if false,
307
* $01 00 00 00 00 00 00 00 == 0x0100000000000000 == 1.
311
static ByteVector fromLongLong(long long value, bool mostSignificantByteFirst = true);
314
* Returns a ByteVector based on the CString \a s.
316
static ByteVector fromCString(const char *s, uint length = 0xffffffff);
319
* Returns a const refernence to the byte at \a index.
321
const char &operator[](int index) const;
324
* Returns a reference to the byte at \a index.
326
char &operator[](int index);
329
* Returns true if this ByteVector and \a v are equal.
331
bool operator==(const ByteVector &v) const;
334
* Returns true if this ByteVector and \a v are not equal.
336
bool operator!=(const ByteVector &v) const;
339
* Returns true if this ByteVector and the null terminated C string \a s
340
* contain the same data.
342
bool operator==(const char *s) const;
345
* Returns true if this ByteVector and the null terminated C string \a s
346
* do not contain the same data.
348
bool operator!=(const char *s) const;
351
* Returns true if this ByteVector is less than \a v. The value of the
352
* vectors is determined by evaluating the character from left to right, and
353
* in the event one vector is a superset of the other, the size is used.
355
bool operator<(const ByteVector &v) const;
358
* Returns true if this ByteVector is greater than \a v.
360
bool operator>(const ByteVector &v) const;
363
* Returns a vector that is \a v appended to this vector.
365
ByteVector operator+(const ByteVector &v) const;
368
* Copies ByteVector \a v.
370
ByteVector &operator=(const ByteVector &v);
373
* Copies ByteVector \a v.
375
ByteVector &operator=(char c);
378
* Copies ByteVector \a v.
380
ByteVector &operator=(const char *data);
383
* A static, empty ByteVector which is convenient and fast (since returning
384
* an empty or "null" value does not require instantiating a new ByteVector).
386
static ByteVector null;
390
* If this ByteVector is being shared via implicit sharing, do a deep copy
391
* of the data and separate from the shared members. This should be called
392
* by all non-const subclass members.
397
class ByteVectorPrivate;
398
ByteVectorPrivate *d;
404
* \relates TagLib::ByteVector
405
* Streams the ByteVector \a v to the output stream \a s.
407
TAGLIB_EXPORT std::ostream &operator<<(std::ostream &s, const TagLib::ByteVector &v);