Skip to content

Commit d9fa4df

Browse files
author
samuelesabella
committed
Fix base64
1 parent ce34638 commit d9fa4df

11 files changed

Lines changed: 468 additions & 48 deletions

File tree

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
*.o
2-
*.a
1+
**/*.o
2+
**/*.a
33
ebpflow.ebpf.enc
44
ebpftest
55
toolebpflow
6-
autom4te.cache/
6+
autom4te.cache/

Makefile.in

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ all: ebpflowexport
2929
container_info.o: container_info.cpp container_info.h
3030
g++ -c $(CFLAGS) container_info.cpp -o container_info.o
3131

32+
libb64.a:
33+
make -C ./libs/b64/src && mv ./libs/b64/src/libb64.a ./libb64.a && make -C ./libs/b64/src clean
34+
3235
libebpfflow.a: ebpf_flow.cpp ebpf_flow.h container_info.o ebpflow.ebpf.enc
33-
g++ -c $(CFLAGS) ebpf_flow.cpp -o ebpf_flow.o
36+
g++ -c $(CFLAGS) ebpf_flow.cpp -I./libs/ -o ebpf_flow.o
3437
ar rvs $@ ebpf_flow.o container_info.o
3538

36-
ebpflowexport: ebpflowexport.cpp libebpfflow.a Makefile
37-
g++ $(CFLAGS) ebpflowexport.cpp -o $@ libebpfflow.a $(LIBS)
39+
ebpflowexport: ebpflowexport.cpp libebpfflow.a Makefile libb64.a
40+
g++ $(CFLAGS) ebpflowexport.cpp -o $@ libebpfflow.a libb64.a $(LIBS)
3841

3942
ebpflow.ebpf.enc: ebpflow_header.ebpf ebpf_types.h ebpflow_code.ebpf Makefile
4043
echo -n "const char * ebpf_code = R\"(" > ebpflow.ebpf.enc
@@ -45,4 +48,4 @@ go_ebpflowexport: ebpflowexport.go Makefile libebpfflow.a
4548
go build -o go_ebpflowexport ebpflowexport.go
4649

4750
clean:
48-
/bin/rm -f *~ container_info.a libebpfflow.a *.o ebpflow.ebpf.enc
51+
/bin/rm -f *~ container_info.a libebpfflow.a *.o ebpflow.ebpf.enc libb64.a

ebpf_flow.cpp

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,52 +34,14 @@
3434
#include <unistd.h>
3535
#include <linux/version.h>
3636

37+
#define BUFFERSIZE 1024
38+
#include <b64/decode.h>
3739
#include "ebpflow.ebpf.enc"
3840

3941
static ContainerInfo cinfo;
4042

