Home | History | Annotate | Line # | Download | only in fstyp
ntfs.c revision 1.2
      1  1.2   tkusumi /*	$NetBSD: ntfs.c,v 1.2 2019/12/28 08:22:30 tkusumi Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5  1.1  christos  * Copyright (c) 2016 The DragonFly Project
      6  1.1  christos  * Copyright (c) 2005 Takanori Watanabe
      7  1.1  christos  * Copyright (c) 2014 The FreeBSD Foundation
      8  1.1  christos  * All rights reserved.
      9  1.1  christos  *
     10  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
     11  1.1  christos  * by Tomohiro Kusumi <kusumi.tomohiro (at) gmail.com>.
     12  1.1  christos  *
     13  1.1  christos  * This software was developed by Edward Tomasz Napierala under sponsorship
     14  1.1  christos  * from the FreeBSD Foundation.
     15  1.1  christos  *
     16  1.1  christos  * Redistribution and use in source and binary forms, with or without
     17  1.1  christos  * modification, are permitted provided that the following conditions
     18  1.1  christos  * are met:
     19  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     20  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     21  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  christos  *    documentation and/or other materials provided with the distribution.
     24  1.1  christos  *
     25  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     29  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  christos  * SUCH DAMAGE.
     36  1.1  christos  */
     37  1.1  christos #include <sys/cdefs.h>
     38  1.2   tkusumi __RCSID("$NetBSD: ntfs.c,v 1.2 2019/12/28 08:22:30 tkusumi Exp $");
     39  1.1  christos 
     40  1.2   tkusumi #include <err.h>
     41  1.2   tkusumi #include <iconv.h>
     42  1.1  christos #include <stdint.h>
     43  1.1  christos #include <stdio.h>
     44  1.1  christos #include <stdlib.h>
     45  1.1  christos #include <string.h>
     46  1.1  christos 
     47  1.1  christos #include "fstyp.h"
     48  1.1  christos 
     49  1.1  christos #define	NTFS_A_VOLUMENAME	0x60
     50  1.1  christos #define	NTFS_FILEMAGIC		((uint32_t)(0x454C4946))
     51  1.1  christos #define	NTFS_VOLUMEINO		3
     52  1.1  christos 
     53  1.1  christos struct ntfs_attr {
     54  1.1  christos 	uint32_t	a_type;
     55  1.1  christos 	uint32_t	reclen;
     56  1.1  christos 	uint8_t		a_flag;
     57  1.1  christos 	uint8_t		a_namelen;
     58  1.1  christos 	uint8_t		a_nameoff;
     59  1.1  christos 	uint8_t		reserved1;
     60  1.1  christos 	uint8_t		a_compression;
     61  1.1  christos 	uint8_t		reserved2;
     62  1.1  christos 	uint16_t	a_index;
     63  1.1  christos 	uint16_t	a_datalen;
     64  1.1  christos 	uint16_t	reserved3;
     65  1.1  christos 	uint16_t	a_dataoff;
     66  1.1  christos 	uint16_t	a_indexed;
     67  1.1  christos } __packed;
     68  1.1  christos 
     69  1.1  christos struct ntfs_filerec {
     70  1.1  christos 	uint32_t	fr_hdrmagic;
     71  1.1  christos 	uint16_t	fr_hdrfoff;
     72  1.1  christos 	uint16_t	fr_hdrfnum;
     73  1.1  christos 	uint8_t		reserved[8];
     74  1.1  christos 	uint16_t	fr_seqnum;
     75  1.1  christos 	uint16_t	fr_nlink;
     76  1.1  christos 	uint16_t	fr_attroff;
     77  1.1  christos 	uint16_t	fr_flags;
     78  1.1  christos 	uint32_t	fr_size;
     79  1.1  christos 	uint32_t	fr_allocated;
     80  1.1  christos 	uint64_t	fr_mainrec;
     81  1.1  christos 	uint16_t	fr_attrnum;
     82  1.1  christos } __packed;
     83  1.1  christos 
     84  1.1  christos struct ntfs_bootfile {
     85  1.1  christos 	uint8_t		reserved1[3];
     86  1.1  christos 	uint8_t		bf_sysid[8];
     87  1.1  christos 	uint16_t	bf_bps;
     88  1.1  christos 	uint8_t		bf_spc;
     89  1.1  christos 	uint8_t		reserved2[7];
     90  1.1  christos 	uint8_t		bf_media;
     91  1.1  christos 	uint8_t		reserved3[2];
     92  1.1  christos 	uint16_t	bf_spt;
     93  1.1  christos 	uint16_t	bf_heads;
     94  1.1  christos 	uint8_t		reserver4[12];
     95  1.1  christos 	uint64_t	bf_spv;
     96  1.1  christos 	uint64_t	bf_mftcn;
     97  1.1  christos 	uint64_t	bf_mftmirrcn;
     98  1.1  christos 	int8_t		bf_mftrecsz;
     99  1.1  christos 	uint32_t	bf_ibsz;
    100  1.1  christos 	uint32_t	bf_volsn;
    101  1.1  christos } __packed;
    102  1.1  christos 
    103  1.2   tkusumi static void
    104  1.2   tkusumi convert_label(const void *label /* LE */, size_t labellen, char *label_out,
    105  1.2   tkusumi     size_t label_sz)
    106  1.2   tkusumi {
    107  1.2   tkusumi 	char *label_out_orig;
    108  1.2   tkusumi 	iconv_t cd;
    109  1.2   tkusumi 	size_t rc;
    110  1.2   tkusumi 
    111  1.2   tkusumi 	/* dstname="" means convert to the current locale. */
    112  1.2   tkusumi 	cd = iconv_open("", NTFS_ENC);
    113  1.2   tkusumi 	if (cd == (iconv_t)-1) {
    114  1.2   tkusumi 		warn("ntfs: Could not open iconv");
    115  1.2   tkusumi 		return;
    116  1.2   tkusumi 	}
    117  1.2   tkusumi 
    118  1.2   tkusumi 	label_out_orig = label_out;
    119  1.2   tkusumi 
    120  1.2   tkusumi 	rc = iconv(cd, __UNCONST(&label), &labellen, &label_out,
    121  1.2   tkusumi 	    &label_sz);
    122  1.2   tkusumi 	if (rc == (size_t)-1) {
    123  1.2   tkusumi 		warn("ntfs: iconv()");
    124  1.2   tkusumi 		*label_out_orig = '\0';
    125  1.2   tkusumi 	} else {
    126  1.2   tkusumi 		/* NUL-terminate result (iconv advances label_out). */
    127  1.2   tkusumi 		if (label_sz == 0)
    128  1.2   tkusumi 			label_out--;
    129  1.2   tkusumi 		*label_out = '\0';
    130  1.2   tkusumi 	}
    131  1.2   tkusumi 
    132  1.2   tkusumi 	iconv_close(cd);
    133  1.2   tkusumi }
    134  1.2   tkusumi 
    135  1.1  christos int
    136  1.1  christos fstyp_ntfs(FILE *fp, char *label, size_t size)
    137  1.1  christos {
    138  1.1  christos 	struct ntfs_bootfile *bf;
    139  1.1  christos 	struct ntfs_filerec *fr;
    140  1.1  christos 	struct ntfs_attr *atr;
    141  1.1  christos 	off_t voloff;
    142  1.1  christos 	char *filerecp, *ap;
    143  1.1  christos 	int8_t mftrecsz;
    144  1.2   tkusumi 	size_t recsize;
    145  1.1  christos 
    146  1.1  christos 	filerecp = NULL;
    147  1.1  christos 
    148  1.1  christos 	bf = read_buf(fp, 0, 512);
    149  1.1  christos 	if (bf == NULL || strncmp((char*)bf->bf_sysid, "NTFS    ", 8) != 0)
    150  1.1  christos 		goto fail;
    151  1.2   tkusumi 	if (!show_label)
    152  1.2   tkusumi 		goto ok;
    153  1.1  christos 
    154  1.1  christos 	mftrecsz = bf->bf_mftrecsz;
    155  1.1  christos 	recsize = mftrecsz > 0 ? (size_t)(mftrecsz * bf->bf_bps * bf->bf_spc)
    156  1.1  christos 	    : (size_t)(1 << -mftrecsz);
    157  1.1  christos 
    158  1.2   tkusumi 	voloff = (off_t)((off_t)bf->bf_mftcn * bf->bf_spc * bf->bf_bps +
    159  1.2   tkusumi 	    (off_t)recsize * NTFS_VOLUMEINO);
    160  1.1  christos 
    161  1.1  christos 	filerecp = read_buf(fp, voloff, recsize);
    162  1.1  christos 	if (filerecp == NULL)
    163  1.1  christos 		goto fail;
    164  1.1  christos 	fr = (struct ntfs_filerec *)filerecp;
    165  1.1  christos 
    166  1.1  christos 	if (fr->fr_hdrmagic != NTFS_FILEMAGIC)
    167  1.1  christos 		goto fail;
    168  1.1  christos 
    169  1.1  christos 	for (ap = filerecp + fr->fr_attroff;
    170  1.1  christos 	    atr = (struct ntfs_attr *)ap, (int)atr->a_type != -1;
    171  1.1  christos 	    ap += atr->reclen) {
    172  1.2   tkusumi 		if (atr->a_type != NTFS_A_VOLUMENAME)
    173  1.2   tkusumi 			continue;
    174  1.2   tkusumi 
    175  1.2   tkusumi 		convert_label(ap + atr->a_dataoff,
    176  1.2   tkusumi 		    atr->a_datalen, label, size);
    177  1.2   tkusumi 		break;
    178  1.1  christos 	}
    179  1.1  christos 
    180  1.2   tkusumi ok:
    181  1.1  christos 	free(bf);
    182  1.1  christos 	free(filerecp);
    183  1.1  christos 
    184  1.1  christos 	return 0;
    185  1.1  christos 
    186  1.1  christos fail:
    187  1.1  christos 	free(bf);
    188  1.1  christos 	free(filerecp);
    189  1.1  christos 
    190  1.1  christos 	return 1;
    191  1.1  christos }
    192