blob.h revision 2632
1/*
2 * blob.h
3 *
4 * Binary blob handling.
5 *
6 * Copyright (c) 2002 Dug Song <dugsong@monkey.org>
7 *
8 * $Id: blob.h,v 1.2 2002/04/05 03:06:44 dugsong Exp $
9 */
10
11#ifndef DNET_BLOB_H
12#define DNET_BLOB_H
13
14typedef struct blob {
15        u_char		*base;		/* start of data */
16        int		 off;		/* offset into data */
17        int		 end;		/* end of data */
18        int		 size;		/* size of allocation */
19} blob_t;
20
21__BEGIN_DECLS
22blob_t	*blob_new(void);
23
24int	 blob_read(blob_t *b, void *buf, int len);
25int	 blob_write(blob_t *b, const void *buf, int len);
26
27int	 blob_seek(blob_t *b, int off, int whence);
28#define  blob_skip(b, l)	blob_seek(b, l, SEEK_CUR)
29#define  blob_rewind(b)		blob_seek(b, 0, SEEK_SET)
30
31#define	 blob_offset(b)		((b)->off)
32#define	 blob_left(b)		((b)->end - (b)->off)
33
34int	 blob_index(blob_t *b, const void *buf, int len);
35int	 blob_rindex(blob_t *b, const void *buf, int len);
36
37int	 blob_pack(blob_t *b, const char *fmt, ...);
38int	 blob_unpack(blob_t *b, const char *fmt, ...);
39
40int	 blob_insert(blob_t *b, const void *buf, int len);
41int	 blob_delete(blob_t *b, void *buf, int len);
42
43int	 blob_print(blob_t *b, char *style, int len);
44
45blob_t	*blob_free(blob_t *b);
46
47int	 blob_register_alloc(size_t size, void *(*bmalloc)(size_t),
48            void (*bfree)(void *), void *(*brealloc)(void *, size_t));
49#ifdef va_start
50typedef int (*blob_fmt_cb)(int pack, int len, blob_t *b, va_list *arg);
51
52int	 blob_register_pack(char c, blob_fmt_cb fmt_cb);
53#endif
54__END_DECLS
55
56#endif /* DNET_BLOB_H */
57