blob: 216f3d09b534577092766d5a7467e85e202961c0 [file] [log] [blame]
Andreas Huber4bbfff22014-02-10 14:40:45 -08001/*
2 * Copyright (C) 2014 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 */
Marco Nelissen42057ce2019-09-23 12:15:57 -070016#include <datasource/DataURISource.h>
Andreas Huber4bbfff22014-02-10 14:40:45 -080017
18#include <media/stagefright/foundation/ABuffer.h>
19#include <media/stagefright/foundation/AString.h>
20#include <media/stagefright/foundation/base64.h>
21
22namespace android {
23
24// static
25sp<DataURISource> DataURISource::Create(const char *uri) {
26 if (strncasecmp("data:", uri, 5)) {
27 return NULL;
28 }
29
Dan Austin8abaa352016-03-24 12:17:36 -070030 const char *commaPos = strrchr(uri, ',');
Andreas Huber4bbfff22014-02-10 14:40:45 -080031
32 if (commaPos == NULL) {
33 return NULL;
34 }
35
36 sp<ABuffer> buffer;
37
38 AString tmp(&uri[5], commaPos - &uri[5]);
39
40 if (tmp.endsWith(";base64")) {
Andreas Huber4bbfff22014-02-10 14:40:45 -080041
Ray Essickdb0354e2019-03-27 09:49:26 -070042 // strip all CR and LF characters.
43 const char *src = commaPos+1;
44 int len = strlen(src) + 1;
45 char *cleansed = (char *) malloc(len);
46 if (cleansed == NULL) return NULL;
47 char *keeping = cleansed;
48 int left = len;
49 for (int i = 0; i < len ; i++)
50 {
51 const char c = *src++;
52 if (c == '\r' || c == '\n') {
53 continue;
Andreas Huber4bbfff22014-02-10 14:40:45 -080054 }
Ray Essickdb0354e2019-03-27 09:49:26 -070055 *keeping++ = c;
56 left--;
Andreas Huber4bbfff22014-02-10 14:40:45 -080057 }
Ray Essickdb0354e2019-03-27 09:49:26 -070058 memset(keeping, 0, left);
59
60 AString encoded(cleansed);
61 free(cleansed);
Andreas Huber4bbfff22014-02-10 14:40:45 -080062
63 buffer = decodeBase64(encoded);
64
65 if (buffer == NULL) {
66 ALOGE("Malformed base64 encoded content found.");
67 return NULL;
68 }
69 } else {
70#if 0
71 size_t dataLen = strlen(uri) - tmp.size() - 6;
72 buffer = new ABuffer(dataLen);
73 memcpy(buffer->data(), commaPos + 1, dataLen);
74
75 // unescape
76#else
77 // MediaPlayer doesn't care for this right now as we don't
78 // play any text-based media.
79 return NULL;
80#endif
81 }
82
83 // We don't really care about charset or mime type.
84
85 return new DataURISource(buffer);
86}
87
88DataURISource::DataURISource(const sp<ABuffer> &buffer)
89 : mBuffer(buffer) {
90}
91
92DataURISource::~DataURISource() {
93}
94
95status_t DataURISource::initCheck() const {
96 return OK;
97}
98
99ssize_t DataURISource::readAt(off64_t offset, void *data, size_t size) {
Mark Salyzyna5750e02014-06-18 16:34:45 -0700100 if ((offset < 0) || (offset >= (off64_t)mBuffer->size())) {
Andreas Huber4bbfff22014-02-10 14:40:45 -0800101 return 0;
102 }
103
104 size_t copy = mBuffer->size() - offset;
105 if (copy > size) {
106 copy = size;
107 }
108
109 memcpy(data, mBuffer->data() + offset, copy);
110
111 return copy;
112}
113
114status_t DataURISource::getSize(off64_t *size) {
115 *size = mBuffer->size();
116
117 return OK;
118}
119
120} // namespace android
121