def check_all(target, max_diff, filename): ((m,r),e) = mrag(target[:2]) if r == 0 or r > 24 or (target == ord(' ')).sum() > 32: sys.stdout.write(target) sys.stdout.flush() return f = file(filename) ans = [] while True: packet = f.read(42) if len(packet) != 42: sys.stderr.write(str(len(ans))+"\n") sys.stderr.flush() ans = np.column_stack(ans) #print ans.shape auni = np.unique(ans) mode = np.zeros(42, dtype=np.uint8) counts = np.zeros(42) for k in auni: count = (ans==k).sum(-1) mode[count>counts] = k counts[count>counts] = count[count>counts] sys.stdout.write("".join([chr(x) for x in mode])) sys.stdout.flush() return packet = np.fromstring(packet, dtype=np.uint8) if ((m,r),e) == mrag(packet[:2]): if(distance(target, packet) <= max_diff): ans.append(packet)
def __init__(self, a): self.array = a.reshape((26,42)) #do_print(self.array[0]) ((self.m,self.r),e) = mrag(self.array[0][:2]) (self.p,e) = page(self.array[0][2:4]) (self.s,self.c),self.e = subcode_bcd(self.array[0][4:10]) # remove parity self.array[2:,2:] &= 0x7f # check if row n-1 is double height self.no_double_on_prev = ((self.array[1:-1,2:]) != 0x0d).all(axis=1) # row 1 can't contain double but might due to not being printable self.no_double_on_prev[0] = True # calculate a target threshold for each line # based on the number of non-blank characters # first count non-blanks self.threshold = ((self.array[2:,2:] != ord(' ')).sum(axis=1)) # if non-blanks <= 5, don't require a match (set threshold to 0) # also ignore rows following a double height row self.threshold *= ((self.threshold > 5) & (self.no_double_on_prev)) # some proportion of non-blanks must match in the rest of the lines self.threshold *= 0.5 # sum required threshold for each line to get total threshold self.threshold_sum = self.threshold.sum() * 1.5 try: self.ds = int("%x" % self.s, 10) except ValueError: self.ds = 1000 rows = np.array([mrag(self.array[n][:2])[0][1] for n in range(26)]) self.goodrows = (rows == Page.rows)
def deconvolve(self): target = gauss(self.vbi, self.gauss_sd) self.target = normalise(target) self.make_guess_mask() self.make_possible_bytes(Vbi.possible_bytes) self._oldbytes = np.zeros(42, dtype=np.uint8) self._deconvolve() packet = "".join([chr(x) for x in self.g.bytes]) F = finders.test(self.finders, packet) if F: sys.stderr.write("matched by finder "+F.name+"\n"); sys.stderr.flush() self.make_possible_bytes(F.possible_bytes) self._deconvolve() F.find(self.g.bytes) packet = F.fixup() return packet # if the packet did not match any of the finders then it isn't # a packet 0 (or 30). if the packet still claims to be a packet 0 it # will mess up the page splitter. so redo the deconvolution but with # packet 0 (and 30) header removed from possible bytes. # note: this doesn't work. i am not sure why. a packet in 63322 # does not match the finders but still passes through this next check # with r=0. which should be impossible. ((m,r),e) = mrag(self.g.bytes[:2]) if r == 0: sys.stderr.write("packet falsely claimed to be packet %d\n" % r); sys.stderr.flush() if not self.allow_unmatched: self._nzdeconvolve() packet = "".join([chr(x) for x in self.g.bytes]) # if it's a link packet, it is completely hammed elif r == 27: self.make_possible_bytes([hammbytes]*42) self._deconvolve() packet = "".join([chr(x) for x in self.g.bytes]) return packet
def __init__(self, tt): self.sequence = PacketHolder.sequence PacketHolder.sequence += 1 (self.m,self.r),e = mrag(np.fromstring(tt[:2], dtype=np.uint8)) match = False F = finders.test(finders.all_headers, tt) if F: self.r = 0 F.check_page_info() self.me = False #F.me self.pe = False #F.pe self.p = F.p match = True if not match and self.r == 0: self.r = -1 self.m = -1 self.tt = tt self.used = False
def do_print(tt): if type(tt) == type(''): tt = np.fromstring(tt, dtype=np.uint8) ret = "" ((m, r),e) = mrag(tt[:2]) ret += "%1d %2d" % (m, r) if r == 0: (p,e) = page(tt[2:4]) ((s,c),e) = subcode_bcd(tt[4:10]) ret += " P%1d%02x " % (m,p) ret += Printer(tt[10:]).string_ansi() ret += " %04x %x" % (s,c) elif r == 30: # broadcast service data # designation code (d,e) = unhamm84(tt[2]) # initial page (p,e) = page(tt[3:5]) ((s,m),e) = subcode_bcd(np.fromstring(tt[5:9], dtype=np.uint8)) ret += " %1d I%1d%02x:%04x " % (d, m, p, s) if d&2: ret += "(PDC) " else: ret += "(NET) " ret += Printer(tt[22:]).string_ansi() elif r == 27: # broadcast service data # designation code (d,e) = unhamm84(tt[2]) ret += " %1d " % (d) for n in range(6): nn = n*6 (p,e) = page(tt[nn+3:nn+5]) ((s,m),e) = subcode_bcd(tt[nn+5:nn+9]) ret += " %1d%02x:%04x " % (m,p,s) else: ret += Printer(tt[2:]).string_ansi() return ret
from util import mrag, bitwise_mode, page def dump_headers(headers): if len(headers) > 1: ans = bitwise_mode([np.fromstring(h, dtype=np.uint8) for h in headers]) ans = "".join([chr(x) for x in ans]) else: ans = headers[0] sys.stdout.write(ans) if __name__=='__main__': headers = [] while(True): tt = sys.stdin.read(42) if len(tt) < 42: exit(0) ((m,r),e) = mrag(np.fromstring(tt[:2], dtype=np.uint8)) (p,e1) = page(np.fromstring(tt[2:4], dtype=np.uint8)) if r == 0: headers.append(tt) else: if len(headers) > 0: dump_headers(headers) headers = [] sys.stdout.write(tt) sys.stdout.flush()
# designation code (d,e) = unhamm84(tt[2]) ret += " %1d " % (d) for n in range(6): nn = n*6 (p,e) = page(tt[nn+3:nn+5]) ((s,m),e) = subcode_bcd(tt[nn+5:nn+9]) ret += " %1d%02x:%04x " % (m,p,s) else: ret += Printer(tt[2:]).string_ansi() return ret if __name__=='__main__': import sys import numpy as np while(True): tt = sys.stdin.read(42) if len(tt) < 42: exit(0) else: tt = np.fromstring(tt, dtype=np.uint8) ((m,r),e) = mrag(tt[:2]) #if r == 0 or r == 30: # to only print headers etc print do_print(tt)