~kvik/lu9-p9

ref: c85dc2c7a75a78c497c55ce5ad276f8f9fa04e17 lu9-p9/base/misc.c -rw-r--r-- 2.5 KiB
c85dc2c7kvik misc: use usize instead of lua_Unsigned 11 months 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
/* Nowhere in particular */

static int
p9_cleanname(lua_State *L)
{
	const char *path, *p;
	usize len;
	luaL_Buffer b;
	
	path = luaL_checklstring(L, 1, &len);
	luaL_buffinit(L, &b);
	luaL_addlstring(&b, path, len);
	luaL_addchar(&b, '\0');
	p = cleanname(luaL_buffaddr(&b));
	lua_pushstring(L, p);
	return 1;
}

/* Taken from treason(1), thanks sigrid */
static uvlong
nanosec(void)
{
	static uvlong fasthz, xstart;
	uvlong x, div;

	if(fasthz == ~0ULL)
		return nsec() - xstart;

	if(fasthz == 0){
		fasthz = _tos->cyclefreq;
		if(fasthz == 0){
			fasthz = ~0ULL;
			xstart = nsec();
		}else{
			cycles(&xstart);
		}
		return 0;
	}
	cycles(&x);
	x -= xstart;

	/* this is ugly */
	for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);

	return x / (fasthz / div);
}

static int
p9_nanosec(lua_State *L)
{
	lua_pushinteger(L, nanosec());
	return 1;
}

static int
p9_nsec(lua_State *L)
{
	lua_pushinteger(L, nsec());
	return 1;
}

static int
decode(lua_State *L, int base)
{
	const char *in;
	char *buf;
	usize insz, bufsz, sz;
	luaL_Buffer b;
	int (*dec)(uchar*, int, char*, int);
	
	in = luaL_checklstring(L, 1, &insz);
	buf = luaL_buffinitsize(L, &b, insz+1);
	switch(base){
	case 64:
		dec = dec64;
		bufsz = 6 * (insz + 3) / 8;
		break;
	case 32:
		dec = dec32;
		bufsz = 5 * (insz + 7) / 8;
		break;
	case 16:
		dec = dec16;
		bufsz = 4 * (insz + 1) / 8;
		break;
	default:
		return error(L, "unsupported base");
	}
	if((sz = dec((uchar*)buf, bufsz, in, insz)) == -1)
		return error(L, "dec: failed");
	luaL_pushresultsize(&b, sz);
	return 1;
}
static int p9_dec64(lua_State *L) { return decode(L, 64); }
static int p9_dec32(lua_State *L) { return decode(L, 32); }
static int p9_dec16(lua_State *L) { return decode(L, 16); }

static int
encode(lua_State *L, int base)
{
	const char *in;
	char *buf;
	usize insz, bufsz, sz;
	luaL_Buffer b;
	int (*enc)(char*, int, uchar*, int);
	
	in = luaL_checklstring(L, 1, &insz);
	switch(base){
	case 64:
		enc = enc64;
 		bufsz = (insz + 3) * 8 / 6;
		break;
	case 32:
		enc = enc32;
 		bufsz = (insz + 7) * 8 / 5;
		break;
	case 16:
		enc = enc16;
 		bufsz = (insz + 1) * 8 / 4;
		break;
	default:
		return error(L, "unsupported base");
	}
	buf = luaL_buffinitsize(L, &b, bufsz);
	if((sz = enc(buf, bufsz, (uchar*)in, insz)) == -1)
		return error(L, "enc: failed");
	luaL_pushresultsize(&b, sz);
	return 1;
}
static int p9_enc64(lua_State *L) { return encode(L, 64); }
static int p9_enc32(lua_State *L) { return encode(L, 32); }
static int p9_enc16(lua_State *L) { return encode(L, 16); }