Skip to content

Commit

Permalink
Fix base64
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelesabella committed Nov 21, 2019
1 parent ce34638 commit d9fa4df
Show file tree
Hide file tree
Showing 11 changed files with 468 additions and 48 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*.o
*.a
**/*.o
**/*.a
ebpflow.ebpf.enc
ebpftest
toolebpflow
autom4te.cache/
autom4te.cache/
11 changes: 7 additions & 4 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ all: ebpflowexport
container_info.o: container_info.cpp container_info.h
g++ -c $(CFLAGS) container_info.cpp -o container_info.o

libb64.a:
make -C ./libs/b64/src && mv ./libs/b64/src/libb64.a ./libb64.a && make -C ./libs/b64/src clean

libebpfflow.a: ebpf_flow.cpp ebpf_flow.h container_info.o ebpflow.ebpf.enc
g++ -c $(CFLAGS) ebpf_flow.cpp -o ebpf_flow.o
g++ -c $(CFLAGS) ebpf_flow.cpp -I./libs/ -o ebpf_flow.o
ar rvs $@ ebpf_flow.o container_info.o

ebpflowexport: ebpflowexport.cpp libebpfflow.a Makefile
g++ $(CFLAGS) ebpflowexport.cpp -o $@ libebpfflow.a $(LIBS)
ebpflowexport: ebpflowexport.cpp libebpfflow.a Makefile libb64.a
g++ $(CFLAGS) ebpflowexport.cpp -o $@ libebpfflow.a libb64.a $(LIBS)

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

clean:
/bin/rm -f *~ container_info.a libebpfflow.a *.o ebpflow.ebpf.enc
/bin/rm -f *~ container_info.a libebpfflow.a *.o ebpflow.ebpf.enc libb64.a
50 changes: 9 additions & 41 deletions ebpf_flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,14 @@
#include <unistd.h>
#include <linux/version.h>

#define BUFFERSIZE 1024
#include <b64/decode.h>
#include "ebpflow.ebpf.enc"

static ContainerInfo cinfo;

/* ******************************************* */

std::string b64decode(const void* data, const size_t len) {
unsigned char* p = (unsigned char*)data;
int pad = len > 0 && (len % 4 || p[len - 1] == '=');
const size_t L = ((len + 3) / 4 - pad) * 4;
std::string str(L / 4 * 3 + pad, '\0');
const int B64index[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0,
0, 0, 0, 63, 0, 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 };

for(size_t i = 0, j = 0; i < L; i += 4) {
int n = B64index[p[i]] << 18 |
B64index[p[i + 1]] << 12 |
B64index[p[i + 2]] << 6 |
B64index[p[i + 3]];
str[j++] = n >> 16;
str[j++] = n >> 8 & 0xFF;
str[j++] = n & 0xFF;
}

if(pad) {
int n = B64index[p[L]] << 18 | B64index[p[L + 1]] << 12;
str[str.size() - 1] = n >> 16;

if(len > L + 2 && p[L + 2] != '=') {
n |= B64index[p[L + 2]] << 6;
str.push_back(n >> 8 & 0xFF);
}
}

return str;
}

/* ******************************************* */
/* ******************************************* */

