2 Unix SMB/Netbios implementation.
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.
24 /* these are little tdb utility functions that are meant to make
25 dealing with a tdb database a little less cumbersome in Samba */
27 /* lock a chain by string */
28 int tdb_lock_bystring(TDB_CONTEXT *tdb, char *keyval)
33 key.dsize = strlen(keyval)+1;
35 return tdb_lockchain(tdb, key);
38 /* unlock a chain by string */
39 int tdb_unlock_bystring(TDB_CONTEXT *tdb, char *keyval)
44 key.dsize = strlen(keyval)+1;
46 return tdb_unlockchain(tdb, key);
49 /* lock a chain by string key */
52 /* fetch a value by a arbitrary blob key, return -1 if not found */
53 int tdb_fetch_int_byblob(TDB_CONTEXT *tdb, char *keyval, size_t len)
60 data = tdb_fetch(tdb, key);
61 if (!data.dptr || data.dsize != sizeof(int)) return -1;
63 memcpy(&ret, data.dptr, sizeof(int));
68 /* fetch a value by string key, return -1 if not found */
69 int tdb_fetch_int(TDB_CONTEXT *tdb, char *keystr)
71 return tdb_fetch_int_byblob(tdb, keystr, strlen(keystr) + 1);
74 /* store a value by an arbitary blob key, return 0 on success, -1 on failure */
75 int tdb_store_int_byblob(TDB_CONTEXT *tdb, char *keystr, size_t len, int v)
81 data.dptr = (void *)&v;
82 data.dsize = sizeof(int);
84 return tdb_store(tdb, key, data, TDB_REPLACE);
87 /* store a value by string key, return 0 on success, -1 on failure */
88 int tdb_store_int(TDB_CONTEXT *tdb, char *keystr, int v)
90 return tdb_store_int_byblob(tdb, keystr, strlen(keystr) + 1, v);
93 /* Store a buffer by a null terminated string key. Return 0 on success, -1
95 int tdb_store_by_string(TDB_CONTEXT *tdb, char *keystr, void *buffer, int len)
100 key.dsize = strlen(keystr) + 1;
105 return tdb_store(tdb, key, data, TDB_REPLACE);
108 /* Fetch a buffer using a null terminated string key. Don't forget to call
109 free() on the result dptr. */
110 TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, char *keystr)
115 key.dsize = strlen(keystr) + 1;
117 return tdb_fetch(tdb, key);
121 /* useful pair of routines for packing/unpacking data consisting of
122 integers and strings */
123 size_t tdb_pack(char *buf, int bufsize, char *fmt, ...)
133 int bufsize0 = bufsize;
140 switch ((c = *fmt++)) {
143 w = va_arg(ap, uint16);
144 if (bufsize >= len) {
150 d = va_arg(ap, uint32);
151 if (bufsize >= len) {
157 p = va_arg(ap, void *);
159 if (bufsize >= len) {
164 s = va_arg(ap,char *);
167 if (bufsize >= len) {
172 s = va_arg(ap,char *);
175 if (bufsize >= len) {
181 s = va_arg(ap, char *);
183 if (bufsize >= len) {
189 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
200 DEBUG(8,("tdb_pack(%s, %d) -> %d\n",
201 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
203 return PTR_DIFF(buf, buf0);
208 /* useful pair of routines for packing/unpacking data consisting of
209 integers and strings */
210 int tdb_unpack(char *buf, int bufsize, char *fmt, ...)
220 int bufsize0 = bufsize;
227 switch ((c=*fmt++)) {
230 w = va_arg(ap, uint16 *);
231 if (bufsize < len) goto no_space;
236 d = va_arg(ap, uint32 *);
237 if (bufsize < len) goto no_space;
242 p = va_arg(ap, void **);
243 if (bufsize < len) goto no_space;
244 *p = (void *)IVAL(buf, 0);
247 s = va_arg(ap,char *);
248 len = strlen(buf) + 1;
249 if (bufsize < len || len > sizeof(pstring)) goto no_space;
253 s = va_arg(ap,char *);
254 len = strlen(buf) + 1;
255 if (bufsize < len || len > sizeof(fstring)) goto no_space;
259 i = va_arg(ap, int *);
260 b = va_arg(ap, char **);
262 if (bufsize < len) goto no_space;
266 if (bufsize < len) goto no_space;
267 *b = (char *)malloc(*i);
268 if (! *b) goto no_space;
269 memcpy(*b, buf+4, *i);
272 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
283 DEBUG(8,("tdb_unpack(%s, %d) -> %d\n",
284 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
286 return PTR_DIFF(buf, buf0);