7 /* simple typed record I/O
11 /* int rec_get(stream, buf, maxsize)
16 /* int rec_get_raw(stream, buf, maxsize, flags)
22 /* int rec_put(stream, type, data, len)
27 /* AUXILIARY FUNCTIONS
28 /* int rec_put_type(stream, type, offset)
33 /* int rec_fprintf(stream, type, format, ...)
36 /* const char *format;
38 /* int rec_fputs(stream, type, str)
43 /* int REC_PUT_BUF(stream, type, buf)
48 /* int rec_vfprintf(stream, type, format, ap)
51 /* const char *format;
54 /* int rec_goto(stream, where)
58 /* int rec_pad(stream, type, len)
63 /* REC_SPACE_NEED(buflen, reclen)
67 /* This module reads and writes typed variable-length records.
68 /* Each record contains a 1-byte type code (0..255), a length
69 /* (1 or more bytes) and as much data as the length specifies.
71 /* rec_get_raw() retrieves a record from the named record stream
72 /* and returns the record type. The \fImaxsize\fR argument is
73 /* zero, or specifies a maximal acceptable record length.
74 /* The result is REC_TYPE_EOF when the end of the file was reached,
75 /* and REC_TYPE_ERROR in case of a bad record. The result buffer is
76 /* null-terminated for convenience. Records may contain embedded
77 /* null characters. The \fIflags\fR argument specifies zero or
78 /* more of the following:
79 /* .IP REC_FLAG_FOLLOW_PTR
80 /* Follow PTR records, instead of exposing them to the application.
81 /* .IP REC_FLAG_SKIP_DTXT
82 /* Skip "deleted text" records, instead of exposing them to
84 /* .IP REC_FLAG_SEEK_END
85 /* Seek to the end-of-file upon reading a REC_TYPE_END record.
87 /* Specify REC_FLAG_NONE to request no special processing,
88 /* and REC_FLAG_DEFAULT for normal use.
90 /* rec_get() is a wrapper around rec_get_raw() that always
91 /* enables the REC_FLAG_FOLLOW_PTR, REC_FLAG_SKIP_DTXT
92 /* and REC_FLAG_SEEK_END features.
94 /* rec_put() stores the specified record and returns the record
95 /* type, or REC_TYPE_ERROR in case of problems.
97 /* rec_put_type() updates the type field of the record at the
98 /* specified file offset. The result is the new record type,
99 /* or REC_TYPE_ERROR in case of trouble.
101 /* rec_fprintf() and rec_vfprintf() format their arguments and
102 /* write the result to the named stream. The result is the same
103 /* as with rec_put().
105 /* rec_fputs() writes a record with as contents a copy of the
106 /* specified string. The result is the same as with rec_put().
108 /* REC_PUT_BUF() is a wrapper for rec_put() that makes it
109 /* easier to handle VSTRING buffers. It is an unsafe macro
110 /* that evaluates some arguments more than once.
112 /* rec_goto() takes the argument of a pointer record and moves
113 /* the file pointer to the specified location. A zero position
114 /* means do nothing. The result is REC_TYPE_ERROR in case of
117 /* rec_pad() writes a record that occupies the larger of (the
118 /* specified amount) or (an implementation-defined minimum).
120 /* REC_SPACE_NEED(buflen, reclen) converts the specified buffer
121 /* length into a record length. This macro modifies its second
124 /* Panics: interface violations. Fatal errors: insufficient memory.
125 /* Warnings: corrupted file.
129 /* The Secure Mailer license must be distributed with this software.
132 /* IBM T.J. Watson Research
134 /* Yorktown Heights, NY 10598, USA
137 /* System library. */
139 #include <sys_defs.h>
140 #include <stdlib.h> /* 44BSD stdarg.h uses abort() */
146 #define NBBY 8 /* XXX should be in sys_defs.h */
149 /* Utility library. */
152 #include <mymalloc.h>
155 #include <stringops.h>
157 /* Global library. */
160 #include <rec_type.h>
163 /* rec_put_type - update record type field */
165 int rec_put_type(VSTREAM
*stream
, int type
, off_t offset
)
167 if (type
< 0 || type
> 255)
168 msg_panic("rec_put_type: bad record type %d", type
);
171 msg_info("rec_put_type: %d at %ld", type
, (long) offset
);
173 if (vstream_fseek(stream
, offset
, SEEK_SET
) < 0
174 || VSTREAM_PUTC(type
, stream
) != type
) {
175 return (REC_TYPE_ERROR
);
181 /* rec_put - store typed record */
183 int rec_put(VSTREAM
*stream
, int type
, const char *data
, ssize_t len
)
188 if (type
< 0 || type
> 255)
189 msg_panic("rec_put: bad record type %d", type
);
192 msg_info("rec_put: type %c len %ld data %.10s",
193 type
, (long) len
, data
);
196 * Write the record type, one byte.
198 if (VSTREAM_PUTC(type
, stream
) == VSTREAM_EOF
)
199 return (REC_TYPE_ERROR
);
202 * Write the record data length in 7-bit portions, using the 8th bit to
203 * indicate that there is more. Use as many length bytes as needed.
207 len_byte
= len_rest
& 0177;
210 if (VSTREAM_PUTC(len_byte
, stream
) == VSTREAM_EOF
) {
211 return (REC_TYPE_ERROR
);
213 } while (len_rest
!= 0);
216 * Write the record data portion. Use as many length bytes as needed.
218 if (len
&& vstream_fwrite(stream
, data
, len
) != len
)
219 return (REC_TYPE_ERROR
);
223 /* rec_get_raw - retrieve typed record */
225 int rec_get_raw(VSTREAM
*stream
, VSTRING
*buf
, ssize_t maxsize
, int flags
)
227 const char *myname
= "rec_get";
237 msg_panic("%s: bad record size limit: %ld", myname
, (long) maxsize
);
242 * Extract the record type.
244 if ((type
= VSTREAM_GETC(stream
)) == VSTREAM_EOF
)
245 return (REC_TYPE_EOF
);
248 * Find out the record data length. Return an error result when the
249 * record data length is malformed or when it exceeds the acceptable
252 for (len
= 0, shift
= 0; /* void */ ; shift
+= 7) {
253 if (shift
>= (int) (NBBY
* sizeof(int))) {
254 msg_warn("%s: too many length bits, record type %d",
255 VSTREAM_PATH(stream
), type
);
256 return (REC_TYPE_ERROR
);
258 if ((len_byte
= VSTREAM_GETC(stream
)) == VSTREAM_EOF
) {
259 msg_warn("%s: unexpected EOF reading length, record type %d",
260 VSTREAM_PATH(stream
), type
);
261 return (REC_TYPE_ERROR
);
263 len
|= (len_byte
& 0177) << shift
;
264 if ((len_byte
& 0200) == 0)
267 if (len
< 0 || (maxsize
> 0 && len
> maxsize
)) {
268 msg_warn("%s: illegal length %ld, record type %d",
269 VSTREAM_PATH(stream
), (long) len
, type
);
270 while (len
-- > 0 && VSTREAM_GETC(stream
) != VSTREAM_EOF
)
272 return (REC_TYPE_ERROR
);
276 * Reserve buffer space for the result, and read the record data into
280 VSTRING_SPACE(buf
, len
);
281 if (vstream_fread(stream
, vstring_str(buf
), len
) != len
) {
282 msg_warn("%s: unexpected EOF in data, record type %d length %ld",
283 VSTREAM_PATH(stream
), type
, (long) len
);
284 return (REC_TYPE_ERROR
);
286 VSTRING_AT_OFFSET(buf
, len
);
287 VSTRING_TERMINATE(buf
);
289 msg_info("%s: type %c len %ld data %.10s", myname
,
290 type
, (long) len
, vstring_str(buf
));
293 * Transparency options.
297 if (type
== REC_TYPE_PTR
&& (flags
& REC_FLAG_FOLLOW_PTR
) != 0
298 && (type
= rec_goto(stream
, vstring_str(buf
))) != REC_TYPE_ERROR
)
300 if (type
== REC_TYPE_DTXT
&& (flags
& REC_FLAG_SKIP_DTXT
) != 0)
302 if (type
== REC_TYPE_END
&& (flags
& REC_FLAG_SEEK_END
) != 0)
303 (void) vstream_fseek(stream
, (off_t
) 0, SEEK_END
);
309 /* rec_goto - follow PTR record */
311 int rec_goto(VSTREAM
*stream
, const char *buf
)
314 static const char *saved_path
;
315 static off_t saved_offset
;
316 static int reverse_count
;
319 * Crude workaround for queue file loops. VSTREAMs currently have no
320 * option to attach application-specific data, so we use global state and
321 * simple logic to detect if an application switches streams. We trigger
322 * on reverse jumps only. There's one reverse jump for every inserted
323 * header, but only one reverse jump for all appended recipients. No-one
324 * is likely to insert 10000 message headers, but someone might append
327 #define STREQ(x,y) ((x) == (y) && strcmp((x), (y)) == 0)
328 #define REVERSE_JUMP_LIMIT 10000
330 if (!STREQ(saved_path
, VSTREAM_PATH(stream
))) {
331 saved_path
= VSTREAM_PATH(stream
);
335 while (ISSPACE(*buf
))
337 if ((offset
= off_cvt_string(buf
)) < 0) {
338 msg_warn("%s: malformed pointer record value: %s",
339 VSTREAM_PATH(stream
), buf
);
340 return (REC_TYPE_ERROR
);
341 } else if (offset
== 0) {
344 } else if (offset
<= saved_offset
&& ++reverse_count
> REVERSE_JUMP_LIMIT
) {
345 msg_warn("%s: too many reverse jump records", VSTREAM_PATH(stream
));
346 return (REC_TYPE_ERROR
);
347 } else if (vstream_fseek(stream
, offset
, SEEK_SET
) < 0) {
348 msg_warn("%s: seek error after pointer record: %m",
349 VSTREAM_PATH(stream
));
350 return (REC_TYPE_ERROR
);
352 saved_offset
= offset
;
357 /* rec_vfprintf - write formatted string to record */
359 int rec_vfprintf(VSTREAM
*stream
, int type
, const char *format
, va_list ap
)
364 vp
= vstring_alloc(100);
367 * Writing a formatted string involves an extra copy, because we must
368 * know the record length before we can write it.
370 vstring_vsprintf(vp
, format
, ap
);
371 return (REC_PUT_BUF(stream
, type
, vp
));
374 /* rec_fprintf - write formatted string to record */
376 int rec_fprintf(VSTREAM
*stream
, int type
, const char *format
,...)
381 va_start(ap
, format
);
382 result
= rec_vfprintf(stream
, type
, format
, ap
);
387 /* rec_fputs - write string to record */
389 int rec_fputs(VSTREAM
*stream
, int type
, const char *str
)
391 return (rec_put(stream
, type
, str
, str
? strlen(str
) : 0));
394 /* rec_pad - write padding record */
396 int rec_pad(VSTREAM
*stream
, int type
, int len
)
398 int width
= len
- 2; /* type + length */
400 return (rec_fprintf(stream
, type
, "%*s",
401 width
< 1 ? 1 : width
, "0"));