Skip to content

Commit c5874e8

Browse files
committed
Move Base64
1 parent ac6efd2 commit c5874e8

File tree

5 files changed

+82
-70
lines changed

5 files changed

+82
-70
lines changed

devserver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ filegroup(
66
"argparse/argparse.h",
77
"json/json.h",
88
"md5/md5.h",
9+
"base64/base64.h",
910
]),
1011
visibility = ["//visibility:public"],
1112
)

devserver/base64/LICENSE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright 2005-2011, Jouni Malinen
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10+
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

devserver/base64/base64.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Base64 encoding/decoding (RFC1341)
3+
* Copyright (c) 2005-2011, Jouni Malinen <[email protected]>
4+
*
5+
* This software may be distributed under the terms of the BSD license.
6+
* See README for more details.
7+
*/
8+
9+
// 2016-12-12 - Gaspard Petit : Slightly modified to return a std::string
10+
// instead of a buffer allocated with malloc.
11+
12+
#include <string>
13+
14+
static const unsigned char base64_table[65] =
15+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16+
17+
/**
18+
* base64_encode - Base64 encode
19+
* @src: Data to be encoded
20+
* @len: Length of the data to be encoded
21+
* @out_len: Pointer to output length variable, or %NULL if not used
22+
* Returns: Allocated buffer of out_len bytes of encoded data,
23+
* or empty string on failure
24+
*/
25+
std::string base64_encode(const unsigned char *src, size_t len) {
26+
unsigned char *out, *pos;
27+
const unsigned char *end, *in;
28+
29+
size_t olen;
30+
31+
olen = 4 * ((len + 2) / 3); /* 3-byte blocks to 4-byte */
32+
33+
if (olen < len) return std::string(); /* integer overflow */
34+
35+
std::string outStr;
36+
outStr.resize(olen);
37+
out = (unsigned char *)&outStr[0];
38+
39+
end = src + len;
40+
in = src;
41+
pos = out;
42+
while (end - in >= 3) {
43+
*pos++ = base64_table[in[0] >> 2];
44+
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
45+
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
46+
*pos++ = base64_table[in[2] & 0x3f];
47+
in += 3;
48+
}
49+
50+
if (end - in) {
51+
*pos++ = base64_table[in[0] >> 2];
52+
if (end - in == 1) {
53+
*pos++ = base64_table[(in[0] & 0x03) << 4];
54+
*pos++ = '=';
55+
} else {
56+
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
57+
*pos++ = base64_table[(in[1] & 0x0f) << 2];
58+
}
59+
*pos++ = '=';
60+
}
61+
62+
return outStr;
63+
}

devserver/devserver_loader.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
console.log("inside devserver_loader.js");
2-
31
setInterval(() => {
42
fetch("/devserver/manifest")
53
.then(function (response) {

devserver/main.cc

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
// #include "external/rules_devserver/devserver/httplib/httplib.h"
66
// #include "external/rules_devserver/devserver/json/json.h"
77
// #include "external/rules_devserver/devserver/md5/md5.h"
8+
// #include "external/rules_devserver/devserver/base64/base64.h"
89

910
#include "devserver/argparse/argparse.h"
11+
#include "devserver/base64/base64.h"
1012
#include "devserver/httplib/httplib.h"
1113
#include "devserver/json/json.h"
1214
#include "devserver/md5/md5.h"
@@ -27,70 +29,6 @@ constexpr char kWorkspaceName[] = "rules_devserver";
2729
constexpr int kDefaultPort = 8080;
2830
constexpr char kHost[] = "localhost";
2931

30-
/*
31-
* Base64 encoding/decoding (RFC1341)
32-
* Copyright (c) 2005-2011, Jouni Malinen <[email protected]>
33-
*
34-
* This software may be distributed under the terms of the BSD license.
35-
* See README for more details.
36-
*/
37-
38-
// 2016-12-12 - Gaspard Petit : Slightly modified to return a std::string
39-
// instead of a buffer allocated with malloc.
40-
41-
#include <string>
42-
43-
static const unsigned char base64_table[65] =
44-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45-
46-
/**
47-
* base64_encode - Base64 encode
48-
* @src: Data to be encoded
49-
* @len: Length of the data to be encoded
50-
* @out_len: Pointer to output length variable, or %NULL if not used
51-
* Returns: Allocated buffer of out_len bytes of encoded data,
52-
* or empty string on failure
53-
*/
54-
std::string base64_encode(const unsigned char *src, size_t len) {
55-
unsigned char *out, *pos;
56-
const unsigned char *end, *in;
57-
58-
size_t olen;
59-
60-
olen = 4 * ((len + 2) / 3); /* 3-byte blocks to 4-byte */
61-
62-
if (olen < len) return std::string(); /* integer overflow */
63-
64-
std::string outStr;
65-
outStr.resize(olen);
66-
out = (unsigned char *)&outStr[0];
67-
68-
end = src + len;
69-
in = src;
70-
pos = out;
71-
while (end - in >= 3) {
72-
*pos++ = base64_table[in[0] >> 2];
73-
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
74-
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
75-
*pos++ = base64_table[in[2] & 0x3f];
76-
in += 3;
77-
}
78-
79-
if (end - in) {
80-
*pos++ = base64_table[in[0] >> 2];
81-
if (end - in == 1) {
82-
*pos++ = base64_table[(in[0] & 0x03) << 4];
83-
*pos++ = '=';
84-
} else {
85-
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
86-
*pos++ = base64_table[(in[1] & 0x0f) << 2];
87-
}
88-
*pos++ = '=';
89-
}
90-
91-
return outStr;
92-
}
93-
9432
struct Arguments {
9533
int32_t port;
9634
std::string static_file;
@@ -109,16 +47,17 @@ json ComputeManifest(const std::map<Path, FileContents> &path_to_contents) {
10947
json manifest;
11048

11149
for (const auto &path_and_contents : path_to_contents) {
112-
Path path = path_and_contents.first;
113-
FileContents contents = path_and_contents.second;
50+
const Path path = path_and_contents.first;
51+
const FileContents contents = path_and_contents.second;
11452

11553
picohash_ctx_t ctx;
11654
char digest[PICOHASH_MD5_DIGEST_LENGTH];
11755

11856
picohash_init_md5(&ctx);
11957
picohash_update(&ctx, contents.c_str(), contents.size());
12058
picohash_final(&ctx, digest);
121-
std::string digest_str(base64_encode(digest, PICOHASH_MD5_DIGEST_LENGTH));
59+
const std::string digest_str(
60+
base64_encode(digest, PICOHASH_MD5_DIGEST_LENGTH));
12261
manifest[path] = digest_str;
12362
}
12463

@@ -230,7 +169,7 @@ int main(int argc, char **argv) {
230169
const std::map<std::string, std::string> path_to_contents = {
231170
{"/", static_file_contents}};
232171
json manifest = ComputeManifest(path_to_contents);
233-
std::cout << "manifest: " << manifest.dump() << std::endl;
172+
DEBUG_LOG("manifest: " << manifest.dump() << "\n\n");
234173

235174
svr.Get("/", [&static_file_contents](const httplib::Request &req,
236175
httplib::Response &res) {

0 commit comments

Comments
 (0)