deal with allocation size of 0 in prs_unistr when UNMARSHALLING
[Samba.git] / source / tdb / tdbutil.c
blobe426aa51cd958acd5709753e55bdf3a9dd99d045
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 tdb utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 /* these are little tdb utility functions that are meant to make
25 dealing with a tdb database a little less cumbersome in Samba */
28 /* fetch a value by a arbitrary blob key, return -1 if not found */
29 int tdb_fetch_int_byblob(TDB_CONTEXT *tdb, char *keyval, size_t len)
31 TDB_DATA key, data;
32 int ret;
34 key.dptr = keyval;
35 key.dsize = len;
36 data = tdb_fetch(tdb, key);
37 if (!data.dptr || data.dsize != sizeof(int)) return -1;
39 memcpy(&ret, data.dptr, sizeof(int));
40 free(data.dptr);
41 return ret;
44 /* fetch a value by string key, return -1 if not found */
45 int tdb_fetch_int(TDB_CONTEXT *tdb, char *keystr)
47 return tdb_fetch_int_byblob(tdb, keystr, strlen(keystr) + 1);
50 /* store a value by an arbitary blob key, return 0 on success, -1 on failure */
51 int tdb_store_int_byblob(TDB_CONTEXT *tdb, char *keystr, size_t len, int v)
53 TDB_DATA key, data;
55 key.dptr = keystr;
56 key.dsize = len;
57 data.dptr = (void *)&v;
58 data.dsize = sizeof(int);
60 return tdb_store(tdb, key, data, TDB_REPLACE);
63 /* store a value by string key, return 0 on success, -1 on failure */
64 int tdb_store_int(TDB_CONTEXT *tdb, char *keystr, int v)
66 return tdb_store_int_byblob(tdb, keystr, strlen(keystr) + 1, v);
69 /* Store a buffer by a null terminated string key. Return 0 on success, -1
70 on failure */
71 int tdb_store_by_string(TDB_CONTEXT *tdb, char *keystr, void *buffer, int len)
73 TDB_DATA key, data;
75 key.dptr = keystr;
76 key.dsize = strlen(keystr) + 1;
78 data.dptr = buffer;
79 data.dsize = len;
81 return tdb_store(tdb, key, data, TDB_REPLACE);
84 /* Fetch a buffer using a null terminated string key. Don't forget to call
85 free() on the result dptr. */
86 TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, char *keystr)
88 TDB_DATA key;
90 key.dptr = keystr;
91 key.dsize = strlen(keystr) + 1;
93 return tdb_fetch(tdb, key);
97 /* useful pair of routines for packing/unpacking data consisting of
98 integers and strings */
99 size_t tdb_pack(char *buf, int bufsize, char *fmt, ...)
101 uint16 w;
102 uint32 d;
103 int i;
104 void *p;
105 int len;
106 char *s;
107 char *buf0 = buf;
108 char *fmt0 = fmt;
109 int bufsize0 = bufsize;
110 va_list ap;
111 char c;
113 va_start(ap, fmt);
115 while (*fmt) {
116 switch ((c = *fmt++)) {
117 case 'w':
118 len = 2;
119 w = va_arg(ap, uint16);
120 if (bufsize >= len) {
121 SSVAL(buf, 0, w);
123 break;
124 case 'd':
125 len = 4;
126 d = va_arg(ap, uint32);
127 if (bufsize >= len) {
128 SIVAL(buf, 0, d);
130 break;
131 case 'p':
132 len = 4;
133 p = va_arg(ap, void *);
134 d = p?1:0;
135 if (bufsize >= len) {
136 SIVAL(buf, 0, d);
138 break;
139 case 'P':
140 s = va_arg(ap,char *);
141 w = strlen(s);
142 len = w + 1;
143 if (bufsize >= len) {
144 memcpy(buf, s, len);
146 break;
147 case 'f':
148 s = va_arg(ap,char *);
149 w = strlen(s);
150 len = w + 1;
151 if (bufsize >= len) {
152 memcpy(buf, s, len);
154 break;
155 case 'B':
156 i = va_arg(ap, int);
157 s = va_arg(ap, char *);
158 len = 4+i;
159 if (bufsize >= len) {
160 SIVAL(buf, 0, i);
161 memcpy(buf+4, s, i);
163 break;
164 default:
165 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
166 c, fmt));
167 break;
170 buf += len;
171 bufsize -= len;
174 va_end(ap);
176 DEBUG(8,("tdb_pack(%s, %d) -> %d\n",
177 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
179 return PTR_DIFF(buf, buf0);
184 /* useful pair of routines for packing/unpacking data consisting of
185 integers and strings */
186 int tdb_unpack(char *buf, int bufsize, char *fmt, ...)
188 uint16 *w;
189 uint32 *d;
190 int len;
191 int *i;
192 void **p;
193 char *s, **b;
194 char *buf0 = buf;
195 char *fmt0 = fmt;
196 int bufsize0 = bufsize;
197 va_list ap;
198 char c;
200 va_start(ap, fmt);
202 while (*fmt) {
203 switch ((c=*fmt++)) {
204 case 'w':
205 len = 2;
206 w = va_arg(ap, uint16 *);
207 if (bufsize < len) goto no_space;
208 *w = SVAL(buf, 0);
209 break;
210 case 'd':
211 len = 4;
212 d = va_arg(ap, uint32 *);
213 if (bufsize < len) goto no_space;
214 *d = IVAL(buf, 0);
215 break;
216 case 'p':
217 len = 4;
218 p = va_arg(ap, void **);
219 if (bufsize < len) goto no_space;
220 *p = (void *)IVAL(buf, 0);
221 break;
222 case 'P':
223 s = va_arg(ap,char *);
224 len = strlen(buf) + 1;
225 if (bufsize < len || len > sizeof(pstring)) goto no_space;
226 memcpy(s, buf, len);
227 break;
228 case 'f':
229 s = va_arg(ap,char *);
230 len = strlen(buf) + 1;
231 if (bufsize < len || len > sizeof(fstring)) goto no_space;
232 memcpy(s, buf, len);
233 break;
234 case 'B':
235 i = va_arg(ap, int *);
236 b = va_arg(ap, char **);
237 len = 4;
238 if (bufsize < len) goto no_space;
239 *i = IVAL(buf, 0);
240 if (! *i) break;
241 len += *i;
242 if (bufsize < len) goto no_space;
243 *b = (char *)malloc(*i);
244 if (! *b) goto no_space;
245 memcpy(*b, buf+4, *i);
246 break;
247 default:
248 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
249 c, fmt));
250 break;
253 buf += len;
254 bufsize -= len;
257 va_end(ap);
259 DEBUG(8,("tdb_unpack(%s, %d) -> %d\n",
260 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
262 return PTR_DIFF(buf, buf0);
264 no_space:
265 return -1;