xref: /netbsd/usr.sbin/fstyp/cd9660.c (revision 59a9e119)
1*59a9e119Schristos /*	$NetBSD: cd9660.c,v 1.1 2018/01/09 03:31:15 christos Exp $	*/
2*59a9e119Schristos 
3*59a9e119Schristos /*-
4*59a9e119Schristos  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5*59a9e119Schristos  * Copyright (c) 2016 The DragonFly Project
6*59a9e119Schristos  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7*59a9e119Schristos  * Copyright (c) 2014 The FreeBSD Foundation
8*59a9e119Schristos  * All rights reserved.
9*59a9e119Schristos  *
10*59a9e119Schristos  * This code is derived from software contributed to The NetBSD Foundation
11*59a9e119Schristos  * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
12*59a9e119Schristos  *
13*59a9e119Schristos  * This software was developed by Edward Tomasz Napierala under sponsorship
14*59a9e119Schristos  * from the FreeBSD Foundation.
15*59a9e119Schristos  *
16*59a9e119Schristos  * Redistribution and use in source and binary forms, with or without
17*59a9e119Schristos  * modification, are permitted provided that the following conditions
18*59a9e119Schristos  * are met:
19*59a9e119Schristos  * 1. Redistributions of source code must retain the above copyright
20*59a9e119Schristos  *    notice, this list of conditions and the following disclaimer.
21*59a9e119Schristos  * 2. Redistributions in binary form must reproduce the above copyright
22*59a9e119Schristos  *    notice, this list of conditions and the following disclaimer in the
23*59a9e119Schristos  *    documentation and/or other materials provided with the distribution.
24*59a9e119Schristos  *
25*59a9e119Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
26*59a9e119Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*59a9e119Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*59a9e119Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
29*59a9e119Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*59a9e119Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*59a9e119Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*59a9e119Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*59a9e119Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*59a9e119Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*59a9e119Schristos  * SUCH DAMAGE.
36*59a9e119Schristos  */
37*59a9e119Schristos #include <sys/cdefs.h>
38*59a9e119Schristos __RCSID("$NetBSD: cd9660.c,v 1.1 2018/01/09 03:31:15 christos Exp $");
39*59a9e119Schristos 
40*59a9e119Schristos #include <sys/param.h>
41*59a9e119Schristos #include <stdio.h>
42*59a9e119Schristos #include <stdlib.h>
43*59a9e119Schristos #include <string.h>
44*59a9e119Schristos 
45*59a9e119Schristos #include "fstyp.h"
46*59a9e119Schristos 
47*59a9e119Schristos #define	ISO9660_MAGIC	"\x01" "CD001" "\x01\x00"
48*59a9e119Schristos #define	ISO9660_OFFSET	0x8000
49*59a9e119Schristos #define	VOLUME_LEN	32
50*59a9e119Schristos 
51*59a9e119Schristos int
fstyp_cd9660(FILE * fp,char * label,size_t size)52*59a9e119Schristos fstyp_cd9660(FILE *fp, char *label, size_t size)
53*59a9e119Schristos {
54*59a9e119Schristos 	char *sector, *volume;
55*59a9e119Schristos 
56*59a9e119Schristos 	sector = read_buf(fp, ISO9660_OFFSET, 512);
57*59a9e119Schristos 	if (sector == NULL)
58*59a9e119Schristos 		return 1;
59*59a9e119Schristos 	if (bcmp(sector, ISO9660_MAGIC, sizeof(ISO9660_MAGIC) - 1) != 0) {
60*59a9e119Schristos 		free(sector);
61*59a9e119Schristos 		return 1;
62*59a9e119Schristos 	}
63*59a9e119Schristos 	volume = sector + 0x28;
64*59a9e119Schristos 	bzero(label, size);
65*59a9e119Schristos 	strlcpy(label, volume, MIN(size, VOLUME_LEN));
66*59a9e119Schristos 	free(sector);
67*59a9e119Schristos 	rtrim(label, size);
68*59a9e119Schristos 	return 0;
69*59a9e119Schristos }
70