github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/injectionutils/libinjection/src/sqlparse2c.py (about) 1 #!/usr/bin/env python3 2 # 3 # Copyright 2012, 2013 Nick Galbreath 4 # nickg@client9.com 5 # BSD License -- see COPYING.txt for details 6 # 7 8 """ 9 Converts a libinjection JSON data file to a C header (.h) file 10 """ 11 12 import sys 13 14 def toc(obj): 15 """ main routine """ 16 17 print(""" 18 #ifndef LIBINJECTION_SQLI_DATA_H 19 #define LIBINJECTION_SQLI_DATA_H 20 21 #include "libinjection.h" 22 #include "libinjection_sqli.h" 23 24 typedef struct { 25 const char *word; 26 char type; 27 } keyword_t; 28 29 static size_t parse_money(sfilter * sf); 30 static size_t parse_other(sfilter * sf); 31 static size_t parse_white(sfilter * sf); 32 static size_t parse_operator1(sfilter *sf); 33 static size_t parse_char(sfilter *sf); 34 static size_t parse_hash(sfilter *sf); 35 static size_t parse_dash(sfilter *sf); 36 static size_t parse_slash(sfilter *sf); 37 static size_t parse_backslash(sfilter * sf); 38 static size_t parse_operator2(sfilter *sf); 39 static size_t parse_string(sfilter *sf); 40 static size_t parse_word(sfilter * sf); 41 static size_t parse_var(sfilter * sf); 42 static size_t parse_number(sfilter * sf); 43 static size_t parse_tick(sfilter * sf); 44 static size_t parse_ustring(sfilter * sf); 45 static size_t parse_qstring(sfilter * sf); 46 static size_t parse_nqstring(sfilter * sf); 47 static size_t parse_xstring(sfilter * sf); 48 static size_t parse_bstring(sfilter * sf); 49 static size_t parse_estring(sfilter * sf); 50 static size_t parse_bword(sfilter * sf); 51 """) 52 53 # 54 # Mapping of character to function 55 # 56 fnmap = { 57 'CHAR_WORD' : 'parse_word', 58 'CHAR_WHITE': 'parse_white', 59 'CHAR_OP1' : 'parse_operator1', 60 'CHAR_UNARY': 'parse_operator1', 61 'CHAR_OP2' : 'parse_operator2', 62 'CHAR_BANG' : 'parse_operator2', 63 'CHAR_BACK' : 'parse_backslash', 64 'CHAR_DASH' : 'parse_dash', 65 'CHAR_STR' : 'parse_string', 66 'CHAR_HASH' : 'parse_hash', 67 'CHAR_NUM' : 'parse_number', 68 'CHAR_SLASH': 'parse_slash', 69 'CHAR_SEMICOLON' : 'parse_char', 70 'CHAR_COMMA': 'parse_char', 71 'CHAR_LEFTPARENS': 'parse_char', 72 'CHAR_RIGHTPARENS': 'parse_char', 73 'CHAR_LEFTBRACE': 'parse_char', 74 'CHAR_RIGHTBRACE': 'parse_char', 75 'CHAR_VAR' : 'parse_var', 76 'CHAR_OTHER': 'parse_other', 77 'CHAR_MONEY': 'parse_money', 78 'CHAR_TICK' : 'parse_tick', 79 'CHAR_UNDERSCORE': 'parse_underscore', 80 'CHAR_USTRING' : 'parse_ustring', 81 'CHAR_QSTRING' : 'parse_qstring', 82 'CHAR_NQSTRING' : 'parse_nqstring', 83 'CHAR_XSTRING' : 'parse_xstring', 84 'CHAR_BSTRING' : 'parse_bstring', 85 'CHAR_ESTRING' : 'parse_estring', 86 'CHAR_BWORD' : 'parse_bword' 87 } 88 print() 89 print("typedef size_t (*pt2Function)(sfilter *sf);") 90 print("static const pt2Function char_parse_map[] = {") 91 pos = 0 92 for character in obj['charmap']: 93 print(" &%s, /* %d */" % (fnmap[character], pos)) 94 pos += 1 95 print("};") 96 print() 97 98 # keywords 99 # load them 100 keywords = obj['keywords'] 101 102 for fingerprint in list(obj['fingerprints']): 103 fingerprint = '0' + fingerprint.upper() 104 keywords[fingerprint] = 'F' 105 106 needhelp = [] 107 for key in keywords.keys(): 108 if key != key.upper(): 109 needhelp.append(key) 110 111 for key in needhelp: 112 tmpv = keywords[key] 113 del keywords[key] 114 keywords[key.upper()] = tmpv 115 116 print("static const keyword_t sql_keywords[] = {") 117 for k in sorted(keywords.keys()): 118 if len(k) > 31: 119 sys.stderr.write("ERROR: keyword greater than 32 chars\n") 120 sys.exit(1) 121 122 print(" {\"%s\", '%s'}," % (k, keywords[k])) 123 print("};") 124 print("static const size_t sql_keywords_sz = %d;" % (len(keywords), )) 125 126 print("#endif") 127 return 0 128 129 if __name__ == '__main__': 130 import json 131 sys.exit(toc(json.load(sys.stdin))) 132