This commit is contained in:
Balhau 2024-06-28 09:09:03 +01:00
commit 41bdbbbe12
34 changed files with 1356 additions and 0 deletions

6
.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
.vscode/
CGuidra*
bin/
data/
*.readelf
*.class

95
Makefile Normal file
View file

@ -0,0 +1,95 @@
include defs/makefile.def
make: c-x86-debug cpp-x86-debug c-x86-release cpp-x86-release go gosilly make-so
make-so:${SO_DIR}/*.c
@mkdir -p ${LIB_DIR}
@for file in $? ; do \
path=$$(echo "$$file" | cut -f 2 -d '/'); \
out=$$(echo "$$path" | cut -f 1 -d '.'); \
${CC} ${CFLAG_LIB} $$file -o ${LIB_DIR}/$$out.o;\
${CC} -shared -o ${LIB_DIR}/$$out.so ${LIB_DIR}/$$out.o -ldl; \
done
gosilly: libsilly.so silly.o
CGO_CFLAGS=${CGO_CFGLAGS_DYN} \
CGO_LDFLAGS=${CGO_LDFLAGS_DYN} \
${GO} build -o ${BIN_DIR}/silly.go golangc/silly.go
.PHONY: libsilly.so
libsilly.so: silly.o
@rm -f ${BIN_DIR}/libsilly.so
@mkdir -p ${LIB_DIR}
${CC} -shared -o ${LIB_DIR}/libsilly.so ${BIN_DIR}/silly.o
@ln -s ../${LIB_DIR}/libsilly.so ${BIN_DIR}/libsilly.so
.PHONY: silly.o
silly.o: golangc/silly.c
@mkdir -p ${BIN_DIR}
${CC} ${CFLAG_LIB} $? -o ${BIN_DIR}/silly.o
c-x86-debug:${C_LANG_DIR}/*.c
@mkdir -p ${BIN_DIR}
@for file in $? ; do \
path=$$(echo "$$file" | cut -f 2 -d '/'); \
out=$$(echo "$$path" | cut -f 1 -d '.'); \
echo ${CC} ${CFLAG_DEBUG} -o ${BIN_DIR}/$$out.c.debug.${ARCH_X86} $$file; \
${CC} ${CFLAG_DEBUG} -o ${BIN_DIR}/$$out.c.debug.${ARCH_X86} $$file -lpthread;\
done
cpp-x86-debug:${CPP_LANG_DIR}/*.cpp
@mkdir -p ${BIN_DIR}
@for file in $? ; do \
path=$$(echo "$$file" | cut -f 2 -d '/'); \
out=$$(echo "$$path" | cut -f 1 -d '.'); \
echo ${CPP} ${CFLAG_DEBUG} -o ${BIN_DIR}/$$out.cpp.debug.${ARCH_X86} $$file; \
${CPP} ${CFLAG_DEBUG} -o ${BIN_DIR}/$$out.cpp.debug.${ARCH_X86} $$file; \
done
c-x86-release:${C_LANG_DIR}/*.c
@mkdir -p ${BIN_DIR}
@for file in $? ; do \
path=$$(echo "$$file" | cut -f 2 -d '/'); \
out=$$(echo "$$path" | cut -f 1 -d '.'); \
echo ${CC} ${CFLAG_RELEASE} -o ${BIN_DIR}/$$out.c.release.${ARCH_X86} $$file; \
${CC} ${CFLAG_RELEASE} -o ${BIN_DIR}/$$out.c.release.${ARCH_X86} $$file; \
done
cpp-x86-release:${CPP_LANG_DIR}/*.cpp
@mkdir -p ${BIN_DIR}
@for file in $? ; do \
path=$$(echo "$$file" | cut -f 2 -d '/'); \
out=$$(echo "$$path" | cut -f 1 -d '.'); \
echo ${CPP} ${CFLAG_RELEASE} -o ${BIN_DIR}/$$out.cpp.release.${ARCH_X86} $$file; \
${CPP} ${CFLAG_RELEASE} -o ${BIN_DIR}/$$out.cpp.release.${ARCH_X86} $$file; \
done
go:${GO_LANG_DIR}/*.go
@mkdir -p ${BIN_DIR}
@for file in $? ; do \
path=$$(echo "$$file" | cut -f 2 -d '/'); \
echo ${GO} build ${GO_FLAGS} -o ${BIN_DIR}/$$path $$file; \
${GO} build ${GO_FLAGS} -o ${BIN_DIR}/$$path $$file; \
done
c-arm:${C_LANG_DIR}/*.c
@mkdir -p ${BIN_DIR}
@for file in $? ; do \
path=$$(echo "$$file" | cut -f 2 -d '/'); \
out=$$(echo "$$path" | cut -f 1 -d '.'); \
echo ${CC} ${CFLAG} -o ${BIN_DIR}/$$out.c.${ARCH} $$file; \
${CC_ARM} ${ARM_CFLAG} -o ${BIN_DIR}/$$out.c.${ARCH_ARM} $$file; \
done
c-asm-x86:
@mkdir -p ${ASM_X86_OUT}
${CC} ${ASM_C_FLAGS} ${C_LANG_DIR}/*.c
@mv *.s ${ASM_X86_OUT}
c-asm-arm:
@mkdir -p ${ASM_ARM_OUT}
${CC_ARM} ${ASM_ARM_C_FLAGS} ${C_LANG_DIR}/*.c
@mv *.s ${ASM_ARM_OUT}
clean:
@rm -rf ${BIN_DIR} ${ASM_ARM_OUT} ${ASM_X86_OUT} ${LIB_DIR}

26
README.md Normal file
View file

@ -0,0 +1,26 @@
# Additional Info
Install arm toolchain on debian linux (assuming x86 environment).
```shell
sudo apt update -y && sudo apt upgrade -y
sudo apt install qemu-user qemu-user-static gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu binutils-aarch64-linux-gnu-dbg build-essential
```
## Utility commands
Convert hex to dec in shell
```shell
echo $((16#07c6))
1990
```
## Resources
* [Go and GDB](https://go.dev/doc/gdb)
* [Go morestack_noctxt](https://blog.leexun.tw/go-runtime-morestack_noctxt)
* [.rodata segment](https://guyonbits.com/from-rodata-to-rwdata-introduction-to-memory-mapping-and-ld-scripts/)
* [mprotect](https://man7.org/linux/man-pages/man2/mprotect.2.html)
* [Makefile Automatic Variables](https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html)

11
c/beforeafter.c Normal file
View file

@ -0,0 +1,11 @@
#include <stdio.h>
// Before attribute
#define BEFORE_ATR __attribute__((constructor))
// After attribute
#define AFTER_ATR __attribute__((destructor))
void BEFORE_ATR beforeFunc() { printf("Executed before main\n"); }
void AFTER_ATR afterFunc() { printf("Executed after main\n"); }
int main(void) { printf("Executed in main\n"); }

63
c/cards.c Normal file
View file

@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#define ToString(a) #a
typedef enum Naipe { Ouro, Espada, Pau, Copa } Naipe;
typedef enum Cara {
Duque,
Terno,
Quadra,
Quina,
Sexta,
Dama,
Valete,
Rei,
Visca,
As
} Cara;
typedef struct Carta {
Naipe naipe;
Cara cara;
int valor;
} Carta;
const Carta cartas[] = {{.naipe = Copa, .cara = Duque, .valor = 0},
{.naipe = Copa, .cara = Terno, .valor = 0},
{.naipe = Copa, .cara = Quadra, .valor = 0},
{.naipe = Copa, .cara = Quina, .valor = 0},
{.naipe = Copa, .cara = Sexta, .valor = 0},
{.naipe = Copa, .cara = Dama, .valor = 2},
{.naipe = Copa, .cara = Valete, .valor = 3},
{.naipe = Copa, .cara = Rei, .valor = 4},
{.naipe = Copa, .cara = Visca, .valor = 10},
{.naipe = Copa, .cara = As, .valor = 11}};
void printCarta(Carta carta) {
printf("Carta: cara=%s,naipe=%s,valor=%d", ToString(carta.cara),
ToString(carta.naipe), carta.valor);
}
#define __S(X) #X
#define _S(X) __S(X)
struct Point {
int x;
int y;
};
#define CAGALHAO 1
int main(void) {
struct Point pontos[] = {{1, 2}, {3, 4}};
printf("Hello cards\n");
printCarta(cartas[0]);
printf("Hello bostinha: " _S(1) "\n");
printf("Cagalhao: " __S(CAGALHAO) "\n");
printf("Cagalhao: " _S(CAGALHAO) "\n");
printf("Size array: %d", sizeof(pontos) / sizeof(pontos[0]));
}

200
c/ex1.c Normal file
View file

@ -0,0 +1,200 @@
#include <stdio.h>
/**
* This file aims to help understanding calling conventions made by gcc
* Arch: x86_64
*
* The conclusions here are only valid for gcc compilers
*/
/***
* Calling convention intel x86
* movl eax, 0
*
* Calling convention arm arch64
* empty preamble
*/
int f00()
{
return 7;
}
void fl(char* str){
printf("%s",str);
}
/***
* Calling convention intel x86
* movl eax, 0
*
* Calling convention arm arch64
* empty preamble
*/
char f01()
{
return 0;
}
/**
* Calling convention intel x86
* movl edi, a -> f1(edi)
*
* Calling convention arm arch64
*
* subtract 16 bytes in the stack for arguments
* fetch from sp -12 (12=-16+4)
*
*/
int f1(int a)
{
return a + 1;
}
/**
* Calling convention intel x86
* movl edi, a -> f1(edi)
*
* Calling convention arm arch64
*
* subtract 16 bytes in the stack for arguments
* fetch from sp -8 (12=-16+8)
*
*/
long f1l(long a)
{
return a+1;
}
/**
* Calling convention
* movl esi, b
* movl edi, a
* f(edi,esi)
*/
int f2(int a, int b)
{
return a + b;
}
/**
* Calling convention
* movl edx, c
* movl esi, b
* movl edi, a
*/
int f3(int a, int b, int c)
{
return a + b - c;
}
/**
* Calling convention
*
* movl ecx, d
* movl edx, c
* movl esi, b
* movl edi, a
*/
int f4(int a, int b, int c, int d)
{
return (a + b) - (c + d);
}
/**
* Calling convention
*
* movl r8d, e
* movl ecx, d
* movl edx, c
* movl esi, b
* movl edi, a
*/
int f5(int a, int b, int c, int d, int e)
{
return (a + b) - (c + d) + e;
}
/**
* Calling convention
*
* movl r9d, f
* movl r8d, e
* movl ecx, d
* movl edx, c
* movl esi, b
* movl edi, a
*/
int f6(int a, int b, int c, int d, int e, int f)
{
int sum1 = a + b + c;
int sum2 = d + e + f;
int mult = sum1 * sum2;
return mult;
}
/**
* Calling convention
*
* push h
* movl r9d, f
* movl r8d, e
* movl ecx, d
* movl edx, c
* movl esi, b
* movl edi, a
*
* Note: After the call we will have
* addq rsp 8 --> Revert the push into stack operation of size long
*/
int f7(int a, int b, int c, int d, int e, int f, int h)
{
return a + b - c + d - e + f - h;
}
/**
* Calling convention
*
* push j
* push h
* movl r9d, f
* movl r8d, e
* movl ecx, d
* movl edx, c
* movl esi, b
* movl edi, a
*
* Note: After the call we have
* addq rsp 16 --> Revert the two push into stack operations (by size long)
*/
int f8(int a, int b, int c, int d, int e, int f, int h, int j)
{
return a + b + c - d - e - f - h + j;
}
long fl0(long int a)
{
return a * a;
}
int main()
{
fl("Hello Master");
f00();
f01();
f1(1);
f1l(1);
f2(1, 2);
f3(1, 2, 3);
f4(1, 2, 3, 4);
f5(1, 2, 3, 4, 5);
f6(1, 2, 3, 4, 5, 6);
f7(1, 2, 3, 4, 5, 6, 7);
f8(1, 2, 3, 4, 5, 6, 7, 8);
fl0(10L);
return 0;
}

