1 /****************************************************************************
2 *
3 * Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
4 * Copyright (C) 2003-2013 Sourcefire, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 as
8 * published by the Free Software Foundation. You may not use, modify or
9 * distribute this program under any other version of the GNU General
10 * Public License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 ****************************************************************************/
22
23 /**
24 ** @file hi_util.h
25 **
26 ** @author Daniel Roelker <droelker@sourcefire.com>
27 **
28 ** @brief HttpInspect utility functions.
29 **
30 ** Contains function prototype and inline utility functions.
31 **
32 ** NOTES:
33 ** - Initial development. DJR
34 */
35
36 #ifndef __HI_UTIL_H__
37 #define __HI_UTIL_H__
38
39 #include <string.h>
40 #include "hi_include.h"
41
42 /*
43 ** NAME
44 ** hi_util_in_bounds::
45 */
46 /**
47 ** This function checks for in bounds condition on buffers.
48 **
49 ** This is very important for much of what we do here, since inspecting
50 ** data buffers is mainly what we do. So we always make sure that we are
51 ** within the buffer.
52 **
53 ** This checks a half-open interval with the end pointer being one char
54 ** after the end of the buffer.
55 **
56 ** @param start the start of the buffer.
57 ** @param end the end of the buffer.
58 ** @param p the pointer within the buffer
59 **
60 ** @return integer
61 **
62 ** @retval 1 within bounds
63 ** @retval 0 not within bounds
64 */
hi_util_in_bounds(const u_char * start,const u_char * end,const u_char * p)65 static inline int hi_util_in_bounds(const u_char *start, const u_char *end, const u_char *p)
66 {
67 if(p >= start && p < end)
68 {
69 return 1;
70 }
71
72 return 0;
73 }
74
SkipWhiteSpace(const u_char * start,const u_char * end,const u_char ** ptr)75 static inline void SkipWhiteSpace(const u_char *start, const u_char *end,
76 const u_char **ptr)
77 {
78 while (hi_util_in_bounds(start, end, *ptr) && isspace((int)**ptr) && (**ptr != '\n'))
79 (*ptr)++;
80 }
81 #if defined(FEAT_OPEN_APPID)
SkipBlankColon(const u_char * start,const u_char * end,const u_char ** ptr)82 static inline int SkipBlankColon(const u_char *start, const u_char *end,
83 const u_char **ptr)
84 {
85 int count = 0;
86 while((hi_util_in_bounds(start, end, *ptr)) && ( **ptr == ' ' || **ptr == '\t' || **ptr == ':') )
87 {
88 (*ptr)++;
89 count++;
90 }
91 return count;
92 }
93 #endif /* defined(FEAT_OPEN_APPID) */
SkipBlankSpace(const u_char * start,const u_char * end,const u_char ** ptr)94 static inline int SkipBlankSpace(const u_char *start, const u_char *end,
95 const u_char **ptr)
96 {
97 int count = 0;
98 while((hi_util_in_bounds(start, end, *ptr)) && ( **ptr == ' ' || **ptr == '\t') )
99 {
100 (*ptr)++;
101 count++;
102 }
103 return count;
104 }
SkipDigits(const u_char * start,const u_char * end,const u_char ** ptr)105 static inline void SkipDigits(const u_char *start, const u_char *end,
106 const u_char **ptr)
107 {
108 while((hi_util_in_bounds(start, end, *ptr)) && (isdigit((int)**ptr)) ) {(*ptr)++;}
109 }
110
SkipBlankAndNewLine(const u_char * start,const u_char * end,const u_char ** ptr)111 static inline void SkipBlankAndNewLine(const u_char *start, const u_char *end,
112 const u_char **ptr)
113 {
114 while( (hi_util_in_bounds(start, end, *ptr)) &&
115 ( **ptr == ' ' || **ptr == '\t') && (**ptr != '\n') ) {(*ptr)++;}
116 }
117
SkipCRLF(const u_char * start,const u_char * end,const u_char ** ptr)118 static inline void SkipCRLF(const u_char *start, const u_char *end,
119 const u_char **ptr)
120 {
121 while( (hi_util_in_bounds(start, end, *ptr)) &&
122 ( **ptr == '\r' || **ptr == '\n') ) {(*ptr)++;}
123 }
124
125
IsHeaderFieldName(const u_char * p,const u_char * end,const char * header_name,size_t header_len)126 static inline int IsHeaderFieldName(const u_char *p, const u_char *end,
127 const char *header_name, size_t header_len)
128 {
129 if ((p+header_len) <= end)
130 {
131 if(!strncasecmp((const char *)p, header_name, header_len))
132 return 1;
133 else
134 return 0;
135 }
136 return 0;
137 }
138
139 #endif /* __HI_UTIL_H__ */
140
141