blob: 670063915f7b925a7f092f0d1f138d041bfdf593 [file] [log] [blame]
Igor Sysoevd90282d2004-09-28 08:34:51 +00001
2/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00007#ifndef _NGX_STRING_H_INCLUDED_
8#define _NGX_STRING_H_INCLUDED_
9
10
11#include <ngx_config.h>
Igor Sysoev1c104622003-06-03 15:42:58 +000012#include <ngx_core.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000013
14
Igor Sysoeva0bb31f2002-12-02 16:09:40 +000015typedef struct {
Igor Sysoev3338cfd2006-05-11 14:43:47 +000016 size_t len;
17 u_char *data;
Igor Sysoeva0bb31f2002-12-02 16:09:40 +000018} ngx_str_t;
19
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000020
Igor Sysoev3338cfd2006-05-11 14:43:47 +000021typedef struct {
22 ngx_str_t key;
23 ngx_str_t value;
24} ngx_keyval_t;
25
26
Igor Sysoev10a543a2004-03-16 07:10:12 +000027#define ngx_string(str) { sizeof(str) - 1, (u_char *) str }
Igor Sysoevb7387572003-03-11 20:38:13 +000028#define ngx_null_string { 0, NULL }
Igor Sysoev960ffa42002-12-26 07:24:21 +000029
30
Igor Sysoevb1dfe472004-12-21 12:30:30 +000031#define ngx_tolower(c) (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
Igor Sysoevc1571722005-03-19 12:38:37 +000032#define ngx_toupper(c) (u_char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
Igor Sysoevb1dfe472004-12-21 12:30:30 +000033
34
Igor Sysoev1b735832004-11-11 14:07:14 +000035#if (NGX_WIN32)
Igor Sysoev42feecb2002-12-15 06:25:09 +000036
Igor Sysoevda85f7f2004-03-16 21:26:01 +000037#define ngx_strncasecmp(s1, s2, n) \
Igor Sysoevd90282d2004-09-28 08:34:51 +000038 strnicmp((const char *) s1, (const char *) s2, n)
Igor Sysoevda85f7f2004-03-16 21:26:01 +000039#define ngx_strcasecmp(s1, s2) \
Igor Sysoevd90282d2004-09-28 08:34:51 +000040 stricmp((const char *) s1, (const char *) s2)
Igor Sysoevad22e012003-01-15 07:02:27 +000041
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000042#else
43
Igor Sysoev10a543a2004-03-16 07:10:12 +000044#define ngx_strncasecmp(s1, s2, n) \
Igor Sysoevd90282d2004-09-28 08:34:51 +000045 strncasecmp((const char *) s1, (const char *) s2, n)
Igor Sysoev10a543a2004-03-16 07:10:12 +000046#define ngx_strcasecmp(s1, s2) \
Igor Sysoevd90282d2004-09-28 08:34:51 +000047 strcasecmp((const char *) s1, (const char *) s2)
Igor Sysoevad22e012003-01-15 07:02:27 +000048
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000049#endif
50
Igor Sysoev3646a162004-03-14 20:46:25 +000051
Igor Sysoev1b735832004-11-11 14:07:14 +000052#define ngx_strncmp(s1, s2, n) strncmp((const char *) s1, (const char *) s2, n)
53
Igor Sysoev3646a162004-03-14 20:46:25 +000054
Igor Sysoev09c684b2005-11-09 17:25:55 +000055/* msvc and icc7 compile strcmp() to inline loop */
Igor Sysoevd90282d2004-09-28 08:34:51 +000056#define ngx_strcmp(s1, s2) strcmp((const char *) s1, (const char *) s2)
Igor Sysoev3646a162004-03-14 20:46:25 +000057
Igor Sysoev1b735832004-11-11 14:07:14 +000058
Igor Sysoevd90282d2004-09-28 08:34:51 +000059#define ngx_strstr(s1, s2) strstr((const char *) s1, (const char *) s2)
60#define ngx_strlen(s) strlen((const char *) s)
Igor Sysoev3646a162004-03-14 20:46:25 +000061
Igor Sysoev1b735832004-11-11 14:07:14 +000062
Igor Sysoev6abfde62003-07-01 15:00:03 +000063/*
Igor Sysoev09c684b2005-11-09 17:25:55 +000064 * msvc and icc7 compile memset() to the inline "rep stos"
Igor Sysoevd90282d2004-09-28 08:34:51 +000065 * while ZeroMemory() and bzero() are the calls.
Igor Sysoev09c684b2005-11-09 17:25:55 +000066 * icc7 may also inline several mov's of a zeroed register for small blocks.
Igor Sysoev6abfde62003-07-01 15:00:03 +000067 */
Igor Sysoeve31e90b2005-05-19 13:25:22 +000068#define ngx_memzero(buf, n) (void) memset(buf, 0, n)
69#define ngx_memset(buf, c, n) (void) memset(buf, c, n)
Igor Sysoev6abfde62003-07-01 15:00:03 +000070
Igor Sysoev1b735832004-11-11 14:07:14 +000071
Igor Sysoevd3283ff2005-12-05 13:18:09 +000072#if (NGX_MEMCPY_LIMIT)
73
74void *ngx_memcpy(void *dst, void *src, size_t n);
75#define ngx_cpymem(dst, src, n) ((u_char *) ngx_memcpy(dst, src, n)) + (n)
76
77#else
78
Igor Sysoev09c684b2005-11-09 17:25:55 +000079/*
80 * gcc3, msvc, and icc7 compile memcpy() to the inline "rep movs".
81 * gcc3 compiles memcpy(d, s, 4) to the inline "mov"es.
82 * icc8 compile memcpy(d, s, 4) to the inline "mov"es or XMM moves.
83 */
Igor Sysoeve31e90b2005-05-19 13:25:22 +000084#define ngx_memcpy(dst, src, n) (void) memcpy(dst, src, n)
Igor Sysoev02025fd2005-01-18 13:03:58 +000085#define ngx_cpymem(dst, src, n) ((u_char *) memcpy(dst, src, n)) + (n)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000086
Igor Sysoevd3283ff2005-12-05 13:18:09 +000087#endif
88
Igor Sysoev1b735832004-11-11 14:07:14 +000089
Igor Sysoev09c684b2005-11-09 17:25:55 +000090#if ( __INTEL_COMPILER >= 800 )
91
92/*
93 * the simple inline cycle copies the variable length strings up to 16
94 * bytes faster than icc8 autodetecting _intel_fast_memcpy()
95 */
96
97static ngx_inline u_char *
98ngx_copy(u_char *dst, u_char *src, size_t len)
99{
100 if (len < 17) {
101
102 while (len) {
103 *dst++ = *src++;
104 len--;
105 }
106
107 return dst;
108
109 } else {
110 return ngx_cpymem(dst, src, len);
111 }
112}
113
114#else
115
116#define ngx_copy ngx_cpymem
117
118#endif
119
120
121/* msvc and icc7 compile memcmp() to the inline loop */
Igor Sysoev6abfde62003-07-01 15:00:03 +0000122#define ngx_memcmp memcmp
123
Igor Sysoev1b735832004-11-11 14:07:14 +0000124
Igor Sysoev10a543a2004-03-16 07:10:12 +0000125u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n);
Igor Sysoev805d9db2005-02-03 19:33:37 +0000126u_char *ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src);
Igor Sysoev4d656dc2005-03-22 16:02:46 +0000127u_char * ngx_cdecl ngx_sprintf(u_char *buf, const char *fmt, ...);
128u_char * ngx_cdecl ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...);
Igor Sysoev1b735832004-11-11 14:07:14 +0000129u_char *ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args);
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000130
Igor Sysoev10a543a2004-03-16 07:10:12 +0000131ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n);
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000132ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n);
Igor Sysoevec3cabd2007-01-12 21:58:02 +0000133ngx_int_t ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2);
Igor Sysoev18684bd2004-05-20 17:33:52 +0000134
Igor Sysoev10a543a2004-03-16 07:10:12 +0000135ngx_int_t ngx_atoi(u_char *line, size_t n);
Igor Sysoevc1571722005-03-19 12:38:37 +0000136ssize_t ngx_atosz(u_char *line, size_t n);
137off_t ngx_atoof(u_char *line, size_t n);
138time_t ngx_atotm(u_char *line, size_t n);
Igor Sysoev18684bd2004-05-20 17:33:52 +0000139ngx_int_t ngx_hextoi(u_char *line, size_t n);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000140
Igor Sysoev10a543a2004-03-16 07:10:12 +0000141void ngx_md5_text(u_char *text, u_char *md5);
Igor Sysoev9cc1ace2003-11-04 22:12:39 +0000142
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000143
144#define ngx_base64_encoded_length(len) (((len + 2) / 3) * 4)
145#define ngx_base64_decoded_length(len) (((len + 3) / 4) * 3)
146
Igor Sysoev924bd792004-10-11 15:07:03 +0000147void ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src);
148ngx_int_t ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src);
Igor Sysoev1b735832004-11-11 14:07:14 +0000149
Igor Sysoevef809b82006-06-28 16:00:26 +0000150uint32_t ngx_utf_decode(u_char **p, size_t n);
151size_t ngx_utf_length(u_char *p, size_t n);
Igor Sysoev5192b362005-07-08 14:34:20 +0000152u_char * ngx_utf_cpystrn(u_char *dst, u_char *src, size_t n);
153
Igor Sysoev1b735832004-11-11 14:07:14 +0000154
Igor Sysoevae33d012006-01-17 20:04:32 +0000155#define NGX_ESCAPE_URI 0
156#define NGX_ESCAPE_ARGS 1
157#define NGX_ESCAPE_HTML 2
158
159#define NGX_UNESCAPE_URI 1
Igor Sysoev1b735832004-11-11 14:07:14 +0000160
Igor Sysoev805d9db2005-02-03 19:33:37 +0000161uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size,
Igor Sysoevd039a2e2005-02-22 14:40:13 +0000162 ngx_uint_t type);
Igor Sysoevae33d012006-01-17 20:04:32 +0000163void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, ngx_uint_t type);
Igor Sysoev967fd632004-08-27 15:40:59 +0000164
Igor Sysoev9cc1ace2003-11-04 22:12:39 +0000165
Igor Sysoev13933252003-05-29 13:02:09 +0000166#define ngx_qsort qsort
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000167
Igor Sysoev13933252003-05-29 13:02:09 +0000168
169#define ngx_value_helper(n) #n
170#define ngx_value(n) ngx_value_helper(n)
Igor Sysoeva9830112003-05-19 16:39:14 +0000171
172
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000173#endif /* _NGX_STRING_H_INCLUDED_ */