4143
/* ******************************************* */
4244

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-
8345
static int attachEBPFTracepoint(ebpf::BPF *bpf,
8446
const char *tracepoint, const char *probe_func) {
8547
ebpf::StatusTuple rc = bpf->attach_tracepoint(tracepoint, probe_func);
@@ -117,7 +79,13 @@ extern "C" {
11779
void* init_ebpf_flow(void *priv_ptr, eBPFHandler ebpfHandler,
11880
ebpfRetCode *rc, u_int16_t flags) {
11981
ebpf::BPF *bpf = NULL;
120-
std::string code = b64decode(ebpf_code, strlen(ebpf_code));
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+
12189
ebpf::StatusTuple open_res(0);
12290

12391
// Default value is 0

libs/b64/README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Credits: http://libb64.sourceforge.net/

libs/b64/cdecode.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
cdecode.h - c header for a base64 decoding algorithm
3+
4+
This is part of the libb64 project, and has been placed in the public domain.
5+
For details, see http://sourceforge.net/projects/libb64
6+
*/
7+
8+
#ifndef BASE64_CDECODE_H
9+
#define BASE64_CDECODE_H
10+
11+
typedef enum
12+
{
13+
step_a, step_b, step_c, step_d
14+
} base64_decodestep;
15+
16+
typedef struct
17+
{
18+
base64_decodestep step;
19+
char plainchar;
20+
} base64_decodestate;
21+
22+
void base64_init_decodestate(base64_decodestate* state_in);
23+
24+
int base64_decode_value(char value_in);
25+
26+
int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in);
27+
28+
#endif /* BASE64_CDECODE_H */
29+

libs/b64/cencode.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
cencode.h - c header for a base64 encoding algorithm
3+
4+
This is part of the libb64 project, and has been placed in the public domain.
5+
For details, see http://sourceforge.net/projects/libb64
6+
*/
7+
8+
#ifndef BASE64_CENCODE_H
9+
#define BASE64_CENCODE_H
10+
11+
typedef enum
12+
{
13+
step_A, step_B, step_C
14+
} base64_encodestep;
15+
16+
typedef struct
17+
{
18+
base64_encodestep step;
19+
char result;
20+
int stepcount;
21+
} base64_encodestate;
22+
23+
void base64_init_encodestate(base64_encodestate* state_in);
24+
25+
char base64_encode_value(char value_in);
26+
27+
int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in);
28+
29+
int base64_encode_blockend(char* code_out, base64_encodestate* state_in);
30+
31+
#endif /* BASE64_CENCODE_H */
32+

libs/b64/decode.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// :mode=c++:
2+
/*
3+
decode.h - c++ wrapper for a base64 decoding algorithm
4+
5+
This is part of the libb64 project, and has been placed in the public domain.
6+
For details, see http://sourceforge.net/projects/libb64
7+
*/
8+
#ifndef BASE64_DECODE_H
9+
#define BASE64_DECODE_H
10+
11+
#include <iostream>
12+
13+
namespace base64
14+
{
15+
extern "C"
16+
{
17+
#include "cdecode.h"
18+
}
19+
20+
struct decoder
21+
{
22+
base64_decodestate _state;
23+
int _buffersize;
24+
25+
decoder(int buffersize_in = BUFFERSIZE)
26+
: _buffersize(buffersize_in)
27+
{}
28+
29+
int decode(char value_in)
30+
{
31+
return base64_decode_value(value_in);
32+
}
33+
34+
int decode(const char* code_in, const int length_in, char* plaintext_out)
35+
{
36+
return base64_decode_block(code_in, length_in, plaintext_out, &_state);
37+
}
38+
39+
void decode(std::istream& istream_in, std::ostream& ostream_in)
40+
{
41+
base64_init_decodestate(&_state);
42+
//
43+
const int N = _buffersize;
44+
char* code = new char[N];
45+
char* plaintext = new char[N];
46+
int codelength;
47+
int plainlength;
48+
49+
do
50+
{
51+
istream_in.read((char*)code, N);
52+
codelength = istream_in.gcount();
53+
plainlength = decode(code, codelength, plaintext);
54+
ostream_in.write((const char*)plaintext, plainlength);
55+
}
56+
while (istream_in.good() && codelength > 0);
57+
//
58+
base64_init_decodestate(&_state);
59+
60+
delete [] code;
61+
delete [] plaintext;
62+
}
63+
};
64+
65+
} // namespace base64
66+
67+
68+
69+
#endif // BASE64_DECODE_H
70+

libs/b64/encode.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// :mode=c++:
2+
/*
3+
encode.h - c++ wrapper for a base64 encoding algorithm
4+
5+
This is part of the libb64 project, and has been placed in the public domain.
6+
For details, see http://sourceforge.net/projects/libb64
7+
*/
8+
#ifndef BASE64_ENCODE_H
9+
#define BASE64_ENCODE_H
10+
11+
#include <iostream>
12+
13+
namespace base64
14+
{
15+
extern "C"
16+
{
17+
#include "cencode.h"
18+
}
19+
20+
struct encoder
21+
{
22+
base64_encodestate _state;
23+
int _buffersize;
24+
25+
encoder(int buffersize_in = BUFFERSIZE)
26+
: _buffersize(buffersize_in)
27+
{}
28+
29+
int encode(char value_in)
30+
{
31+
return base64_encode_value(value_in);
32+
}
33+
34+
int encode(const char* code_in, const int length_in, char* plaintext_out)
35+
{
36+
return base64_encode_block(code_in, length_in, plaintext_out, &_state);
37+
}
38+
39+
int encode_end(char* plaintext_out)
40+
{
41+
return base64_encode_blockend(plaintext_out, &_state);
42+
}
43+
44+
void encode(std::istream& istream_in, std::ostream& ostream_in)
45+
{
46+
base64_init_encodestate(&_state);
47+
//
48+
const int N = _buffersize;
49+
char* plaintext = new char[N];
50+
char* code = new char[2*N];
51+
int plainlength;
52+
int codelength;
53+
54+
do
55+
{
56+
istream_in.read(plaintext, N);
57+
plainlength = istream_in.gcount();
58+
//
59+
codelength = encode(plaintext, plainlength, code);
60+
ostream_in.write(code, codelength);
61+
}
62+
while (istream_in.good() && plainlength > 0);
63+
64+
codelength = encode_end(code);
65+
ostream_in.write(code, codelength);
66+
//
67+
base64_init_encodestate(&_state);
68+
69+
delete [] code;
70+
delete [] plaintext;
71+
}
72+
};
73+
74+
} // namespace base64
75+
76+
#endif // BASE64_ENCODE_H
77+

libs/b64/src/Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
LIBRARIES = libb64.a
2+
3+
# Build flags (uncomment one)
4+
#############################
5+
# Release build flags
6+
CFLAGS += -O3
7+
#############################
8+
# Debug build flags
9+
#CFLAGS += -g
10+
#############################
11+
12+
SOURCES = cdecode.c cencode.c
13+
14+
TARGETS = $(LIBRARIES)
15+
16+
LINK.o = gcc
17+
18+
CFLAGS += -Werror -pedantic
19+
CFLAGS += -I../..
20+
21+
vpath %.h ../include/b64
22+
23+
.PHONY : clean
24+
25+
all: $(TARGETS) #strip
26+
27+
libb64.a: cencode.o cdecode.o
28+
$(AR) $(ARFLAGS) $@ $^
29+
30+
strip:
31+
strip $(BINARIES) *.exe
32+
33+
clean:
34+
rm -f *.exe* *.o $(TARGETS) *.bak *~
35+
36+
distclean: clean
37+
rm -f depend
38+
39+
depend: $(SOURCES)
40+
makedepend -f- $(CFLAGS) $(SOURCES) 2> /dev/null 1> depend
41+
42+
-include depend
43+

0 commit comments

Comments
 (0)