home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2002 November
/
VPR0211A.ISO
/
OLS
/
BZ2L003
/
bz2l003.lzh
/
BZ2LIB
/
BZ2LIB.C
< prev
next >
Wrap
C/C++ Source or Header
|
1998-06-02
|
4KB
|
198 lines
#include "bz2lib.h"
#include "stringq.h"
#include "bzip2.h"
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
#include <io.h>
#endif
static volatile int flag_running=0;
/* Get Version Number (x 100) of bz2lib */
int EXPORTAPI BZ2GetVersion(void)
{
return BZ2LIB_VERSION;
}
/* return 1 if bz2lib is already running (check for multithreading) */
int EXPORTAPI BZ2GetRunning(void)
{
return flag_running;
}
/* get bz2 file's level. if not bzip2 file,return -1 */
int EXPORTAPI BZ2GetLevelStream(FILE *zStream)
{
char magic[16];
int level;
int len;
len=fread(magic,1,16,zStream);
fseek(zStream,-len,SEEK_CUR);
if(strncmp("BZh",magic,3)!=0){return -1;}
/* magic is 3.14159265359 (= Pi) */
if(strncmp(magic+4,"\x31\x41\x59\x26\x53\x59",6)!=0){return -1;}
if('0'<=magic[3] && magic[3]<='9'){level = magic[3];}else{return -1;}
return level;
}
int EXPORTAPI BZ2GetLevel(char *file)
{
FILE *fp;
int level;
fp=fopen(file,"rb");
if(fp==NULL){return -1;}
level=BZ2GetLevelStream(fp);
fclose(fp);
return level;
}
BZ2FILE * EXPORTAPI BZ2OpenStream(FILE *zStream,char *mode)
{
BZ2FILE *BZ2fp;
flag_running++;
BZ2fp = malloc(sizeof(BZ2FILE));
if(BZ2fp==NULL){goto endlabel;}
memset(BZ2fp,0,sizeof(BZ2FILE));
BZ2fp->sq = STRINGQ_open(STRING_QUEUE_BUFFER_SIZE);
if(BZ2fp->sq==NULL){
free(BZ2fp);BZ2fp=NULL;
goto endlabel;
}
BZ2fp->fp = zStream;
BZ2fp->mode ='r';
BZ2fp->level = 9;
while(*mode){
if(*mode == 'r'){
BZ2fp->mode ='r';
}else if(*mode =='w'){
BZ2fp->mode ='w';
}else if('0' <= *mode && *mode <='9'){
BZ2fp->level = *mode - '0';
}
mode++;
}
/*{
int fd2;
fd2 = dup(zStream);
if(BZ2fp->mode == 'w'){
BZ2fp->fp = fdopen(fd2,"wb");
}else{
BZ2fp->fp = fdopen(fd2,"rb");
}
}*/
endlabel:
flag_running--;
/*fputc ( 'A',BZ2fp->fp);*/
return BZ2fp;
}
/*static FILE * orig_fp;*/
BZ2FILE * EXPORTAPI BZ2Open(char *file,char *mode)
{
FILE *fp;
BZ2FILE *BZ2fp;
if(strchr(mode,'w')){
if(file){
fp=fopen(file,"wb");
}else{
fp=stdout;
}
}else{
if(file){
fp=fopen(file,"rb");
}else{
fp=stdin;
}
}
if(fp==NULL){return NULL;}
BZ2fp=BZ2OpenStream(fp,mode);
if(BZ2fp==NULL){
if(!(fp==stdin || fp==stdout)){
fclose(fp);
}
return NULL;
}
/*orig_fp = fp;*/
return BZ2fp;
}
void EXPORTAPI BZ2CloseStream(BZ2FILE *BZ2fp)
{
flag_running++;
STRINGQ_puteof(BZ2fp->sq);
BZ2fp->blocking = 0;
BZ2fp->error = 0;
if(BZ2fp->mode =='w'){
compressStream2(BZ2fp->sq,BZ2fp->fp,BZ2fp);
}else{
;/*uncompressStream2(BZ2fp->fp,BZ2fp->sq,BZ2fp);*/
}
if(BZ2fp->error == 1){goto endlabel /*return -1*/;}
fclose(BZ2fp->fp);
free(BZ2fp);
endlabel:
flag_running--;
}
void EXPORTAPI BZ2Close(BZ2FILE *BZ2fp)
{
FILE *fp = BZ2fp->fp;
BZ2CloseStream(BZ2fp);
if(!(fp==stdin || fp==stdout)){
fclose(fp);
}
}
int EXPORTAPI BZ2Write(BZ2FILE *BZ2fp,char *buff,int size)
{
int len;
int left_size = size;
int retval;
flag_running++;
while(left_size>0){
len = MIN(left_size,BZ2fp->sq->bufsize);
STRINGQ_write(BZ2fp->sq,buff,len);
BZ2fp->blocking = 0;
BZ2fp->error = 0;
compressStream2(BZ2fp->sq,BZ2fp->fp,BZ2fp);
if(BZ2fp->error == 1){retval=-1;goto endlabel;}
left_size -=len;
buff +=len;
}
retval = size;
endlabel:
flag_running--;
return retval;
}
int EXPORTAPI BZ2Read(BZ2FILE *BZ2fp,char *buff,int size)
{
int len;
int left_size = size;
flag_running++;
while(left_size>0){
len = MIN(left_size,BZ2fp->sq->len);
STRINGQ_read(BZ2fp->sq,buff,len);
left_size -= len;
buff += len;
if(len == left_size){break;}
if(BZ2fp->sq->exist_eof){break;}
BZ2fp->blocking = 0;
BZ2fp->error = 0;
uncompressStream2(BZ2fp->fp,BZ2fp->sq,BZ2fp);
}
flag_running--;
return size - left_size;
}