Von C nach Java, Teil 4: Datenkompression und Verschlüsselung

Seite 8: Listing 4

Inhaltsverzeichnis
int processFile(char *fileName) {
FILE *ifp, *ofp;
char outFileName[300];
struct timeb tp;
int i, l, ch, blockNo, lastBlockNo;
long tics;

srand(DEFINE_RAND_SEED);
if (isDecompress)
return processDecompression(fileName);
ftime(&tp);
tics=1000*(long)tp.time+tp.millitm;
if ((ifp=fopen(fileName,"rb"))==NULL) {
fprintf(stderr,"unable to open file \"%s\"\nfopen():
%s\n", fileName, strerror(errno));
return -1;
}
sprintf(outFileName,outFileNamePattern,tics);
if ((ofp=fopen(outFileName,"wb"))==NULL) {
fprintf(stderr,"unable to open file \"%s\"\nfopen():
%s\n", outFileName, strerror(errno));
return -1;
}
/**
* write the file header to the output file right here
*/
for (i=0;i<4;i++) {
ch=magicHdr[i];
if (i==3 && isCrypt) ch++;
fputc(ch, ofp);
}
if (isCrypt) {
/**
* store 16 byte MD5 sum of password here ...
*/
uchar *md5Hash=getMD5Hash(passwd, strlen(passwd));
fwrite(md5Hash, 1, MD5LEN, ofp);
}
/**
* store length of filename
* and the filename simply scrambled by some random numbers ...
*/
fputc(l=strlen(fileName), ofp);
for (i=0;i<l;i++) {
fputc(fileName[i] ^ ~rand(), ofp);
}
/**
* then store the block size in KB to give the decompressor a hint of
* how many bytes should be allocated for the destination buffer
*/
fputc(blockSizeKB & 0xff, ofp);
fputc((blockSizeKB >> 8) & 0xff, ofp);
if (isVerbose) fprintf(stderr,"processing %s ...\n", fileName);
for (blockNo=0;;blockNo++) {
DWORD dwTid;
PBP_S pbp_s=calloc(1,sizeof(BP_S));

uchar *inputBlock=malloc(1024*blockSizeKB);
int nbRead=fread(inputBlock, 1, 1024*blockSizeKB, ifp);
fprintf(stderr,"Read %2d. block with %7d bytes length\n",
blockNo+1, nbRead);
if (nbRead<=0) break;
pbp_s->blockNo=lastBlockNo=blockNo;
pbp_s->inputBlock=inputBlock;
pbp_s->inputSize=nbRead;
pbp_s->ofp=ofp;
allocateThread();
fprintf(stderr,"Creating processBlock_Thread for block #%d
and %d input bytes ...\n", blockNo, nbRead);
if (CreateThread((LPSECURITY_ATTRIBUTES)NULL,0,
(LPTHREAD_START_ROUTINE)processBlock,(LPVOID)pbp_s,0,
&dwTid)==NULL) {
fprintf(stderr,"CreateThread(processBlock()) failed!\n");
fclose(ofp); unlink(outFileName);
return -1;
}
}
/**
* waiting for the threads to terminate ...
*/
fclose(ifp);
waitForBlockNoWritten(lastBlockNo);
fclose(ofp);
if (isRemoveSource) unlink(fileName);
return 0;
}