Back to home page

MITgcm

 
 

    


File indexing completed on 2018-03-02 18:44:57 UTC

view on githubraw file Latest commit e768bd12 on 2008-02-26 17:05:00 UTC
ec6cf3b09d Ed H*0001 /* (C) Copyright 1993,1994 by Carnegie Mellon University
                0002  * All Rights Reserved.
                0003  *
                0004  * Permission to use, copy, modify, distribute, and sell this software
                0005  * and its documentation for any purpose is hereby granted without
                0006  * fee, provided that the above copyright notice appear in all copies
                0007  * and that both that copyright notice and this permission notice
                0008  * appear in supporting documentation, and that the name of Carnegie
                0009  * Mellon University not be used in advertising or publicity
                0010  * pertaining to distribution of the software without specific,
                0011  * written prior permission.  Carnegie Mellon University makes no
                0012  * representations about the suitability of this software for any
                0013  * purpose.  It is provided "as is" without express or implied
                0014  * warranty.
                0015  *
                0016  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
                0017  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
                0018  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
                0019  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                0020  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
                0021  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                0022  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
                0023  * SOFTWARE.
                0024  */
                0025 #include <stdio.h>
e768bd1221 Jean*0026 #include <string.h>
ec6cf3b09d Ed H*0027 
                0028 /* Description of the various file formats and their magic numbers */
                0029 struct magic {
                0030     char *name;         /* Name of the file format */
                0031     char *num;      /* The magic number */
                0032     int len;        /* Length of same (0 means strlen(magicnum)) */
                0033 };
                0034 
                0035 /* The magic numbers of the file formats we know about */
                0036 static struct magic magic[] = {
                0037     { "image/gif", "GIF", 0 },
                0038     { "image/jpeg", "\377\330\377", 0 },
                0039     { "video/mpeg", "\0\0\001\263", 4 },
                0040     { "application/postscript", "%!", 0 },
                0041 };
                0042 static int num_magic = (sizeof(magic)/sizeof(magic[0]));
                0043 static char *default_type = "application/octet-stream";
                0044 
                0045 /* The longest magic number */
                0046 static int max_magiclen = 0;
                0047 
                0048 /*
                0049  * Determins the format of the file "inputf".  The name
                0050  * of the file format (or NULL on error) is returned.
                0051  */
                0052 char *magic_look(FILE *infile)
                0053 {
                0054     int i, j;
                0055     char buf[80];
                0056     int numread = 0;
                0057 
                0058     if (max_magiclen == 0) {
                0059     for (i=0; i<num_magic; i++) {
                0060         if (magic[i].len == 0) magic[i].len = strlen(magic[i].num);
                0061         if (magic[i].len > max_magiclen) max_magiclen = magic[i].len;
                0062     }
                0063     }
                0064 
                0065     numread = fread(buf, 1, max_magiclen, infile);
                0066     rewind(infile);
                0067 
                0068     for (i=0; i<num_magic; i++) {
                0069     if (numread >= magic[i].len) {
                0070         for (j=0; j<magic[i].len; j++) {
                0071         if (buf[j] != magic[i].num[j]) break;
                0072         }
                0073         if (j == magic[i].len) return magic[i].name;
                0074     }
                0075     }
                0076 
                0077     return default_type;
                0078 }