"Fossies" - the Fresh Open Source Software Archive

Member "uriparser-0.9.7/src/UriShorten.c" (5 Oct 2022, 10372 Bytes) of package /linux/www/uriparser-0.9.7.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 "UriShorten.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.9.3_vs_0.9.4.

    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 /* What encodings are enabled? */
   41 #include <uriparser/UriDefsConfig.h>
   42 #if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE))
   43 /* Include SELF twice */
   44 # ifdef URI_ENABLE_ANSI
   45 #  define URI_PASS_ANSI 1
   46 #  include "UriShorten.c"
   47 #  undef URI_PASS_ANSI
   48 # endif
   49 # ifdef URI_ENABLE_UNICODE
   50 #  define URI_PASS_UNICODE 1
   51 #  include "UriShorten.c"
   52 #  undef URI_PASS_UNICODE
   53 # endif
   54 #else
   55 # ifdef URI_PASS_ANSI
   56 #  include <uriparser/UriDefsAnsi.h>
   57 # else
   58 #  include <uriparser/UriDefsUnicode.h>
   59 #  include <wchar.h>
   60 # endif
   61 
   62 
   63 
   64 #ifndef URI_DOXYGEN
   65 # include <uriparser/Uri.h>
   66 # include "UriCommon.h"
   67 # include "UriMemory.h"
   68 #endif
   69 
   70 
   71 
   72 static URI_INLINE UriBool URI_FUNC(AppendSegment)(URI_TYPE(Uri) * uri,
   73         const URI_CHAR * first, const URI_CHAR * afterLast,
   74         UriMemoryManager * memory) {
   75     /* Create segment */
   76     URI_TYPE(PathSegment) * segment = memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));
   77     if (segment == NULL) {
   78         return URI_FALSE; /* Raises malloc error */
   79     }
   80     segment->next = NULL;
   81     segment->text.first = first;
   82     segment->text.afterLast = afterLast;
   83 
   84     /* Put into chain */
   85     if (uri->pathTail == NULL) {
   86         uri->pathHead = segment;
   87     } else {
   88         uri->pathTail->next = segment;
   89     }
   90     uri->pathTail = segment;
   91 
   92     return URI_TRUE;
   93 }
   94 
   95 
   96 
   97 static URI_INLINE UriBool URI_FUNC(EqualsAuthority)(const URI_TYPE(Uri) * first,
   98         const URI_TYPE(Uri) * second) {
   99     /* IPv4 */
  100     if (first->hostData.ip4 != NULL) {
  101         return ((second->hostData.ip4 != NULL)
  102                 && !memcmp(first->hostData.ip4->data,
  103                     second->hostData.ip4->data, 4)) ? URI_TRUE : URI_FALSE;
  104     }
  105 
  106     /* IPv6 */
  107     if (first->hostData.ip6 != NULL) {
  108         return ((second->hostData.ip6 != NULL)
  109                 && !memcmp(first->hostData.ip6->data,
  110                     second->hostData.ip6->data, 16)) ? URI_TRUE : URI_FALSE;
  111     }
  112 
  113     /* IPvFuture */
  114     if (first->hostData.ipFuture.first != NULL) {
  115         return ((second->hostData.ipFuture.first != NULL)
  116                 && !URI_FUNC(CompareRange)(&first->hostData.ipFuture,
  117                     &second->hostData.ipFuture)) ? URI_TRUE : URI_FALSE;
  118     }
  119 
  120     return !URI_FUNC(CompareRange)(&first->hostText, &second->hostText)
  121             ? URI_TRUE : URI_FALSE;
  122 }
  123 
  124 
  125 
  126 static int URI_FUNC(RemoveBaseUriImpl)(URI_TYPE(Uri) * dest,
  127         const URI_TYPE(Uri) * absSource,
  128         const URI_TYPE(Uri) * absBase,
  129         UriBool domainRootMode, UriMemoryManager * memory) {
  130     if (dest == NULL) {
  131         return URI_ERROR_NULL;
  132     }
  133     URI_FUNC(ResetUri)(dest);
  134 
  135     if ((absSource == NULL) || (absBase == NULL)) {
  136         return URI_ERROR_NULL;
  137     }
  138 
  139     /* absBase absolute? */
  140     if (absBase->scheme.first == NULL) {
  141         return URI_ERROR_REMOVEBASE_REL_BASE;
  142     }
  143 
  144     /* absSource absolute? */
  145     if (absSource->scheme.first == NULL) {
  146         return URI_ERROR_REMOVEBASE_REL_SOURCE;
  147     }
  148 
  149     /* [01/50]  if (A.scheme != Base.scheme) then */
  150                 if (URI_FUNC(CompareRange)(&absSource->scheme, &absBase->scheme)) {
  151     /* [02/50]     T.scheme    = A.scheme; */
  152                     dest->scheme = absSource->scheme;
  153     /* [03/50]     T.authority = A.authority; */
  154                     if (!URI_FUNC(CopyAuthority)(dest, absSource, memory)) {
  155                         return URI_ERROR_MALLOC;
  156                     }
  157     /* [04/50]     T.path      = A.path; */
  158                     if (!URI_FUNC(CopyPath)(dest, absSource, memory)) {
  159                         return URI_ERROR_MALLOC;
  160                     }
  161     /* [05/50]  else */
  162                 } else {
  163     /* [06/50]     undef(T.scheme); */
  164                     /* NOOP */
  165     /* [07/50]     if (A.authority != Base.authority) then */
  166                     if (!URI_FUNC(EqualsAuthority)(absSource, absBase)) {
  167     /* [08/50]        T.authority = A.authority; */
  168                         if (!URI_FUNC(CopyAuthority)(dest, absSource, memory)) {
  169                             return URI_ERROR_MALLOC;
  170                         }
  171     /* [09/50]        T.path      = A.path; */
  172                         if (!URI_FUNC(CopyPath)(dest, absSource, memory)) {
  173                             return URI_ERROR_MALLOC;
  174                         }
  175     /* [10/50]     else */
  176                     } else {
  177     /* [11/50]        if domainRootMode then */
  178                         if (domainRootMode == URI_TRUE) {
  179     /* [12/50]           undef(T.authority); */
  180                             /* NOOP */
  181     /* [13/50]           if (first(A.path) == "") then */
  182                             /* GROUPED */
  183     /* [14/50]              T.path   = "/." + A.path; */
  184                                 /* GROUPED */
  185     /* [15/50]           else */
  186                                 /* GROUPED */
  187     /* [16/50]              T.path   = A.path; */
  188                                 /* GROUPED */
  189     /* [17/50]           endif; */
  190                             if (!URI_FUNC(CopyPath)(dest, absSource, memory)) {
  191                                 return URI_ERROR_MALLOC;
  192                             }
  193                             dest->absolutePath = URI_TRUE;
  194 
  195                             if (!URI_FUNC(FixAmbiguity)(dest, memory)) {
  196                                 return URI_ERROR_MALLOC;
  197                             }
  198     /* [18/50]        else */
  199                         } else {
  200                             const URI_TYPE(PathSegment) * sourceSeg = absSource->pathHead;
  201                             const URI_TYPE(PathSegment) * baseSeg = absBase->pathHead;
  202     /* [19/50]           bool pathNaked = true; */
  203                             UriBool pathNaked = URI_TRUE;
  204     /* [20/50]           undef(last(Base.path)); */
  205                             /* NOOP */
  206     /* [21/50]           T.path = ""; */
  207                             dest->absolutePath = URI_FALSE;
  208     /* [22/50]           while (first(A.path) == first(Base.path)) do */
  209                             while ((sourceSeg != NULL) && (baseSeg != NULL)
  210                                     && !URI_FUNC(CompareRange)(&sourceSeg->text, &baseSeg->text)
  211                                     && !((sourceSeg->text.first == sourceSeg->text.afterLast)
  212                                         && ((sourceSeg->next == NULL) != (baseSeg->next == NULL)))) {
  213     /* [23/50]              A.path++; */
  214                                 sourceSeg = sourceSeg->next;
  215     /* [24/50]              Base.path++; */
  216                                 baseSeg = baseSeg->next;
  217     /* [25/50]           endwhile; */
  218                             }
  219     /* [26/50]           while defined(first(Base.path)) do */
  220                             while ((baseSeg != NULL) && (baseSeg->next != NULL)) {
  221     /* [27/50]              Base.path++; */
  222                                 baseSeg = baseSeg->next;
  223     /* [28/50]              T.path += "../"; */
  224                                 if (!URI_FUNC(AppendSegment)(dest, URI_FUNC(ConstParent),
  225                                         URI_FUNC(ConstParent) + 2, memory)) {
  226                                     return URI_ERROR_MALLOC;
  227                                 }
  228     /* [29/50]              pathNaked = false; */
  229                                 pathNaked = URI_FALSE;
  230     /* [30/50]           endwhile; */
  231                             }
  232     /* [31/50]           while defined(first(A.path)) do */
  233                             while (sourceSeg != NULL) {
  234     /* [32/50]              if pathNaked then */
  235                                 if (pathNaked == URI_TRUE) {
  236     /* [33/50]                 if (first(A.path) contains ":") then */
  237                                     UriBool containsColon = URI_FALSE;
  238                                     const URI_CHAR * ch = sourceSeg->text.first;
  239                                     for (; ch < sourceSeg->text.afterLast; ch++) {
  240                                         if (*ch == _UT(':')) {
  241                                             containsColon = URI_TRUE;
  242                                             break;
  243                                         }
  244                                     }
  245 
  246                                     if (containsColon) {
  247     /* [34/50]                    T.path += "./"; */
  248                                         if (!URI_FUNC(AppendSegment)(dest, URI_FUNC(ConstPwd),
  249                                                 URI_FUNC(ConstPwd) + 1, memory)) {
  250                                             return URI_ERROR_MALLOC;
  251                                         }
  252     /* [35/50]                 elseif (first(A.path) == "") then */
  253                                     } else if (sourceSeg->text.first == sourceSeg->text.afterLast) {
  254     /* [36/50]                    T.path += "/."; */
  255                                         if (!URI_FUNC(AppendSegment)(dest, URI_FUNC(ConstPwd),
  256                                                 URI_FUNC(ConstPwd) + 1, memory)) {
  257                                             return URI_ERROR_MALLOC;
  258                                         }
  259     /* [37/50]                 endif; */
  260                                     }
  261     /* [38/50]              endif; */
  262                                 }
  263     /* [39/50]              T.path += first(A.path); */
  264                                 if (!URI_FUNC(AppendSegment)(dest, sourceSeg->text.first,
  265                                         sourceSeg->text.afterLast, memory)) {
  266                                     return URI_ERROR_MALLOC;
  267                                 }
  268     /* [40/50]              pathNaked = false; */
  269                                 pathNaked = URI_FALSE;
  270     /* [41/50]              A.path++; */
  271                                 sourceSeg = sourceSeg->next;
  272     /* [42/50]              if defined(first(A.path)) then */
  273                                 /* NOOP */
  274     /* [43/50]                 T.path += + "/"; */
  275                                 /* NOOP */
  276     /* [44/50]              endif; */
  277                                 /* NOOP */
  278     /* [45/50]           endwhile; */
  279                             }
  280     /* [46/50]        endif; */
  281                         }
  282     /* [47/50]     endif; */
  283                     }
  284     /* [48/50]  endif; */
  285                 }
  286     /* [49/50]  T.query     = A.query; */
  287                 dest->query = absSource->query;
  288     /* [50/50]  T.fragment  = A.fragment; */
  289                 dest->fragment = absSource->fragment;
  290 
  291     return URI_SUCCESS;
  292 }
  293 
  294 
  295 
  296 int URI_FUNC(RemoveBaseUri)(URI_TYPE(Uri) * dest,
  297         const URI_TYPE(Uri) * absSource,
  298         const URI_TYPE(Uri) * absBase,
  299         UriBool domainRootMode) {
  300     return URI_FUNC(RemoveBaseUriMm)(dest, absSource, absBase,
  301             domainRootMode, NULL);
  302 }
  303 
  304 
  305 
  306 int URI_FUNC(RemoveBaseUriMm)(URI_TYPE(Uri) * dest,
  307         const URI_TYPE(Uri) * absSource,
  308         const URI_TYPE(Uri) * absBase,
  309         UriBool domainRootMode, UriMemoryManager * memory) {
  310     int res;
  311 
  312     URI_CHECK_MEMORY_MANAGER(memory);  /* may return */
  313 
  314     res = URI_FUNC(RemoveBaseUriImpl)(dest, absSource,
  315             absBase, domainRootMode, memory);
  316     if ((res != URI_SUCCESS) && (dest != NULL)) {
  317         URI_FUNC(FreeUriMembersMm)(dest, memory);
  318     }
  319     return res;
  320 }
  321 
  322 
  323 
  324 #endif