1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ID3_H_
18 
19 #define ID3_H_
20 
21 #include <utils/RefBase.h>
22 
23 namespace android {
24 
25 class DataSource;
26 class String8;
27 
28 struct ID3 {
29     enum Version {
30         ID3_UNKNOWN,
31         ID3_V1,
32         ID3_V1_1,
33         ID3_V2_2,
34         ID3_V2_3,
35         ID3_V2_4,
36     };
37 
38     ID3(const sp<DataSource> &source, bool ignoreV1 = false, off64_t offset = 0);
39     ID3(const uint8_t *data, size_t size, bool ignoreV1 = false);
40     ~ID3();
41 
42     bool isValid() const;
43 
44     Version version() const;
45 
46     const void *getAlbumArt(size_t *length, String8 *mime) const;
47 
48     struct Iterator {
49         Iterator(const ID3 &parent, const char *id);
50         ~Iterator();
51 
52         bool done() const;
53         void getID(String8 *id) const;
54         void getString(String8 *s, String8 *ss = NULL) const;
55         const uint8_t *getData(size_t *length) const;
56         void next();
57 
58     private:
59         const ID3 &mParent;
60         char *mID;
61         size_t mOffset;
62 
63         const uint8_t *mFrameData;
64         size_t mFrameSize;
65 
66         void findFrame();
67 
68         size_t getHeaderLength() const;
69         void getstring(String8 *s, bool secondhalf) const;
70 
71         Iterator(const Iterator &);
72         Iterator &operator=(const Iterator &);
73     };
74 
rawSizeID375     size_t rawSize() const { return mRawSize; }
76 
77 private:
78     bool mIsValid;
79     uint8_t *mData;
80     size_t mSize;
81     size_t mFirstFrameOffset;
82     Version mVersion;
83 
84     // size of the ID3 tag including header before any unsynchronization.
85     // only valid for IDV2+
86     size_t mRawSize;
87 
88     bool parseV1(const sp<DataSource> &source);
89     bool parseV2(const sp<DataSource> &source, off64_t offset);
90     void removeUnsynchronization();
91     bool removeUnsynchronizationV2_4(bool iTunesHack);
92 
93     static bool ParseSyncsafeInteger(const uint8_t encoded[4], size_t *x);
94 
95     ID3(const ID3 &);
96     ID3 &operator=(const ID3 &);
97 };
98 
99 }  // namespace android
100 
101 #endif  // ID3_H_
102 
103