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 */
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
)
36 data
= tdb_fetch(tdb
, key
);
37 if (!data
.dptr
|| data
.dsize
!= sizeof(int)) return -1;
39 memcpy(&ret
, data
.dptr
, sizeof(int));
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
)
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
71 int tdb_store_by_string(TDB_CONTEXT
*tdb
, char *keystr
, void *buffer
, int len
)
76 key
.dsize
= strlen(keystr
) + 1;
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
)
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
, ...)
109 int bufsize0
= bufsize
;
116 switch ((c
= *fmt
++)) {
119 w
= va_arg(ap
, uint16
);
120 if (bufsize
>= len
) {
126 d
= va_arg(ap
, uint32
);
127 if (bufsize
>= len
) {
133 p
= va_arg(ap
, void *);
135 if (bufsize
>= len
) {
140 s
= va_arg(ap
,char *);
143 if (bufsize
>= len
) {
148 s
= va_arg(ap
,char *);
151 if (bufsize
>= len
) {
157 s
= va_arg(ap
, char *);
159 if (bufsize
>= len
) {
165 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
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
, ...)
196 int bufsize0
= bufsize
;
203 switch ((c
=*fmt
++)) {
206 w
= va_arg(ap
, uint16
*);
207 if (bufsize
< len
) goto no_space
;
212 d
= va_arg(ap
, uint32
*);
213 if (bufsize
< len
) goto no_space
;
218 p
= va_arg(ap
, void **);
219 if (bufsize
< len
) goto no_space
;
220 *p
= (void *)IVAL(buf
, 0);
223 s
= va_arg(ap
,char *);
224 len
= strlen(buf
) + 1;
225 if (bufsize
< len
|| len
> sizeof(pstring
)) goto no_space
;
229 s
= va_arg(ap
,char *);
230 len
= strlen(buf
) + 1;
231 if (bufsize
< len
|| len
> sizeof(fstring
)) goto no_space
;
235 i
= va_arg(ap
, int *);
236 b
= va_arg(ap
, char **);
238 if (bufsize
< len
) goto no_space
;
242 if (bufsize
< len
) goto no_space
;
243 *b
= (char *)malloc(*i
);
244 if (! *b
) goto no_space
;
245 memcpy(*b
, buf
+4, *i
);
248 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
259 DEBUG(8,("tdb_unpack(%s, %d) -> %d\n",
260 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
262 return PTR_DIFF(buf
, buf0
);