home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 December / PCO_1298.ISO / filesbbs / os2 / fn128os2.arj / FN128OS2.ZIP / fn128os2 / src / os2lx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-02  |  8.3 KB  |  228 lines

  1. /*
  2.  # $Id: os2lx.c,v 1.2 1998/04/10 10:26:06 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 "os2obj.h"
  41. #include "os2page.h"
  42. #include "trace.h"
  43. #include "morpher.h"
  44. #include "md5.h"
  45. #include "spans.h"
  46.  
  47. #define PAGESZ            4096
  48.  
  49. static int
  50. lxFile_read_hdr(int fd, lxfile_t *lx)
  51. {
  52.     int    i;
  53.  
  54.     i = read(fd, (char *) &lx->mz, sizeof(SIMPLE_MZ_EXE));
  55.     if (i != sizeof(SIMPLE_MZ_EXE) || lx->mz.magic != 0x5a4d) /* "MZ" as a short int */
  56.         return -1;
  57.  
  58.     i = lseek(fd, lx->mz.lfanew, SEEK_SET);
  59.     if (i != lx->mz.lfanew)
  60.         return -1;
  61.  
  62.     i = read(fd, (char *) &lx->lx, sizeof(LX_EXE));
  63.     if (i != sizeof(LX_EXE))
  64.         return -1;
  65.  
  66.     if (lx->lx.Magic != 0x584c            /* "LX" as a short int */
  67.         || lx->lx.PgSize != PAGESZ
  68.         || lx->lx.ModNumPgs <= 0
  69.         || lx->lx.NumObjects <= 0)
  70.         return -1;
  71.  
  72.     return 0;
  73. }
  74.  
  75. void
  76. lxFile_print_hdr(lxfile_t *lx)
  77. {
  78.     trace(6, ("t6>> mz.lfanew = %ld\n", lx->mz.lfanew));
  79.     trace(6, ("t6>> LX header: %d [0x%x] bytes\n", sizeof(lx->lx), sizeof(lx->lx)));
  80.  
  81.     trace(6, ("t6>> \n"));
  82.     trace(6, ("t6>> Magic =  %d  0x%x\n", lx->lx.Magic, lx->lx.Magic));
  83.     trace(6, ("t6>> ByteOrder =  %d  0x%x\n", lx->lx.ByteOrder, lx->lx.ByteOrder));
  84.     trace(6, ("t6>> WordOrder =  %d  0x%x\n", lx->lx.WordOrder, lx->lx.WordOrder));
  85.     trace(6, ("t6>> FormatLevel =  %ld  0x%lx\n", lx->lx.FormatLevel, lx->lx.FormatLevel));
  86.     trace(6, ("t6>> CpuType =  %d  0x%x\n", lx->lx.CpuType, lx->lx.CpuType));
  87.     trace(6, ("t6>> OSType =  %d  0x%x\n", lx->lx.OSType, lx->lx.OSType));
  88.     trace(6, ("t6>> ModVersion =  %ld  0x%lx\n", lx->lx.ModVersion, lx->lx.ModVersion));
  89.     trace(6, ("t6>> ModFlags =  %ld  0x%lx\n", lx->lx.ModFlags, lx->lx.ModFlags));
  90.     trace(6, ("t6>> ModNumPgs =  %ld  0x%lx\n", lx->lx.ModNumPgs, lx->lx.ModNumPgs));
  91.     trace(6, ("t6>> EIPObjNum =  %ld  0x%lx\n", lx->lx.EIPObjNum, lx->lx.EIPObjNum));
  92.     trace(6, ("t6>> EIP =  %ld  0x%lx\n", lx->lx.EIP, lx->lx.EIP));
  93.     trace(6, ("t6>> ESPObjNum =  %ld  0x%lx\n", lx->lx.ESPObjNum, lx->lx.ESPObjNum));
  94.     trace(6, ("t6>> Esp =  %ld  0x%lx\n", lx->lx.Esp, lx->lx.Esp));
  95.     trace(6, ("t6>> PgSize =  %ld  0x%lx\n", lx->lx.PgSize, lx->lx.PgSize));
  96.     trace(6, ("t6>> PgOfsShift =  %ld  0x%lx\n", lx->lx.PgOfsShift, lx->lx.PgOfsShift));
  97.     trace(6, ("t6>> FixupSectionSize =  %ld  0x%lx\n", lx->lx.FixupSectionSize, lx->lx.FixupSectionSize));
  98.     trace(6, ("t6>> FixupCksum =  %ld  0x%lx\n", lx->lx.FixupCksum, lx->lx.FixupCksum));
  99.     trace(6, ("t6>> LoaderSecSize =  %ld  0x%lx\n", lx->lx.LoaderSecSize, lx->lx.LoaderSecSize));
  100.     trace(6, ("t6>> LoaderSecCksum =  %ld  0x%lx\n", lx->lx.LoaderSecCksum, lx->lx.LoaderSecCksum));
  101.     trace(6, ("t6>> ObjTblOfs =  %ld  0x%lx\n", lx->lx.ObjTblOfs, lx->lx.ObjTblOfs));
  102.     trace(6, ("t6>> NumObjects =  %ld  0x%lx\n", lx->lx.NumObjects, lx->lx.NumObjects));
  103.     trace(6, ("t6>> ObjPgTblOfs =  %ld  0x%lx\n", lx->lx.ObjPgTblOfs, lx->lx.ObjPgTblOfs));
  104.     trace(6, ("t6>> ObjIterPgsOfs =  %ld  0x%lx\n", lx->lx.ObjIterPgsOfs, lx->lx.ObjIterPgsOfs));
  105.     trace(6, ("t6>> RscTblOfs =  %ld  0x%lx\n", lx->lx.RscTblOfs, lx->lx.RscTblOfs));
  106.     trace(6, ("t6>> NumRscTblEnt =  %ld  0x%lx\n", lx->lx.NumRscTblEnt, lx->lx.NumRscTblEnt));
  107.     trace(6, ("t6>> ResNameTblOfs =  %ld  0x%lx\n", lx->lx.ResNameTblOfs, lx->lx.ResNameTblOfs));
  108.     trace(6, ("t6>> EntryTblOfs =  %ld  0x%lx\n", lx->lx.EntryTblOfs, lx->lx.EntryTblOfs));
  109.     trace(6, ("t6>> ModDirOfs =  %ld  0x%lx\n", lx->lx.ModDirOfs, lx->lx.ModDirOfs));
  110.     trace(6, ("t6>> NumModDirs =  %ld  0x%lx\n", lx->lx.NumModDirs, lx->lx.NumModDirs));
  111.     trace(6, ("t6>> FixupPgTblOfs =  %ld  0x%lx\n", lx->lx.FixupPgTblOfs, lx->lx.FixupPgTblOfs));
  112.     trace(6, ("t6>> FixupRecTblOfs =  %ld  0x%lx\n", lx->lx.FixupRecTblOfs, lx->lx.FixupRecTblOfs));
  113.     trace(6, ("t6>> ImpModTblOfs =  %ld  0x%lx\n", lx->lx.ImpModTblOfs, lx->lx.ImpModTblOfs));
  114.     trace(6, ("t6>> NumImpModEnt =  %ld  0x%lx\n", lx->lx.NumImpModEnt, lx->lx.NumImpModEnt));
  115.     trace(6, ("t6>> ImpProcTblOfs =  %ld  0x%lx\n", lx->lx.ImpProcTblOfs, lx->lx.ImpProcTblOfs));
  116.     trace(6, ("t6>> PerPgCksumOfs =  %ld  0x%lx\n", lx->lx.PerPgCksumOfs, lx->lx.PerPgCksumOfs));
  117.     trace(6, ("t6>> DataPgOfs =  %ld  0x%lx\n", lx->lx.DataPgOfs, lx->lx.DataPgOfs));
  118.     trace(6, ("t6>> NumPreloadPg =  %ld  0x%lx\n", lx->lx.NumPreloadPg, lx->lx.NumPreloadPg));
  119.     trace(6, ("t6>> NResNameTblOfs =  %ld  0x%lx\n", lx->lx.NResNameTblOfs, lx->lx.NResNameTblOfs));
  120.     trace(6, ("t6>> NResNameTblLen =  %ld  0x%lx\n", lx->lx.NResNameTblLen, lx->lx.NResNameTblLen));
  121.     trace(6, ("t6>> NResNameTblCksum =  %ld  0x%lx\n", lx->lx.NResNameTblCksum, lx->lx.NResNameTblCksum));
  122.     trace(6, ("t6>> AutoDSObj =  %ld  0x%lx\n", lx->lx.AutoDSObj, lx->lx.AutoDSObj));
  123.     trace(6, ("t6>> DebugInfoOfs =  %ld  0x%lx\n", lx->lx.DebugInfoOfs, lx->lx.DebugInfoOfs));
  124.     trace(6, ("t6>> DebugInfoLen =  %ld  0x%lx\n", lx->lx.DebugInfoLen, lx->lx.DebugInfoLen));
  125.     trace(6, ("t6>> NumInstPreload =  %ld  0x%lx\n", lx->lx.NumInstPreload, lx->lx.NumInstPreload));
  126.     trace(6, ("t6>> NumInstDemand =  %ld  0x%lx\n", lx->lx.NumInstDemand, lx->lx.NumInstDemand));
  127.     trace(6, ("t6>> HeapSize =  %ld  0x%lx\n", lx->lx.HeapSize, lx->lx.HeapSize));
  128. }
  129.  
  130. lxfile_t *
  131. lxFile_is_lx(int fd)
  132. {
  133.     lxfile_t    *lx;
  134.  
  135.     lx = (lxfile_t *) _calloc(1, sizeof(lxfile_t));
  136.  
  137.     if (lxFile_read_hdr(fd, lx) == -1
  138.         || lxPages_build(fd, lx) == -1
  139.         || lxObj_build(lx, fd) == -1) {
  140.         free(lx);
  141.         return NULL;
  142.     }
  143.     return lx;
  144. }
  145.  
  146. void
  147. lxFile_free(lxfile_t *lx)
  148. {
  149.     int    i;
  150.  
  151.     if (lx) {
  152.         for (i = 0; i < lx->nlxObjects; i++)
  153.             free(lx->lxObjects[i]);
  154.         free(lx->lxObjects);
  155.         lx->lxObjects = NULL;
  156.         lx->nlxObjects = 0;
  157.  
  158.         for (i = 0; i < lx->npages; i++)
  159.             free(lx->pages[i]);
  160.         free(lx->pages);
  161.         lx->pages = NULL;
  162.         lx->npages = 0;
  163.     }
  164. }
  165.  
  166. static int
  167. lxFile_md5_page(int start, int stop, void *p1, void *p2, void *p3)
  168. {
  169.     unsigned char    *p;
  170.     int        pageno;
  171.     lxfile_t    *lx = (lxfile_t *) p1;
  172.     int        fd = (int) p2;
  173.     MD5_CTX        *c = (MD5_CTX *) p3;
  174.  
  175.     for (pageno = start; pageno <= stop; pageno++) {
  176.  
  177.         trace(4, ("t4>> LX MD5 calc for page %d\n", pageno));
  178.  
  179.         if (lxPages_uncompressed(lx, pageno) != 1) {
  180.             trace(3, ("t3>> LX page %d is compressed\n", pageno));
  181.             return ERR_LXCOMPR;
  182.         }
  183.  
  184.         p = lxPages_get_data(lx, pageno, fd);
  185.         if (!p)
  186.             return -1;
  187.  
  188.         MD5_Update(c, p, (unsigned long) lx->lx.PgSize);
  189.     }
  190.     return 0;
  191. }
  192.  
  193. char *
  194. lxFile_md5_calc(lxfile_t *lx, int fd, char *span, int *err)
  195. {
  196.     int            i, rtn;
  197.     unsigned char        *p;
  198.         MD5_CTX            c;
  199.         unsigned char           md[MD5_DIGEST_LENGTH];
  200.         static unsigned char       buf[512];
  201.  
  202.     MD5_Init(&c);
  203.  
  204.     rtn = do_spans(span, lxFile_md5_page, (void *) lx, (void *) fd, (void *) &c);
  205.     if (rtn != 0) {
  206.         *err = rtn;
  207.         return "";
  208.     }
  209.  
  210.     MD5_Final(md, &c);
  211.  
  212.         for (i = 0, p = buf; i < MD5_DIGEST_LENGTH; i++, p += 2)
  213.                 sprintf(p, "%02x", md[i]);
  214.     return buf;
  215. }
  216.  
  217. unsigned long
  218. lxFile_get_offset(lxfile_t *lx, unsigned long addr)
  219. {
  220.     unsigned long    page_base = (addr & ~(lx->lx.PgSize - 1));
  221.     unsigned long    page_offset = (addr & (lx->lx.PgSize - 1));
  222.  
  223.     page_base = lxPages_get_file_offset(lx, page_base);
  224.     if (page_base != 0)
  225.         return page_base + page_offset;
  226.     return 0;
  227. }
  228.