57
c/ex2.c Normal file
View file

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
/**
* Return into
* eax
*/
unsigned int f0(unsigned int a){
return 2*a;
}
/**
* Return into
* rax
*/
unsigned long f1(unsigned long a){
return 2*a;
}
unsigned short f2(unsigned short a){
return (short)(2*a);
}
void fp0(int *a){
*a=2*(*a);
}
void fp1(long *a){
*a=2*(*a);
}
int main(){
printf("Hello f0: %d\n",f0(1));
printf("Hello f1: %ld\n",f1(2));
printf("Hello f2: %d\n",f2(2));
int* ip = malloc(sizeof(int));
long* lp = malloc(sizeof(long));
*ip=12;
*lp=21;
printf("IPointer:\t %p, %d\n",ip, *ip);
printf("LPointer:\t %p, %ld\n",lp, *lp);
fp0(ip);
fp1(lp);
printf("IPointer:\t %p, %d\n",ip, *ip);
printf("LPointer:\t %p, %ld\n",lp, *lp);
fp0(ip);
fp1(lp);
free(ip);
free(lp);
}

18
c/ex3.c Normal file
View file

@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
printf("Arg Len: %d\n",argc);
int input = strtol(argv[1],NULL,16);
printf("Secret inserted: %d\n", input);
int secret = 0xcafebabe;
if (secret == input)
{
printf("Great success\n");
}
else
{
printf("Oh boy...\n");
}
}

