File: ntcat.c

package info (click to toggle)
ntfs 971218-4
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 692 kB
  • ctags: 670
  • sloc: ansic: 7,774; sh: 1,509; makefile: 232
file content (169 lines) | stat: -rw-r--r-- 3,573 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 *  ntcat.c
 *
 *  Copyright (C) 1995-1997 Martin von Lwis
 *  Copyright (C) 1997 Rgis Duchesne
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#define getopt_long(a,v,o,ol,x)        getopt(a,v,o)
#endif
#include <unistd.h>
#include <string.h>
#include "types.h"
#include "struct.h"
#include "util.h"
#include "inode.h"
#include "nttools.h"

char *short_opts="f:o:s:B:b:1Vh";
#ifdef HAVE_GETOPT_H
struct option options[]={
	{"filesystem",1,0,'f'},
	{"offset",1,0,'o'},
	{"size",1,0,'s'},
	{"bias",1,0,'B'},
	{"buflen",1,0,'b'},
	{"8859-1",0,0,'1'},
	{"version",0,0,'V'},
	{"help",0,0,'h'},
	{0,0,0,0}
};
#endif

char usage_str[]=
"ntcat: dumps the contents of an NTFS file to stdout\n"
"ntcat [OPTIONS] [directory/]filename\n"
"  --filesystem, -f device   Use device\n"
"  --offset, -o offset	     Use offset\n"
"  --size, -s size           Only first size bytes\n"
"  --bias, -B bias           Use partition bias\n"
"  --buflen, -b buflen       Read with buffer size buflen\n"
"  --8859-1, -1              Use charset ISO-8859-1\n"
"                            (default is UTF-8)\n"
"  --version, -V             Display version\n"
"  --help, -h                Display this message\n"
;


void usage(void)
{
/*	fprintf(stderr,"ntcat [-f device] [-o offset] [-l size] [-1] [directory/]filename\n");*/
        fprintf(stderr,usage_str);

}

/* print the file ino on stdout */
void ntfs_cat_file(ntfs_inode *ino,int offset,int length,int bufsize)
{
	int position;
	char *buf;
	ntfs_io io;

	position=offset;
	if(!bufsize) bufsize=8192;
	buf=(char*)malloc(bufsize);
	/* Very large file */
	if(!buf)
	{
		bufsize=8192;
		buf=(char*)malloc(bufsize);
		if(!buf)
		{
			perror("ntcat");
			exit(0);
		}
	}
	io.fn_put=ntfs_put;
	io.fn_get=0;
	io.do_read=1;
	io.param=buf;
	io.size=bufsize;
	/* read from the unnamed data attribute */
	while(ntfs_read_attr(ino,ino->vol->at_data,
			     NULL,position,&io)==0 && io.size)
	{
		write(1,buf,io.size);
		position+=io.size;
		length-=io.size;
		if(length==0)
			break;
		io.param=buf;
		io.size=bufsize;
	}
	free(buf);
}

int main(int argc,char *argv[])
{
	int c;
	char *device=0;
	int offset=0;
	int length=0;
	char *name;
	ntfs_volume *volume;
	ntfs_inode ino;
	int inum;
	int bias=0;
	int buflen=8192;
	extern int opterr,optind;
	extern char* optarg;
	unsigned int charset=nct_utf8;

	opterr=1;
	while((c=getopt_long(argc,argv,short_opts,options,NULL))>0)
		switch(c)
		{
		case 'f': device=optarg;break;
		case 'o': offset=strtol(optarg,NULL,0);break;
		case 's': length=strtol(optarg,NULL,0);break;
		case 'B': bias=strtol(optarg,NULL,0);break;
		case 'b': buflen=strtol(optarg,NULL,0);break;
		case '1': charset=nct_iso8859_1;break;
		case 'V': printf("ntcat " NTFS_VERSION "\n");exit(0);break;
		case 'h': usage();exit(0);break;
		}
	name=argv[optind];
	volume=ntfs_open_volume(device,bias,1,0);
	if(!volume)return 1;
	volume->nct=charset;
	inum=5;
	/* walk the directory tree */
	do{
		char *next;
		if(ntfs_init_inode(&ino,volume,inum)){
			fprintf(stderr,"error finding %s\n",name);
			return 1;
		}
		if(!name || !*name)break;
		next=strchr(name,'/');
		if(next){
			*next='\0';
			next++;
		}
		inum=ntfs_find_file(&ino,name);
		if(inum==-1){
			fprintf(stderr,"%s not found\n",name);
			return 1;
		}
		name=next;
	}while(1);
	if(ntfs_init_inode(&ino,volume,inum))
		fprintf(stderr,"error opening %s\n",name);
	ntfs_cat_file(&ino,offset,length,buflen);
	return 0;
}

/*
 * Local variables:
 * c-file-style: "linux"
 * End:
 */