Blame src/microhttpd/mhd_str.h

Packit 875988
/*
Packit 875988
  This file is part of libmicrohttpd
Packit 875988
  Copyright (C) 2015, 2016 Karlson2k (Evgeny Grin)
Packit 875988
Packit 875988
  This library is free software; you can redistribute it and/or
Packit 875988
  modify it under the terms of the GNU Lesser General Public
Packit 875988
  License as published by the Free Software Foundation; either
Packit 875988
  version 2.1 of the License, or (at your option) any later version.
Packit 875988
Packit 875988
  This library is distributed in the hope that it will be useful,
Packit 875988
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 875988
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 875988
  Lesser General Public License for more details.
Packit 875988
Packit 875988
  You should have received a copy of the GNU Lesser General Public
Packit 875988
  License along with this library; if not, write to the Free Software
Packit 875988
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 875988
*/
Packit 875988
Packit 875988
/**
Packit 875988
 * @file microhttpd/mhd_str.h
Packit 875988
 * @brief  Header for string manipulating helpers
Packit 875988
 * @author Karlson2k (Evgeny Grin)
Packit 875988
 */
Packit 875988
Packit 875988
#ifndef MHD_STR_H
Packit 875988
#define MHD_STR_H 1
Packit 875988
Packit 875988
#include "mhd_options.h"
Packit 875988
Packit 875988
#include <stdint.h>
Packit 875988
#include <stdlib.h>
Packit 875988
#ifdef HAVE_STDBOOL_H
Packit 875988
#include <stdbool.h>
Packit 875988
#endif /* HAVE_STDBOOL_H */
Packit 875988
Packit 875988
#ifdef MHD_FAVOR_SMALL_CODE
Packit 875988
#include "mhd_limits.h"
Packit 875988
#endif /* MHD_FAVOR_SMALL_CODE */
Packit 875988
Packit 875988
#ifndef MHD_STATICSTR_LEN_
Packit 875988
/**
Packit 875988
 * Determine length of static string / macro strings at compile time.
Packit 875988
 */
Packit 875988
#define MHD_STATICSTR_LEN_(macro) (sizeof(macro)/sizeof(char) - 1)
Packit 875988
#endif /* ! MHD_STATICSTR_LEN_ */
Packit 875988
Packit 875988
/*
Packit 875988
 * Block of functions/macros that use US-ASCII charset as required by HTTP
Packit 875988
 * standards. Not affected by current locale settings.
Packit 875988
 */
Packit 875988
Packit 875988
#ifndef MHD_FAVOR_SMALL_CODE
Packit 875988
/**
Packit 875988
 * Check two string for equality, ignoring case of US-ASCII letters.
Packit 875988
 * @param str1 first string to compare
Packit 875988
 * @param str2 second string to compare
Packit 875988
 * @return non-zero if two strings are equal, zero otherwise.
Packit 875988
 */
Packit 875988
int
Packit 875988
MHD_str_equal_caseless_ (const char * str1,
Packit 875988
                 const char * str2);
Packit 875988
#else  /* MHD_FAVOR_SMALL_CODE */
Packit 875988
/* Reuse MHD_str_equal_caseless_n_() to reduce size */
Packit 875988
#define MHD_str_equal_caseless_(s1,s2) MHD_str_equal_caseless_n_((s1),(s2), SIZE_MAX)
Packit 875988
#endif /* MHD_FAVOR_SMALL_CODE */
Packit 875988
Packit 875988
Packit 875988
/**
Packit 875988
 * Check two string for equality, ignoring case of US-ASCII letters and
Packit 875988
 * checking not more than @a maxlen characters.
Packit 875988
 * Compares up to first terminating null character, but not more than
Packit 875988
 * first @a maxlen characters.
Packit 875988
 * @param str1 first string to compare
Packit 875988
 * @param str2 second string to compare
Packit 875988
 * @param maxlen maximum number of characters to compare
Packit 875988
 * @return non-zero if two strings are equal, zero otherwise.
Packit 875988
 */
Packit 875988
int
Packit 875988
MHD_str_equal_caseless_n_ (const char * const str1,
Packit 875988
                  const char * const str2,
Packit 875988
                  size_t maxlen);
Packit 875988
Packit 875988
Packit 875988
/**
Packit 875988
 * Check whether @a str has case-insensitive @a token.
Packit 875988
 * Token could be surrounded by spaces and tabs and delimited by comma.
Packit 875988
 * Match succeed if substring between start, end of string or comma
Packit 875988
 * contains only case-insensitive token and optional spaces and tabs.
Packit 875988
 * @warning token must not contain null-charters except optional
Packit 875988
 *          terminating null-character.
Packit 875988
 * @param str the string to check
Packit 875988
 * @param token the token to find
Packit 875988
 * @param token_len length of token, not including optional terminating
Packit 875988
 *                  null-character.
Packit 875988
 * @return non-zero if two strings are equal, zero otherwise.
Packit 875988
 */
