Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
4b06a24e
Commit
4b06a24e
authored
May 24, 2019
by
Joe Lee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for url-safe base64 encoding
parent
463052ee
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
0 deletions
+68
-0
encoding-test.c++
c++/src/kj/encoding-test.c++
+45
-0
encoding.c++
c++/src/kj/encoding.c++
+20
-0
encoding.h
c++/src/kj/encoding.h
+3
-0
No files found.
c++/src/kj/encoding-test.c++
View file @
4b06a24e
...
...
@@ -475,5 +475,50 @@ KJ_TEST("base64 encoding/decoding") {
}
}
KJ_TEST
(
"base64 url encoding"
)
{
{
// Handles empty.
auto
encoded
=
encodeBase64Url
(
StringPtr
(
""
).
asBytes
());
KJ_EXPECT
(
encoded
==
""
,
encoded
,
encoded
.
size
());
}
{
// Handles paddingless encoding.
auto
encoded
=
encodeBase64Url
(
StringPtr
(
"foo"
).
asBytes
());
KJ_EXPECT
(
encoded
==
"Zm9v"
,
encoded
,
encoded
.
size
());
}
{
// Handles padded encoding.
auto
encoded1
=
encodeBase64Url
(
StringPtr
(
"quux"
).
asBytes
());
KJ_EXPECT
(
encoded1
==
"cXV1eA"
,
encoded1
,
encoded1
.
size
());
auto
encoded2
=
encodeBase64Url
(
StringPtr
(
"corge"
).
asBytes
());
KJ_EXPECT
(
encoded2
==
"Y29yZ2U"
,
encoded2
,
encoded2
.
size
());
}
{
// No line breaks.
StringPtr
fullLine
=
"012345678901234567890123456789012345678901234567890123"
;
auto
encoded
=
encodeBase64Url
(
StringPtr
(
fullLine
).
asBytes
());
KJ_EXPECT
(
encoded
==
"MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIz"
,
encoded
);
}
{
// Replaces plusses.
ArrayPtr
<
const
byte
>
data
=
{
0
b11111011
,
0
b11101111
,
0
b10111110
};
auto
encoded
=
encodeBase64Url
(
data
);
KJ_EXPECT
(
encoded
==
"----"
,
encoded
,
encoded
.
size
());
}
{
// Replaces slashes.
ArrayPtr
<
const
byte
>
data
=
{
0
b11111111
,
0
b11111111
,
0
b11111111
};
auto
encoded
=
encodeBase64Url
(
data
);
KJ_EXPECT
(
encoded
==
"____"
,
encoded
,
encoded
.
size
());
}
}
}
// namespace
}
// namespace kj
c++/src/kj/encoding.c++
View file @
4b06a24e
...
...
@@ -1001,4 +1001,24 @@ EncodingResult<Array<byte>> decodeBase64(ArrayPtr<const char> input) {
return
EncodingResult
<
Array
<
byte
>>
(
kj
::
mv
(
output
),
state
.
hadErrors
);
}
String
encodeBase64Url
(
ArrayPtr
<
const
byte
>
bytes
)
{
// TODO(perf): Rewrite as single pass?
// TODO(someday): Write decoder?
auto
base64
=
kj
::
encodeBase64
(
bytes
);
for
(
char
&
c
:
base64
)
{
if
(
c
==
'+'
)
c
=
'-'
;
if
(
c
==
'/'
)
c
=
'_'
;
}
// Remove trailing '='s.
kj
::
ArrayPtr
<
const
char
>
slice
=
base64
;
while
(
slice
.
size
()
>
0
&&
slice
.
back
()
==
'='
)
{
slice
=
slice
.
slice
(
0
,
slice
.
size
()
-
1
);
}
return
kj
::
str
(
slice
);
}
}
// namespace kj
c++/src/kj/encoding.h
View file @
4b06a24e
...
...
@@ -213,6 +213,9 @@ EncodingResult<Array<byte>> decodeBase64(ArrayPtr<const char> text);
// Decode base64 text. This function reports errors required by the WHATWG HTML/Infra specs: see
// https://html.spec.whatwg.org/multipage/webappapis.html#atob for details.
String
encodeBase64Url
(
ArrayPtr
<
const
byte
>
bytes
);
// Encode the given bytes as URL-safe base64 text. (RFC 4648, section 5)
// =======================================================================================
// inline implementation details
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment