asnwriter.py 10.1 KB
Newer Older
C
calin cerchez 已提交
1 2 3 4 5 6
import asnparser
from asnparser import *

asnobjs = list()
imports = list()
includes = list()
C
calin cerchez 已提交
7
forbidden = ['explicit']
C
calin cerchez 已提交
8

C
calincerchez 已提交
9 10 11
def firstlower(string):
        return string[0].lower() + string[1:]

C
calin cerchez 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
def checkandhandledeps(asnobj, hdrfile, srcfile):
	retval = 0
	for i in range(0, len(asnobj.objs)):
		obj = asnobj.objs[i]
		if obj.type in types:
			writeobject(obj, hdrfile, srcfile)
		elif obj.type in imports:
                        pass
		else:
			found = 0
			for j in range(0, len(asnobjs)):
				if obj.type == asnobjs[j].name:
					writeobject(asnobjs[j], hdrfile, srcfile)
					found = 1
					break
			if not found:
				print ("Warning: Unknown ASN.1 object type " + obj.type + ". Skipping " + asnobj.name + ".\n")
				retval = -1
	return retval	
			
def writeobject(asnobj, hdrfile, srcfile):
	if asnobj.written == 0 and asnobj.name != '':
		if asnobj.parent != None:
			asnobj.name = asnobj.parent.name + asnobj.name

                # Non standard types

		if asnobj.type not in types:
                        objs = list()
			obj = ASNObject()
			obj.type = asnobj.type
			objs.append(obj)
			asnobj.objs = objs
			#print asnobj.name
C
calincerchez 已提交
46 47 48
			if checkandhandledeps(asnobj, hdrfile, srcfile) != 0:
                                return
			hdrfile.write("typedef " + asnobj.type + " " + asnobj.name + ";\n")
C
calin cerchez 已提交
49 50 51 52 53 54 55 56
		
		# Null and Boolean
		
		if asnobj.type == "Null" or asnobj.type == "Boolean":
			hdrfile.write("typedef " + asnobj.type + " " + asnobj.name + ";\n")
			asnobj.type = asnobj.name
	
		# Constraint types
C
calincerchez 已提交
57
		
C
calin cerchez 已提交
58 59 60 61 62 63 64 65 66 67 68 69
		if asnobj.type in constrainttypes:
			if asnobj.constrainttype == "CONSTANT":
				hdrfile.write("#define " + asnobj.name + " " + str(asnobj.value) + "\n")
			elif asnobj.constrainttype == "UNCONSTRAINED":
				hdrfile.write("typedef " + asnobj.type + " " + asnobj.name + ";\n")
			else:
				hdrfile.write("typedef " + asnobj.type + "<" + asnobj.constrainttype)
				if asnobj.lowerlimit != '':
					hdrfile.write(", " + asnobj.lowerlimit)
				if asnobj.upperlimit != '':
					hdrfile.write(", " + asnobj.upperlimit)
				hdrfile.write("> " + asnobj.name + ";\n")
C
calincerchez 已提交
70
			asnobj.type = asnobj.name
C
calin cerchez 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		
		# Enumerated
		
		if asnobj.type == "Enumerated":
			asnobj.type = asnobj.name
			hdrfile.write("enum " + asnobj.name + "Values {\n")
			for j in range(0, len(asnobj.objs)):
				childobj = asnobj.objs[j]
				hdrfile.write("\t" + childobj.name + "_" + asnobj.name + " = " + str(j) + ",\n")
			hdrfile.write("};\n")
			hdrfile.write("typedef Enumerated<" + asnobj.constrainttype + ", " + str(len(asnobj.objs) - 1) + "> " + asnobj.name + ";\n")
		
		# Sequence
		
		if asnobj.type == "Sequence":
			optnr = 0
			extnr = 0
C
calin cerchez 已提交
88 89 90 91 92 93
			objs = list()
			if len(asnobj.objs) > 0:
                                for j in range(0, len(asnobj.objs)):
                                        obj = asnobj.objs[j]
                                        if obj.opt == 0:
                                                objs.append(obj)
C
calin cerchez 已提交
94 95 96
			asnobj.type = asnobj.name
			if checkandhandledeps(asnobj, hdrfile, srcfile) != 0:
				return
