home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d887 / segtextmaster.lha / SegTextMaster / Examples / example.c < prev    next >
C/C++ Source or Header  |  1993-07-16  |  3KB  |  215 lines

  1. /*
  2. **      example.c
  3. **
  4. **      written in 1993 by Titus v. Kraft
  5. **
  6. **      compiler: maxon-c++
  7. **
  8. **
  9. **
  10. **      routines:
  11. **
  12. **      GetTextSeg (Segment-Numer, Segment-Base, [Password-String])
  13. **
  14. **      DecodeTextSeg (Segment-Number, Segment-Base, Password-String)
  15. **
  16. **      MakeTextSegPtrs (Segment-Base)
  17. **
  18. */
  19.  
  20. #define EORKEY 0x98
  21.  
  22. #include "stdio.h"
  23. #include "stdlib.h"
  24. #include "string.h"
  25. #include "exec/types.h"
  26. #include "exec/memory.h"
  27. #include "dos/dos.h"
  28. #include "dos/dosextens.h"
  29.  
  30. #include "pragma/exec_lib.h"
  31. #include "pragma/dos_lib.h"
  32.  
  33.  
  34.  
  35. void DecodeTextSeg(ULONG DTSeg, ULONG *DTBase,UBYTE *DTPassword)
  36. {
  37.  char *DTText;
  38.  unsigned long count = 0L, count2 = 0L;
  39.  
  40.  if (DTBase[1] >= DTSeg)
  41.   {
  42.    DTText = (char *)DTBase[4+DTSeg-1];
  43.    if (DTText)
  44.     {
  45.      do
  46.      {
  47.       DTText[count] --;
  48.       DTText[count] = DTText[count] ^ EORKEY;
  49.       DTText[count] = DTText[count]+DTPassword[count2];
  50.       count++; count2++;
  51.       if (!(DTPassword[count2+1])) count2 = 0L;
  52.      } while (DTText[count-1]);
  53.     };
  54.   };
  55. }
  56.  
  57.  
  58.  
  59.  
  60. void MakeTextSegPtrs(unsigned long *MSBase)
  61. {
  62.  unsigned long von  =  0;
  63.  unsigned long help =  (unsigned long)&MSBase[0];
  64.  unsigned long bis  =  MSBase[1];
  65.  
  66.  MSBase = &MSBase[4];
  67.  for (von=0; von<bis; von++)
  68.  {
  69.   if (MSBase[von]!=0L)
  70.   {
  71.    MSBase[von]=MSBase[von]+help;
  72.   };
  73.  };
  74. }
  75.  
  76.  
  77.  
  78. char *GetTextSeg(ULONG GTSeg,ULONG *GTBase,UBYTE *GTPassword)
  79. {
  80.   char *SegPtr;
  81.  
  82.   if (GTBase[1] >= GTSeg)
  83.   {
  84.    if (GTBase[2] && 1)
  85.     {
  86.      DecodeTextSeg(GTSeg,GTBase,GTPassword);
  87.     };
  88.    SegPtr=(char *)GTBase[4+GTSeg-1];
  89.   }
  90.   else
  91.   {
  92.    SegPtr="segment-number too great.";
  93.   };
  94.  
  95.  return(&SegPtr[0]);
  96. }
  97.  
  98.  
  99.  
  100. main(int argc,char *argv[])
  101. {
  102.  
  103.  BPTR   fh     = NULL;
  104.  APTR   mem    = NULL;
  105.  ULONG  size   = NULL;
  106.  BPTR   flock;
  107.  struct FileInfoBlock *fib;
  108.  char   fname[256];
  109.  
  110.  ULONG Seg       = NULL;
  111.  ULONG *Base     = NULL;
  112.  char  *TextSeg  = NULL;
  113.  char  *Password = "Mr. Spock Lives On Vulcan";
  114.  char  stfbuff[4];
  115.  
  116.  if (argv[2]) Seg = (ULONG)atoi(argv[2]);
  117.  
  118.  if ((!(argv[1])) || (!(Seg)))
  119.  {
  120.   printf("USAGE: %s File Segment-Number\n", argv[0]); exit(NULL);
  121.  }
  122.  else
  123.  {
  124.   strcpy(fname,argv[1]);
  125.  };
  126.  
  127.  
  128.  flock = Lock(fname,ACCESS_READ);
  129.  if (flock)
  130.  {
  131.   fib = (struct FileInfoBlock *)AllocMem((ULONG)
  132.         (sizeof(struct FileInfoBlock)),MEMF_PUBLIC);
  133.   if (fib)
  134.   {
  135.    Examine(flock,fib);
  136.    if (size=(fib->fib_Size))
  137.    {
  138.     fh = Open(fname,MODE_OLDFILE);
  139.     if (fh)
  140.     {
  141.      Read(fh,stfbuff,4);
  142.      if (strcmp("STF",stfbuff)==NULL)
  143.      {
  144.       Seek(fh,0,OFFSET_BEGINNING);       /* correct fileptr */
  145.       mem = AllocMem(size,MEMF_PUBLIC);
  146.       if (mem)
  147.       {
  148.        Read(fh,mem,size);
  149.  
  150. /*
  151.  
  152.  
  153.        offsets to pointers
  154.  
  155.  
  156. */
  157.  
  158.        Base = (ULONG *)mem;
  159.        MakeTextSegPtrs(Base);
  160.  
  161. /*
  162.  
  163.  
  164.        get pointer to the text-segment and
  165.        put out segment
  166.  
  167.  
  168. */
  169.        TextSeg = GetTextSeg(Seg,Base,Password);
  170.        if (TextSeg)
  171.        {
  172.         printf("\n%s\n",TextSeg)
  173.        }
  174.        else
  175.        {
  176.         printf("emty slot.\n");
  177.        };
  178.  
  179.  
  180.        FreeMem(mem,size);
  181.        }
  182.        else
  183.        {
  184.         printf("not enought memory.\n");
  185.        };
  186.       }
  187.       else
  188.       {
  189.        printf("this is not a sft-file.\n");
  190.       };
  191.       Close(fh);
  192.      }
  193.      else
  194.      {
  195.       printf("cannot open %s.\n",argv[1]);
  196.      };
  197.     };
  198.    FreeMem(fib,sizeof(struct FileInfoBlock));
  199.    }
  200.    else
  201.    {
  202.     printf("not enought memory.\n");
  203.    };
  204.   UnLock(flock);
  205.   }
  206.   else
  207.   {
  208.    printf("file not found.\n");
  209.   };
  210.  
  211.  
  212.  return(NULL);
  213.  
  214. } /* end of main */
  215.