From 4d3171c60fa2571a5cf59b7a8207888e6b1a682c Mon Sep 17 00:00:00 2001 From: icefire Date: Tue, 23 Jun 2009 00:33:28 -0700 Subject: [PATCH 1/2] fixed tpl shit --- TPL.py | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/TPL.py b/TPL.py index a735c9b..bc8ab12 100644 --- a/TPL.py +++ b/TPL.py @@ -83,7 +83,7 @@ class TPL(): else: self.file = None self.data = file - def toTPL(self, outfile, width = 0, height = 0, format="RGBA8"): #single texture only + def toTPL(self, outfile, (width, height) = (None, None), format = "RGBA8"): #single texture only """This converts a PNG image into a TPL. The PNG image is specified as the file parameter to the class initializer, while the output filename is specified here as the parameter outfile. Width and height are optional parameters and specify the size to resize the image to, if needed. Returns the output filename. This only can create TPL images with a single texture.""" @@ -98,8 +98,8 @@ class TPL(): img = Image.open(self.file) theWidth, theHeight = img.size - if(width !=0 and height != 0 and (width != theWidth or height != theHeight)): - img.resize(width, height) + if(width != None and height != None and (width != theWidth or height != theHeight)): + img = img.resize((width, height), Image.ANTIALIAS) w, h = img.size texhead = self.TPLTextureHeader() @@ -146,6 +146,7 @@ class TPL(): ''' ADD toCMP ''' raise Exception("toCMP not done") #tpldata = self.toCMP((w, h), img) + texhead.data_off = 0x14 + len(texhead) texhead.wrap = [0, 0] texhead.filter = [1, 1] @@ -188,7 +189,6 @@ class TPL(): f.close() return outfile - def toI4(self, (w, h), img): out = [0 for i in range(align(w, 4) * align(h, 4) / 2)] outp = 0 @@ -201,9 +201,15 @@ class TPL(): newpixel = 0 else: rgba = flatten(inp[x+y*w]) - i1 = (rgba >> 0) & 0xff + r = (rgba >> 0) & 0xff + g = (rgba >> 8) & 0xff + b = (rgba >> 16) & 0xff + i1 = ((r + g + b) / 3) & 0xff rgba = flatten(inp[x+1+y*w]) - i2 = (rgba >> 0) & 0xff + r = (rgba >> 0) & 0xff + g = (rgba >> 8) & 0xff + b = (rgba >> 16) & 0xff + i2 = ((r + g + b) / 3) & 0xff newpixel = (((i1 * 15) / 255) << 4) newpixel |= (((i2 * 15) / 255) & 0xf) @@ -222,7 +228,10 @@ class TPL(): if x>= w or y>=h: i1 = 0 else: - i1 = (rgba >> 0) & 0xff + r = (rgba >> 0) & 0xff + g = (rgba >> 8) & 0xff + b = (rgba >> 16) & 0xff + i1 = ((r + g + b) / 3) & 0xff out[outp] = i1 outp += 1 return out @@ -238,7 +247,10 @@ class TPL(): newpixel = 0 else: rgba = flatten(inp[x + (y * w)]) - i1 = (rgba >> 0) & 0xff + r = (rgba >> 0) & 0xff + g = (rgba >> 8) & 0xff + b = (rgba >> 16) & 0xff + i1 = ((r + g + b) / 3) & 0xff a1 = (rgba >> 24) & 0xff newpixel = (((i1 * 15) / 255) & 0xf) @@ -258,7 +270,10 @@ class TPL(): newpixel = 0 else: rgba = flatten(inp[x + (y * w)]) - i1 = (rgba >> 0) & 0xff + r = (rgba >> 0) & 0xff + g = (rgba >> 8) & 0xff + b = (rgba >> 16) & 0xff + i1 = ((r + g + b) / 3) & 0xff a1 = (rgba >> 24) & 0xff newpixel = a1 << 8 @@ -496,13 +511,15 @@ class TPL(): class imp(wx.Panel): def __init__(self, parent, id, im): wx.Panel.__init__(self, parent, id) - w, h = im.size + w = img.GetWidth() + h = img.GetHeight() wx.StaticBitmap(self, -1, im, ( ((max(w, 300) - w) / 2), ((max(h, 200) - h) / 2) ), (w, h)) self.toImage("tmp.png") - img = Image.open("tmp.png") - w, h = img.size - app = wx.App(redirect = True) + img = wx.Image("tmp.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap() + w = img.GetWidth() + h = img.GetHeight() + app = wx.App(redirect = False) frame = wx.Frame(None, -1, "TPL (" + str(w) + ", " + str(h) + ")", size = (max(w, 300), max(h, 200))) image = imp(frame, -1, img) frame.Show(True) From 002b254056704913b828365809b7d2855d73a2c9 Mon Sep 17 00:00:00 2001 From: Alex Marshall Date: Tue, 23 Jun 2009 01:50:23 -0700 Subject: [PATCH 2/2] Added netConfig class, which handles the file on NAND /shared2/sys/net/02/config.dat, which is responsible for most network configuration. --- formats.py | 241 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) diff --git a/formats.py b/formats.py index 0743431..3ac1c56 100644 --- a/formats.py +++ b/formats.py @@ -301,6 +301,247 @@ class CONF: """sets the Serial Number key. (Shortcut for setKeyValue("SERNO", value))""" return self.setKeyValue("SERNO", value) +class netConfig: + """This class performs network configuration. The file is located on the NAND at /shared2/sys/net/02/config.dat.""" + class configEntry(Struct): + __endian__ = Struct.BE + def __format__(self): + self.selected = Struct.uint8 + self.padding_1 = Struct.string(1987) + self.ssid = Struct.string(32) + self.padding_2 = Struct.uint8 + self.ssid_len = Struct.uint8 + self.padding_3 = Struct.string(2) + self.padding_4 = Struct.uint8 + self.encryption = Struct.uint8 # OPEN: 0x00, WEP: 0x01, WPA-PSK (TKIP): 0x04, WPA2-PSK (AES): 0x05, WPA-PSK (AES): 0x06 + self.padding_5 = Struct.string(2) + self.padding_6 = Struct.uint8 + self.key_len = Struct.uint8 + self.padding_7 = Struct.string(2) + self.key = Struct.string(64) + self.padding_3 = Struct.string(236) + + def __init__(self, conf): + self.f = conf + if(not os.path.isfile(self.f)): + fp = open(self.f, "wb") + fp.write("\x00\x00\x00\x00\x01\x07\x00\x00") + fp.write("\x00" * 0x91C * 3) + fp.close() + fp = open(self.f, "rb") + head = fp.read(8) + if(head != "\x00\x00\x00\x00\x01\x07\x00\x00"): + print("Config file is invalid!\n") + + def getNotBlank(self, config): + fp = open(self.f, "rb") + fp.seek(8 + (0x91C * config)) + sel = unpack(">B", fp.read(1))[0] + fp.close() + if(sel & 0x20): + return 1 + return 0 + + + def getIPType(self, config): + if(not self.getNotBlank(config)): + return None + fp = open(self.f, "rb") + fp.seek(8 + (0x91C * config)) + sel = unpack(">B", fp.read(1))[0] + if(sel & 0x04): + return 0 + else: + return 1 + fp.close() + return sel + + def getWireType(self, config): + if(not self.getNotBlank(config)): + return None + fp = open(self.f, "rb") + fp.seek(8 + (0x91C * config)) + sel = unpack(">B", fp.read(1))[0] + if(sel & 0x02): + return 0 + else: + return 1 + fp.close() + + def getSSID(self, config): + if(not self.getNotBlank(config)): + return None + fp = open(self.f, "rb") + fp.seek(8 + (0x91C * config) + 2021) + len = unpack(">B", fp.read(1))[0] + fp.seek(8 + (0x91C * config) + 1988) + ssid = fp.read(len) + fp.close() + return ssid + + def getEncryptionType(self, config): + if(not self.getNotBlank(config)): + return None + fp = open(self.f, "rb") + fp.seek(8 + (0x91C * config) + 2025) + crypt = unpack(">B", fp.read(1))[0] + type = "" + if(crypt == 0): + type = "OPEN" + elif(crypt == 1): + type = "WEP" + elif(crypt == 4): + type = "WPA (TKIP)" + elif(crypt == 5): + type = "WPA2" + elif(crypt == 6): + type = "WPA (AES)" + else: + print("Invalid crypto type %02X. Valid types are: ``OPEN'', ``WEP'', ``WPA (TKIP)'', ``WPA2'', or ``WPA (AES)''\n" % crypt) + fp.close() + return None + fp.close() + return type + + def getEncryptionKey(self, config): + if(not self.getNotBlank(config)): + return None + fp = open(self.f, "rb") + fp.seek(8 + (0x91C * config) + 2025) + crypt = unpack(">B", fp.read(1))[0] + type = "" + if(crypt == 0): + type = "OPEN" + elif(crypt == 1): + type = "WEP" + elif(crypt == 4): + type = "WPA (TKIP)" + elif(crypt == 5): + type = "WPA2" + elif(crypt == 6): + type = "WPA (AES)" + else: + print("Invalid crypto type %02X. Valid types are: ``OPEN'', ``WEP'', ``WPA (TKIP)'', ``WPA2'', or ``WPA (AES)''\n" % crypt) + fp.close() + return None + if(crypt != "\x00"): + fp.seek(8 + (0x91C * config) + 2029) + keylen = unpack(">B", fp.read(1))[0] + fp.seek(8 + (0x91C * config) + 2032) + key = fp.read(keylen) + fp.close() + return key + fp.close() + return None + + def clearConfig(self, config): + fp = open(self.f, "rb+") + fp.seek(8 + (0x91C * config)) + sel = unpack(">B", fp.read(1))[0] + sel &= 0xDF + fp.seek(8 + (0x91C * config)) + fp.write(pack(">B", sel)) + fp.close() + + def setNotBlank(self, config): + fp = open(self.f, "rb+") + fp.seek(8 + (0x91C * config)) + sel = unpack(">B", fp.read(1))[0] + sel |= 0x20 + fp.seek(8 + (0x91C * config)) + fp.write(pack(">B", sel)) + fp.close() + + def setIPType(self, config, static): + fp = open(self.f, "rb+") + fp.seek(8 + (0x91C * config)) + sel = unpack(">B", fp.read(1))[0] + if(not static): + sel |= 0x04 + else: + sel &= 0xFB + fp.seek(8 + (0x91C * config)) + fp.write(pack(">B", sel)) + fp.close() + self.setNotBlank(config) + + def setWireType(self, config, wired): + fp = open(self.f, "rb+") + fp.seek(8 + (0x91C * config)) + sel = unpack(">B", fp.read(1))[0] + if(not wired): + sel |= 0x02 + else: + sel &= 0xFD + fp.seek(8 + (0x91C * config)) + fp.write(pack(">B", sel)) + fp.close() + self.setNotBlank(config) + + def setSSID(self, config, ssid): + fp = open(self.f, "rb+") + fp.seek(8 + (0x91C * config) + 1988) + fp.write(ssid) + fp.seek(8 + (0x91C * config) + 2021) + fp.write(a2b_hex("%02X" % len(ssid))) + fp.close() + self.setNotBlank(config) + + def setEncryption(self, config, crypt, key): + fp = open(self.f, "rb+") + fp.seek(8 + (0x91C * config) + 2025) + if(crypt == "OPEN"): + fp.write("\x00") + elif(crypt == "WEP"): + fp.write("\x01") + elif(crypt == "WPA (TKIP)"): + fp.write("\x04") + elif(crypt == "WPA2"): + fp.write("\x05") + elif(crypt == "WPA (AES)"): + fp.write("\x06") + else: + print("Invalid crypto type. Valid types are: ``OPEN'', ``WEP'', ``WPA (TKIP)'', ``WPA2'', or ``WPA (AES)''\n") + fp.close() + return + if(crypt != "OPEN"): + fp.seek(8 + (0x91C * config) + 2029) + fp.write(a2b_hex("%02X" % len(key))) + fp.seek(8 + (0x91C * config) + 2032) + fp.write(key) + fp.close() + self.setNotBlank(config) + + def selectConfig(self, config): + fp = open(self.f, "rb+") + fp.seek(8 + (0x91C * 0)) + sel = unpack(">B", fp.read(1))[0] + if(config == 0): + sel |= 0x80 + else: + sel &= 0x7F + fp.seek(8 + (0x91C * 0)) + fp.write(pack(">B", sel)) + fp.seek(8 + (0x91C * 1)) + sel = unpack(">B", fp.read(1))[0] + if(config == 1): + sel |= 0x80 + else: + sel &= 0x7F + fp.seek(8 + (0x91C * 1)) + fp.write(pack(">B", sel)) + fp.seek(8 + (0x91C * 2)) + sel = unpack(">B", fp.read(1))[0] + if(config == 2): + sel |= 0x80 + else: + sel &= 0x7F + fp.seek(8 + (0x91C * 2)) + fp.write(pack(">B", sel)) + self.setNotBlank(config) + fp.close() + + class ContentMap: """This class performs all content.map related actions. Has functions to add contents, and find contents by hash. The ``map'' parameter is the location of the content.map file."""