#include <stdio.h>typedef unsigned char byte;int main(int argc, char *argv[]){ FILE *f; int out_len; //compressed length uint size; byte control; byte *data; byte *out; byte buf[4]; int buf_size; int read; int pos; int control_pos; int raw_offset; int marker; int len; int i; byte *flipped, *src, *dest; FILE *outf; if (argc != 2) { printf("lzsexpand -- Uncompress a LZS file\n"); printf("\n"); printf("USAGE: lzsepand [lzs file]\n"); } f = fopen(argv[1], "rb"); fread(buf, 4, 1, f); size = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0]; printf("Size: %d\n", size); //read in the data data = (byte*)calloc(size, 1); read = fread(data, size, 1, f); printf("Bytes read: %d\n", read); pos = 0; out_len = 0; buf_size = 4096; out = (byte*)malloc(buf_size); while (pos < size) { if (out_len + 8 * 18 > buf_size) { buf_size += 4096; out = (byte*)realloc(out, buf_size); } control = data[pos]; pos++; for (control_pos = 0; control_pos < 8; control_pos++) { if (control & 1) { out[out_len] = data[pos]; pos++; out_len++; } else { raw_offset = data[pos] | ((data[pos + 1] & 0xF0) << 4); len = (data[pos + 1] & 0xF) + 3; marker = out_len - ((out_len - 18 - raw_offset) & 0xFFF); pos += 2; for (i = 0; i < len; i++) { if (marker < 0) out[out_len] = 0x00; else { out[out_len] = out[marker]; } out_len++; marker++; } } control >>= 1; if (pos >= size) break; } } /* the lines in the bitmap are upside, after some research * this is to be expected. allocate enough memory for one * vertical line and start reversing * 640 pixels * 2 bytes per pixel = 1280 bytes */ flipped = (byte*)malloc(640 * 480 * 2); dest = flipped; src = out + 640 * 479 * 2; for (i = 0; i < 480; i++) { memcpy(dest, src, 1280); dest += 1280; src -= 1280; } free(out); out = flipped; free(data); fclose(f); outf = fopen("test.bmp", "wb"); { byte header[] = { 0x42, 0x4d, 0x36, 0xb4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //calc size real quick unsigned int fsize = 640 * 480 * 2 + sizeof(header); header[2] = fsize & 0xFF; header[3] = (fsize >> 8) & 0xFF; header[4] = (fsize >> 16) & 0xFF; header[5] = (fsize >> 24) & 0xFF; printf("Size of header %X\n", sizeof(header)); fwrite(header, sizeof(header), 1, outf); } fwrite(out, out_len, 1, outf); free(out); fclose(outf); return 0;}