Initial version

This commit is contained in:
nc543 2025-09-19 20:36:01 -04:00
commit b57d85c68b
5 changed files with 136 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.code-workspace

BIN
binaryEditor Executable file

Binary file not shown.

82
binaryEditor.c Normal file
View File

@ -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);
}
}

48
binaryEditor.h Normal file
View File

@ -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

5
makefile Normal file
View File

@ -0,0 +1,5 @@
all:
gcc -Wall binaryEditor.c -o binaryEditor
clean:
rm -f binaryEditor