about summary refs log tree commit diff
path: root/pls.c
blob: b7240333605733f3352e1b50f713cfbf7a5d69c8 (plain) (blame)
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
185
186
/* Copyright (C) 2018  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

#include "chat.h"

wchar_t *wcsnchr(const wchar_t *wcs, size_t len, wchar_t chr) {
	len = wcsnlen(wcs, len);
	for (size_t i = 0; i < len; ++i) {
		if (wcs[i] == chr) return (wchar_t *)&wcs[i];
	}
	return NULL;
}

wchar_t *wcsnrchr(const wchar_t *wcs, size_t len, wchar_t chr) {
	len = wcsnlen(wcs, len);
	for (size_t i = len - 1; i < len; --i) {
		if (wcs[i] == chr) return (wchar_t *)&wcs[i];
	}
	return NULL;
}

wchar_t *ambstowcs(const char *src) {
	size_t len = mbsrtowcs(NULL, &src, 0, NULL);
	if (len == (size_t)-1) return NULL;

	wchar_t *dst = malloc(sizeof(*dst) * (1 + len));
	if (!dst) return NULL;

	len = mbsrtowcs(dst, &src, len, NULL);
	if (len == (size_t)-1) {
		free(dst);
		return NULL;
	}

	dst[len] = L'\0';
	return dst;
}

char *awcstombs(const wchar_t *src) {
	size_t len = wcsrtombs(NULL, &src, 0, NULL);
	if (len == (size_t)-1) return NULL;

	char *dst = malloc(sizeof(*dst) * (1 + len));
	if (!dst) return NULL;

	len = wcsrtombs(dst, &src, len, NULL);
	if (len == (size_t)-1) {
		free(dst);
		return NULL;
	}

	dst[len] = '\0';
	return dst;
}

char *awcsntombs(const wchar_t *src, size_t nwc) {
	size_t len = wcsnrtombs(NULL, &src, nwc, 0, NULL);
	if (len == (size_t)-1) return NULL;

	char *dst = malloc(sizeof(*dst) * (1 + len));
	if (!dst) return NULL;

	len = wcsnrtombs(dst, &src, nwc, len, NULL);
	if (len == (size_t)-1) {
		free(dst);
		return NULL;
	}

	dst[len] = '\0';
	return dst;
}

// From <https://en.cppreference.com/w/c/io/fwprintf#Notes>:
//
// While narrow strings provide snprintf, which makes it possible to determine
// the required output buffer size, there is no equivalent for wide strings
// (until C11's snwprintf_s), and in order to determine the buffer size, the
// program may need to call swprintf, check the result value, and reallocate a
// larger buffer, trying again until successful.
//
// snwprintf_s, unlike swprintf_s, will truncate the result to fit within the
// array pointed to by buffer, even though truncation is treated as an error by
// most bounds-checked functions.
int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap) {
	*ret = NULL;

	for (size_t cap = 2 * wcslen(format);; cap *= 2) {
		wchar_t *buf = realloc(*ret, sizeof(*buf) * (1 + cap));
		if (!buf) goto fail;
		*ret = buf;

		va_list _ap;
		va_copy(_ap, ap);
		errno = EOVERFLOW; // vswprintf may not set errno.
		int len = vswprintf(*ret, 1 + cap, format, _ap);
		va_end(_ap);

		if (len >= 0) return len;
		if (errno != EOVERFLOW) goto fail;
	}

fail:
	free(*ret);
	*ret = NULL;
	return -1;
}

int aswprintf(wchar_t **ret, const wchar_t *format, ...) {
	va_list ap;
	va_start(ap, format);
	int n = vaswprintf(ret, format, ap);
	va_end(ap);
	return n;
}

static const char Base64[64] = {
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
};

size_t base64Size(size_t len) {
	return 1 + (len + 2) / 3 * 4;
}

void base64(char *dst, const byte *src, size_t len) {
	size_t i = 0;
	while (len > 2) {
		dst[i++] = Base64[0x3F & (src[0] >> 2)];
		dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
		dst[i++] = Base64[0x3F & (src[1] << 2 | src[2] >> 6)];
		dst[i++] = Base64[0x3F & src[2]];
		src += 3;
		len -= 3;
	}
	if (len) {
		dst[i++] = Base64[0x3F & (src[0] >> 2)];
		if (len > 1) {
			dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
			dst[i++] = Base64[0x3F & (src[1] << 2)];
		} else {
			dst[i++] = Base64[0x3F & (src[0] << 4)];
			dst[i++] = '=';
		}
		dst[i++] = '=';
	}
	dst[i] = '\0';
}

#ifdef TEST
#include <assert.h>
#include <string.h>

int main() {
	assert(5 == base64Size(1));
	assert(5 == base64Size(2));
	assert(5 == base64Size(3));
	assert(9 == base64Size(4));

	char b64[base64Size(3)];
	assert((base64(b64, (byte *)"cat", 3), !strcmp("Y2F0", b64)));
	assert((base64(b64, (byte *)"ca", 2), !strcmp("Y2E=", b64)));
	assert((base64(b64, (byte *)"c", 1), !strcmp("Yw==", b64)));

	assert((base64(b64, (byte *)"\xFF\x00\xFF", 3), !strcmp("/wD/", b64)));
	assert((base64(b64, (byte *)"\x00\xFF\x00", 3), !strcmp("AP8A", b64)));
}

#endif