"Fossies" - the Fresh Open Source Software Archive

Member "uriparser-0.9.5/include/uriparser/UriBase.h" (18 Mar 2021, 12535 Bytes) of package /linux/www/uriparser-0.9.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "UriBase.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.9.4_vs_0.9.5.

    1 /*
    2  * uriparser - RFC 3986 URI parsing library
    3  *
    4  * Copyright (C) 2007, Weijia Song <songweijia@gmail.com>
    5  * Copyright (C) 2007, Sebastian Pipping <sebastian@pipping.org>
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source  and binary forms, with or without
    9  * modification, are permitted provided  that the following conditions
   10  * are met:
   11  *
   12  *     1. Redistributions  of  source  code   must  retain  the  above
   13  *        copyright notice, this list  of conditions and the following
   14  *        disclaimer.
   15  *
   16  *     2. Redistributions  in binary  form  must  reproduce the  above
   17  *        copyright notice, this list  of conditions and the following
   18  *        disclaimer  in  the  documentation  and/or  other  materials
   19  *        provided with the distribution.
   20  *
   21  *     3. Neither the  name of the  copyright holder nor the  names of
   22  *        its contributors may be used  to endorse or promote products
   23  *        derived from  this software  without specific  prior written
   24  *        permission.
   25  *
   26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   27  * "AS IS" AND  ANY EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT
   28  * LIMITED TO,  THE IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS
   29  * FOR  A  PARTICULAR  PURPOSE  ARE  DISCLAIMED.  IN  NO  EVENT  SHALL
   30  * THE  COPYRIGHT HOLDER  OR CONTRIBUTORS  BE LIABLE  FOR ANY  DIRECT,
   31  * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   32  * (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE GOODS OR
   33  * SERVICES; LOSS OF USE, DATA,  OR PROFITS; OR BUSINESS INTERRUPTION)
   34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   35  * STRICT  LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE  OR  OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   37  * OF THE POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 /**
   41  * @file UriBase.h
   42  * Holds definitions independent of the encoding pass.
   43  */
   44 
   45 #ifndef URI_BASE_H
   46 #define URI_BASE_H 1
   47 
   48 
   49 
   50 /* Version helper macro */
   51 #define URI_ANSI_TO_UNICODE(x) L##x
   52 
   53 
   54 
   55 /* Version */
   56 #define URI_VER_MAJOR           0
   57 #define URI_VER_MINOR           9
   58 #define URI_VER_RELEASE         5
   59 #define URI_VER_SUFFIX_ANSI     ""
   60 #define URI_VER_SUFFIX_UNICODE  URI_ANSI_TO_UNICODE(URI_VER_SUFFIX_ANSI)
   61 
   62 
   63 
   64 /* More version helper macros */
   65 #define URI_INT_TO_ANSI_HELPER(x) #x
   66 #define URI_INT_TO_ANSI(x) URI_INT_TO_ANSI_HELPER(x)
   67 
   68 #define URI_INT_TO_UNICODE_HELPER(x) URI_ANSI_TO_UNICODE(#x)
   69 #define URI_INT_TO_UNICODE(x) URI_INT_TO_UNICODE_HELPER(x)
   70 
   71 #define URI_VER_ANSI_HELPER(ma, mi, r, s) \
   72     URI_INT_TO_ANSI(ma) "." \
   73     URI_INT_TO_ANSI(mi) "." \
   74     URI_INT_TO_ANSI(r) \
   75     s
   76 
   77 #define URI_VER_UNICODE_HELPER(ma, mi, r, s) \
   78     URI_INT_TO_UNICODE(ma) L"." \
   79     URI_INT_TO_UNICODE(mi) L"." \
   80     URI_INT_TO_UNICODE(r) \
   81     s
   82 
   83 
   84 
   85 /* Full version strings */
   86 #define URI_VER_ANSI     URI_VER_ANSI_HELPER(URI_VER_MAJOR, URI_VER_MINOR, URI_VER_RELEASE, URI_VER_SUFFIX_ANSI)
   87 #define URI_VER_UNICODE  URI_VER_UNICODE_HELPER(URI_VER_MAJOR, URI_VER_MINOR, URI_VER_RELEASE, URI_VER_SUFFIX_UNICODE)
   88 
   89 
   90 
   91 /* Unused parameter macro */
   92 #ifdef __GNUC__
   93 # define URI_UNUSED(x) unused_##x __attribute__((unused))
   94 #else
   95 # define URI_UNUSED(x) x
   96 #endif
   97 
   98 
   99 
  100 /* Import/export decorator */
  101 #if defined(URI_STATIC_BUILD)
  102 # define URI_PUBLIC
  103 #else
  104 # if defined(URI_LIBRARY_BUILD)
  105 #  if defined(_MSC_VER)
  106 #   define URI_PUBLIC __declspec(dllexport)
  107 #  elif defined(URI_VISIBILITY)
  108 #   define URI_PUBLIC __attribute__ ((visibility("default")))
  109 #  else
  110 #   define URI_PUBLIC
  111 #  endif
  112 # else
  113 #  if defined(_MSC_VER)
  114 #   define URI_PUBLIC __declspec(dllimport)
  115 #  else
  116 #   define URI_PUBLIC
  117 #  endif
  118 # endif
  119 #endif
  120 
  121 
  122 
  123 typedef int UriBool; /**< Boolean type */
  124 
  125 #define URI_TRUE     1
  126 #define URI_FALSE    0
  127 
  128 
  129 
  130 /* Shared errors */
  131 #define URI_SUCCESS                        0
  132 #define URI_ERROR_SYNTAX                   1 /* Parsed text violates expected format */
  133 #define URI_ERROR_NULL                     2 /* One of the params passed was NULL
  134                                                 although it mustn't be */
  135 #define URI_ERROR_MALLOC                   3 /* Requested memory could not be allocated */
  136 #define URI_ERROR_OUTPUT_TOO_LARGE         4 /* Some output is to large for the receiving buffer */
  137 #define URI_ERROR_NOT_IMPLEMENTED          8 /* The called function is not implemented yet */
  138 #define URI_ERROR_RANGE_INVALID            9 /* The parameters passed contained invalid ranges */
  139 #define URI_ERROR_MEMORY_MANAGER_INCOMPLETE  10 /* [>=0.9.0] The UriMemoryManager passed does not implement all needed functions */
  140 
  141 
  142 /* Errors specific to ToString */
  143 #define URI_ERROR_TOSTRING_TOO_LONG        URI_ERROR_OUTPUT_TOO_LARGE /* Deprecated, test for URI_ERROR_OUTPUT_TOO_LARGE instead */
  144 
  145 /* Errors specific to AddBaseUri */
  146 #define URI_ERROR_ADDBASE_REL_BASE         5 /* Given base is not absolute */
  147 
  148 /* Errors specific to RemoveBaseUri */
  149 #define URI_ERROR_REMOVEBASE_REL_BASE      6 /* Given base is not absolute */
  150 #define URI_ERROR_REMOVEBASE_REL_SOURCE    7 /* Given base is not absolute */
  151 
  152 /* Error specific to uriTestMemoryManager */
  153 #define URI_ERROR_MEMORY_MANAGER_FAULTY   11 /* [>=0.9.0] The UriMemoryManager given did not pass the test suite */
  154 
  155 
  156 #ifndef URI_DOXYGEN
  157 # include <stdio.h> /* For NULL, snprintf */
  158 # include <ctype.h> /* For wchar_t */
  159 # include <string.h> /* For strlen, memset, memcpy */
  160 # include <stdlib.h> /* For malloc */
  161 #endif /* URI_DOXYGEN */
  162 
  163 
  164 
  165 /**
  166  * Holds an IPv4 address.
  167  */
  168 typedef struct UriIp4Struct {
  169     unsigned char data[4]; /**< Each octet in one byte */
  170 } UriIp4; /**< @copydoc UriIp4Struct */
  171 
  172 
  173 
  174 /**
  175  * Holds an IPv6 address.
  176  */
  177 typedef struct UriIp6Struct {
  178     unsigned char data[16]; /**< Each quad in two bytes */
  179 } UriIp6; /**< @copydoc UriIp6Struct */
  180 
  181 
  182 struct UriMemoryManagerStruct;  /* foward declaration to break loop */
  183 
  184 
  185 /**
  186  * Function signature that custom malloc(3) functions must conform to
  187  *
  188  * @since 0.9.0
  189  */
  190 typedef void * (*UriFuncMalloc)(struct UriMemoryManagerStruct *, size_t);
  191 
  192 /**
  193  * Function signature that custom calloc(3) functions must conform to
  194  *
  195  * @since 0.9.0
  196  */
  197 typedef void * (*UriFuncCalloc)(struct UriMemoryManagerStruct *, size_t, size_t);
  198 
  199 /**
  200  * Function signature that custom realloc(3) functions must conform to
  201  *
  202  * @since 0.9.0
  203  */
  204 typedef void * (*UriFuncRealloc)(struct UriMemoryManagerStruct *, void *, size_t);
  205 
  206 /**
  207  * Function signature that custom reallocarray(3) functions must conform to
  208  *
  209  * @since 0.9.0
  210  */
  211 typedef void * (*UriFuncReallocarray)(struct UriMemoryManagerStruct *, void *, size_t, size_t);
  212 
  213 /**
  214  * Function signature that custom free(3) functions must conform to
  215  *
  216  * @since 0.9.0
  217  */
  218 typedef void (*UriFuncFree)(struct UriMemoryManagerStruct *, void *);
  219 
  220 
  221 /**
  222  * Class-like interface of custom memory managers
  223  *
  224  * @see uriCompleteMemoryManager
  225  * @see uriEmulateCalloc
  226  * @see uriEmulateReallocarray
  227  * @see uriTestMemoryManager
  228  * @since 0.9.0
  229  */
  230 typedef struct UriMemoryManagerStruct {
  231     UriFuncMalloc malloc; /**< Pointer to custom malloc(3) */
  232     UriFuncCalloc calloc; /**< Pointer to custom calloc(3); to emulate using malloc and memset see uriEmulateCalloc */
  233     UriFuncRealloc realloc; /**< Pointer to custom realloc(3) */
  234     UriFuncReallocarray reallocarray; /**< Pointer to custom reallocarray(3); to emulate using realloc see uriEmulateReallocarray */
  235     UriFuncFree free; /**< Pointer to custom free(3) */
  236     void * userData; /**< Pointer to data that the other function members need access to */
  237 } UriMemoryManager; /**< @copydoc UriMemoryManagerStruct */
  238 
  239 
  240 /**
  241  * Specifies a line break conversion mode.
  242  */
  243 typedef enum UriBreakConversionEnum {
  244     URI_BR_TO_LF, /**< Convert to Unix line breaks ("\\x0a") */
  245     URI_BR_TO_CRLF, /**< Convert to Windows line breaks ("\\x0d\\x0a") */
  246     URI_BR_TO_CR, /**< Convert to Macintosh line breaks ("\\x0d") */
  247     URI_BR_TO_UNIX = URI_BR_TO_LF, /**< @copydoc UriBreakConversionEnum::URI_BR_TO_LF */
  248     URI_BR_TO_WINDOWS = URI_BR_TO_CRLF, /**< @copydoc UriBreakConversionEnum::URI_BR_TO_CRLF */
  249     URI_BR_TO_MAC = URI_BR_TO_CR, /**< @copydoc UriBreakConversionEnum::URI_BR_TO_CR */
  250     URI_BR_DONT_TOUCH /**< Copy line breaks unmodified */
  251 } UriBreakConversion; /**< @copydoc UriBreakConversionEnum */
  252 
  253 
  254 
  255 /**
  256  * Specifies which component of a %URI has to be normalized.
  257  */
  258 typedef enum UriNormalizationMaskEnum {
  259     URI_NORMALIZED = 0, /**< Do not normalize anything */
  260     URI_NORMALIZE_SCHEME = 1 << 0, /**< Normalize scheme (fix uppercase letters) */
  261     URI_NORMALIZE_USER_INFO = 1 << 1, /**< Normalize user info (fix uppercase percent-encodings) */
  262     URI_NORMALIZE_HOST = 1 << 2, /**< Normalize host (fix uppercase letters) */
  263     URI_NORMALIZE_PATH = 1 << 3, /**< Normalize path (fix uppercase percent-encodings and redundant dot segments) */
  264     URI_NORMALIZE_QUERY = 1 << 4, /**< Normalize query (fix uppercase percent-encodings) */
  265     URI_NORMALIZE_FRAGMENT = 1 << 5 /**< Normalize fragment (fix uppercase percent-encodings) */
  266 } UriNormalizationMask; /**< @copydoc UriNormalizationMaskEnum */
  267 
  268 
  269 
  270 /**
  271  * Specifies how to resolve %URI references.
  272  */
  273 typedef enum UriResolutionOptionsEnum {
  274     URI_RESOLVE_STRICTLY = 0, /**< Full RFC conformance */
  275     URI_RESOLVE_IDENTICAL_SCHEME_COMPAT = 1 << 0 /**< Treat %URI to resolve with identical scheme as having no scheme */
  276 } UriResolutionOptions; /**< @copydoc UriResolutionOptionsEnum */
  277 
  278 
  279 
  280 /**
  281  * Wraps a memory manager backend that only provides malloc and free
  282  * to make a complete memory manager ready to be used.
  283  *
  284  * The core feature of this wrapper is that you don't need to implement
  285  * realloc if you don't want to.  The wrapped memory manager uses
  286  * backend->malloc, memcpy, and backend->free and soieof(size_t) extra
  287  * bytes per allocation to emulate fallback realloc for you.
  288  *
  289  * memory->calloc is uriEmulateCalloc.
  290  * memory->free uses backend->free and handles the size header.
  291  * memory->malloc uses backend->malloc and adds a size header.
  292  * memory->realloc uses memory->malloc, memcpy, and memory->free and reads
  293  *                 the size header.
  294  * memory->reallocarray is uriEmulateReallocarray.
  295  *
  296  * The internal workings behind memory->free, memory->malloc, and
  297  * memory->realloc may change so the functions exposed by these function
  298  * pointer sshould be consided internal and not public API.
  299  *
  300  * @param memory   <b>OUT</b>: Where to write the wrapped memory manager to
  301  * @param backend  <b>IN</b>: Memory manager to use as a backend
  302  * @return          Error code or 0 on success
  303  *
  304  * @see uriEmulateCalloc
  305  * @see uriEmulateReallocarray
  306  * @see UriMemoryManager
  307  * @since 0.9.0
  308  */
  309 URI_PUBLIC int uriCompleteMemoryManager(UriMemoryManager * memory,
  310         UriMemoryManager * backend);
  311 
  312 
  313 
  314 /**
  315  * Offers emulation of calloc(3) based on memory->malloc and memset.
  316  * See "man 3 calloc" as well.
  317  *
  318  * @param memory  <b>IN</b>: Memory manager to use, should not be NULL
  319  * @param nmemb   <b>IN</b>: Number of elements to allocate
  320  * @param size    <b>IN</b>: Size in bytes per element
  321  * @return        Pointer to allocated memory or NULL
  322  *
  323  * @see uriCompleteMemoryManager
  324  * @see uriEmulateReallocarray
  325  * @see UriMemoryManager
  326  * @since 0.9.0
  327  */
  328 URI_PUBLIC void * uriEmulateCalloc(UriMemoryManager * memory,
  329         size_t nmemb, size_t size);
  330 
  331 
  332 
  333 /**
  334  * Offers emulation of reallocarray(3) based on memory->realloc.
  335  * See "man 3 reallocarray" as well.
  336  *
  337  * @param memory  <b>IN</b>: Memory manager to use, should not be NULL
  338  * @param ptr     <b>IN</b>: Pointer allocated using memory->malloc/... or NULL
  339  * @param nmemb   <b>IN</b>: Number of elements to allocate
  340  * @param size    <b>IN</b>: Size in bytes per element
  341  * @return        Pointer to allocated memory or NULL
  342  *
  343  * @see uriCompleteMemoryManager
  344  * @see uriEmulateCalloc
  345  * @see UriMemoryManager
  346  * @since 0.9.0
  347  */
  348 URI_PUBLIC void * uriEmulateReallocarray(UriMemoryManager * memory,
  349         void * ptr, size_t nmemb, size_t size);
  350 
  351 
  352 
  353 /**
  354  * Run multiple tests against a given memory manager.
  355  * For example, one test
  356  * 1. allocates a small amount of memory,
  357  * 2. writes some magic bytes to it,
  358  * 3. reallocates it,
  359  * 4. checks that previous values are still present,
  360  * 5. and frees that memory.
  361  *
  362  * It is recommended to compile with AddressSanitizer enabled
  363  * to take full advantage of uriTestMemoryManager.
  364  *
  365  * @param memory  <b>IN</b>: Memory manager to use, should not be NULL
  366  * @return        Error code or 0 on success
  367  *
  368  * @see uriEmulateCalloc
  369  * @see uriEmulateReallocarray
  370  * @see UriMemoryManager
  371  * @since 0.9.0
  372  */
  373 URI_PUBLIC int uriTestMemoryManager(UriMemoryManager * memory);
  374 
  375 
  376 
  377 #endif /* URI_BASE_H */