Skip to content

Commit 9b93775

Browse files
committed
完成第一版
1 parent 3f9c3fc commit 9b93775

29 files changed

+2690
-0
lines changed

build/imports/codec.cse

1.99 MB
Binary file not shown.

codec.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Covariant Script Network Extension
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Copyright (C) 2018 Michael Lee(李登淳)
17+
18+
* Github: https://github.com/mikecovlee
19+
*/
20+
// Base32
21+
#include <cppcodec/base32_rfc4648.hpp>
22+
#include <cppcodec/base32_crockford.hpp>
23+
#include <cppcodec/base32_hex.hpp>
24+
// Base64
25+
#include <cppcodec/base64_rfc4648.hpp>
26+
#include <cppcodec/base64_url.hpp>
27+
#include <cppcodec/base64_url_unpadded.hpp>
28+
// CovScript DLL Header
29+
#include <covscript/dll.hpp>
30+
31+
#define gen_invoker(NAME) [](const cs::string &str) -> cs::string { return NAME<std::string>(str); }
32+
33+
void cs_extension_main(cs::name_space *ns)
34+
{
35+
// Base32 root namespace
36+
cs::namespace_t base32_ns = cs::make_shared_namespace<cs::name_space>();
37+
// Base32 child namespace
38+
cs::namespace_t base32_rfc4648_ns = cs::make_shared_namespace<cs::name_space>();
39+
cs::namespace_t base32_crockford_ns = cs::make_shared_namespace<cs::name_space>();
40+
cs::namespace_t base32_hex_ns = cs::make_shared_namespace<cs::name_space>();
41+
// Base64 root namespace
42+
cs::namespace_t base64_ns = cs::make_shared_namespace<cs::name_space>();
43+
// Base64 child namespace
44+
cs::namespace_t base64_rfc4648_ns = cs::make_shared_namespace<cs::name_space>();
45+
cs::namespace_t base64_url_ns = cs::make_shared_namespace<cs::name_space>();
46+
cs::namespace_t base64_url_unpadded_ns = cs::make_shared_namespace<cs::name_space>();
47+
// Register
48+
(*ns)
49+
.add_var("base32", cs::make_namespace(base32_ns))
50+
.add_var("base64", cs::make_namespace(base64_ns));
51+
(*base32_ns)
52+
.add_var("standard", cs::make_namespace(base32_rfc4648_ns))
53+
.add_var("rfc4648", cs::make_namespace(base32_rfc4648_ns))
54+
.add_var("crockford", cs::make_namespace(base32_crockford_ns))
55+
.add_var("hex", cs::make_namespace(base32_hex_ns));
56+
(*base32_rfc4648_ns)
57+
.add_var("encode", cs::make_cni(gen_invoker(cppcodec::base32_rfc4648::encode), true))
58+
.add_var("decode", cs::make_cni(gen_invoker(cppcodec::base32_rfc4648::decode), true));
59+
(*base32_crockford_ns)
60+
.add_var("encode", cs::make_cni(gen_invoker(cppcodec::base32_crockford::encode), true))
61+
.add_var("decode", cs::make_cni(gen_invoker(cppcodec::base32_crockford::decode), true));
62+
(*base32_hex_ns)
63+
.add_var("encode", cs::make_cni(gen_invoker(cppcodec::base32_hex::encode), true))
64+
.add_var("decode", cs::make_cni(gen_invoker(cppcodec::base32_hex::decode), true));
65+
(*base64_ns)
66+
.add_var("standard", cs::make_namespace(base64_rfc4648_ns))
67+
.add_var("rfc4648", cs::make_namespace(base64_rfc4648_ns))
68+
.add_var("url", cs::make_namespace(base64_url_ns))
69+
.add_var("url_unpadded", cs::make_namespace(base64_url_unpadded_ns));
70+
(*base64_rfc4648_ns)
71+
.add_var("encode", cs::make_cni(gen_invoker(cppcodec::base64_rfc4648::encode), true))
72+
.add_var("decode", cs::make_cni(gen_invoker(cppcodec::base64_rfc4648::decode), true));
73+
(*base64_url_ns)
74+
.add_var("encode", cs::make_cni(gen_invoker(cppcodec::base64_url::encode), true))
75+
.add_var("decode", cs::make_cni(gen_invoker(cppcodec::base64_url::decode), true));
76+
(*base64_url_unpadded_ns)
77+
.add_var("encode", cs::make_cni(gen_invoker(cppcodec::base64_url_unpadded::encode), true))
78+
.add_var("decode", cs::make_cni(gen_invoker(cppcodec::base64_url_unpadded::decode), true));
79+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright (C) 2015 Topology LP
3+
* All rights reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to
7+
* deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
* sell copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
* IN THE SOFTWARE.
22+
*/
23+
24+
#ifndef CPPCODEC_BASE32_CROCKFORD
25+
#define CPPCODEC_BASE32_CROCKFORD
26+
27+
#include "detail/codec.hpp"
28+
#include "detail/base32.hpp"
29+
30+
namespace cppcodec {
31+
32+
namespace detail {
33+
34+
static constexpr const char base32_crockford_alphabet[] = {
35+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', // at index 10
36+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 18 - no I
37+
'J', 'K', // 20 - no L
38+
'M', 'N', // 22 - no O
39+
'P', 'Q', 'R', 'S', 'T', // 27 - no U
40+
'V', 'W', 'X', 'Y', 'Z' // 32
41+
};
42+
43+
class base32_crockford_base
44+
{
45+
public:
46+
static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() {
47+
static_assert(sizeof(base32_crockford_alphabet) == 32, "base32 alphabet must have 32 values");
48+
return sizeof(base32_crockford_alphabet);
49+
}
50+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx)
51+
{
52+
return base32_crockford_alphabet[idx];
53+
}
54+
static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c)
55+
{
56+
// Hex decoding is always case-insensitive (even in RFC 4648), the question
57+
// is only for encoding whether to use upper-case or lower-case letters.
58+
return (c == 'O' || c == 'o') ? '0'
59+
: (c == 'I' || c == 'i' || c == 'L' || c == 'l') ? '1'
60+
: (c >= 'a' && c <= 'z') ? (c - 'a' + 'A')
61+
: c;
62+
}
63+
64+
static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return false; }
65+
static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return false; }
66+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char) { return false; }
67+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; }
68+
69+
static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char c) {
70+
return c == '-'; // "Hyphens (-) can be inserted into strings [for readability]."
71+
}
72+
};
73+
74+
// base32_crockford is a concatenative iterative (i.e. streaming) interpretation of Crockford base32.
75+
// It interprets the statement "zero-extend the number to make its bit-length a multiple of 5"
76+
// to mean zero-extending it on the right.
77+
// (The other possible interpretation is base32_crockford_num, a place-based single number encoding system.
78+
// See http://merrigrove.blogspot.ca/2014/04/what-heck-is-base64-encoding-really.html for more info.)
79+
class base32_crockford : public base32_crockford_base
80+
{
81+
public:
82+
template <typename Codec> using codec_impl = stream_codec<Codec, base32_crockford>;
83+
};
84+
85+
} // namespace detail
86+
87+
using base32_crockford = detail::codec<detail::base32<detail::base32_crockford>>;
88+
89+
} // namespace cppcodec
90+
91+
#endif // CPPCODEC_BASE32_CROCKFORD
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (C) 2015 Topology LP
3+
* All rights reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to
7+
* deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
* sell copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
* IN THE SOFTWARE.
22+
*/
23+
24+
#ifndef CPPCODEC_BASE32_DEFAULT_CROCKFORD
25+
#define CPPCODEC_BASE32_DEFAULT_CROCKFORD
26+
27+
#include "base32_crockford.hpp"
28+
29+
using base32 = cppcodec::base32_crockford;
30+
31+
#endif // CPPCODEC_BASE32_DEFAULT_CROCKFORD
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (C) 2015, 2016 Topology LP
3+
* All rights reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to
7+
* deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
* sell copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
* IN THE SOFTWARE.
22+
*/
23+
24+
#ifndef CPPCODEC_BASE32_DEFAULT_HEX
25+
#define CPPCODEC_BASE32_DEFAULT_HEX
26+
27+
#include "base32_hex.hpp"
28+
29+
using base32 = cppcodec::base32_hex;
30+
31+
#endif // CPPCODEC_BASE32_DEFAULT_HEX
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (C) 2015 Topology LP
3+
* All rights reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to
7+
* deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
* sell copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
* IN THE SOFTWARE.
22+
*/
23+
24+
#ifndef CPPCODEC_BASE32_DEFAULT_RFC4648
25+
#define CPPCODEC_BASE32_DEFAULT_RFC4648
26+
27+
#include "base32_rfc4648.hpp"
28+
29+
using base32 = cppcodec::base32_rfc4648;
30+
31+
#endif // CPPCODEC_BASE32_DEFAULT_RFC4648