51
c/ex4.c Normal file
View file

@ -0,0 +1,51 @@
#include <stdio.h>
typedef struct DateOfBirth
{
int Day;
int Month;
int Year;
} DateOfBirth;
typedef struct User
{
char *Name;
char *Location;
DateOfBirth DateOfBirth;
} User;
void printUser(User user)
{
printf("Name: %s\n Location: %s, DateOfBirth: %d/%d/%d",
user.Name,
user.Location,
user.DateOfBirth.Day,
user.DateOfBirth.Month,
user.DateOfBirth.Year);
}
void printUserByRef(User *user)
{
printf("Name: %s\n Location: %s, DateOfBirth: %d/%d/%d",
user->Name,
user->Location,
user->DateOfBirth.Day,
user->DateOfBirth.Month,
user->DateOfBirth.Year);
}
int main(int argc, char **argv)
{
User user = {
.Name = "John Doe",
.Location = "Los Alamos",
.DateOfBirth = {
.Day = 1,
.Month = 12,
.Year = 1990}};
printUser(user);
printUserByRef(&user);
return 0;
}

22
c/ex5.c Normal file
View file

@ -0,0 +1,22 @@
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
int main(void)
{
char *data1 = "HELLO";
char data2[] = "Hello Master";
if (mprotect(data1, strlen(data1) + 1, PROT_WRITE) == 0)
{
data1[0] = (char)'F';
printf("Data1 is writable: %s, %p, %ld chars\n", data1, data1, strlen(data1));
printf("Data2 is writable: %s, %p, %ld chars\n", data2, data2, strlen(data2));
}
else
{
data2[0] = (char)'F';
printf("Data1 is not writable: %s, %p, %ld chars\n", data1, data1, strlen(data1));
printf("Data1 is not writable: %s, %p, %ld chars\n", data2, data2, strlen(data2));
}
}

