home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / unpost / part07 / ident.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-18  |  2.3 KB  |  78 lines

  1. /******************************************************************************
  2. * Module    :   Identify a binary file.
  3. *
  4. * Author    :   John Stevens
  5. ******************************************************************************/
  6.  
  7. #include    "compiler.h"
  8.  
  9. #include    "ident.h"
  10.  
  11. /*  Define file type information for static storage.    */
  12. typedef struct
  13. {
  14.     int             Offset;
  15.     int             length;
  16.     char            *string;
  17.     char            ext[5];
  18.     BIN_TYPES       FileType;
  19. } FILE_TYPES;
  20.  
  21. static  FILE_TYPES  FileTypes[] =
  22. {
  23.     {   0,  6,  "GIF87a",   ".gif",     GIF87A          },
  24.     {   0,  6,  "GIF89a",   ".gif",     GIF89A          },
  25.     {   6,  4,  "JFIF",     ".jpg",     JFIF_JPEG       },
  26.     {   0,  4,  "hsi1",     ".hsi",     HSI1_JPEG       },
  27.     {   0,  0,  "",         "",         UNKNOWN_TYPE    }
  28. };
  29.  
  30. /*-----------------------------------------------------------------------------
  31. | Routine   :   IdUUFile() --- Identify a uuencoded binary file from it's
  32. |               first line.  Currently this only works for graphics that
  33. |               I have access to.
  34. |
  35. | Inputs    :   Bfr - Buffer containing first line.
  36. |               Len - Number of bytes in buffer.
  37. |               Ext - Pointer to extension buffer.
  38. |
  39. | Returns   :   Returns the file type.
  40. -----------------------------------------------------------------------------*/
  41.  
  42. BIN_TYPES   IdUUFile(BYTE   *Bfr,
  43.                      int    Len,
  44.                      char   *Ext)
  45. {
  46.     register    int     i;
  47.     register    int     j;
  48.     auto        BYTE    *tp;
  49.  
  50.     /*  Run file types, looking for all listed types.   */
  51.     *Ext = '\0';
  52.     for (i = 0; FileTypes[i].length; i++)
  53.     {
  54.         /*  Make sure that there are enough bytes in the buffer
  55.         *   to identify this file.
  56.         */
  57.         if (FileTypes[i].length + (int) FileTypes[i].Offset > Len)
  58.             continue;
  59.  
  60.         /*  Compare the buffers.    */
  61.         for (j = 0, tp = Bfr + FileTypes[i].Offset;
  62.              j < FileTypes[i].length;
  63.              j++)
  64.             if (FileTypes[i].string[j] != (char) tp[j])
  65.                 break;
  66.  
  67.         /*  Check for file type found.  */
  68.         if (j == FileTypes[i].length)
  69.         {
  70.             strcpy(Ext, FileTypes[i].ext);
  71.             return( FileTypes[i].FileType );
  72.         }
  73.     }
  74.  
  75.     /*  Return type unknown.    */
  76.     return( UNKNOWN_TYPE );
  77. }
  78.