def test_utf_16_be_decode(self): string, num_processed = codecs.utf_16_be_decode(b'\0a\0b\0c') self.assertEqual(string, "abc") self.assertEqual(num_processed, 3 * 2) string, num_processed = codecs.utf_16_be_decode(codecs.BOM_UTF16_BE + b'\0a\0b\0c') self.assertEqual(string, "\uFEFFabc") self.assertEqual(num_processed, 4 * 2)
def test_utf_16_be_decode_incremental(self): b = b"\xff\xfe\x00\x41\xd9\x00\xdd\x00\xdc\x00\xd8\x00\xdc\x00" expected = [ ('', 0), ('', 0), ('\ufffe', 2), ('\ufffe', 2), ('\ufffeA', 4), ('\ufffeA', 4), ('\ufffeA', 4), ('\ufffeA', 4), ('\ufffeA\U00050100', 8), ('\ufffeA\U00050100', 8), ('\ufffeA\U00050100\ufffd', 10), ('\ufffeA\U00050100\ufffd', 10), ('\ufffeA\U00050100\ufffd', 10), ('\ufffeA\U00050100\ufffd', 10), ('\ufffeA\U00050100\ufffd\U00010000', 14) ] if not is_cli: # CPython's strings are UTF-32 so an invalid surrogate pair results in one replacement char. # Therefore CPython cannot report error on a dangling low surrogate until it verifies # that the next char is not an invalid surrogate as well. expected[10] = expected[11] = ('\ufffeA\U00050100', 8) for i in range(len(b) + 1): res = codecs.utf_16_be_decode(b[:i], 'replace') self.assertEqual(res, expected[i])
def GetMCStatus(hostname, port=25565, timeout=0.5): """Query a minecraft beta server. Args: hostname: (String) hostname of the server to connect to port: (integer) port number to connect to timeout: (float) timeout in seconds for making a TCP connection Returns: list: If successful, something like: [True, 'MOTD here', 5, 20] # Online, MOTD, cur_players, max_players If unsuccessful, returns [False] """ logging.debug('Trying to establish connection to %s:%d with timeout %f', hostname, port, timeout) try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ip = socket.gethostbyname(hostname) s.connect((ip, port)) s.settimeout(timeout) s.send(chr(254)) data, _ = s.recvfrom(2048) if data[0] == chr(255): data, _ = codecs.utf_16_be_decode(data[1:]) data = data[1:] p = data.split(u'\xa7') return [True] + p except socket.error: logging.exception('Socket error when querying from %s', hostname) return [False]
def get_server_info(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ip = socket.gethostbyname(self.host) try: s.settimeout(self.timeout) s.connect((ip, self.port)) s.send(bytearray([0xFE, 0x01])) data_raw = s.recv(1024) s.close() # 两种解码方式(cp437)(utf-16),切换释放注释并注释掉另一种 # (cp437) # data = data_raw.decode('cp437').split('\x00\x00\x00') # (utf-16)这个可以看到MOTD和其他信息 data = codecs.utf_16_be_decode(data_raw[1:])[0].split('\x00') data[3] = re.sub(r'§\w', "", data[3]).replace(" ", "").replace('\n', ",") server_info = { '0': data[0].replace("\x00", ""), '1': data[1].replace("\x00", ""), 'version': data[2].replace("\x00", ""), 'MOTD': data[3].replace("\x00", ""), 'online_players': data[4].replace("\x00", ""), 'max_players': data[5].replace("\x00", "") } # print(server_info) return server_info, "%s:%s" % (self.host, self.port) except socket.error: return False, "%s:%s" % (self.host, self.port)
def decode(input: bytes, errors: str = "strict") -> typing.Tuple[str, int]: """ return a string decoded from the given bytes and its length. Parameters: input: the bytes to decode errors: same meaning as the errors argument to pythons' `encode <https://docs.python.org/3/library/codecs.html#codecs.encode>`_ method """ return codecs.utf_16_be_decode(input, errors)
def unpack_string(data): ''' Extracts a string from a data stream. Like in the pack method, UCS-2 isn't handled correctly. Since usernames and hosts can't contain special characters this isn't an issue. ''' (l,) = unpack(">h", data[:2]) assert len(data) >= 2 + 2 * l return utf_16_be_decode(data[2:l * 2])[0]
def GetMCStatus(hostname, port=25565, timeout=8): logging.debug('Trying to establish connection to %s:%d with timeout %f', hostname, port, timeout) try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ip = socket.gethostbyname(hostname) s.settimeout(timeout) s.connect((ip, port)) s.send(chr(254)) data, _ = s.recvfrom(2048) if data[0] == chr(255): data, _ = codecs.utf_16_be_decode(data[1:]) data = data[1:] p = data.split(u'\xa7') return [True] + p except socket.error: pass return [False]
def test_codecs_builtins(self): s = "abc" encoded = codecs.utf_8_encode(s) self.assertEqual(s, codecs.utf_8_decode(encoded[0])[0]) encoded = codecs.utf_7_encode(s) self.assertEqual(s, codecs.utf_7_decode(encoded[0])[0]) encoded = codecs.utf_16_encode(s) self.assertEqual(s, codecs.utf_16_decode(encoded[0])[0]) encoded = codecs.utf_16_le_encode(s) self.assertEqual(s, codecs.utf_16_le_decode(encoded[0])[0]) encoded = codecs.utf_16_be_encode(s) self.assertEqual(s, codecs.utf_16_be_decode(encoded[0])[0]) encoded = codecs.utf_32_encode(s) self.assertEqual(s, codecs.utf_32_decode(encoded[0])[0]) encoded = codecs.utf_32_le_encode(s) self.assertEqual(s, codecs.utf_32_le_decode(encoded[0])[0]) encoded = codecs.utf_32_be_encode(s) self.assertEqual(s, codecs.utf_32_be_decode(encoded[0])[0]) encoded = codecs.utf_32_be_encode(s) self.assertEqual(s, codecs.utf_32_be_decode(encoded[0])[0]) encoded = codecs.raw_unicode_escape_encode(s) self.assertEqual(s, codecs.raw_unicode_escape_decode(encoded[0])[0]) encoded = codecs.unicode_escape_encode(s) self.assertEqual(s, codecs.unicode_escape_decode(encoded[0])[0]) encoded = codecs.latin_1_encode(s) self.assertEqual(s, codecs.latin_1_decode(encoded[0])[0]) encoded = codecs.ascii_encode(s) self.assertEqual(s, codecs.ascii_decode(encoded[0])[0])
def parse(self, input): """parses the given file or file source string""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): qmsrc = input.read() input.close() input = qmsrc if len(input) < 16: raise ValueError("This is not a .qm file: file empty or too small") magic = struct.unpack(">4L", input[:16]) if magic != QM_MAGIC_NUMBER: raise ValueError("This is not a .qm file: invalid magic number") startsection = 16 sectionheader = 5 def section_debug(name, section_type, startsection, length): print "Section: %s (type: %#x, offset: %#x, length: %d)" % ( name, section_type, startsection, length) return while startsection < len(input): section_type, length = struct.unpack( ">BL", input[startsection:startsection + sectionheader]) if section_type == 0x42: #section_debug("Hash", section_type, startsection, length) hashash = True hash_start = startsection + sectionheader hash_data = struct.unpack( ">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) elif section_type == 0x69: #section_debug("Messages", section_type, startsection, length) hasmessages = True messages_start = startsection + sectionheader messages_data = struct.unpack( ">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) elif section_type == 0x2f: #section_debug("Contexts", section_type, startsection, length) hascontexts = True contexts_start = startsection + sectionheader contexts_data = struct.unpack( ">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) elif section_type == 0x88: #section_debug("NumerusRules", section_type, startsection, length) hasnumerusrules = True numerusrules_start = startsection + sectionheader numerusrules_data = struct.unpack( ">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) else: section_debug("Unkown", section_type, startsection, length) startsection = startsection + sectionheader + length pos = messages_start source = target = None while pos < messages_start + len(messages_data): subsection, = struct.unpack(">B", input[pos:pos + 1]) if subsection == 0x01: # End #print "End" pos = pos + 1 if not source is None and not target is None: newunit = self.addsourceunit(source) newunit.target = target source = target = None else: raise ValueError("Old .qm format with no source defined") continue #print pos, subsection pos = pos + 1 length, = struct.unpack(">l", input[pos:pos + 4]) if subsection == 0x03: # Translation if length != -1: raw, = struct.unpack(">%ds" % length, input[pos + 4:pos + 4 + length]) string, templen = codecs.utf_16_be_decode(raw) if target: target.strings.append(string) else: target = multistring(string) pos = pos + 4 + length else: target = "" pos = pos + 4 #print "Translation: %s" % target.encode('utf-8') elif subsection == 0x06: # SourceText source = input[pos + 4:pos + 4 + length].decode('iso-8859-1') #print "SourceText: %s" % source pos = pos + 4 + length elif subsection == 0x07: # Context context = input[pos + 4:pos + 4 + length].decode('iso-8859-1') #print "Context: %s" % context pos = pos + 4 + length elif subsection == 0x08: # Disambiguating-comment comment = input[pos + 4:pos + 4 + length] #print "Disambiguating-comment: %s" % comment pos = pos + 4 + length elif subsection == 0x05: # hash hash = input[pos:pos + 4] #print "Hash: %s" % hash pos = pos + 4 else: if subsection == 0x02: # SourceText16 subsection_name = "SourceText16" elif subsection == 0x04: # Context16 subsection_name = "Context16" else: subsection_name = "Unkown" print >> sys.stderr, "Unimplemented: %s %s" % \ (subsection, subsection_name) return
def decode(input, errors = 'strict'): return codecs.utf_16_be_decode(input, errors, True)
def parse(self, input): """Parses the given file or file source string.""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): qmsrc = input.read() input.close() input = qmsrc if len(input) < 16: raise ValueError("This is not a .qm file: file empty or too small") magic = struct.unpack(">4L", input[:16]) if magic != QM_MAGIC_NUMBER: raise ValueError("This is not a .qm file: invalid magic number") startsection = 16 sectionheader = 5 def section_debug(name, section_type, startsection, length): print("Section: %s (type: %#x, offset: %#x, length: %d)" % (name, section_type, startsection, length)) return while startsection < len(input): section_type, length = struct.unpack(">BL", input[startsection:startsection + sectionheader]) if section_type == 0x42: #section_debug("Hash", section_type, startsection, length) hashash = True hash_start = startsection + sectionheader hash_data = struct.unpack(">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) elif section_type == 0x69: #section_debug("Messages", section_type, startsection, length) hasmessages = True messages_start = startsection + sectionheader messages_data = struct.unpack(">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) elif section_type == 0x2f: #section_debug("Contexts", section_type, startsection, length) hascontexts = True contexts_start = startsection + sectionheader contexts_data = struct.unpack(">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) elif section_type == 0x88: #section_debug("NumerusRules", section_type, startsection, length) hasnumerusrules = True numerusrules_start = startsection + sectionheader numerusrules_data = struct.unpack(">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) else: section_debug("Unkown", section_type, startsection, length) startsection = startsection + sectionheader + length pos = messages_start source = target = None while pos < messages_start + len(messages_data): subsection, = struct.unpack(">B", input[pos:pos + 1]) if subsection == 0x01: # End pos = pos + 1 if source is not None and target is not None: newunit = self.addsourceunit(source) newunit.target = target source = target = None else: raise ValueError("Old .qm format with no source defined") continue pos = pos + 1 length, = struct.unpack(">l", input[pos:pos + 4]) if subsection == 0x03: # Translation if length != -1: raw, = struct.unpack(">%ds" % length, input[pos + 4:pos + 4 + length]) string, templen = codecs.utf_16_be_decode(raw) if target: target.strings.append(string) else: target = multistring(string) pos = pos + 4 + length else: target = u"" pos = pos + 4 elif subsection == 0x06: # SourceText source = input[pos + 4:pos + 4 + length].decode('iso-8859-1') pos = pos + 4 + length elif subsection == 0x07: # Context context = input[pos + 4:pos + 4 + length].decode('iso-8859-1') pos = pos + 4 + length elif subsection == 0x08: # Disambiguating-comment comment = input[pos + 4:pos + 4 + length] pos = pos + 4 + length elif subsection == 0x05: # hash hash = input[pos:pos + 4] pos = pos + 4 else: if subsection == 0x02: # SourceText16 subsection_name = "SourceText16" elif subsection == 0x04: # Context16 subsection_name = "Context16" else: subsection_name = "Unknown" logger.warning("Unimplemented: 0x%x %s", subsection, subsection_name) return
def decode(self, input, errors="strict"): return codecs.utf_16_be_decode(input, errors) # pytype: disable=module-attr
def utf_16_be_decode(data, errors, finish=False): if not finish and len(data) % 2 == 1: data = data[:-1] return codecs.utf_16_be_decode(data, errors)
def test_utf_16_be_decode(self): #sanity new_str, size = codecs.utf_16_be_decode("abc") self.assertEqual(new_str, u'\u6162') self.assertEqual(size, 2)
def render_GET(self, request): if not self.is_authorized(request): return "" if request.path == "/whitelist": return self.render_whitelist(request) if request.path != "/banhammer": request.setResponseCode(500) return "wrong url path" sqlstatement = ('' 'select users.profileid,enabled,data,users.gameid,console,users.userid ' 'from nas_logins ' 'inner join users ' 'on users.userid = nas_logins.userid ' 'inner join ( ' ' select max(profileid) newestpid, userid, gameid, devname ' ' from users ' ' group by userid,gameid) ' 'ij on ij.userid = users.userid and ' 'users.profileid = ij.newestpid ' 'order by users.gameid ' '') dbconn = sqlite3.connect('gpcm.db') responsedata = ("<html><meta charset='utf-8'>\r\n" "<title>altwfc admin page</title>" '<a href="http://%20:%20@'+request.getHeader('host')+'">[CLICK HERE TO LOG OUT]</a>' "<br><br>" '<a href="http://'+request.getHeader('host')+'/whitelist">WhiteList</a>' "<table border='1'>" "<tr><td>ingamesn or devname</td><td>gameid</td>" "<td>Enabled</td><td>newest dwc_pid</td>" "<td>gsbrcd</td><td>userid</td></tr>\r\n") for row in dbconn.cursor().execute(sqlstatement): dwc_pid = str(row[0]) enabled = str(row[1]) nasdata = collections.defaultdict(lambda: '', json.loads(row[2])) gameid = str(row[3]) is_console = int(str(row[4])) userid = str(row[5]) gsbrcd = str(nasdata['gsbrcd']) ingamesn = '' if 'ingamesn' in nasdata: ingamesn = str(nasdata['ingamesn']) elif 'devname' in nasdata: ingamesn = str(nasdata['devname']) if ingamesn: ingamesn = gs_utils.base64_decode(ingamesn) if is_console: ingamesn = codecs.utf_16_be_decode(ingamesn)[0] else: ingamesn = codecs.utf_16_le_decode(ingamesn)[0] else: ingamesn = '[NOT AVAILABLE]' responsedata += "<tr>" responsedata += "<td>"+ingamesn+"</td>" responsedata += "<td>"+gameid+"</td>" responsedata += "<td>"+enabled+"</td>" responsedata += "<td>"+dwc_pid+"</td>" responsedata += "<td>"+gsbrcd+"</td>" responsedata += "<td>"+userid+"</td>" if enabled == "1": responsedata += ("<td><form action='disableuser' method='POST'>" "<input type='hidden' name='userid' value='"+userid+"'>" "<input type='hidden' name='gameid' value='"+gameid+"'>" "<input type='hidden' name='ingamesn' value='"+ingamesn+"'>" "<input type='submit' value='Ban'></form></td></tr>") else: responsedata += ("<td><form action='enableuser' method='POST'>" "<input type='hidden' name='userid' value='"+userid+"'>" "<input type='hidden' name='gameid' value='"+gameid+"'>" "<input type='hidden' name='ingamesn' value='"+ingamesn+"'>" "<input type='submit' value='----- unban -----'></form></td></tr>") responsedata += "</table></html>" dbconn.close() request.setHeader("Content-Type", "text/html; charset=utf-8") return responsedata.encode('utf-8')
def unpack_ucs2_bytes(s): return codecs.utf_16_be_decode(s.decode('hex'))[0]
def render_blacklist(self, request): sqlstatement = """ SELECT users.profileid, enabled, data, users.gameid, console, users.userid FROM nas_logins INNER JOIN users ON users.userid = nas_logins.userid INNER JOIN ( SELECT max(profileid) newestpid, userid, gameid, devname FROM users GROUP BY userid, gameid ) ij ON ij.userid = users.userid AND users.profileid = ij.newestpid ORDER BY users.gameid""" dbconn = sqlite3.connect('gpcm.db') banned_list = [] for row in dbconn.cursor().execute("SELECT * FROM BANNED"): banned_list.append(str(row[0])+":"+str(row[1])) responsedata = """ <a href="http://%%20:%%20@%s">[CLICK HERE TO LOG OUT]</a> <br><br> <table border='1'>" <tr> <td>ingamesn or devname</td> <td>gameid</td> <td>Enabled</td> <td>newest dwc_pid</td>" <td>gsbrcd</td> <td>userid</td> <td>ipAddr</td> </tr>""" % request.getHeader('host') for row in dbconn.cursor().execute(sqlstatement): dwc_pid = str(row[0]) enabled = str(row[1]) nasdata = collections.defaultdict(lambda: '', json.loads(row[2])) gameid = str(row[3]) is_console = int(str(row[4])) userid = str(row[5]) gsbrcd = str(nasdata['gsbrcd']) ipaddr = str(nasdata['ipaddr']) ingamesn = '' if 'ingamesn' in nasdata: ingamesn = str(nasdata['ingamesn']) elif 'devname' in nasdata: ingamesn = str(nasdata['devname']) if ingamesn: ingamesn = gs_utils.base64_decode(ingamesn) if is_console: ingamesn = codecs.utf_16_be_decode(ingamesn)[0] else: ingamesn = codecs.utf_16_le_decode(ingamesn)[0] else: ingamesn = '[NOT AVAILABLE]' responsedata += """ <tr> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> """ % (ingamesn, gameid, enabled, dwc_pid, gsbrcd, userid, ipaddr) if gameid[:-1] + ":" + ipaddr in banned_list: responsedata += """ <td> <form action='updatebanlist' method='POST'> <input type='hidden' name='gameid' value='%s'> <input type='hidden' name='ipaddr' value='%s'> <input type='hidden' name='action' value='unban'> <input type='submit' value='----- unban -----'> </form> </td> </tr>""" % (gameid, ipaddr) else: responsedata += """ <td> <form action='updatebanlist' method='POST'> <input type='hidden' name='gameid' value='%s'> <input type='hidden' name='ipaddr' value='%s'> <input type='hidden' name='action' value='ban'> <input type='submit' value='Ban'> </form> </td> </tr> """ % (gameid, ipaddr) responsedata += "</table>" dbconn.close() request.setHeader("Content-Type", "text/html; charset=utf-8") return responsedata.encode('utf-8')
def decode(self, input, errors='strict'): return codecs.utf_16_be_decode(input, errors)
def utf_16_be_decode(data, errors, finish = False): if not finish and len(data) % 2 == 1: data = data[:-1] return codecs.utf_16_be_decode(data, errors)
def render_blacklist(self, request): sqlstatement = ( '' 'select users.profileid,enabled,data,users.gameid,console,users.userid ' 'from nas_logins ' 'inner join users ' 'on users.userid = nas_logins.userid ' 'inner join ( ' ' select max(profileid) newestpid, userid, gameid, devname ' ' from users ' ' group by userid,gameid) ' 'ij on ij.userid = users.userid and ' 'users.profileid = ij.newestpid ' 'order by users.gameid ' '') dbconn = sqlite3.connect('gpcm.db') banned_list = [] for row in dbconn.cursor().execute("SELECT * FROM BANNED"): banned_list.append(str(row[0]) + ":" + str(row[1])) responsedata = ( "" '<a href="http://%20:%20@' + request.getHeader('host') + '">[CLICK HERE TO LOG OUT]</a>' "<br><br>" "<table border='1'>" "<tr><td>ingamesn or devname</td><td>gameid</td>" "<td>Enabled</td><td>newest dwc_pid</td>" "<td>gsbrcd</td><td>userid</td><td>ipAddr</td></tr>\r\n") for row in dbconn.cursor().execute(sqlstatement): dwc_pid = str(row[0]) enabled = str(row[1]) nasdata = collections.defaultdict(lambda: '', json.loads(row[2])) gameid = str(row[3]) is_console = int(str(row[4])) userid = str(row[5]) gsbrcd = str(nasdata['gsbrcd']) ipaddr = str(nasdata['ipaddr']) ingamesn = '' if 'ingamesn' in nasdata: ingamesn = str(nasdata['ingamesn']) elif 'devname' in nasdata: ingamesn = str(nasdata['devname']) if ingamesn: ingamesn = gs_utils.base64_decode(ingamesn) if is_console: ingamesn = codecs.utf_16_be_decode(ingamesn)[0] else: ingamesn = codecs.utf_16_le_decode(ingamesn)[0] else: ingamesn = '[NOT AVAILABLE]' responsedata += "<tr>" responsedata += "<td>" + ingamesn + "</td>" responsedata += "<td>" + gameid + "</td>" responsedata += "<td>" + enabled + "</td>" responsedata += "<td>" + dwc_pid + "</td>" responsedata += "<td>" + gsbrcd + "</td>" responsedata += "<td>" + userid + "</td>" responsedata += "<td>" + ipaddr + "</td>" if gameid[:-1] + ":" + ipaddr in banned_list: responsedata += ( "<td><form action='updatebanlist' method='POST'>" "<input type='hidden' name='gameid' value='" + gameid + "'>" "<input type='hidden' name='ipaddr' value='" + ipaddr + "'>" "<input type='hidden' name='action' value='unban'>" "<input type='submit' value='----- unban -----'></form></td></tr>" ) else: responsedata += ( "<td><form action='updatebanlist' method='POST'>" "<input type='hidden' name='gameid' value='" + gameid + "'>" "<input type='hidden' name='ipaddr' value='" + ipaddr + "'>" "<input type='hidden' name='action' value='ban'>" "<input type='submit' value='Ban'></form></td></tr>") responsedata += "</table>" dbconn.close() request.setHeader("Content-Type", "text/html; charset=utf-8") return responsedata.encode('utf-8')
def render_GET(self, request): if not self.is_authorized(request): return "" if request.path == "/whitelist": return self.render_whitelist(request) if request.path != "/banhammer": request.setResponseCode(500) return "wrong url path" sqlstatement = ( '' 'select users.profileid,enabled,data,users.gameid,console,users.userid ' 'from nas_logins ' 'inner join users ' 'on users.userid = nas_logins.userid ' 'inner join ( ' ' select max(profileid) newestpid, userid, gameid, devname ' ' from users ' ' group by userid,gameid) ' 'ij on ij.userid = users.userid and ' 'users.profileid = ij.newestpid ' 'order by users.gameid ' '') dbconn = sqlite3.connect('gpcm.db') responsedata = ("<html><meta charset='utf-8'>\r\n" "<title>altwfc admin page</title>" '<a href="http://%20:%20@' + request.getHeader('host') + '">[CLICK HERE TO LOG OUT]</a>' "<br><br>" '<a href="http://' + request.getHeader('host') + '/whitelist">WhiteList</a>' "<table border='1'>" "<tr><td>ingamesn or devname</td><td>gameid</td>" "<td>Enabled</td><td>newest dwc_pid</td>" "<td>gsbrcd</td><td>userid</td></tr>\r\n") for row in dbconn.cursor().execute(sqlstatement): dwc_pid = str(row[0]) enabled = str(row[1]) nasdata = collections.defaultdict(lambda: '', json.loads(row[2])) gameid = str(row[3]) is_console = int(str(row[4])) userid = str(row[5]) gsbrcd = str(nasdata['gsbrcd']) ingamesn = '' if 'ingamesn' in nasdata: ingamesn = str(nasdata['ingamesn']) elif 'devname' in nasdata: ingamesn = str(nasdata['devname']) if ingamesn: ingamesn = gs_utils.base64_decode(ingamesn) if is_console: ingamesn = codecs.utf_16_be_decode(ingamesn)[0] else: ingamesn = codecs.utf_16_le_decode(ingamesn)[0] else: ingamesn = '[NOT AVAILABLE]' responsedata += "<tr>" responsedata += "<td>" + ingamesn + "</td>" responsedata += "<td>" + gameid + "</td>" responsedata += "<td>" + enabled + "</td>" responsedata += "<td>" + dwc_pid + "</td>" responsedata += "<td>" + gsbrcd + "</td>" responsedata += "<td>" + userid + "</td>" if enabled == "1": responsedata += ( "<td><form action='disableuser' method='POST'>" "<input type='hidden' name='userid' value='" + userid + "'>" "<input type='hidden' name='gameid' value='" + gameid + "'>" "<input type='hidden' name='ingamesn' value='" + ingamesn + "'>" "<input type='submit' value='Ban'></form></td></tr>") else: responsedata += ( "<td><form action='enableuser' method='POST'>" "<input type='hidden' name='userid' value='" + userid + "'>" "<input type='hidden' name='gameid' value='" + gameid + "'>" "<input type='hidden' name='ingamesn' value='" + ingamesn + "'>" "<input type='submit' value='----- unban -----'></form></td></tr>" ) responsedata += "</table></html>" dbconn.close() request.setHeader("Content-Type", "text/html; charset=utf-8") return responsedata.encode('utf-8')
def decode(input, errors='strict'): return codecs.utf_16_be_decode(input, errors, True)
def render_blacklist(self, request): sqlstatement = ('' 'select users.profileid,enabled,data,users.gameid,console,users.userid ' 'from nas_logins ' 'inner join users ' 'on users.userid = nas_logins.userid ' 'inner join ( ' ' select max(profileid) newestpid, userid, gameid, devname ' ' from users ' ' group by userid,gameid) ' 'ij on ij.userid = users.userid and ' 'users.profileid = ij.newestpid ' 'order by users.gameid ' '') dbconn = sqlite3.connect('gpcm.db') banned_list = [] for row in dbconn.cursor().execute("SELECT * FROM IP_BANNED"): banned_list.append(str(row[0])) responsedata = ("" '<a href="http://%20:%20@'+request.getHeader('host')+'">[CLICK HERE TO LOG OUT]</a>' "<br><br>" "<table border='1'>" "<tr><td>ingamesn or devname</td><td>gameid</td>" "<td>Enabled</td><td>newest dwc_pid</td>" "<td>gsbrcd</td><td>userid</td><td>ipAddr</td><td>macadr</td><td>cfc</td><td>csnum</td><td>Ban/Unban IP</td></tr>\r\n") for row in dbconn.cursor().execute(sqlstatement): dwc_pid = str(row[0]) enabled = str(row[1]) nasdata = collections.defaultdict(lambda: '', json.loads(row[2])) gameid = str(row[3]) is_console = int(str(row[4])) userid = str(row[5]) gsbrcd = str(nasdata['gsbrcd']) ipaddr = str(nasdata['ipaddr']) macadr = str(nasdata['macadr']) cfc = str(nasdata['cfc']) csnum = str(nasdata['csnum']) ingamesn = '' if 'ingamesn' in nasdata: ingamesn = str(nasdata['ingamesn']) elif 'devname' in nasdata: ingamesn = str(nasdata['devname']) if ingamesn: ingamesn = gs_utils.base64_decode(ingamesn) if is_console: ingamesn = codecs.utf_16_be_decode(ingamesn)[0] else: ingamesn = codecs.utf_16_le_decode(ingamesn)[0] else: ingamesn = '[NOT AVAILABLE]' responsedata += "<tr>" responsedata += "<td>"+ingamesn+"</td>" responsedata += "<td>"+gameid+"</td>" responsedata += "<td>"+enabled+"</td>" responsedata += "<td>"+dwc_pid+"</td>" responsedata += "<td>"+gsbrcd+"</td>" responsedata += "<td>"+userid+"</td>" responsedata += "<td>"+ipaddr+"</td>" responsedata += "<td>"+macadr+"</td>" responsedata += "<td>"+cfc+"</td>" responsedata +="<td>"+csnum+"</td>" if ipaddr in banned_list: responsedata += ("<td><form action='updatebanlist' method='POST'>" "<input type='hidden' name='gameid' value='"+gameid+"'>" "<input type='hidden' name='ipaddr' value='"+ipaddr+"'>" "<input type='hidden' name='action' value='unban'>" "<input type='submit' value='----- unban -----'></form></td></tr>") else: responsedata += ("<td><form action='updatebanlist' method='POST'>" "<input type='hidden' name='gameid' value='"+gameid+"'>" "<input type='hidden' name='ipaddr' value='"+ipaddr+"'>" "<input type='hidden' name='action' value='ban'>" "<input type='submit' value='Ban'></form></td></tr>") responsedata += "</table>" dbconn.close() request.setHeader("Content-Type", "text/html; charset=utf-8") return responsedata.encode('utf-8')
def readstring(): chars= unpack("!I") print mapToDict.offset, chars s = unpack("%ds"%chars) return codecs.utf_16_be_decode(s)[0]
def render_blacklist(self, request): sqlstatement = """ SELECT users.profileid, enabled, data, users.gameid, console, users.userid FROM nas_logins INNER JOIN users ON users.userid = nas_logins.userid INNER JOIN ( SELECT max(profileid) newestpid, userid, gameid, devname FROM users GROUP BY userid, gameid ) ij ON ij.userid = users.userid AND users.profileid = ij.newestpid ORDER BY users.gameid""" dbconn = sqlite3.connect('gpcm.db') banned_list = [] for row in dbconn.cursor().execute("SELECT * FROM BANNED"): banned_list.append(str(row[0]) + ":" + str(row[1])) responsedata = """ <a href="http://%%20:%%20@%s">[CLICK HERE TO LOG OUT]</a> <br><br> <table border='1'>" <tr> <td>ingamesn or devname</td> <td>gameid</td> <td>Enabled</td> <td>newest dwc_pid</td>" <td>gsbrcd</td> <td>userid</td> <td>ipAddr</td> </tr>""" % request.getHeader('host') for row in dbconn.cursor().execute(sqlstatement): dwc_pid = str(row[0]) enabled = str(row[1]) nasdata = collections.defaultdict(lambda: '', json.loads(row[2])) gameid = str(row[3]) is_console = int(str(row[4])) userid = str(row[5]) gsbrcd = str(nasdata['gsbrcd']) ipaddr = str(nasdata['ipaddr']) ingamesn = '' if 'ingamesn' in nasdata: ingamesn = str(nasdata['ingamesn']) elif 'devname' in nasdata: ingamesn = str(nasdata['devname']) if ingamesn: ingamesn = gs_utils.base64_decode(ingamesn) if is_console: ingamesn = codecs.utf_16_be_decode(ingamesn)[0] else: ingamesn = codecs.utf_16_le_decode(ingamesn)[0] else: ingamesn = '[NOT AVAILABLE]' responsedata += """ <tr> <td>%s</td> <td id="game">%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> """ % (ingamesn, gameid, enabled, dwc_pid, gsbrcd, userid, ipaddr) if gameid[:-1] + ":" + ipaddr in banned_list: responsedata += """ <td> <form action='updatebanlist' method='POST'> <input type='hidden' name='gameid' value='%s'> <input type='hidden' name='ipaddr' value='%s'> <input type='hidden' name='action' value='unban'> <input type='submit' value='----- unban -----'> </form> </td> </tr>""" % (gameid, ipaddr) else: responsedata += """ <td> <form action='updatebanlist' method='POST'> <input type='hidden' name='gameid' value='%s'> <input type='hidden' name='ipaddr' value='%s'> <input type='hidden' name='action' value='ban'> <input type='submit' value='Ban'> </form> </td> </tr> """ % (gameid, ipaddr) responsedata += "</table>" dbconn.close() request.setHeader("Content-Type", "text/html; charset=utf-8") return responsedata.encode('utf-8')
def parse(self, input): """Parses the given file or file source string.""" if hasattr(input, "name"): self.filename = input.name elif not getattr(self, "filename", ""): self.filename = "" if hasattr(input, "read"): qmsrc = input.read() input.close() input = qmsrc if len(input) < 16: raise ValueError("This is not a .qm file: file empty or too small") magic = struct.unpack(">4L", input[:16]) if magic != QM_MAGIC_NUMBER: raise ValueError("This is not a .qm file: invalid magic number") startsection = 16 sectionheader = 5 def section_debug(name, section_type, startsection, length): print("Section: %s (type: %#x, offset: %#x, length: %d)" % (name, section_type, startsection, length)) return while startsection < len(input): section_type, length = struct.unpack( ">BL", input[startsection:startsection + sectionheader]) if section_type == 0x42: # section_debug("Hash", section_type, startsection, length) # hash_start = startsection + sectionheader # hash_data = struct.unpack(">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) pass elif section_type == 0x69: # section_debug("Messages", section_type, startsection, length) messages_start = startsection + sectionheader messages_data = struct.unpack( ">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length], ) elif section_type == 0x2F: # section_debug("Contexts", section_type, startsection, length) # contexts_start = startsection + sectionheader # contexts_data = struct.unpack(">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) pass elif section_type == 0x88: # section_debug("NumerusRules", section_type, startsection, length) # numerusrules_start = startsection + sectionheader # numerusrules_data = struct.unpack(">%db" % length, input[startsection + sectionheader:startsection + sectionheader + length]) pass else: section_debug("Unkown", section_type, startsection, length) startsection = startsection + sectionheader + length pos = messages_start source = target = None while pos < messages_start + len(messages_data): (subsection, ) = struct.unpack(">B", input[pos:pos + 1]) if subsection == 0x01: # End pos = pos + 1 if source is not None and target is not None: newunit = self.addsourceunit(source) newunit.target = target source = target = None else: raise ValueError("Old .qm format with no source defined") continue pos = pos + 1 (length, ) = struct.unpack(">l", input[pos:pos + 4]) if subsection == 0x03: # Translation if length != -1: (raw, ) = struct.unpack(">%ds" % length, input[pos + 4:pos + 4 + length]) string, templen = codecs.utf_16_be_decode(raw) if target: target.strings.append(string) else: target = multistring(string) pos = pos + 4 + length else: target = "" pos = pos + 4 elif subsection == 0x06: # SourceText source = input[pos + 4:pos + 4 + length].decode("iso-8859-1") pos = pos + 4 + length elif subsection == 0x07: # Context # context = input[pos + 4:pos + 4 + length].decode('iso-8859-1') pos = pos + 4 + length elif subsection == 0x08: # Disambiguating-comment # comment = input[pos + 4:pos + 4 + length] pos = pos + 4 + length elif subsection == 0x05: # hash # hash = input[pos:pos + 4] pos = pos + 4 else: if subsection == 0x02: # SourceText16 subsection_name = "SourceText16" elif subsection == 0x04: # Context16 subsection_name = "Context16" else: subsection_name = "Unknown" logger.warning("Unimplemented: 0x%x %s", subsection, subsection_name) return
def update_event(self, inp=-1): self.set_output_val( 0, codecs.utf_16_be_decode(self.input(0), self.input(1), self.input(2)))