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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
const z = @import( "std" );
const zap = @import( "zap" );
const net = @import( "net-util.zig" );
const json = @import( "json" );
const getty = @import( "getty" );
const ArenaAllocator = z.heap.ArenaAllocator;
const GettyResult = getty.de.Result;
pub var gpa = z.heap.GeneralPurposeAllocator( .{ .thread_safe = true, .stack_trace_frames = 64 } ){};
pub const alloc = gpa.allocator();
const Status = zap.StatusCode;
const Request = zap.Request;
const pbkdf2 = z.crypto.pwhash.pbkdf2;
const aes256Encrypt = z.crypto.aead.aes_gcm.Aes256Gcm.encrypt;
const aes256Decrypt = z.crypto.aead.aes_gcm.Aes256Gcm.decrypt;
pub const @"json.ignore.unknown" = struct {
pub const attributes = .{
.Container = .{ .ignore_unknown_fields = true },
};
};
pub fn MergeTypes( comptime t1: type, comptime t2: type ) type {
const t1info = @typeInfo( t1 ).Struct;
const t2info = @typeInfo( t2 ).Struct;
comptime if( t1info.is_tuple ) return t2;
comptime if( t2info.is_tuple ) return t1;
var t3info = t1info;
t3info.fields = t1info.fields ++ t2info.fields;
var t1s = @typeInfo( t1 );
t1s.Struct = t3info;
return @Type( t1s );
}
pub fn JsonResponse( comptime t: type ) type {
return struct {
v: t,
@"#alloc": ArenaAllocator,
pub fn deinit( self: @This() ) void {
self.@"#alloc".deinit();
}
};
}
///freed by caller
pub fn jsonParse( comptime t: type, str: []const u8 ) !JsonResponse(t) {
return jsonParseAlloc( t, str, alloc );
}
///freed by caller
pub fn jsonParseAlloc( comptime t: type, str: []const u8, a: z.mem.Allocator ) !JsonResponse(t) {
var arena = ArenaAllocator.init( a );
const allocator = arena.allocator();
const ret = json.fromSlice( allocator, t, str ) catch |e| {
arena.deinit();
return e;
};
var obj: JsonResponse(t) = undefined;
obj.v = ret.value;
obj.@"#alloc" = arena;
return obj;
}
///has to be freed using page_allocator
pub fn jsonStringify( obj: anytype ) ![]const u8 {
var arr = z.ArrayList( u8 ).init( alloc );
try json.toWriter( alloc, obj, arr.writer() );
return arr.toOwnedSlice();
}
pub fn jsonStringifyAlloc( obj: anytype, a: z.mem.Allocator ) ![]const u8 {
var arr = z.ArrayList( u8 ).init( a );
try json.toWriter( alloc, obj, arr.writer() );
return arr.toOwnedSlice();
}
///has to be freed by caller with page_allocator
///10mb max
pub fn readFile( path: []const u8 ) ![]const u8 {
return readFileAlloc( path, alloc );
}
pub fn readFileAlloc( path: []const u8, a: z.mem.Allocator ) ![]const u8 {
const file = try z.fs.cwd().openFile( path, .{} );
defer file.close();
var reader = z.io.bufferedReader( file.reader() );
var stream = reader.reader();
const contents = try stream.readAllAlloc( a, 1024 * 10000 );
return contents[0..contents.len];
}
pub fn fileExists( path: []const u8 ) bool {
const file = z.fs.cwd().openFile( path, .{} ) catch return false;
_ = file.stat() catch return false;
file.close();
return true;
}
pub fn writeFile( path: []const u8, contents: []const u8 ) !void {
z.fs.cwd().deleteFile( path ) catch |e| {
switch (e) {
error.FileNotFound => {},
else => return e
}
};
const file = try z.fs.cwd().createFile( path, .{} );
defer file.close();
var writer = file.writer();
writer.writeAll( contents ) catch return error.OutOfMemory;
}
pub fn deleteFile( path: []const u8 ) !void {
z.fs.cwd().deleteFile( path ) catch |e| {
switch (e) {
error.FileNotFound => {},
else => return e
}
};
}
pub fn hexToBytesAlloc( hex: []const u8, a: z.mem.Allocator ) ![]const u8 {
var bytes = try a.alloc( u8, hex.len / 2 );
var i: u32 = 0;
while( i < hex.len ) : (i += 2) {
z.debug.print( "byte: {s}\n", .{ hex[i..i+1] } );
const byte = try z.fmt.parseInt( u8, hex[i..i+1], 16 );
bytes[i/2] = byte;
}
return bytes;
}
pub fn hexToBytes( hex: []const u8 ) ![]const u8 {
return hexToBytesAlloc( hex, alloc );
}
///EXTREMELY GAY but zig crypto lib has a bug that makes it not compile.
///IMPORTANT: FIX THIS!!!!!
pub fn readFileCrypto( path: []const u8 ) ![]const u8 {
const proc = try z.process.Child.run( .{
.allocator = alloc,
.argv = &.{ "node", "../chat-reader.cjs", "read", path }
} );
defer alloc.free( proc.stdout );
defer alloc.free( proc.stderr );
if( proc.stdout[0] == '0' )
return error.FileNotFound;
const buf = alloc.dupe( u8, proc.stdout );
return buf;
}
pub fn writeFileCrypto( path: []const u8, buf: []const u8 ) ![]const u8 {
const proc = try z.process.Child.run( .{
.allocator = alloc,
.argv = &.{ "node", "../chat-reader.cjs", "write", path, buf }
} );
defer alloc.free( proc.stdout );
defer alloc.free( proc.stderr );
const ret = alloc.dupe( u8, proc.stdout );
return ret;
}
pub const Validator = struct {
pub fn isValidChar( c: u8 ) bool {
if( c >= 'a' and c <= 'z' )
return true;
if( c >= 'A' and c <= 'Z' )
return true;
if( c >= '0' and c <= '9' )
return true;
if( c == '-' or c == '_' )
return true;
if( c == '.' )
return true;
if( c == '@' )
return true;
return false;
}
pub fn isValidCharSystem( c: u8 ) bool {
const allowlist = ",;:/?=!#$^&*()+~[]{}<>|` ";
for( allowlist ) |allowc| {
if( allowc == c )
return true;
}
return isValidChar( c );
}
pub fn isValidCharFont( c: u8 ) bool {
if( c == ' ' )
return true;
if( !isValidChar( c ) )
return false;
return true;
}
pub fn isValidCharEmail( c: u8 ) bool {
if( !isValidChar( c ) )
return false;
return true;
}
pub fn isValidStrSystem( str: []const u8 ) bool {
if( str.len > 2048 )
return false;
for( str ) |c| {
if( !isValidCharSystem( c ) )
return false;
}
return true;
}
pub fn isValidStrNickname( str: []const u8 ) bool {
if( str.len > 24 )
return false;
if( str.len < 2 )
return false;
for( str ) |c| {
if( !isValidCharEmail( c ) )
return false;
}
return true;
}
pub fn isValidStrEmail( str: []const u8 ) bool {
if( str.len > 127 )
return false;
if( str.len < 5 )
return false;
var has_at = false;
var has_dot = false;
for( str ) |c| {
if( !isValidCharEmail( c ) )
return false;
if( c == '@' ) has_at = true;
if( c == '.' ) has_dot = true;
}
return true;
}
pub fn isValidStrFont( str: []const u8 ) bool {
if( str.len > 64 )
return false;
if( str.len < 3 )
return false;
for( str ) |c| {
if( !isValidCharFont( c ) )
return false;
}
return true;
}
};
|