MetaData.cpp revision fca812565cebd229fba8be32bad14983c5252a7b
120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber/*
220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Copyright (C) 2009 The Android Open Source Project
320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * you may not use this file except in compliance with the License.
620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * You may obtain a copy of the License at
720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
1020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Unless required by applicable law or agreed to in writing, software
1120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
1220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * See the License for the specific language governing permissions and
1420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * limitations under the License.
1520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber */
1620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
17feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen//#define LOG_NDEBUG 0
18feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen#define LOG_TAG "MetaData"
19feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen#include <utils/Log.h>
20feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen
2120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <stdlib.h>
2220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <string.h>
2320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
24f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong#include <media/stagefright/foundation/ADebug.h>
2556997121c5031598fbbba7b7c53980b7fd529c2dMarco Nelissen#include <media/stagefright/foundation/AString.h>
2656997121c5031598fbbba7b7c53980b7fd529c2dMarco Nelissen#include <media/stagefright/foundation/hexdump.h>
2720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/MetaData.h>
2820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
2920111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubernamespace android {
3020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
3120111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMetaData::MetaData() {
3220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
3320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
3420111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMetaData::MetaData(const MetaData &from)
3520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    : RefBase(),
3620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber      mItems(from.mItems) {
3720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
3820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
3920111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMetaData::~MetaData() {
4020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    clear();
4120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
4220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
4320111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MetaData::clear() {
4420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    mItems.clear();
4520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
4620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
4720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberbool MetaData::remove(uint32_t key) {
4820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    ssize_t i = mItems.indexOfKey(key);
4920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
5020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (i < 0) {
5120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        return false;
5220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
5320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
5420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    mItems.removeItemsAt(i);
5520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
5620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return true;
5720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
5820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
5920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberbool MetaData::setCString(uint32_t key, const char *value) {
6020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return setData(key, TYPE_C_STRING, value, strlen(value) + 1);
6120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
6220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
6320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberbool MetaData::setInt32(uint32_t key, int32_t value) {
6420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return setData(key, TYPE_INT32, &value, sizeof(value));
6520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
6620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
6748c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huberbool MetaData::setInt64(uint32_t key, int64_t value) {
6848c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    return setData(key, TYPE_INT64, &value, sizeof(value));
6948c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber}
7048c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber
7120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberbool MetaData::setFloat(uint32_t key, float value) {
7220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return setData(key, TYPE_FLOAT, &value, sizeof(value));
7320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
7420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
7520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberbool MetaData::setPointer(uint32_t key, void *value) {
7620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return setData(key, TYPE_POINTER, &value, sizeof(value));
7720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
7820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
79f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huberbool MetaData::setRect(
80f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber        uint32_t key,
81f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber        int32_t left, int32_t top,
82f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber        int32_t right, int32_t bottom) {
83f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    Rect r;
84f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    r.mLeft = left;
85f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    r.mTop = top;
86f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    r.mRight = right;
87f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    r.mBottom = bottom;
88f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber
89f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    return setData(key, TYPE_RECT, &r, sizeof(r));
90f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber}
91f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber
922cf9c5073ca3342ee52673ad68763fadd2c2be79James Dongbool MetaData::findCString(uint32_t key, const char **value) {
9320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    uint32_t type;
9420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    const void *data;
9520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    size_t size;
9620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (!findData(key, &type, &data, &size) || type != TYPE_C_STRING) {
9720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        return false;
9820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
9920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
10020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *value = (const char *)data;
10120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
10220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return true;
10320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
10420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1052cf9c5073ca3342ee52673ad68763fadd2c2be79James Dongbool MetaData::findInt32(uint32_t key, int32_t *value) {
10620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    uint32_t type;
10720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    const void *data;
10820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    size_t size;
10920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (!findData(key, &type, &data, &size) || type != TYPE_INT32) {
11020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        return false;
11120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
11220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1130c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber    CHECK_EQ(size, sizeof(*value));
11420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
11520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *value = *(int32_t *)data;
11620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
11720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return true;
11820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
11920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1202cf9c5073ca3342ee52673ad68763fadd2c2be79James Dongbool MetaData::findInt64(uint32_t key, int64_t *value) {
12148c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    uint32_t type;
12248c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    const void *data;
12348c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    size_t size;
12448c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    if (!findData(key, &type, &data, &size) || type != TYPE_INT64) {
12548c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber        return false;
12648c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    }
12748c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber
12848c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    CHECK_EQ(size, sizeof(*value));
12948c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber
13048c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    *value = *(int64_t *)data;
13148c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber
13248c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber    return true;
13348c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber}
13448c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber
1352cf9c5073ca3342ee52673ad68763fadd2c2be79James Dongbool MetaData::findFloat(uint32_t key, float *value) {
13620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    uint32_t type;
13720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    const void *data;
13820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    size_t size;
13920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (!findData(key, &type, &data, &size) || type != TYPE_FLOAT) {
14020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        return false;
14120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
14220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1430c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber    CHECK_EQ(size, sizeof(*value));
14420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
14520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *value = *(float *)data;
14620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
14720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return true;
14820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
14920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1502cf9c5073ca3342ee52673ad68763fadd2c2be79James Dongbool MetaData::findPointer(uint32_t key, void **value) {
15120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    uint32_t type;
15220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    const void *data;
15320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    size_t size;
15420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (!findData(key, &type, &data, &size) || type != TYPE_POINTER) {
15520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        return false;
15620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
15720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1580c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber    CHECK_EQ(size, sizeof(*value));
15920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
16020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *value = *(void **)data;
16120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
16220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return true;
16320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
16420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
165f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huberbool MetaData::findRect(
166f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber        uint32_t key,
167f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber        int32_t *left, int32_t *top,
1682cf9c5073ca3342ee52673ad68763fadd2c2be79James Dong        int32_t *right, int32_t *bottom) {
169f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    uint32_t type;
170f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    const void *data;
171f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    size_t size;
172f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    if (!findData(key, &type, &data, &size) || type != TYPE_RECT) {
173f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber        return false;
174f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    }
175f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber
176f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    CHECK_EQ(size, sizeof(Rect));
177f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber
178f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    const Rect *r = (const Rect *)data;
179f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    *left = r->mLeft;
180f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    *top = r->mTop;
181f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    *right = r->mRight;
182f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    *bottom = r->mBottom;
183f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber
184f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber    return true;
185f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber}
186f5ab57c2d5e02af7483c94eddb177e4f5c9e9892Andreas Huber
18720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberbool MetaData::setData(
18820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        uint32_t key, uint32_t type, const void *data, size_t size) {
18920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    bool overwrote_existing = true;
19020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
19120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    ssize_t i = mItems.indexOfKey(key);
19220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (i < 0) {
19320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        typed_data item;
19420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        i = mItems.add(key, item);
19520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
19620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        overwrote_existing = false;
19720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
19820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
19920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    typed_data &item = mItems.editValueAt(i);
20020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
20120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    item.setData(type, data, size);
20220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
20320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return overwrote_existing;
20420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
20520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
20620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberbool MetaData::findData(uint32_t key, uint32_t *type,
20720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber                        const void **data, size_t *size) const {
20820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    ssize_t i = mItems.indexOfKey(key);
20920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
21020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (i < 0) {
21120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        return false;
21220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
21320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
21420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    const typed_data &item = mItems.valueAt(i);
21520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
21620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    item.getData(type, data, size);
21720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
21820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return true;
21920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
22020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
22120111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMetaData::typed_data::typed_data()
22220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    : mType(0),
22320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber      mSize(0) {
22420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
22520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
22620111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMetaData::typed_data::~typed_data() {
22720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    clear();
22820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
22920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
23020111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMetaData::typed_data::typed_data(const typed_data &from)
23120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    : mType(from.mType),
23220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber      mSize(0) {
23320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    allocateStorage(from.mSize);
23420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    memcpy(storage(), from.storage(), mSize);
23520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
23620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
23720111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMetaData::typed_data &MetaData::typed_data::operator=(
23820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        const MetaData::typed_data &from) {
23920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (this != &from) {
24020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        clear();
24120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        mType = from.mType;
24220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        allocateStorage(from.mSize);
24320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        memcpy(storage(), from.storage(), mSize);
24420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
24520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
24620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return *this;
24720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
24820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
24920111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MetaData::typed_data::clear() {
25020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    freeStorage();
25120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
25220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    mType = 0;
25320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
25420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
25520111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MetaData::typed_data::setData(
25620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        uint32_t type, const void *data, size_t size) {
25720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    clear();
25820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
25920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    mType = type;
26020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    allocateStorage(size);
26120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    memcpy(storage(), data, size);
26220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
26320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
26420111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MetaData::typed_data::getData(
26520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        uint32_t *type, const void **data, size_t *size) const {
26620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *type = mType;
26720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *size = mSize;
26820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *data = storage();
26920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
27020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
27120111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MetaData::typed_data::allocateStorage(size_t size) {
27220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    mSize = size;
27320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
27420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (usesReservoir()) {
27520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        return;
27620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
27720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
27820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    u.ext_data = malloc(mSize);
27920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
28020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
28120111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MetaData::typed_data::freeStorage() {
28220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    if (!usesReservoir()) {
28320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        if (u.ext_data) {
28420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber            free(u.ext_data);
285fca812565cebd229fba8be32bad14983c5252a7bChrister Fletcher            u.ext_data = NULL;
28620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        }
28720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
28820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
28920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    mSize = 0;
29020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
29120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
292feba11fd788c99b0b63e0bce724730817688d82fMarco NelissenString8 MetaData::typed_data::asString() const {
293feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    String8 out;
294feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    const void *data = storage();
295feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    switch(mType) {
296feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        case TYPE_NONE:
297feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            out = String8::format("no type, size %d)", mSize);
298feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            break;
299feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        case TYPE_C_STRING:
300feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            out = String8::format("(char*) %s", (const char *)data);
301feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            break;
302feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        case TYPE_INT32:
303feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            out = String8::format("(int32_t) %d", *(int32_t *)data);
304feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            break;
305feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        case TYPE_INT64:
306feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            out = String8::format("(int64_t) %lld", *(int64_t *)data);
307feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            break;
308feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        case TYPE_FLOAT:
309feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            out = String8::format("(float) %f", *(float *)data);
310feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            break;
311feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        case TYPE_POINTER:
312feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            out = String8::format("(void*) %p", *(void **)data);
313feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            break;
314feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        case TYPE_RECT:
315feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        {
316feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            const Rect *r = (const Rect *)data;
317feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            out = String8::format("Rect(%d, %d, %d, %d)",
318feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen                                  r->mLeft, r->mTop, r->mRight, r->mBottom);
319feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            break;
320feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        }
321feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen
322feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        default:
323feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            out = String8::format("(unknown type %d, size %d)", mType, mSize);
32456997121c5031598fbbba7b7c53980b7fd529c2dMarco Nelissen            if (mSize <= 48) { // if it's less than three lines of hex data, dump it
32556997121c5031598fbbba7b7c53980b7fd529c2dMarco Nelissen                AString foo;
32656997121c5031598fbbba7b7c53980b7fd529c2dMarco Nelissen                hexdump(data, mSize, 0, &foo);
32756997121c5031598fbbba7b7c53980b7fd529c2dMarco Nelissen                out.append("\n");
32856997121c5031598fbbba7b7c53980b7fd529c2dMarco Nelissen                out.append(foo.c_str());
32956997121c5031598fbbba7b7c53980b7fd529c2dMarco Nelissen            }
330feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen            break;
331feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    }
332feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    return out;
333feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen}
334feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen
335feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissenstatic void MakeFourCCString(uint32_t x, char *s) {
336feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    s[0] = x >> 24;
337feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    s[1] = (x >> 16) & 0xff;
338feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    s[2] = (x >> 8) & 0xff;
339feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    s[3] = x & 0xff;
340feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    s[4] = '\0';
341feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen}
342feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen
343feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissenvoid MetaData::dumpToLog() const {
344feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    for (int i = mItems.size(); --i >= 0;) {
345feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        int32_t key = mItems.keyAt(i);
346feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        char cc[5];
347feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        MakeFourCCString(key, cc);
348feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        const typed_data &item = mItems.valueAt(i);
349feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen        ALOGI("%s: %s", cc, item.asString().string());
350feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen    }
351feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen}
352feba11fd788c99b0b63e0bce724730817688d82fMarco Nelissen
35320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}  // namespace android
35420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
355