2021-06-10 13:19:09 +03:00
|
|
|
#include "buffer.h"
|
2020-05-18 16:51:10 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-09-20 18:49:25 +03:00
|
|
|
typedef struct {
|
|
|
|
int size;
|
|
|
|
uint8_t data[];
|
|
|
|
} Buffer;
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
void* newBuffer(int bytes) {
|
2020-05-18 16:51:10 +03:00
|
|
|
size_t size = sizeof(Buffer) + bytes*sizeof(uint8_t);
|
|
|
|
|
|
|
|
Buffer* buf = malloc(size);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->size = bytes;
|
2020-06-11 14:42:52 +03:00
|
|
|
// memset(buf->data, 0, bytes);
|
2020-05-18 16:51:10 +03:00
|
|
|
|
|
|
|
return (void*)buf;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
void freeBuffer(void* buf) {
|
2020-05-18 16:51:10 +03:00
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
void copyBuffer(void* from, int start, int len,
|
|
|
|
void* to, int loc) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* bfrom = from;
|
|
|
|
Buffer* bto = to;
|
|
|
|
|
|
|
|
if (loc >= 0 && loc+len <= bto->size) {
|
|
|
|
memcpy(bto->data + loc, bfrom->data + start, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
int getBufferSize(void* buffer) {
|
2020-05-18 16:51:10 +03:00
|
|
|
return ((Buffer*)buffer)->size;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
void setBufferByte(void* buffer, int loc, int byte) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* b = buffer;
|
|
|
|
if (loc >= 0 && loc < b->size) {
|
|
|
|
b->data[loc] = byte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
void setBufferInt(void* buffer, int loc, int64_t val) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* b = buffer;
|
|
|
|
if (loc >= 0 && loc+3 < b->size) {
|
2020-09-19 15:32:45 +03:00
|
|
|
b->data[loc ] = val & 0xff;
|
|
|
|
b->data[loc+1] = (val >> 8) & 0xff;
|
2020-05-18 16:51:10 +03:00
|
|
|
b->data[loc+2] = (val >> 16) & 0xff;
|
|
|
|
b->data[loc+3] = (val >> 24) & 0xff;
|
2020-09-19 15:32:45 +03:00
|
|
|
b->data[loc+4] = (val >> 32) & 0xff;
|
|
|
|
b->data[loc+5] = (val >> 40) & 0xff;
|
|
|
|
b->data[loc+6] = (val >> 48) & 0xff;
|
|
|
|
b->data[loc+7] = (val >> 56) & 0xff;
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
void setBufferDouble(void* buffer, int loc, double val) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* b = buffer;
|
|
|
|
// I am not proud of this
|
|
|
|
if (loc >= 0 && loc + sizeof(double) <= b->size) {
|
|
|
|
unsigned char* c = (unsigned char*)(& val);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sizeof(double); ++i) {
|
|
|
|
b->data[loc+i] = c[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
void setBufferString(void* buffer, int loc, char* str) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* b = buffer;
|
|
|
|
int len = strlen(str);
|
|
|
|
|
|
|
|
if (loc >= 0 && loc+len <= b->size) {
|
|
|
|
memcpy((b->data)+loc, str, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:24:05 +03:00
|
|
|
size_t writeBufferData(FILE* h, void* buffer, size_t loc, size_t len) {
|
|
|
|
Buffer* b = buffer;
|
|
|
|
return fwrite(b->data + loc, sizeof(uint8_t), len, h);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
uint8_t getBufferByte(void* buffer, int loc) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* b = buffer;
|
|
|
|
if (loc >= 0 && loc < b->size) {
|
|
|
|
return b->data[loc];
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
int64_t getBufferInt(void* buffer, int loc) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* b = buffer;
|
2020-09-19 15:32:45 +03:00
|
|
|
if (loc >= 0 && loc+7 < b->size) {
|
|
|
|
int64_t result = 0;
|
|
|
|
for (size_t i=0; i<8; i++) {
|
2021-06-10 18:45:31 +03:00
|
|
|
result |= (int64_t)b->data[loc + i] << (8 * i);
|
2020-09-19 15:32:45 +03:00
|
|
|
}
|
|
|
|
return result;
|
2020-05-18 16:51:10 +03:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
double getBufferDouble(void* buffer, int loc) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* b = buffer;
|
|
|
|
double d;
|
|
|
|
// I am even less proud of this
|
|
|
|
unsigned char *c = (unsigned char*)(& d);
|
|
|
|
if (loc >= 0 && loc + sizeof(double) <= b->size) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sizeof(double); ++i) {
|
|
|
|
c[i] = b->data[loc+i];
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:19:09 +03:00
|
|
|
char* getBufferString(void* buffer, int loc, int len) {
|
2020-05-18 16:51:10 +03:00
|
|
|
Buffer* b = buffer;
|
|
|
|
char * s = (char*)(b->data + loc);
|
|
|
|
char * rs = malloc(len + 1);
|
|
|
|
strncpy(rs, s, len);
|
|
|
|
rs[len] = '\0';
|
|
|
|
return rs;
|
|
|
|
}
|
2021-06-10 13:24:05 +03:00
|
|
|
|
|
|
|
size_t readBufferData(FILE* h, void* buffer, size_t loc, size_t max) {
|
|
|
|
Buffer* b = buffer;
|
|
|
|
return fread(b->data + loc, sizeof(uint8_t), max, h);
|
|
|
|
}
|