commit b57d85c68b6a93561155a9f3aca4d3c0a672cd86 Author: nc543 Date: Fri Sep 19 20:36:01 2025 -0400 Initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1bb4614 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.code-workspace \ No newline at end of file diff --git a/binaryEditor b/binaryEditor new file mode 100755 index 0000000..6d90aff Binary files /dev/null and b/binaryEditor differ diff --git a/binaryEditor.c b/binaryEditor.c new file mode 100644 index 0000000..0277063 --- /dev/null +++ b/binaryEditor.c @@ -0,0 +1,82 @@ +#include "binaryEditor.h" + +char *input; +unsigned int inByteOffset = 0; +unsigned short inBitOffset = 0; + +char *output; +unsigned int outByteOffset = 0; +unsigned short outBitOffset = 0; + +void setBuffer(Buffer buffer, void *data){ + switch (buffer){ + case BUF_IN: + input = (char *) data; + inByteOffset = 0; + inBitOffset = 0; + break; + case BUF_OUT: + output = (char *) data; + outByteOffset = 0; + outBitOffset = 0; + break; + } +} + +void seekBuffer(Buffer buffer, unsigned int position){ + switch (buffer){ + case BUF_IN: + inByteOffset = position - (position % 8); + inBitOffset = position % 8; + break; + case BUF_OUT: + outByteOffset = position - (position % 8); + inBitOffset = position % 8; + break; + } +} + +unsigned int getBufferOffset(Buffer buffer){ + switch (buffer){ + case BUF_IN: + return (inByteOffset * 8) + inBitOffset; + case BUF_OUT: + return (outByteOffset * 8) + outBitOffset; + default: + return 0; + } +} + +char readBit(){ + char result = (input[inByteOffset] & (((char) 1) << ( 7 - inBitOffset))) >> (7 - inBitOffset); + inBitOffset++; + if (inBitOffset >= 8){ + inByteOffset++; + inBitOffset = 0; + } + return result; +} + +char readByte(){ + char result = 0; + for (int i = 0; i < 8; i++){ + unsigned short bit = readBit(); + result += bit << (7 - i); + } + return result; +} + +void writeBit(unsigned char bit){ + output[outByteOffset] += bit << (7 - outBitOffset); + outBitOffset++; + if (outBitOffset >= 8){ + outByteOffset++; + outBitOffset = 0; + } +} + +void writeByte(unsigned char byte){ + for (int i = 7; i >= 0; i--){ + writeBit((byte & (1 << i)) >> i); + } +} diff --git a/binaryEditor.h b/binaryEditor.h new file mode 100644 index 0000000..826661e --- /dev/null +++ b/binaryEditor.h @@ -0,0 +1,48 @@ +#ifndef BINARYEDITOR +#define BINARYEDITOR + +typedef enum{ + BUF_IN, + BUF_OUT +} Buffer; + +/** +Allows setting the input and output buffers for the binary editor. +*/ +void setBuffer(Buffer buffer, void *data); + +/** +Allows the changing of the read/write position in the given buffer. +position is specified in bits. +*/ +void seekBuffer(Buffer buffer, unsigned int position); + +/** +Returns what the current offset of a buffer is, in bits. +It gives the index of the next bit to be read/written +*/ +unsigned int getBufferOffset(Buffer buffer); + +/** +Returns the next bit from the read buffer. +*/ +char readBit(); + +/** +Returns the next 8 bits from the read buffer. +This is equivalent to calling readBit() 8 times. +*/ +char readByte(); + +/** +This writes a single bit to the output buffer +*/ +void writeBit(unsigned char bit); + +/** +This writes one byte to the output buffer. +This is equivalent to calling writeBit() 8 times. +*/ +void writeByte(unsigned char byte); + +#endif \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..fdccc3a --- /dev/null +++ b/makefile @@ -0,0 +1,5 @@ +all: + gcc -Wall binaryEditor.c -o binaryEditor + +clean: + rm -f binaryEditor \ No newline at end of file