summaryrefslogtreecommitdiff
path: root/lib/util/fix_win32_filename.c
blob: d368eb0b12d41d0cd5ff151d18715fd55cb79684 (plain)
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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * fix_win32_filename.c
 *
 * Copyright (C) 2024 David Oberhollenzer <goliath@infraroot.at>
 */
#include "util/util.h"

#include <string.h>
#include <stdlib.h>

#ifdef _MSC_VER
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif

typedef struct {
	size_t used;
	size_t available;
	char buffer[];
} buffer_t;

static buffer_t *buffer_append(buffer_t *buf, const char *data, size_t count)
{
	size_t bufspace, needed;

	if (buf == NULL) {
		buf = calloc(1, sizeof(*buf) + 128);
		if (buf == NULL)
			return NULL;

		buf->used = 1;
		buf->available = 128;
		buf->buffer[0] = '\0';
	}

	bufspace = buf->available;
	needed = buf->used + count;

	while (bufspace < needed)
		bufspace += 128;

	if (bufspace != buf->available) {
		void *new_buf = realloc(buf, sizeof(*buf) + bufspace);
		if (new_buf == NULL) {
			free(buf);
			return NULL;
		}
		buf = new_buf;
		buf->available = bufspace;
	}

	buf->used -= 1;
	memcpy(buf->buffer + buf->used, data, count);
	buf->used += count;
	buf->buffer[buf->used++] = '\0';
	return buf;
}

static const char *bad_names[] = {
	"CON", "PRN", "AUX", "NUL",
	"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
	"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
};

static buffer_t *handle_component(buffer_t *buf, const char *comp, size_t len)
{
	for (size_t i = 0; i < sizeof(bad_names) / sizeof(bad_names[0]); ++i) {
		size_t badlen = strlen(bad_names[i]);

		if (badlen > len || strncasecmp(comp, bad_names[i], badlen))
			continue;

		while (badlen < len && comp[badlen] == '_')
			++badlen;

		if (badlen == len) {
			buf = buffer_append(buf, comp, len);
			if (buf != NULL)
				buf = buffer_append(buf, "_", 1);
			return buf;
		}
	}

	while (len > 0) {
		sqfs_u8 value, rep[3];
		size_t i = 0;

		for (i = 0; i < len; ++i) {
			if (comp[i] < 0x20 || comp[i] == 0x7F)
				break;
			if (comp[i] == '<' || comp[i] == '>' || comp[i] == ':')
				break;
			if (comp[i] == '|' || comp[i] == '?' || comp[i] == '*')
				break;
			if (comp[i] == '\\' || comp[i] == '\"')
				break;
		}

		if (i > 0) {
			buf = buffer_append(buf, comp, i);
			if (buf == NULL || i == len)
				break;
		}

		value = comp[i++];
		comp += i;
		len -= i;

		rep[0] = 0xEF;
		rep[1] = 0x80 | ((value >> 6) & 0x3f);
		rep[2] = 0x80 | ( value       & 0x3f);

		buf = buffer_append(buf, (const char *)rep, 3);
		if (buf == NULL)
			break;
	}

	return buf;
}

static buffer_t *handle_name(buffer_t *buf, const char *name, size_t len)
{
	char *sep;

	while ((sep = memchr(name, '.', len)) != NULL) {
		buf = handle_component(buf, name, sep - name);
		if (buf == NULL)
			return NULL;

		buf = buffer_append(buf, ".", 1);
		if (buf == NULL)
			return NULL;

		len -= sep - name + 1;
		name = sep + 1;
	}

	return handle_component(buf, name, len);
}

char *fix_win32_filename(const char *path)
{
	buffer_t *buf = NULL;
	char *sep, *out;
	size_t len;

	while ((sep = strchr(path, '/')) != NULL) {
		buf = handle_name(buf, path, sep - path);
		if (buf == NULL)
			return NULL;

		buf = buffer_append(buf, "/", 1);
		if (buf == NULL)
			return NULL;

		path = sep + 1;
	}

	buf = handle_name(buf, path, strlen(path));
	if (buf == NULL)
		return NULL;

	len = buf->used;
	memmove(buf, buf->buffer, len);

	out = realloc(buf, len);
	if (out == NULL)
		out = (char *)buf;

	return out;
}