/*
 * Copyright (c) 2006, Andreas Stricker <andy@knitter.ch>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright 
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above 
 *     copyright notice, this list of conditions and the following 
 *     disclaimer in the documentation and/or other materials provided 
 *     with the distribution.
 *   * The names of its contributors must not be used to endorse or 
 *     promote products derived from this software without specific 
 *     prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Original source is located at: 
 *   http://www.knitter.ch/src/snipplets/parse_buffer/
 *
 * The function is heavly inspired by the Python struct Module and its
 * Python/C API counterpart PyArg_ParseTuple. (http://www.python.org/)
 */

#ifndef PARSEBUFFER_H_KNITTER_CH_2006
#define PARSEBUFFER_H_KNITTER_CH_2006

#include <stdarg.h>
#include <stdint.h>
#include <sys/types.h>

/**
 * @brief Binary buffer parsing function based on format string.
 *
 * Parses the binary buffer @a buffer with @a size into variable argument 
 * list.
 * 
 * Each variable argument is expected to be a pointer to a valid storage 
 * place. This function never allocates memory.
 *
 * The variables arguments are usually only output variables except in one
 * special case: if a string with variable length is parsed, the size
 * argument is expected to contain the length of the string storage. After
 * parsing the variable contains the real length of the parsed string.
 *
 * Format syntax for @a fmt argument:
 * @arg x padding byte (with no correspondig argument)
 * @arg c character (char)
 * @arg b signed byte (int8_t)
 * @arg B unsigned byte (uint8_t)
 * @arg h signed short integer (int16_t)
 * @arg H unsigned short (uint16_t)
 * @arg i signed integer (int32_t)
 * @arg I unsigned integer (uint32_t)
 * @arg l signed long integer (int32_t) 
 * @arg L unsigned long integer (uint32_t)
 * @arg wN buffer of size N where N is a decimal number that fit into a 
 *         size_t. The function expects as first argument a pointer to buffer 
 *         and as second a pointer to a size_t variable to store the buffer 
 *         length.
 * @arg s[BHI] a variable buffer with a preceding length. s must be followed
 *             either by B, H or I that indicate the length of the size field.
 *
 * @a fmt may start with a "<" character that indicate that the string is
 *        stored in little-endian format. Support of big-Endian and network
 *        may be added in the future.
 *
 * @param buffer Pointer to an array of character with @size.
 * @param size Size of the @a buffer.
 * @param fmt Format string with the syntax described above.
 * @param ... Variable list of argument that must match the format as 
 *            specified in @a fmt.
 */
int parse_buffer(uint8_t *buffer, size_t size, const char* fmt, ...);

#endif /* PARSEBUFFER_H_KNITTER_CH_2006 */

