[Wisp-cvs] wisp/users/dig bits.py,NONE,1.1 linkie.py,1.35,1.36 Makefile.am,1.10,1.11
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-04-26 17:36:21
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv27812 Modified Files: linkie.py Makefile.am Added Files: bits.py Log Message: moved the Bits class from linkie.py to bits.py --- NEW FILE: bits.py --- #### bits.py - a Python module for low-level bit holders # # Copyleft © 2003 by Andres Soolo (di...@us...) # This file is licensed under the GNU GPL v2. If you # don't know what that means, please do read the GPL. # #### @(#) $Id: bits.py,v 1.1 2003/04/26 17:36:17 digg Exp $ from array import array from struct import pack, unpack class Bits (object): __slots__ = ['_contents', '_skipped'] def __init__ (this, byte_order = None): this._contents = array('c') if byte_order == None: pass elif byte_order == '<': this.emit_wyde = this.emit_lewyde this.emit_tetra = this.emit_letetra this.add_wyde = this.add_lewyde this.add_tetra = this.add_letetra elif byte_order == '>': this.emit_wyde = this.emit_bewyde this.emit_tetra = this.emit_betetra this.add_wyde = this.add_bewyde this.add_tetra = this.add_betetra else: raise "Unknown byte order", byte_order def emit_byte (this, b): if this._skipped <> 0: raise "Events out of order", this this._contents.append(chr(b & 0xff)) def emit_bewyde (this, w): if this._skipped <> 0: raise "Events out of order", this this._contents.fromstring(pack('>H', w)) # FIXME? def emit_lewyde (this, w): if this._skipped <> 0: raise "Events out of order", this this._contents.fromstring(pack('<H', w)) # FIXME? def emit_betetra (this, t): if this._skipped <> 0: raise "Events out of order", this this._contents.fromstring(pack('>L', t)) # FIXME? def emit_letetra (this, t): if this._skipped <> 0: raise "Events out of order", this this._contents.fromstring(pack('<L', t)) # FIXME? def emit_string (this, s): if this._skipped <> 0: raise "Events out of order", this this._contents.fromstring(s) def from_array (this, a): this._contents.extend(a) def add_byte (this, ofs, value): this._contents[ofs] = chr((ord(this._contents[ofs]) + value) % 0x100) def _add (this, ofs, value, size, tpl): datum, = unpack(tpl, this._contents[ofs : ofs + size]) datum += value datum %= 1L << (size << 3) this._contents[ofs : ofs + size] = array('c', pack(tpl, datum)) def add_lewyde (this, ofs, value): this._add(ofs, value, 2, '<H') def add_bewyde (this, ofs, value): this._add(ofs, value, 2, '>H') def add_letetra (this, ofs, value): this._add(ofs, value, 4, '<L') def add_betetra (this, ofs, value): this._add(ofs, value, 4, '>L') Index: linkie.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/linkie.py,v retrieving revision 1.35 retrieving revision 1.36 diff -u -d -r1.35 -r1.36 --- linkie.py 26 Apr 2003 16:52:41 -0000 1.35 +++ linkie.py 26 Apr 2003 17:36:17 -0000 1.36 @@ -6,61 +6,14 @@ # #### @(#) $Id$ -from struct import pack, unpack +from bits import Bits +from types import IntType, StringType from array import array -from types import * - -class Bits (object): - __slots__ = ['_contents'] - def __init__ (this, byte_order = None): - this._contents = array('c') - if byte_order == None: pass - elif byte_order == '<': - this.emit_wyde = this.emit_lewyde - this.emit_tetra = this.emit_letetra - this.add_wyde = this.add_lewyde - this.add_tetra = this.add_letetra - elif byte_order == '>': - this.emit_wyde = this.emit_bewyde - this.emit_tetra = this.emit_betetra - this.add_wyde = this.add_bewyde - this.add_tetra = this.add_betetra - else: raise "Unknown byte order", byte_order - def emit_byte (this, b): - if this._skipped <> 0: raise "Events out of order", this - this._contents.append(chr(b & 0xff)) - def emit_bewyde (this, w): - if this._skipped <> 0: raise "Events out of order", this - this._contents.fromstring(pack('>H', w)) # FIXME? - def emit_lewyde (this, w): - if this._skipped <> 0: raise "Events out of order", this - this._contents.fromstring(pack('<H', w)) # FIXME? - def emit_betetra (this, t): - if this._skipped <> 0: raise "Events out of order", this - this._contents.fromstring(pack('>L', t)) # FIXME? - def emit_letetra (this, t): - if this._skipped <> 0: raise "Events out of order", this - this._contents.fromstring(pack('<L', t)) # FIXME? - def emit_string (this, s): - if this._skipped <> 0: raise "Events out of order", this - this._contents.fromstring(s) - def from_array (this, a): - this._contents.extend(a) - def add_byte (this, ofs, value): - this._contents[ofs] = chr((ord(this._contents[ofs]) + value) % 0x100) - def _add (this, ofs, value, size, tpl): - datum, = unpack(tpl, this._contents[ofs : ofs + size]) - datum += value - datum %= 1L << (size << 3) - this._contents[ofs : ofs + size] = array('c', pack(tpl, datum)) - def add_lewyde (this, ofs, value): this._add(ofs, value, 2, '<H') - def add_bewyde (this, ofs, value): this._add(ofs, value, 2, '>H') - def add_letetra (this, ofs, value): this._add(ofs, value, 4, '<L') - def add_betetra (this, ofs, value): this._add(ofs, value, 4, '>L') class Linkie (Bits): def __init__ (this, byte_order = None): Bits.__init__(this, byte_order) + this._byte_order = byte_order this._symbols = [] # symbol -> address this._alignment = 1 # minimal required alignment constraint this._skipped = 0 # uninitialized space after bits Index: Makefile.am =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/Makefile.am,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- Makefile.am 21 Apr 2003 11:28:48 -0000 1.10 +++ Makefile.am 26 Apr 2003 17:36:17 -0000 1.11 @@ -6,8 +6,8 @@ # #### @(#) $Id$ -EXTRA_DIST = .cvsignore .pycheckrc struburn.wisp elf.py linkie.py \ - makehello.py elfdump.py +EXTRA_DIST = .cvsignore .pycheckrc struburn.wisp bits.py elf.py \ + linkie.py makehello.py elfdump.py all: |