Skip to content

Commit 149a3b7

Browse files
committed
⬆️
1 parent 73c863e commit 149a3b7

File tree

8 files changed

+220
-11
lines changed

8 files changed

+220
-11
lines changed

package/android/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ add_library(
6262
"${PROJECT_SOURCE_DIR}/../cpp/rnskia/dom/base/ConcatablePaint.cpp"
6363

6464
"${PROJECT_SOURCE_DIR}/../cpp/api/third_party/CSSColorParser.cpp"
65+
"${PROJECT_SOURCE_DIR}/../cpp/api/third_party/base64.cpp"
6566

6667
)
6768

package/android/cpp/rnskia-android/SkiaOpenGLSurfaceFactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ sk_sp<SkSurface> SkiaOpenGLSurfaceFactory::makeOffscreenSurface(int width,
3131
// Create texture
3232
auto texture =
3333
ThreadContextHolder::ThreadSkiaOpenGLContext.directContext
34-
->createBackendTexture(width, height, colorType, GrMipMapped::kNo,
34+
->createBackendTexture(width, height, colorType, skgpu::Mipmapped::kNo,
3535
GrRenderable::kYes);
3636

3737
struct ReleaseContext {

package/cpp/api/JsiSkAnimatedImage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#include <utility>
66

77
#include "JsiSkHostObjects.h"
8+
#include "third_party/base64.h"
89

910
#pragma clang diagnostic push
1011
#pragma clang diagnostic ignored "-Wdocumentation"
1112

1213
#include "JsiSkImage.h"
13-
#include "SkBase64.h"
1414
#include "SkStream.h"
1515
#include "include/codec/SkEncodedImageFormat.h"
1616

package/cpp/api/JsiSkDataFactory.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "JsiPromises.h"
99
#include "JsiSkData.h"
10-
#include "SkBase64.h"
10+
#include "third_party/base64.h"
1111

1212
namespace RNSkia {
1313

@@ -67,17 +67,17 @@ class JsiSkDataFactory : public JsiSkHostObject {
6767
// Calculate length
6868
size_t len;
6969
auto err =
70-
SkBase64::Decode(&base64.utf8(runtime).c_str()[0], size, nullptr, &len);
71-
if (err != SkBase64::Error::kNoError) {
70+
Base64::Decode(&base64.utf8(runtime).c_str()[0], size, nullptr, &len);
71+
if (err != Base64::Error::kNone) {
7272
throw jsi::JSError(runtime, "Error decoding base64 string");
7373
return jsi::Value::undefined();
7474
}
7575

7676
// Create data object and decode
7777
auto data = SkData::MakeUninitialized(len);
78-
err = SkBase64::Decode(&base64.utf8(runtime).c_str()[0], size,
78+
err = Base64::Decode(&base64.utf8(runtime).c_str()[0], size,
7979
data->writable_data(), &len);
80-
if (err != SkBase64::Error::kNoError) {
80+
if (err != Base64::Error::kNone) {
8181
throw jsi::JSError(runtime, "Error decoding base64 string");
8282
return jsi::Value::undefined();
8383
}

package/cpp/api/JsiSkImage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string>
55
#include <utility>
66

7+
#include "third_party/base64.h"
78
#include "JsiSkHostObjects.h"
89
#include "JsiSkImageInfo.h"
910
#include "JsiSkMatrix.h"
@@ -14,7 +15,6 @@
1415
#pragma clang diagnostic push
1516
#pragma clang diagnostic ignored "-Wdocumentation"
1617

17-
#include "SkBase64.h"
1818
#include "SkImage.h"
1919
#include "SkStream.h"
2020
#include "include/codec/SkEncodedImageFormat.h"
@@ -133,9 +133,9 @@ class JsiSkImage : public JsiSkWrappingSkPtrHostObject<SkImage> {
133133
JSI_HOST_FUNCTION(encodeToBase64) {
134134
auto data = encodeImageData(arguments, count);
135135

136-
auto len = SkBase64::Encode(data->bytes(), data->size(), nullptr);
136+
auto len = Base64::Encode(data->bytes(), data->size(), nullptr);
137137
auto buffer = std::string(len, 0);
138-
SkBase64::Encode(data->bytes(), data->size(),
138+
Base64::Encode(data->bytes(), data->size(),
139139
reinterpret_cast<void *>(&buffer[0]));
140140
return jsi::String::createFromAscii(runtime, buffer);
141141
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2006 The Android Open Source Project
3+
*
4+
* Use of this source code is governed by a BSD-style license that can be
5+
* found in the LICENSE file.
6+
*/
7+
#include "third_party/base64.h"
8+
9+
#include <cstdint>
10+
11+
#define DecodePad -2
12+
#define EncodePad 64
13+
14+
static const char kDefaultEncode[] =
15+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16+
"abcdefghijklmnopqrstuvwxyz"
17+
"0123456789+/=";
18+
19+
static const signed char kDecodeData[] = {
20+
62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1,
21+
-1, -1, DecodePad, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
22+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
23+
-1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
24+
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
25+
26+
namespace RNSkia {
27+
28+
Base64::Error Base64::Decode(const void* srcv,
29+
size_t srcLength,
30+
void* dstv,
31+
size_t* dstLength) {
32+
const unsigned char* src = static_cast<const unsigned char*>(srcv);
33+
unsigned char* dst = static_cast<unsigned char*>(dstv);
34+
35+
int i = 0;
36+
bool padTwo = false;
37+
bool padThree = false;
38+
char unsigned const* const end = src + srcLength;
39+
while (src < end) {
40+
unsigned char bytes[4] = {0, 0, 0, 0};
41+
int byte = 0;
42+
do {
43+
unsigned char srcByte = *src++;
44+
if (srcByte == 0) {
45+
*dstLength = i;
46+
return Error::kNone;
47+
}
48+
if (srcByte <= ' ') {
49+
continue; // treat as white space
50+
}
51+
if (srcByte < '+' || srcByte > 'z') {
52+
return Error::kBadChar;
53+
}
54+
signed char decoded = kDecodeData[srcByte - '+'];
55+
bytes[byte] = decoded;
56+
if (decoded != DecodePad) {
57+
if (decoded < 0) {
58+
return Error::kBadChar;
59+
}
60+
byte++;
61+
if (*src) {
62+
continue;
63+
}
64+
if (byte == 0) {
65+
*dstLength = i;
66+
return Error::kNone;
67+
}
68+
if (byte == 4) {
69+
break;
70+
}
71+
}
72+
// As an optimization, if we find an equals sign
73+
// we assume all future bytes to read are the
74+
// appropriate number of padding equals signs.
75+
if (byte < 2) {
76+
return Error::kBadPadding;
77+
}
78+
padThree = true;
79+
if (byte == 2) {
80+
padTwo = true;
81+
}
82+
break;
83+
} while (byte < 4);
84+
int two = 0;
85+
int three = 0;
86+
if (dst) {
87+
int one = (uint8_t)(bytes[0] << 2);
88+
two = bytes[1];
89+
one |= two >> 4;
90+
two = (uint8_t)((two << 4) & 0xFF);
91+
three = bytes[2];
92+
two |= three >> 2;
93+
three = (uint8_t)((three << 6) & 0xFF);
94+
three |= bytes[3];
95+
dst[i] = (unsigned char)one;
96+
}
97+
i++;
98+
if (padTwo) {
99+
break;
100+
}
101+
if (dst) {
102+
dst[i] = (unsigned char)two;
103+
}
104+
i++;
105+
if (padThree) {
106+
break;
107+
}
108+
if (dst) {
109+
dst[i] = (unsigned char)three;
110+
}
111+
i++;
112+
}
113+
*dstLength = i;
114+
return Error::kNone;
115+
}
116+
117+
size_t Base64::Encode(const void* srcv, size_t length, void* dstv) {
118+
const unsigned char* src = static_cast<const unsigned char*>(srcv);
119+
unsigned char* dst = static_cast<unsigned char*>(dstv);
120+
121+
const char* encode = kDefaultEncode;
122+
if (dst) {
123+
size_t remainder = length % 3;
124+
char unsigned const* const end = &src[length - remainder];
125+
while (src < end) {
126+
unsigned a = *src++;
127+
unsigned b = *src++;
128+
unsigned c = *src++;
129+
int d = c & 0x3F;
130+
c = (c >> 6 | b << 2) & 0x3F;
131+
b = (b >> 4 | a << 4) & 0x3F;
132+
a = a >> 2;
133+
*dst++ = encode[a];
134+
*dst++ = encode[b];
135+
*dst++ = encode[c];
136+
*dst++ = encode[d];
137+
}
138+
if (remainder > 0) {
139+
int k1 = 0;
140+
int k2 = EncodePad;
141+
int a = (uint8_t)*src++;
142+
if (remainder == 2) {
143+
int b = *src++;
144+
k1 = b >> 4;
145+
k2 = (b << 2) & 0x3F;
146+
}
147+
*dst++ = encode[a >> 2];
148+
*dst++ = encode[(k1 | a << 4) & 0x3F];
149+
*dst++ = encode[k2];
150+
*dst++ = encode[EncodePad];
151+
}
152+
}
153+
return (length + 2) / 3 * 4;
154+
}
155+
156+
} // namespace RNSkia

package/cpp/api/third_party/base64.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2006 The Android Open Source Project
3+
*
4+
* Use of this source code is governed by a BSD-style license that can be
5+
* found in the LICENSE file.
6+
*/
7+
#pragma once
8+
9+
#include <cstddef>
10+
11+
namespace RNSkia {
12+
13+
struct Base64 {
14+
public:
15+
enum class Error {
16+
kNone,
17+
kBadPadding,
18+
kBadChar,
19+
};
20+
21+
/**
22+
Base64 encodes src into dst.
23+
24+
Normally this is called once with 'dst' nullptr to get the required size,
25+
then again with an allocated 'dst' pointer to do the actual encoding.
26+
27+
@param dst nullptr or a pointer to a buffer large enough to receive the
28+
result
29+
30+
@return the required length of dst for encoding.
31+
*/
32+
static size_t Encode(const void* src, size_t length, void* dst);
33+
34+
/**
35+
Base64 decodes src into dst.
36+
37+
Normally this is called once with 'dst' nullptr to get the required size,
38+
then again with an allocated 'dst' pointer to do the actual encoding.
39+
40+
@param dst nullptr or a pointer to a buffer large enough to receive the
41+
result
42+
43+
@param dstLength assigned the length dst is required to be. Must not be
44+
nullptr.
45+
*/
46+
[[nodiscard]] static Error Decode(const void* src,
47+
size_t srcLength,
48+
void* dst,
49+
size_t* dstLength);
50+
};
51+
52+
} // namespace RNSkia

package/cpp/jsi/JsiPromises.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <jsi/jsi.h>
88

9-
#include "SkBase64.h"
9+
#include "third_party/base64.h"
1010

1111
namespace RNJsi {
1212
namespace jsi = facebook::jsi;

0 commit comments

Comments
 (0)