home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 November / PCO_1198.ISO / filesbbs / os2 / fn127os2.arj / FN127OS2.ZIP / fn127os2 / src / os2obj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-25  |  5.6 KB  |  219 lines

  1. /*
  2.  # $Id: os2obj.c,v 1.2 1998/04/10 10:27:18 fbm Exp fbm $
  3.  # Copyright (C) 1997,1998 Farrell McKay
  4.  # All rights reserved.
  5.  #
  6.  # This file is part of the Fortify distribution, a toolkit for
  7.  # upgrading the cryptographic strength of the Netscape Navigator
  8.  # web browser, authored by Farrell McKay.
  9.  #
  10.  # This toolkit is provided to the recipient under the
  11.  # following terms and conditions:-
  12.  #   1.  This copyright notice must not be removed or modified.
  13.  #   2.  This toolkit may not be reproduced or included in any commercial
  14.  #       media distribution, or commercial publication (for example CD-ROM,
  15.  #       disk, book, magazine, journal) without first obtaining the author's
  16.  #       express permission.
  17.  #   3.  This toolkit, or any component of this toolkit, may not be
  18.  #       commercially resold, redeveloped, rewritten, enhanced or otherwise
  19.  #       used as the basis for commercial venture, without first obtaining
  20.  #       the author's express permission.
  21.  #   4.  Subject to the above conditions being observed (1-3), this toolkit
  22.  #       may be freely reproduced or redistributed.
  23.  #   5.  This software is provided "as-is", without express or implied
  24.  #       warranty.  In no event shall the author be liable for any direct,
  25.  #       indirect or consequential damages however caused.
  26.  #   6.  Subject to the above conditions being observed (1-5),
  27.  #       this toolkit may be used at no cost to the recipient.
  28.  #
  29.  # Farrell McKay
  30.  # Wayfarer Systems Pty Ltd        contact@fortify.net
  31.  */
  32.  
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37.  
  38. #include "misc.h"
  39. #include "os2lx.h"
  40. #include "os2page.h"
  41. #include "os2obj.h"
  42. #include "trace.h"
  43.  
  44.  
  45. static lxObject_t *
  46. lxObj_new
  47. (
  48.     unsigned long        _size,
  49.     unsigned long        _loadAddr,
  50.     unsigned long        _flags,
  51.     unsigned long        _pageTblIndex,
  52.     unsigned long        _nPageTblEntries
  53. )
  54. {
  55.     lxObject_t        *p;
  56.  
  57.     p = (lxObject_t *) _calloc(1, sizeof(lxObject_t));
  58.     p->size = _size;
  59.     p->loadAddr = _loadAddr;
  60.     p->flags = _flags;
  61.     p->pageTblIndex = _pageTblIndex;
  62.     p->nPageTblEntries = _nPageTblEntries;
  63.  
  64.     return p;
  65. }
  66.  
  67. static void
  68. lxObj_print_entry(int i, lxObject_t *lxObj)
  69. {
  70.     trace(6, ("t6>> Object %3d: %#10lx %#10lx %8ld %8ld %#8lx ",
  71.             i, lxObj->loadAddr, lxObj->size,
  72.             lxObj->pageTblIndex, lxObj->nPageTblEntries,
  73.             lxObj->flags));
  74.  
  75.     if (lxObj->flags) {
  76.         if (lxObj->flags & 0x1)
  77.             trace(6, (" RD"));
  78.         if (lxObj->flags & 0x2)
  79.             trace(6, (" WR"));
  80.         if (lxObj->flags & 0x4)
  81.             trace(6, (" EX"));
  82.         if (lxObj->flags & 0x8)
  83.             trace(6, (" RESRC"));
  84.         if (lxObj->flags & 0x10)
  85.             trace(6, (" DISCRD"));
  86.         if (lxObj->flags & 0x20)
  87.             trace(6, (" SHARED"));
  88.         if (lxObj->flags & 0x40)
  89.             trace(6, (" PRELOAD-PGS"));
  90.         if (lxObj->flags & 0x80)
  91.             trace(6, (" INVALID-PGS"));
  92.         if (lxObj->flags & 0x300)
  93.             trace(6, (" RES-CONTIG"));
  94.         else {
  95.             if (lxObj->flags & 0x100)
  96.                 trace(6, (" ZERO-PGS"));
  97.             if (lxObj->flags & 0x200)
  98.                 trace(6, (" RES"));
  99.         }
  100.         if (lxObj->flags & 0x400)
  101.             trace(6, (" RES-LONG-LOCK"));
  102.         if (lxObj->flags & 0x800)
  103.             trace(6, (" RESRVD"));
  104.         if (lxObj->flags & 0x1000)
  105.             trace(6, (" 16:16"));
  106.         if (lxObj->flags & 0x2000)
  107.             trace(6, (" BIG-BIT"));
  108.         if (lxObj->flags & 0x4000)
  109.             trace(6, (" CONFORM"));
  110.         if (lxObj->flags & 0x8000)
  111.             trace(6, (" IO-PRIV"));
  112.     }
  113.     trace(6, ("\n"));
  114. }
  115.  
  116. void
  117. lxObj_print(lxfile_t *lx)
  118. {
  119.     int    i;
  120.  
  121.     trace(6, ("t6>> \n"));
  122.     trace(6, ("t6>> Object        baseAddr     size   pgTblIdx pgTblEnts   flags\n"));
  123.     for (i = 0; i < lx->nlxObjects; i++)
  124.         lxObj_print_entry(i+1, lx->lxObjects[i]);
  125. }
  126.  
  127. static void
  128. lxObj_inject(lxfile_t *lx, lxObject_t *lxObj, int pgSize)
  129. {
  130.     int    j;
  131.  
  132.     for (j = 0; j < lxObj->nPageTblEntries; j++) {
  133.         lxPages_set_addr(lx, lxObj->pageTblIndex + j - 1, 
  134.                     lxObj->loadAddr + (j * pgSize));
  135.     }
  136. }
  137.  
  138. int
  139. lxObj_build(lxfile_t *lx, int fd)
  140. {
  141.     int        i, objno, off;
  142.     LX_OBJ        obj;
  143.  
  144.     off = lx->mz.lfanew + lx->lx.ObjTblOfs;
  145.     i = lseek(fd, off, SEEK_SET);
  146.         if (i != off) {
  147.                 fprintf(stderr, "LX-ObjTable: error locating offset %#08x: ", off);
  148.                 perror("");
  149.         return -1;
  150.     }
  151.  
  152.         lx->lxObjects = (lxObject_t **) _calloc(lx->lx.NumObjects, sizeof(lxObject_t *));
  153.         lx->nlxObjects = lx->lx.NumObjects;
  154.  
  155.         for (objno = 0; objno < lx->lx.NumObjects; objno++) {
  156.         i = read(fd, (char *) &obj, sizeof(obj));
  157.         if (i != sizeof(obj)) {
  158.                         fprintf(stderr, "LX-ObjTable %d: error reading %d bytes: ",
  159.                                 i, sizeof(obj));
  160.                         perror("");
  161.             return -1;
  162.         }
  163.  
  164.                 lx->lxObjects[objno] = lxObj_new(obj.size, obj.reloc_base_addr,
  165.                                 obj.obj_flags, obj.pg_tbl_index, obj.num_pg_tbl_entries);
  166.  
  167.         lxObj_inject(lx, lx->lxObjects[objno], lx->lx.PgSize);
  168.         }
  169.     return 0;
  170. }
  171.  
  172.  
  173. int
  174. lxObj_write_mem_image(lxfile_t *lx, int ifd, int ofd)
  175. {
  176.         int         i, objno, pageno;
  177.     long        addr;
  178.     lxObject_t    *obj;
  179.     char        *nulls;
  180.     unsigned char    *p;
  181.  
  182.         addr = lseek(ifd, 0L, SEEK_SET);
  183.         if (addr != 0)
  184.                 return -1;
  185.     
  186.     nulls = (char *) _calloc(lx->lx.PgSize, sizeof(char));
  187.  
  188.     trace(8, ("t8>> write_mem_image\n"));
  189.  
  190.         for (objno = 0; objno < lx->lx.NumObjects; objno++) {
  191.         obj = lx->lxObjects[objno];
  192.         if (obj->loadAddr == 0)
  193.             continue;
  194.         
  195.         trace(8, ("t8>> next obj addr is %#lx\n", obj->loadAddr));
  196.  
  197.         for (; addr < obj->loadAddr; addr += lx->lx.PgSize) {
  198.             i = write(ofd, nulls, lx->lx.PgSize);
  199.             if (i != lx->lx.PgSize) {
  200.                 free(nulls);
  201.                 return -1;
  202.             }
  203.         }
  204.  
  205.         for (pageno = 0; pageno < obj->nPageTblEntries; pageno++) {
  206.             p = lxPages_get_data(lx, obj->pageTblIndex + pageno - 1, ifd);
  207.             i = write(ofd, p, lx->lx.PgSize);
  208.             if (i != lx->lx.PgSize) {
  209.                 free(nulls);
  210.                 return -1;
  211.             }
  212.             addr += lx->lx.PgSize;
  213.         }
  214.     }
  215.  
  216.     free(nulls);
  217.     return 0;
  218. }
  219.