PostgreSQL Source Code  git master
ascii.c File Reference
#include "postgres.h"
#include "mb/pg_wchar.h"
#include "utils/ascii.h"
#include "utils/fmgrprotos.h"
#include "varatt.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

◆ RANGE_128

#define RANGE_128   128

◆ RANGE_160

#define RANGE_160   160

Function Documentation

◆ ascii_safe_strlcpy()

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

Definition at line 174 of file ascii.c.

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

References generate_unaccent_rules::dest.

Referenced by BackgroundWorkerStateChange(), and pgstat_get_crashed_backend_activity().

◆ encode_to_ascii()

static text * encode_to_ascii ( text data,
int  enc 
)
static

Definition at line 104 of file ascii.c.

105 {
106  pg_to_ascii((unsigned char *) VARDATA(data), /* src */
107  (unsigned char *) (data) + VARSIZE(data), /* src end */
108  (unsigned char *) VARDATA(data), /* dest */
109  enc); /* encoding */
110 
111  return data;
112 }
static void pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *dest, int enc)
Definition: ascii.c:29
enc
const void * data
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARSIZE(PTR)
Definition: varatt.h:279

References data, enc, pg_to_ascii(), VARDATA, and VARSIZE.

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

◆ pg_to_ascii()

static void pg_to_ascii ( unsigned char *  src,
unsigned char *  src_end,
unsigned char *  dest,
int  enc 
)
static

Definition at line 29 of file ascii.c.

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

References ascii(), generate_unaccent_rules::dest, enc, ereport, errcode(), errmsg(), ERROR, pg_encoding_to_char, PG_LATIN1, PG_LATIN2, PG_LATIN9, PG_WIN1250, range(), RANGE_128, RANGE_160, and x.

Referenced by encode_to_ascii().

◆ to_ascii_default()

Datum to_ascii_default ( PG_FUNCTION_ARGS  )

Definition at line 156 of file ascii.c.

157 {
159  int enc = GetDatabaseEncoding();
160 
162 }
static text * encode_to_ascii(text *data, int enc)
Definition: ascii.c:104
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:315
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
Definition: c.h:687

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

◆ to_ascii_enc()

Datum to_ascii_enc ( PG_FUNCTION_ARGS  )

Definition at line 138 of file ascii.c.

139 {
141  int enc = PG_GETARG_INT32(1);
142 
143  if (!PG_VALID_ENCODING(enc))
144  ereport(ERROR,
145  (errcode(ERRCODE_UNDEFINED_OBJECT),
146  errmsg("%d is not a valid encoding code", enc)));
147 
149 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:287

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

◆ to_ascii_encname()

Datum to_ascii_encname ( PG_FUNCTION_ARGS  )

Definition at line 119 of file ascii.c.

120 {
122  char *encname = NameStr(*PG_GETARG_NAME(1));
123  int enc = pg_char_to_encoding(encname);
124 
125  if (enc < 0)
126  ereport(ERROR,
127  (errcode(ERRCODE_UNDEFINED_OBJECT),
128  errmsg("%s is not a valid encoding name", encname)));
129 
131 }
#define NameStr(name)
Definition: c.h:746
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define pg_char_to_encoding
Definition: pg_wchar.h:629

References data, 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.