28
c/ex6.c Normal file
View file

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
int sum(int *array, int len)
{
int i = 0;
int *p = array;
int sum = 0;
for (i = 0; i < len; i++)
{
sum += *p;
p = p + 1;
}
return sum;
}
#define ALPHA_SIZE 10
char alpha[] = {'A','B','H','C','E','F','0','1','5','6'};
int main(int argc, char** argv)
{
int array[] = {1, 2, 3, 4, 5};
int s = sum(array, 5);
int mul = atoi(argv[1]);
int ch_p = (s*mul) % ALPHA_SIZE;
printf("Sum: %d %c\n", s, (int)alpha[ch_p]);
}

46
c/ex7.c Normal file
View file

@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#define log_2d(pointer) \
printf("Value: %p, %u, %u\n", pointer, pointer->x, pointer->y)
#define log_3d(pointer) \
printf("Value: %p, %u, %u, %u\n", pointer, pointer->x, pointer->y, \
pointer->z)
typedef struct Point2D {
int x, y;
} Point2D;
typedef struct Point3D {
int x, y, z;
} Point3D;
int main(void) {
printf("Allocation Thinkering: \n");
Point2D *heap_point_2d = malloc(sizeof(Point2D));
Point2D *heap_point_2d_2 = malloc(sizeof(Point2D));
Point3D *heap_point_3d;
log_2d(heap_point_2d);
*heap_point_2d = (Point2D){1, 2};
*heap_point_2d_2 = (Point2D){3, 4};
heap_point_3d = (Point3D *)(heap_point_2d);
log_3d(heap_point_3d);
log_2d(heap_point_2d);
log_2d(heap_point_2d_2);
free(heap_point_2d);
log_2d(heap_point_2d);
log_2d(heap_point_2d_2);
log_3d(heap_point_3d);
heap_point_2d = malloc(sizeof(Point2D));
*heap_point_2d = (Point2D){5, 6};
log_2d(heap_point_2d);
log_2d(heap_point_2d_2);
log_3d(heap_point_3d);
free(heap_point_2d_2);
log_2d(heap_point_2d);
log_2d(heap_point_2d_2);
log_3d(heap_point_3d);
exit(0);
}

