summaryrefslogtreecommitdiff
path: root/scan_urlencoded_query.c
diff options
context:
space:
mode:
Diffstat (limited to 'scan_urlencoded_query.c')
-rw-r--r--scan_urlencoded_query.c32
1 files changed, 12 insertions, 20 deletions
diff --git a/scan_urlencoded_query.c b/scan_urlencoded_query.c
index 7aeabab..6ba7808 100644
--- a/scan_urlencoded_query.c
+++ b/scan_urlencoded_query.c
@@ -1,13 +1,5 @@
1#include "scan.h" 1#include "scan.h"
2 2#include "scan_urlencoded_query.h"
3#define BREAK_AT_QUESTIONMARK (1<<0)
4#define BREAK_AT_WHITESPACE (1<<1)
5#define BREAK_AT_AMPERSAND (1<<2)
6#define BREAK_AT_EQUALSIGN (1<<3)
7
8#define SCAN_PATH ( BREAK_AT_QUESTIONMARK | BREAK_AT_WHITESPACE )
9#define SCAN_SEARCHPATH_PARAM ( BREAK_AT_EQUALSIGN )
10#define SCAN_SEARCHPATH_VALUE ( BREAK_AT_AMPERSAND | BREAK_AT_WHITESPACE )
11 3
12// Idea is to do a in place replacement or guarantee at least 4// Idea is to do a in place replacement or guarantee at least
13// strlen( string ) bytes in deststring 5// strlen( string ) bytes in deststring
@@ -17,19 +9,19 @@
17// we add '%' to the matrix to not stop at encoded chars. 9// we add '%' to the matrix to not stop at encoded chars.
18 10
19static const unsigned char reserved_matrix[] = { 0xA2, 0x63, 0xFF, 0x03, 0xFE, 0xFF, 0xFF, 0x87, 0xFE, 0xFF, 0xFF, 0x47}; 11static const unsigned char reserved_matrix[] = { 0xA2, 0x63, 0xFF, 0x03, 0xFE, 0xFF, 0xFF, 0x87, 0xFE, 0xFF, 0xFF, 0x47};
20inline int is_unreserved( unsigned char c ) const { 12inline int is_unreserved( unsigned char c ) {
21 if( ( c <= 32 ) || ( c >= 127 ) ) return 0; return 1&(reserved_matrix[(c-32)>>3]>>(c&7)); 13 if( ( c <= 32 ) || ( c >= 127 ) ) return 0; return 1&(reserved_matrix[(c-32)>>3]>>(c&7));
22} 14}
23 15
24size_t scan_urlencoded_query(char **string, char *deststring, int flags) { 16size_t scan_urlencoded_query(char **string, char *deststring, int flags) {
25 register const unsigned char* s=*(const unsigned char*) string; 17 register const unsigned char* s=*(const unsigned char**) string;
26 const unsigned char *d = deststring; 18 unsigned char *d = (unsigned char*)deststring;
27 register unsigned char b, c; 19 register unsigned char b, c;
28 20
29 while ( is_unreserved( c = *s++) ) { 21 while ( is_unreserved( c = *s++) ) {
30 if (c=='%') { 22 if (c=='%') {
31 if( ( c = scan_fromhex(*s++) ) < 0 ) return -1; 23 if( ( c = scan_fromhex(*s++) ) == 0xff ) return -1;
32 if( ( b = scan_fromhex(*s++) ) < 0 ) return -1; 24 if( ( b = scan_fromhex(*s++) ) == 0xff ) return -1;
33 c=(c<<4)|b; 25 c=(c<<4)|b;
34 } 26 }
35 *d++ = c; 27 *d++ = c;
@@ -37,21 +29,21 @@ size_t scan_urlencoded_query(char **string, char *deststring, int flags) {
37 29
38 switch( c ) { 30 switch( c ) {
39 case 0: case '\r': case '\n': case ' ': 31 case 0: case '\r': case '\n': case ' ':
40 if ( flags & BREAK_AT_WHITESPACE == 0 ) return -1; 32 if ( ( flags & BREAK_AT_WHITESPACE ) == 0 ) return -1;
41 break; 33 break;
42 case '?': 34 case '?':
43 if ( flags & BREAK_AT_QUESTIONMARK == 0 ) return -1; 35 if ( ( flags & BREAK_AT_QUESTIONMARK ) == 0 ) return -1;
44 break; 36 break;
45 case '=': 37 case '=':
46 if ( flags & BREAK_AT_EQUALSIGN == 0 ) return -1; 38 if ( ( flags & BREAK_AT_EQUALSIGN ) == 0 ) return -1;
47 break; 39 break;
48 case '&': 40 case '&':
49 if ( flags & BREAK_AT_AMPERSAND == 0 ) return -1; 41 if ( ( flags & BREAK_AT_AMPERSAND ) == 0 ) return -1;
50 break; 42 break;
51 default: 43 default:
52 return -1; 44 return -1;
53 } 45 }
54 46
55 *string = s; 47 *string = (char *)s;
56 return d - deststring; 48 return d - (unsigned char*)deststring;
57} 49}