GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
escape.c
1 /********************************************************************\
2  * escape.c : escape SQL reserved characters *
3  * Copyright (C) 2001 Linas Vepstas <[email protected]> *
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 [email protected] *
21 \********************************************************************/
22 
23 /*
24  * FILE:
25  * esacpe.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 = g_realloc(b->escape, len + 100);
97  b->esc_buflen = len + 100;
98  }
99 
100  /* copy and escape */
101  src_head = (char *) str;
102  dst_tail = b->escape;
103  p = src_head + strcspn (src_head, "\\\'");
104  while (*p)
105  {
106  size_t cp_len = p - src_head;
107 
108  strncpy (dst_tail, src_head, cp_len);
109  dst_tail += cp_len;
110  *dst_tail = '\\';
111  dst_tail ++;
112  *dst_tail = *p;
113  dst_tail ++;
114 
115  src_head = p + 1;
116  p = src_head + strcspn (src_head, "\\\'");
117  }
118  if (p != src_head)
119  {
120  size_t cp_len = p - src_head;
121 
122  strncpy (dst_tail, src_head, cp_len);
123  dst_tail += cp_len;
124  }
125  *dst_tail = 0;
126 
127  LEAVE("b->escape = %s", b->escape);
128  return b->escape;
129 }
130 
131 /* ================================================ */
132 
133 #define INITIAL_BUFSZ 2000
134 
135 sqlEscape *
136 sqlEscape_new (void)
137 {
138  sqlEscape *b = g_new (sqlEscape, 1);
139 
140  b->escape = g_malloc (INITIAL_BUFSZ);
141  b->esc_buflen = INITIAL_BUFSZ;
142  return (b);
143 }
144 
145 /* ================================================ */
146 
147 void
148 sqlEscape_destroy (sqlEscape *b)
149 {
150  ENTER(" ");
151  if (!b)
152  {
153  LEAVE("b is (null)");
154  return;
155  }
156  g_free (b->escape);
157  b->escape = NULL;
158  g_free (b);
159  LEAVE(" ");
160 }
161 
162 /* ================ END OF FILE ==================== */
#define ENTER(format, args...)
Definition: qoflog.h:261
Definition: escape.c:42
All type declarations for the whole Gnucash engine.
#define LEAVE(format, args...)
Definition: qoflog.h:271
const gchar * QofLogModule
Definition: qofid.h:89