/*
**      example.c
**
**      written in 1993 by Titus v. Kraft
**
**      compiler: maxon-c++
**
**
**
**      routines:
**
**      GetTextSeg (Segment-Numer, Segment-Base, [Password-String])
**
**      DecodeTextSeg (Segment-Number, Segment-Base, Password-String)
**
**      MakeTextSegPtrs (Segment-Base)
**
*/

#define EORKEY 0x98

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "exec/types.h"
#include "exec/memory.h"
#include "dos/dos.h"
#include "dos/dosextens.h"

#include "pragma/exec_lib.h"
#include "pragma/dos_lib.h"



void DecodeTextSeg(ULONG DTSeg, ULONG *DTBase,UBYTE *DTPassword)
{
 char *DTText;
 unsigned long count = 0L, count2 = 0L;

 if (DTBase[1] >= DTSeg)
  {
   DTText = (char *)DTBase[4+DTSeg-1];
   if (DTText)
    {
     do
     {
      DTText[count] --;
      DTText[count] = DTText[count] ^ EORKEY;
      DTText[count] = DTText[count]+DTPassword[count2];
      count++; count2++;
      if (!(DTPassword[count2+1])) count2 = 0L;
     } while (DTText[count-1]);
    };
  };
}




void MakeTextSegPtrs(unsigned long *MSBase)
{
 unsigned long von  =  0;
 unsigned long help =  (unsigned long)&MSBase[0];
 unsigned long bis  =  MSBase[1];

 MSBase = &MSBase[4];
 for (von=0; von<bis; von++)
 {
  if (MSBase[von]!=0L)
  {
   MSBase[von]=MSBase[von]+help;
  };
 };
}



char *GetTextSeg(ULONG GTSeg,ULONG *GTBase,UBYTE *GTPassword)
{
  char *SegPtr;

  if (GTBase[1] >= GTSeg)
  {
   if (GTBase[2] && 1)
    {
     DecodeTextSeg(GTSeg,GTBase,GTPassword);
    };
   SegPtr=(char *)GTBase[4+GTSeg-1];
  }
  else
  {
   SegPtr="segment-number too great.";
  };

 return(&SegPtr[0]);
}



main(int argc,char *argv[])
{

 BPTR   fh     = NULL;
 APTR   mem    = NULL;
 ULONG  size   = NULL;
 BPTR   flock;
 struct FileInfoBlock *fib;
 char   fname[256];

 ULONG Seg       = NULL;
 ULONG *Base     = NULL;
 char  *TextSeg  = NULL;
 char  *Password = "Mr. Spock Lives On Vulcan";
 char  stfbuff[4];

 if (argv[2]) Seg = (ULONG)atoi(argv[2]);

 if ((!(argv[1])) || (!(Seg)))
 {
  printf("USAGE: %s File Segment-Number\n", argv[0]); exit(NULL);
 }
 else
 {
  strcpy(fname,argv[1]);
 };


 flock = Lock(fname,ACCESS_READ);
 if (flock)
 {
  fib = (struct FileInfoBlock *)AllocMem((ULONG)
        (sizeof(struct FileInfoBlock)),MEMF_PUBLIC);
  if (fib)
  {
   Examine(flock,fib);
   if (size=(fib->fib_Size))
   {
    fh = Open(fname,MODE_OLDFILE);
    if (fh)
    {
     Read(fh,stfbuff,4);
     if (strcmp("STF",stfbuff)==NULL)
     {
      Seek(fh,0,OFFSET_BEGINNING);       /* correct fileptr */
      mem = AllocMem(size,MEMF_PUBLIC);
      if (mem)
      {
       Read(fh,mem,size);

/*


       offsets to pointers


*/

       Base = (ULONG *)mem;
       MakeTextSegPtrs(Base);

/*


       get pointer to the text-segment and
       put out segment


*/
       TextSeg = GetTextSeg(Seg,Base,Password);
       if (TextSeg)
       {
        printf("\n%s\n",TextSeg)
       }
       else
       {
        printf("emty slot.\n");
       };


       FreeMem(mem,size);
       }
       else
       {
        printf("not enought memory.\n");
       };
      }
      else
      {
       printf("this is not a sft-file.\n");
      };
      Close(fh);
     }
     else
     {
      printf("cannot open %s.\n",argv[1]);
     };
    };
   FreeMem(fib,sizeof(struct FileInfoBlock));
   }
   else
   {
    printf("not enought memory.\n");
   };
  UnLock(flock);
  }
  else
  {
   printf("file not found.\n");
  };


 return(NULL);

} /* end of main */
