home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / helper.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  9KB  |  385 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1991 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. #include "defines.h"
  7. #include "nasm.h"
  8. #include OSBIND
  9. #include "code.h"
  10. #include <stddef.h>     /* get size_t */
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "debug.h"
  14.  
  15. #if STATISTICS
  16. #include "stats.h"
  17. #include <stdio.h>
  18. #endif
  19.  
  20. extern char    outofmem[];
  21.  
  22. #if STATISTICS || ! VERSION
  23. #define MAXHISTORY   256
  24. #define MAXWATCHES   16
  25. #define MAGIC        0x41424344L
  26.  
  27. static struct
  28. {
  29.    byte huge   *p;
  30.    lword       size;
  31. } history[ MAXHISTORY];
  32.  
  33.  
  34. static struct watch
  35. {
  36.    unsigned long  magic;
  37.    unsigned char  *p;
  38.    char           *name;
  39.    short          size;
  40.    short          occupied;
  41.    unsigned long  chksum;
  42. } watches[ MAXWATCHES] = 
  43. {
  44.    { MAGIC, 0, 0, 0, 0, 0 },   /* fill this up appropriately */
  45.    { MAGIC, 0, 0, 0, 0, 0 },
  46.    { MAGIC, 0, 0, 0, 0, 0 },
  47.    { MAGIC, 0, 0, 0, 0, 0 },
  48.    { MAGIC, 0, 0, 0, 0, 0 },
  49.    { MAGIC, 0, 0, 0, 0, 0 },
  50.    { MAGIC, 0, 0, 0, 0, 0 },
  51.    { MAGIC, 0, 0, 0, 0, 0 },
  52.    { MAGIC, 0, 0, 0, 0, 0 },
  53.    { MAGIC, 0, 0, 0, 0, 0 },
  54.    { MAGIC, 0, 0, 0, 0, 0 },
  55.    { MAGIC, 0, 0, 0, 0, 0 },
  56.    { MAGIC, 0, 0, 0, 0, 0 },
  57.    { MAGIC, 0, 0, 0, 0, 0 },
  58.    { MAGIC, 0, 0, 0, 0, 0 },
  59.    { MAGIC, 0, 0, 0, 0, 0 }
  60. };
  61.  
  62.  
  63.  
  64. unsigned long chksum( p, size)
  65. unsigned char   *p;
  66. {
  67.    unsigned long   x;
  68.    int             i;
  69.  
  70.    x = *p++;
  71.    for( i = 3; i-- && size; size--)
  72.       x = (x << 8) + *p++;
  73.    while( size--)
  74.       x = (x << 3) + *p++;
  75.    return( x);
  76. }
  77.  
  78.  
  79. void  add_watch( p, size, name)
  80. unsigned char  *p;
  81. size_t         size;
  82. char           *name;
  83. {
  84.    struct watch  *q;
  85.    int           i;
  86.  
  87.    ENTER("add_watch");
  88.    if( ! p)
  89.       return;
  90.    for( q = watches, i = MAXWATCHES; i--; q++)
  91.       if( ! q->occupied)
  92.       {
  93.          q->magic    = MAGIC;
  94.          q->p        = p;
  95.          q->name     = name;
  96.          q->size     = size;
  97.          q->chksum   = chksum( p, (int) size);
  98.          q->occupied = 1;
  99.          LEAVE();
  100.          return;
  101.       }
  102.    nierror("Watch space exhausted");
  103. }
  104.  
  105.  
  106. print_watches()
  107. {
  108.    struct watch  *q;
  109.    int            i;
  110.  
  111.    printf("Active watches:\n");
  112.    for( q = watches, i = MAXWATCHES; i--; q++)
  113.       if( q->occupied)
  114.         printf( "\t%s\n", q->name);
  115. }
  116.  
  117.  
  118. void del_watch( p)
  119. unsigned char *p;
  120. {
  121.    struct watch  *q;
  122.    int            i;
  123.  
  124.    ENTER("del_watch");
  125.    if( ! p)
  126.       return;
  127.    for( q = watches, i = MAXWATCHES; i--; q++)
  128.       if( q->occupied && q->p == p)
  129.       {
  130.          q->occupied = 0;
  131.          LEAVE();
  132.          return;
  133.       }
  134.    nierror("Watch not found");
  135. }
  136.  
  137.  
  138. do_watch()
  139. {
  140.    struct watch  *q;
  141.    int            i;
  142.  
  143.    for( q = watches, i = MAXWATCHES; i--; q++)
  144.    {
  145.       if( q->magic != MAGIC)
  146.          nierror("Good work! You managed to clobber the watch space");
  147.  
  148.       if( q->occupied && q->chksum != chksum( q->p, q->size))
  149.       {
  150.          auto char buffer[ 256];
  151.  
  152.          if( q->name)
  153.             sprintf( buffer, "Someone clobbered >>%s<<", q->name);
  154.          else
  155.             sprintf( buffer, "Someone clobbered %d bytes @$%.8lX",
  156.                     q->size, (long) q->p);
  157.          nierror( buffer);
  158.       }
  159.    }
  160. }
  161.  
  162.  
  163. check_mallocs()
  164. {
  165.    int            i = 0;
  166.    byte huge      *p, huge *q, huge *r;
  167.  
  168.    for( i = 0; i < MAXHISTORY; i++)
  169.       if( p = (byte huge *) history[ i].p)
  170.       {
  171.          q = p - sizeof( lword);
  172.          r = p + history[ i].size;
  173.          if( lbeek( q) != MAGIC ||
  174.              lbeek( r) != MAGIC)
  175.             nierror("Someone clobbered the malloc space");
  176.       }
  177. }
  178.  
  179.  
  180.  
  181. do_check()
  182. {
  183.    ENTER("do_check");
  184.    do_watch();
  185.    check_mallocs();
  186.    LEAVE();
  187. }
  188.  
  189.  
  190.  
  191. bad_pointer( p)
  192. byte huge   *p;
  193. {
  194.    int         i = 0;
  195.    byte huge   *q;
  196.  
  197.    if( ! p)
  198.       return;
  199.    for( i = 0; i < MAXHISTORY; i++)
  200.       if( (q = history[ i].p) && p >= q && p < (q + history[ i].size))
  201.          return;
  202.    nierror("You got a bad pointer here");
  203. }
  204.  
  205. #endif
  206.  
  207.  
  208. byte huge  *nmalloc( size)
  209. lword   size;
  210. {
  211.    register byte volatile huge   *p;
  212.  
  213.    ENTER("nmalloc");
  214. #if ! VERSION
  215.    if( size > 0x01000000)
  216.       nwarning("Probably some conversion trouble when calling nmalloc");
  217.    size += sizeof( lword) * 2;
  218. #endif
  219.  
  220. #if STATISTICS
  221.    _mused += size;
  222.    if( _mused >= _maxused)
  223.       _maxused = _mused;
  224. #endif
  225.                            /* fucking TURBO.C libraries, nothing works */
  226. #if OS == TOS && __TURBOC__ && ! defined( __PUREC__)
  227.    if( ! (p = (byte huge *) malloc( size)) || (long) p < 0)
  228. #else
  229.    if( ! (p = (byte huge *) malloc( size)))
  230. #endif
  231.       nferror( outofmem);
  232. #if ! VERSION
  233.    memset( p, 0xFF, size);
  234. # if OS == MSDOS
  235.    if( get_ADDR( p) < 0x10000 || get_ADDR( p) > 0x400000)
  236.       nierror( "Pointer trouble in paradise");
  237. # endif
  238.    check_mallocs();
  239.    if( ((*p = 0x55) && *p != 0x55) || ((*p = 0xAA) && *p != 0xAA))
  240.       nierror("malloc is seriously fucked");
  241.  
  242.    lbyte( p, MAGIC);
  243.    size -= sizeof( lword) * 2;
  244.    p    += sizeof( lword);
  245.    lbyte( p + size, MAGIC);
  246. #endif
  247.  
  248. #if STATISTICS || ! VERSION
  249.    {
  250.       register int   i = 0;
  251.  
  252.       while( history[ i].p && i++ < MAXHISTORY);
  253.       if( i == MAXHISTORY)
  254.          nierror("Statistic space for nmalloc exhausted");
  255.       history[ i].p    = (byte huge *) p;
  256.       history[ i].size = size;
  257.    }
  258. #endif
  259.    IMESS("Memory starts @$%8.8lX", (lword) p, 4);
  260.    LEAVE();
  261.    return( (byte huge *) p);
  262. }
  263.  
  264. void  nfree( p)
  265. void huge   *p;
  266. {
  267.    ENTER("nfree");
  268.    if( ! p)             /* check for p == NULL (Can happen!) */
  269.       return;
  270. #if STATISTICS || ! VERSION
  271.    {
  272.       int   i = 0;
  273.  
  274.       while( history[ i].p != p && i++ < MAXHISTORY);
  275.       if( i == MAXHISTORY)
  276.          nierror("Faulty nfree");
  277. #if STATISTICS
  278.       _mused -= history[ i].size;
  279. #endif
  280.       memset( (char *) p - sizeof( lword), 0xFE, history[ i].size + 2 * sizeof( lword));
  281.       history[i].p = 0;
  282.    }
  283. #endif
  284. #if ! VERSION
  285.    (char huge *) p -= sizeof( lword);
  286. #endif
  287.    free( p);
  288.    LEAVE();
  289. }
  290.  
  291.  
  292. char  *get_filename( pathname)
  293. char  *pathname;
  294. {
  295.    register char  c,
  296.                   *p = pathname;
  297.  
  298.    while( (c = *p++) && c != DIRSLASH);
  299.    if( ! c)
  300.       return( pathname);
  301.    while( *p++);
  302.    while( *--p != DIRSLASH);
  303.    return( ++p);
  304. }
  305.  
  306.  
  307. void  complete( filename, ext, force)
  308. char  *filename, *ext;
  309. {
  310.    char  *x;
  311.  
  312.    ENTER("complete");
  313.  
  314.    if( x = strrchr( filename, '.'))
  315.    {
  316.       if( strrchr( filename, DIRSLASH) > x)
  317.          goto dis;
  318.       if( force)
  319.          while( *++x = *++ext);
  320.    }
  321.    else
  322. dis:
  323.       strcat( filename, ext);
  324.    LEAVE();
  325. }
  326.  
  327. #if LOWERFILE
  328. #include <ctype.h>
  329.  
  330. void  downcase( s)
  331. register char  *s;
  332. {
  333.    register char  c;
  334.    
  335.    while( c = *s)
  336.       *s++ = tolower( c);
  337. }
  338. #endif
  339. /* ---------------------------------------------------------- */
  340. /* Character representation (from Your Atari Computer)        */
  341. /*   $00-$1F  ->  $40-$5F    (+ $40)                          */
  342. /*   $20-$5F  ->  $00-$3F    (- $20)                          */
  343. /*   $60-$7F  ->  $60-$7F    (+-$00)                          */
  344. /*   $80-$9F  ->  $C0-$DF    (+ $40)                          */
  345. /*   $A0-$DF  ->  $80-$BF    (- $20)                          */
  346. /*   $EO-$FF  ->  $E0-$FF    (+-$00)                          */
  347. /* ---------------------------------------------------------- */
  348. byte  scrtab[] =
  349. {
  350.    0x40, 0x41, 0x42, 0x43,  0x44, 0x45, 0x46, 0x47,
  351.    0x48, 0x49, 0x4A, 0x4B,  0x4C, 0x4D, 0x4E, 0x4F,
  352.    0x50, 0x51, 0x52, 0x53,  0x54, 0x55, 0x56, 0x57,
  353.    0x58, 0x59, 0x5A, 0x5B,  0x5C, 0x5D, 0x5E, 0x5F,
  354.    0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07,
  355.    0x08, 0x09, 0x0A, 0x0B,  0x0C, 0x0D, 0x0E, 0x0F,
  356.    0x10, 0x11, 0x12, 0x13,  0x14, 0x15, 0x16, 0x17,
  357.    0x18, 0x19, 0x1A, 0x1B,  0x1C, 0x1D, 0x1E, 0x1F,
  358.    0x20, 0x21, 0x22, 0x23,  0x24, 0x25, 0x26, 0x27,
  359.    0x28, 0x29, 0x2A, 0x2B,  0x2C, 0x2D, 0x2E, 0x2F,
  360.    0x30, 0x31, 0x32, 0x33,  0x34, 0x35, 0x36, 0x37,
  361.    0x38, 0x39, 0x3A, 0x3B,  0x3C, 0x3D, 0x3E, 0x3F,
  362.    0x60, 0x61, 0x62, 0x63,  0x64, 0x65, 0x66, 0x67,
  363.    0x68, 0x69, 0x6A, 0x6B,  0x6C, 0x6D, 0x6E, 0x6F,
  364.    0x70, 0x71, 0x72, 0x73,  0x74, 0x75, 0x76, 0x77,
  365.    0x78, 0x79, 0x7A, 0x7B,  0x7C, 0x7D, 0x7E, 0x7F,
  366.  
  367.    0xC0, 0xC1, 0xC2, 0xC3,  0xC4, 0xC5, 0xC6, 0xC7,
  368.    0xC8, 0xC9, 0xCA, 0xCB,  0xCC, 0xCD, 0xCE, 0xCF,
  369.    0xD0, 0xD1, 0xD2, 0xD3,  0xD4, 0xD5, 0xD6, 0xD7,
  370.    0xD8, 0xD9, 0xDA, 0xDB,  0xDC, 0xDD, 0xDE, 0xDF,
  371.    0x80, 0x81, 0x82, 0x83,  0x84, 0x85, 0x86, 0x87,
  372.    0x88, 0x89, 0x8A, 0x8B,  0x8C, 0x8D, 0x8E, 0x8F,
  373.    0x90, 0x91, 0x92, 0x93,  0x94, 0x95, 0x96, 0x97,
  374.    0x98, 0x99, 0x9A, 0x9B,  0x9C, 0x9D, 0x9E, 0x9F,
  375.    0xA0, 0xA1, 0xA2, 0xA3,  0xA4, 0xA5, 0xA6, 0xA7,
  376.    0xA8, 0xA9, 0xAA, 0xAB,  0xAC, 0xAD, 0xAE, 0xAF,
  377.    0xB0, 0xB1, 0xB2, 0xB3,  0xB4, 0xB5, 0xB6, 0xB7,
  378.    0xB8, 0xB9, 0xBA, 0xBB,  0xBC, 0xBD, 0xBE, 0xBF,
  379.    0xE0, 0xE1, 0xE2, 0xE3,  0xE4, 0xE5, 0xE6, 0xE7,
  380.    0xE8, 0xE9, 0xEA, 0xEB,  0xEC, 0xED, 0xEE, 0xEF,
  381.    0xF0, 0xF1, 0xF2, 0xF3,  0xF4, 0xF5, 0xF6, 0xF7,
  382.    0xF8, 0xF9, 0xFA, 0xFB,  0xFC, 0xFD, 0xFE, 0xFF
  383. };
  384.  
  385.