Skip to content

Instantly share code, notes, and snippets.

@Cracked5pider
Last active October 1, 2024 13:27
Show Gist options
  • Save Cracked5pider/1857e292a9fec28cba88bed80d4e509d to your computer and use it in GitHub Desktop.
Save Cracked5pider/1857e292a9fec28cba88bed80d4e509d to your computer and use it in GitHub Desktop.
#include <Buffer.h>
/*!
* Allocates an empty buffer
* @return pointer to empty buffer
*/
PVOID BufferNew( )
{
// Allocate a buffer that contains * nothing *
return ( PVOID ) LocalAlloc( LPTR, 0 );
}
/*!
* Cleans up buffer memory
* @param Buffer
*/
VOID BufferDestroy( PVOID* Buffer )
{
memset( *Buffer, 0, LocalSize( *Buffer ) );
LocalFree( *Buffer );
}
/*!
* Add raw data to the buffer
* @param Buffer
* @param Data
* @param Size
*/
VOID BufferAddRaw( PVOID* Buffer, PVOID Data, UINT32 Size )
{
UINT32 Length = 0;
Length = LocalSize( *Buffer );
*Buffer = LocalReAlloc( *Buffer, Length + Size, LMEM_MOVEABLE | LMEM_ZEROINIT );
memcpy( C_PTR( U_PTR( *Buffer ) + Length ), Data, Size );
}
/*!
* Add size + data to the buffer
* @param Buffer
* @param Data
* @param Size
* @return Buffer pointer
*/
VOID BufferAddData( PVOID* Buffer, PVOID Data, UINT32 Size )
{
BufferAddInt32( Buffer, Size );
BufferAddRaw( Buffer, Data, Size );
}
/*!
* Add an 32-bit integer to the buffer
* @param Buffer
* @param Data
*/
VOID BufferAddInt32( PVOID* Buffer, UINT32 Data )
{
BufferAddRaw( Buffer, &Data, sizeof( UINT32 ) );
}
#include <windows.h>
#define U_PTR( x ) ( UINT_PTR ) ( x )
#define C_PTR( x ) ( PVOID ) ( x )
PVOID BufferNew( );
VOID BufferDestroy( PVOID* Buffer );
VOID BufferAddRaw( PVOID* Buffer, PVOID Data, UINT32 Size );
VOID BufferAddData( PVOID* Buffer, PVOID Data, UINT32 Size );
VOID BufferAddInt32( PVOID* Buffer, UINT32 Data );
#include <Buffer.h>
VOID Main()
{
PVOID Buffer = BufferNew();
BufferAddData( &Buffer, ( PVOID ) "test", 4 );
BufferAddInt32( &Buffer, 69 );
// Do something with your buffer....
/*
for example you can send it now to your server:
Buffer that contains your data --+ +-- Size of your data
| |
HttpSendRequest( Buffer, LocalSize( Buffer ) );
*/
// Cleanup
BufferDestroy( &Buffer );
}
package parser
import (
"encoding/binary"
)
type Parser struct {
buffer []byte
}
func NewParser(buffer []byte) *Parser {
var parser = new(Parser)
parser.buffer = buffer
return parser
}
func (p *Parser) ParseInt32() int {
var integer []byte
if len(p.buffer) >= 4 {
if len(p.buffer) == 4 || len(p.buffer) == 0 {
integer = p.buffer[:len(p.buffer)]
p.buffer = []byte{}
} else {
integer = p.buffer[:len(p.buffer)-4]
p.buffer = p.buffer[4:]
}
}
return int(binary.BigEndian.Uint32(integer))
}
func (p *Parser) ParserBytes() []byte {
var bytesBuffer []byte
BytesSize := p.ParseInt32()
if BytesSize > len(p.buffer) {
bytesBuffer, p.buffer = p.buffer[:len(p.buffer)], p.buffer[len(p.buffer):]
} else {
bytesBuffer, p.buffer = p.buffer[:BytesSize], p.buffer[BytesSize:]
}
return bytesBuffer
}
func (p *Parser) ParseAtLeastBytes(NumberOfBytes int) []byte {
var bytesBuffer []byte
if NumberOfBytes > len(p.buffer) {
bytesBuffer, p.buffer = p.buffer[:len(p.buffer)], p.buffer[len(p.buffer):]
} else {
bytesBuffer, p.buffer = p.buffer[:NumberOfBytes], p.buffer[NumberOfBytes:]
}
return bytesBuffer
}
func (p *Parser) Length() int {
return len(p.buffer)
}
func (p *Parser) Buffer() []byte {
return p.buffer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment