1 #ifndef _ATTR_H_INCLUDED_
2 #define _ATTR_H_INCLUDED_
3 
4 /*++
5 /* NAME
6 /*	attr 3h
7 /* SUMMARY
8 /*	attribute list manipulations
9 /* SYNOPSIS
10 /*	#include "attr.h"
11  DESCRIPTION
12  .nf
13 
14  /*
15   * System library.
16   */
17 #include <stdarg.h>
18 
19  /*
20   * Utility library.
21   */
22 #include <vstream.h>
23 #include <vstring.h>
24 #include <htable.h>
25 #include <nvtable.h>
26 #include <check_arg.h>
27 
28  /*
29   * Delegation for better data abstraction.
30   */
31 typedef int (*ATTR_SCAN_COMMON_FN) (VSTREAM *, int,...);
32 typedef int (*ATTR_SCAN_CUSTOM_FN) (ATTR_SCAN_COMMON_FN, VSTREAM *, int, void *);
33 typedef int (*ATTR_PRINT_COMMON_FN) (VSTREAM *, int,...);
34 typedef int (*ATTR_PRINT_CUSTOM_FN) (ATTR_PRINT_COMMON_FN, VSTREAM *, int, const void *);
35 
36  /*
37   * Attribute types. See attr_scan(3) for documentation.
38   */
39 #define ATTR_TYPE_END		0	/* end of data */
40 #define ATTR_TYPE_INT		1	/* Unsigned integer */
41 #define ATTR_TYPE_NUM		ATTR_TYPE_INT
42 #define ATTR_TYPE_STR		2	/* Character string */
43 #define ATTR_TYPE_HASH		3	/* Hash table */
44 #define ATTR_TYPE_NV		3	/* Name-value table */
45 #define ATTR_TYPE_LONG		4	/* Unsigned long */
46 #define ATTR_TYPE_DATA		5	/* Binary data */
47 #define ATTR_TYPE_FUNC		6	/* Function pointer */
48 #define ATTR_TYPE_STREQ		7	/* Requires (name, value) match */
49 
50  /*
51   * Optional sender-specified grouping for hash or nameval tables.
52   */
53 #define ATTR_TYPE_OPEN		'{'
54 #define ATTR_TYPE_CLOSE		'}'
55 #define ATTR_NAME_OPEN		"{"
56 #define ATTR_NAME_CLOSE		"}"
57 
58 #define ATTR_HASH_LIMIT		1024	/* Size of hash table */
59 
60  /*
61   * Typechecking support for variadic function arguments. See check_arg(3h)
62   * for documentation.
63   */
64 #define SEND_ATTR_INT(name, val)	ATTR_TYPE_INT, CHECK_CPTR(ATTR, char, (name)), CHECK_VAL(ATTR, int, (val))
65 #define SEND_ATTR_STR(name, val)	ATTR_TYPE_STR, CHECK_CPTR(ATTR, char, (name)), CHECK_CPTR(ATTR, char, (val))
66 #define SEND_ATTR_HASH(val)		ATTR_TYPE_HASH, CHECK_CPTR(ATTR, HTABLE, (val))
67 #define SEND_ATTR_NV(val)		ATTR_TYPE_NV, CHECK_CPTR(ATTR, NVTABLE, (val))
68 #define SEND_ATTR_LONG(name, val)	ATTR_TYPE_LONG, CHECK_CPTR(ATTR, char, (name)), CHECK_VAL(ATTR, long, (val))
69 #define SEND_ATTR_DATA(name, len, val)	ATTR_TYPE_DATA, CHECK_CPTR(ATTR, char, (name)), CHECK_VAL(ATTR, ssize_t, (len)), CHECK_CPTR(ATTR, void, (val))
70 #define SEND_ATTR_FUNC(func, val)	ATTR_TYPE_FUNC, CHECK_VAL(ATTR, ATTR_PRINT_CUSTOM_FN, (func)), CHECK_CPTR(ATTR, void, (val))
71 
72 #define RECV_ATTR_INT(name, val)	ATTR_TYPE_INT, CHECK_CPTR(ATTR, char, (name)), CHECK_PTR(ATTR, int, (val))
73 #define RECV_ATTR_STR(name, val)	ATTR_TYPE_STR, CHECK_CPTR(ATTR, char, (name)), CHECK_PTR(ATTR, VSTRING, (val))
74 #define RECV_ATTR_STREQ(name, val)	ATTR_TYPE_STREQ, CHECK_CPTR(ATTR, char, (name)), CHECK_CPTR(ATTR, char, (val))
75 #define RECV_ATTR_HASH(val)		ATTR_TYPE_HASH, CHECK_PTR(ATTR, HTABLE, (val))
76 #define RECV_ATTR_NV(val)		ATTR_TYPE_NV, CHECK_PTR(ATTR, NVTABLE, (val))
77 #define RECV_ATTR_LONG(name, val)	ATTR_TYPE_LONG, CHECK_CPTR(ATTR, char, (name)), CHECK_PTR(ATTR, long, (val))
78 #define RECV_ATTR_DATA(name, val)	ATTR_TYPE_DATA, CHECK_CPTR(ATTR, char, (name)), CHECK_PTR(ATTR, VSTRING, (val))
79 #define RECV_ATTR_FUNC(func, val)	ATTR_TYPE_FUNC, CHECK_VAL(ATTR, ATTR_SCAN_CUSTOM_FN, (func)), CHECK_PTR(ATTR, void, (val))
80 
81 CHECK_VAL_HELPER_DCL(ATTR, ssize_t);
82 CHECK_VAL_HELPER_DCL(ATTR, long);
83 CHECK_VAL_HELPER_DCL(ATTR, int);
84 CHECK_PTR_HELPER_DCL(ATTR, void);
85 CHECK_PTR_HELPER_DCL(ATTR, long);
86 CHECK_PTR_HELPER_DCL(ATTR, int);
87 CHECK_PTR_HELPER_DCL(ATTR, VSTRING);
88 CHECK_PTR_HELPER_DCL(ATTR, NVTABLE);
89 CHECK_PTR_HELPER_DCL(ATTR, HTABLE);
90 CHECK_CPTR_HELPER_DCL(ATTR, void);
91 CHECK_CPTR_HELPER_DCL(ATTR, char);
92 CHECK_CPTR_HELPER_DCL(ATTR, NVTABLE);
93 CHECK_CPTR_HELPER_DCL(ATTR, HTABLE);
94 CHECK_VAL_HELPER_DCL(ATTR, ATTR_PRINT_CUSTOM_FN);
95 CHECK_VAL_HELPER_DCL(ATTR, ATTR_SCAN_CUSTOM_FN);
96 
97  /*
98   * Flags that control processing. See attr_scan(3) for documentation.
99   */
100 #define ATTR_FLAG_NONE		0
101 #define ATTR_FLAG_MISSING	(1<<0)	/* Flag missing attribute */
102 #define ATTR_FLAG_EXTRA		(1<<1)	/* Flag spurious attribute */
103 #define ATTR_FLAG_MORE		(1<<2)	/* Don't skip or terminate */
104 #define ATTR_FLAG_PRINTABLE	(1<<3)	/* Sanitize received strings */
105 
106 #define ATTR_FLAG_STRICT	(ATTR_FLAG_MISSING | ATTR_FLAG_EXTRA)
107 #define ATTR_FLAG_ALL		(017)
108 
109  /*
110   * Default to null-terminated, as opposed to base64-encoded.
111   */
112 #define attr_print	attr_print0
113 #define attr_vprint	attr_vprint0
114 #define attr_scan	attr_scan0
115 #define attr_vscan	attr_vscan0
116 #define attr_scan_more	attr_scan_more0
117 
118  /*
119   * attr_print64.c.
120   */
121 extern int attr_print64(VSTREAM *, int,...);
122 extern int attr_vprint64(VSTREAM *, int, va_list);
123 
124  /*
125   * attr_scan64.c.
126   */
127 extern int WARN_UNUSED_RESULT attr_scan64(VSTREAM *, int,...);
128 extern int WARN_UNUSED_RESULT attr_vscan64(VSTREAM *, int, va_list);
129 extern int WARN_UNUSED_RESULT attr_scan_more64(VSTREAM *);
130 
131  /*
132   * attr_print0.c.
133   */
134 extern int attr_print0(VSTREAM *, int,...);
135 extern int attr_vprint0(VSTREAM *, int, va_list);
136 
137  /*
138   * attr_scan0.c.
139   */
140 extern int WARN_UNUSED_RESULT attr_scan0(VSTREAM *, int,...);
141 extern int WARN_UNUSED_RESULT attr_vscan0(VSTREAM *, int, va_list);
142 extern int WARN_UNUSED_RESULT attr_scan_more0(VSTREAM *);
143 
144  /*
145   * attr_scan_plain.c.
146   */
147 extern int attr_print_plain(VSTREAM *, int,...);
148 extern int attr_vprint_plain(VSTREAM *, int, va_list);
149 extern int attr_scan_more_plain(VSTREAM *);
150 
151  /*
152   * attr_print_plain.c.
153   */
154 extern int WARN_UNUSED_RESULT attr_scan_plain(VSTREAM *, int,...);
155 extern int WARN_UNUSED_RESULT attr_vscan_plain(VSTREAM *, int, va_list);
156 
157  /*
158   * Attribute names for testing the compatibility of the read and write
159   * routines.
160   */
161 #ifdef TEST
162 #define ATTR_NAME_INT		"number"
163 #define ATTR_NAME_STR		"string"
164 #define ATTR_NAME_LONG		"long_number"
165 #define ATTR_NAME_DATA		"data"
166 #endif
167 
168 /* LICENSE
169 /* .ad
170 /* .fi
171 /*	The Secure Mailer license must be distributed with this software.
172 /* AUTHOR(S)
173 /*	Wietse Venema
174 /*	IBM T.J. Watson Research
175 /*	P.O. Box 704
176 /*	Yorktown Heights, NY 10598, USA
177 /*
178 /*	Wietse Venema
179 /*	Google, Inc.
180 /*	111 8th Avenue
181 /*	New York, NY 10011, USA
182 /*--*/
183 
184 #endif
185