src/core/ngx_string.c - nginx source code

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. static u_char *ngx_sprintf_num(u_char *buf, u_char *last, uint64_t ui64,
  8.     u_char zero, ngx_uint_t hexadecimal, ngx_uint_t width);
  9. static u_char *ngx_sprintf_str(u_char *buf, u_char *last, u_char *src,
  10.     size_t len, ngx_uint_t hexadecimal);
  11. static void ngx_encode_base64_internal(ngx_str_t *dst, ngx_str_t *src,
  12.     const u_char *basis, ngx_uint_t padding);
  13. static ngx_int_t ngx_decode_base64_internal(ngx_str_t *dst, ngx_str_t *src,
  14.     const u_char *basis);


  15. void
  16. ngx_strlow(u_char *dst, u_char *src, size_t n)
  17. {
  18.     while (n) {
  19.         *dst = ngx_tolower(*src);
  20.         dst++;
  21.         src++;
  22.         n--;
  23.     }
  24. }


  25. size_t
  26. ngx_strnlen(u_char *p, size_t n)
  27. {
  28.     size_t  i;

  29.     for (i = 0; i < n; i++) {

  30.         if (p[i] == '\0') {
  31.             return i;
  32.         }
  33.     }

  34.     return n;
  35. }


  36. u_char *
  37. ngx_cpystrn(u_char *dst, u_char *src, size_t n)
  38. {
  39.     if (n == 0) {
  40.         return dst;
  41.     }

  42.     while (--n) {
  43.         *dst = *src;

  44.         if (*dst == '\0') {
  45.             return dst;
  46.         }

  47.         dst++;
  48.         src++;
  49.     }

  50.     *dst = '\0';

  51.     return dst;
  52. }


  53. u_char *
  54. ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src)
  55. {
  56.     u_char  *dst;

  57.     dst = ngx_pnalloc(pool, src->len);
  58.     if (dst == NULL) {
  59.         return NULL;
  60.     }

  61.     ngx_memcpy(dst, src->data, src->len);

  62.     return dst;
  63. }


  64. /*
  65. * supported formats:
  66. *    %[0][width][x][X]O        off_t
  67. *    %[0][width]T              time_t
  68. *    %[0][width][u][x|X]z      ssize_t/size_t
  69. *    %[0][width][u][x|X]d      int/u_int
  70. *    %[0][width][u][x|X]l      long
  71. *    %[0][width|m][u][x|X]i    ngx_int_t/ngx_uint_t
  72. *    %[0][width][u][x|X]D      int32_t/uint32_t
  73. *    %[0][width][u][x|X]L      int64_t/uint64_t
  74. *    %[0][width|m][u][x|X]A    ngx_atomic_int_t/ngx_atomic_uint_t
  75. *    %[0][width][.width]f      double, max valid number fits to %18.15f
  76. *    %P                        ngx_pid_t
  77. *    %M                        ngx_msec_t
  78. *    %r                        rlim_t
  79. *    %p                        void *
  80. *    %[x|X]V                   ngx_str_t *
  81. *    %[x|X]v                   ngx_variable_value_t *
  82. *    %[x|X]s                   null-terminated string
  83. *    %*[x|X]s                  length and string
  84. *    %Z                        '\0'
  85. *    %N                        '\n'
  86. *    %c                        char
  87. *    %%                        %
  88. *
  89. *  reserved:
  90. *    %t                        ptrdiff_t
  91. *    %S                        null-terminated wchar string
  92. *    %C                        wchar
  93. */


  94. u_char * ngx_cdecl
  95. ngx_sprintf(u_char *buf, const char *fmt, ...)
  96. {
  97.     u_char   *p;
  98.     va_list   args;

  99.     va_start(args, fmt);
  100.     p = ngx_vslprintf(buf, (void *) -1, fmt, args);
  101.     va_end(args);

  102.     return p;
  103. }


  104. u_char * ngx_cdecl
  105. ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)
  106. {
  107.     u_char   *p;
  108.     va_list   args;

  109.     va_start(args, fmt);
  110.     p = ngx_vslprintf(buf, buf + max, fmt, args);
  111.     va_end(args);

  112.     return p;
  113. }


  114. u_char * ngx_cdecl
  115. ngx_slprintf(u_char *buf, u_char *last, const char *fmt, ...)
  116. {
  117.     u_char   *p;
  118.     va_list   args;

  119.     va_start(args, fmt);
  120.     p = ngx_vslprintf(buf, last, fmt, args);
  121.     va_end(args);

  122.     return p;
  123. }


  124. u_char *
  125. ngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args)
  126. {
  127.     u_char                *p, zero;
  128.     int                    d;
  129.     double                 f;
  130.     size_t                 slen;
  131.     int64_t                i64;
  132.     uint64_t               ui64, frac;
  133.     ngx_msec_t             ms;
  134.     ngx_uint_t             width, sign, hex, max_width, frac_width, scale, n;
  135.     ngx_str_t             *v;
  136.     ngx_variable_value_t  *vv;

  137.     while (*fmt && buf < last) {

  138.         /*
  139.          * "buf < last" means that we could copy at least one character:
  140.          * the plain character, "%%", "%c", and minus without the checking
  141.          */

  142.         if (*fmt == '%') {

  143.             i64 = 0;
  144.             ui64 = 0;

  145.             zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
  146.             width = 0;
  147.             sign = 1;
  148.             hex = 0;
  149.             max_width = 0;
  150.             frac_width = 0;
  151.             slen = (size_t) -1;

  152.             while (*fmt >= '0' && *fmt <= '9') {
  153.                 width = width * 10 + (*fmt++ - '0');
  154.             }


  155.             for ( ;; ) {
  156.                 switch (*fmt) {

  157.                 case 'u':
  158.                     sign = 0;
  159.                     fmt++;
  160.                     continue;

  161.                 case 'm':
  162.                     max_width = 1;
  163.                     fmt++;
  164.                     continue;

  165.                 case 'X':
  166.                     hex = 2;
  167.                     sign = 0;
  168.                     fmt++;
  169.                     continue;

  170.                 case 'x':
  171.                     hex = 1;
  172.                     sign = 0;
  173.                     fmt++;
  174.                     continue;

  175.                 case '.':
  176.                     fmt++;

  177.                     while (*fmt >= '0' && *fmt <= '9') {
  178.                         frac_width = frac_width * 10 + (*fmt++ - '0');
  179.                     }

  180.                     break;

  181.                 case '*':
  182.                     slen = va_arg(args, size_t);
  183.                     fmt++;
  184.                     continue;

  185.                 default:
  186.                     break;
  187.                 }

  188.                 break;
  189.             }


  190.             switch (*fmt) {

  191.             case 'V':
  192.                 v = va_arg(args, ngx_str_t *);

  193.                 buf = ngx_sprintf_str(buf, last, v->data, v->len, hex);
  194.                 fmt++;

  195.                 continue;

  196.             case 'v':
  197.                 vv = va_arg(args, ngx_variable_value_t *);

  198.                 buf = ngx_sprintf_str(buf, last, vv->data, vv->len, hex);
  199.                 fmt++;

  200.                 continue;

  201.             case 's':
  202.                 p = va_arg(args, u_char *);

  203.                 buf = ngx_sprintf_str(buf, last, p, slen, hex);
  204.                 fmt++;

  205.                 continue;

  206.             case 'O':
  207.                 i64 = (int64_t) va_arg(args, off_t);
  208.                 sign = 1;
  209.                 break;

  210.             case 'P':
  211.                 i64 = (int64_t) va_arg(args, ngx_pid_t);
  212.                 sign = 1;
  213.                 break;

  214.             case 'T':
  215.                 i64 = (int64_t) va_arg(args, time_t);
  216.                 sign = 1;
  217.                 break;

  218.             case 'M':
  219.                 ms = (ngx_msec_t) va_arg(args, ngx_msec_t);
  220.                 if ((ngx_msec_int_t) ms == -1) {
  221.                     sign = 1;
  222.                     i64 = -1;
  223.                 } else {
  224.                     sign = 0;
  225.                     ui64 = (uint64_t) ms;
  226.                 }
  227.                 break;

  228.             case 'z':
  229.                 if (sign) {
  230.                     i64 = (int64_t) va_arg(args, ssize_t);
  231.                 } else {
  232.                     ui64 = (uint64_t) va_arg(args, size_t);
  233.                 }
  234.                 break;

  235.             case 'i':
  236.                 if (sign) {
  237.                     i64 = (int64_t) va_arg(args, ngx_int_t);
  238.                 } else {
  239.                     ui64 = (uint64_t) va_arg(args, ngx_uint_t);
  240.                 }

  241.                 if (max_width) {
  242.                     width = NGX_INT_T_LEN;
  243.                 }

  244.                 break;

  245.             case 'd':
  246.                 if (sign) {
  247.                     i64 = (int64_t) va_arg(args, int);
  248.                 } else {
  249.                     ui64 = (uint64_t) va_arg(args, u_int);
  250.                 }
  251.                 break;

  252.             case 'l':
  253.                 if (sign) {
  254.                     i64 = (int64_t) va_arg(args, long);
  255.                 } else {
  256.                     ui64 = (uint64_t) va_arg(args, u_long);
  257.                 }
  258.                 break;

  259.             case 'D':
  260.                 if (sign) {
  261.                     i64 = (int64_t) va_arg(args, int32_t);
  262.                 } else {
  263.                     ui64 = (uint64_t) va_arg(args, uint32_t);
  264.                 }
  265.                 break;

  266.             case 'L':
  267.                 if (sign) {
  268.                     i64 = va_arg(args, int64_t);
  269.                 } else {
  270.                     ui64 = va_arg(args, uint64_t);
  271.                 }
  272.                 break;

  273.             case 'A':
  274.                 if (sign) {
  275.                     i64 = (int64_t) va_arg(args, ngx_atomic_int_t);
  276.                 } else {
  277.                     ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);
  278.                 }

  279.                 if (max_width) {
  280.                     width = NGX_ATOMIC_T_LEN;
  281.                 }

  282.                 break;

  283.             case 'f':
  284.                 f = va_arg(args, double);

  285.                 if (f < 0) {
  286.                     *buf++ = '-';
  287.                     f = -f;
  288.                 }

  289.                 ui64 = (int64_t) f;
  290.                 frac = 0;

  291.                 if (frac_width) {

  292.                     scale = 1;
  293.                     for (n = frac_width; n; n--) {
  294.                         scale *= 10;
  295.                     }

  296.                     frac = (uint64_t) ((f - (double) ui64) * scale + 0.5);

  297.                     if (frac == scale) {
  298.                         ui64++;
  299.                         frac = 0;
  300.                     }
  301.                 }

  302.                 buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);

  303.                 if (frac_width) {
  304.                     if (buf < last) {
  305.                         *buf++ = '.';
  306.                     }

  307.                     buf = ngx_sprintf_num(buf, last, frac, '0', 0, frac_width);
  308.                 }

  309.                 fmt++;

  310.                 continue;

  311. #if !(NGX_WIN32)
  312.             case 'r':
  313.                 i64 = (int64_t) va_arg(args, rlim_t);
  314.                 sign = 1;
  315.                 break;
  316. #endif

  317.             case 'p':
  318.                 ui64 = (uintptr_t) va_arg(args, void *);
  319.                 hex = 2;
  320.                 sign = 0;
  321.                 zero = '0';
  322.                 width = 2 * sizeof(void *);
  323.                 break;

  324.             case 'c':
  325.                 d = va_arg(args, int);
  326.                 *buf++ = (u_char) (d & 0xff);
  327.                 fmt++;

  328.                 continue;

  329.             case 'Z':
  330.                 *buf++ = '\0';
  331.                 fmt++;

  332.                 continue;

  333.             case 'N':
  334. #if (NGX_WIN32)
  335.                 *buf++ = CR;
  336.                 if (buf < last) {
  337.                     *buf++ = LF;
  338.                 }
  339. #else
  340.                 *buf++ = LF;
  341. #endif
  342.                 fmt++;

  343.                 continue;

  344.             case '%':
  345.                 *buf++ = '%';
  346.                 fmt++;

  347.                 continue;

  348.             default:
  349.                 *buf++ = *fmt++;

  350.                 continue;
  351.             }

  352.             if (sign) {
  353.                 if (i64 < 0) {
  354.                     *buf++ = '-';
  355.                     ui64 = (uint64_t) -i64;

  356.                 } else {
  357.                     ui64 = (uint64_t) i64;
  358.                 }
  359.             }

  360.             buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);

  361.             fmt++;

  362.         } else {
  363.             *buf++ = *fmt++;
  364.         }
  365.     }

  366.     return buf;
  367. }


  368. static u_char *
  369. ngx_sprintf_num(u_char *buf, u_char *last, uint64_t ui64, u_char zero,
  370.     ngx_uint_t hexadecimal, ngx_uint_t width)
  371. {
  372.     u_char         *p, temp[NGX_INT64_LEN + 1];
  373.                        /*
  374.                         * we need temp[NGX_INT64_LEN] only,
  375.                         * but icc issues the warning
  376.                         */
  377.     size_t          len;
  378.     uint32_t        ui32;
  379.     static u_char   hex[] = "0123456789abcdef";
  380.     static u_char   HEX[] = "0123456789ABCDEF";

  381.     p = temp + NGX_INT64_LEN;

  382.     if (hexadecimal == 0) {

  383.         if (ui64 <= (uint64_t) NGX_MAX_UINT32_VALUE) {

  384.             /*
  385.              * To divide 64-bit numbers and to find remainders
  386.              * on the x86 platform gcc and icc call the libc functions
  387.              * [u]divdi3() and [u]moddi3(), they call another function
  388.              * in its turn.  On FreeBSD it is the qdivrem() function,
  389.              * its source code is about 170 lines of the code.
  390.              * The glibc counterpart is about 150 lines of the code.
  391.              *
  392.              * For 32-bit numbers and some divisors gcc and icc use
  393.              * a inlined multiplication and shifts.  For example,
  394.              * unsigned "i32 / 10" is compiled to
  395.              *
  396.              *     (i32 * 0xCCCCCCCD) >> 35
  397.              */

  398.             ui32 = (uint32_t) ui64;

  399.             do {
  400.                 *--p = (u_char) (ui32 % 10 + '0');
  401.             } while (ui32 /= 10);

  402.         } else {
  403.             do {
  404.                 *--p = (u_char) (ui64 % 10 + '0');
  405.             } while (ui64 /= 10);
  406.         }

  407.     } else if (hexadecimal == 1) {

  408.         do {

  409.             /* the "(uint32_t)" cast disables the BCC's warning */
  410.             *--p = hex[(uint32_t) (ui64 & 0xf)];

  411.         } while (ui64 >>= 4);

  412.     } else { /* hexadecimal == 2 */

  413.         do {

  414.             /* the "(uint32_t)" cast disables the BCC's warning */
  415.             *--p = HEX[(uint32_t) (ui64 & 0xf)];

  416.         } while (ui64 >>= 4);
  417.     }

  418.     /* zero or space padding */

  419.     len = (temp + NGX_INT64_LEN) - p;

  420.     while (len++ < width && buf < last) {
  421.         *buf++ = zero;
  422.     }

  423.     /* number safe copy */

  424.     len = (temp + NGX_INT64_LEN) - p;

  425.     if (buf + len > last) {
  426.         len = last - buf;
  427.     }

  428.     return ngx_cpymem(buf, p, len);
  429. }


  430. static u_char *
  431. ngx_sprintf_str(u_char *buf, u_char *last, u_char *src, size_t len,
  432.     ngx_uint_t hexadecimal)
  433. {
  434.     static u_char   hex[] = "0123456789abcdef";
  435.     static u_char   HEX[] = "0123456789ABCDEF";

  436.     if (hexadecimal == 0) {

  437.         if (len == (size_t) -1) {
  438.             while (*src && buf < last) {
  439.                 *buf++ = *src++;
  440.             }

  441.         } else {
  442.             len = ngx_min((size_t) (last - buf), len);
  443.             buf = ngx_cpymem(buf, src, len);
  444.         }

  445.     } else if (hexadecimal == 1) {

  446.         if (len == (size_t) -1) {

  447.             while (*src && buf < last - 1) {
  448.                 *buf++ = hex[*src >> 4];
  449.                 *buf++ = hex[*src++ & 0xf];
  450.             }

  451.         } else {

  452.             while (len-- && buf < last - 1) {
  453.                 *buf++ = hex[*src >> 4];
  454.                 *buf++ = hex[*src++ & 0xf];
  455.             }
  456.         }

  457.     } else { /* hexadecimal == 2 */

  458.         if (len == (size_t) -1) {

  459.             while (*src && buf < last - 1) {
  460.                 *buf++ = HEX[*src >> 4];
  461.                 *buf++ = HEX[*src++ & 0xf];
  462.             }

  463.         } else {

  464.             while (len-- && buf < last - 1) {
  465.                 *buf++ = HEX[*src >> 4];
  466.                 *buf++ = HEX[*src++ & 0xf];
  467.             }
  468.         }
  469.     }

  470.     return buf;
  471. }


  472. /*
  473. * We use ngx_strcasecmp()/ngx_strncasecmp() for 7-bit ASCII strings only,
  474. * and implement our own ngx_strcasecmp()/ngx_strncasecmp()
  475. * to avoid libc locale overhead.  Besides, we use the ngx_uint_t's
  476. * instead of the u_char's, because they are slightly faster.
  477. */

  478. ngx_int_t
  479. ngx_strcasecmp(u_char *s1, u_char *s2)
  480. {
  481.     ngx_uint_t  c1, c2;

  482.     for ( ;; ) {
  483.         c1 = (ngx_uint_t) *s1++;
  484.         c2 = (ngx_uint_t) *s2++;

  485.         c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
  486.         c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;

  487.         if (c1 == c2) {

  488.             if (c1) {
  489.                 continue;
  490.             }

  491.             return 0;
  492.         }

  493.         return c1 - c2;
  494.     }
  495. }


  496. ngx_int_t
  497. ngx_strncasecmp(u_char *s1, u_char *s2, size_t n)
  498. {
  499.     ngx_uint_t  c1, c2;

  500.     while (n) {
  501.         c1 = (ngx_uint_t) *s1++;
  502.         c2 = (ngx_uint_t) *s2++;

  503.         c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
  504.         c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;

  505.         if (c1 == c2) {

  506.             if (c1) {
  507.                 n--;
  508.                 continue;
  509.             }

  510.             return 0;
  511.         }

  512.         return c1 - c2;
  513.     }

  514.     return 0;
  515. }


  516. u_char *
  517. ngx_strnstr(u_char *s1, char *s2, size_t len)
  518. {
  519.     u_char  c1, c2;
  520.     size_t  n;

  521.     c2 = *(u_char *) s2++;

  522.     n = ngx_strlen(s2);

  523.     do {
  524.         do {
  525.             if (len-- == 0) {
  526.                 return NULL;
  527.             }

  528.             c1 = *s1++;

  529.             if (c1 == 0) {
  530.                 return NULL;
  531.             }

  532.         } while (c1 != c2);

  533.         if (n > len) {
  534.             return NULL;
  535.         }

  536.     } while (ngx_strncmp(s1, (u_char *) s2, n) != 0);

  537.     return --s1;
  538. }


  539. /*
  540. * ngx_strstrn() and ngx_strcasestrn() are intended to search for static
  541. * substring with known length in null-terminated string. The argument n
  542. * must be length of the second substring - 1.
  543. */

  544. u_char *
  545. ngx_strstrn(u_char *s1, char *s2, size_t n)
  546. {
  547.     u_char  c1, c2;

  548.     c2 = *(u_char *) s2++;

  549.     do {
  550.         do {
  551.             c1 = *s1++;

  552.             if (c1 == 0) {
  553.                 return NULL;
  554.             }

  555.         } while (c1 != c2);

  556.     } while (ngx_strncmp(s1, (u_char *) s2, n) != 0);

  557.     return --s1;
  558. }


  559. u_char *
  560. ngx_strcasestrn(u_char *s1, char *s2, size_t n)
  561. {
  562.     ngx_uint_t  c1, c2;

  563.     c2 = (ngx_uint_t) *s2++;
  564.     c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;

  565.     do {
  566.         do {
  567.             c1 = (ngx_uint_t) *s1++;

  568.             if (c1 == 0) {
  569.                 return NULL;
  570.             }

  571.             c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;

  572.         } while (c1 != c2);

  573.     } while (ngx_strncasecmp(s1, (u_char *) s2, n) != 0);

  574.     return --s1;
  575. }


  576. /*
  577. * ngx_strlcasestrn() is intended to search for static substring
  578. * with known length in string until the argument last. The argument n
  579. * must be length of the second substring - 1.
  580. */

  581. u_char *
  582. ngx_strlcasestrn(u_char *s1, u_char *last, u_char *s2, size_t n)
  583. {
  584.     ngx_uint_t  c1, c2;

  585.     c2 = (ngx_uint_t) *s2++;
  586.     c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
  587.     last -= n;

  588.     do {
  589.         do {
  590.             if (s1 >= last) {
  591.                 return NULL;
  592.             }

  593.             c1 = (ngx_uint_t) *s1++;

  594.             c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;

  595.         } while (c1 != c2);

  596.     } while (ngx_strncasecmp(s1, s2, n) != 0);

  597.     return --s1;
  598. }


  599. ngx_int_t
  600. ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
  601. {
  602.     if (n == 0) {
  603.         return 0;
  604.     }

  605.     n--;

  606.     for ( ;; ) {
  607.         if (s1[n] != s2[n]) {
  608.             return s1[n] - s2[n];
  609.         }

  610.         if (n == 0) {
  611.             return 0;
  612.         }

  613.         n--;
  614.     }
  615. }


  616. ngx_int_t
  617. ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
  618. {
  619.     u_char  c1, c2;

  620.     if (n == 0) {
  621.         return 0;
  622.     }

  623.     n--;

  624.     for ( ;; ) {
  625.         c1 = s1[n];
  626.         if (c1 >= 'a' && c1 <= 'z') {
  627.             c1 -= 'a' - 'A';
  628.         }

  629.         c2 = s2[n];
  630.         if (c2 >= 'a' && c2 <= 'z') {
  631.             c2 -= 'a' - 'A';
  632.         }

  633.         if (c1 != c2) {
  634.             return c1 - c2;
  635.         }

  636.         if (n == 0) {
  637.             return 0;
  638.         }

  639.         n--;
  640.     }
  641. }


  642. ngx_int_t
  643. ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2)
  644. {
  645.     size_t     n;
  646.     ngx_int_t  m, z;

  647.     if (n1 <= n2) {
  648.         n = n1;
  649.         z = -1;

  650.     } else {
  651.         n = n2;
  652.         z = 1;
  653.     }

  654.     m = ngx_memcmp(s1, s2, n);

  655.     if (m || n1 == n2) {
  656.         return m;
  657.     }

  658.     return z;
  659. }


  660. ngx_int_t
  661. ngx_dns_strcmp(u_char *s1, u_char *s2)
  662. {
  663.     ngx_uint_t  c1, c2;

  664.     for ( ;; ) {
  665.         c1 = (ngx_uint_t) *s1++;
  666.         c2 = (ngx_uint_t) *s2++;

  667.         c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
  668.         c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;

  669.         if (c1 == c2) {

  670.             if (c1) {
  671.                 continue;
  672.             }

  673.             return 0;
  674.         }

  675.         /* in ASCII '.' > '-', but we need '.' to be the lowest character */

  676.         c1 = (c1 == '.') ? ' ' : c1;
  677.         c2 = (c2 == '.') ? ' ' : c2;

  678.         return c1 - c2;
  679.     }
  680. }


  681. ngx_int_t
  682. ngx_filename_cmp(u_char *s1, u_char *s2, size_t n)
  683. {
  684.     ngx_uint_t  c1, c2;

  685.     while (n) {
  686.         c1 = (ngx_uint_t) *s1++;
  687.         c2 = (ngx_uint_t) *s2++;

  688. #if (NGX_HAVE_CASELESS_FILESYSTEM)
  689.         c1 = tolower(c1);
  690.         c2 = tolower(c2);
  691. #endif

  692.         if (c1 == c2) {

  693.             if (c1) {
  694.                 n--;
  695.                 continue;
  696.             }

  697.             return 0;
  698.         }

  699.         /* we need '/' to be the lowest character */

  700.         if (c1 == 0 || c2 == 0) {
  701.             return c1 - c2;
  702.         }

  703.         c1 = (c1 == '/') ? 0 : c1;
  704.         c2 = (c2 == '/') ? 0 : c2;

  705.         return c1 - c2;
  706.     }

  707.     return 0;
  708. }


  709. ngx_int_t
  710. ngx_atoi(u_char *line, size_t n)
  711. {
  712.     ngx_int_t  value, cutoff, cutlim;

  713.     if (n == 0) {
  714.         return NGX_ERROR;
  715.     }

  716.     cutoff = NGX_MAX_INT_T_VALUE / 10;
  717.     cutlim = NGX_MAX_INT_T_VALUE % 10;

  718.     for (value = 0; n--; line++) {
  719.         if (*line < '0' || *line > '9') {
  720.             return NGX_ERROR;
  721.         }

  722.         if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
  723.             return NGX_ERROR;
  724.         }

  725.         value = value * 10 + (*line - '0');
  726.     }

  727.     return value;
  728. }


  729. /* parse a fixed point number, e.g., ngx_atofp("10.5", 4, 2) returns 1050 */

  730. ngx_int_t
  731. ngx_atofp(u_char *line, size_t n, size_t point)
  732. {
  733.     ngx_int_t   value, cutoff, cutlim;
  734.     ngx_uint_t  dot;

  735.     if (n == 0) {
  736.         return NGX_ERROR;
  737.     }

  738.     cutoff = NGX_MAX_INT_T_VALUE / 10;
  739.     cutlim = NGX_MAX_INT_T_VALUE % 10;

  740.     dot = 0;

  741.     for (value = 0; n--; line++) {

  742.         if (point == 0) {
  743.             return NGX_ERROR;
  744.         }

  745.         if (*line == '.') {
  746.             if (dot) {
  747.                 return NGX_ERROR;
  748.             }

  749.             dot = 1;
  750.             continue;
  751.         }

  752.         if (*line < '0' || *line > '9') {
  753.             return NGX_ERROR;
  754.         }

  755.         if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
  756.             return NGX_ERROR;
  757.         }

  758.         value = value * 10 + (*line - '0');
  759.         point -= dot;
  760.     }

  761.     while (point--) {
  762.         if (value > cutoff) {
  763.             return NGX_ERROR;
  764.         }

  765.         value = value * 10;
  766.     }

  767.     return value;
  768. }


  769. ssize_t
  770. ngx_atosz(u_char *line, size_t n)
  771. {
  772.     ssize_t  value, cutoff, cutlim;

  773.     if (n == 0) {
  774.         return NGX_ERROR;
  775.     }

  776.     cutoff = NGX_MAX_SIZE_T_VALUE / 10;
  777.     cutlim = NGX_MAX_SIZE_T_VALUE % 10;

  778.     for (value = 0; n--; line++) {
  779.         if (*line < '0' || *line > '9') {
  780.             return NGX_ERROR;
  781.         }

  782.         if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
  783.             return NGX_ERROR;
  784.         }

  785.         value = value * 10 + (*line - '0');
  786.     }

  787.     return value;
  788. }


  789. off_t
  790. ngx_atoof(u_char *line, size_t n)
  791. {
  792.     off_t  value, cutoff, cutlim;

  793.     if (n == 0) {
  794.         return NGX_ERROR;
  795.     }

  796.     cutoff = NGX_MAX_OFF_T_VALUE / 10;
  797.     cutlim = NGX_MAX_OFF_T_VALUE % 10;

  798.     for (value = 0; n--; line++) {
  799.         if (*line < '0' || *line > '9') {
  800.             return NGX_ERROR;
  801.         }

  802.         if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
  803.             return NGX_ERROR;
  804.         }

  805.         value = value * 10 + (*line - '0');
  806.     }

  807.     return value;
  808. }


  809. time_t
  810. ngx_atotm(u_char *line, size_t n)
  811. {
  812.     time_t  value, cutoff, cutlim;

  813.     if (n == 0) {
  814.         return NGX_ERROR;
  815.     }

  816.     cutoff = NGX_MAX_TIME_T_VALUE / 10;
  817.     cutlim = NGX_MAX_TIME_T_VALUE % 10;

  818.     for (value = 0; n--; line++) {
  819.         if (*line < '0' || *line > '9') {
  820.             return NGX_ERROR;
  821.         }

  822.         if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
  823.             return NGX_ERROR;
  824.         }

  825.         value = value * 10 + (*line - '0');
  826.     }

  827.     return value;
  828. }


  829. ngx_int_t
  830. ngx_hextoi(u_char *line, size_t n)
  831. {
  832.     u_char     c, ch;
  833.     ngx_int_t  value, cutoff;

  834.     if (n == 0) {
  835.         return NGX_ERROR;
  836.     }

  837.     cutoff = NGX_MAX_INT_T_VALUE / 16;

  838.     for (value = 0; n--; line++) {
  839.         if (value > cutoff) {
  840.             return NGX_ERROR;
  841.         }

  842.         ch = *line;

  843.         if (ch >= '0' && ch <= '9') {
  844.             value = value * 16 + (ch - '0');
  845.             continue;
  846.         }

  847.         c = (u_char) (ch | 0x20);

  848.         if (c >= 'a' && c <= 'f') {
  849.             value = value * 16 + (c - 'a' + 10);
  850.             continue;
  851.         }

  852.         return NGX_ERROR;
  853.     }

  854.     return value;
  855. }


  856. u_char *
  857. ngx_hex_dump(u_char *dst, u_char *src, size_t len)
  858. {
  859.     static u_char  hex[] = "0123456789abcdef";

  860.     while (len--) {
  861.         *dst++ = hex[*src >> 4];
  862.         *dst++ = hex[*src++ & 0xf];
  863.     }

  864.     return dst;
  865. }


  866. void
  867. ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src)
  868. {
  869.     static u_char   basis64[] =
  870.             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  871.     ngx_encode_base64_internal(dst, src, basis64, 1);
  872. }


  873. void
  874. ngx_encode_base64url(ngx_str_t *dst, ngx_str_t *src)
  875. {
  876.     static u_char   basis64[] =
  877.             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

  878.     ngx_encode_base64_internal(dst, src, basis64, 0);
  879. }


  880. static void
  881. ngx_encode_base64_internal(ngx_str_t *dst, ngx_str_t *src, const u_char *basis,
  882.     ngx_uint_t padding)
  883. {
  884.     u_char         *d, *s;
  885.     size_t          len;

  886.     len = src->len;
  887.     s = src->data;
  888.     d = dst->data;

  889.     while (len > 2) {
  890.         *d++ = basis[(s[0] >> 2) & 0x3f];
  891.         *d++ = basis[((s[0] & 3) << 4) | (s[1] >> 4)];
  892.         *d++ = basis[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
  893.         *d++ = basis[s[2] & 0x3f];

  894.         s += 3;
  895.         len -= 3;
  896.     }

  897.     if (len) {
  898.         *d++ = basis[(s[0] >> 2) & 0x3f];

  899.         if (len == 1) {
  900.             *d++ = basis[(s[0] & 3) << 4];
  901.             if (padding) {
  902.                 *d++ = '=';
  903.             }

  904.         } else {
  905.             *d++ = basis[((s[0] & 3) << 4) | (s[1] >> 4)];
  906.             *d++ = basis[(s[1] & 0x0f) << 2];
  907.         }

  908.         if (padding) {
  909.             *d++ = '=';
  910.         }
  911.     }

  912.     dst->len = d - dst->data;
  913. }


  914. ngx_int_t
  915. ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src)
  916. {
  917.     static u_char   basis64[] = {
  918.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  919.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  920.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
  921.         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
  922.         770123456789, 10, 11, 12, 13, 14,
  923.         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,
  924.         77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  925.         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,

  926.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  927.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  928.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  929.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  930.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  931.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  932.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  933.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
  934.     };

  935.     return ngx_decode_base64_internal(dst, src, basis64);
  936. }


  937. ngx_int_t
  938. ngx_decode_base64url(ngx_str_t *dst, ngx_str_t *src)
  939. {
  940.     static u_char   basis64[] = {
  941.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  942.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  943.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77,
  944.         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
  945.         770123456789, 10, 11, 12, 13, 14,
  946.         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 63,
  947.         77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  948.         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,

  949.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  950.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  951.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  952.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  953.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  954.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  955.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
  956.         77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
  957.     };

  958.     return ngx_decode_base64_internal(dst, src, basis64);
  959. }


  960. static ngx_int_t
  961. ngx_decode_base64_internal(ngx_str_t *dst, ngx_str_t *src, const u_char *basis)
  962. {
  963.     size_t          len;
  964.     u_char         *d, *s;

  965.     for (len = 0; len < src->len; len++) {
  966.         if (src->data[len] == '=') {
  967.             break;
  968.         }

  969.         if (basis[src->data[len]] == 77) {
  970.             return NGX_ERROR;
  971.         }
  972.     }

  973.     if (len % 4 == 1) {
  974.         return NGX_ERROR;
  975.     }

  976.     s = src->data;
  977.     d = dst->data;

  978.     while (len > 3) {
  979.         *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
  980.         *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
  981.         *d++ = (u_char) (basis[s[2]] << 6 | basis[s[3]]);

  982.         s += 4;
  983.         len -= 4;
  984.     }

  985.     if (len > 1) {
  986.         *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
  987.     }

  988.     if (len > 2) {
  989.         *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
  990.     }

  991.     dst->len = d - dst->data;

  992.     return NGX_OK;
  993. }


  994. /*
  995. * ngx_utf8_decode() decodes two and more bytes UTF sequences only
  996. * the return values:
  997. *    0x80 - 0x10ffff         valid character
  998. *    0x110000 - 0xfffffffd   invalid sequence
  999. *    0xfffffffe              incomplete sequence
  1000. *    0xffffffff              error
  1001. */

  1002. uint32_t
  1003. ngx_utf8_decode(u_char **p, size_t n)
  1004. {
  1005.     size_t    len;
  1006.     uint32_t  u, i, valid;

  1007.     u = **p;

  1008.     if (u >= 0xf8) {

  1009.         (*p)++;
  1010.         return 0xffffffff;

  1011.     } else if (u >= 0xf0) {

  1012.         u &= 0x07;
  1013.         valid = 0xffff;
  1014.         len = 3;

  1015.     } else if (u >= 0xe0) {

  1016.         u &= 0x0f;
  1017.         valid = 0x7ff;
  1018.         len = 2;

  1019.     } else if (u >= 0xc2) {

  1020.         u &= 0x1f;
  1021.         valid = 0x7f;
  1022.         len = 1;

  1023.     } else {
  1024.         (*p)++;
  1025.         return 0xffffffff;
  1026.     }

  1027.     if (n - 1 < len) {
  1028.         return 0xfffffffe;
  1029.     }

  1030.     (*p)++;

  1031.     while (len) {
  1032.         i = *(*p)++;

  1033.         if (i < 0x80) {
  1034.             return 0xffffffff;
  1035.         }

  1036.         u = (u << 6) | (i & 0x3f);

  1037.         len--;
  1038.     }

  1039.     if (u > valid) {
  1040.         return u;
  1041.     }

  1042.     return 0xffffffff;
  1043. }


  1044. size_t
  1045. ngx_utf8_length(u_char *p, size_t n)
  1046. {
  1047.     u_char  c, *last;
  1048.     size_t  len;

  1049.     last = p + n;

  1050.     for (len = 0; p < last; len++) {

  1051.         c = *p;

  1052.         if (c < 0x80) {
  1053.             p++;
  1054.             continue;
  1055.         }

  1056.         if (ngx_utf8_decode(&p, last - p) > 0x10ffff) {
  1057.             /* invalid UTF-8 */
  1058.             return n;
  1059.         }
  1060.     }

  1061.     return len;
  1062. }


  1063. u_char *
  1064. ngx_utf8_cpystrn(u_char *dst, u_char *src, size_t n, size_t len)
  1065. {
  1066.     u_char  c, *next;

  1067.     if (n == 0) {
  1068.         return dst;
  1069.     }

  1070.     while (--n) {

  1071.         c = *src;
  1072.         *dst = c;

  1073.         if (c < 0x80) {

  1074.             if (c != '\0') {
  1075.                 dst++;
  1076.                 src++;
  1077.                 len--;

  1078.                 continue;
  1079.             }

  1080.             return dst;
  1081.         }

  1082.         next = src;

  1083.         if (ngx_utf8_decode(&next, len) > 0x10ffff) {
  1084.             /* invalid UTF-8 */
  1085.             break;
  1086.         }

  1087.         while (src < next) {
  1088.             *dst++ = *src++;
  1089.             len--;
  1090.         }
  1091.     }

  1092.     *dst = '\0';

  1093.     return dst;
  1094. }


  1095. uintptr_t
  1096. ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
  1097. {
  1098.     ngx_uint_t      n;
  1099.     uint32_t       *escape;
  1100.     static u_char   hex[] = "0123456789ABCDEF";

  1101.     /*
  1102.      * Per RFC 3986 only the following chars are allowed in URIs unescaped:
  1103.      *
  1104.      * unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
  1105.      * gen-delims    = ":" / "/" / "?" / "#" / "[" / "]" / "@"
  1106.      * sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
  1107.      *               / "*" / "+" / "," / ";" / "="
  1108.      *
  1109.      * And "%" can appear as a part of escaping itself.  The following
  1110.      * characters are not allowed and need to be escaped: %00-%1F, %7F-%FF,
  1111.      * " ", """, "<", ">", "\", "^", "`", "{", "|", "}".
  1112.      */

  1113.                     /* " ", "#", "%", "?", not allowed */

  1114.     static uint32_t   uri[] = {
  1115.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

  1116.                     /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
  1117.         0xd000002d, /* 1101 0000 0000 0000  0000 0000 0010 1101 */

  1118.                     /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
  1119.         0x50000000, /* 0101 0000 0000 0000  0000 0000 0000 0000 */

  1120.                     /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
  1121.         0xb8000001, /* 1011 1000 0000 0000  0000 0000 0000 0001 */

  1122.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1123.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1124.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1125.         0xffffffff  /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1126.     };

  1127.                     /* " ", "#", "%", "&", "+", ";", "?", not allowed */

  1128.     static uint32_t   args[] = {
  1129.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

  1130.                     /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
  1131.         0xd800086d, /* 1101 1000 0000 0000  0000 1000 0110 1101 */

  1132.                     /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
  1133.         0x50000000, /* 0101 0000 0000 0000  0000 0000 0000 0000 */

  1134.                     /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
  1135.         0xb8000001, /* 1011 1000 0000 0000  0000 0000 0000 0001 */

  1136.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1137.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1138.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1139.         0xffffffff  /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1140.     };

  1141.                     /* not ALPHA, DIGIT, "-", ".", "_", "~" */

  1142.     static uint32_t   uri_component[] = {
  1143.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

  1144.                     /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
  1145.         0xfc009fff, /* 1111 1100 0000 0000  1001 1111 1111 1111 */

  1146.                     /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
  1147.         0x78000001, /* 0111 1000 0000 0000  0000 0000 0000 0001 */

  1148.                     /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
  1149.         0xb8000001, /* 1011 1000 0000 0000  0000 0000 0000 0001 */

  1150.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1151.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1152.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1153.         0xffffffff  /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1154.     };

  1155.                     /* " ", "#", """, "%", "'", not allowed */

  1156.     static uint32_t   html[] = {
  1157.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

  1158.                     /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
  1159.         0x500000ad, /* 0101 0000 0000 0000  0000 0000 1010 1101 */

  1160.                     /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
  1161.         0x50000000, /* 0101 0000 0000 0000  0000 0000 0000 0000 */

  1162.                     /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
  1163.         0xb8000001, /* 1011 1000 0000 0000  0000 0000 0000 0001 */

  1164.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1165.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1166.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1167.         0xffffffff  /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1168.     };

  1169.                     /* " ", """, "'", not allowed */

  1170.     static uint32_t   refresh[] = {
  1171.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

  1172.                     /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
  1173.         0x50000085, /* 0101 0000 0000 0000  0000 0000 1000 0101 */

  1174.                     /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
  1175.         0x50000000, /* 0101 0000 0000 0000  0000 0000 0000 0000 */

  1176.                     /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
  1177.         0xd8000001, /* 1011 1000 0000 0000  0000 0000 0000 0001 */

  1178.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1179.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1180.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1181.         0xffffffff  /* 1111 1111 1111 1111  1111 1111 1111 1111 */
  1182.     };

  1183.                     /* " ", "%", %00-%1F */

  1184.     static uint32_t   memcached[] = {
  1185.         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

  1186.                     /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
  1187.         0x00000021, /* 0000 0000 0000 0000  0000 0000 0010 0001 */

  1188.                     /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
  1189.         0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */

  1190.                     /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
  1191.         0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */

  1192.         0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */
  1193.         0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */
  1194.         0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */
  1195.         0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */
  1196.     };

  1197.                     /* mail_auth is the same as memcached */

  1198.     static uint32_t  *map[] =
  1199.         { uri, args, uri_component, html, refresh, memcached, memcached };


  1200.     escape = map[type];

  1201.     if (dst == NULL) {

  1202.         /* find the number of the characters to be escaped */

  1203.         n = 0;

  1204.         while (size) {
  1205.             if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
  1206.                 n++;
  1207.             }
  1208.             src++;
  1209.             size--;
  1210.         }

  1211.         return (uintptr_t) n;
  1212.     }

  1213.     while (size) {
  1214.         if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
  1215.             *dst++ = '%';
  1216.             *dst++ = hex[*src >> 4];
  1217.             *dst++ = hex[*src & 0xf];
  1218.             src++;

  1219.         } else {
  1220.             *dst++ = *src++;
  1221.         }
  1222.         size--;
  1223.     }

  1224.     return (uintptr_t) dst;
  1225. }


  1226. void
  1227. ngx_unescape_uri(u_char **dst, u_char **src, size_t size, ngx_uint_t type)
  1228. {
  1229.     u_char  *d, *s, ch, c, decoded;
  1230.     enum {
  1231.         sw_usual = 0,
  1232.         sw_quoted,
  1233.         sw_quoted_second
  1234.     } state;

  1235.     d = *dst;
  1236.     s = *src;

  1237.     state = 0;
  1238.     decoded = 0;

  1239.     while (size--) {

  1240.         ch = *s++;

  1241.         switch (state) {
  1242.         case sw_usual:
  1243.             if (ch == '?'
  1244.                 && (type & (NGX_UNESCAPE_URI|NGX_UNESCAPE_REDIRECT)))
  1245.             {
  1246.                 *d++ = ch;
  1247.                 goto done;
  1248.             }

  1249.             if (ch == '%') {
  1250.                 state = sw_quoted;
  1251.                 break;
  1252.             }

  1253.             *d++ = ch;
  1254.             break;

  1255.         case sw_quoted:

  1256.             if (ch >= '0' && ch <= '9') {
  1257.                 decoded = (u_char) (ch - '0');
  1258.                 state = sw_quoted_second;
  1259.                 break;
  1260.             }

  1261.             c = (u_char) (ch | 0x20);
  1262.             if (c >= 'a' && c <= 'f') {
  1263.                 decoded = (u_char) (c - 'a' + 10);
  1264.                 state = sw_quoted_second;
  1265.                 break;
  1266.             }

  1267.             /* the invalid quoted character */

  1268.             state = sw_usual;

  1269.             *d++ = ch;

  1270.             break;

  1271.         case sw_quoted_second:

  1272.             state = sw_usual;

  1273.             if (ch >= '0' && ch <= '9') {
  1274.                 ch = (u_char) ((decoded << 4) + (ch - '0'));

  1275.                 if (type & NGX_UNESCAPE_REDIRECT) {
  1276.                     if (ch > '%' && ch < 0x7f) {
  1277.                         *d++ = ch;
  1278.                         break;
  1279.                     }

  1280.                     *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);

  1281.                     break;
  1282.                 }

  1283.                 *d++ = ch;

  1284.                 break;
  1285.             }

  1286.             c = (u_char) (ch | 0x20);
  1287.             if (c >= 'a' && c <= 'f') {
  1288.                 ch = (u_char) ((decoded << 4) + (c - 'a') + 10);

  1289.                 if (type & NGX_UNESCAPE_URI) {
  1290.                     if (ch == '?') {
  1291.                         *d++ = ch;
  1292.                         goto done;
  1293.                     }

  1294.                     *d++ = ch;
  1295.                     break;
  1296.                 }

  1297.                 if (type & NGX_UNESCAPE_REDIRECT) {
  1298.                     if (ch == '?') {
  1299.                         *d++ = ch;
  1300.                         goto done;
  1301.                     }

  1302.                     if (ch > '%' && ch < 0x7f) {
  1303.                         *d++ = ch;
  1304.                         break;
  1305.                     }

  1306.                     *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
  1307.                     break;
  1308.                 }

  1309.                 *d++ = ch;

  1310.                 break;
  1311.             }

  1312.             /* the invalid quoted character */

  1313.             break;
  1314.         }
  1315.     }

  1316. done:

  1317.     *dst = d;
  1318.     *src = s;
  1319. }


  1320. uintptr_t
  1321. ngx_escape_html(u_char *dst, u_char *src, size_t size)
  1322. {
  1323.     u_char      ch;
  1324.     ngx_uint_t  len;

  1325.     if (dst == NULL) {

  1326.         len = 0;

  1327.         while (size) {
  1328.             switch (*src++) {

  1329.             case '<':
  1330.                 len += sizeof("&lt;") - 2;
  1331.                 break;

  1332.             case '>':
  1333.                 len += sizeof("&gt;") - 2;
  1334.                 break;

  1335.             case '&':
  1336.                 len += sizeof("&amp;") - 2;
  1337.                 break;

  1338.             case '"':
  1339.                 len += sizeof("&quot;") - 2;
  1340.                 break;

  1341.             default:
  1342.                 break;
  1343.             }
  1344.             size--;
  1345.         }

  1346.         return (uintptr_t) len;
  1347.     }

  1348.     while (size) {
  1349.         ch = *src++;

  1350.         switch (ch) {

  1351.         case '<':
  1352.             *dst++ = '&'; *dst++ = 'l'; *dst++ = 't'; *dst++ = ';';
  1353.             break;

  1354.         case '>':
  1355.             *dst++ = '&'; *dst++ = 'g'; *dst++ = 't'; *dst++ = ';';
  1356.             break;

  1357.         case '&':
  1358.             *dst++ = '&'; *dst++ = 'a'; *dst++ = 'm'; *dst++ = 'p';
  1359.             *dst++ = ';';
  1360.             break;

  1361.         case '"':
  1362.             *dst++ = '&'; *dst++ = 'q'; *dst++ = 'u'; *dst++ = 'o';
  1363.             *dst++ = 't'; *dst++ = ';';
  1364.             break;

  1365.         default:
  1366.             *dst++ = ch;
  1367.             break;
  1368.         }
  1369.         size--;
  1370.     }

  1371.     return (uintptr_t) dst;
  1372. }


  1373. uintptr_t
  1374. ngx_escape_json(u_char *dst, u_char *src, size_t size)
  1375. {
  1376.     u_char      ch;
  1377.     ngx_uint_t  len;

  1378.     if (dst == NULL) {
  1379.         len = 0;

  1380.         while (size) {
  1381.             ch = *src++;

  1382.             if (ch == '\\' || ch == '"') {
  1383.                 len++;

  1384.             } else if (ch <= 0x1f) {

  1385.                 switch (ch) {
  1386.                 case '\n':
  1387.                 case '\r':
  1388.                 case '\t':
  1389.                 case '\b':
  1390.                 case '\f':
  1391.                     len++;
  1392.                     break;

  1393.                 default:
  1394.                     len += sizeof("\\u001F") - 2;
  1395.                 }
  1396.             }

  1397.             size--;
  1398.         }

  1399.         return (uintptr_t) len;
  1400.     }

  1401.     while (size) {
  1402.         ch = *src++;

  1403.         if (ch > 0x1f) {

  1404.             if (ch == '\\' || ch == '"') {
  1405.                 *dst++ = '\\';
  1406.             }

  1407.             *dst++ = ch;

  1408.         } else {
  1409.             *dst++ = '\\';

  1410.             switch (ch) {
  1411.             case '\n':
  1412.                 *dst++ = 'n';
  1413.                 break;

  1414.             case '\r':
  1415.                 *dst++ = 'r';
  1416.                 break;

  1417.             case '\t':
  1418.                 *dst++ = 't';
  1419.                 break;

  1420.             case '\b':
  1421.                 *dst++ = 'b';
  1422.                 break;

  1423.             case '\f':
  1424.                 *dst++ = 'f';
  1425.                 break;

  1426.             default:
  1427.                 *dst++ = 'u'; *dst++ = '0'; *dst++ = '0';
  1428.                 *dst++ = '0' + (ch >> 4);

  1429.                 ch &= 0xf;

  1430.                 *dst++ = (ch < 10) ? ('0' + ch) : ('A' + ch - 10);
  1431.             }
  1432.         }

  1433.         size--;
  1434.     }

  1435.     return (uintptr_t) dst;
  1436. }


  1437. void
  1438. ngx_str_rbtree_insert_value(ngx_rbtree_node_t *temp,
  1439.     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
  1440. {
  1441.     ngx_str_node_t      *n, *t;
  1442.     ngx_rbtree_node_t  **p;

  1443.     for ( ;; ) {

  1444.         n = (ngx_str_node_t *) node;
  1445.         t = (ngx_str_node_t *) temp;

  1446.         if (node->key != temp->key) {

  1447.             p = (node->key < temp->key) ? &temp->left : &temp->right;

  1448.         } else if (n->str.len != t->str.len) {

  1449.             p = (n->str.len < t->str.len) ? &temp->left : &temp->right;

  1450.         } else {
  1451.             p = (ngx_memcmp(n->str.data, t->str.data, n->str.len) < 0)
  1452.                  ? &temp->left : &temp->right;
  1453.         }

  1454.         if (*p == sentinel) {
  1455.             break;
  1456.         }

  1457.         temp = *p;
  1458.     }

  1459.     *p = node;
  1460.     node->parent = temp;
  1461.     node->left = sentinel;
  1462.     node->right = sentinel;
  1463.     ngx_rbt_red(node);
  1464. }


  1465. ngx_str_node_t *
  1466. ngx_str_rbtree_lookup(ngx_rbtree_t *rbtree, ngx_str_t *val, uint32_t hash)
  1467. {
  1468.     ngx_int_t           rc;
  1469.     ngx_str_node_t     *n;
  1470.     ngx_rbtree_node_t  *node, *sentinel;

  1471.     node = rbtree->root;
  1472.     sentinel = rbtree->sentinel;

  1473.     while (node != sentinel) {

  1474.         n = (ngx_str_node_t *) node;

  1475.         if (hash != node->key) {
  1476.             node = (hash < node->key) ? node->left : node->right;
  1477.             continue;
  1478.         }

  1479.         if (val->len != n->str.len) {
  1480.             node = (val->len < n->str.len) ? node->left : node->right;
  1481.             continue;
  1482.         }

  1483.         rc = ngx_memcmp(val->data, n->str.data, val->len);

  1484.         if (rc < 0) {
  1485.             node = node->left;
  1486.             continue;
  1487.         }

  1488.         if (rc > 0) {
  1489.             node = node->right;
  1490.             continue;
  1491.         }

  1492.         return n;
  1493.     }

  1494.     return NULL;
  1495. }


  1496. /* ngx_sort() is implemented as insertion sort because we need stable sort */

  1497. void
  1498. ngx_sort(void *base, size_t n, size_t size,
  1499.     ngx_int_t (*cmp)(const void *, const void *))
  1500. {
  1501.     u_char  *p1, *p2, *p;

  1502.     p = ngx_alloc(size, ngx_cycle->log);
  1503.     if (p == NULL) {
  1504.         return;
  1505.     }

  1506.     for (p1 = (u_char *) base + size;
  1507.          p1 < (u_char *) base + n * size;
  1508.          p1 += size)
  1509.     {
  1510.         ngx_memcpy(p, p1, size);

  1511.         for (p2 = p1;
  1512.              p2 > (u_char *) base && cmp(p2 - size, p) > 0;
  1513.              p2 -= size)
  1514.         {
  1515.             ngx_memcpy(p2, p2 - size, size);
  1516.         }

  1517.         ngx_memcpy(p2, p, size);
  1518.     }

  1519.     ngx_free(p);
  1520. }


  1521. void
  1522. ngx_explicit_memzero(void *buf, size_t n)
  1523. {
  1524.     ngx_memzero(buf, n);
  1525.     ngx_memory_barrier();
  1526. }


  1527. #if (NGX_MEMCPY_LIMIT)

  1528. void *
  1529. ngx_memcpy(void *dst, const void *src, size_t n)
  1530. {
  1531.     if (n > NGX_MEMCPY_LIMIT) {
  1532.         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, "memcpy %uz bytes", n);
  1533.         ngx_debug_point();
  1534.     }

  1535.     return memcpy(dst, src, n);
  1536. }

  1537. #endif