static int attachEBPFTracepoint(ebpf::BPF *bpf,
const char *tracepoint, const char *probe_func) {
ebpf::StatusTuple rc = bpf->attach_tracepoint(tracepoint, probe_func);
Expand Down Expand Up @@ -117,7 +79,13 @@ extern "C" {
void* init_ebpf_flow(void *priv_ptr, eBPFHandler ebpfHandler,
ebpfRetCode *rc, u_int16_t flags) {
ebpf::BPF *bpf = NULL;
std::string code = b64decode(ebpf_code, strlen(ebpf_code));
// Decoding ebpf b64 (http://libb64.sourceforge.net)
std::stringstream ebpf_b64_s(ebpf_code);
std::stringstream code_s;
base64::decoder E;
E.decode(ebpf_b64_s, code_s);
std::string code = code_s.str().c_str();

ebpf::StatusTuple open_res(0);

// Default value is 0
Expand Down
1 change: 1 addition & 0 deletions libs/b64/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Credits: http://libb64.sourceforge.net/
29 changes: 29 additions & 0 deletions libs/b64/cdecode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
cdecode.h - c header for a base64 decoding algorithm
This is part of the libb64 project, and has been placed in the public domain.
For details, see http://sourceforge.net/projects/libb64
*/

#ifndef BASE64_CDECODE_H
#define BASE64_CDECODE_H

typedef enum
{
step_a, step_b, step_c, step_d
} base64_decodestep;

typedef struct
{
base64_decodestep step;
char plainchar;
} base64_decodestate;

void base64_init_decodestate(base64_decodestate* state_in);

int base64_decode_value(char value_in);

int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in);

#endif /* BASE64_CDECODE_H */

32 changes: 32 additions & 0 deletions libs/b64/cencode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
cencode.h - c header for a base64 encoding algorithm
This is part of the libb64 project, and has been placed in the public domain.
For details, see http://sourceforge.net/projects/libb64
*/

#ifndef BASE64_CENCODE_H
#define BASE64_CENCODE_H

typedef enum
{
step_A, step_B, step_C
} base64_encodestep;

typedef struct
{
base64_encodestep step;
char result;
int stepcount;
} base64_encodestate;

void base64_init_encodestate(base64_encodestate* state_in);

char base64_encode_value(char value_in);

int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in);

int base64_encode_blockend(char* code_out, base64_encodestate* state_in);

#endif /* BASE64_CENCODE_H */

70 changes: 70 additions & 0 deletions libs/b64/decode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// :mode=c++:
/*
decode.h - c++ wrapper for a base64 decoding algorithm
This is part of the libb64 project, and has been placed in the public domain.
For details, see http://sourceforge.net/projects/libb64
*/
#ifndef BASE64_DECODE_H
#define BASE64_DECODE_H

#include <iostream>

namespace base64
{
extern "C"
{
#include "cdecode.h"
}

struct decoder
{
base64_decodestate _state;
int _buffersize;

decoder(int buffersize_in = BUFFERSIZE)
: _buffersize(buffersize_in)
{}

int decode(char value_in)
{
return base64_decode_value(value_in);
}

int decode(const char* code_in, const int length_in, char* plaintext_out)
{
return base64_decode_block(code_in, length_in, plaintext_out, &_state);
}

void decode(std::istream& istream_in, std::ostream& ostream_in)
{
base64_init_decodestate(&_state);
//
const int N = _buffersize;
char* code = new char[N];
char* plaintext = new char[N];
int codelength;
int plainlength;

do
{
istream_in.read((char*)code, N);
codelength = istream_in.gcount();
plainlength = decode(code, codelength, plaintext);
ostream_in.write((const char*)plaintext, plainlength);
}
while (istream_in.good() && codelength > 0);
//
base64_init_decodestate(&_state);

delete [] code;
delete [] plaintext;
}
};

} // namespace base64



#endif // BASE64_DECODE_H

77 changes: 77 additions & 0 deletions libs/b64/encode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// :mode=c++:
/*
encode.h - c++ wrapper for a base64 encoding algorithm
This is part of the libb64 project, and has been placed in the public domain.
For details, see http://sourceforge.net/projects/libb64
*/
#ifndef BASE64_ENCODE_H
#define BASE64_ENCODE_H

#include <iostream>

namespace base64
{
extern "C"
{
#include "cencode.h"
}

struct encoder
{
base64_encodestate _state;
int _buffersize;

encoder(int buffersize_in = BUFFERSIZE)
: _buffersize(buffersize_in)
{}

int encode(char value_in)
{
return base64_encode_value(value_in);
}

int encode(const char* code_in, const int length_in, char* plaintext_out)
{
return base64_encode_block(code_in, length_in, plaintext_out, &_state);
}

int encode_end(char* plaintext_out)
{
return base64_encode_blockend(plaintext_out, &_state);
}

void encode(std::istream& istream_in, std::ostream& ostream_in)
{
base64_init_encodestate(&_state);
//
const int N = _buffersize;
char* plaintext = new char[N];
char* code = new char[2*N];
int plainlength;
int codelength;

do
{
istream_in.read(plaintext, N);
plainlength = istream_in.gcount();
//
codelength = encode(plaintext, plainlength, code);
ostream_in.write(code, codelength);
}
while (istream_in.good() && plainlength > 0);

codelength = encode_end(code);
ostream_in.write(code, codelength);
//
base64_init_encodestate(&_state);

delete [] code;
delete [] plaintext;
}
};

} // namespace base64

#endif // BASE64_ENCODE_H

43 changes: 43 additions & 0 deletions libs/b64/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
LIBRARIES = libb64.a

# Build flags (uncomment one)
#############################
# Release build flags
CFLAGS += -O3
#############################
# Debug build flags
#CFLAGS += -g
#############################

SOURCES = cdecode.c cencode.c

TARGETS = $(LIBRARIES)

LINK.o = gcc

CFLAGS += -Werror -pedantic
CFLAGS += -I../..

vpath %.h ../include/b64

.PHONY : clean

all: $(TARGETS) #strip

libb64.a: cencode.o cdecode.o
$(AR) $(ARFLAGS) $@ $^

strip:
strip $(BINARIES) *.exe

clean:
rm -f *.exe* *.o $(TARGETS) *.bak *~

distclean: clean
rm -f depend

depend: $(SOURCES)
makedepend -f- $(CFLAGS) $(SOURCES) 2> /dev/null 1> depend

-include depend

Loading

0 comments on commit d9fa4df

Please sign in to comment.