22
c/forkhandler.c Normal file
View file

@ -0,0 +1,22 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void prepare() { printf("preparing for fork...\n"); }
void parent() { printf("in parent after fork...\n"); }
void child() { printf("in child after fork...\n"); }
int main() {
pthread_atfork(prepare, parent, child);
pid_t pid = fork();
if (pid == 0) {
// in child process
printf("I am the child!\n");
} else {
// in parent process
printf("I am the parent!\n");
}
return 0;
}

10
c/pthreadattrdestroy.c Normal file
View file

@ -0,0 +1,10 @@
#include <pthread.h>
#include <stdio.h>
int main() {
pthread_attr_t attr;
pthread_attr_init(&attr);
// use the attribute object...
pthread_attr_destroy(&attr);
return 0;
}

21
c/pthreadex1.c Normal file
View file

@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
void prepare() { printf("preparing for fork...\n"); }
void parent() { printf("in parent after fork...\n"); }
void child() { printf("in child after fork...\n"); }
int main() {
__register_atfork(prepare, parent, child);
pid_t pid = fork();
if (pid == 0) {
// in child process
printf("I am the child!\n");
} else {
// in parent process
printf("I am the parent!\n");
}
return 0;
}

18
c/pthreadex2.c Normal file
View file

@ -0,0 +1,18 @@
#include <pthread.h>
#include <stdio.h>
void *thread_func(void *data) {
printf("Hello from the new thread!\n");
return NULL;
}
int main() {
pthread_t thread;
int result = pthread_create(&thread, NULL, thread_func, NULL);
if (result != 0) {
printf("Error creating thread: %d\n", result);
return 1;
}
printf("Hello from the main thread!\n");
return 0;
}

34
c/pthreadex3.c Normal file
View file

@ -0,0 +1,34 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct coco {
char *name;
} Coco;
char *COCO_NAME = "COCO";
void *thread_func(void *data) {
printf("Hello from the new thread!\n");
Coco *c = malloc(sizeof(Coco));
c->name = COCO_NAME;
return c;
}
int main() {
pthread_t thread;
void *ret;
int result = pthread_create(&thread, NULL, thread_func, NULL);
if (result != 0) {
printf("Error creating thread: %d\n", result);
return 1;
}
result = pthread_join(thread, &ret);
if (result != 0) {
printf("Error joining thread: %d\n", result);
return 1;
}
printf("Hello from the main thread!\n");
printf("Return Value: %s", ((Coco *)(ret))->name);
return 0;
}

29
c/pthreadex4.c Normal file
View file

@ -0,0 +1,29 @@
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *thread_func(void *data) {
sleep(1);
printf("Hello from the new thread!\n");
return NULL;
}
int main() {
pthread_t thread;
int result = pthread_create(&thread, NULL, thread_func, NULL);
if (result != 0) {
printf("Error creating thread: %d\n", result);
return 1;
}
while (1) {
result = pthread_tryjoin_np(thread, NULL);
if (result == 0) {
// thread has finished
break;
}
printf("Waiting for thread to finish...\n");
sleep(1);
}
printf("Hello from the main thread!\n");
return 0;
}

42
c/pthreadex5.c Normal file
View file

@ -0,0 +1,42 @@
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void *thread_func(void *arg) {
// Sleep for a random amount of time between 1 and 5 seconds
int sleep_time = 1 + rand() % 5;
sleep(sleep_time);
return arg;
}
int main() {
pthread_t thread;
int thread_arg = 100;
// Create the thread
pthread_create(&thread, NULL, thread_func, &thread_arg);
// Set up a timeout for the pthread_timedjoin_np function
struct timespec timeout;
clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += 3; // Wait for at most 3 seconds
void *result;
int ret;
// Wait for the thread to finish, with a timeout
ret = pthread_timedjoin_np(thread, &result, &timeout);
if (ret == 0) {
printf("Thread finished within the timeout\n");
} else if (ret == ETIMEDOUT) {
printf("Timed out waiting for thread to finish\n");
} else {
printf("Error waiting for thread to finish %d\n", ret);
}
return 0;
}

40
c/pthreadex6.c Normal file
View file

