1 /*-----------------------------------------------------------------------
3 * The PostgreSQL routine for string to ascii conversion.
5 * Portions Copyright (c) 1999-2019, PostgreSQL Global Development Group
8 * src/backend/utils/adt/ascii.c
10 *-----------------------------------------------------------------------
14 #include "mb/pg_wchar.h"
15 #include "utils/ascii.h"
16 #include "utils/builtins.h"
18 static void pg_to_ascii(unsigned char *src, unsigned char *src_end,
19 unsigned char *dest, int enc);
20 static text *encode_to_ascii(text *data, int enc);
28 pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *dest, int enc)
31 const unsigned char *ascii;
35 * relevant start for an encoding
43 * ISO-8859-1 <range: 160 -- 255>
45 ascii = (const unsigned char *) " cL Y \"Ca -R 'u ., ?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
48 else if (enc == PG_LATIN2)
51 * ISO-8859-2 <range: 160 -- 255>
53 ascii = (const unsigned char *) " A L LS \"SSTZ-ZZ a,l'ls ,sstz\"zzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt.";
56 else if (enc == PG_LATIN9)
59 * ISO-8859-15 <range: 160 -- 255>
61 ascii = (const unsigned char *) " cL YS sCa -R Zu .z EeY?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
64 else if (enc == PG_WIN1250)
67 * Window CP1250 <range: 128 -- 255>
69 ascii = (const unsigned char *) " ' \" %S<STZZ `'\"\".-- s>stzz L A \"CS -RZ ,l'u .,as L\"lzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt ";
75 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
76 errmsg("encoding conversion from %s to ASCII not supported",
77 pg_encoding_to_char(enc))));
78 return; /* keep compiler quiet */
84 for (x = src; x < src_end; x++)
89 *dest++ = ' '; /* bogus 128 to 'range' */
91 *dest++ = ascii[*x - range];
98 * The text datum is overwritten in-place, therefore this coding method
99 * cannot support conversions that change the string length!
103 encode_to_ascii(text *data, int enc)
105 pg_to_ascii((unsigned char *) VARDATA(data), /* src */
106 (unsigned char *) (data) + VARSIZE(data), /* src end */
107 (unsigned char *) VARDATA(data), /* dest */
114 * convert to ASCII - enc is set as 'name' arg.
118 to_ascii_encname(PG_FUNCTION_ARGS)
120 text *data = PG_GETARG_TEXT_P_COPY(0);
121 char *encname = NameStr(*PG_GETARG_NAME(1));
122 int enc = pg_char_to_encoding(encname);
126 (errcode(ERRCODE_UNDEFINED_OBJECT),
127 errmsg("%s is not a valid encoding name", encname)));
129 PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
133 * convert to ASCII - enc is set as int4
137 to_ascii_enc(PG_FUNCTION_ARGS)
139 text *data = PG_GETARG_TEXT_P_COPY(0);
140 int enc = PG_GETARG_INT32(1);
142 if (!PG_VALID_ENCODING(enc))
144 (errcode(ERRCODE_UNDEFINED_OBJECT),
145 errmsg("%d is not a valid encoding code", enc)));
147 PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
151 * convert to ASCII - current enc is DatabaseEncoding
155 to_ascii_default(PG_FUNCTION_ARGS)
157 text *data = PG_GETARG_TEXT_P_COPY(0);
158 int enc = GetDatabaseEncoding();
160 PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
164 * Copy a string in an arbitrary backend-safe encoding, converting it to a
165 * valid ASCII string by replacing non-ASCII bytes with '?'. Otherwise the
166 * behavior is identical to strlcpy(), except that we don't bother with a
169 * This must not trigger ereport(ERROR), as it is called in postmaster.
173 ascii_safe_strlcpy(char *dest, const char *src, size_t destsiz)
175 if (destsiz == 0) /* corner case: no room for trailing nul */
178 while (--destsiz > 0)
180 /* use unsigned char here to avoid compiler warning */
181 unsigned char ch = *src++;
185 /* Keep printable ASCII characters */
186 if (32 <= ch && ch <= 127)
188 /* White-space is also OK */
189 else if (ch == '\n' || ch == '\r' || ch == '\t')
191 /* Everything else is replaced with '?' */