-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathwasm_wrapper_light.c
More file actions
213 lines (178 loc) Β· 6.42 KB
/
wasm_wrapper_light.c
File metadata and controls
213 lines (178 loc) Β· 6.42 KB
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
#include "pg_query.h"
#include <emscripten.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
static int validate_input(const char* input) {
return input != NULL && strlen(input) > 0;
}
static char* safe_strdup(const char* str) {
if (!str) return NULL;
char* result = strdup(str);
if (!result) {
return NULL;
}
return result;
}
static void* safe_malloc(size_t size) {
void* ptr = malloc(size);
if (!ptr && size > 0) {
return NULL;
}
return ptr;
}
EMSCRIPTEN_KEEPALIVE
char* wasm_parse_query(const char* input) {
if (!validate_input(input)) {
return safe_strdup("Invalid input: query cannot be null or empty");
}
PgQueryParseResult result = pg_query_parse(input);
if (result.error) {
char* error_msg = safe_strdup(result.error->message);
pg_query_free_parse_result(result);
return error_msg ? error_msg : safe_strdup("Memory allocation failed");
}
char* parse_tree = safe_strdup(result.parse_tree);
pg_query_free_parse_result(result);
return parse_tree;
}
EMSCRIPTEN_KEEPALIVE
char* wasm_parse_plpgsql(const char* input) {
if (!validate_input(input)) {
return safe_strdup("Invalid input: query cannot be null or empty");
}
PgQueryPlpgsqlParseResult result = pg_query_parse_plpgsql(input);
if (result.error) {
char* error_msg = safe_strdup(result.error->message);
pg_query_free_plpgsql_parse_result(result);
return error_msg ? error_msg : safe_strdup("Memory allocation failed");
}
if (!result.plpgsql_funcs) {
pg_query_free_plpgsql_parse_result(result);
return safe_strdup("{\"plpgsql_funcs\":[]}");
}
size_t funcs_len = strlen(result.plpgsql_funcs);
size_t json_len = strlen("{\"plpgsql_funcs\":}") + funcs_len + 1;
char* wrapped_result = safe_malloc(json_len);
if (!wrapped_result) {
pg_query_free_plpgsql_parse_result(result);
return safe_strdup("Memory allocation failed");
}
int written = snprintf(wrapped_result, json_len, "{\"plpgsql_funcs\":%s}", result.plpgsql_funcs);
if (written >= json_len) {
free(wrapped_result);
pg_query_free_plpgsql_parse_result(result);
return safe_strdup("Buffer overflow prevented");
}
pg_query_free_plpgsql_parse_result(result);
return wrapped_result;
}
EMSCRIPTEN_KEEPALIVE
char* wasm_fingerprint(const char* input) {
if (!validate_input(input)) {
return safe_strdup("Invalid input: query cannot be null or empty");
}
PgQueryFingerprintResult result = pg_query_fingerprint(input);
if (result.error) {
char* error_msg = safe_strdup(result.error->message);
pg_query_free_fingerprint_result(result);
return error_msg ? error_msg : safe_strdup("Memory allocation failed");
}
char* fingerprint_str = safe_strdup(result.fingerprint_str);
pg_query_free_fingerprint_result(result);
return fingerprint_str;
}
EMSCRIPTEN_KEEPALIVE
char* wasm_normalize_query(const char* input) {
if (!validate_input(input)) {
return safe_strdup("Invalid input: query cannot be null or empty");
}
PgQueryNormalizeResult result = pg_query_normalize(input);
if (result.error) {
char* error_msg = safe_strdup(result.error->message);
pg_query_free_normalize_result(result);
return error_msg ? error_msg : safe_strdup("Memory allocation failed");
}
char* normalized = safe_strdup(result.normalized_query);
pg_query_free_normalize_result(result);
if (!normalized) {
return safe_strdup("Memory allocation failed");
}
return normalized;
}
typedef struct {
int has_error;
char* message;
char* funcname;
char* filename;
int lineno;
int cursorpos;
char* context;
char* data;
size_t data_len;
} WasmDetailedResult;
EMSCRIPTEN_KEEPALIVE
WasmDetailedResult* wasm_parse_query_detailed(const char* input) {
WasmDetailedResult* result = safe_malloc(sizeof(WasmDetailedResult));
if (!result) {
return NULL;
}
memset(result, 0, sizeof(WasmDetailedResult));
if (!validate_input(input)) {
result->has_error = 1;
result->message = safe_strdup("Invalid input: query cannot be null or empty");
return result;
}
PgQueryParseResult parse_result = pg_query_parse(input);
if (parse_result.error) {
result->has_error = 1;
size_t message_len = strlen("Parse error: at line , position ") + strlen(parse_result.error->message) + 20;
char* prefixed_message = safe_malloc(message_len);
if (!prefixed_message) {
result->has_error = 1;
result->message = safe_strdup("Memory allocation failed");
pg_query_free_parse_result(parse_result);
return result;
}
snprintf(prefixed_message, message_len,
"Parse error: %s at line %d, position %d",
parse_result.error->message,
parse_result.error->lineno,
parse_result.error->cursorpos);
result->message = prefixed_message;
char* funcname_copy = parse_result.error->funcname ? safe_strdup(parse_result.error->funcname) : NULL;
char* filename_copy = parse_result.error->filename ? safe_strdup(parse_result.error->filename) : NULL;
char* context_copy = parse_result.error->context ? safe_strdup(parse_result.error->context) : NULL;
result->funcname = funcname_copy;
result->filename = filename_copy;
result->lineno = parse_result.error->lineno;
result->cursorpos = parse_result.error->cursorpos;
result->context = context_copy;
} else {
result->data = safe_strdup(parse_result.parse_tree);
if (result->data) {
result->data_len = strlen(result->data);
} else {
result->has_error = 1;
result->message = safe_strdup("Memory allocation failed");
}
}
pg_query_free_parse_result(parse_result);
return result;
}
EMSCRIPTEN_KEEPALIVE
void wasm_free_detailed_result(WasmDetailedResult* result) {
if (result) {
free(result->message);
free(result->funcname);
free(result->filename);
free(result->context);
free(result->data);
free(result);
}
}
EMSCRIPTEN_KEEPALIVE
void wasm_free_string(char* str) {
free(str);
}