@ -0,0 +1,40 @@
#include <pthread.h>
#include <stdio.h>
void *thread_func(void *arg) {
pthread_t thread_id = pthread_self();
printf("Inside thread_func, thread ID = %ld\n", thread_id);
// Return the thread ID as the thread's return value
return (void *)thread_id;
}
int main() {
pthread_t thread1, thread2;
int thread1_arg = 10, thread2_arg = 20;
// Create two threads
pthread_create(&thread1, NULL, thread_func, &thread1_arg);
pthread_create(&thread2, NULL, thread_func, &thread2_arg);
void *thread1_result, *thread2_result;
// Wait for the threads to finish
pthread_join(thread1, &thread1_result);
pthread_join(thread2, &thread2_result);
// Compare the thread IDs
if (pthread_equal(thread1, thread2)) {
printf("Thread IDs are equal\n");
} else {
printf("Thread IDs are not equal\n");
}
// Print the thread IDs and return values
printf("Thread 1 ID = %ld, return value = %ld\n", thread1,
(long)thread1_result);
printf("Thread 2 ID = %ld, return value = %ld\n", thread2,
(long)thread2_result);
return 0;
}

176
c/sha3.c Normal file
View file

@ -0,0 +1,176 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define DIGEST 32 // 256-bit digest in bytes.
#define ROUNDS 24 // Number of KECCAK rounds to perform for SHA3-256.
#define WIDTH 200 // 1600-bit width in bytes.
#define LANES 25 // The state is an unrolled 5x5 array of 64-bit lanes.
#define RATE 136 // 1600-bit width - 512-bit capacity in bytes.
#define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
typedef unsigned long long uint64_t;
typedef unsigned char uint8_t;
union state {
uint64_t words[LANES];
uint8_t bytes[WIDTH];
};
static void theta(void *arg) {
union state *state = (union state*) arg;
uint64_t C[5] = { 0 };
uint64_t D[5] = { 0 };
for (int i = 0; i < 5; i++) {
C[i] = state->words[i];
C[i] ^= state->words[i + 5];
C[i] ^= state->words[i + 10];
C[i] ^= state->words[i + 15];
C[i] ^= state->words[i + 20];
}
for (int i = 0; i < 5; i++) {
D[i] = C[(i + 4) % 5] ^ ROTL64(C[(i + 1) % 5], 1);
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 25; j += 5) {
state->words[i + j] ^= D[i];
}
}
}
static void rho(void *arg) {
union state *state = (union state*) arg;
const int rotations[25] = {
0, 1, 62, 28, 27,
36, 44, 6, 55, 20,
3, 10, 43, 25, 39,
41, 45, 15, 21, 8,
18, 2, 61, 56, 14
};
for (int i = 0; i < 25; i++) {
state->words[i] = ROTL64(state->words[i], rotations[i]);
}
}
static void pi(void *arg) {
union state *state = (union state*) arg;
const int switcheroo[25] = {
0, 10, 20, 5, 15,
16, 1, 11, 21, 6,
7, 17, 2, 12, 22,
23, 8, 18, 3, 13,
14, 24, 9, 19, 4
};
uint64_t temp[25] = { 0 };
for (int i = 0; i < 25; i++) {
temp[i] = state->words[i];
}
for (int i = 0; i < 25; i++) {
state->words[switcheroo[i]] = temp[i];
}
}
static void chi(void *arg) {
union state *state = (union state*) arg;
uint64_t temp[5] = { 0 };
for (int j = 0; j < 25; j += 5) {
for (int i = 0; i < 5; i++) {
temp[i] = state->words[i + j];
}
for (int i = 0; i < 5; i++) {
state->words[i + j] ^= (~temp[(i + 1) % 5]) & temp[(i + 2) % 5];
}
}
}
static void iota(const uint8_t round, void *arg) {
union state *state = (union state*) arg;
const uint64_t constants[24] = {
0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
0x8000000000008080, 0x0000000080000001, 0x8000000080008008
};
state->words[0] ^= constants[round];
}
static void keccak(void *arg) {
union state *state = (union state*) arg;
for (int round = 0; round < ROUNDS; round++) {
theta(state); rho(state); pi(state); chi(state); iota(round,state);
}
}
static int absorb(const uint64_t len, const uint8_t data[len], int absorbed, void *arg) {
union state *state = (union state*) arg;
for (uint64_t i = 0; i < len; i++) {
state->bytes[absorbed++] ^= data[i];
if (absorbed == RATE) {
keccak(state);
absorbed = 0;
}
}
return absorbed;
}
static void squeeze(uint8_t digest[DIGEST], int padpoint, void *arg) {
union state *state = (union state*) arg;
state->bytes[padpoint] ^= 0x06;
state->bytes[RATE - 1] ^= 0x80;
keccak(state);
for (int i = 0; i < DIGEST; i++) {
digest[i] = state->bytes[i];
}
}
void sha3_hash(uint8_t digest[DIGEST], const uint64_t len, const uint8_t data[len]) {
union state *state = malloc(sizeof(union state));
memset(state,'\0',sizeof(union state)); // absolutely necessary
int absorbed = 0;
int padpoint = absorb(len, data, absorbed, state);
squeeze(digest, padpoint, state);
free(state); state = NULL;
}
void printDig(uint8_t* cPtr, uint64_t len){
uint64_t i=0;
uint8_t *p=cPtr;
for (i=0;i<len;i++){
printf("%x",*p);
p++;
}
}
#define DATA_LEN 32
int main(){
//Dummy digest
uint8_t dig[DIGEST] = {
0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,
0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,
0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,
0xA,0xB
};
uint8_t data[DATA_LEN]={
0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,
0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,
0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,
0xA,0xC
};
sha3_hash(dig,DATA_LEN,data);
printDig(dig,DATA_LEN);
return 0;
}

