xref: /netbsd-src/usr.bin/crunch/crunchide/exec_coff.c (revision cff555419110263654b37ec4508061f64aa8ffb4)
1*cff55541She /* $NetBSD: exec_coff.c,v 1.7 2009/08/20 17:39:51 he Exp $ */
217b68982Smsaitoh 
317b68982Smsaitoh /*
4db755e7cScgd  * Copyright (c) 1997 Christopher G. Demetriou
5db755e7cScgd  * All rights reserved.
617b68982Smsaitoh  *
717b68982Smsaitoh  * Redistribution and use in source and binary forms, with or without
817b68982Smsaitoh  * modification, are permitted provided that the following conditions
917b68982Smsaitoh  * are met:
1017b68982Smsaitoh  * 1. Redistributions of source code must retain the above copyright
1117b68982Smsaitoh  *    notice, this list of conditions and the following disclaimer.
1217b68982Smsaitoh  * 2. Redistributions in binary form must reproduce the above copyright
1317b68982Smsaitoh  *    notice, this list of conditions and the following disclaimer in the
1417b68982Smsaitoh  *    documentation and/or other materials provided with the distribution.
1517b68982Smsaitoh  * 3. All advertising materials mentioning features or use of this software
1617b68982Smsaitoh  *    must display the following acknowledgement:
17db755e7cScgd  *          This product includes software developed for the
180f5a0c15Ssalo  *          NetBSD Project.  See http://www.NetBSD.org/ for
19db755e7cScgd  *          information about NetBSD.
2017b68982Smsaitoh  * 4. The name of the author may not be used to endorse or promote products
21db755e7cScgd  *    derived from this software without specific prior written permission.
2217b68982Smsaitoh  *
2317b68982Smsaitoh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2417b68982Smsaitoh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2517b68982Smsaitoh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2617b68982Smsaitoh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2717b68982Smsaitoh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2817b68982Smsaitoh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2917b68982Smsaitoh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3017b68982Smsaitoh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3117b68982Smsaitoh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3217b68982Smsaitoh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33db755e7cScgd  *
34db755e7cScgd  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
3517b68982Smsaitoh  */
3617b68982Smsaitoh 
3717b68982Smsaitoh #include <sys/cdefs.h>
3817b68982Smsaitoh #ifndef lint
39*cff55541She __RCSID("$NetBSD: exec_coff.c,v 1.7 2009/08/20 17:39:51 he Exp $");
4017b68982Smsaitoh #endif
4117b68982Smsaitoh 
4217b68982Smsaitoh #include <stdio.h>
4317b68982Smsaitoh #include <stdlib.h>
4417b68982Smsaitoh #include <string.h>
4517b68982Smsaitoh #include <unistd.h>
4617b68982Smsaitoh #include <errno.h>
47490896d4Stsutsui #include <limits.h>
4817b68982Smsaitoh #include <sys/types.h>
4917b68982Smsaitoh #include <sys/stat.h>
5017b68982Smsaitoh 
5117b68982Smsaitoh #include "extern.h"
5217b68982Smsaitoh 
5317b68982Smsaitoh #if defined(NLIST_COFF)
5417b68982Smsaitoh 
55049ad0d9Smsaitoh #include <sys/exec_coff.h>
5617b68982Smsaitoh 
5717b68982Smsaitoh int
check_coff(int fd,const char * filename)5817b68982Smsaitoh check_coff(int fd, const char *filename)
5917b68982Smsaitoh {
6017b68982Smsaitoh 	struct coff_filehdr fh;
6117b68982Smsaitoh 	struct stat sb;
6217b68982Smsaitoh 
6317b68982Smsaitoh 	/*
6417b68982Smsaitoh 	 * Check the header to make sure it's an COFF file (of the
6517b68982Smsaitoh 	 * appropriate size).
6617b68982Smsaitoh 	 */
6717b68982Smsaitoh 	if (fstat(fd, &sb) == -1)
6817b68982Smsaitoh 		return 0;
6917b68982Smsaitoh 	if (sb.st_size > SIZE_T_MAX)
7017b68982Smsaitoh 		return 0;
7117b68982Smsaitoh 	if (read(fd, &fh, sizeof fh) != sizeof fh)
7217b68982Smsaitoh 		return 0;
7317b68982Smsaitoh 
7417b68982Smsaitoh 	if (COFF_BADMAG(&fh))
7517b68982Smsaitoh 		return 0;
7617b68982Smsaitoh 
7717b68982Smsaitoh 	return 1;
7817b68982Smsaitoh }
7917b68982Smsaitoh 
8017b68982Smsaitoh int
hide_coff(int fd,const char * filename)8117b68982Smsaitoh hide_coff(int fd, const char *filename)
8217b68982Smsaitoh {
8317b68982Smsaitoh 
8417b68982Smsaitoh 	fprintf(stderr, "%s: COFF executables not currently supported\n",
8517b68982Smsaitoh 		filename);
8617b68982Smsaitoh 	return 1;
8717b68982Smsaitoh }
8817b68982Smsaitoh 
8917b68982Smsaitoh #endif /* defined(NLIST_COFF) */
90