2 * Copyright (c) 2000-2002, 2004, 2005 Proofpoint, Inc. and its suppliers.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * By using this file, you agree to the terms and conditions set
11 * forth in the LICENSE file which can be found at the top level of
12 * the sendmail distribution.
16 SM_IDSTR(id, "@(#)$Id: strio.c,v 1.45 2013-11-22 20:51:43 ca Exp $")
28 static int sm_strsetmode __P((SM_FILE_T*, const int *));
29 static int sm_strgetmode __P((SM_FILE_T*, int *));
32 ** Cookie structure for the "strio" file type
42 const void *strio_rpool;
45 typedef struct sm_str_obj SM_STR_OBJ_T;
48 ** SM_STRGROW -- increase storage space for string
51 ** s -- current cookie
52 ** size -- new storage size request
55 ** true iff successful.
58 static bool sm_strgrow __P((SM_STR_OBJ_T *, size_t));
67 if (s->strio_size >= size)
69 p = sm_realloc(s->strio_base, size);
73 s->strio_end = s->strio_base + size;
79 ** SM_STRREAD -- read a portion of the string
82 ** fp -- the file pointer
83 ** buf -- location to place read data
84 ** n -- number of bytes to read
87 ** Failure: -1 and sets errno
88 ** Success: >=0, number of bytes read
92 sm_strread(fp, buf, n)
97 register SM_STR_OBJ_T *s = fp->f_cookie;
100 if (!(s->strio_flags & SMRD) && !(s->strio_flags & SMRW))
105 len = SM_MIN(s->strio_size - s->strio_offset, n);
106 (void) memmove(buf, s->strio_base + s->strio_offset, len);
107 s->strio_offset += len;
112 ** SM_STRWRITE -- write a portion of the string
115 ** fp -- the file pointer
116 ** buf -- location of data for writing
117 ** n -- number of bytes to write
120 ** Failure: -1 and sets errno
121 ** Success: >=0, number of bytes written
125 sm_strwrite(fp, buf, n)
130 register SM_STR_OBJ_T *s = fp->f_cookie;
132 if (!(s->strio_flags & SMWR) && !(s->strio_flags & SMRW))
137 if (n + s->strio_offset > s->strio_size)
139 if (!sm_strgrow(s, n + s->strio_offset))
142 (void) memmove(s->strio_base + s->strio_offset, buf, n);
143 s->strio_offset += n;
148 ** SM_STRSEEK -- position the offset pointer for the string
150 ** Only SM_IO_SEEK_SET, SM_IO_SEEK_CUR and SM_IO_SEEK_END are valid
151 ** values for whence.
154 ** fp -- the file pointer
155 ** offset -- number of bytes offset from "base"
156 ** whence -- determines "base" for 'offset'
159 ** Failure: -1 and sets errno
160 ** Success: >=0, number of bytes read
164 sm_strseek(fp, offset, whence)
170 register SM_STR_OBJ_T *s = fp->f_cookie;
179 ret = s->strio_offset + offset;
188 if (ret < 0 || ret > (off_t)(size_t)(-1)) /* XXX ugly */
190 if ((size_t) ret > s->strio_size)
192 if (sm_strgrow(s, (size_t)ret))
195 /* errno set by sm_strgrow */
198 s->strio_offset = (size_t) ret;
203 ** SM_STROPEN -- open a string file type
206 ** fp -- file pointer open to be associated with
207 ** info -- initial contents (NULL for none)
208 ** flags -- flags for methods of access (was mode)
209 ** rpool -- resource pool to use memory from (if applicable)
213 ** Failure: -1 and sets errno
217 sm_stropen(fp, info, flags, rpool)
223 register SM_STR_OBJ_T *s;
226 s = sm_rpool_malloc_x(rpool, sizeof(SM_STR_OBJ_T));
228 s = sm_malloc(sizeof(SM_STR_OBJ_T));
231 #endif /* SM_RPOOL */
234 s->strio_rpool = rpool;
237 s->strio_base = NULL;
243 s->strio_flags = SMRW;
246 s->strio_flags = SMRD;
249 s->strio_flags = SMWR;
252 if (s->strio_rpool == NULL)
257 if (s->strio_rpool == NULL)
265 s->strio_base = sm_strdup_x(info);
266 if (s->strio_base == NULL)
268 int save_errno = errno;
270 if (s->strio_rpool == NULL)
275 s->strio_size = strlen(info);
276 s->strio_end = s->strio_base + s->strio_size;
282 ** SM_STRCLOSE -- close the string file type and free resources
285 ** fp -- file pointer
295 SM_STR_OBJ_T *s = fp->f_cookie;
298 sm_free(s->strio_base);
299 s->strio_base = NULL;
300 #endif /* !SM_RPOOL */
305 ** SM_STRSETMODE -- set mode info for the file
307 ** Note: changing the mode can be a safe way to have the "parent"
308 ** set up a string that the "child" is not to modify
311 ** fp -- the file pointer
312 ** mode -- location of new mode to set
316 ** Failure: -1 and sets errno
320 sm_strsetmode(fp, mode)
324 register SM_STR_OBJ_T *s = fp->f_cookie;
345 s->strio_flags &= ~SMMODEMASK;
346 s->strio_flags |= flags;
351 ** SM_STRGETMODE -- get mode info for the file
354 ** fp -- the file pointer
355 ** mode -- location to store current mode
359 ** Failure: -1 and sets errno
363 sm_strgetmode(fp, mode)
367 register SM_STR_OBJ_T *s = fp->f_cookie;
369 switch (s->strio_flags & SMMODEMASK)
375 *mode = SM_IO_RDONLY;
378 *mode = SM_IO_WRONLY;
388 ** SM_STRSETINFO -- set info for the file
390 ** Currently only SM_IO_WHAT_MODE is supported for 'what'.
393 ** fp -- the file pointer
394 ** what -- type of information to set
395 ** valp -- location to data for doing set
398 ** Failure: -1 and sets errno
399 ** Success: sm_strsetmode() return [0 (zero)]
403 sm_strsetinfo(fp, what, valp)
410 case SM_IO_WHAT_MODE:
411 return sm_strsetmode(fp, (int *) valp);
419 ** SM_STRGETINFO -- get info for the file
421 ** Currently only SM_IO_WHAT_MODE is supported for 'what'.
424 ** fp -- the file pointer
425 ** what -- type of information requested
426 ** valp -- location to return information in
429 ** Failure: -1 and sets errno
430 ** Success: sm_strgetmode() return [0 (zero)]
434 sm_strgetinfo(fp, what, valp)
441 case SM_IO_WHAT_MODE:
442 return sm_strgetmode(fp, (int *) valp);
450 ** SM_STRIO_INIT -- initializes a write-only string type
452 ** Original comments below. This function does not appear to be used anywhere.
453 ** The same functionality can be done by changing the mode of the file.
455 ** sm_strio_init initializes an SM_FILE_T structure as a write-only file
456 ** that writes into the specified buffer:
457 ** - Use sm_io_putc, sm_io_fprintf, etc, to write into the buffer.
458 ** Attempts to write more than size-1 characters into the buffer will fail
459 ** silently (no error is reported).
460 ** - Use sm_io_fflush to nul terminate the string in the buffer
461 ** (the write pointer is not advanced).
462 ** No memory is allocated either by sm_strio_init or by sm_io_{putc,write} etc.
465 ** fp -- file pointer
466 ** buf -- memory location for stored data
467 ** size -- size of 'buf'
474 sm_strio_init(fp, buf, size)
479 fp->sm_magic = SmFileMagic;
480 fp->f_flags = SMWR | SMSTR;
482 fp->f_bf.smb_base = fp->f_p = (unsigned char *) buf;
483 fp->f_bf.smb_size = fp->f_w = (size ? size - 1 : 0);
488 fp->f_getinfo = NULL;
489 fp->f_setinfo = NULL;