Packit 875988
bool
Packit 875988
MHD_str_has_token_caseless_ (const char * str,
Packit 875988
                             const char * const token,
Packit 875988
                             size_t token_len);
Packit 875988
Packit 875988
/**
Packit 875988
 * Check whether @a str has case-insensitive static @a tkn.
Packit 875988
 * Token could be surrounded by spaces and tabs and delimited by comma.
Packit 875988
 * Match succeed if substring between start, end of string or comma
Packit 875988
 * contains only case-insensitive token and optional spaces and tabs.
Packit 875988
 * @warning tkn must be static string
Packit 875988
 * @param str the string to check
Packit 875988
 * @param tkn the static string of token to find
Packit 875988
 * @return non-zero if two strings are equal, zero otherwise.
Packit 875988
 */
Packit 875988
#define MHD_str_has_s_token_caseless_(str,tkn) \
Packit 875988
    MHD_str_has_token_caseless_((str),(tkn),MHD_STATICSTR_LEN_(tkn))
Packit 875988
Packit 875988
#ifndef MHD_FAVOR_SMALL_CODE
Packit 875988
/* Use individual function for each case to improve speed */
Packit 875988
Packit 875988
/**
Packit 875988
 * Convert decimal US-ASCII digits in string to number in uint64_t.
Packit 875988
 * Conversion stopped at first non-digit character.
Packit 875988
 * @param str string to convert
Packit 875988
 * @param out_val pointer to uint64_t to store result of conversion
Packit 875988
 * @return non-zero number of characters processed on succeed,
Packit 875988
 *         zero if no digit is found, resulting value is larger
Packit 875988
 *         then possible to store in uint64_t or @a out_val is NULL
Packit 875988
 */
Packit 875988
size_t
Packit 875988
MHD_str_to_uint64_ (const char * str,
Packit 875988
                    uint64_t * out_val);
Packit 875988
Packit 875988
/**
Packit 875988
 * Convert not more then @a maxlen decimal US-ASCII digits in string to
Packit 875988
 * number in uint64_t.
Packit 875988
 * Conversion stopped at first non-digit character or after @a maxlen 
Packit 875988
 * digits.
Packit 875988
 * @param str string to convert
Packit 875988
 * @param maxlen maximum number of characters to process
Packit 875988
 * @param out_val pointer to uint64_t to store result of conversion
Packit 875988
 * @return non-zero number of characters processed on succeed,
Packit 875988
 *         zero if no digit is found, resulting value is larger
Packit 875988
 *         then possible to store in uint64_t or @a out_val is NULL
Packit 875988
 */
Packit 875988
size_t
Packit 875988
MHD_str_to_uint64_n_ (const char * str,
Packit 875988
                      size_t maxlen,
Packit 875988
                      uint64_t * out_val);
Packit 875988
Packit 875988
Packit 875988
/**
Packit 875988
 * Convert hexadecimal US-ASCII digits in string to number in uint32_t.
Packit 875988
 * Conversion stopped at first non-digit character.
Packit 875988
 * @param str string to convert
Packit 875988
 * @param out_val pointer to uint32_t to store result of conversion
Packit 875988
 * @return non-zero number of characters processed on succeed, 
Packit 875988
 *         zero if no digit is found, resulting value is larger
Packit 875988
 *         then possible to store in uint32_t or @a out_val is NULL
Packit 875988
 */
Packit 875988
size_t
Packit 875988
MHD_strx_to_uint32_ (const char * str,
Packit 875988
                     uint32_t * out_val);
Packit 875988
Packit 875988
Packit 875988
/**
Packit 875988
 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
Packit 875988
 * to number in uint32_t.
Packit 875988
 * Conversion stopped at first non-digit character or after @a maxlen 
Packit 875988
 * digits.
Packit 875988
 * @param str string to convert
Packit 875988
 * @param maxlen maximum number of characters to process
Packit 875988
 * @param out_val pointer to uint32_t to store result of conversion
Packit 875988
 * @return non-zero number of characters processed on succeed,
Packit 875988
 *         zero if no digit is found, resulting value is larger
Packit 875988
 *         then possible to store in uint32_t or @a out_val is NULL
Packit 875988
 */
Packit 875988
size_t
Packit 875988
MHD_strx_to_uint32_n_ (const char * str,
Packit 875988
                      size_t maxlen,
Packit 875988
                      uint32_t * out_val);
Packit 875988
Packit 875988
Packit 875988
/**
Packit 875988
 * Convert hexadecimal US-ASCII digits in string to number in uint64_t.
Packit 875988
 * Conversion stopped at first non-digit character.
Packit 875988
 * @param str string to convert
Packit 875988
 * @param out_val pointer to uint64_t to store result of conversion
Packit 875988
 * @return non-zero number of characters processed on succeed, 
Packit 875988
 *         zero if no digit is found, resulting value is larger
Packit 875988
 *         then possible to store in uint64_t or @a out_val is NULL
Packit 875988
 */
