|
34 | 34 | #include <unistd.h> |
35 | 35 | #include <linux/version.h> |
36 | 36 |
|
37 | | -#define BUFFERSIZE 1024 |
38 | | -#include <b64/decode.h> |
39 | 37 | #include "ebpflow.ebpf.enc" |
40 | 38 |
|
41 | 39 | static ContainerInfo cinfo; |
42 | 40 |
|
43 | 41 | /* ******************************************* */ |
44 | 42 |
|
| 43 | +std::string b64decode(const void* data, const size_t len) { |
| 44 | + unsigned char* p = (unsigned char*)data; |
| 45 | + int pad = len > 0 && (len % 4 || p[len - 1] == '='); |
| 46 | + const size_t L = ((len + 3) / 4 - pad) * 4; |
| 47 | + std::string str(L / 4 * 3 + pad, '\0'); |
| 48 | + const int B64index[256] = { |
| 49 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 50 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 51 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55, |
| 52 | + 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, |
| 53 | + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, |
| 54 | + 0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 55 | + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; |
| 56 | + |
| 57 | + for(size_t i = 0, j = 0; i < L; i += 4) { |
| 58 | + int n = B64index[p[i]] << 18 | |
| 59 | + B64index[p[i + 1]] << 12 | |
| 60 | + B64index[p[i + 2]] << 6 | |
| 61 | + B64index[p[i + 3]]; |
| 62 | + str[j++] = n >> 16; |
| 63 | + str[j++] = n >> 8 & 0xFF; |
| 64 | + str[j++] = n & 0xFF; |
| 65 | + } |
| 66 | + |
| 67 | + if(pad) { |
| 68 | + int n = B64index[p[L]] << 18 | B64index[p[L + 1]] << 12; |
| 69 | + str[str.size() - 1] = n >> 16; |
| 70 | + |
| 71 | + if(len > L + 2 && p[L + 2] != '=') { |
| 72 | + n |= B64index[p[L + 2]] << 6; |
| 73 | + str.push_back(n >> 8 & 0xFF); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return str; |
| 78 | +} |
| 79 | + |
| 80 | +/* ******************************************* */ |
| 81 | +/* ******************************************* */ |
| 82 | + |
45 | 83 | static int attachEBPFTracepoint(ebpf::BPF *bpf, |
46 | 84 | const char *tracepoint, const char *probe_func) { |
47 | 85 | ebpf::StatusTuple rc = bpf->attach_tracepoint(tracepoint, probe_func); |
@@ -79,13 +117,7 @@ extern "C" { |
79 | 117 | void* init_ebpf_flow(void *priv_ptr, eBPFHandler ebpfHandler, |
80 | 118 | ebpfRetCode *rc, u_int16_t flags) { |
81 | 119 | ebpf::BPF *bpf = NULL; |
82 | | - // Decoding ebpf b64 (http://libb64.sourceforge.net) |
83 | | - std::stringstream ebpf_b64_s(ebpf_code); |
84 | | - std::stringstream code_s; |
85 | | - base64::decoder E; |
86 | | - E.decode(ebpf_b64_s, code_s); |
87 | | - std::string code = code_s.str().c_str(); |
88 | | - |
| 120 | + std::string code = b64decode(ebpf_code, strlen(ebpf_code)); |
89 | 121 | ebpf::StatusTuple open_res(0); |
90 | 122 |
|
91 | 123 | // Default value is 0 |
|
0 commit comments