7 /* send attributes over byte stream
11 /* int attr_print0(fp, flags, type, name, ..., ATTR_TYPE_END)
17 /* int attr_vprint0(fp, flags, ap)
22 /* attr_print0() takes zero or more (name, value) simple attributes
23 /* and converts its input to a byte stream that can be recovered with
24 /* attr_scan0(). The stream is not flushed.
26 /* attr_vprint0() provides an alternate interface that is convenient
27 /* for calling from within variadic functions.
29 /* Attributes are sent in the requested order as specified with the
30 /* attr_print0() argument list. This routine satisfies the formatting
31 /* rules as outlined in attr_scan0(3).
35 /* Stream to write the result to.
37 /* The bit-wise OR of zero or more of the following.
40 /* After sending the requested attributes, leave the output stream in
41 /* a state that is usable for more attribute sending operations on
42 /* the same output attribute list.
43 /* By default, attr_print0() automatically appends an attribute list
44 /* terminator when it has sent the last requested attribute.
47 /* The type determines the arguments that follow.
49 /* .IP "ATTR_TYPE_INT (char *, int)"
50 /* This argument is followed by an attribute name and an integer.
51 /* .IP "ATTR_TYPE_LONG (char *, long)"
52 /* This argument is followed by an attribute name and a long integer.
53 /* .IP "ATTR_TYPE_STR (char *, char *)"
54 /* This argument is followed by an attribute name and a null-terminated
56 /* .IP "ATTR_TYPE_DATA (char *, ssize_t, char *)"
57 /* This argument is followed by an attribute name, an attribute value
58 /* length, and an attribute value pointer.
59 /* .IP "ATTR_TYPE_FUNC (ATTR_PRINT_SLAVE_FN, void *)"
60 /* This argument is followed by a function pointer and generic data
61 /* pointer. The caller-specified function returns whatever the
62 /* specified attribute printing function returns.
63 /* .IP "ATTR_TYPE_HASH (HTABLE *)"
64 /* .IP "ATTR_TYPE_NAMEVAL (NVTABLE *)"
65 /* The content of the table is sent as a sequence of string-valued
66 /* attributes with names equal to the table lookup keys.
68 /* This terminates the attribute list.
71 /* The result value is 0 in case of success, VSTREAM_EOF in case
74 /* Panic: interface violation. All system call errors are fatal.
76 /* attr_scan0(3) recover attributes from byte stream
80 /* The Secure Mailer license must be distributed with this software.
83 /* IBM T.J. Watson Research
85 /* Yorktown Heights, NY 10598, USA
94 /* Utility library. */
101 #include <base64_code.h>
103 #define STR(x) vstring_str(x)
104 #define LEN(x) VSTRING_LEN(x)
106 /* attr_vprint0 - send attribute list to stream */
108 int attr_vprint0(VSTREAM
*fp
, int flags
, va_list ap
)
110 const char *myname
= "attr_print0";
114 unsigned long long_val
;
116 HTABLE_INFO
**ht_info_list
;
119 static VSTRING
*base64_buf
;
120 ATTR_PRINT_SLAVE_FN print_fn
;
126 if (flags
& ~ATTR_FLAG_ALL
)
127 msg_panic("%s: bad flags: 0x%x", myname
, flags
);
130 * Iterate over all (type, name, value) triples, and produce output on
133 while ((attr_type
= va_arg(ap
, int)) != ATTR_TYPE_END
) {
136 attr_name
= va_arg(ap
, char *);
137 vstream_fwrite(fp
, attr_name
, strlen(attr_name
) + 1);
138 int_val
= va_arg(ap
, int);
139 vstream_fprintf(fp
, "%u", (unsigned) int_val
);
140 VSTREAM_PUTC('\0', fp
);
142 msg_info("send attr %s = %u", attr_name
, int_val
);
145 attr_name
= va_arg(ap
, char *);
146 vstream_fwrite(fp
, attr_name
, strlen(attr_name
) + 1);
147 long_val
= va_arg(ap
, unsigned long);
148 vstream_fprintf(fp
, "%lu", (unsigned long) long_val
);
149 VSTREAM_PUTC('\0', fp
);
151 msg_info("send attr %s = %lu", attr_name
, long_val
);
154 attr_name
= va_arg(ap
, char *);
155 vstream_fwrite(fp
, attr_name
, strlen(attr_name
) + 1);
156 str_val
= va_arg(ap
, char *);
157 vstream_fwrite(fp
, str_val
, strlen(str_val
) + 1);
159 msg_info("send attr %s = %s", attr_name
, str_val
);
162 attr_name
= va_arg(ap
, char *);
163 vstream_fwrite(fp
, attr_name
, strlen(attr_name
) + 1);
164 len_val
= va_arg(ap
, ssize_t
);
165 str_val
= va_arg(ap
, char *);
167 base64_buf
= vstring_alloc(10);
168 base64_encode(base64_buf
, str_val
, len_val
);
169 vstream_fwrite(fp
, STR(base64_buf
), LEN(base64_buf
) + 1);
171 msg_info("send attr %s = [data %ld bytes]",
172 attr_name
, (long) len_val
);
175 print_fn
= va_arg(ap
, ATTR_PRINT_SLAVE_FN
);
176 print_arg
= va_arg(ap
, void *);
177 print_fn(attr_print0
, fp
, flags
| ATTR_FLAG_MORE
, print_arg
);
180 ht_info_list
= htable_list(va_arg(ap
, HTABLE
*));
181 for (ht
= ht_info_list
; *ht
; ht
++) {
182 vstream_fwrite(fp
, ht
[0]->key
, strlen(ht
[0]->key
) + 1);
183 vstream_fwrite(fp
, ht
[0]->value
, strlen(ht
[0]->value
) + 1);
185 msg_info("send attr name %s value %s",
186 ht
[0]->key
, ht
[0]->value
);
188 myfree((char *) ht_info_list
);
191 msg_panic("%s: unknown type code: %d", myname
, attr_type
);
194 if ((flags
& ATTR_FLAG_MORE
) == 0)
195 VSTREAM_PUTC('\0', fp
);
196 return (vstream_ferror(fp
));
199 int attr_print0(VSTREAM
*fp
, int flags
,...)
205 ret
= attr_vprint0(fp
, flags
, ap
);
213 * Proof of concept test program. Mirror image of the attr_scan0 test
216 #include <msg_vstream.h>
218 int main(int unused_argc
, char **argv
)
220 HTABLE
*table
= htable_create(1);
222 msg_vstream_init(argv
[0], VSTREAM_ERR
);
224 htable_enter(table
, "foo-name", mystrdup("foo-value"));
225 htable_enter(table
, "bar-name", mystrdup("bar-value"));
226 attr_print0(VSTREAM_OUT
, ATTR_FLAG_NONE
,
227 ATTR_TYPE_INT
, ATTR_NAME_INT
, 4711,
228 ATTR_TYPE_LONG
, ATTR_NAME_LONG
, 1234,
229 ATTR_TYPE_STR
, ATTR_NAME_STR
, "whoopee",
230 ATTR_TYPE_DATA
, ATTR_NAME_DATA
, strlen("whoopee"), "whoopee",
231 ATTR_TYPE_HASH
, table
,
233 attr_print0(VSTREAM_OUT
, ATTR_FLAG_NONE
,
234 ATTR_TYPE_INT
, ATTR_NAME_INT
, 4711,
235 ATTR_TYPE_LONG
, ATTR_NAME_LONG
, 1234,
236 ATTR_TYPE_STR
, ATTR_NAME_STR
, "whoopee",
237 ATTR_TYPE_DATA
, ATTR_NAME_DATA
, strlen("whoopee"), "whoopee",
239 if (vstream_fflush(VSTREAM_OUT
) != 0)
240 msg_fatal("write error: %m");
242 htable_free(table
, myfree
);