Beispiel #1
0
    def generate_code(self):
        self.out = Writer(open(self.base_path + '_ber.py', 'w'), indent_size=4)
        command = os.path.basename(sys.argv[0])
        self.out.writelines(
            '# -*- Mode: Python -*-',
            '# generated by: %s %s' % (command, " ".join(sys.argv[1:])),
            '# *** do not edit ***',
            '',
        )
        if self.args.no_standalone:
            self.out.writelines('from tinyber.codec import *', '')
        else:
            pkg_dir, _ = os.path.split(__file__)

            self.out.writelines('# --- start codec.py ---', '')
            with open(os.path.join(pkg_dir, 'codec.py'), 'r') as infile:
                for line in infile:
                    self.out.writelines(line[:-1])
            self.out.writelines('', '# --- end codec.py ---')

        self.tag_assignments = self.walker.tag_assignments
        # generate typedefs and prototypes.
        for (type_name, node, type_decl) in self.walker.defined_types:
            if hasattr(node, 'parent_class'):
                parent_class = node.parent_class
            else:
                parent_class = 'ASN1'
            self.out.newline()
            self.out.newline()
            self.out.writelines('class %s(%s):' % (type_name, parent_class))
            with self.out.indent():
                self.out.writelines('max_size = %d' % (node.max_size()))
                node.emit(self.out)
                self.gen_codec_funs(type_name, type_decl, node)
        self.out.close()
Beispiel #2
0
    def generate_code (self):
        self.copyfiles()
        self.hout = Writer (open (self.base_path + '.h', 'w'))
        self.cout = Writer (open (self.base_path + '.c', 'w'))
        self.hout.writelines (
            '',
            '// generated by %r' % sys.argv,
            '// *** do not edit ***',
            '',
            '#ifndef _%s_H_' % self.module_name.upper(),
            '#define _%s_H_' % self.module_name.upper(),
            '',
            '#include <stdint.h>',
            '#include <string.h>',
            '#include "tinyber.h"',
            '',
        )
        self.cout.writelines (
            '',
            '// generated by %r' % sys.argv,
            '// *** do not edit ***',
            '',
            '#include "%s.h"' % (self.module_name,),
            '',
            # needed for the inline defs when compiled with inlining disabled.
            'extern void init_obuf (buf_t * self, uint8_t * buffer, unsigned int size);',
            'extern void init_ibuf (buf_t * self, uint8_t * buffer, unsigned int size);',
            '',
        )

        self.tag_assignments = self.walker.tag_assignments

        # generate typedefs and prototypes.
        out = self.hout
        for (type_name, node, type_decl) in self.walker.defined_types:
            if isinstance (node, c_choice):
                node.emit_enum (out)
            out.write ('typedef ')
            node.emit (out)
            out.writelines (
                ' %s_t;' % (type_name,),
                '#define %s_MAX_SIZE %d' % (type_name, node.max_size()),
                ''
            )

        for (type_name, node, type_decl) in self.walker.defined_types:
            self.gen_codec_funs (type_name, type_decl, node)

        self.hout.writelines (
            '',
            '#endif // _%s_H_' % self.module_name.upper()
        )
        self.hout.close()
        self.cout.close()
Beispiel #3
0
    def generate_code (self):
        self.out = Writer (open (self.base_path + '_ber.py', 'w'), indent_size=4)
        command = os.path.basename(sys.argv[0])
        self.out.writelines (
            '# -*- Mode: Python -*-',
            '# generated by: %s %s' % (command, " ".join(sys.argv[1:])),
            '# *** do not edit ***',
            '',
        )
        if self.args.no_standalone:
            self.out.writelines ('from tinyber.codec import *', '')
        else:
            pkg_dir, _ = os.path.split (__file__)

            self.out.writelines ('# --- start codec.py ---', '')
            with open(os.path.join(pkg_dir, 'codec.py'), 'r') as infile:
                for line in infile:
                    self.out.writelines (line[:-1])
            self.out.writelines('', '# --- end codec.py ---')

        self.tag_assignments = self.walker.tag_assignments
        # generate typedefs and prototypes.
        for (type_name, node, type_decl) in self.walker.defined_types:
            if hasattr (node, 'parent_class'):
                parent_class = node.parent_class
            else:
                parent_class = 'ASN1'
            self.out.newline()
            self.out.newline()
            self.out.writelines ('class %s(%s):' % (type_name, parent_class))
            with self.out.indent():
                self.out.writelines (
                    'max_size = %d' % (node.max_size())
                )
                node.emit (self.out)
                self.gen_codec_funs (type_name, type_decl, node)
        self.out.close()
