I'm going to release quickbms 0.9.0 and the following are 2 examples of C code for using the 3 available IPC interfaces of "quickbms.exe -W 1234" (1234 is the port of the web API which is not covered by the example) and the quickbms_compression function of quickbms.dll:
Code: Select all
// ipctest.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
// example
char compressed_algo[] = "zlib";
unsigned char compressed_data[] =
"\x78\x01\x53\xa6\x0e\xe0\xe5\x02\xc1\xc0\xd2\xcc\xe4\x6c\x27\xdf"
"\x60\x5e\xae\xa4\x4a\x05\x9f\xd2\xcc\xf4\x4c\x05\xc7\xd2\xa2\xcc"
"\xd4\xdc\xdc\x44\x5e\xae\x54\xdd\xdc\xc4\xcc\x1c\x2b\x85\xdc\x54"
"\x87\xc4\x1c\x90\x9c\x5e\x7e\x51\x3a\x2f\x57\x79\x6a\x92\x95\x02"
"\x10\x20\x8b\x65\xe4\xe7\xa6\x82\x04\x33\x4a\x4a\x0a\xac\xf4\xf5"
"\x0b\x41\xc6\x26\xe5\x16\xeb\x25\xe7\xe7\x02\x25\x53\x73\x0a\x90"
"\x24\xab\x52\xf3\x32\x12\x2b\x20\x52\x20\x48\x35\x0f\x01\x00\xe7"
"\x38\x3d\x1c";
int compressed_size = sizeof(compressed_data) - 1;
unsigned char *decompressed_data = NULL;
int decompressed_size = 282;
int main(int argc, char *argv[]) {
HANDLE h = INVALID_HANDLE_VALUE,
h2 = INVALID_HANDLE_VALUE;
DWORD dw;
int ipc_mode,
size;
char *name,
tmp[32];
if(argc < 2) {
printf("\nUsage: %s <mode(0,1,2)>\n", argv[0]);
exit(1);
}
ipc_mode = atoi(argv[1]);
switch(ipc_mode) {
case 0: name = "\\\\.\\pipe\\quickbms_byte"; break;
case 1: name = "\\\\.\\pipe\\quickbms"; break;
case 2: name = "\\\\.\\mailslot\\quickbms\\send"; break;
default: exit(1); break;
}
printf("name %d %s\n", ipc_mode, name);
h = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
printf("handle %p\n", h);
if(h == INVALID_HANDLE_VALUE) exit(1);
switch(ipc_mode) {
case 0:
dw = PIPE_READMODE_MESSAGE;
SetNamedPipeHandleState(h, &dw, NULL, NULL);
break;
case 1:
dw = PIPE_READMODE_BYTE;
SetNamedPipeHandleState(h, &dw, NULL, NULL);
break;
case 2:
SetMailslotInfo(h, MAILSLOT_WAIT_FOREVER);
h2 = CreateMailslot("\\\\.\\mailslot\\quickbms\\recv", 0, MAILSLOT_WAIT_FOREVER, NULL);
if(h2 == INVALID_HANDLE_VALUE) exit(1);
break;
}
SetLastError(0); // useful but not necessary
sprintf(tmp, "comtype %s\n", compressed_algo);
WriteFile(h, tmp, strlen(tmp), &dw, NULL);
printf("dw %d (%d)\n", (int)dw, (int)GetLastError());
sprintf(tmp, "%d\n", compressed_size);
WriteFile(h, tmp, strlen(tmp), &dw, NULL);
printf("dw %d (%d)\n", (int)dw, (int)GetLastError());
WriteFile(h, compressed_data, compressed_size, &dw, NULL);
printf("dw %d (%d)\n", (int)dw, (int)GetLastError());
sprintf(tmp, "%d\n", decompressed_size);
WriteFile(h, tmp, strlen(tmp), &dw, NULL);
printf("dw %d (%d)\n", (int)dw, (int)GetLastError());
if(ipc_mode == 2) {
CloseHandle(h);
h = h2;
}
ReadFile(h, tmp, sizeof(tmp), &dw, NULL);
printf("dw %d (%d)\n", (int)dw, (int)GetLastError());
size = atoi(tmp);
decompressed_data = calloc(1, size);
ReadFile(h, decompressed_data, size, &dw, NULL);
printf("dw %d (%d)\n", (int)dw, (int)GetLastError());
CloseHandle(h);
fwrite(decompressed_data, 1, size, stdout);
return 0;
}
Code: Select all
// dlltest.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
// example
char compressed_algo[] = "zlib";
unsigned char compressed_data[] =
"\x78\x01\x53\xa6\x0e\xe0\xe5\x02\xc1\xc0\xd2\xcc\xe4\x6c\x27\xdf"
"\x60\x5e\xae\xa4\x4a\x05\x9f\xd2\xcc\xf4\x4c\x05\xc7\xd2\xa2\xcc"
"\xd4\xdc\xdc\x44\x5e\xae\x54\xdd\xdc\xc4\xcc\x1c\x2b\x85\xdc\x54"
"\x87\xc4\x1c\x90\x9c\x5e\x7e\x51\x3a\x2f\x57\x79\x6a\x92\x95\x02"
"\x10\x20\x8b\x65\xe4\xe7\xa6\x82\x04\x33\x4a\x4a\x0a\xac\xf4\xf5"
"\x0b\x41\xc6\x26\xe5\x16\xeb\x25\xe7\xe7\x02\x25\x53\x73\x0a\x90"
"\x24\xab\x52\xf3\x32\x12\x2b\x20\x52\x20\x48\x35\x0f\x01\x00\xe7"
"\x38\x3d\x1c";
int compressed_size = sizeof(compressed_data) - 1;
unsigned char *decompressed_data = NULL;
int decompressed_size = 282;
int __stdcall (*quickbms_compression)(char *algo, void *in, int zsize, void *out, int size) = NULL;
int main(int argc, char *argv[]) {
printf("LoadLibrary %s\n", "quickbms.dll");
HMODULE hlib = LoadLibrary("quickbms.dll");
printf("hlib %p\n", hlib);
if(!hlib) exit(1);
quickbms_compression = (void *)GetProcAddress(hlib, "quickbms_compression");
printf("quickbms_compression %p\n", quickbms_compression);
decompressed_data = calloc(1, decompressed_size);
printf("input size %d\n", compressed_size);
printf("output size %d\n", decompressed_size);
int size = quickbms_compression(compressed_algo, compressed_data, compressed_size, decompressed_data, decompressed_size);
printf("output_size %d\n", size);
if(size >= 0) {
fwrite(decompressed_data, 1, size, stdout);
}
return 0;
}
The compressed data used in the example (same for both) is the header of quickbms.txt
*edit* fixed calling convention, needs to be stdcall.