C
calincerchez 已提交
97
			hdrfile.write("class " + asnobj.name + " : public Sequence {\n" +
C
calin cerchez 已提交
98 99 100 101 102
						"private:\n" +
						"\tstatic const void *itemsInfo[" + str(len(asnobj.objs)) + "];\n" +
						"\tstatic bool itemsPres[" + str(len(asnobj.objs)) + "];\n" +
						"public:\n" +
						"\tstatic const Info theInfo;\n"
C
calin cerchez 已提交
103 104 105 106 107 108 109 110
						"\t" + asnobj.name + "(): Sequence(&theInfo) {}\n")
			if len(objs) > 0:
                                hdrfile.write("\t" + asnobj.name + "(")
                                for j in range(0, len(objs) - 1):
                                        obj = objs[j]
                                        hdrfile.write("const " + obj.type + "& " + firstlower(obj.name) + ", ")
                                obj = objs[len(objs) - 1]
                                hdrfile.write("const " + obj.type + "& " + firstlower(obj.name) + ");\n\n")
C
calin cerchez 已提交
111 112
			for j in range(0, len(asnobj.objs)):
                                obj = asnobj.objs[j]
C
calincerchez 已提交
113
                                hdrfile.write("\tvoid set" + obj.name + "(const " + obj.type + "& " + firstlower(obj.name) + ") { *static_cast<" + obj.type + "*>(items[" + str(j) + "]) = " + firstlower(obj.name) + "; }\n")
C
calin cerchez 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
			hdrfile.write("};\n")
			srcfile.write("const void *" + asnobj.name + "::itemsInfo[" + str(len(asnobj.objs)) + "] = {\n")
			for j in range(0, len(asnobj.objs)):
				obj = asnobj.objs[j]
				srcfile.write("\t&" + obj.type + "::theInfo,\n")
			srcfile.write("};\n")
			srcfile.write("bool " + asnobj.name + "::itemsPres[" + str(len(asnobj.objs)) + "] = {\n")
			for j in range(0, len(asnobj.objs)):
				obj = asnobj.objs[j]
				if obj.opt == 0:
					srcfile.write("\t1,\n")
				else:
					srcfile.write("\t0,\n")
					optnr += 1
			srcfile.write("};\n")
			srcfile.write("const " + asnobj.name + "::Info " + asnobj.name + "::theInfo = {\n" +
						"\t" + asnobj.name + "::create,\n" +
						"\tSEQUENCE,\n" +
						"\t0,\n")
			if asnobj.constrainttype == "EXTCONSTRAINED":
				srcfile.write("\ttrue,\n")
			else:
				srcfile.write("\tfalse,\n")
			srcfile.write("\titemsInfo,\n" +
						"\titemsPres,\n"
						"\t" + str(len(asnobj.objs)) + ", " + str(optnr) + ", " + str(extnr) + "\n" +
						"};\n")
C
calin cerchez 已提交
141 142 143 144 145 146 147 148 149 150 151
			if len(objs) > 0:
                                srcfile.write(asnobj.name + "::" + asnobj.name + "(")
                                for j in range(0, len(objs) - 1):
                                        obj = objs[j]
                                        srcfile.write("const " + obj.type + "& " + firstlower(obj.name) + ", ")
                                obj = objs[len(objs) - 1]
                                srcfile.write("const " + obj.type + "& " + firstlower(obj.name) + ") : Sequence(&theInfo) {\n")
                                for j in range(0, len(objs)):
                                        obj = objs[j]
                                        srcfile.write("\tset" + obj.name + "(" + firstlower(obj.name) + ");\n")
                                srcfile.write("}\n")
C
calin cerchez 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
			srcfile.write("\n")
			
		# Sequence Of
		
		if asnobj.type == "SequenceOf":
			asnobj.type = asnobj.name
			if checkandhandledeps(asnobj, hdrfile, srcfile) != 0:
				return
			hdrfile.write("typedef SequenceOf<" + asnobj.objs[0].type + ", " + asnobj.constrainttype + ", " + asnobj.lowerlimit + ", " + asnobj.upperlimit + "> " + asnobj.name + ";\n")
			
		# Choice
		
		if asnobj.type == "Choice":
			asnobj.type = asnobj.name
			if checkandhandledeps(asnobj, hdrfile, srcfile) != 0:
				return
