int main( int argc, char **argv ) { // Assert proper usage if( argc != 3 ) { printf( "Usage: script_inserter <script name> <table file>\n" ); return -1; } // phase 1: Script conversion Convert_Symbols( argv[1], argv[2], "pass1.bin" ); // phase 2: Dual-tile encoding //#define DTE #ifdef DTE if( DTE_Process( "pass1.bin", "pass2.bin" ) ) return 0; // phase 3: Huffman encoding Huffman_Compress( "pass2.bin", "script", "script_trees.bin" ); #else // phase 2: Huffman encoding Huffman_Compress( "pass1.bin", "script", "script_trees.bin" ); #endif return 0; }
static int EnumIpDataLines(const char *pszFileCSV,const char *pszFileOut) { FILE *fp; char line[1024],out[512],*pszFrom,*pszTo,*pszTwo,*pszCountry,*buf; int i,j; DWORD dwOut; WORD wOut; struct ResizableByteBuffer buffer; memset(&buffer, 0, sizeof(buffer)); fp=fopen(pszFileCSV,"rt"); if (fp != NULL) { OutputDebugStringA("Running IP data convert...\n"); /* all ascii */ while (!feof(fp)) { if (fgets(line,sizeof(line),fp) == NULL) break; /* get line data */ pszFrom=line+1; pszTo=strchr(pszFrom,','); *(pszTo-1)='\0'; pszTo+=2; pszTwo=strchr(pszTo,','); *(pszTwo-1)='\0'; pszTwo+=2; pszCountry=strchr(pszTwo,',')+1; pszCountry=strchr(pszCountry,',')+2; buf=strchr(pszCountry,'"'); *buf=pszTwo[2]='\0'; /* corrections */ if (!mir_tstrcmpi(pszCountry,"ANTARCTICA")) continue; if (!mir_tstrcmpi(pszCountry,"TIMOR-LESTE")) continue; if (!mir_tstrcmpi(pszCountry,"PALESTINIAN TERRITORY, OCCUPIED")) mir_tstrcpy(pszCountry,"ISRAEL"); else if (!mir_tstrcmpi(pszCountry,"UNITED STATES MINOR OUTLYING ISLANDS")) mir_tstrcpy(pszCountry,"UNITED STATES"); else if (!mir_tstrcmpi(pszCountry,"SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS")) mir_tstrcpy(pszCountry,"UNITED KINGDOM"); else if (!mir_tstrcmpi(pszTwo,"JE")) /* map error */ mir_tstrcpy(pszCountry,"UNITED KINGDOM"); else if (!mir_tstrcmpi(pszTwo,"AX")) /* Åland Island belongs to Finland */ mir_tstrcpy(pszCountry,"FINLAND"); else if (!mir_tstrcmpi(pszTwo,"ME")) mir_tstrcpy(pszCountry,"MONTENEGRO"); else if (!mir_tstrcmpi(pszTwo,"RS") || !mir_tstrcmpi(pszTwo,"CS")) mir_tstrcpy(pszCountry,"SERBIA"); /* convert */ for(i=0;i<nCountriesCount;i++) { /* map different writings */ for(j=0;j<_countof(differentCountryNames);j++) if (!mir_tstrcmpi(countries[i].szName,differentCountryNames[j].szMir)) { buf=(char*)differentCountryNames[j].szCSV; break; } if (j == _countof(differentCountryNames)) buf=(char*)countries[i].szName; /* check country */ if (!mir_strcmpi(pszCountry,buf)) { dwOut=(DWORD)atoi(pszFrom); AppendToByteBuffer(&buffer,(void*)&dwOut,sizeof(DWORD)); dwOut=(DWORD)atoi(pszTo); AppendToByteBuffer(&buffer,(void*)&dwOut,sizeof(DWORD)); wOut=(WORD)countries[i].id; AppendToByteBuffer(&buffer,(void*)&wOut,sizeof(WORD)); break; } } /* not in list */ if (i == nCountriesCount) { mir_snprintf(out, "Unknown: %s-%s [%s, %s]\n", pszFrom, pszTo, pszTwo, pszCountry); OutputDebugStringA(out); /* all ascii */ } } fclose(fp); OutputDebugStringA("Done!\n"); /* all ascii */ if (buffer.buf != NULL) { HANDLE hFileOut; DWORD cbWritten=0; BYTE *compressed; DWORD cbCompressed; /* compress whole data */ OutputDebugStringA("Compressing...\n"); /* all ascii */ compressed=(BYTE*)mir_alloc(buffer.cbAlloced+384); if (compressed != NULL) { cbCompressed=Huffman_Compress(buffer.buf,compressed,buffer.cbLength); OutputDebugStringA("Done!\n"); /* all ascii */ OutputDebugStringA("Writing to file...\n"); /* all ascii */ hFileOut=CreateFile(pszFileOut,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); if (hFileOut != INVALID_HANDLE_VALUE) { /* store data length count at beginning */ dwOut=buffer.cbLength; WriteFile(hFileOut,&dwOut,sizeof(DWORD),&cbWritten,NULL); /* store compressed data records */ WriteFile(hFileOut,compressed,cbCompressed,&cbWritten,NULL); CloseHandle(hFileOut); } OutputDebugStringA("Done!\n"); /* all ascii */ mir_free(compressed); } mir_free(buffer.buf); } return 0; } return 1; }
int TestFile( char *name, int algo, int verbose ) { unsigned int insize, outsize, bufsize, *work, k, err_count; unsigned char *in, *out, *buf; FILE *f; double t0, t_comp, t_uncomp; printf( "Testing %s...", name ); /* Open input file */ f = fopen( name, "rb" ); if( !f ) { printf( "unable to open!\n" ); return 0; } /* Get input size */ insize = GetFileSize( f ); if( insize < 1 ) { printf( "empty file!\n" ); fclose( f ); return 0; } /* Worst case output buffer size */ bufsize = (insize*104+50)/100 + 384; /* Allocate memory */ in = (unsigned char *) malloc( insize + 2*bufsize ); if( !in ) { printf( "out of memory!\n" ); fclose( f ); return 0; } /* Pointers to compression buffer and output memory */ buf = &in[ insize ]; out = &buf[ bufsize ]; /* Read and close input file */ fread( in, 1, insize, f ); fclose( f ); /* Compress and decompress */ switch( algo ) { case 1: t0 = GetTime(); outsize = RLE_Compress( in, insize, buf, bufsize ); t_comp = GetTime() - t0; t0 = GetTime(); RLE_Uncompress( buf, outsize, out, insize ); t_uncomp = GetTime() - t0; break; case 2: t0 = GetTime(); outsize = Huffman_Compress( in, buf, insize ); t_comp = GetTime() - t0; t0 = GetTime(); Huffman_Uncompress( buf, out, outsize, insize ); t_uncomp = GetTime() - t0; break; case 3: t0 = GetTime(); outsize = Rice_Compress( in, buf, insize, RICE_FMT_UINT8 ); t_comp = GetTime() - t0; t0 = GetTime(); Rice_Uncompress( buf, out, outsize, insize, RICE_FMT_UINT8 ); t_uncomp = GetTime() - t0; break; case 4: t0 = GetTime(); outsize = Rice_Compress( in, buf, insize, RICE_FMT_UINT16 ); t_comp = GetTime() - t0; t0 = GetTime(); Rice_Uncompress( buf, out, outsize, insize, RICE_FMT_UINT16 ); t_uncomp = GetTime() - t0; break; case 5: t0 = GetTime(); outsize = Rice_Compress( in, buf, insize, RICE_FMT_UINT32 ); t_comp = GetTime() - t0; t0 = GetTime(); Rice_Uncompress( buf, out, outsize, insize, RICE_FMT_UINT32 ); t_uncomp = GetTime() - t0; break; case 6: t0 = GetTime(); outsize = Rice_Compress( in, buf, insize, RICE_FMT_INT8 ); t_comp = GetTime() - t0; t0 = GetTime(); Rice_Uncompress( buf, out, outsize, insize, RICE_FMT_INT8 ); t_uncomp = GetTime() - t0; break; case 7: t0 = GetTime(); outsize = Rice_Compress( in, buf, insize, RICE_FMT_INT16 ); t_comp = GetTime() - t0; t0 = GetTime(); Rice_Uncompress( buf, out, outsize, insize, RICE_FMT_INT16 ); t_uncomp = GetTime() - t0; break; case 8: t0 = GetTime(); outsize = Rice_Compress( in, buf, insize, RICE_FMT_INT32 ); t_comp = GetTime() - t0; t0 = GetTime(); Rice_Uncompress( buf, out, outsize, insize, RICE_FMT_INT32 ); t_uncomp = GetTime() - t0; break; case 9: t0 = GetTime(); outsize = LZ_Compress( in, buf, insize ); t_comp = GetTime() - t0; t0 = GetTime(); LZ_Uncompress( buf, out, outsize ); t_uncomp = GetTime() - t0; break; case 10: work = malloc( sizeof(unsigned int) * (65536+insize) ); if( work ) { t0 = GetTime(); outsize = LZ_CompressFast( in, buf, insize, work ); t_comp = GetTime() - t0; free( work ); t0 = GetTime(); LZ_Uncompress( buf, out, outsize ); t_uncomp = GetTime() - t0; } else { printf( "unable to allocate working buffer!\n" ); t_comp = 0.0; t_uncomp = 0.0; outsize = 0; } break; case 11: t0 = GetTime(); outsize = SF_Compress( in, buf, insize ); t_comp = GetTime() - t0; t0 = GetTime(); SF_Uncompress( buf, out, outsize, insize ); t_uncomp = GetTime() - t0; break; default: /* Should never happen... */ outsize = 0; t_comp = 0.0; t_uncomp = 0.0; } err_count = 0; if(outsize > 0) { /* Show compression result */ if( verbose ) { printf( "\n Compression: %d/%d bytes (%.1f%%)", outsize, insize, 100*(float)outsize/(float)insize ); } /* Compare input / output data */ for( k = 0; k < insize; ++ k ) { if( in[ k ] != out[ k ] ) { if( err_count == 0 ) printf( "\n" ); if( err_count == 30 ) printf( " ...\n" ); else if( err_count < 30 ) { printf( " %d: %d != %d\n", k, out[ k ], in[ k ] ); } ++ err_count; } } /* Did we have success? */ if( err_count == 0 ) { printf( " - OK!\n" ); if( verbose ) { printf( " Compression speed: %.1f KB/s (%.2f ms)\n", (double) insize / (1024.0 * t_comp), 1000.0 * t_comp ); printf( " Uncompression speed: %.1f KB/s (%.2f ms)\n", (double) insize / (1024.0 * t_uncomp), 1000.0 * t_uncomp ); } } else { printf( " *******************************\n" ); printf( " ERROR: %d faulty bytes\n", err_count ); printf( " *******************************\n" ); } } /* Free all memory */ free( in ); return (outsize > 0) && (err_count == 0); }