26
c/socket_ex.c Normal file
View file

@ -0,0 +1,26 @@
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define SECRET "MySecretThought"
int main(int argc, char **argv) {
if (argc > 1) {
printf("Your secret: %s\n", argv[1]);
int r = strcmp(argv[1], SECRET);
if (r != 0) {
printf("Secret is invalid\n");
} else {
printf("Yay secret is valid\n");
}
} else {
printf("You need to provide a secret as an argument\n");
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
return fd;
}

8
cpp/ex1.cpp Normal file
View file

@ -0,0 +1,8 @@
#include <iostream>
#include <iconv.h>
int main(){
std::cout << "Quem es tu miuda" << std::endl;
std::cout << "Hello world" << std::endl;
return 0;
}

21
cpp/ex2.cpp Normal file
View file

@ -0,0 +1,21 @@
#include <iostream>
namespace World{
namespace Country{
class Citizen {
private:
int number;
public:
Citizen(int _number): number(_number){};
int getNumber(){
return this->number;
};
};
}
}
int main(void){
World::Country::Citizen john = World::Country::Citizen(12);
std::cout << john.getNumber() << std::endl;
}

26
defs/makefile.def Normal file
View file

@ -0,0 +1,26 @@
CFLAG_DEBUG_STATIC=-Wall -g -static
CFLAG_DEBUG=-Wall -g
CFLAG_RELEASE=-Wall -O3 -static
CFLAG_LIB=-c -Wall -Werror -fpic
CC=gcc
CPP=g++
CC_ARM=aarch64-linux-gnu-gcc
ARM_CFLAG=-Wall -g -static
ASM_C_FLAGS=-S #-masm=intel
ASM_ARM_C_FLAGS=-S
GO=go
BIN_DIR=bin
LIB_DIR=lib
GO_FLAGS=-gcflags=all="-N -l"
ARCH_X86=x86
ARCH_ARM=aarch64
GO_LANG_DIR=golang
CPP_LANG_DIR=cpp
C_LANG_DIR=c
ASM_ARM_OUT=asm/arm/
ASM_X86_OUT=asm/x86/
SO_DIR=so
CGO_CFGLAGS_DYN="-I./golang/"
CGO_LDFLAGS_DYN="-L./${LIB_DIR}"

91
golang/calls.go Normal file
View file

@ -0,0 +1,91 @@
package main
import "fmt"
func nothing() {
}
func voidS8Bit() int8 {
return 77
}
func voidS16Bit() int16 {
return 100
}
func voidS32Bit() int32 {
return 200
}
func voidS64Bit() int64 {
return 300
}
func voidU8Bit() uint8 {
return 77
}
func voidU16Bit() uint16 {
return 100
}
func voidU32Bit() uint32 {
return 200
}
func voidU64Bit() uint64 {
return 300
}
func voidS8Pair() (int8, int8) {
return -100, 100
}
func voidS16Pair() (int16, int16) {
return -1000, 1000
}
func neg8(a int8) int8 {
return -a
}
func neg16(a int16) int16 {
return -a
}
func sum16(a int16, b int16) int32 {
return (int32)(a + b)
}
func main() {
nothing()
// Signed integers
s8 := voidS8Bit()
s16 := voidS16Bit()
s32 := voidS32Bit()
s64 := voidS64Bit()
fmt.Println(s8, s16, s32, s64)
//Unsigned integers
voidU8Bit()
voidU16Bit()
voidU32Bit()
voidU64Bit()
//One argument function
a8 := neg8(1)
b8 := neg8(-1)
//Return pairs
a8, b8 = voidS8Pair()
neg8(a8)
neg8(b8)
a16, b16 := voidS16Pair()
neg16(a16)
neg16(b16)
sum16(a16, b16)
}

15
golang/findme.go Normal file
View file

@ -0,0 +1,15 @@
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Invalid arguments number")
os.Exit(-1)
}
fmt.Println("Your secret is: ", os.Args[1])
}