include/cppcodec/base32_hex.hpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (C) 2015, 2016 Topology LP
3+
* All rights reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to
7+
* deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
* sell copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
* IN THE SOFTWARE.
22+
*/
23+
24+
#ifndef CPPCODEC_BASE32_HEX
25+
#define CPPCODEC_BASE32_HEX
26+
27+
#include "detail/codec.hpp"
28+
#include "detail/base32.hpp"
29+
30+
namespace cppcodec {
31+
32+
namespace detail {
33+
34+
// RFC 4648 uses a simple alphabet: A-Z starting at index 0, then 2-7 starting at index 26.
35+
static constexpr const char base32_hex_alphabet[] = {
36+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
37+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
38+
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V'
39+
};
40+
41+
class base32_hex
42+
{
43+
public:
44+
template <typename Codec> using codec_impl = stream_codec<Codec, base32_hex>;
45+
46+
static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() {
47+
static_assert(sizeof(base32_hex_alphabet) == 32, "base32 alphabet must have 32 values");
48+
return sizeof(base32_hex_alphabet);
49+
}
50+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx)
51+
{
52+
return base32_hex_alphabet[idx];
53+
}
54+
static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c)
55+
{
56+
// Lower-case letters are accepted, though not generally expected.
57+
return (c >= 'a' && c <= 'v') ? (c - 'a' + 'A') : c;
58+
}
59+
60+
static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
61+
static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
62+
static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
63+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char c) { return c == '='; }
64+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; }
65+
66+
// RFC4648 does not specify any whitespace being allowed in base32 encodings.
67+
static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char) { return false; }
68+
};
69+
70+
} // namespace detail
71+
72+
using base32_hex = detail::codec<detail::base32<detail::base32_hex>>;
73+
74+
} // namespace cppcodec
75+
76+
#endif // CPPCODEC_BASE32_HEX

0 commit comments

Comments
 (0)