1#include <ctype.h>
2#include <stdio.h>
3#include <malloc.h>
4#include <unistd.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7#include "include/byteorder.h"
8
9#define PARSE_SCALARS (1<<0)
10#define PARSE_BUFFERS (1<<1)
11
12#ifndef MIN
13#define MIN(a,b) ((a)<(b)?(a):(b))
14#endif
15
16#ifndef MAX
17#define MAX(a,b) ((a)>(b)?(a):(b))
18#endif
19
20#define DEBUG(lvl, str) printf str;
21#define DEBUGADD(lvl, str) printf str;
22
23#define MARSHALL 0
24#define UNMARSHALL 1
25
26#define MARSHALLING(ps) (!(ps)->io)
27#define UNMARSHALLING(ps) ((ps)->io)
28
29typedef int BOOL;
30typedef unsigned char uint8;
31typedef unsigned char uchar;
32typedef unsigned short uint16;
33typedef unsigned short wchar;
34typedef unsigned uint32;
35typedef char *SMBSTR;
36
37/* a null terminated unicode string */
38typedef uint16 ZUSTRING;
39
40#ifndef _PSTRING
41
42#define PSTRING_LEN 1024
43#define FSTRING_LEN 128
44
45typedef char pstring[PSTRING_LEN];
46typedef char fstring[FSTRING_LEN];
47
48#define _PSTRING
49
50#endif
51#define False 0
52#define True 1
53
54/* zero a structure given a pointer to the structure */
55#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
56
57#define MAX_UNISTRLEN 256
58#define MAX_STRINGLEN 256
59#define MAX_BUFFERLEN 512
60
61typedef struct _io_struct
62{
63	BOOL io; /* parsing in or out of data stream */
64	/*
65	 * If the (incoming) data is big-endian. On output we are
66	 * always little-endian.
67	 */
68	BOOL bigendian_data;
69	BOOL is_dynamic; /* Do we own this memory or not ? */
70	BOOL autoalign; /* should we auto-align all elements? */
71	uint32 data_offset; /* Current working offset into data. */
72	uint32 buffer_size; /* Current size of the buffer. */
73	uint32 grow_size; /* size requested via io_grow() calls */
74	char *data_p; /* The buffer itself. */
75} io_struct;
76
77
78char *io_mem_get(io_struct *ps, uint32 extra_size);
79BOOL io_init(io_struct *ps, uint32 size, BOOL io);
80void io_debug(io_struct *ps, int depth, char *desc, char *fn_name);
81BOOL io_align(io_struct *ps, int align);
82BOOL io_align4(io_struct *ps, int align);
83BOOL io_align2(io_struct *ps, int align);
84BOOL io_read(io_struct *ps, int fd, size_t len, int timeout);
85void dump_data(int level,char *buf1,int len);
86BOOL io_alloc(char *name, io_struct *ps, void **ptr, unsigned size);
87BOOL io_uint32(char *name, io_struct *ps, int depth, uint32 *data32, unsigned flags);
88BOOL io_uint16(char *name, io_struct *ps, int depth, uint16 *data16, unsigned flags);
89BOOL io_uint8(char *name, io_struct *ps, int depth, uint8 *data8, unsigned flags);
90BOOL io_pointer(char *desc, io_struct *ps, int depth, void **p, unsigned flags);
91BOOL io_SMBSTR(char *name, io_struct *ps, int depth, char **str, unsigned flags);
92BOOL io_io_struct(char *name, io_struct *ps, int depth, io_struct *io, unsigned flags);
93BOOL io_wstring(char *name, io_struct *ps, int depth, uint16 *data16s, int len, unsigned flags);
94BOOL io_uint8s_fixed(char *name, io_struct *ps, int depth, uint8 *data8s, int len, unsigned flags);
95BOOL io_uint8s(char *name, io_struct *ps, int depth, uint8 **data8s, int len, unsigned flags);
96
97char *tab_depth(int depth);
98void *Realloc(void *p,size_t size);
99void dump_data(int level,char *buf1,int len);
100void print_asc(int level, uchar const *buf, int len);
101BOOL io_ZUSTRING(char *name, io_struct *ps, int depth, uint16 **ustr, unsigned flags);
102size_t strlen_w(void *src);
103
104