home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / ramfs102.zip / src / patch / patchram.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-20  |  1.9 KB  |  106 lines

  1. /* $Id: patchram.c,v 1.1.2.1 2002/10/21 00:11:40 root Exp $ */
  2.  
  3. #include "std32.h"
  4. #include "patchram.h"
  5.  
  6. /* Area seek operation (SEEK_SET only) */
  7.  
  8. static void aseek(struct area *a, unsigned long pos)
  9. {
  10.  a->pos=(pos<=a->len)?pos:a->len;
  11. }
  12.  
  13. /* Write operation */
  14.  
  15. void acs(unsigned char c, struct area *a)
  16. {
  17.  a->first[a->pos++]=c;
  18.  if(a->pos>a->len)
  19.   a->len=a->pos;
  20. }
  21.  
  22. /* 2-byte write operation */
  23.  
  24. void aws(unsigned short w, struct area *a)
  25. {
  26.  unsigned char *p;
  27.  
  28.  p=(unsigned char *)SSToDS(&w);
  29.  acs(p[0], a);
  30.  acs(p[1], a);
  31. }
  32.  
  33. /* 4-byte write operation */
  34.  
  35. void ads(unsigned long d, struct area *a)
  36. {
  37.  unsigned short *p;
  38.  
  39.  p=(unsigned short *)SSToDS(&d);
  40.  aws(p[0], a);
  41.  aws(p[1], a);
  42. }
  43.  
  44. /* Read operation */
  45.  
  46. static unsigned int aread(unsigned char *dest, unsigned int e, unsigned int c,
  47.                           struct area *a)
  48. {
  49.  unsigned int j, w=0;
  50.  
  51.  for(j=e*c; j>0; j--)
  52.  {
  53.   if(a->pos>=a->len)
  54.    return(w);
  55.   *dest++=a->first[a->pos++];
  56.   w++;
  57.  }
  58.  return(w);
  59. }
  60.  
  61. /* Verifies the bytes at location to match the pattern */
  62.  
  63. int verify(struct locator *l, struct area *stream, long offset)
  64. {
  65.  int i;
  66.  unsigned char c;
  67.  
  68.  if(l->offset<0)
  69.   return(1);
  70.  aseek(stream, offset+l->offset);
  71.  for(i=0; i<l->shift; i++)
  72.  {
  73.   if(aread((unsigned char *)SSToDS(&c), 1, 1, stream)==0)
  74.    break;
  75.   if(c==l->c)
  76.   {
  77.    if(verify(l+1, stream, offset+i+l->offset))
  78.     return(1);
  79.    aseek(stream, offset+l->offset+i+1);
  80.   }
  81.  }
  82.  return(0);
  83. }
  84.  
  85. /* Locate a patch strip in the memory */
  86.  
  87. long locate(struct locator *l, struct area *stream, unsigned long first)
  88. {
  89.  unsigned char buf[512];
  90.  int i, fetch;
  91.  long cur_pos=first;
  92.  
  93.  aseek(stream, first);
  94.  while((fetch=aread((unsigned char *)SSToDS(buf), 1, sizeof(buf), stream))>0)
  95.  {
  96.   for(i=0; i<fetch; i++)
  97.   {
  98.    if(buf[i]==l->c&&verify(l+1, stream, cur_pos+i))
  99.     return(cur_pos+i);
  100.   }
  101.   cur_pos+=fetch;
  102.   aseek(stream, cur_pos);
  103.  }
  104.  return(-1);
  105. }
  106.