Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
272
273
274
275
276
277
278
/*--------------------------------------------------------------------------
 *
 * test_saslprep.c
 *      Test harness for the SASLprep implementation.
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *      src/test/modules/test_saslprep/test_saslprep.c
 *
 * -------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include "access/htup_details.h"
#include "common/saslprep.h"
#include "fmgr.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/builtins.h"
 
PG_MODULE_MAGIC;
 
static const char *
saslprep_status_to_text(pg_saslprep_rc rc)
{
    const char *status = "???";
 
    switch (rc)
    {
        case SASLPREP_OOM:
            status = "OOM";
            break;
        case SASLPREP_SUCCESS:
            status = "SUCCESS";
            break;
        case SASLPREP_INVALID_UTF8:
            status = "INVALID_UTF8";
            break;
        case SASLPREP_PROHIBITED:
            status = "PROHIBITED";
            break;
    }
 
    return status;
}
 
/*
 * Simple function to test SASLprep with arbitrary bytes as input.
 *
 * This takes a bytea in input, returning in output the generating data as
 * bytea with the status returned by pg_saslprep().
 */
PG_FUNCTION_INFO_V1(test_saslprep);
Datum
test_saslprep(PG_FUNCTION_ARGS)
{
    bytea      *string = PG_GETARG_BYTEA_PP(0);
    char       *src;
    Size        src_len;
    char       *input_data;
    char       *result;
    Size        result_len;
    bytea      *result_bytea = NULL;
    const char *status = NULL;
    Datum      *values;
    bool       *nulls;
    TupleDesc   tupdesc;
    pg_saslprep_rc rc;
 
    /* determine result type */
    if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
        elog(ERROR, "return type must be a row type");
 
    values = palloc0_array(Datum, tupdesc->natts);
    nulls = palloc0_array(bool, tupdesc->natts);
 
    src_len = VARSIZE_ANY_EXHDR(string);
    src = VARDATA_ANY(string);
 
    /*
     * Copy the input given, to make SASLprep() act on a sanitized string.
     */
    input_data = palloc0(src_len + 1);
    memcpy(input_data, src, src_len);
    input_data[src_len] = '\0';
 
    rc = pg_saslprep(input_data, &result);
    status = saslprep_status_to_text(rc);
 
    if (result)
    {
        result_len = strlen(result);
        result_bytea = palloc(result_len + VARHDRSZ);
        SET_VARSIZE(result_bytea, result_len + VARHDRSZ);
        memcpy(VARDATA(result_bytea), result, result_len);
        values[0] = PointerGetDatum(result_bytea);
    }
    else
        nulls[0] = true;
 
    values[1] = CStringGetTextDatum(status);
 
    PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
}
 
/* Context structure for set-returning function with ranges */
typedef struct
{
    int         current_range;
    char32_t    current_codepoint;
} pg_saslprep_test_context;
 
/*
 * UTF-8 code point ranges.
 */
typedef struct
{
    char32_t    start_codepoint;
    char32_t    end_codepoint;
} pg_utf8_codepoint_range;
 
static const pg_utf8_codepoint_range pg_utf8_test_ranges[] = {
    /* 1, 2, 3 bytes */
    {0x0000, 0xD7FF},           /* Basic Multilingual Plane, before surrogates */
    {0xE000, 0xFFFF},           /* Basic Multilingual Plane, after surrogates */
    /* 4 bytes */
    {0x10000, 0x1FFFF},         /* Supplementary Multilingual Plane */
    {0x20000, 0x2FFFF},         /* Supplementary Ideographic Plane */
    {0x30000, 0x3FFFF},         /* Tertiary Ideographic Plane */
    {0x40000, 0xDFFFF},         /* Unassigned planes */
    {0xE0000, 0xEFFFF},         /* Supplementary Special-purpose Plane */
    {0xF0000, 0xFFFFF},         /* Private Use Area A */
    {0x100000, 0x10FFFF},       /* Private Use Area B */
};
 
#define PG_UTF8_TEST_RANGES_LEN \
    (sizeof(pg_utf8_test_ranges) / sizeof(pg_utf8_test_ranges[0]))
 
 
/*
 * test_saslprep_ranges
 *
 * Test SASLprep across various UTF-8 ranges.
 */
