Skip to content
This repository
tree: 3a81e27747
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 169 lines (132 sloc) 4.769 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "abstractfile.h"
#include <xpwn/lzssfile.h>
#include <xpwn/lzss.h>
#include <xpwn/libxpwn.h>

void flipCompHeader(CompHeader* header) {
FLIPENDIAN(header->signature);
FLIPENDIAN(header->compression_type);
FLIPENDIAN(header->checksum);
FLIPENDIAN(header->length_uncompressed);
FLIPENDIAN(header->length_compressed);
}

size_t readComp(AbstractFile* file, void* data, size_t len) {
InfoComp* info = (InfoComp*) (file->data);
memcpy(data, (void*)((uint8_t*)info->buffer + (uint32_t)info->offset), len);
info->offset += (size_t)len;
return len;
}

size_t writeComp(AbstractFile* file, const void* data, size_t len) {
InfoComp* info = (InfoComp*) (file->data);

while((info->offset + (size_t)len) > info->header.length_uncompressed) {
info->header.length_uncompressed = info->offset + (size_t)len;
info->buffer = realloc(info->buffer, info->header.length_uncompressed);
}

memcpy((void*)((uint8_t*)info->buffer + (uint32_t)info->offset), data, len);
info->offset += (size_t)len;

info->dirty = TRUE;

return len;
}

int seekComp(AbstractFile* file, off_t offset) {
InfoComp* info = (InfoComp*) (file->data);
info->offset = (size_t)offset;
return 0;
}

off_t tellComp(AbstractFile* file) {
InfoComp* info = (InfoComp*) (file->data);
return (off_t)info->offset;
}

off_t getLengthComp(AbstractFile* file) {
InfoComp* info = (InfoComp*) (file->data);
return info->header.length_uncompressed;
}

void closeComp(AbstractFile* file) {
InfoComp* info = (InfoComp*) (file->data);
uint8_t *compressed;
if(info->dirty) {
info->header.checksum = lzadler32((uint8_t*)info->buffer, info->header.length_uncompressed);

compressed = malloc(info->header.length_uncompressed * 2);
info->header.length_compressed = (uint32_t)(compress_lzss(compressed, info->header.length_uncompressed * 2, info->buffer, info->header.length_uncompressed) - compressed);

info->file->seek(info->file, sizeof(info->header));
info->file->write(info->file, compressed, info->header.length_compressed);

free(compressed);

flipCompHeader(&(info->header));
info->file->seek(info->file, 0);
info->file->write(info->file, &(info->header), sizeof(info->header));
}

free(info->buffer);
info->file->close(info->file);
free(info);
free(file);
}


AbstractFile* createAbstractFileFromComp(AbstractFile* file) {
InfoComp* info;
AbstractFile* toReturn;
uint8_t *compressed;

if(!file) {
return NULL;
}

info = (InfoComp*) malloc(sizeof(InfoComp));
info->file = file;
file->seek(file, 0);
file->read(file, &(info->header), sizeof(info->header));
flipCompHeader(&(info->header));
if(info->header.signature != COMP_SIGNATURE) {
free(info);
return NULL;
}

if(info->header.compression_type != LZSS_SIGNATURE) {
free(info);
return NULL;
}

info->buffer = malloc(info->header.length_uncompressed);
compressed = malloc(info->header.length_compressed);
file->read(file, compressed, info->header.length_compressed);

uint32_t real_uncompressed = decompress_lzss(info->buffer, compressed, info->header.length_compressed);
if(real_uncompressed != info->header.length_uncompressed) {
XLOG(5, "mismatch: %d %d %d %x %x\n", info->header.length_compressed, real_uncompressed, info->header.length_uncompressed, compressed[info->header.length_compressed - 2], compressed[info->header.length_compressed - 1]);
free(compressed);
free(info);
return NULL;
}

XLOG(5, "match: %d %d %d %x %x\n", info->header.length_compressed, real_uncompressed, info->header.length_uncompressed, compressed[info->header.length_compressed - 2], compressed[info->header.length_compressed - 1]);

free(compressed);

info->dirty = FALSE;

info->offset = 0;

toReturn = (AbstractFile*) malloc(sizeof(AbstractFile));
toReturn->data = info;
toReturn->read = readComp;
toReturn->write = writeComp;
toReturn->seek = seekComp;
toReturn->tell = tellComp;
toReturn->getLength = getLengthComp;
toReturn->close = closeComp;
toReturn->type = AbstractFileTypeLZSS;

return toReturn;
}

AbstractFile* duplicateCompFile(AbstractFile* file, AbstractFile* backing) {
InfoComp* info;
AbstractFile* toReturn;

if(!file) {
return NULL;
}

info = (InfoComp*) malloc(sizeof(InfoComp));
memcpy(info, file->data, sizeof(InfoComp));

info->file = backing;
info->buffer = malloc(1);
info->header.length_uncompressed = 0;
info->dirty = TRUE;
info->offset = 0;

toReturn = (AbstractFile*) malloc(sizeof(AbstractFile));
toReturn->data = info;
toReturn->read = readComp;
toReturn->write = writeComp;
toReturn->seek = seekComp;
toReturn->tell = tellComp;
toReturn->getLength = getLengthComp;
toReturn->close = closeComp;
toReturn->type = AbstractFileTypeLZSS;

return toReturn;
}

Something went wrong with that request. Please try again.