-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
toBase64 results differ from js-base64 #3
Comments
Android? iOS? Reproduction code? |
That would be Android. import { fromByteArray } from "react-native-quick-base64"
import { fromUint8Array } from "js-base64"
function transferEncode(value: Uint8Array) {
console.log(value)
console.log("cpp64:", fromByteArray(value))
console.log("jsb64:", fromUint8Array(value))
}
transferEncode(Uint8Array.from([184, 179, 212, 210, 19, 142, 29, 203, 166, 118, 216, 245, 113, 162, 222, 115])) |
Ok, so I have checked with the original library that is used here and wrote a gtest for this particular array: TEST(Base64Encode, encode_special_byte_array) {
const unsigned char input[] = { 184, 179, 212, 210, 19, 142, 29, 203, 166, 118, 216, 245, 113, 162, 222, 115 };
const std::string output = base64_encode(input, sizeof(input));
ASSERT_STREQ(output.c_str(), "uLPU0hOOHcumdtj1caLecw==");
} It runs fine. So I investigated the code and tried to reproduce what the code does in my gtest: This line will crash the test. One can't create a string from the given byte array:
Therefore the Android bridge code must always use the function with the signature: |
Hmm... after reinstalling this RN plugin and rebuilding the app, the base64cpp output now matches the one from base64. So, I don't know what was going on :( |
Thanks. Yeah, that's strange. |
shoot. I just had this popping up again!
When I convert the cpp64 result back to hex it has exactly 16 0-bytes in front. The Input byte array is 20 bytes long and follows those 16 "empty" bytes. So its probably related to the bridge then. How could we track this down? Race condition? |
OK, the error is probably in JSI. It does not properly take into account ArrayBuffer with However, the JSI function to get the underlaying data does not take the byteOffset into account. I have filed a bug over there. A temporary fix for now is to wrap the EDITHmmm, it seems the error might still be within this bundle. I checked again how the buffer is transferred to the c++ side. And if I change export function fromByteArray(uint8: Uint8Array): string {
- return base64FromArrayBuffer(uint8.buffer);
+ return base64FromArrayBuffer(uint8.buffer.slice(uint8.byteOffset));
} While this fixes it, I still think the error is in the jni side when handling the BufferView and ignoring its |
Ok, I have created a fix in the cpp wrapper code. LOG cpp64 : tt4oOmGUfAlbuczTPq4vZJUBPptMx3qWVFmaagDndCY= 0.19234305620193481
LOG cpp64fix: tt4oOmGUfAlbuczTPq4vZJUBPptMx3qWVFmaagDndCY= 0.09135401248931885
LOG jsb64 : tt4oOmGUfAlbuczTPq4vZJUBPptMx3qWVFmaagDndCY= 0.11026102304458618 I'll add you performance test screen to my app and run the benchmark there. A PR will follow. |
Wow, your fix improved the performance? Cool. |
Ah, so how does it affect on iOS? |
haven't tested on iOS yet. But I made some observations.
Learning question: |
Yeah, it's interesting that js-base64 is so fast. I borrowed |
perhaps it's something to do with this report? I've managed to fix it: efdb589 |
I have run the same byte arrays through js-base64 and cpp64 with those results:
The
A
padding on the last tuple is wrong..Any idea what could be wrong?
The text was updated successfully, but these errors were encountered: