Add support for data properties in RPC layer
The data is base64 encoded before being sent via the RPC.
This commit is contained in:
parent
3bf84a1b6f
commit
616f78ea07
|
@ -64,6 +64,8 @@
|
|||
F67191381BCE231100AD0939 /* GCDWebServers.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 02A3C7941BC4317A00B1A7BE /* GCDWebServers.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
|
||||
F68A278C1BC2722A0063D40A /* RJSModuleLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */; };
|
||||
F68A278E1BC30F0A0063D40A /* index.js in Resources */ = {isa = PBXBuildFile; fileRef = F68A278D1BC30F0A0063D40A /* index.js */; };
|
||||
F6BB7DF11BF681BC00D0A69E /* base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6BB7DEF1BF681BC00D0A69E /* base64.cpp */; };
|
||||
F6BB7DF21BF681BC00D0A69E /* base64.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F6BB7DF01BF681BC00D0A69E /* base64.hpp */; };
|
||||
F6F405F81BCF0C1A00A1E24F /* base-test.js in Resources */ = {isa = PBXBuildFile; fileRef = F6F405F71BCF0C1A00A1E24F /* base-test.js */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
|
@ -202,7 +204,7 @@
|
|||
029445921BDEDF40006D1617 /* transact_log_handler.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = transact_log_handler.hpp; path = "src/object-store/impl/transact_log_handler.hpp"; sourceTree = "<group>"; };
|
||||
029445951BDEDF46006D1617 /* external_commit_helper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = external_commit_helper.cpp; path = "src/object-store/impl/apple/external_commit_helper.cpp"; sourceTree = "<group>"; };
|
||||
029445961BDEDF46006D1617 /* external_commit_helper.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = external_commit_helper.hpp; path = "src/object-store/impl/apple/external_commit_helper.hpp"; sourceTree = "<group>"; };
|
||||
02A3C7841BC4317A00B1A7BE /* GCDWebServer.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GCDWebServer.xcodeproj; path = vendor/GCDWebServer/GCDWebServer.xcodeproj; sourceTree = "<group>"; };
|
||||
02A3C7841BC4317A00B1A7BE /* GCDWebServer.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GCDWebServer.xcodeproj; path = GCDWebServer/GCDWebServer.xcodeproj; sourceTree = "<group>"; };
|
||||
02A3C7A41BC4341500B1A7BE /* libc++.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libc++.tbd"; path = "usr/lib/libc++.tbd"; sourceTree = SDKROOT; };
|
||||
02B29A161B7CF7C9008A7E6B /* RealmReact.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RealmReact.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
02B58CB11AE99CEC009B348C /* RealmJS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RealmJS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
|
@ -217,6 +219,9 @@
|
|||
F68A278A1BC2722A0063D40A /* RJSModuleLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RJSModuleLoader.h; path = tests/RJSModuleLoader.h; sourceTree = SOURCE_ROOT; };
|
||||
F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RJSModuleLoader.m; path = tests/RJSModuleLoader.m; sourceTree = SOURCE_ROOT; };
|
||||
F68A278D1BC30F0A0063D40A /* index.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = index.js; path = tests/index.js; sourceTree = SOURCE_ROOT; };
|
||||
F6BB7DEF1BF681BC00D0A69E /* base64.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = base64.cpp; sourceTree = "<group>"; };
|
||||
F6BB7DF01BF681BC00D0A69E /* base64.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = base64.hpp; sourceTree = "<group>"; };
|
||||
F6C3FBBC1BF680EC00E6FFD4 /* json.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = json.hpp; sourceTree = "<group>"; };
|
||||
F6F405F71BCF0C1A00A1E24F /* base-test.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = "base-test.js"; path = "tests/base-test.js"; sourceTree = SOURCE_ROOT; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
|
@ -321,6 +326,7 @@
|
|||
isa = PBXGroup;
|
||||
children = (
|
||||
02B58CCF1AE99D8C009B348C /* Frameworks */,
|
||||
F6C3FBB41BF680D000E6FFD4 /* Vendor */,
|
||||
0270BC3D1B7CFBFD00010E03 /* RealmJS */,
|
||||
02B58CC01AE99CEC009B348C /* RealmJSTests */,
|
||||
0270BCCE1B7D066100010E03 /* RealmReact */,
|
||||
|
@ -365,7 +371,6 @@
|
|||
children = (
|
||||
02A3C7A41BC4341500B1A7BE /* libc++.tbd */,
|
||||
02B58CCD1AE99D4D009B348C /* JavaScriptCore.framework */,
|
||||
02A3C7841BC4317A00B1A7BE /* GCDWebServer.xcodeproj */,
|
||||
);
|
||||
name = Frameworks;
|
||||
sourceTree = "<group>";
|
||||
|
@ -379,6 +384,18 @@
|
|||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
F6C3FBB41BF680D000E6FFD4 /* Vendor */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
02A3C7841BC4317A00B1A7BE /* GCDWebServer.xcodeproj */,
|
||||
F6BB7DF01BF681BC00D0A69E /* base64.hpp */,
|
||||
F6BB7DEF1BF681BC00D0A69E /* base64.cpp */,
|
||||
F6C3FBBC1BF680EC00E6FFD4 /* json.hpp */,
|
||||
);
|
||||
name = Vendor;
|
||||
path = vendor;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXHeadersBuildPhase section */
|
||||
|
@ -398,6 +415,7 @@
|
|||
02601F0E1BA0F3A7007C91FF /* schema.hpp in Headers */,
|
||||
0270BC6C1B7CFC1C00010E03 /* object_store.hpp in Headers */,
|
||||
0270BC681B7CFC1C00010E03 /* object_accessor.hpp in Headers */,
|
||||
F6BB7DF21BF681BC00D0A69E /* base64.hpp in Headers */,
|
||||
029445941BDEDF40006D1617 /* transact_log_handler.hpp in Headers */,
|
||||
0270BC711B7CFC1C00010E03 /* shared_realm.hpp in Headers */,
|
||||
029445981BDEDF46006D1617 /* external_commit_helper.hpp in Headers */,
|
||||
|
@ -648,6 +666,7 @@
|
|||
02C0864E1BCDB27000942F9C /* list.cpp in Sources */,
|
||||
0270BC6B1B7CFC1C00010E03 /* object_store.cpp in Sources */,
|
||||
0270BC701B7CFC1C00010E03 /* shared_realm.cpp in Sources */,
|
||||
F6BB7DF11BF681BC00D0A69E /* base64.cpp in Sources */,
|
||||
0270BC4E1B7CFC0D00010E03 /* RJSList.cpp in Sources */,
|
||||
029445971BDEDF46006D1617 /* external_commit_helper.cpp in Sources */,
|
||||
0270BC531B7CFC0D00010E03 /* RJSRealm.mm in Sources */,
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
/* Copyright 2015 Realm Inc - All Rights Reserved
|
||||
* Proprietary and Confidential
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
const CHAR_MAP = {};
|
||||
|
||||
Array.from(CHARS, (char, i) => CHAR_MAP[char] = i);
|
||||
|
||||
module.exports = {
|
||||
decode,
|
||||
encode,
|
||||
};
|
||||
|
||||
function decode(base64) {
|
||||
let length = base64.length;
|
||||
let byteCount = length * 0.75;
|
||||
|
||||
if (base64[length - 1] === '=') {
|
||||
byteCount--;
|
||||
if (base64[length - 2] === '=') {
|
||||
byteCount--;
|
||||
}
|
||||
}
|
||||
|
||||
let buffer = new ArrayBuffer(byteCount);
|
||||
let bytes = new Uint8Array(buffer);
|
||||
|
||||
for (let i = 0, j = 0; i < length; i += 4) {
|
||||
let index1 = CHAR_MAP[base64[i]];
|
||||
let index2 = CHAR_MAP[base64[i + 1]];
|
||||
let index3 = CHAR_MAP[base64[i + 2]];
|
||||
let index4 = CHAR_MAP[base64[i + 3]];
|
||||
|
||||
bytes[j++] = (index1 << 2) + ((index2 & 0x30) >> 4);
|
||||
bytes[j++] = ((index2 & 0x0f) << 4) + ((index3 & 0x3c) >> 2);
|
||||
bytes[j++] = ((index3 & 0x03) << 6) + index4;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
function encode(data) {
|
||||
let bytes = new Uint8Array(data);
|
||||
let byteCount = bytes.length;
|
||||
let base64 = '';
|
||||
|
||||
for (let i = 0; i < byteCount; i += 3) {
|
||||
base64 += CHARS[(bytes[i] & 0xfc) >> 2];
|
||||
base64 += CHARS[((bytes[i] & 0x03) << 4) + ((bytes[i + 1] & 0xf0) >> 4)];
|
||||
base64 += CHARS[((bytes[i + 1] & 0x0f) << 2) + ((bytes[i + 2] & 0xc0) >> 6)];
|
||||
base64 += CHARS[bytes[i + 2] & 0x3f];
|
||||
}
|
||||
|
||||
switch (byteCount % 3) {
|
||||
case 1:
|
||||
return base64.slice(0, -2) + '==';
|
||||
case 2:
|
||||
return base64.slice(0, -1) + '=';
|
||||
}
|
||||
|
||||
return base64;
|
||||
}
|
|
@ -14,8 +14,6 @@ const util = require('./util');
|
|||
const {keys, propTypes, objectTypes} = constants;
|
||||
const listenersKey = Symbol();
|
||||
|
||||
// TODO: DATA
|
||||
rpc.registerTypeConverter(propTypes.DATE, (_, info) => new Date(info.value));
|
||||
rpc.registerTypeConverter(propTypes.LIST, lists.create);
|
||||
rpc.registerTypeConverter(propTypes.OBJECT, objects.create);
|
||||
rpc.registerTypeConverter(objectTypes.RESULTS, results.create);
|
||||
|
|
11
lib/rpc.js
11
lib/rpc.js
|
@ -4,6 +4,7 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
const base64 = require('./base64');
|
||||
const constants = require('./constants');
|
||||
|
||||
const DEVICE_HOST = 'localhost:8082';
|
||||
|
@ -37,6 +38,9 @@ module.exports = {
|
|||
clearTestState,
|
||||
};
|
||||
|
||||
registerTypeConverter(propTypes.DATA, (_, {value}) => base64.decode(value));
|
||||
registerTypeConverter(propTypes.DATE, (_, {value}) => new Date(value));
|
||||
|
||||
function registerTypeConverter(type, handler) {
|
||||
typeConverters[type] = handler;
|
||||
}
|
||||
|
@ -120,6 +124,13 @@ function serialize(realmId, value) {
|
|||
return {value: array};
|
||||
}
|
||||
|
||||
if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
|
||||
if (value instanceof DataView) {
|
||||
throw new Error('Unable to serialize a DataView object');
|
||||
}
|
||||
return {type: propTypes.DATA, value: base64.encode(value)};
|
||||
}
|
||||
|
||||
let object = {};
|
||||
for (let key in value) {
|
||||
object[key] = serialize(realmId, value[key]);
|
||||
|
|
|
@ -14,10 +14,12 @@
|
|||
#include "RJSRealm.hpp"
|
||||
#include "RJSUtil.hpp"
|
||||
|
||||
#include "base64.hpp"
|
||||
#include "object_accessor.hpp"
|
||||
#include "shared_realm.hpp"
|
||||
#include "results.hpp"
|
||||
|
||||
using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>;
|
||||
using namespace realm_js;
|
||||
|
||||
static const char * const RealmObjectTypesFunction = "ObjectTypesFUNCTION";
|
||||
|
@ -247,6 +249,13 @@ json RPCServer::serialize_json_value(JSValueRef value) {
|
|||
}
|
||||
return {{"value", array}};
|
||||
}
|
||||
else if (RJSIsValueArrayBuffer(m_context, value)) {
|
||||
std::string data = RJSAccessor::to_binary(m_context, value);
|
||||
return {
|
||||
{"type", RJSTypeGet(realm::PropertyTypeData)},
|
||||
{"value", base64_encode((unsigned char *)data.data(), data.size())},
|
||||
};
|
||||
}
|
||||
else if (RJSIsValueDate(m_context, value)) {
|
||||
return {
|
||||
{"type", RJSTypeGet(realm::PropertyTypeDate)},
|
||||
|
@ -290,6 +299,13 @@ JSValueRef RPCServer::deserialize_json_value(const json dict)
|
|||
|
||||
return js_function;
|
||||
}
|
||||
else if (type_string == RJSTypeGet(realm::PropertyTypeData)) {
|
||||
std::string bytes;
|
||||
if (!base64_decode(value.get<std::string>(), &bytes)) {
|
||||
throw std::runtime_error("Failed to decode base64 encoded data");
|
||||
}
|
||||
return RJSAccessor::from_binary(m_context, realm::BinaryData(bytes));
|
||||
}
|
||||
else if (type_string == RJSTypeGet(realm::PropertyTypeDate)) {
|
||||
JSValueRef exception = NULL;
|
||||
JSValueRef time = JSValueMakeNumber(m_context, value.get<double>());
|
||||
|
|
|
@ -0,0 +1,197 @@
|
|||
/*
|
||||
From: http://www.adp-gmbh.ch/cpp/common/base64.html
|
||||
|
||||
base64.hpp and base64.cpp
|
||||
|
||||
Copyright (C) 2004-2008 René Nyffenegger
|
||||
|
||||
This source code is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the author be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this source code must not be misrepresented; you must not
|
||||
claim that you wrote the original source code. If you use this source code
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original source code.
|
||||
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
|
||||
|
||||
This file was modified by jmarantz@google.com to:
|
||||
- Add a static map and an initializer function
|
||||
- add a web-safe variant that uses '-' and '_' in lieu of '+' and '/'
|
||||
- change the 'decode' interface to return false if the input contains
|
||||
characters not in the expected char-set.
|
||||
- removed is_base64 helper function and instead rely on the char_maps.
|
||||
- use a static constant for the padding character '='
|
||||
*/
|
||||
|
||||
#include "base64.hpp"
|
||||
|
||||
static const char base64_chars[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
static const char web64_chars[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789-_";
|
||||
|
||||
static unsigned int base64_char_map[256], web64_char_map[256];
|
||||
static bool initialized = false;
|
||||
static const unsigned int kInvalidChar = 0xffffffff;
|
||||
static const char kPadChar = '=';
|
||||
|
||||
// Be sure to call this function before calling any other functions
|
||||
// in this file when using multiple threads.
|
||||
void base64_init() {
|
||||
if (!initialized) {
|
||||
initialized = true;
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
base64_char_map[i] = kInvalidChar;
|
||||
web64_char_map[i] = kInvalidChar;
|
||||
}
|
||||
for (int i = 0; i < sizeof(base64_chars) - 1; ++i) {
|
||||
base64_char_map[base64_chars[i]] = i;
|
||||
}
|
||||
for (int i = 0; i < sizeof(web64_chars) - 1; ++i) {
|
||||
web64_char_map[web64_chars[i]] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline std::string encode(
|
||||
const char* char_set, unsigned char const* bytes_to_encode,
|
||||
size_t in_len) {
|
||||
base64_init();
|
||||
std::string ret;
|
||||
size_t i = 0;
|
||||
size_t j = 0;
|
||||
unsigned char char_array_3[3];
|
||||
unsigned char char_array_4[4];
|
||||
|
||||
while (in_len--) {
|
||||
char_array_3[i++] = *(bytes_to_encode++);
|
||||
if (i == 3) {
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
|
||||
((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
|
||||
((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for (i = 0; i < 4 ; i++)
|
||||
ret += char_set[char_array_4[i]];
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (j = i; j < 3; j++) {
|
||||
char_array_3[j] = '\0';
|
||||
}
|
||||
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
|
||||
((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
|
||||
((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for (j = 0; (j < i + 1); j++)
|
||||
ret += char_set[char_array_4[j]];
|
||||
|
||||
while ((i++ < 3)) {
|
||||
ret += kPadChar;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline bool decode(
|
||||
unsigned int* char_map, const std::string& encoded_string,
|
||||
std::string* output) {
|
||||
base64_init();
|
||||
size_t in_len = encoded_string.size();
|
||||
size_t i = 0;
|
||||
size_t j = 0;
|
||||
size_t in_ = 0;
|
||||
unsigned char char_array_4[4], char_array_3[3];
|
||||
|
||||
while (in_len-- && (encoded_string[in_] != kPadChar)) {
|
||||
char_array_4[i++] = encoded_string[in_];
|
||||
in_++;
|
||||
if (i == 4) {
|
||||
for (i = 0; i < 4; i++) {
|
||||
unsigned int char_index = char_map[char_array_4[i]];
|
||||
if (char_index == kInvalidChar) {
|
||||
return false;
|
||||
}
|
||||
char_array_4[i] = char_index;
|
||||
}
|
||||
|
||||
char_array_3[0] = (char_array_4[0] << 2) +
|
||||
((char_array_4[1] & 0x30) >> 4);
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) +
|
||||
((char_array_4[2] & 0x3c) >> 2);
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
for (i = 0; (i < 3); i++)
|
||||
*output += char_array_3[i];
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (j = i; j < 4; j++)
|
||||
char_array_4[j] = 0;
|
||||
|
||||
for (j = 0; j < i; j++) {
|
||||
unsigned int char_index = char_map[char_array_4[j]];
|
||||
if (char_index == kInvalidChar) {
|
||||
return false;
|
||||
}
|
||||
char_array_4[j] = char_index;
|
||||
}
|
||||
|
||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) +
|
||||
((char_array_4[2] & 0x3c) >> 2);
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
for (j = 0; (j < i - 1); j++) {
|
||||
*output += char_array_3[j];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
std::string base64_encode(unsigned char const* bytes_to_encode,
|
||||
size_t in_len) {
|
||||
return encode(base64_chars, bytes_to_encode, in_len);
|
||||
}
|
||||
|
||||
std::string web64_encode(unsigned char const* bytes_to_encode,
|
||||
size_t in_len) {
|
||||
return encode(web64_chars, bytes_to_encode, in_len);
|
||||
}
|
||||
|
||||
bool base64_decode(const std::string& encoded_string, std::string* output) {
|
||||
return decode(base64_char_map, encoded_string, output);
|
||||
}
|
||||
|
||||
bool web64_decode(const std::string& encoded_string, std::string* output) {
|
||||
return decode(web64_char_map, encoded_string, output);
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
From: http://www.adp-gmbh.ch/cpp/common/base64.html
|
||||
|
||||
base64.hpp and base64.cpp
|
||||
|
||||
Copyright (C) 2004-2008 René Nyffenegger
|
||||
|
||||
This source code is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the author be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this source code must not be misrepresented; you must not
|
||||
claim that you wrote the original source code. If you use this source code
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original source code.
|
||||
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
|
||||
|
||||
This file was modified by jmarantz@google.com to:
|
||||
- Add a static map and an initializer function
|
||||
- add a web-safe variant that uses '-' and '_' in lieu of '+' and '/'
|
||||
- change the 'decode' interface to return false if the input contains
|
||||
characters not in the expected char-set.
|
||||
- removed is_base64 helper function and instead rely on the char_maps.
|
||||
- use a static constant for the padding character '='
|
||||
*/
|
||||
|
||||
#ifndef BASE64_HPP
|
||||
#define BASE64_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
// Encode/deocde strings in common base64 encoding
|
||||
std::string base64_encode(const unsigned char* data, size_t len);
|
||||
bool base64_decode(const std::string& s, std::string* output);
|
||||
|
||||
#endif // BASE64_HPP
|
Loading…
Reference in New Issue