PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ escape_connection_string()

char* escape_connection_string ( char *  str)

Escape strings that are to be used as part of a PostgreSQL connection string.

If no characters require escaping, simply return the input pointer. Otherwise return a new allocated string.

Definition at line 109 of file shpcommon.c.

110 {
111  /*
112  * Escape apostrophes and backslashes:
113  * ' -> \'
114  * \ -> \\
115  *
116  * 1. find # of characters
117  * 2. make new string
118  */
119 
120  char *result;
121  char *ptr, *optr;
122  int toescape = 0;
123  size_t size;
124 
125  ptr = str;
126 
127  /* Count how many characters we need to escape so we know the size of the string we need to return */
128  while (*ptr)
129  {
130  if (*ptr == '\'' || *ptr == '\\')
131  toescape++;
132 
133  ptr++;
134  }
135 
136  /* If we don't have to escape anything, simply return the input pointer */
137  if (toescape == 0)
138  return str;
139 
140  size = ptr - str + toescape + 1;
141  result = calloc(1, size);
142  optr = result;
143  ptr = str;
144 
145  while (*ptr)
146  {
147  if (*ptr == '\'' || *ptr == '\\')
148  *optr++ = '\\';
149 
150  *optr++ = *ptr++;
151  }
152 
153  *optr = '\0';
154 
155  return result;
156 }