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

  1. /*
  2.  # $Id: index.c,v 1.11 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 <ctype.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <fcntl.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <unistd.h>
  41.  
  42. #include "misc.h"
  43. #include "index.h"
  44. #include "morpher.h"
  45. #include "md5_calc.h"
  46. #include "os2lx.h"
  47. #include "os2obj.h"
  48. #include "os2page.h"
  49. #include "trace.h"
  50.  
  51. #define VERN_STR    "version"
  52.  
  53. static int        idx_sz;
  54. static index_entry_t    **idx_ents;
  55.  
  56. int
  57. have_morphs(index_entry_t *ent)
  58. {
  59.     return (ent->flds[IDX_MORPHS] && ent->flds[IDX_MORPHS][0] != '\0');
  60. }
  61.  
  62. int
  63. build_index(char *indexfile, char **vern)
  64. {
  65.     char        buf[1024];
  66.     FILE        *ifp;
  67.     int        i, len, nflds;
  68.     int        vern_seen    = 0;
  69.     char        *p;
  70.     index_entry_t    *ent;
  71.  
  72.     idx_sz = 0;
  73.  
  74.     ifp = fopen(indexfile, "r");
  75.     if (ifp == NULL) {
  76.         fprintf(stderr, "Cannot open \"%s\": ", indexfile);
  77.         perror("");
  78.         return -1;
  79.     }
  80.  
  81.     while (fgets(buf, sizeof(buf), ifp)) {
  82.         if (*buf == '#' || *buf == '\n')
  83.             continue;
  84.         len = strlen(buf);
  85.         if (len <= 1)
  86.             continue;
  87.         
  88.         if (!vern_seen && strncmp(buf, VERN_STR, strlen(VERN_STR)) == 0) {
  89.             p = buf + strlen(VERN_STR);
  90.             while (isspace(*p))
  91.                 p++;
  92.             *vern = _strndup(p, strlen(p)-1);
  93.             vern_seen++;
  94.             continue;
  95.         }
  96.  
  97.         ent = (index_entry_t *) _calloc(1, sizeof(index_entry_t));
  98.  
  99.         ent->raw = p = _strndup(buf, len);
  100.  
  101.         for (i = nflds = 0; i < IDX_NFLDS; i++) {
  102.             while (isspace(*p))
  103.                 p++;
  104.             ent->flds[i] = p;
  105.             while (*p && !isspace(*p))
  106.                 p++;
  107.             *p = '\0';
  108.             if (strlen(ent->flds[i]) > 0)
  109.                 nflds++;
  110.             if (strcmp(ent->flds[i], "-") == 0)
  111.                 ent->flds[i] = p;
  112.  
  113.             if (p < ent->raw + len)
  114.                 p++;
  115.         }
  116.  
  117.         if (nflds != IDX_NFLDS) {
  118.             free(ent->raw);
  119.             free(ent);
  120.             continue;
  121.         }
  122.  
  123.         ent->size = atoi(ent->flds[IDX_SZ]);
  124.         ent->grade = atoi(ent->flds[IDX_GRADE]);
  125.         ent->max_grade = atoi(ent->flds[IDX_MAX_GRADE]);
  126.  
  127.         if (ent->grade == 0) {
  128.             ent->flds[IDX_GRADE] = "Export grade";
  129.         }
  130.         else if (ent->grade == ent->max_grade) {
  131.             ent->flds[IDX_GRADE] = "Fully Fortified";
  132.         }
  133.         else if (ent->grade == 1) {
  134.             ent->flds[IDX_GRADE] = "Fortified-SSL grade";
  135.         }
  136.         else {
  137.             ent->flds[IDX_GRADE] = "?? Unknown grade ??";
  138.         }
  139.  
  140.         idx_sz++;
  141.         idx_ents = (index_entry_t **) _realloc((void *) idx_ents,
  142.                 idx_sz * sizeof(index_entry_t *));
  143.         idx_ents[idx_sz - 1] = ent;
  144.     }
  145.  
  146.     fclose(ifp);
  147.     return idx_sz;
  148. }
  149.  
  150. int
  151. is_lx_entry(index_entry_t *ip)
  152. {
  153.     return (strcmp(ip->flds[IDX_SZ], "lx") == 0);
  154. }
  155.  
  156. static int
  157. is_script(int ifd)
  158. {
  159.     int        n;
  160.     static char    script_hdr[] = { '#', '!' };
  161.     char        tmp[sizeof(script_hdr)];
  162.  
  163.     lseek(ifd, 0L, SEEK_SET);
  164.     n = read(ifd, tmp, sizeof(tmp));
  165.     if (n != sizeof(tmp))
  166.         return -1;
  167.     return (memcmp(tmp, script_hdr, sizeof(tmp)) == 0);
  168. }
  169.  
  170. index_entry_t *
  171. index_lookup(char *tgt, int *err)
  172. {
  173.     int        i, ifd;
  174.     lxfile_t    *lx;
  175.     struct stat    st;
  176.     index_entry_t    *ip;
  177.     char        *md5 = NULL;
  178.     char        *md5span = NULL;
  179.  
  180.     trace(2, ("\nt2>> Index lookup:\n"));
  181.     *err = 0;
  182.  
  183.     if ((ifd = open(tgt, OPENFL(O_RDONLY), 0666)) == -1
  184.         || fstat(ifd, (struct stat *) &st) == -1) {
  185.         trace(2, ("t2>> error ERR_OPEN\n"));
  186.         close(ifd);
  187.         *err = ERR_OPEN;
  188.         return NULL;
  189.     }
  190.  
  191.     lx = lxFile_is_lx(ifd);
  192.     trace(2, ("t2>> file size = %ld\n", (long) st.st_size));
  193.     trace(2, ("t2>> file is%s LX format\n", lx? "": " not"));
  194.     if (lx) {
  195.         lxFile_print_hdr(lx);
  196.         lxObj_print(lx);
  197.         lxPages_print(lx);
  198.     }
  199.  
  200.     for (i = 0; i < idx_sz; i++) {
  201.         ip = idx_ents[i];
  202.         if (lx && is_lx_entry(ip)) {
  203.             if (md5 == NULL || strcmp(ip->flds[IDX_MD5SPAN], md5span) != 0) {
  204.                 md5 = lxFile_md5_calc(lx, ifd, ip->flds[IDX_MD5SPAN], err);
  205.                 md5span = ip->flds[IDX_MD5SPAN];
  206.                 if (*md5 == '\0' && *err == ERR_LXCOMPR) {
  207.                     close(ifd);
  208.                     lxFile_free(lx);
  209.                     return NULL;
  210.                 }
  211.                 trace(3, ("t3>> md5_calc = %s\n", md5));
  212.                 trace(3, ("t3>> md5_span = %s\n", md5span));
  213.             }
  214.             trace(3, ("t3>> match against %s\n", ip->flds[IDX_MD5]));
  215.             if (strcmp(ip->flds[IDX_MD5], md5) == 0) {
  216.                 close(ifd);
  217.                 lxFile_free(lx);
  218.                 return ip;
  219.             }
  220.         }
  221.         else if (ip->size == st.st_size) {
  222.             if (md5 == NULL || strcmp(ip->flds[IDX_MD5SPAN], md5span) != 0) {
  223.                 md5 = md5_calc(ifd, ip->flds[IDX_MD5SPAN]);
  224.                 md5span = ip->flds[IDX_MD5SPAN];
  225.                 trace(3, ("t3>> md5_calc = %s\n", md5));
  226.                 trace(3, ("t3>> md5_span = %s\n", md5span));
  227.             }
  228.             trace(3, ("t3>> match against %s\n", ip->flds[IDX_MD5]));
  229.             if (strcmp(ip->flds[IDX_MD5], md5) == 0) {
  230.                 close(ifd);
  231.                 return ip;
  232.             }
  233.         }
  234.     }
  235.  
  236.     if (st.st_size < 1000000 && is_script(ifd) == 1) {
  237.         trace(2, ("t2>> error ERR_ISSCRIPT\n"));
  238.         close(ifd);
  239.         *err = ERR_ISSCRIPT;
  240.         return NULL;
  241.     }
  242.  
  243.     close(ifd);
  244.     lxFile_free(lx);
  245.  
  246.     trace(2, ("t2>> no matches\n"));
  247.     *err = ERR_NOMATCH;
  248.     return NULL;
  249. }
  250.