~rabbits/uxn

uxn/src/devices/file.c -rw-r--r-- 3.7 KiB
055fa9bcAndrew Alderwick Close some security holes. 2 days ago
                                                                                
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "../uxn.h"
#include "file.h"

/*
Copyright (c) 2021 Devine Lu Linvega
Copyright (c) 2021 Andrew Alderwick

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

static FILE *f;
static DIR *dir;
static char *current_filename = "";
static struct dirent *de;

static enum { IDLE,
	FILE_READ,
	FILE_WRITE,
	DIR_READ } state;

static void
reset(void)
{
	if(f != NULL) {
		fclose(f);
		f = NULL;
	}
	if(dir != NULL) {
		closedir(dir);
		dir = NULL;
	}
	de = NULL;
	state = IDLE;
}

static Uint16
get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int fail_nonzero)
{
	struct stat st;
	if(len < strlen(basename) + 7)
		return 0;
	if(stat(pathname, &st))
		return fail_nonzero ? sprintf(p, "!!!! %s\n", basename) : 0;
	else if(S_ISDIR(st.st_mode))
		return sprintf(p, "---- %s\n", basename);
	else if(st.st_size < 0x10000)
		return sprintf(p, "%04x %s\n", (unsigned int)st.st_size, basename);
	else
		return sprintf(p, "???? %s\n", basename);
}

static Uint16
file_read_dir(char *dest, Uint16 len)
{
	static char pathname[4096];
	char *p = dest;
	if(de == NULL) de = readdir(dir);
	for(; de != NULL; de = readdir(dir)) {
		Uint16 n;
		if(de->d_name[0] == '.' && de->d_name[1] == '\0')
			continue;
		if(strlen(current_filename) + 1 + strlen(de->d_name) < sizeof(pathname))
			sprintf(pathname, "%s/%s", current_filename, de->d_name);
		else
			pathname[0] = '\0';
		n = get_entry(p, len, pathname, de->d_name, 1);
		if(!n) break;
		p += n;
		len -= n;
	}
	return p - dest;
}

static Uint16
file_init(void *filename)
{
	reset();
	current_filename = filename;
	return 0;
}

static Uint16
file_read(void *dest, Uint16 len)
{
	if(state != FILE_READ && state != DIR_READ) {
		reset();
		if((dir = opendir(current_filename)) != NULL)
			state = DIR_READ;
		else if((f = fopen(current_filename, "rb")) != NULL)
			state = FILE_READ;
	}
	if(state == FILE_READ)
		return fread(dest, 1, len, f);
	if(state == DIR_READ)
		return file_read_dir(dest, len);
	return 0;
}

static Uint16
file_write(void *src, Uint16 len, Uint8 flags)
{
	Uint16 ret = 0;
	if(state != FILE_WRITE) {
		reset();
		if((f = fopen(current_filename, (flags & 0x01) ? "ab" : "wb")) != NULL)
			state = FILE_WRITE;
	}
	if(state == FILE_WRITE) {
		if((ret = fwrite(src, 1, len, f)) > 0 && fflush(f) != 0)
			ret = 0;
	}
	return ret;
}

static Uint16
file_stat(void *dest, Uint16 len)
{
	char *basename = strrchr(current_filename, '/');
	if(basename != NULL)
		basename++;
	else
		basename = current_filename;
	return get_entry(dest, len, current_filename, basename, 0);
}

static Uint16
file_delete(void)
{
	return unlink(current_filename);
}

/* IO */

void
file_deo(Device *d, Uint8 port)
{
	Uint16 a, b, res;
	switch(port) {
	case 0x5:
		DEVPEEK16(a, 0x4);
		DEVPEEK16(b, 0xa);
		if(b > 0x10000 - a)
			b = 0x10000 - a;
		res = file_stat(&d->u->ram[a], b);
		DEVPOKE16(0x2, res);
		break;
	case 0x6:
		res = file_delete();
		DEVPOKE16(0x2, res);
		break;
	case 0x9:
		DEVPEEK16(a, 0x8);
		res = file_init(&d->u->ram[a]);
		DEVPOKE16(0x2, res);
		break;
	case 0xd:
		DEVPEEK16(a, 0xc);
		DEVPEEK16(b, 0xa);
		if(b > 0x10000 - a)
			b = 0x10000 - a;
		res = file_read(&d->u->ram[a], b);
		DEVPOKE16(0x2, res);
		break;
	case 0xf:
		DEVPEEK16(a, 0xe);
		DEVPEEK16(b, 0xa);
		if(b > 0x10000 - a)
			b = 0x10000 - a;
		res = file_write(&d->u->ram[a], b, d->dat[0x7]);
		DEVPOKE16(0x2, res);
		break;
	}
}