C
calincerchez 已提交
168

C
calincerchez 已提交
169
			hdrfile.write("class " + asnobj.name + " : public Choice {\n" +
C
calin cerchez 已提交
170 171
						"private:\n" +
						"\tstatic const void *choicesInfo[" + str(len(asnobj.objs)) + "];\n" +
C
calincerchez 已提交
172 173 174 175
						"public:\n")
			hdrfile.write("\tenum " + asnobj.name + "Choices {\n")
			for j in range(0, len(asnobj.objs)):
                                obj = asnobj.objs[j]
C
calin cerchez 已提交
176 177 178
                                obj.name = firstlower(obj.name)
                                if obj.name in forbidden:
                                        obj.name = "_" + obj.name
C
calincerchez 已提交
179 180 181 182
                                hdrfile.write("\t\t" + firstlower(obj.name) + " = " + str(j) + ",\n")
			hdrfile.write("\t};\n")
			hdrfile.write("\tstatic const Info theInfo;\n"
                                                "\t" + asnobj.name + "(): Choice(&theInfo) {}\n")
C
calin cerchez 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
			hdrfile.write("};\n")
			srcfile.write("const void *" + asnobj.name + "::choicesInfo[" + str(len(asnobj.objs)) + "] = {\n")
			for j in range(0, len(asnobj.objs)):
				obj = asnobj.objs[j]
				srcfile.write("\t&" + obj.type + "::theInfo,\n")
			srcfile.write("};\n")
			srcfile.write("const " + asnobj.name + "::Info " + asnobj.name + "::theInfo = {\n" +
						"\t" + asnobj.name + "::create,\n" +
						"\tCHOICE,\n" +
						"\t0,\n")
			if asnobj.constrainttype == "EXTCONSTRAINED":
				srcfile.write("\ttrue,\n")
			else:
				srcfile.write("\tfalse,\n")
			srcfile.write("\tchoicesInfo,\n" +
						"\t" + str(len(asnobj.objs) - 1) + "\n" +
						"};\n")
                    	srcfile.write("\n")
			
		hdrfile.write("\n")
		asnobj.written = 1

def writeheader(file):
	file.write("//\n" +
		"// Copyright (C) 2012 Calin Cerchez\n" +
		"//\n" +
		"// This program is free software: you can redistribute it and/or modify\n" +
		"// it under the terms of the GNU Lesser General Public License as published by\n" +
		"// the Free Software Foundation, either version 3 of the License, or\n" +
		"// (at your option) any later version.\n" +
		"//\n" + 
		"// This program is distributed in the hope that it will be useful,\n" +
		"// but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
		"// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" +
		"// GNU Lesser General Public License for more details.\n" +
		"//\n" + 
		"// You should have received a copy of the GNU Lesser General Public License\n" +
		"// along with this program.  If not, see http://www.gnu.org/licenses/.\n" +
		"//\n\n")



def writefile(directory, filename, objs, module):
        global includes
        global imports
        global asnobjs
        asnobjs = objs
        outfilename = asnobjs[0].outfilename
        includes = asnobjs[0].includes
        imports = asnobjs[0].imports
        
        print ("writing source files for " + filename + "...")
	hdrfile = open(directory + outfilename + ".h", 'w')
	srcfile = open(directory + outfilename + ".cc", 'w')
	writeheader(hdrfile)
	writeheader(srcfile)

	hdrfile.write("#ifndef " + outfilename.upper() + "_H_\n" +
			"#define " + outfilename.upper() + "_H_\n\n" +
			"#include \"ASNTypes.h\"\n")
	for i in range(0, len(includes)):
		hdrfile.write("#include \"" + includes[i] + ".h\"\n")
	hdrfile.write("\n")

	srcfile.write("#include \"" + outfilename + ".h\"\n\n")

	srcfile.write("namespace " + module.lower() + " {\n\n")
	hdrfile.write("namespace " + module.lower() + " {\n\n")

	for i in range (0, len(asnobjs)):
		asnobj = asnobjs[i]
		writeobject(asnobj, hdrfile, srcfile)

        srcfile.write("}\n")
        hdrfile.write("}\n\n")

	hdrfile.write("#endif /* " + outfilename.upper() + "_H_ */\n")

	srcfile.close()
	hdrfile.close()