-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathsqlite_encoding.cpp
More file actions
105 lines (92 loc) · 3.02 KB
/
Copy pathsqlite_encoding.cpp
File metadata and controls
105 lines (92 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Copyright (c) 2014-present, The osquery authors
*
* This source code is licensed as defined by the LICENSE file found in the
* root directory of this source tree.
*
* SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)
*/
#include <string>
#include <sqlite3.h>
#include <osquery/utils/base64.h>
#include <osquery/utils/chars.h>
namespace osquery {
enum class B64Type {
B64_ENCODE_CONDITIONAL = 1,
B64_ENCODE = 2,
B64_DECODE = 3,
};
static void b64SqliteValue(sqlite3_context* ctx,
int argc,
sqlite3_value** argv,
B64Type encode) {
if (argc == 0) {
return;
}
if (SQLITE_NULL == sqlite3_value_type(argv[0])) {
sqlite3_result_null(ctx);
return;
}
const auto* value = sqlite3_value_text(argv[0]);
auto size = static_cast<size_t>(sqlite3_value_bytes(argv[0]));
std::string input(reinterpret_cast<const char*>(value), size);
std::string result;
switch (encode) {
case B64Type::B64_ENCODE_CONDITIONAL:
if (isPrintable(input)) {
result = input;
break;
}
[[fallthrough]];
case B64Type::B64_ENCODE:
result = base64::encode(input);
break;
case B64Type::B64_DECODE:
result = base64::decode(input);
break;
}
sqlite3_result_text(
ctx, result.c_str(), static_cast<int>(result.size()), SQLITE_TRANSIENT);
}
static void sqliteB64ConditionalEncFunc(sqlite3_context* context,
int argc,
sqlite3_value** argv) {
b64SqliteValue(context, argc, argv, B64Type::B64_ENCODE_CONDITIONAL);
}
static void sqliteB64EncFunc(sqlite3_context* context,
int argc,
sqlite3_value** argv) {
b64SqliteValue(context, argc, argv, B64Type::B64_ENCODE);
}
static void sqliteB64DecFunc(sqlite3_context* context,
int argc,
sqlite3_value** argv) {
b64SqliteValue(context, argc, argv, B64Type::B64_DECODE);
}
void registerEncodingExtensions(sqlite3* db) {
sqlite3_create_function(db,
"conditional_to_base64",
1,
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
nullptr,
sqliteB64ConditionalEncFunc,
nullptr,
nullptr);
sqlite3_create_function(db,
"to_base64",
1,
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
nullptr,
sqliteB64EncFunc,
nullptr,
nullptr);
sqlite3_create_function(db,
"from_base64",
1,
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
nullptr,
sqliteB64DecFunc,
nullptr,
nullptr);
}
} // namespace osquery