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

  1. /*
  2.  # $Id: morpher.c,v 1.7 1998/04/10 10:27:56 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 <ctype.h>
  34. #include <stdlib.h>
  35. #include <fcntl.h>
  36. #include <stdio.h>
  37. #include <unistd.h>
  38.  
  39. #include "misc.h"
  40. #include "index.h"
  41. #include "morpher.h"
  42. #include "os2lx.h"
  43.  
  44.  
  45. static void
  46. show_vec(FILE *f, unsigned char *src, int len, int format)
  47. {
  48.     if (len > 0) {
  49.         if (format == VEC_NUM_FMT) {
  50.             char pre = '[';
  51.             while (len-- > 0) {
  52.                 fprintf(f, "%c%02x", pre, (unsigned int) *src++);
  53.                 pre = ' ';
  54.             }
  55.             fprintf(f, "]");
  56.         } else if (format == VEC_STR_FMT) {
  57.             fputc('"', f);
  58.             for (; len-- > 0; src++) {
  59.                 if (isprint(*src))
  60.                     fputc(*src, f);
  61.                 else
  62.                     fprintf(f, "\\%03o", (unsigned int) *src);
  63.             }
  64.             fputc('"', f);
  65.         }
  66.     }
  67. }
  68.  
  69. #define MAXMORPHLEN    1024
  70.  
  71. static int
  72. apply_morphs(int tgt, morph_set *ms, int old_grade, int new_grade, int nowrite, int verbose, int *count)
  73. {
  74.     int        i;
  75.     int        cmp;
  76.     int        candidates = 0;
  77.     morph_t        *p;
  78.     off_t        offset;
  79.     unsigned char    tmp[MAXMORPHLEN];
  80.     vec_t        *old, *new;
  81.     int        errs = 0;
  82.  
  83.     for (i = 0; i < ms->morphs_sz; i++) {
  84.         p = ms->morphs[i];
  85.         if (p->is_context)
  86.             continue;
  87.  
  88.         if (new_grade < old_grade && p->grade <= old_grade && p->grade > new_grade) {
  89.             old = &p->new;
  90.             new = &p->old;
  91.         }
  92.         else if (new_grade > old_grade && p->grade > old_grade && p->grade <= new_grade) {
  93.             old = &p->old;
  94.             new = &p->new;
  95.         }
  96.         else
  97.             continue;
  98.  
  99.         candidates++;
  100.         if (new->len > MAXMORPHLEN) {
  101.             errs++;
  102.             fprintf(stderr,
  103.                 "Morph \"%s\": woops, this morph is too big!\n",
  104.                 p->name);
  105.             continue;
  106.         }
  107.         if (verbose) {
  108.             cmp = memcmp((void *) old->data, (void *) new->data, old->len);
  109.             if (cmp == 0) {
  110.                 fprintf(stderr,
  111.                     "Morph \"%s\": Warning: morph changes nothing ??!\n",
  112.                     p->name);
  113.             }
  114.  
  115.             printf("At file offset 0x%06lx, replacing ", p->filepos);
  116.             show_vec(stdout, old->data, old->len, old->format);
  117.             printf(" with ");
  118.             show_vec(stdout, new->data, new->len, new->format);
  119.             printf("\n");
  120.         }
  121.  
  122.         offset = lseek(tgt, (off_t) p->filepos, SEEK_SET);
  123.         if (offset == -1) {
  124.             errs++;
  125.             fprintf(stderr,
  126.                 "Morph \"%s\": error locating offset 0x%08lx [run-time 0x%lx]: ",
  127.                 p->name, p->filepos, p->base + p->offset);
  128.             perror("");
  129.             continue;
  130.         }
  131.  
  132.         if (read(tgt, (char *) tmp, old->len) != old->len) {
  133.             errs++;
  134.             fprintf(stderr,
  135.                 "Morph \"%s\": error reading %d bytes at 0x%08lx: ",
  136.                 p->name, old->len, p->filepos);
  137.             perror("");
  138.             continue;
  139.         }
  140.  
  141.         cmp = memcmp((void *) tmp, (void *) old->data, (size_t) old->len);
  142.         if (cmp != 0) {
  143.             errs++;
  144.             fprintf(stderr, "Morph \"%s\": data mismatch: expected ", p->name);
  145.             show_vec(stderr, old->data, old->len, VEC_NUM_FMT);
  146.             fprintf(stderr, ", found ");
  147.             show_vec(stderr, tmp, old->len, VEC_NUM_FMT);
  148.             fprintf(stderr, "\n");
  149.             continue;
  150.         }
  151.  
  152.         if (!nowrite) {
  153.             offset = lseek(tgt, (off_t) p->filepos, SEEK_SET);
  154.             if (offset == -1) {
  155.                 errs++;
  156.                 fprintf(stderr,
  157.                     "Morph \"%s\": error repositioning at address 0x%08lx [run-time 0x%lx]: ",
  158.                     p->name, p->filepos, p->base + p->offset);
  159.                 perror("");
  160.                 continue;
  161.             }
  162.             if (write(tgt, (char *) new->data, new->len) != new->len) {
  163.                 errs++;
  164.                 fprintf(stderr,
  165.                     "Morph \"%s\": error writing data to 0x%08lx [run-time 0x%lx]: ",
  166.                     p->name, p->filepos, p->base + p->offset);
  167.                 perror("");
  168.                 continue;
  169.             }
  170.         }
  171.     }
  172.     *count = candidates;
  173.     return errs;
  174. }
  175.  
  176. int
  177. morpher(char *target_name, char *morph_file, index_entry_t *ent, int nowrite, int old_grade, int new_grade, int verbose)
  178. {
  179.     int        i, tgt, errs, flags;
  180.     lxfile_t    *lx = NULL;
  181.     int        candidates = 0;
  182.     FILE        *mf;
  183.     morph_set    *ms;
  184.  
  185.     flags = nowrite? OPENFL(O_RDONLY): OPENFL(O_RDWR);
  186.     tgt = open(target_name, flags, 0666);
  187.     if (tgt == -1) {
  188.         fprintf(stderr, "Cannot open \"%s\": ", target_name);
  189.         perror("");
  190.         fprintf(stderr, "Perhaps you are running Netscape at the moment?\n");
  191.         return 1;
  192.     }
  193.  
  194.     mf = fopen(morph_file, "r");
  195.     if (mf == NULL) {
  196.         fprintf(stderr, "Woops: cannot open \"%s\": ", morph_file);
  197.         perror("");
  198.         fprintf(stderr, "Perhaps your Fortify distribution was not unpacked correctly?\n");
  199.         fprintf(stderr, "Please refer to the README file for full unpacking instructions.\n");
  200.         return 1;
  201.     }
  202.  
  203.     ms = parse(mf);
  204.  
  205.     if (is_lx_entry(ent))
  206.         lx = lxFile_is_lx(tgt);
  207.  
  208.     for (i = 0; i < ms->morphs_sz; i++) {
  209.         morph_t *p = ms->morphs[i];
  210.         if (lx)
  211.             p->filepos = lxFile_get_offset(lx, p->base + p->offset);
  212.         else
  213.             p->filepos = p->base + p->offset - ms->offsets[p->seg];
  214.     }
  215.  
  216.     errs = apply_morphs(tgt, ms, old_grade, new_grade, 1, verbose, (int *) &candidates);
  217.     if (errs == 0 && ms->nmorphs > 0 && nowrite == 0) {
  218.         errs = apply_morphs(tgt, ms, old_grade, new_grade, nowrite, 0, (int *) &candidates);
  219.     }
  220.     else {
  221.         fprintf(stderr,
  222.             "%d errors from %d potential morphs.\n",
  223.             errs, candidates);
  224.         fprintf(stderr, "No changes made to \"%s\".\n", target_name);
  225.     }
  226.  
  227.     close(tgt);
  228.     lxFile_free(lx);
  229.     fclose(mf);
  230.     return ((errs > 0)? 1: 0);
  231. }
  232.