v2 / thirdparty / pg / pgport_stubs.c
671 lines · 613 sloc · 18.49 KB · 3ac2c4636dd1054c3faf99c5ac5638be42597156
Raw
1/*
2 * Minimal libpgport/libpgcommon stubs for V's `db.pg` cross compilation to
3 * Linux. The bundled `linuxroot` sysroot ships a static `libpq.a` but no
4 * `libpgcommon.a` / `libpgport.a`, so a number of symbols referenced by
5 * libpq stay unresolved at link time. The implementations here cover only
6 * the entry points actually referenced by the libpq.a frontend (verified
7 * via `ld.lld --error-limit=0`), forwarding to libc / OpenSSL equivalents.
8 * They are NOT a full replacement for libpgport / libpgcommon: SASLprep
9 * is a no-op, encoding tables only know the name list, etc. Enough to
10 * connect to a Postgres server using SCRAM-SHA-256 and exchange queries
11 * in SQL_ASCII / UTF8.
12 *
13 * Linked only via `$if cross_compile ? && linux` in vlib/db/pg/pg.c.v.
14 * The linuxroot sysroot lacks some POSIX headers (e.g. pwd.h), so the few
15 * libc entry points used here are declared inline instead of pulled from
16 * system headers.
17 */
18
19#define _GNU_SOURCE
20#include <stdarg.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <strings.h>
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <fcntl.h>
29
30/* Forward declarations to avoid relying on headers the linuxroot sysroot
31 * doesn't ship (pwd.h, full netdb.h getaddrinfo/getnameinfo prototypes,
32 * sys/un.h ucred). */
33struct passwd;
34struct addrinfo;
35struct sockaddr;
36
37extern int getpwuid_r(unsigned int uid, struct passwd *pwd, char *buf,
38 size_t buflen, struct passwd **result);
39extern int getaddrinfo(const char *node, const char *service,
40 const struct addrinfo *hints, struct addrinfo **res);
41extern void freeaddrinfo(struct addrinfo *res);
42extern int getnameinfo(const struct sockaddr *sa, unsigned int salen, char *host,
43 unsigned int hostlen, char *serv, unsigned int servlen,
44 int flags);
45extern const char *inet_ntop(int af, const void *src, char *dst, unsigned int size);
46
47/* OpenSSL — already linked via -lssl -lcrypto. We declare what we need so we
48 * don't depend on <openssl/*.h> being on the include search path. */
49typedef struct evp_md_st EVP_MD;
50typedef struct hmac_ctx_st HMAC_CTX;
51typedef struct engine_st ENGINE;
52extern const EVP_MD *EVP_sha224(void);
53extern const EVP_MD *EVP_sha256(void);
54extern const EVP_MD *EVP_sha384(void);
55extern const EVP_MD *EVP_sha512(void);
56extern const EVP_MD *EVP_md5(void);
57extern const EVP_MD *EVP_sha1(void);
58extern HMAC_CTX *HMAC_CTX_new(void);
59extern void HMAC_CTX_free(HMAC_CTX *ctx);
60extern int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
61 const EVP_MD *md, ENGINE *impl);
62extern int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len);
63extern int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
64extern int RAND_bytes(unsigned char *buf, int num);
65
66size_t strlcpy(char *dst, const char *src, size_t siz) {
67 const char *s = src;
68 size_t n = siz;
69 if (n != 0) {
70 while (--n != 0) {
71 if ((*dst++ = *s++) == '\0') {
72 break;
73 }
74 }
75 }
76 if (n == 0) {
77 if (siz != 0) {
78 *dst = '\0';
79 }
80 while (*s++) {
81 }
82 }
83 return (size_t)(s - src - 1);
84}
85
86int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
87 return vsnprintf(str, count, fmt, args);
88}
89
90int pg_snprintf(char *str, size_t count, const char *fmt, ...) {
91 va_list ap;
92 va_start(ap, fmt);
93 int n = vsnprintf(str, count, fmt, ap);
94 va_end(ap);
95 return n;
96}
97
98int pg_sprintf(char *str, const char *fmt, ...) {
99 va_list ap;
100 va_start(ap, fmt);
101 int n = vsprintf(str, fmt, ap);
102 va_end(ap);
103 return n;
104}
105
106int pg_vfprintf(FILE *stream, const char *fmt, va_list args) {
107 return vfprintf(stream, fmt, args);
108}
109
110int pg_fprintf(FILE *stream, const char *fmt, ...) {
111 va_list ap;
112 va_start(ap, fmt);
113 int n = vfprintf(stream, fmt, ap);
114 va_end(ap);
115 return n;
116}
117
118int pg_printf(const char *fmt, ...) {
119 va_list ap;
120 va_start(ap, fmt);
121 int n = vprintf(fmt, ap);
122 va_end(ap);
123 return n;
124}
125
126int pg_strcasecmp(const char *s1, const char *s2) {
127 return strcasecmp(s1, s2);
128}
129
130int pg_strncasecmp(const char *s1, const char *s2, size_t n) {
131 return strncasecmp(s1, s2, n);
132}
133
134unsigned char pg_tolower(unsigned char ch) {
135 if (ch >= 'A' && ch <= 'Z') {
136 return ch + ('a' - 'A');
137 }
138 return ch;
139}
140
141unsigned char pg_toupper(unsigned char ch) {
142 if (ch >= 'a' && ch <= 'z') {
143 return ch - ('a' - 'A');
144 }
145 return ch;
146}
147
148int pg_strip_crlf(char *str) {
149 int len = (int)strlen(str);
150 while (len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r')) {
151 str[--len] = '\0';
152 }
153 return len;
154}
155
156char *pg_strerror_r(int errnum, char *buf, size_t buflen) {
157 const char *s = strerror(errnum);
158 if (s == NULL) {
159 snprintf(buf, buflen, "errno %d", errnum);
160 } else {
161 strncpy(buf, s, buflen);
162 if (buflen > 0) {
163 buf[buflen - 1] = '\0';
164 }
165 }
166 return buf;
167}
168
169int pqGetpwuid(unsigned int uid, struct passwd *resbuf, char *buf, size_t buflen,
170 struct passwd **result) {
171 return getpwuid_r(uid, resbuf, buf, buflen, result);
172}
173
174int pg_getaddrinfo_all(const char *hostname, const char *servname,
175 const struct addrinfo *hint, struct addrinfo **result) {
176 return getaddrinfo(hostname, servname, hint, result);
177}
178
179void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai) {
180 (void)hint_ai_family;
181 if (ai != NULL) {
182 freeaddrinfo(ai);
183 }
184}
185
186int pg_getnameinfo_all(const void *addr, int salen, char *node, size_t nodelen,
187 char *service, size_t servicelen, int flags) {
188 return getnameinfo((const struct sockaddr *)addr, (unsigned int)salen, node,
189 (unsigned int)nodelen, service, (unsigned int)servicelen,
190 flags);
191}
192
193int pg_set_noblock(int sock) {
194 int flags = fcntl(sock, F_GETFL);
195 if (flags < 0) {
196 return 0;
197 }
198 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
199 return 0;
200 }
201 return 1;
202}
203
204/* Linux-only: getpeereid via SO_PEERCRED. */
205struct pg_ucred_compat { unsigned int pid; unsigned int uid; unsigned int gid; };
206#ifndef SO_PEERCRED
207#define SO_PEERCRED 17
208#endif
209
210int getpeereid(int sock, unsigned int *euid, unsigned int *egid) {
211 struct pg_ucred_compat cred;
212 unsigned int len = sizeof(cred);
213 if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0) {
214 return -1;
215 }
216 if (euid != NULL) {
217 *euid = cred.uid;
218 }
219 if (egid != NULL) {
220 *egid = cred.gid;
221 }
222 return 0;
223}
224
225const char *pg_inet_net_ntop(int af, const void *src, int bits, char *dst,
226 size_t size) {
227 (void)bits;
228 return inet_ntop(af, src, dst, (unsigned int)size);
229}
230
231/* SASLprep: a real implementation normalizes Unicode per RFC 4013. The
232 * frontend uses it on passwords before SCRAM hashing; if we just pass the
233 * raw bytes through, ASCII-only passwords still hash to the same value as
234 * any compliant client would produce. Non-ASCII passwords may fail to
235 * authenticate. PG_SASLPREP_SUCCESS == 0 in PostgreSQL. */
236int pg_saslprep(const char *input, char **output) {
237 size_t n = strlen(input);
238 char *copy = (char *)malloc(n + 1);
239 if (copy == NULL) {
240 return -1; /* PG_SASLPREP_OOM */
241 }
242 memcpy(copy, input, n + 1);
243 *output = copy;
244 return 0;
245}
246
247int pg_strong_random(void *buf, size_t len) {
248 if (RAND_bytes((unsigned char *)buf, (int)len) == 1) {
249 return 1;
250 }
251 return 0;
252}
253
254/* Base64 codec: standard alphabet, identical layout to PostgreSQL's
255 * src/common/base64.c so libpq's helpers (which expect specific overflow
256 * semantics) behave the same. */
257static const char b64_alphabet[] =
258 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
259
260static const signed char b64_lookup[256] = {
261 [0 ... 255] = -1,
262 ['A'] = 0, ['B'] = 1, ['C'] = 2, ['D'] = 3, ['E'] = 4, ['F'] = 5,
263 ['G'] = 6, ['H'] = 7, ['I'] = 8, ['J'] = 9, ['K'] = 10, ['L'] = 11,
264 ['M'] = 12, ['N'] = 13, ['O'] = 14, ['P'] = 15, ['Q'] = 16, ['R'] = 17,
265 ['S'] = 18, ['T'] = 19, ['U'] = 20, ['V'] = 21, ['W'] = 22, ['X'] = 23,
266 ['Y'] = 24, ['Z'] = 25, ['a'] = 26, ['b'] = 27, ['c'] = 28, ['d'] = 29,
267 ['e'] = 30, ['f'] = 31, ['g'] = 32, ['h'] = 33, ['i'] = 34, ['j'] = 35,
268 ['k'] = 36, ['l'] = 37, ['m'] = 38, ['n'] = 39, ['o'] = 40, ['p'] = 41,
269 ['q'] = 42, ['r'] = 43, ['s'] = 44, ['t'] = 45, ['u'] = 46, ['v'] = 47,
270 ['w'] = 48, ['x'] = 49, ['y'] = 50, ['z'] = 51, ['0'] = 52, ['1'] = 53,
271 ['2'] = 54, ['3'] = 55, ['4'] = 56, ['5'] = 57, ['6'] = 58, ['7'] = 59,
272 ['8'] = 60, ['9'] = 61, ['+'] = 62, ['/'] = 63,
273};
274
275int pg_b64_enc_len(int srclen) {
276 return ((srclen + 2) / 3) * 4;
277}
278
279int pg_b64_dec_len(int srclen) {
280 return (srclen / 4) * 3;
281}
282
283int pg_b64_encode(const char *src, int len, char *dst, int dstlen) {
284 int i = 0, j = 0;
285 while (i < len) {
286 unsigned int b0 = (unsigned char)src[i++];
287 unsigned int b1 = (i < len) ? (unsigned char)src[i++] : 0;
288 unsigned int b2 = (i < len) ? (unsigned char)src[i++] : 0;
289 int rem = (i > len) ? (i - len) : 0;
290 if (j + 4 > dstlen) {
291 return -1;
292 }
293 dst[j++] = b64_alphabet[b0 >> 2];
294 dst[j++] = b64_alphabet[((b0 & 0x03) << 4) | (b1 >> 4)];
295 dst[j++] = (rem >= 2) ? '=' : b64_alphabet[((b1 & 0x0f) << 2) | (b2 >> 6)];
296 dst[j++] = (rem >= 1) ? '=' : b64_alphabet[b2 & 0x3f];
297 }
298 return j;
299}
300
301int pg_b64_decode(const char *src, int len, char *dst, int dstlen) {
302 int i = 0, j = 0;
303 while (i < len) {
304 while (i < len && (src[i] == '\n' || src[i] == '\r' || src[i] == ' ' ||
305 src[i] == '\t')) {
306 i++;
307 }
308 if (i >= len) {
309 break;
310 }
311 signed char c0 = b64_lookup[(unsigned char)src[i++]];
312 if (c0 < 0) {
313 return -1;
314 }
315 while (i < len && (src[i] == '\n' || src[i] == '\r' || src[i] == ' ' ||
316 src[i] == '\t')) {
317 i++;
318 }
319 if (i >= len) {
320 return -1;
321 }
322 signed char c1 = b64_lookup[(unsigned char)src[i++]];
323 if (c1 < 0) {
324 return -1;
325 }
326 while (i < len && (src[i] == '\n' || src[i] == '\r' || src[i] == ' ' ||
327 src[i] == '\t')) {
328 i++;
329 }
330 signed char c2 = -1, c3 = -1;
331 char raw2 = 0, raw3 = 0;
332 if (i < len) {
333 raw2 = src[i++];
334 if (raw2 != '=') {
335 c2 = b64_lookup[(unsigned char)raw2];
336 if (c2 < 0) {
337 return -1;
338 }
339 }
340 }
341 while (i < len && (src[i] == '\n' || src[i] == '\r' || src[i] == ' ' ||
342 src[i] == '\t')) {
343 i++;
344 }
345 if (i < len) {
346 raw3 = src[i++];
347 if (raw3 != '=') {
348 c3 = b64_lookup[(unsigned char)raw3];
349 if (c3 < 0) {
350 return -1;
351 }
352 }
353 }
354 if (j >= dstlen) {
355 return -1;
356 }
357 dst[j++] = (char)((c0 << 2) | ((c1 & 0x30) >> 4));
358 if (raw2 == '=') {
359 break;
360 }
361 if (j >= dstlen) {
362 return -1;
363 }
364 dst[j++] = (char)(((c1 & 0x0f) << 4) | ((c2 & 0x3c) >> 2));
365 if (raw3 == '=') {
366 break;
367 }
368 if (j >= dstlen) {
369 return -1;
370 }
371 dst[j++] = (char)(((c2 & 0x03) << 6) | c3);
372 }
373 return j;
374}
375
376/* pg_hmac: thin wrapper over OpenSSL HMAC_CTX. PG's pg_cryptohash_type:
377 * 0 PG_MD5, 1 PG_SHA1, 2 PG_SHA224, 3 PG_SHA256, 4 PG_SHA384, 5 PG_SHA512 */
378struct pg_hmac_ctx {
379 HMAC_CTX *ctx;
380 const EVP_MD *md;
381 int type;
382 int error;
383};
384
385struct pg_hmac_ctx *pg_hmac_create(int type) {
386 const EVP_MD *md = NULL;
387 switch (type) {
388 case 0: md = EVP_md5(); break;
389 case 1: md = EVP_sha1(); break;
390 case 2: md = EVP_sha224(); break;
391 case 3: md = EVP_sha256(); break;
392 case 4: md = EVP_sha384(); break;
393 case 5: md = EVP_sha512(); break;
394 default: return NULL;
395 }
396 if (md == NULL) {
397 return NULL;
398 }
399 struct pg_hmac_ctx *c = (struct pg_hmac_ctx *)malloc(sizeof(*c));
400 if (c == NULL) {
401 return NULL;
402 }
403 c->md = md;
404 c->type = type;
405 c->error = 0;
406 c->ctx = HMAC_CTX_new();
407 if (c->ctx == NULL) {
408 free(c);
409 return NULL;
410 }
411 return c;
412}
413
414int pg_hmac_init(struct pg_hmac_ctx *ctx, const unsigned char *key, size_t len) {
415 if (ctx == NULL) {
416 return -1;
417 }
418 if (HMAC_Init_ex(ctx->ctx, key, (int)len, ctx->md, NULL) != 1) {
419 ctx->error = 1;
420 return -1;
421 }
422 return 0;
423}
424
425int pg_hmac_update(struct pg_hmac_ctx *ctx, const unsigned char *data, size_t len) {
426 if (ctx == NULL) {
427 return -1;
428 }
429 if (HMAC_Update(ctx->ctx, data, len) != 1) {
430 ctx->error = 1;
431 return -1;
432 }
433 return 0;
434}
435
436int pg_hmac_final(struct pg_hmac_ctx *ctx, unsigned char *dest, size_t len) {
437 if (ctx == NULL) {
438 return -1;
439 }
440 unsigned int outlen = (unsigned int)len;
441 if (HMAC_Final(ctx->ctx, dest, &outlen) != 1) {
442 ctx->error = 1;
443 return -1;
444 }
445 return 0;
446}
447
448void pg_hmac_free(struct pg_hmac_ctx *ctx) {
449 if (ctx == NULL) {
450 return;
451 }
452 if (ctx->ctx != NULL) {
453 HMAC_CTX_free(ctx->ctx);
454 }
455 free(ctx);
456}
457
458const char *pg_hmac_error(struct pg_hmac_ctx *ctx) {
459 if (ctx == NULL) {
460 return "out of memory";
461 }
462 if (ctx->error) {
463 return "HMAC error";
464 }
465 return "";
466}
467
468/* SCRAM-SHA-256 helpers. Signatures match PG 14 src/common/scram-common.c.
469 * SCRAM_KEY_LEN is 32 (SHA-256 output). */
470extern int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
471 const unsigned char *salt, int saltlen, int iter,
472 const EVP_MD *digest, int keylen,
473 unsigned char *out);
474
475static int scram_hmac_one(const unsigned char *key, const char *msg,
476 unsigned char *result, const char **errstr) {
477 struct pg_hmac_ctx *ctx = pg_hmac_create(3 /* PG_SHA256 */);
478 if (ctx == NULL) {
479 if (errstr != NULL) {
480 *errstr = "out of memory";
481 }
482 return -1;
483 }
484 if (pg_hmac_init(ctx, key, 32) < 0 ||
485 pg_hmac_update(ctx, (const unsigned char *)msg, strlen(msg)) < 0 ||
486 pg_hmac_final(ctx, result, 32) < 0) {
487 if (errstr != NULL) {
488 *errstr = pg_hmac_error(ctx);
489 }
490 pg_hmac_free(ctx);
491 return -1;
492 }
493 pg_hmac_free(ctx);
494 return 0;
495}
496
497int scram_ServerKey(const unsigned char *salted_password, unsigned char *result,
498 const char **errstr) {
499 return scram_hmac_one(salted_password, "Server Key", result, errstr);
500}
501
502int scram_ClientKey(const unsigned char *salted_password, unsigned char *result,
503 const char **errstr) {
504 return scram_hmac_one(salted_password, "Client Key", result, errstr);
505}
506
507/* SHA-256 of input → result (32 bytes). */
508extern unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md);
509int scram_H(const unsigned char *input, int len, unsigned char *result,
510 const char **errstr) {
511 if (SHA256(input, (size_t)len, result) == NULL) {
512 if (errstr != NULL) {
513 *errstr = "SHA256 failed";
514 }
515 return -1;
516 }
517 return 0;
518}
519
520/* PBKDF2-HMAC-SHA256(password, salt, iterations) → 32-byte salted password. */
521int scram_SaltedPassword(const char *password, const char *salt, int saltlen,
522 int iterations, unsigned char *result,
523 const char **errstr) {
524 if (PKCS5_PBKDF2_HMAC(password, (int)strlen(password),
525 (const unsigned char *)salt, saltlen, iterations,
526 EVP_sha256(), 32, result) != 1) {
527 if (errstr != NULL) {
528 *errstr = "PBKDF2 failed";
529 }
530 return -1;
531 }
532 return 0;
533}
534
535/* scram_build_secret is server-side; the archive references it because some
536 * objects in libpq.a were compiled with both frontend and backend sections
537 * sharing scram-common.c. Provide a stub that returns NULL — the frontend
538 * code path that actually calls this would be unreachable. */
539char *scram_build_secret(const char *salt, int saltlen, int iterations,
540 const char *password, const char **errstr) {
541 (void)salt;
542 (void)saltlen;
543 (void)iterations;
544 (void)password;
545 if (errstr != NULL) {
546 *errstr = "scram_build_secret not implemented in frontend stubs";
547 }
548 return NULL;
549}
550
551/* pg_md5_encrypt: legacy "md5"-prefixed MD5(password || username) hash. */
552extern unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md);
553int pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len,
554 char *buf) {
555 static const char hex[] = "0123456789abcdef";
556 size_t passwd_len = strlen(passwd);
557 unsigned char digest[16];
558 unsigned char *tmp = (unsigned char *)malloc(passwd_len + salt_len);
559 if (tmp == NULL) {
560 return 0;
561 }
562 memcpy(tmp, passwd, passwd_len);
563 memcpy(tmp + passwd_len, salt, salt_len);
564 if (MD5(tmp, passwd_len + salt_len, digest) == NULL) {
565 free(tmp);
566 return 0;
567 }
568 free(tmp);
569 buf[0] = 'm';
570 buf[1] = 'd';
571 buf[2] = '5';
572 for (int i = 0; i < 16; i++) {
573 buf[3 + 2 * i] = hex[digest[i] >> 4];
574 buf[3 + 2 * i + 1] = hex[digest[i] & 0x0f];
575 }
576 buf[35] = '\0';
577 return 1;
578}
579
580/* Encoding helpers: the linuxroot libpq.a is built without libpgcommon, so the
581 * client-side encoding tables are absent. Returning sensible defaults keeps
582 * basic libpq usage (connect, query in SQL_ASCII / UTF8) working. */
583
584int pg_get_encoding_from_locale(const char *ctype, int write_message) {
585 (void)ctype;
586 (void)write_message;
587 return -1;
588}
589
590static const char *const pg_encoding_names[] = {
591 "SQL_ASCII", "EUC_JP", "EUC_CN", "EUC_KR", "EUC_TW", "EUC_JIS_2004",
592 "UTF8", "MULE_INTERNAL", "LATIN1", "LATIN2", "LATIN3", "LATIN4", "LATIN5",
593 "LATIN6", "LATIN7", "LATIN8", "LATIN9", "LATIN10", "WIN1256", "WIN1258",
594 "WIN866", "WIN874", "KOI8R", "WIN1251", "WIN1252", "ISO_8859_5", "ISO_8859_6",
595 "ISO_8859_7", "ISO_8859_8", "WIN1250", "WIN1253", "WIN1254", "WIN1255",
596 "WIN1257", "KOI8U", "SJIS", "BIG5", "GBK", "UHC", "GB18030", "JOHAB",
597 "SHIFT_JIS_2004",
598};
599
600const char *pg_encoding_to_char(int encoding) {
601 const int n = (int)(sizeof(pg_encoding_names) / sizeof(pg_encoding_names[0]));
602 if (encoding < 0 || encoding >= n) {
603 return "SQL_ASCII";
604 }
605 return pg_encoding_names[encoding];
606}
607
608int pg_char_to_encoding(const char *name) {
609 const int n = (int)(sizeof(pg_encoding_names) / sizeof(pg_encoding_names[0]));
610 if (name == NULL) {
611 return -1;
612 }
613 for (int i = 0; i < n; i++) {
614 if (strcasecmp(name, pg_encoding_names[i]) == 0) {
615 return i;
616 }
617 }
618 return -1;
619}
620
621int pg_valid_server_encoding_id(int encoding) {
622 return encoding == 0 || encoding == 6;
623}
624
625/* Multibyte helpers. Treat SQL_ASCII (0) and UTF8 (6) properly; for other
626 * encodings, fall back to UTF8-shaped behaviour. Good enough for libpq's
627 * frontend encoding probes, but not a substitute for libpgcommon's tables. */
628int pg_encoding_max_length(int encoding) {
629 if (encoding == 0 /* SQL_ASCII */) {
630 return 1;
631 }
632 return 4; /* UTF-8 worst case */
633}
634
635int pg_encoding_mblen(int encoding, const char *mbstr) {
636 unsigned char c = (unsigned char)*mbstr;
637 if (encoding == 0 /* SQL_ASCII */) {
638 return 1;
639 }
640 if (c < 0x80) {
641 return 1;
642 }
643 if ((c & 0xe0) == 0xc0) {
644 return 2;
645 }
646 if ((c & 0xf0) == 0xe0) {
647 return 3;
648 }
649 if ((c & 0xf8) == 0xf0) {
650 return 4;
651 }
652 return 1;
653}
654
655int pg_encoding_dsplen(int encoding, const char *mbstr) {
656 (void)encoding;
657 unsigned char c = (unsigned char)*mbstr;
658 if (c == 0) {
659 return 0;
660 }
661 if (c < 0x20 || c == 0x7f) {
662 return -1; /* control char: caller decides */
663 }
664 return 1;
665}
666
667/* libpgcommon link canary: signals that libpq was linked with the matching
668 * libpgcommon flavour. We only have the frontend, so always report frontend. */
669int pg_link_canary_is_frontend(void) {
670 return 1;
671}
672