PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ascii.c File Reference
#include "postgres.h"
#include "mb/pg_wchar.h"
#include "utils/ascii.h"
#include "utils/builtins.h"
Include dependency graph for ascii.c:

Go to the source code of this file.

Macros

#define RANGE_128   128
 
#define RANGE_160   160
 

Functions

static void pg_to_ascii (unsigned char *src, unsigned char *src_end, unsigned char *dest, int enc)
 
static textencode_to_ascii (text *data, int enc)
 
Datum to_ascii_encname (PG_FUNCTION_ARGS)
 
Datum to_ascii_enc (PG_FUNCTION_ARGS)
 
Datum to_ascii_default (PG_FUNCTION_ARGS)
 
void ascii_safe_strlcpy (char *dest, const char *src, size_t destsiz)
 

Macro Definition Documentation

#define RANGE_128   128

Referenced by pg_to_ascii().

#define RANGE_160   160

Referenced by pg_to_ascii().

Function Documentation

void ascii_safe_strlcpy ( char *  dest,
const char *  src,
size_t  destsiz 
)

Definition at line 173 of file ascii.c.

Referenced by BackgroundWorkerStateChange(), and pgstat_get_crashed_backend_activity().

174 {
175  if (destsiz == 0) /* corner case: no room for trailing nul */
176  return;
177 
178  while (--destsiz > 0)
179  {
180  /* use unsigned char here to avoid compiler warning */
181  unsigned char ch = *src++;
182 
183  if (ch == '\0')
184  break;
185  /* Keep printable ASCII characters */
186  if (32 <= ch && ch <= 127)
187  *dest = ch;
188  /* White-space is also OK */
189  else if (ch == '\n' || ch == '\r' || ch == '\t')
190  *dest = ch;
191  /* Everything else is replaced with '?' */
192  else
193  *dest = '?';
194  dest++;
195  }
196 
197  *dest = '\0';
198 }
static text * encode_to_ascii ( text data,
int  enc 
)
static

Definition at line 103 of file ascii.c.

References pg_to_ascii(), VARDATA, and VARSIZE.

Referenced by to_ascii_default(), to_ascii_enc(), and to_ascii_encname().

104 {
105  pg_to_ascii((unsigned char *) VARDATA(data), /* src */
106  (unsigned char *) (data) + VARSIZE(data), /* src end */
107  (unsigned char *) VARDATA(data), /* dest */
108  enc); /* encoding */
109 
110  return data;
111 }
static void pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *dest, int enc)
Definition: ascii.c:28
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
struct pg_encoding enc
Definition: encode.c:522
static void pg_to_ascii ( unsigned char *  src,
unsigned char *  src_end,
unsigned char *  dest,
int  enc 
)
static

Definition at line 28 of file ascii.c.

References ascii(), ereport, errcode(), errmsg(), ERROR, pg_encoding_to_char(), PG_LATIN1, PG_LATIN2, PG_LATIN9, PG_WIN1250, range(), RANGE_128, and RANGE_160.

Referenced by encode_to_ascii().

29 {
30  unsigned char *x;
31  const unsigned char *ascii;
32  int range;
33 
34  /*
35  * relevant start for an encoding
36  */
37 #define RANGE_128 128
38 #define RANGE_160 160
39 
40  if (enc == PG_LATIN1)
41  {
42  /*
43  * ISO-8859-1 <range: 160 -- 255>
44  */
45  ascii = (const unsigned char *) " cL Y \"Ca -R 'u ., ?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
46  range = RANGE_160;
47  }
48  else if (enc == PG_LATIN2)
49  {
50  /*
51  * ISO-8859-2 <range: 160 -- 255>
52  */
53  ascii = (const unsigned char *) " A L LS \"SSTZ-ZZ a,l'ls ,sstz\"zzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt.";
54  range = RANGE_160;
55  }
56  else if (enc == PG_LATIN9)
57  {
58  /*
59  * ISO-8859-15 <range: 160 -- 255>
60  */
61  ascii = (const unsigned char *) " cL YS sCa -R Zu .z EeY?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
62  range = RANGE_160;
63  }
64  else if (enc == PG_WIN1250)
65  {
66  /*
67  * Window CP1250 <range: 128 -- 255>
68  */
69  ascii = (const unsigned char *) " ' \" %S<STZZ `'\"\".-- s>stzz L A \"CS -RZ ,l'u .,as L\"lzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt ";
70  range = RANGE_128;
71  }
72  else
73  {
74  ereport(ERROR,
75  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
76  errmsg("encoding conversion from %s to ASCII not supported",
78  return; /* keep compiler quiet */
79  }
80 
81  /*
82  * Encode
83  */
84  for (x = src; x < src_end; x++)
85  {
86  if (*x < 128)
87  *dest++ = *x;
88  else if (*x < range)
89  *dest++ = ' '; /* bogus 128 to 'range' */
90  else
91  *dest++ = ascii[*x - range];
92  }
93 }
#define RANGE_128
#define RANGE_160
Datum ascii(PG_FUNCTION_ARGS)
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
struct pg_encoding enc
Definition: encode.c:522
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
#define ereport(elevel, rest)
Definition: elog.h:122
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum to_ascii_default ( PG_FUNCTION_ARGS  )

Definition at line 155 of file ascii.c.

References enc, encode_to_ascii(), GetDatabaseEncoding(), PG_GETARG_TEXT_P_COPY, and PG_RETURN_TEXT_P.

156 {
157  text *data = PG_GETARG_TEXT_P_COPY(0);
158  int enc = GetDatabaseEncoding();
159 
160  PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
161 }
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:279
struct pg_encoding enc
Definition: encode.c:522
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
Definition: c.h:439
static text * encode_to_ascii(text *data, int enc)
Definition: ascii.c:103
Datum to_ascii_enc ( PG_FUNCTION_ARGS  )

Definition at line 137 of file ascii.c.

References enc, encode_to_ascii(), ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_TEXT_P_COPY, PG_RETURN_TEXT_P, and PG_VALID_ENCODING.

138 {
139  text *data = PG_GETARG_TEXT_P_COPY(0);
140  int enc = PG_GETARG_INT32(1);
141 
142  if (!PG_VALID_ENCODING(enc))
143  ereport(ERROR,
144  (errcode(ERRCODE_UNDEFINED_OBJECT),
145  errmsg("%d is not a valid encoding code", enc)));
146 
147  PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
148 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:279
#define ERROR
Definition: elog.h:43
struct pg_encoding enc
Definition: encode.c:522
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:301
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
static text * encode_to_ascii(text *data, int enc)
Definition: ascii.c:103
Datum to_ascii_encname ( PG_FUNCTION_ARGS  )

Definition at line 118 of file ascii.c.

References enc, encode_to_ascii(), ereport, errcode(), errmsg(), ERROR, NameStr, pg_char_to_encoding(), PG_GETARG_NAME, PG_GETARG_TEXT_P_COPY, and PG_RETURN_TEXT_P.

119 {
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);
123 
124  if (enc < 0)
125  ereport(ERROR,
126  (errcode(ERRCODE_UNDEFINED_OBJECT),
127  errmsg("%s is not a valid encoding name", encname)));
128 
129  PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
130 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:279
#define ERROR
Definition: elog.h:43
struct pg_encoding enc
Definition: encode.c:522
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
Definition: c.h:439
static text * encode_to_ascii(text *data, int enc)
Definition: ascii.c:103
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243