GnuCash  5.6-150-g038405b370+
escape.cpp
1 /********************************************************************\
2  * escape.c : escape SQL reserved characters *
3  * Copyright (C) 2001 Linas Vepstas <linas@linas.org> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21 \********************************************************************/
22 
23 /*
24  * FILE:
25  * escape.c
26  *
27  * FUNCTION:
28  * Escapes the ' and \ characters in a string
29  */
30 
31 #include <config.h>
32 #include <glib.h>
33 #include <string.h>
34 
35 #include "gnc-engine.h"
36 #include "escape.h"
37 
38 static QofLogModule log_module = GNC_MOD_BACKEND;
39 
40 /* ================================================ */
41 
42 struct _escape
43 {
44  /* pointer to memory used for escaping arguments */
45  char* escape;
46  size_t esc_buflen;
47 };
48 
49 /* ================================================ */
50 /* escape single-quote marks and backslashes so that the
51  * database SQL parser doesn't puke on the query string
52  */
53 
54 const char*
55 sqlEscapeString (sqlEscape* b, const char* str)
56 {
57  const char* p, *src_head;
58  char* dst_tail;
59  size_t len, slen;
60 
61  ENTER ("str = %s", str);
62 
63  if (!b || !str)
64  {
65  LEAVE ("(null) args");
66  return NULL;
67  }
68 
69  /* if a string is escaped twice, just return the first */
70  if (b->escape == str)
71  {
72  LEAVE ("%s: already escaped", str);
73  return str;
74  }
75 
76  /* if nothing to escape, just return */
77  len = strlen (str);
78  slen = strcspn (str, "\\\'");
79  if (len == slen)
80  {
81  LEAVE ("nothing to escape");
82  return str;
83  }
84 
85  /* count to see how much space we'll need */
86  p = str + slen + 1;
87  while (*p)
88  {
89  len ++;
90  p += 1 + strcspn (p, "\\\'");
91  }
92 
93  /* get more space, if needed */
94  if (len >= b->esc_buflen)
95  {
96  b->escape = static_cast < decltype (b->escape) > (g_realloc (b->escape,
97  len + 100));
98  b->esc_buflen = len + 100;
99  }
100 
101  /* copy and escape */
102  src_head = (char*) str;
103  dst_tail = b->escape;
104  p = src_head + strcspn (src_head, "\\\'");
105  while (*p)
106  {
107  size_t cp_len = p - src_head;
108 
109  strncpy (dst_tail, src_head, cp_len);
110  dst_tail += cp_len;
111  *dst_tail = '\\';
112  dst_tail ++;
113  *dst_tail = *p;
114  dst_tail ++;
115 
116  src_head = p + 1;
117  p = src_head + strcspn (src_head, "\\\'");
118  }
119  if (p != src_head)
120  {
121  size_t cp_len = p - src_head;
122 
123  strncpy (dst_tail, src_head, cp_len);
124  dst_tail += cp_len;
125  }
126  *dst_tail = 0;
127 
128  LEAVE ("b->escape = %s", b->escape);
129  return b->escape;
130 }
131 
132 /* ================================================ */
133 
134 #define INITIAL_BUFSZ 2000
135 
136 sqlEscape*
137 sqlEscape_new (void)
138 {
139  sqlEscape* b = g_new (sqlEscape, 1);
140 
141  b->escape = static_cast < decltype (b->escape) > (g_malloc (INITIAL_BUFSZ));
142  b->esc_buflen = INITIAL_BUFSZ;
143  return (b);
144 }
145 
146 /* ================================================ */
147 
148 void
149 sqlEscape_destroy (sqlEscape* b)
150 {
151  ENTER (" ");
152  if (!b)
153  {
154  LEAVE ("b is (null)");
155  return;
156  }
157  g_free (b->escape);
158  b->escape = NULL;
159  g_free (b);
160  LEAVE (" ");
161 }
162 
163 /* ================ END OF FILE ==================== */
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
All type declarations for the whole Gnucash engine.
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282