-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplwasm_utils_str.c
104 lines (85 loc) · 1.96 KB
/
plwasm_utils_str.c
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
#include "plwasm_utils_str.h"
#include <string.h>
#include "postgres.h"
#include "mb/pg_wchar.h"
#include "plwasm_log.h"
bool plwasm_utils_str_eq_safe(
const char *str1,
int str1_len,
const char *str2,
int str2_len
) {
if (str1_len != str2_len) {
return false;
}
if (memcmp(str1, str2, str2_len)) {
return false;
}
return true;
}
bool plwasm_utils_str_startsWithN_safe(
const char *str,
int str_len,
const char *search,
int search_len
) {
if (str_len < search_len) {
return false;
}
if (memcmp(str, search, search_len)) {
return false;
}
return true;
}
bool plwasm_utils_str_endsWithN_safe(
const char *str,
int str_len,
const char *search,
int search_len
) {
if (str_len < search_len) {
return false;
}
if (memcmp(str + str_len - search_len, search, search_len)) {
return false;
}
return true;
}
char* plwasm_utils_str_enc(
plwasm_call_context_t *cctx,
const char *src,
int src_len,
int src_enc,
int dest_enc,
bool force_null_termination,
size_t *converted_sz
) {
const char *FUNC_NAME = "plwasm_utils_str_enc";
char *dest;
CALL_DEBUG5(cctx,
"%s begin. from=%d, to=%d, src_len=%d, force_null_termiation=%d",
FUNC_NAME, src_enc, dest_enc, src_len, force_null_termination);
// TODO UTF16-UTF16
if (dest_enc == src_enc) {
CALL_DEBUG5(cctx, "%s skip.", FUNC_NAME);
dest = pnstrdup(src, src_len); // add null termination
*converted_sz = strlen(dest) + 1;
return dest;
}
if (src_enc < 0) {
CALL_ERROR(cctx, "Invalid src encoding.");
}
if (dest_enc < 0) {
CALL_ERROR(cctx, "Invalid dest encoding.");
}
dest = (char*)pg_do_encoding_conversion(
(unsigned char*)src,
src_len,
src_enc,
dest_enc);
*converted_sz = strlen(dest) + 1;
CALL_DEBUG5(cctx,
"%s success. from=%d, to=%d, src_len=%d, force_null_termiation=%d",
FUNC_NAME, src_enc, dest_enc, src_len, force_null_termination);
return dest;
}