PG_FUNCTION_INFO_V1(test_saslprep_ranges);
Datum
test_saslprep_ranges(PG_FUNCTION_ARGS)
{
    FuncCallContext *funcctx;
    pg_saslprep_test_context *ctx;
    HeapTuple   tuple;
    Datum       result;
 
    /* First call setup */
    if (SRF_IS_FIRSTCALL())
    {
        MemoryContext oldcontext;
        TupleDesc   tupdesc;
 
        funcctx = SRF_FIRSTCALL_INIT();
        oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
 
        if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
            elog(ERROR, "return type must be a row type");
        funcctx->tuple_desc = tupdesc;
 
        /* Allocate context with range setup */
        ctx = (pg_saslprep_test_context *) palloc(sizeof(pg_saslprep_test_context));
        ctx->current_range = 0;
        ctx->current_codepoint = pg_utf8_test_ranges[0].start_codepoint;
        funcctx->user_fctx = ctx;
 
        MemoryContextSwitchTo(oldcontext);
    }
 
    funcctx = SRF_PERCALL_SETUP();
    ctx = (pg_saslprep_test_context *) funcctx->user_fctx;
 
    while (ctx->current_range < PG_UTF8_TEST_RANGES_LEN)
    {
        char32_t    codepoint = ctx->current_codepoint;
        unsigned char utf8_buf[5];
        char        input_str[6];
        char       *output = NULL;
        pg_saslprep_rc rc;
        int         utf8_len;
        const char *status;
        bytea      *input_bytea;
        bytea      *output_bytea;
        char        codepoint_str[16];
        Datum       values[4] = {0};
        bool        nulls[4] = {0};
        const pg_utf8_codepoint_range *range =
            &pg_utf8_test_ranges[ctx->current_range];
 
        CHECK_FOR_INTERRUPTS();
 
        /* Switch to next range if finished with the previous one */
        if (ctx->current_codepoint > range->end_codepoint)
        {
            ctx->current_range++;
            if (ctx->current_range < PG_UTF8_TEST_RANGES_LEN)
                ctx->current_codepoint =
                    pg_utf8_test_ranges[ctx->current_range].start_codepoint;
            continue;
        }
 
        codepoint = ctx->current_codepoint;
 
        /* Convert code point to UTF-8 */
        utf8_len = unicode_utf8len(codepoint);
        if (utf8_len == 0)
        {
            ctx->current_codepoint++;
            continue;
        }
        unicode_to_utf8(codepoint, utf8_buf);
 
        /* Create null-terminated string */
        memcpy(input_str, utf8_buf, utf8_len);
        input_str[utf8_len] = '\0';
 
        /* Test with pg_saslprep */
        rc = pg_saslprep(input_str, &output);
 
        /* Prepare output values */
        memset(nulls, false, sizeof(nulls));
 
        /* codepoint as text U+XXXX format */
        if (codepoint <= 0xFFFF)
            snprintf(codepoint_str, sizeof(codepoint_str), "U+%04X", codepoint);
        else
            snprintf(codepoint_str, sizeof(codepoint_str), "U+%06X", codepoint);
        values[0] = CStringGetTextDatum(codepoint_str);
 
        /* status */
        status = saslprep_status_to_text(rc);
        values[1] = CStringGetTextDatum(status);
 
        /* input_bytes */
        input_bytea = (bytea *) palloc(VARHDRSZ + utf8_len);
        SET_VARSIZE(input_bytea, VARHDRSZ + utf8_len);
        memcpy(VARDATA(input_bytea), utf8_buf, utf8_len);
        values[2] = PointerGetDatum(input_bytea);
 
        /* output_bytes */
        if (output != NULL)
        {
            int         output_len = strlen(output);
 
            output_bytea = (bytea *) palloc(VARHDRSZ + output_len);
            SET_VARSIZE(output_bytea, VARHDRSZ + output_len);
            memcpy(VARDATA(output_bytea), output, output_len);
            values[3] = PointerGetDatum(output_bytea);
            pfree(output);
        }
        else
        {
            nulls[3] = true;
            values[3] = (Datum) 0;
        }
 
        /* Build and return tuple */
        tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
        result = HeapTupleGetDatum(tuple);
 
        /* Move to next code point */
        ctx->current_codepoint++;
 
        SRF_RETURN_NEXT(funcctx, result);
    }
 
    /* All done */
    SRF_RETURN_DONE(funcctx);
}