Beispiel #4
0
class PythonBackend:

    def __init__ (self, args, walker, module_name, path):
        self.args = args
        self.walker = walker
        self.module_name = module_name
        self.path = path
        self.base_path = os.path.join(path, module_name)

    def gen_decoder (self, type_name, type_decl, node):
        # generate a decoder for a type assignment.
        self.out.newline()
        self.out.writelines ('def _decode(self, src):')
        with self.out.indent():
            node.emit_decode (self.out)
            # this line is unecessary (but harmless) on normal defined sequence types
            self.out.writelines ('self.value = v')

    def gen_encoder (self, type_name, type_decl, node):
        # generate an encoder for a type assignment
        self.out.newline()
        self.out.writelines ('def _encode(self, dst):')
        with self.out.indent():
            node.emit_encode (self.out, 'self.value')

    def gen_codec_funs (self, type_name, type_decl, node):
        if not hasattr (node, 'nodecoder'):
            self.gen_decoder (type_name, type_decl, node)
        if not hasattr (node, 'noencoder'):
            self.gen_encoder (type_name, type_decl, node)

    def generate_code (self):
        self.out = Writer (open (self.base_path + '_ber.py', 'w'), indent_size=4)
        command = os.path.basename(sys.argv[0])
        self.out.writelines (
            '# -*- Mode: Python -*-',
            '# generated by: %s %s' % (command, " ".join(sys.argv[1:])),
            '# *** do not edit ***',
            '',
        )
        if self.args.no_standalone:
            self.out.writelines ('from tinyber.codec import *', '')
        else:
            pkg_dir, _ = os.path.split (__file__)

            self.out.writelines ('# --- start codec.py ---', '')
            with open(os.path.join(pkg_dir, 'codec.py'), 'r') as infile:
                for line in infile:
                    self.out.writelines (line[:-1])
            self.out.writelines('', '# --- end codec.py ---')

        self.tag_assignments = self.walker.tag_assignments
        # generate typedefs and prototypes.
        for (type_name, node, type_decl) in self.walker.defined_types:
            if hasattr (node, 'parent_class'):
                parent_class = node.parent_class
            else:
                parent_class = 'ASN1'
            self.out.newline()
            self.out.newline()
            self.out.writelines ('class %s(%s):' % (type_name, parent_class))
            with self.out.indent():
                self.out.writelines (
                    'max_size = %d' % (node.max_size())
                )
                node.emit (self.out)
                self.gen_codec_funs (type_name, type_decl, node)
        self.out.close()
