Skip to content

Instantly share code, notes, and snippets.

@opsJson
opsJson / bit.c
Last active July 20, 2022 16:34
Easy way to set each individual bit of data of any size.
#include <stdio.h>
#include <stdarg.h>
unsigned int counter(const char *str) {
unsigned int i = 1;
while (*str) {
if (*str == ',') i++;
@opsJson
opsJson / base64.c
Created July 16, 2022 02:06
Encode and decode base64
#include <stdio.h>
#include <string.h>
#define BASE64_SIZE(str) ((4 * strlen(str) / 3) + 3) & ~3
static char const *to_base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static char const from_base64_table[] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
@opsJson
opsJson / websocket_codec.h
Last active July 15, 2022 01:17
Encode and Decode messages for webscokets.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
#define NTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
#define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
@opsJson
opsJson / webdriver.c
Last active May 29, 2022 07:16
Simple WebDriver
#include <stdio.h>
#include "./easy-curl.h" //https://gist.github.com/opsJson/6200d1fe4666b6bfa6919677a05428b0
char _WEBDRIVER_SESSION[33];
typedef enum WEBDRIVER_CODE {
WEBDRIVER_SUCCESS,
WEBDRIVER_ERROR_JAVASCRIPT,
WEBDRIVER_ERROR_NO_SESSION
} WEBDRIVER_CODE;
@opsJson
opsJson / json.c
Last active July 15, 2022 02:13
Simple JSON Parser.
#include <stdio.h>
#include <string.h>
#include <signal.h>
typedef struct json_t {
char *key;
char *value;
} json_t;
void _json_parse(int s) {
@opsJson
opsJson / easy-curl.c
Last active June 23, 2022 01:59
Easy cURL interface
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
typedef struct EasyCurlString {
int size;
char **str;
} EasyCurlString;
@opsJson
opsJson / bmp.c
Last active March 19, 2022 05:29
Easily read and write bitmaps image files.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct header {
unsigned char signature[2];
unsigned int fileSize;
unsigned int reserved;
unsigned int dataOffset;
} Header;
@opsJson
opsJson / wav.c
Last active March 19, 2022 02:52
Easily read and write WAV sound files.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct riff {
unsigned char chunkID[4];
unsigned int chunkSize;
unsigned char format[4];
} Riff;
@opsJson
opsJson / GetFunctionFromDLL.c
Last active October 12, 2023 17:54
needs to be compiled on the same DLL architecture. (32bit or 64bit)
#include <stdio.h>
#include <windows.h>
void *GetFunctionFromDll(LPCSTR DLL, LPCSTR procName) {
HMODULE hModule;
void *function;
if ((hModule = LoadLibrary(DLL)) == NULL)
{
fwprintf(stderr, L"ERROR: could not load DLL.\n");
@opsJson
opsJson / unique-ptr.c
Last active March 19, 2022 02:23
Call a destructor function when variable is out of scope.
#include <stdio.h>
#include <stdlib.h>
#define unique_ptr(destructor) __attribute__ ((cleanup(destructor)))
void destructor(void *ptr) {
void *aux = *(void**)ptr;
fclose(aux);
printf("file closed.\n");