PrpAnalysis-simple3


Here’s a later version of my program PrpAnalysis… let’s just say things went a little nasty. The code, no matter how un-useful, holds an important message: don’t reinvent the wheel. Rather than having a 70-something line function to test if something is a sane ascii character, it’s probably a much better idea to use a Python built-in function.

import struct
import binascii


def letterList(letter1):
      letter = letter1.lower
      if letter == 'a':
            return 1
      elif letter == 'b':
            return 1
      elif letter == 'c':
            return 1
      elif letter == 'd':
            return 1
      elif letter == 'e':
            return 1
      elif letter == 'f':
            return 1
      elif letter == 'g':
            return 1
      elif letter == 'h':
            return 1
      elif letter == 'i':
            return 1
      elif letter == 'j':
            return 1
      elif letter == 'k':
            return 1
      elif letter == 'l':
            return 1
      elif letter == 'm':
            return 1
      elif letter == 'n':
            return 1
      elif letter == 'o':
            return 1
      elif letter == 'p':
            return 1
      elif letter == 'q':
            return 1
      elif letter == 'r':
            return 1
      elif letter == 's':
            return 1
      elif letter == 't':
            return 1
      elif letter == 'u':
            return 1
      elif letter == 'v':
            return 1
      elif letter == 'w':
            return 1
      elif letter == 'x':
            return 1
      elif letter == 'y':
            return 1
      elif letter == 'z':
            return 1
      elif letter == '_':
            return 1
      elif letter == '0':
            return 1
      elif letter == '1':
            return 1
      elif letter == '2':
            return 1
      elif letter == '3':
            return 1
      elif letter == '4':
            return 1
      elif letter == '5':
            return 1
      elif letter == '6':
            return 1
      elif letter == '7':
            return 1
      elif letter == '8':
            return 1
      elif letter == '9':
            return 1
      else:
            return 0

def checkNormalLetter(letter):
      if letterList(letter):
            return 1
      else:
            return 0
def UruFloat(byte1,byte2,byte3,byte4):
      stringHex4 = byte1+byte2+byte3+byte4
      num = eval('0x'+stringHex4)
      r, = struct.unpack("f", struct.pack("I", int(num)))
      return r

def UruString(stringHex):
      try:
            if len(stringHex) == 1:
                  stringHex = '0'+stringHex
            dec = eval(stringHex)
            s = hex(255-dec)
            s = s[2:]
            return binascii.a2b_hex(s)
      except:
            return "<N>"

fname = raw_input("file:")

data = open(fname).read()
byteList = []
stringList = []
hypotheticalList = []
count = 0
while 1:
      try:
            num = 1
            bytes, = struct.unpack(str(num)+'s',data[count:count+num])
            stringHex = bytes.encode('hex')
            byteList.append(stringHex)
            stringList.append(UruString('0x'+stringHex))
            count += num
      except:
            print "-end of file or error in reading bytes-"
            break
print byteList
print stringList


#write a blank list
for i in byteList:
      hypotheticalList.append('00')

#calc strings
times = 0
for i in stringList:
      if checkNormalLetter(i):
            hypotheticalList.__setitem__(times,i)
      times += 1
#calc floats
times = 0
for i in byteList:
      try:
            UFloat = UruFloat(byteList[times],byteList[times+1],byteList[times+2],byteList[times+3])
            UFloatS = str(UFloat)
            UFloatSP = UFloatS.split('.')
            decpl = UFloatSP[1]
            if len(decpl) < 3:
                  hypotheticalList.__setitem__(times,UFloat)
                  hypotheticalList.__setitem__(times+1,UFloat)
                  hypotheticalList.__setitem__(times+2,UFloat)
                  hypotheticalList.__setitem__(times+3,UFloat)
                  print UFloat, str(times)+':'+str(times+4)
            times += 1
      except:
            print "[END]"
            break

print '______________'
for i in hypotheticalList:
      print i,
raw_input()

Posted in: Code by nsundin
Copyright © 2011-2025 Programmatic Verse · RSS Feed
Built on Skeleton
Powerered by Wordpress