Beispiel #5
0
class CBackend:

    def __init__ (self, args, walker, module_name, path):
        self.args = args
        self.walker = walker
        self.module_name = module_name
        self.path = path
        self.base_path = os.path.join(path, module_name)

    def gen_decoder (self, type_name, type_decl, node):
        # generate a decoder for a type assignment.
        sig = 'int decode_%s (%s_t * dst, buf_t * src)' % (type_name, type_name)
        self.hout.writelines (sig + ';')
        self.cout.writelines (sig, '{')
        with self.cout.indent():
            self.cout.writelines (
                'asn1raw_t tlv;',
            )
            node.emit_decode (self.cout, 'dst', 'src')
            self.cout.writelines ('return 0;')
        self.cout.writelines ('}', '')

    def gen_encoder (self, type_name, type_decl, node):
        # generate an encoder for a type assignment
        sig = 'int encode_%s (buf_t * dst, const %s_t * src)' % (type_name, type_name)
        self.cout.writelines (sig, '{')
        self.hout.writelines (sig + ';')
        with self.cout.indent():
            node.emit_encode (self.cout, 'dst', 'src')
            self.cout.writelines ('return 0;')
        self.cout.writelines ('}', '')

    def gen_codec_funs (self, type_name, type_decl, node):
        self.gen_decoder (type_name, type_decl, node)
        self.gen_encoder (type_name, type_decl, node)

    def copyfiles(self):
        import shutil
        pkg_dir, _ = os.path.split(__file__)
        tinyberc = os.path.join(pkg_dir, "data", "tinyber.c")
        tinyberh = os.path.join(pkg_dir, "data", "tinyber.h")
        shutil.copy(tinyberc, self.path)
        shutil.copy(tinyberh, self.path)

    def generate_code (self):
        self.copyfiles()
        self.hout = Writer (open (self.base_path + '.h', 'w'))
        self.cout = Writer (open (self.base_path + '.c', 'w'))
        self.hout.writelines (
            '',
            '// generated by %r' % sys.argv,
            '// *** do not edit ***',
            '',
            '#ifndef _%s_H_' % self.module_name.upper(),
            '#define _%s_H_' % self.module_name.upper(),
            '',
            '#include <stdint.h>',
            '#include <string.h>',
            '#include "tinyber.h"',
            '',
        )
        self.cout.writelines (
            '',
            '// generated by %r' % sys.argv,
            '// *** do not edit ***',
            '',
            '#include "%s.h"' % (self.module_name,),
            '',
            # needed for the inline defs when compiled with inlining disabled.
            'extern void init_obuf (buf_t * self, uint8_t * buffer, unsigned int size);',
            'extern void init_ibuf (buf_t * self, uint8_t * buffer, unsigned int size);',
            '',
        )

        self.tag_assignments = self.walker.tag_assignments

        # generate typedefs and prototypes.
        out = self.hout
        for (type_name, node, type_decl) in self.walker.defined_types:
            if isinstance (node, c_choice):
                node.emit_enum (out)
            out.write ('typedef ')
            node.emit (out)
            out.writelines (
                ' %s_t;' % (type_name,),
                '#define %s_MAX_SIZE %d' % (type_name, node.max_size()),
                ''
            )

        for (type_name, node, type_decl) in self.walker.defined_types:
            self.gen_codec_funs (type_name, type_decl, node)

        self.hout.writelines (
            '',
            '#endif // _%s_H_' % self.module_name.upper()
        )
        self.hout.close()
        self.cout.close()
Beispiel #6
0
class PythonBackend:
    def __init__(self, args, walker, module_name, path):
        self.args = args
        self.walker = walker
        self.module_name = module_name
        self.path = path
        self.base_path = os.path.join(path, module_name)

    def gen_decoder(self, type_name, type_decl, node):
        # generate a decoder for a type assignment.
        self.out.newline()
        self.out.writelines('def _decode(self, src):')
        with self.out.indent():
            node.emit_decode(self.out)
            # this line is unecessary (but harmless) on normal defined sequence types
            self.out.writelines('self.value = v')

    def gen_encoder(self, type_name, type_decl, node):
        # generate an encoder for a type assignment
        self.out.newline()
        self.out.writelines('def _encode(self, dst):')
        with self.out.indent():
            node.emit_encode(self.out, 'self.value')

    def gen_codec_funs(self, type_name, type_decl, node):
        if not hasattr(node, 'nodecoder'):
            self.gen_decoder(type_name, type_decl, node)
        if not hasattr(node, 'noencoder'):
            self.gen_encoder(type_name, type_decl, node)

    def generate_code(self):
        self.out = Writer(open(self.base_path + '_ber.py', 'w'), indent_size=4)
        command = os.path.basename(sys.argv[0])
        self.out.writelines(
            '# -*- Mode: Python -*-',
            '# generated by: %s %s' % (command, " ".join(sys.argv[1:])),
            '# *** do not edit ***',
            '',
        )
        if self.args.no_standalone:
            self.out.writelines('from tinyber.codec import *', '')
        else:
            pkg_dir, _ = os.path.split(__file__)

            self.out.writelines('# --- start codec.py ---', '')
            with open(os.path.join(pkg_dir, 'codec.py'), 'r') as infile:
                for line in infile:
                    self.out.writelines(line[:-1])
            self.out.writelines('', '# --- end codec.py ---')

        self.tag_assignments = self.walker.tag_assignments
        # generate typedefs and prototypes.
        for (type_name, node, type_decl) in self.walker.defined_types:
            if hasattr(node, 'parent_class'):
                parent_class = node.parent_class
            else:
                parent_class = 'ASN1'
            self.out.newline()
            self.out.newline()
            self.out.writelines('class %s(%s):' % (type_name, parent_class))
            with self.out.indent():
                self.out.writelines('max_size = %d' % (node.max_size()))
                node.emit(self.out)
                self.gen_codec_funs(type_name, type_decl, node)
        self.out.close()