Originally Posted by
laserlight
Hey, I am just trying to help you improve your contest rules
There's nothing wrong with the rules as they are. No professional compiler would be so anal as to NOT include malloc.h if only for compatibility with pre-standard code. If your implementation doesn't support malloc.h then too bad, it's not my problem. As I said, I highly SUGGEST, you are free to do whatever you want, if it fails to run it fails to run. But, as laserlight pointed out, it is unlikely that any implementation would run with stdlib and fail to run with malloc. Since malloc is used in the main function, not in your function, you are free to include whatever headers you desire, within the limits of the contest.
As a sign of good faith, I will provide a very simple compressor. Obviously this doesn't meet the requirements of the contest, as I rolle dthe whole thing into main. it also suffers from poor compression because of a known weakness, that is each letter gets compressed by 2 bits, but every non letter, specifically spaces, eat an extra 6 bits. Overall it saved only 60KB out fo a 3MB file. Like I said its very crude compression.
Code:
int main(){
char* pBuffer = (char*)malloc(1048576);
FILE* pFile = fopen("e:\\warandpeace.txt" , "r+b");
long BytesRead = -1;
unsigned long ByteCount[256];
for(int x = 0;x<256;x++) ByteCount[x] = 0;
unsigned long TotalBytes = 0;
while(BytesRead){
BytesRead = fread(pBuffer , 1 , 1048576 , pFile);
if(BytesRead) for(int x=0;x<BytesRead;x++) ByteCount[pBuffer[x]]++;
TotalBytes += BytesRead;
}
fclose(pFile);
pFile = 0;
pFile = fopen("e:\\ByteCount.txt" , "w+b");
long Index = 0;
for(int x = 0;x<256;x++){
fprintf(pFile , "%2.2x - %d%c%c" , x , ByteCount[x] , 13 , 10);
if(ByteCount[x]) Index++;
}
fprintf(pFile , "%c%cTotal Symbols Used %d%c%c" , 13 , 10 , Index , 13 , 10);
fclose(pFile);
pBuffer = (char*)realloc(pBuffer , TotalBytes);
pFile = fopen("e:\\warandpeace.txt" , "r+b");
fread(pBuffer , 1 , TotalBytes , pFile);
fclose(pFile);
char* pOutput = (char*)malloc(TotalBytes);
unsigned long BitCache = 0;
long BitIndex = 0;
Index = 0;
pFile = fopen("e:\\warandpeace.paq" , "w+b");
for(int x = 0;x<TotalBytes;x++){
if((pBuffer[x] > 64) && (pBuffer[x] < 128)){
BitCache *= 64;
BitCache = BitCache & 0xFFFFFFC4;
BitCache = BitCache | (pBuffer[x] & 63);
BitIndex += 6;
} else {
BitCache *= 64;
BitCache = BitCache & 0xFFFFFFC4;
BitIndex += 6;
BitCache *= 256;
BitCache = BitCache & 0xFFFFFF00;
BitCache = BitCache | pBuffer[x];
BitIndex += 8;
}
while(BitIndex > 7){
pOutput[Index] = BitCache & 0xFF;
BitCache /= 256;
BitCache = BitCache & 0x00FFFFFF;
BitIndex -= 8;
Index++;
}
}
while(BitIndex > 0){
pOutput[Index] = BitCache & 0xFF;
BitCache /= 256;
BitCache = BitCache & 0x00FFFFFF;
BitIndex -= 8;
Index++;
}
fwrite(&TotalBytes , sizeof(unsigned long) , 1 , pFile);
fwrite(&Index , sizeof(unsigned long) , 1 , pFile);
fwrite(pOutput , 1 , Index , pFile);
fclose(pFile);
return 0;
}