#ifndef p1_pack_bb
#define p1_pack_bb

#include <pragma/dos_lib.h>
#include <stdio.h>
#include <string.h>
#include <tools/str.h>
#include <exec/memory.h>
#include <pragma/exec_lib.h>

int P1Pack1(char*,char*,int);
int P1UnPack1(char*,char*,int);

int P1UnPack1(char* f1,char* f2,int isprev){
 BPTR file1=0,file2=0;
 int err=0;
 char hilf[6];
 LONG x,y;
 char c;
 char s1[42];
 char buffer[256];

 tmpnam(s1);

 if(!(file1 = Open(f1,MODE_OLDFILE))){err=1;goto beenden;}
 if(!(file2 = Open(s1,MODE_READWRITE))){err=1;goto beenden;}
 if(isprev){
  FGets(file1,hilf,5);hilf[5]=0;
  if(strcmp("P-0-1",hilf)){err=2;goto beenden;}
 }
 
 weiter1:
 x=FGetC(file1);if(x==-1)goto beenden;
 y=FGetC(file1);if(x==-1)goto beenden;
 c=x;
 if(c>0){
  while(c){if(-1==FPutC(file2,y)){err=3;goto beenden;};c--;}  
 }
 else{
  c=-c;
  if(-1==FPutC(file2,y)){err=3;goto beenden;};c--;
  while(c){y=FGetC(file1);if(-1==FPutC(file2,y)){err=3;goto beenden;};c--;}
 }
 goto weiter1;

 beenden:
 if(file1) Close(file1);
 if(file2) Close(file2);
 if(!err){
  if(!(file1 = Open(s1,MODE_OLDFILE))){err=1;goto beenden;}
  if(!(file2 = Open(f2,MODE_NEWFILE))){err=1;goto beenden;}
  while(x = FRead(file1,buffer,1,256)){
   if(!(x == FWrite(file2,buffer,1,x))){err=3;goto beenden;}
  }
  Close(file1);Close(file2);
 }
 remove(s1);
 return err;
}

int P1Pack1(char* f1,char* f2,int doprev){
 BPTR file1=0;
 FileInfoBlock* exfh1=0;
 char* buf1=0; 
 int err=0;
 int x,y,n;
 char a,b;
 char s1[42];
 LONG xxx[130];

 tmpnam(s1);

 if(!(exfh1 = (FileInfoBlock*)AllocMem(sizeof(FileInfoBlock),MEMF_ANY))){err=4;goto beenden;}
 if(!(file1 = Open(f1,MODE_OLDFILE))){err=1;goto beenden;}
 if(!(ExamineFH(file1,exfh1))){err=1;goto beenden;}
 if(!(buf1 = new char[exfh1->fib_Size])){err=4;goto beenden;}
 if(!(exfh1->fib_Size == Read(file1,buf1,exfh1->fib_Size))){err=2;goto beenden;}
 Close(file1);file1=0;

 if(!(file1 = Open(s1,MODE_NEWFILE))){err=1;goto beenden;}

 for(x=0;x<exfh1->fib_Size;x++){
  y=1;
  a = buf1[x];
  while(buf1[x+1]==a){
   y++;x++;
   if(x>=exfh1->fib_Size){y--;goto weiter1;}
   if(y>126) goto weiter1;
  }
  weiter1:
  if(!(y==FPutC(file1,y))){err=2;goto beenden;}
  if(!(a==FPutC(file1,a))){err=2;goto beenden;}
 }

 Close(file1);file1=0;delete[] buf1;buf1=0;
 if(!(file1 = Open(s1,MODE_OLDFILE))){err=1;goto beenden;}
 if(!(ExamineFH(file1,exfh1))){err=1;goto beenden;}
 if(!(buf1 = new char[exfh1->fib_Size])){err=4;goto beenden;}
 if(!(exfh1->fib_Size == Read(file1,buf1,exfh1->fib_Size))){err=2;goto beenden;}
 Close(file1);file1=0;remove(s1); 

 if(!(file1 = Open(f2,MODE_NEWFILE))){err=1;goto beenden;}
 if(doprev) FWrite(file1,"P-0-1",5,1);

 for(x=0;x<exfh1->fib_Size;x++){
  a=buf1[x];b=buf1[x+1];x++;
  weiter12:
  if(a>1){if(!(a==FPutC(file1,a))){err=2;goto beenden;} if(!(b==FPutC(file1,b))){err=2;goto beenden;}}
  else{
   n=1;xxx[0]=b;
   weiter11:
   x++;if(x>=exfh1->fib_Size){if(!(-n==FPutC(file1,-n))){err=2;goto beenden;}for(y=0;y<n;y++){if(!(xxx[y]==FPutC(file1,xxx[y]))){err=2;goto beenden;}}goto weiter13;}
   a=buf1[x];b=buf1[x+1];x++;
   if(n>126) {if(!(-n==FPutC(file1,-n))){err=2;goto beenden;}for(y=0;y<n;y++){if(!(xxx[y]==FPutC(file1,xxx[y]))){err=2;goto beenden;}}goto weiter12;}
   if(a==1){xxx[n]=b;n++;goto weiter11;}
   else {if(!(-n==FPutC(file1,-n))){err=2;goto beenden;}for(y=0;y<n;y++){if(!(xxx[y]==FPutC(file1,xxx[y]))){err=2;goto beenden;}}goto weiter12;}
  }
 }
 weiter13:

 beenden:
 if(file1) Close(file1);
 if(exfh1) FreeMem(exfh1,sizeof(FileInfoBlock));
 if(buf1) delete[] buf1;
 remove(s1);
 return err;
}
#endif