8
golang/hello.go Normal file
View file

@ -0,0 +1,8 @@
package main
import "fmt"
func main() {
fmt.Println("Hello World")
fmt.Println("Holla!")
}

52
golang/structs.go Normal file
View file

@ -0,0 +1,52 @@
package main
import (
"fmt"
)
type DateOfBirth struct {
Year uint32
Month uint8
Day uint8
}
type Lord struct {
Name string
Sex string
}
type God struct {
Name string
Birth DateOfBirth
}
func main() {
fmt.Println("Hey")
name := "Jizzus"
fmt.Println(name)
dob := DateOfBirth{
Year: 0,
Month: 12,
Day: 25,
}
lord := Lord{
Name: "Jizz",
Sex: "Lots of them",
}
fmt.Println(dob)
fmt.Println(lord)
jesus := God{
Name: name,
Birth: dob,
}
fmt.Println(jesus)
}

18
golangc/silly.c Normal file
View file

@ -0,0 +1,18 @@
#include "silly.h"
const char* SILLY_AUTHOR="Silly Author <silly@author.is>";
void add(PSilly silly_one,PSilly silly_two){
silly_one->age+=silly_two->legs;
silly_one->legs+=silly_two->age;
}
void sub(PSilly silly_one, PSilly silly_two){
silly_one->age-=silly_two->legs;
silly_one->legs-=silly_two->age;
}
void print(PSilly silly){
printf("Silly:\t age:%d\t%d\n",silly->age,silly->legs);
}

23
golangc/silly.go Normal file
View file

@ -0,0 +1,23 @@
package main
/*
#cgo LDFLAGS: -lsilly
#include <silly.h>
*/
import "C"
import "fmt"
/**
* Use LD_LIBRARY_PATH=. to inject the shared object.
*/
func main() {
silly_one := C.struct_Silly{age: 12, legs: 21}
silly_two := C.struct_Silly{age: 2, legs: 4}
C.print(&silly_one)
C.print(&silly_two)
C.add(&silly_one, &silly_two)
C.print(&silly_one)
C.print(&silly_two)
fmt.Println("Silly version %d, author: %s", C.SILLY_VERSION, C.SILLY_AUTHOR)
}

17
golangc/silly.h Normal file
View file

@ -0,0 +1,17 @@
#pragma once
#include <stdio.h>
const unsigned int SILLY_VERSION=0x7;
extern const char* SILLY_AUTHOR;
typedef struct Silly{
int age;
int legs;
} Silly;
typedef Silly* PSilly;
void add(PSilly silly_one,PSilly silly_two);
void sub(PSilly silly_one,PSilly silly_two);
void print(PSilly silly);

6
java/Test.java Normal file
View file

@ -0,0 +1,6 @@
class Test{
public static void main(String[] args){
System.out.println("Hello world");
}
}

30
so/hook.c Normal file
View file

@ -0,0 +1,30 @@
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
int (*original_socket)(int, int, int) = NULL;
int (*original_strcmp)(const char *arg1, const char *arg2) = NULL;
/**
* Hook into socket function, replace the original with this hook
*/
int socket(int domain, int type, int protocol) {
original_socket = dlsym(RTLD_NEXT, "socket");
if (original_socket == NULL) {
printf("Could not hook into socket");
return -1;
}
printf("Socket function hooked");
original_socket(domain, type, protocol);
return 0;
}
/**
* Hook into strcmp function
*/
int strcmp(const char *__s1, const char *__s2) {
printf("strcmp hooked\n");
return 0;
}