|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys |
|
|
import time |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def makeRange(lst): |
|
|
ret = [] |
|
|
pos = 0 |
|
|
while pos < len(lst): |
|
|
try: |
|
|
s = lst[pos:].index(1) |
|
|
except: |
|
|
break |
|
|
pos += s |
|
|
try: |
|
|
e = lst[pos:].index(0) |
|
|
e += pos |
|
|
except: |
|
|
e = len(lst) |
|
|
ret.append((pos, e-1)) |
|
|
pos = e + 1 |
|
|
return ret |
|
|
|
|
|
sources = "chvalid.def" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
minTableSize = 6 |
|
|
|
|
|
|
|
|
Functs = {} |
|
|
|
|
|
state = 0 |
|
|
|
|
|
try: |
|
|
defines = open("chvalid.def", "r") |
|
|
except: |
|
|
print("Missing chvalid.def, aborting ...") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for line in defines.readlines(): |
|
|
|
|
|
if line[0] == '#': |
|
|
continue |
|
|
line = line.strip() |
|
|
if line == '': |
|
|
continue |
|
|
|
|
|
try: |
|
|
fields = line.split(' ') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if fields[0] == 'name': |
|
|
name = fields[1] |
|
|
if state != 0: |
|
|
print("'name' %s found before previous name" \ |
|
|
"completed" % (fields[1])) |
|
|
continue |
|
|
state = 1 |
|
|
if name in Functs: |
|
|
print("name '%s' already present - may give" \ |
|
|
" wrong results" % (name)) |
|
|
else: |
|
|
|
|
|
Functs[name] = [ [], [] ] |
|
|
for v in range(256): |
|
|
Functs[name][0].append(0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif fields[0] == 'end': |
|
|
if state == 0: |
|
|
print("'end' found outside of function block") |
|
|
continue |
|
|
state = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif fields[0] == 'ur': |
|
|
if state != 1: |
|
|
raise Exception("'ur' found outside of 'name' block") |
|
|
for el in fields[1:]: |
|
|
pos = el.find('..') |
|
|
|
|
|
if pos <= 0: |
|
|
|
|
|
if el[0:2] == '0x': |
|
|
value = int(el[2:],16) |
|
|
elif el[0] == "'": |
|
|
value = ord(el[1]) |
|
|
else: |
|
|
value = int(el) |
|
|
if ((value < 0) | (value > 0x1fffff)): |
|
|
raise Exception('Illegal value (%s) in ch for'\ |
|
|
' name %s' % (el,name)) |
|
|
|
|
|
|
|
|
currange = (value, value) |
|
|
|
|
|
|
|
|
else: |
|
|
|
|
|
(first, last) = el.split("..") |
|
|
|
|
|
if first[0:2] == '0x': |
|
|
start = int(first[2:],16) |
|
|
elif first[0] == "'": |
|
|
start = ord(first[1]) |
|
|
else: |
|
|
start = int(first) |
|
|
if last[0:2] == '0x': |
|
|
end = int(last[2:],16) |
|
|
elif last[0] == "'": |
|
|
end = ord(last[1]) |
|
|
else: |
|
|
end = int(last) |
|
|
if (start < 0) | (end > 0x1fffff) | (start > end): |
|
|
raise Exception("Invalid range '%s'" % el) |
|
|
currange = (start, end) |
|
|
|
|
|
|
|
|
if currange[1] < 0x100: |
|
|
for ch in range(currange[0],currange[1]+1): |
|
|
|
|
|
if Functs[name][0][ch]: |
|
|
msg = "Duplicate ch value '%s' for name '%s'" % (el, name) |
|
|
raise Exception(msg) |
|
|
Functs[name][0][ch] = 1 |
|
|
else: |
|
|
if currange in Functs[name][1]: |
|
|
raise Exception("range already defined in" \ |
|
|
" function") |
|
|
else: |
|
|
Functs[name][1].append(currange) |
|
|
|
|
|
except: |
|
|
print("Failed to process line: %s" % (line)) |
|
|
raise |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
header = open("include/libxml/chvalid.h", "w") |
|
|
except: |
|
|
print("Failed to open include/libxml/chvalid.h") |
|
|
sys.exit(1) |
|
|
|
|
|
try: |
|
|
output = open("chvalid.c", "w") |
|
|
except: |
|
|
print("Failed to open chvalid.c") |
|
|
sys.exit(1) |
|
|
|
|
|
date = time.asctime(time.localtime(time.time())) |
|
|
|
|
|
header.write( |
|
|
"""/* |
|
|
* Summary: Unicode character range checking |
|
|
* Description: this module exports interfaces for the character |
|
|
* range validation APIs |
|
|
* |
|
|
* This file is automatically generated from the cvs source |
|
|
* definition files using the genChRanges.py Python script |
|
|
* |
|
|
* Generation date: %s |
|
|
* Sources: %s |
|
|
* Author: William Brack <[email protected]> |
|
|
*/ |
|
|
|
|
|
#ifndef __XML_CHVALID_H__ |
|
|
#define __XML_CHVALID_H__ |
|
|
|
|
|
#include <libxml/xmlversion.h> |
|
|
#include <libxml/xmlstring.h> |
|
|
|
|
|
#ifdef __cplusplus |
|
|
extern "C" { |
|
|
#endif |
|
|
|
|
|
/* |
|
|
* Define our typedefs and structures |
|
|
* |
|
|
*/ |
|
|
typedef struct _xmlChSRange xmlChSRange; |
|
|
typedef xmlChSRange *xmlChSRangePtr; |
|
|
struct _xmlChSRange { |
|
|
unsigned short\tlow; |
|
|
unsigned short\thigh; |
|
|
}; |
|
|
|
|
|
typedef struct _xmlChLRange xmlChLRange; |
|
|
typedef xmlChLRange *xmlChLRangePtr; |
|
|
struct _xmlChLRange { |
|
|
unsigned int\tlow; |
|
|
unsigned int\thigh; |
|
|
}; |
|
|
|
|
|
typedef struct _xmlChRangeGroup xmlChRangeGroup; |
|
|
typedef xmlChRangeGroup *xmlChRangeGroupPtr; |
|
|
struct _xmlChRangeGroup { |
|
|
int\t\t\tnbShortRange; |
|
|
int\t\t\tnbLongRange; |
|
|
const xmlChSRange\t*shortRange;\t/* points to an array of ranges */ |
|
|
const xmlChLRange\t*longRange; |
|
|
}; |
|
|
|
|
|
/** |
|
|
* Range checking routine |
|
|
*/ |
|
|
XMLPUBFUN int |
|
|
\t\txmlCharInRange(unsigned int val, const xmlChRangeGroup *group); |
|
|
|
|
|
""" % (date, sources)); |
|
|
output.write( |
|
|
"""/* |
|
|
* chvalid.c:\tthis module implements the character range |
|
|
*\t\tvalidation APIs |
|
|
* |
|
|
* This file is automatically generated from the cvs source |
|
|
* definition files using the genChRanges.py Python script |
|
|
* |
|
|
* Generation date: %s |
|
|
* Sources: %s |
|
|
* William Brack <[email protected]> |
|
|
*/ |
|
|
|
|
|
#define IN_LIBXML |
|
|
#include "libxml.h" |
|
|
#include <libxml/chvalid.h> |
|
|
|
|
|
#include <stddef.h> |
|
|
|
|
|
/* |
|
|
* The initial tables ({func_name}_tab) are used to validate whether a |
|
|
* single-byte character is within the specified group. Each table |
|
|
* contains 256 bytes, with each byte representing one of the 256 |
|
|
* possible characters. If the table byte is set, the character is |
|
|
* allowed. |
|
|
* |
|
|
*/ |
|
|
""" % (date, sources)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fkeys = sorted(Functs.keys()) |
|
|
|
|
|
for f in fkeys: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if max(Functs[f][0]) > 0: |
|
|
rangeTable = makeRange(Functs[f][0]) |
|
|
numRanges = len(rangeTable) |
|
|
if numRanges >= minTableSize: |
|
|
header.write("XMLPUBVAR const unsigned char %s_tab[256];\n" % f) |
|
|
header.write(""" |
|
|
/** |
|
|
* %s_ch: |
|
|
* @c: char to validate |
|
|
* |
|
|
* Automatically generated by genChRanges.py |
|
|
*/ |
|
|
""" % f) |
|
|
header.write("#define %s_ch(c)\t(%s_tab[(c)])\n" % (f, f)) |
|
|
|
|
|
|
|
|
output.write("const unsigned char %s_tab[256] = {\n" % f) |
|
|
pline = " " |
|
|
for n in range(255): |
|
|
pline += " 0x%02x," % Functs[f][0][n] |
|
|
if len(pline) > 72: |
|
|
output.write(pline + "\n") |
|
|
pline = " " |
|
|
output.write(pline + " 0x%02x };\n\n" % Functs[f][0][255]) |
|
|
|
|
|
else: |
|
|
|
|
|
|
|
|
try: |
|
|
ix = rangeTable.remove((0x20, 0x20)) |
|
|
rangeTable.insert(0, (0x20, 0x20)) |
|
|
except: |
|
|
pass |
|
|
firstFlag = 1 |
|
|
|
|
|
header.write(""" |
|
|
/** |
|
|
* %s_ch: |
|
|
* @c: char to validate |
|
|
* |
|
|
* Automatically generated by genChRanges.py |
|
|
*/ |
|
|
""" % f) |
|
|
|
|
|
pline = "#define %s_ch(c)" % f |
|
|
|
|
|
ntab = 4 - (len(pline)) // 8 |
|
|
if ntab < 0: |
|
|
ntab = 0 |
|
|
just = "" |
|
|
for i in range(ntab): |
|
|
just += "\t" |
|
|
pline = pline + just + "(" |
|
|
for rg in rangeTable: |
|
|
if not firstFlag: |
|
|
pline += " || \\\n\t\t\t\t " |
|
|
else: |
|
|
firstFlag = 0 |
|
|
if rg[0] == rg[1]: |
|
|
pline += "((c) == 0x%x)" % rg[0] |
|
|
else: |
|
|
|
|
|
if rg[1] != 0xff: |
|
|
pline += "((0x%x <= (c)) &&" % rg[0] |
|
|
pline += " ((c) <= 0x%x))" % rg[1] |
|
|
else: |
|
|
pline += " (0x%x <= (c))" % rg[0] |
|
|
pline += ")\n" |
|
|
header.write(pline) |
|
|
|
|
|
header.write(""" |
|
|
/** |
|
|
* %sQ: |
|
|
* @c: char to validate |
|
|
* |
|
|
* Automatically generated by genChRanges.py |
|
|
*/ |
|
|
""" % f) |
|
|
pline = "#define %sQ(c)" % f |
|
|
ntab = 4 - (len(pline)) // 8 |
|
|
if ntab < 0: |
|
|
ntab = 0 |
|
|
just = "" |
|
|
for i in range(ntab): |
|
|
just += "\t" |
|
|
header.write(pline + just + "(((c) < 0x100) ? \\\n\t\t\t\t ") |
|
|
if max(Functs[f][0]) > 0: |
|
|
header.write("%s_ch((c)) :" % f) |
|
|
else: |
|
|
header.write("0 :") |
|
|
|
|
|
|
|
|
numRanges = len(Functs[f][1]) |
|
|
if numRanges == 0: |
|
|
header.write(" 0)\n\n") |
|
|
else: |
|
|
if numRanges >= minTableSize: |
|
|
header.write(" \\\n\t\t\t\t xmlCharInRange((c), &%sGroup))\n\n" % f) |
|
|
else: |
|
|
firstFlag = 1 |
|
|
for rg in Functs[f][1]: |
|
|
if not firstFlag: |
|
|
pline += " || \\\n\t\t\t\t " |
|
|
else: |
|
|
firstFlag = 0 |
|
|
pline = "\\\n\t\t\t\t(" |
|
|
if rg[0] == rg[1]: |
|
|
pline += "((c) == 0x%x)" % rg[0] |
|
|
else: |
|
|
pline += "((0x%x <= (c)) &&" % rg[0] |
|
|
pline += " ((c) <= 0x%x))" % rg[1] |
|
|
pline += "))\n\n" |
|
|
header.write(pline) |
|
|
|
|
|
|
|
|
if len(Functs[f][1]) > 0: |
|
|
header.write("XMLPUBVAR const xmlChRangeGroup %sGroup;\n" % f) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for f in fkeys: |
|
|
if len(Functs[f][1]) > 0: |
|
|
rangeTable = Functs[f][1] |
|
|
rangeTable.sort() |
|
|
numShort = 0 |
|
|
numLong = 0 |
|
|
for rg in rangeTable: |
|
|
if rg[1] < 0x10000: |
|
|
if numShort == 0: |
|
|
pline = "static const xmlChSRange %s_srng[] = {" % f |
|
|
else: |
|
|
pline += "," |
|
|
numShort += 1 |
|
|
if len(pline) > 60: |
|
|
output.write(pline + "\n") |
|
|
pline = " " |
|
|
else: |
|
|
pline += " " |
|
|
pline += "{0x%x, 0x%x}" % (rg[0], rg[1]) |
|
|
else: |
|
|
if numLong == 0: |
|
|
if numShort > 0: |
|
|
output.write(pline + "};\n") |
|
|
pline = "static const xmlChLRange %s_lrng[] = { " % f |
|
|
else: |
|
|
pline += ", " |
|
|
numLong += 1 |
|
|
if len(pline) > 60: |
|
|
output.write(pline + "\n") |
|
|
pline = " " |
|
|
pline += "{0x%x, 0x%x}" % (rg[0], rg[1]) |
|
|
output.write(pline + "};\n") |
|
|
|
|
|
pline = "const xmlChRangeGroup %sGroup =\n\t{%d, %d, " % (f, numShort, numLong) |
|
|
if numShort > 0: |
|
|
pline += "%s_srng" % f |
|
|
else: |
|
|
pline += "(xmlChSRangePtr)0" |
|
|
if numLong > 0: |
|
|
pline += ", %s_lrng" % f |
|
|
else: |
|
|
pline += ", (xmlChLRangePtr)0" |
|
|
|
|
|
output.write(pline + "};\n\n") |
|
|
|
|
|
output.write( |
|
|
""" |
|
|
/** |
|
|
* xmlCharInRange: |
|
|
* @val: character to be validated |
|
|
* @rptr: pointer to range to be used to validate |
|
|
* |
|
|
* Does a binary search of the range table to determine if char |
|
|
* is valid |
|
|
* |
|
|
* Returns: true if character valid, false otherwise |
|
|
*/ |
|
|
int |
|
|
xmlCharInRange (unsigned int val, const xmlChRangeGroup *rptr) { |
|
|
int low, high, mid; |
|
|
const xmlChSRange *sptr; |
|
|
const xmlChLRange *lptr; |
|
|
|
|
|
if (rptr == NULL) return(0); |
|
|
if (val < 0x10000) {\t/* is val in 'short' or 'long' array? */ |
|
|
\tif (rptr->nbShortRange == 0) |
|
|
\t return 0; |
|
|
\tlow = 0; |
|
|
\thigh = rptr->nbShortRange - 1; |
|
|
\tsptr = rptr->shortRange; |
|
|
\twhile (low <= high) { |
|
|
\t mid = (low + high) / 2; |
|
|
\t if ((unsigned short) val < sptr[mid].low) { |
|
|
\t\thigh = mid - 1; |
|
|
\t } else { |
|
|
\t\tif ((unsigned short) val > sptr[mid].high) { |
|
|
\t\t low = mid + 1; |
|
|
\t\t} else { |
|
|
\t\t return 1; |
|
|
\t\t} |
|
|
\t } |
|
|
\t} |
|
|
} else { |
|
|
\tif (rptr->nbLongRange == 0) { |
|
|
\t return 0; |
|
|
\t} |
|
|
\tlow = 0; |
|
|
\thigh = rptr->nbLongRange - 1; |
|
|
\tlptr = rptr->longRange; |
|
|
\twhile (low <= high) { |
|
|
\t mid = (low + high) / 2; |
|
|
\t if (val < lptr[mid].low) { |
|
|
\t\thigh = mid - 1; |
|
|
\t } else { |
|
|
\t\tif (val > lptr[mid].high) { |
|
|
\t\t low = mid + 1; |
|
|
\t\t} else { |
|
|
\t\t return 1; |
|
|
\t\t} |
|
|
\t } |
|
|
\t} |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
"""); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for f in fkeys: |
|
|
output.write(""" |
|
|
/** |
|
|
* %s: |
|
|
* @ch: character to validate |
|
|
* |
|
|
* This function is DEPRECATED. |
|
|
""" % f); |
|
|
if max(Functs[f][0]) > 0: |
|
|
output.write(" * Use %s_ch or %sQ instead" % (f, f)) |
|
|
else: |
|
|
output.write(" * Use %sQ instead" % f) |
|
|
output.write(""" |
|
|
* |
|
|
* Returns true if argument valid, false otherwise |
|
|
*/ |
|
|
""") |
|
|
output.write("int\n%s(unsigned int ch) {\n return(%sQ(ch));\n}\n\n" % (f,f)) |
|
|
header.write("XMLPUBFUN int\n\t\t%s(unsigned int ch);\n" % f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
header.write(""" |
|
|
#ifdef __cplusplus |
|
|
} |
|
|
#endif |
|
|
#endif /* __XML_CHVALID_H__ */ |
|
|
""") |
|
|
|
|
|
header.close() |
|
|
|
|
|
output.close() |
|
|
|
|
|
|