home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / TEMPLATE / WMF2.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  3KB  |  148 lines

  1. /*
  2.  * WMF Import Filter - ADD METAFILE BITS
  3.  *
  4.  * LANGUAGE      : Microsoft C6.0
  5.  * MODEL         : medium
  6.  * ENVIRONMENT   : Microsoft Windows 3.0 SDK
  7.  * STATUS        : operational
  8.  *
  9.  * This module contains functions to validate a WMF file and to add the
  10.  * metafile bits from the file to the memory metafile being constructed
  11.  * by the filter to return to the calling application.
  12.  *
  13.  *    Eikon Systems, Inc.
  14.  *    989 East Hillsdale Blvd, Suite 260
  15.  *    Foster City, California 94404
  16.  *    415-349-4664
  17.  *
  18.  * 08/01/91 1.00 - David E. West - initial creation.
  19.  * 09/01/91 1.01 - Kevin P. Welch - minor revisions.
  20.  *
  21.  */
  22.  
  23. #define NOCOMM
  24.  
  25. #include <windows.h>
  26. #include <limits.h>
  27. #include <stdio.h>
  28. #include <dos.h>
  29. #include "wmf.h"
  30.  
  31. /* */
  32.  
  33. /*
  34.  * ValidateWMF( lpwmf ) : BOOL;
  35.  *
  36.  *    lpwmf           pointer to WMF header
  37.  *
  38.  * This function checks the already open file with the handle hfSrc to see
  39.  * if it is a valid WMF file.
  40.  *
  41.  * The return value is TRUE if the file is a valid WMF file, and FALSE
  42.  * otherwise.
  43.  *
  44.  */
  45.  
  46. BOOL FAR PASCAL ValidateWMF(
  47.     LPWMFHEADER        lpwmf )
  48. {
  49.     BOOL                fValid;
  50.     WORD                idChecksum;
  51.  
  52.     /* calculate checksum */
  53.     idChecksum = ComputeWMFChecksum( lpwmf );
  54.     fValid = (lpwmf->idChecksum == idChecksum);
  55.  
  56.     return fValid;
  57. }
  58.  
  59. /* */
  60.  
  61. /*
  62.  * ComputeWMFChecksum( lpwmf ) : WORD
  63.  *
  64.  *    lpwmf          pointer to metafile header
  65.  *
  66.  * This function computes the checksum for the given metafile header,
  67.  * which is an Aldus disk metafile header.  The checksum is the result
  68.  * of XORing the first 10 words in the file.
  69.  *
  70.  */
  71.  
  72. WORD FAR PASCAL ComputeWMFChecksum(
  73.     LPWMFHEADER        lpwmf )
  74. {
  75.     LPWORD            lp;
  76.     LPWORD            lpChecksum;
  77.     WORD                idChecksum;
  78.  
  79.     /* initialize checksum */
  80.     idChecksum = 0;
  81.  
  82.     /* calculate checksume */
  83.     lpChecksum = (LPWORD)&(lpwmf->idChecksum);
  84.     for (lp = (LPWORD)lpwmf; FP_OFF(lp) < FP_OFF(lpChecksum); ++lp)
  85.         idChecksum ^= *lp;
  86.  
  87.     return idChecksum;
  88. }
  89.  
  90. /* */
  91.  
  92. /*
  93.  * GetWMFBits( lphbMF, cbMF, hfSrc ) : WORD;
  94.  *
  95.  *    lphbMF         pointer to handle for metafile bits
  96.  *    cbMF           size of metafile bits
  97.  *    hfSrc          handle to file
  98.  *
  99.  * This function reads the metafile bits from the file hfSrc beginning at
  100.  * the current file position.  A memory block with size cbMF is allocated,
  101.  * and then the bytes are read from the file into this memory block.  The
  102.  * handle to the memory block is returned in *lphbMF.
  103.  *
  104.  * The return value is an IE_* Aldus-defined error code, or SUCCESS if there
  105.  * is no error.
  106.  *
  107.  */
  108.  
  109. WORD FAR PASCAL GetWMFBits(
  110.     LPHANDLE            lphbMF,
  111.     LONG                cbMF,
  112.     INT                hfSrc )
  113. {
  114.     WORD                idErr;
  115.     HANDLE            hbMF;    
  116.     LPSTR                lpbMF;
  117.  
  118.     /* initialize return value */
  119.     idErr = SUCCESS;
  120.  
  121.     /* allocate memory for metafile bits */
  122.     hbMF = GlobalAlloc( GHND, cbMF );
  123.     lpbMF = hbMF ? GlobalLock(hbMF) : NULL;
  124.     if (lpbMF)
  125.     {
  126.         /* read bits */
  127.         if (cbMF < USHRT_MAX - 1)
  128.         {
  129.             /* read memory block in one read */
  130.             _lread( hfSrc, lpbMF, (WORD)cbMF );
  131.         }
  132.         else
  133.         {
  134.             /* read memory block in several reads */
  135.             hread( hfSrc, (HPSTR)lpbMF, cbMF );
  136.         }
  137.  
  138.         /* unlock memory block & assign return handle */
  139.         GlobalUnlock( hbMF );
  140.         *lphbMF = hbMF;
  141.  
  142.     }
  143.     else
  144.         idErr = IE_MEM_FULL;
  145.  
  146.     return idErr;
  147. }
  148.