Packit 875988
size_t
Packit 875988
MHD_strx_to_uint64_ (const char * str,
Packit 875988
                     uint64_t * out_val);
Packit 875988
Packit 875988
Packit 875988
/**
Packit 875988
 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
Packit 875988
 * to number in uint64_t.
Packit 875988
 * Conversion stopped at first non-digit character or after @a maxlen 
Packit 875988
 * digits.
Packit 875988
 * @param str string to convert
Packit 875988
 * @param maxlen maximum number of characters to process
Packit 875988
 * @param out_val pointer to uint64_t to store result of conversion
Packit 875988
 * @return non-zero number of characters processed on succeed,
Packit 875988
 *         zero if no digit is found, resulting value is larger
Packit 875988
 *         then possible to store in uint64_t or @a out_val is NULL
Packit 875988
 */
Packit 875988
size_t
Packit 875988
MHD_strx_to_uint64_n_ (const char * str,
Packit 875988
                       size_t maxlen,
Packit 875988
                       uint64_t * out_val);
Packit 875988
Packit 875988
#else  /* MHD_FAVOR_SMALL_CODE */
Packit 875988
/* Use one universal function and macros to reduce size */
Packit 875988
Packit 875988
/**
Packit 875988
 * Generic function for converting not more then @a maxlen
Packit 875988
 * hexadecimal or decimal US-ASCII digits in string to number.
Packit 875988
 * Conversion stopped at first non-digit character or after @a maxlen 
Packit 875988
 * digits.
Packit 875988
 * To be used only within macro.
Packit 875988
 * @param str the string to convert
Packit 875988
 * @param maxlen the maximum number of characters to process
Packit 875988
 * @param out_val the pointer to uint64_t to store result of conversion
Packit 875988
 * @param val_size the size of variable pointed by @a out_val
Packit 875988
 * @param max_val the maximum decoded number
Packit 875988
 * @param base the numeric base, 10 or 16
Packit 875988
 * @return non-zero number of characters processed on succeed,
Packit 875988
 *         zero if no digit is found, resulting value is larger
Packit 875988
 *         then @ max_val or @a out_val is NULL
Packit 875988
 */
Packit 875988
size_t
Packit 875988
MHD_str_to_uvalue_n_ (const char * str,
Packit 875988
                      size_t maxlen,
Packit 875988
                      void * out_val,
Packit 875988
                      size_t val_size,
Packit 875988
                      uint64_t max_val,
Packit 875988
                      int base);
Packit 875988
Packit 875988
#define MHD_str_to_uint64_(s,ov) MHD_str_to_uvalue_n_((s),SIZE_MAX,(ov),\
Packit 875988
                                             sizeof(uint64_t),UINT64_MAX,10)
Packit 875988
Packit 875988
#define MHD_str_to_uint64_n_(s,ml,ov) MHD_str_to_uvalue_n_((s),(ml),(ov),\
Packit 875988
                                             sizeof(uint64_t),UINT64_MAX,10)
Packit 875988
Packit 875988
#define MHD_strx_to_sizet_(s,ov) MHD_str_to_uvalue_n_((s),SIZE_MAX,(ov),\
Packit 875988
                                             sizeof(size_t),SIZE_MAX,16)
Packit 875988
Packit 875988
#define MHD_strx_to_sizet_n_(s,ml,ov) MHD_str_to_uvalue_n_((s),(ml),(ov),\
Packit 875988
                                             sizeof(size_t),SIZE_MAX,16)
Packit 875988
Packit 875988
#define MHD_strx_to_uint32_(s,ov) MHD_str_to_uvalue_n_((s),SIZE_MAX,(ov),\
Packit 875988
                                             sizeof(uint32_t),UINT32_MAX,16)
Packit 875988
Packit 875988
#define MHD_strx_to_uint32_n_(s,ml,ov) MHD_str_to_uvalue_n_((s),(ml),(ov),\
Packit 875988
                                             sizeof(uint32_t),UINT32_MAX,16)
Packit 875988
Packit 875988
#define MHD_strx_to_uint64_(s,ov) MHD_str_to_uvalue_n_((s),SIZE_MAX,(ov),\
Packit 875988
                                             sizeof(uint64_t),UINT64_MAX,16)
Packit 875988
Packit 875988
#define MHD_strx_to_uint64_n_(s,ml,ov) MHD_str_to_uvalue_n_((s),(ml),(ov),\
Packit 875988
                                             sizeof(uint64_t),UINT64_MAX,16)
Packit 875988
Packit 875988
#endif /* MHD_FAVOR_SMALL_CODE */
Packit 875988
Packit 875988
#endif /* MHD_STR_H */