Skip to content

Commit 77aa956

Browse files
authored
Merge pull request sandstorm-io#2941 from sandstorm-io/kj-encoding
Delete code: Use new KJ encoding library.
2 parents 2c2006f + 2a639c0 commit 77aa956

File tree

9 files changed

+22
-417
lines changed

9 files changed

+22
-417
lines changed

src/sandstorm/app-index/indexer.c++

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <sandstorm/id-to-text.h>
2121
#include <sandstorm/appid-replacements.h>
2222
#include <capnp/serialize.h>
23+
#include <kj/encoding.h>
2324
#include <stdlib.h>
2425
#include <map>
2526
#include <sodium/crypto_generichash_blake2b.h>
@@ -198,7 +199,7 @@ class DataHandler: public capnp::JsonCodec::Handler<capnp::Data> {
198199
public:
199200
void encode(const capnp::JsonCodec& codec, capnp::Data::Reader input,
200201
capnp::JsonValue::Builder output) const override {
201-
output.setString(base64Encode(input, false));
202+
output.setString(kj::encodeBase64(input, false));
202203
}
203204

204205
capnp::Orphan<capnp::Data> decode(
@@ -453,7 +454,7 @@ kj::String Indexer::writeImage(kj::ArrayPtr<const byte> data, kj::StringPtr exte
453454
crypto_generichash_blake2b(hash, sizeof(hash), data.begin(), data.size(), nullptr, 0);
454455

455456
// Write if not already present.
456-
auto basename = kj::str(hexEncode(hash), extension);
457+
auto basename = kj::str(kj::encodeHex(hash), extension);
457458
auto filename = kj::str("/var/www/images/", basename);
458459

459460
if (access(filename.cStr(), F_OK) < 0) {

src/sandstorm/bridge-proxy.c++

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <sandstorm/bridge-proxy.capnp.h>
2121
#include <capnp/compat/json.h>
2222
#include <kj/debug.h>
23+
#include <kj/encoding.h>
2324
#include "util.h"
2425

2526
namespace sandstorm {
@@ -127,7 +128,7 @@ public:
127128
(capnp::Response<SandstormApi<BridgeObjectId>::SaveResults>&& claim) {
128129
capnp::MallocMessageBuilder builder(64);
129130
auto root = builder.initRoot<ProxyClaimRequestResponse>();
130-
root.setCap(base64Encode(claim.getToken(), false));
131+
root.setCap(kj::encodeBase64(claim.getToken(), false));
131132

132133
capnp::JsonCodec json;
133134
kj::String text = json.encode(root);
@@ -221,7 +222,7 @@ private:
221222
auto cap = capnp::Capability::Client(
222223
kj::heap<CapRedirector>([sandstormApi=sandstormApi,token=kj::str(token)]() mutable {
223224
auto req = sandstormApi.restoreRequest();
224-
req.setToken(base64Decode(token));
225+
req.setToken(kj::decodeBase64(token));
225226
return req.send().getCap();
226227
})).castAs<ApiSession>();
227228

src/sandstorm/id-to-text.c++

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "id-to-text.h"
1818
#include "util.h"
19+
#include <kj/encoding.h>
1920

2021
namespace sandstorm {
2122

@@ -188,7 +189,7 @@ kj::String packageIdString(spk::PackageId::Reader packageId) {
188189

189190
kj::String packageIdString(kj::ArrayPtr<const kj::byte> packageId) {
190191
KJ_ASSERT(packageId.size() == PACKAGE_ID_BYTE_SIZE);
191-
return hexEncode(packageId);
192+
return kj::encodeHex(packageId);
192193
}
193194

194195
bool tryParsePackageId(kj::StringPtr in, spk::PackageId::Builder out) {

src/sandstorm/run-bundle.c++

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <kj/io.h>
2020
#include <kj/parse/common.h>
2121
#include <kj/parse/char.h>
22+
#include <kj/encoding.h>
2223
#include <capnp/schema.h>
2324
#include <capnp/dynamic.h>
2425
#include <capnp/serialize.h>
@@ -954,7 +955,7 @@ public:
954955
// Get 20 random bytes for token.
955956
kj::byte bytes[20];
956957
randombytes_buf(bytes, sizeof(bytes));
957-
auto hexString = hexEncode(bytes);
958+
auto hexString = kj::encodeHex(bytes);
958959

959960
auto config = readConfig();
960961

@@ -2883,7 +2884,7 @@ private:
28832884
call.setFunction("BinData");
28842885
auto params = call.initParams(2);
28852886
params[0].setNumber(0);
2886-
params[1].setString(base64Encode(input, false));
2887+
params[1].setString(kj::encodeBase64(input, false));
28872888
}
28882889

28892890
capnp::Orphan<capnp::Data> decode(

src/sandstorm/sandstorm-http-bridge.c++

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <kj/async-io.h>
2626
#include <kj/async-unix.h>
2727
#include <kj/io.h>
28+
#include <kj/encoding.h>
2829
#include <capnp/rpc-twoparty.h>
2930
#include <capnp/rpc.capnp.h>
3031
#include <capnp/schema.h>
@@ -70,7 +71,7 @@ kj::Array<byte> toBytes(kj::StringPtr text, kj::ArrayPtr<const byte> data = null
7071
kj::String textIdentityId(capnp::Data::Reader id) {
7172
// We truncate to 128 bits to be a little more wieldy. Still 32 chars, though.
7273
KJ_ASSERT(id.size() == 32, "Identity ID not a SHA-256?");
73-
return hexEncode(id.slice(0, kj::min(id.size(), 16)));
74+
return kj::encodeHex(id.slice(0, kj::min(id.size(), 16)));
7475
}
7576

7677
struct HttpStatusInfo {
@@ -1227,7 +1228,7 @@ private:
12271228
buf[n] = '\0';
12281229

12291230
auto req = apiCap.restoreRequest();
1230-
req.setToken(percentDecode(buf));
1231+
req.setToken(kj::decodeBinaryUriComponent(buf));
12311232

12321233
return req.send().getCap().castAs<Identity>();
12331234
}
@@ -1241,7 +1242,7 @@ private:
12411242
req.initLabel().setDefaultText("user identity");
12421243
tasks.add(req.send().then([this,textId](auto result) -> void {
12431244
// Sandstorm tokens are primarily text but use percent-encoding to be safe.
1244-
auto tokenText = percentEncode(result.getToken());
1245+
auto tokenText = kj::encodeUriComponent(result.getToken());
12451246

12461247
// Clean up any existing symlink.
12471248
dropIdentity(textId);
@@ -1271,7 +1272,7 @@ private:
12711272
KJ_SYSCALL(renameat(identitiesDir, symlink.cStr(), trashDir, trashSymlink.cStr()));
12721273

12731274
auto req = apiCap.dropRequest();
1274-
req.setToken(percentDecode(buf));
1275+
req.setToken(kj::decodeBinaryUriComponent(buf));
12751276
tasks.add(req.send().then([KJ_MVCAP(trashSymlink), this](auto response) -> void {
12761277
KJ_SYSCALL(unlinkat(trashDir, trashSymlink.cStr(), 0));
12771278
}));
@@ -1297,7 +1298,7 @@ public:
12971298
bridgeContext(bridgeContext),
12981299
sessionId(kj::mv(sessionId)),
12991300
tabId(kj::mv(tabId)),
1300-
userDisplayName(percentEncode(userInfo.getDisplayName().getDefaultText())),
1301+
userDisplayName(kj::encodeUriComponent(userInfo.getDisplayName().getDefaultText())),
13011302
userHandle(kj::heapString(userInfo.getPreferredHandle())),
13021303
userPicture(kj::heapString(userInfo.getPictureUrl())),
13031304
userPronouns(userInfo.getPronouns()),
@@ -2025,7 +2026,7 @@ private:
20252026
addHeader(lines, "Content-Id", attachment.getContentId());
20262027
lines.add(nullptr);
20272028

2028-
lines.add(base64Encode(attachment.getContent(), true));
2029+
lines.add(kj::encodeBase64(attachment.getContent(), true));
20292030
}
20302031
};
20312032

@@ -2252,7 +2253,7 @@ public:
22522253
UiSession::Client session =
22532254
kj::heap<WebSessionImpl>(serverAddress, userInfo, params.getContext(),
22542255
bridgeContext, kj::str(sessionIdCounter++),
2255-
hexEncode(params.getTabId()),
2256+
kj::encodeHex(params.getTabId()),
22562257
kj::heapString(sessionParams.getBasePath()),
22572258
kj::heapString(sessionParams.getUserAgent()),
22582259
kj::strArray(sessionParams.getAcceptableLanguages(), ","),
@@ -2275,7 +2276,7 @@ public:
22752276
UiSession::Client session =
22762277
kj::heap<WebSessionImpl>(serverAddress, userInfo, params.getContext(),
22772278
bridgeContext, kj::str(sessionIdCounter++),
2278-
hexEncode(params.getTabId()),
2279+
kj::encodeHex(params.getTabId()),
22792280
kj::heapString(""), kj::heapString(""), kj::heapString(""),
22802281
kj::heapString(config.getApiPath()),
22812282
bridgeContext.formatPermissions(userPermissions),

src/sandstorm/spk.c++

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "spk.h"
2020
#include <kj/debug.h>
2121
#include <kj/io.h>
22+
#include <kj/encoding.h>
2223
#include <capnp/serialize.h>
2324
#include <capnp/serialize-packed.h>
2425
#include <capnp/compat/json.h>
@@ -86,7 +87,7 @@ public:
8687
} else {
8788
auto call = output.initCall();
8889
call.setFunction("Base64");
89-
call.initParams(1)[0].setString(base64Encode(input, false));
90+
call.initParams(1)[0].setString(kj::encodeBase64(input, false));
9091
}
9192
}
9293

src/sandstorm/util-test.c++

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,67 +22,6 @@
2222
namespace sandstorm {
2323
namespace {
2424

25-
KJ_TEST("base64 encoding/decoding") {
26-
{
27-
auto encoded = base64Encode(kj::StringPtr("foo").asBytes(), false);
28-
KJ_EXPECT(encoded == "Zm9v", encoded, encoded.size());
29-
KJ_EXPECT(kj::heapString(base64Decode(encoded).asChars()) == "foo");
30-
}
31-
32-
{
33-
auto encoded = base64Encode(kj::StringPtr("corge").asBytes(), false);
34-
KJ_EXPECT(encoded == "Y29yZ2U=", encoded);
35-
KJ_EXPECT(kj::heapString(base64Decode(encoded).asChars()) == "corge");
36-
}
37-
38-
KJ_EXPECT(kj::heapString(base64Decode("Y29yZ2U").asChars()) == "corge");
39-
KJ_EXPECT(kj::heapString(base64Decode("Y\n29y Z@2U=\n").asChars()) == "corge");
40-
41-
{
42-
auto encoded = base64Encode(kj::StringPtr("corge").asBytes(), true);
43-
KJ_EXPECT(encoded == "Y29yZ2U=\n", encoded);
44-
}
45-
46-
kj::StringPtr fullLine = "012345678901234567890123456789012345678901234567890123";
47-
{
48-
auto encoded = base64Encode(fullLine.asBytes(), false);
49-
KJ_EXPECT(
50-
encoded == "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIz",
51-
encoded);
52-
}
53-
{
54-
auto encoded = base64Encode(fullLine.asBytes(), true);
55-
KJ_EXPECT(
56-
encoded == "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIz\n",
57-
encoded);
58-
}
59-
60-
kj::String multiLine = kj::str(fullLine, "456");
61-
{
62-
auto encoded = base64Encode(multiLine.asBytes(), false);
63-
KJ_EXPECT(
64-
encoded == "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2",
65-
encoded);
66-
}
67-
{
68-
auto encoded = base64Encode(multiLine.asBytes(), true);
69-
KJ_EXPECT(
70-
encoded == "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIz\n"
71-
"NDU2\n",
72-
encoded);
73-
}
74-
}
75-
76-
KJ_TEST("percent encoding/decoding") {
77-
KJ_EXPECT(percentEncode("foo") == "foo");
78-
KJ_EXPECT(percentEncode("foo bar") == "foo%20bar");
79-
KJ_EXPECT(percentEncode("\xab\xba") == "%ab%ba");
80-
KJ_EXPECT(percentEncode(kj::StringPtr("foo\0bar", 7)) == "foo%00bar");
81-
82-
KJ_EXPECT(kj::str(percentDecode("foo%20bar").asChars()) == "foo bar");
83-
KJ_EXPECT(kj::str(percentDecode("%ab%BA").asChars()) == "\xab\xba");
84-
}
85-
8625
KJ_TEST("HeaderWhitelist") {
8726
const char* WHITELIST[] = {
8827
"bar-baz",

0 commit comments

Comments
 (0)