2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * include/k5-buf.h
2N/A *
2N/A * Copyright 2008 Massachusetts Institute of Technology.
2N/A * All Rights Reserved.
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of M.I.T. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A * M.I.T. makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A *
2N/A *
2N/A * k5buf string buffer module interface
2N/A */
2N/A
2N/A#ifndef K5_BUF_H
2N/A#define K5_BUF_H
2N/A
2N/A#if defined(_MSDOS) || defined(_WIN32)
2N/A#include <win-mac.h>
2N/A#endif
2N/A#ifndef KRB5_CALLCONV
2N/A#define KRB5_CALLCONV
2N/A#define KRB5_CALLCONV_C
2N/A#endif
2N/A
2N/A#include <stdarg.h>
2N/A#include <string.h>
2N/A#ifndef _WIN32
2N/A#include <unistd.h>
2N/A#endif
2N/A
2N/A/* The k5buf module is intended to allow multi-step string
2N/A construction in a fixed or dynamic buffer without the need to check
2N/A for a failure at each step (and without aborting on malloc
2N/A failure). If an allocation failure occurs or if the fixed buffer
2N/A runs out of room, the error will be discovered when the caller
2N/A retrieves the C string value or checks the length of the resulting
2N/A buffer.
2N/A
2N/A k5buf structures are stack-allocated, but are intended to be
2N/A opaque, so do not access the fields directly. This is a tool, not
2N/A a way of life, so do not put k5buf structure pointers into the
2N/A public API or into significant internal APIs. */
2N/A
2N/A/* We must define the k5buf structure here to allow stack allocation.
2N/A The structure is intended to be opaque, so the fields have funny
2N/A names. */
2N/Astruct k5buf {
2N/A int xx_buftype;
2N/A char *xx_data;
2N/A size_t xx_space;
2N/A size_t xx_len;
2N/A};
2N/A
2N/A/* Initialize a k5buf using a fixed-sized, existing buffer. SPACE
2N/A must be more than zero, or an assertion failure will result. */
2N/Avoid krb5int_buf_init_fixed(struct k5buf *buf, char *data, size_t space);
2N/A
2N/A/* Initialize a k5buf using an internally allocated dynamic buffer.
2N/A The buffer contents must be freed with krb5int_free_buf. */
2N/Avoid krb5int_buf_init_dynamic(struct k5buf *buf);
2N/A
2N/A/* Add a C string to BUF. */
2N/Avoid krb5int_buf_add(struct k5buf *buf, const char *data);
2N/A
2N/A/* Add a counted set of bytes to BUF. If is okay for DATA[0..LEN-1]
2N/A to contain null bytes if you are prepared to deal with that in the
2N/A output (use krb5int_buf_len to retrieve the length of the output). */
2N/Avoid krb5int_buf_add_len(struct k5buf *buf, const char *data, size_t len);
2N/A
2N/A/* Add sprintf-style formatted data to BUF. */
2N/Avoid krb5int_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
2N/A#if !defined(__cplusplus) && (__GNUC__ > 2)
2N/A __attribute__((__format__(__printf__, 2, 3)))
2N/A#endif
2N/A ;
2N/A
2N/A/* Truncate BUF. LEN must be between 0 and the existing buffer
2N/A length, or an assertion failure will result. */
2N/Avoid krb5int_buf_truncate(struct k5buf *buf, size_t len);
2N/A
2N/A/* Retrieve the byte array value of BUF, or NULL if there has been an
2N/A allocation failure or the fixed buffer ran out of room.
2N/A
2N/A The byte array will be a C string unless binary data was added with
2N/A krb5int_buf_add_len; it will be null-terminated regardless.
2N/A Modifying the byte array does not invalidate the buffer, as long as
2N/A its length is not changed.
2N/A
2N/A For a fixed buffer, the return value will always be equal to the
2N/A passed-in value of DATA at initialization time if it is not NULL.
2N/A
2N/A For a dynamic buffer, any buffer modification operation except
2N/A krb5int_buf_truncate may invalidate the byte array address. */
2N/Achar *krb5int_buf_data(struct k5buf *buf);
2N/A
2N/A/* Retrieve the length of BUF, or -1 if there has been an allocation
2N/A failure or the fixed buffer ran out of room. The length is equal
2N/A to strlen(krb5int_buf_data(buf)) unless binary data was added with
2N/A krb5int_buf_add_len. */
2N/Assize_t krb5int_buf_len(struct k5buf *buf);
2N/A
2N/A/* Free the storage used in the dynamic buffer BUF. The caller may
2N/A choose to take responsibility for freeing the return value of
2N/A krb5int_buf_data instead of using this function. If BUF is a fixed
2N/A buffer, an assertion failure will result. It is unnecessary
2N/A (though harmless) to free a buffer after an error is detected; the
2N/A storage will already have been freed in that case. */
2N/Avoid krb5int_free_buf(struct k5buf *buf);
2N/A
2N/A#endif /* K5_BUF_H */