<commit_before>#include "websocket.h"

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <poll.h>

#include <sys/stat.h>
#include <fcntl.h>

#define WS_KEY_SIZE 20

#define WS_STATE_CLEAN        0
#define WS_STATE_DISCONNECTED 1
#define WS_STATE_INITIALIZED  2
#define WS_STATE_CONNECTED    3
#define WS_STATE_SHAKEREQUEST 4


struct websocket {
	int socket;
	int state;
	struct pollfd fd;

	ws_dataCallback onData;
	void* onDataLParam;
	ws_closeCallback onClose;
	void* onCloseLParam;
};


void* ws_create() {
	struct websocket* ws = calloc(1, sizeof(struct websocket));
	ws->socket = -1;
	ws->fd.events = POLLIN;
	return ws;
}

void ws_free(void* ws) {
	ws_disconnect(ws);
	free(ws);
}

int _ws_handle_websocket(struct websocket* ws, char* data, int dataLength) {
	struct ws_header header;
	char* nextPart;
	unsigned int payloadLength;
	size_t unmaskIndex;
	uint8_t mask;
	char maskingKey[4];

	header.finalFragment = data[0] >> 7 & 0x1;
	header.opcode = data[0] & 0xf;
	mask = data[1] >> 7 & 0x1;
	payloadLength = data[1] & 0x7f;

	assert(payloadLength <= 127);

	if (payloadLength < 126) {
		header.payloadLength = payloadLength;
		nextPart = data + 2;
	} else if (payloadLength == 126) {
		header.payloadLength = *(uint16_t*)&data[2]; /* TODO: needs fix! is network byte order: convert */
		nextPart = data + 2 + 2;
	} else if (payloadLength == 127) {
		header.payloadLength = *(uint64_t*)&data[2]; /* TODO: needs fix! is network byte order: convert */
		nextPart = data + 2 + 8;
	}
	header.data = nextPart;

	if (mask != 0) {
		memcpy(maskingKey, nextPart, 4);
		header.data += 4; /* mask */
<commit_msg>fix network byte order in payload length<commit_after>#include "websocket.h"

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <poll.h>
#include <endian.h>

#include <sys/stat.h>
#include <fcntl.h>


#define WS_KEY_SIZE 20

#define WS_STATE_CLEAN        0
#define WS_STATE_DISCONNECTED 1
#define WS_STATE_INITIALIZED  2
#define WS_STATE_CONNECTED    3
#define WS_STATE_SHAKEREQUEST 4


/* endianess */
const int isNetworkByteOrder() { const int bIsNetworkByteOrder = 1 == htonl(1); return bIsNetworkByteOrder; }
#define htonll(x) (isNetworkByteOrder() ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#define ntohll(x) (isNetworkByteOrder() ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))


struct websocket {
	int socket;
	int state;
	struct pollfd fd;

	ws_dataCallback onData;
	void* onDataLParam;
	ws_closeCallback onClose;
	void* onCloseLParam;
};


void* ws_create() {
	struct websocket* ws = calloc(1, sizeof(struct websocket));
	ws->socket = -1;
	ws->fd.events = POLLIN;
	return ws;
}

void ws_free(void* ws) {
	ws_disconnect(ws);
	free(ws);
}

int _ws_handle_websocket(struct websocket* ws, char* data, int dataLength) {
	struct ws_header header;
	char* nextPart;
	unsigned int payloadLength;
	size_t unmaskIndex;
	uint8_t mask;
	char maskingKey[4];

	header.finalFragment = data[0] >> 7 & 0x1;
	header.opcode = data[0] & 0xf;
	mask = data[1] >> 7 & 0x1;
	payloadLength = data[1] & 0x7f;

	assert(payloadLength <= 127);

	if (payloadLength < 126) {
		header.payloadLength = payloadLength;
		nextPart = data + 2;
	} else if (payloadLength == 126) {
		header.payloadLength = ntohs(*(uint16_t*)&data[2]);
		nextPart = data + 2 + 2;
	} else if (payloadLength == 127) {
		header.payloadLength = ntohll(*(uint64_t*)&data[2]);
		nextPart = data + 2 + 8;
	}
	header.data = nextPart;

	if (mask != 0) {
		memcpy(maskingKey, nextPart, 4);
		header.data += 4; /* mask */

{'fraction_non_alphanumeric': 0.14798886921325574, 'fraction_numerical': 0.029344801416645586, 'mean_word_length': 4.476454293628809, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1405710', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1548, 'n_words': 379}