1/*
2 * Copyright (C) 2007 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#include <dcf/DrmDcfCommon.h>
18
19int64_t ntoh_int64(int64_t x)
20{
21    return (((int64_t)(ntohl((int32_t)((x << 32) >> 32))) << 32) | (uint32_t)ntohl(((int32_t)(x >> 32))));
22}
23
24/**
25 * Class: Box
26 */
27Box::Box(const uint8_t* box):mLargeSize(0),mUserType(NULL)
28{
29    if(!box)
30    {
31        return ;
32    }
33
34    const uint8_t* p = box;
35
36    /* Get the size value */
37    mSize = ntohl(*(uint32_t *)p);
38    p += sizeof(mSize);
39
40    /* Get the type value */
41    mType = *((uint32_t *)p);
42    p += sizeof(mType);
43
44    if (1 == mSize)
45    {
46        mLargeSize = ntoh_int64(*(uint64_t *)p);
47        p += sizeof(mLargeSize);
48    }
49
50    if (DCF_USER_TYPE == mType)
51    {
52        mUserType = new uint8_t[USER_TYPE_LEN];
53        memcpy(mUserType, p, USER_TYPE_LEN);
54        p += USER_TYPE_LEN;
55    }
56
57    mBoxLength = p - box;
58}
59
60Box::Box(const Box& other)
61{
62    mSize = other.mSize;
63    mType = other.mType;
64    mLargeSize = other.mLargeSize;
65    mUserType = NULL;
66
67    if(other.mUserType)
68    {
69        mUserType = new uint8_t[USER_TYPE_LEN];
70        memcpy(mUserType,other.mUserType,USER_TYPE_LEN);
71    }
72}
73
74Box& Box::operator=(const Box& other)
75{
76    if(this == &other)
77    {
78        return *this;
79    }
80
81    if(mUserType)
82    {
83        delete []mUserType;
84        mUserType = NULL;
85    }
86
87    if(other.mUserType)
88    {
89        mUserType = new uint8_t[USER_TYPE_LEN];
90        memcpy(mUserType, other.mUserType, USER_TYPE_LEN);
91    }
92
93    return *this;
94}
95
96Box::~Box()
97{
98    if(mUserType)
99    {
100        delete []mUserType;
101        mUserType = NULL;
102    }
103}
104
105uint64_t Box::getSize(void) const
106{
107    if(1 == mSize)
108    {
109        return mLargeSize;
110    }
111
112    return mSize;
113}
114
115uint32_t Box::getType(void) const
116{
117    return mType;
118}
119
120const uint8_t* Box::getUsertype(void) const
121{
122    return mUserType;
123}
124
125uint32_t Box::getLen(void) const
126{
127    return mBoxLength;
128}
129
130
131/**
132 * Class: FullBox
133 */
134FullBox::FullBox(const uint8_t* fullBox) : Box(fullBox)
135{
136    if(!fullBox)
137    {
138        return ;
139    }
140
141    const uint8_t* p = fullBox;
142
143    p += Box::getLen();
144
145    mVersion = *p;
146    p++;
147
148    memcpy(mFlag, p,FLAG_LEN);
149    p += FLAG_LEN;
150
151    mFullBoxLength = p - fullBox;
152}
153
154uint8_t FullBox::getVersion(void) const
155{
156    return mVersion;
157}
158
159const uint8_t* FullBox::getFlag(void) const
160{
161    return mFlag;
162}
163
164uint32_t FullBox::getLen(void) const
165{
166    return mFullBoxLength;
167}
168
169///// class TextualHeader implementation
170TextualHeader::TextualHeader(const string& inData)
171{
172    string::size_type loc1 = inData.find(":", 0);
173
174    if (loc1 != string::npos)
175    {
176        name.assign(inData, 0, loc1);
177    }
178
179    string::size_type loc2 = inData.find(";", loc1 + 1);
180
181    if (loc2 != string::npos)
182    {
183        value.assign(inData, loc1 + 1, loc2 - loc1 - 1);
184        param.assign(inData, loc2 + 1, inData.length() - loc2 - 1);
185    }
186    else
187    {
188        value.assign(inData, loc1 + 1, inData.length() - loc1 - 1);
189    }
190}
191
192string TextualHeader::getName() const
193{
194    return name;
195}
196
197string TextualHeader::getValue() const
198{
199    return value;
200}
201
202string TextualHeader::getParam() const
203{
204    return param;
205}
206
207