More python3 fixes
authorNIIBE Yutaka <gniibe@fsij.org>
Mon, 3 Aug 2015 07:44:38 +0000 (16:44 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Mon, 3 Aug 2015 07:44:38 +0000 (16:44 +0900)
ChangeLog
tool/gnuk_token.py
tool/gnuk_upgrade.py
tool/upgrade_by_passwd.py
tool/usb_strings.py

index d453852..de36acd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2015-08-03  Niibe Yutaka  <gniibe@fsij.org>
+
+       * tool/gnuk_token.py (gnuk_token.download, gnuk_token.execute)
+       (regnual.download): Python3 fix.
+       * tool/upgrade_by_passwd.py (maian): Python3 fix.
+       * tool/usb_strings.py (main): Python3 fix.
+
 2015-07-31  Niibe Yutaka  <gniibe@fsij.org>
 
        * src/configure (output_vendor_product_serial_strings): Fix sed
index f46c5c1..393fbf9 100644 (file)
@@ -114,7 +114,7 @@ class gnuk_token(object):
 
     def mem_info(self):
         mem = self.__devhandle.controlMsg(requestType = 0xc0, request = 0,
-                                          value = 0, index = 0, buffer = 8,
+                                          buffer = 8, value = 0, index = 0,
                                           timeout = 10)
         start = ((mem[3]*256 + mem[2])*256 + mem[1])*256 + mem[0]
         end = ((mem[7]*256 + mem[6])*256 + mem[5])*256 + mem[4]
@@ -123,7 +123,7 @@ class gnuk_token(object):
     def download(self, start, data, verbose=False):
         addr = start
         addr_end = (start + len(data)) & 0xffffff00
-        i = (addr - 0x20000000) / 0x100
+        i = int((addr - 0x20000000) / 0x100)
         j = 0
         print("start %08x" % addr)
         print("end   %08x" % addr_end)
@@ -131,9 +131,8 @@ class gnuk_token(object):
             if verbose:
                 print("# %08x: %d : %d" % (addr, i, 256))
             self.__devhandle.controlMsg(requestType = 0x40, request = 1,
-                                        value = i, index = 0,
                                         buffer = data[j*256:j*256+256],
-                                        timeout = 10)
+                                        value = i, index = 0, timeout = 10)
             i = i+1
             j = j+1
             addr = addr + 256
@@ -142,15 +141,14 @@ class gnuk_token(object):
             if verbose:
                 print("# %08x: %d : %d" % (addr, i, residue))
             self.__devhandle.controlMsg(requestType = 0x40, request = 1,
-                                        value = i, index = 0,
                                         buffer = data[j*256:],
-                                        timeout = 10)
+                                        value = i, index = 0, timeout = 10)
 
     def execute(self, last_addr):
-        i = (last_addr - 0x20000000) / 0x100
+        i = int((last_addr - 0x20000000) / 0x100)
         o = (last_addr - 0x20000000) % 0x100
         self.__devhandle.controlMsg(requestType = 0x40, request = 2,
-                                    value = i, index = o, buffer = None,
+                                    buffer = None, value = i, index = o, 
                                     timeout = 10)
 
     def icc_get_result(self):
@@ -484,7 +482,7 @@ class regnual(object):
 
     def mem_info(self):
         mem = self.__devhandle.controlMsg(requestType = 0xc0, request = 0,
-                                          value = 0, index = 0, buffer = 8,
+                                          buffer = 8, value = 0, index = 0,
                                           timeout = 10000)
         start = ((mem[3]*256 + mem[2])*256 + mem[1])*256 + mem[0]
         end = ((mem[7]*256 + mem[6])*256 + mem[5])*256 + mem[4]
@@ -493,7 +491,7 @@ class regnual(object):
     def download(self, start, data, verbose=False):
         addr = start
         addr_end = (start + len(data)) & 0xffffff00
-        i = (addr - 0x08000000) / 0x100
+        i = int((addr - 0x08000000) / 0x100)
         j = 0
         print("start %08x" % addr)
         print("end   %08x" % addr_end)
@@ -501,23 +499,21 @@ class regnual(object):
             if verbose:
                 print("# %08x: %d: %d : %d" % (addr, i, j, 256))
             self.__devhandle.controlMsg(requestType = 0x40, request = 1,
-                                        value = 0, index = 0,
                                         buffer = data[j*256:j*256+256],
-                                        timeout = 10000)
+                                        value = 0, index = 0, timeout = 10000)
             crc32code = crc32(data[j*256:j*256+256])
             res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                              value = 0, index = 0, buffer = 4,
+                                              buffer = 4, value = 0, index = 0,
                                               timeout = 10000)
             r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
             if (crc32code ^ r_value) != 0xffffffff:
                 print("failure")
             self.__devhandle.controlMsg(requestType = 0x40, request = 3,
-                                        value = i, index = 0,
                                         buffer = None,
-                                        timeout = 10000)
+                                        value = i, index = 0, timeout = 10000)
             time.sleep(0.010)
             res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                              value = 0, index = 0, buffer = 4,
+                                              buffer = 4, value = 0, index = 0,
                                               timeout = 10000)
             r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
             if r_value == 0:
@@ -530,23 +526,21 @@ class regnual(object):
             if verbose:
                 print("# %08x: %d : %d" % (addr, i, residue))
             self.__devhandle.controlMsg(requestType = 0x40, request = 1,
-                                        value = 0, index = 0,
                                         buffer = data[j*256:],
-                                        timeout = 10000)
-            crc32code = crc32(data[j*256:].ljust(256,chr(255)))
+                                        value = 0, index = 0, timeout = 10000)
+            crc32code = crc32(data[j*256:].ljust(256,b'\xff'))
             res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                              value = 0, index = 0, buffer = 4,
+                                              buffer = 4, value = 0, index = 0,
                                               timeout = 10000)
             r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
             if (crc32code ^ r_value) != 0xffffffff:
                 print("failure")
             self.__devhandle.controlMsg(requestType = 0x40, request = 3,
-                                        value = i, index = 0,
                                         buffer = None,
-                                        timeout = 10000)
+                                        value = i, index = 0, timeout = 10000)
             time.sleep(0.010)
             res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                              value = 0, index = 0, buffer = 4,
+                                              buffer = 4, value = 0, index = 0, 
                                               timeout = 10000)
             r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
             if r_value == 0:
@@ -554,11 +548,11 @@ class regnual(object):
 
     def protect(self):
         self.__devhandle.controlMsg(requestType = 0x40, request = 4,
-                                    value = 0, index = 0, buffer = None,
+                                    buffer = None, value = 0, index = 0, 
                                     timeout = 10000)
         time.sleep(0.100)
         res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                          value = 0, index = 0, buffer = 4,
+                                          buffer = 4, value = 0, index = 0, 
                                           timeout = 10000)
         r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
         if r_value == 0:
@@ -566,7 +560,7 @@ class regnual(object):
 
     def finish(self):
         self.__devhandle.controlMsg(requestType = 0x40, request = 5,
-                                    value = 0, index = 0, buffer = None,
+                                    buffer = None, value = 0, index = 0, 
                                     timeout = 10000)
 
     def reset_device(self):
index f476287..22a8fe2 100755 (executable)
@@ -79,16 +79,16 @@ def gpg_sign(keygrip, hash):
 def main(keyno,keygrip, data_regnual, data_upgrade):
     l = len(data_regnual)
     if (l & 0x03) != 0:
-        data_regnual = data_regnual.ljust(l + 4 - (l & 0x03), chr(0))
+        data_regnual = data_regnual.ljust(l + 4 - (l & 0x03), b'\x00')
     crc32code = crc32(data_regnual)
-    print "CRC32: %04x\n" % crc32code
+    print("CRC32: %04x\n" % crc32code)
     data_regnual += pack('<I', crc32code)
     for (dev, config, intf) in gnuk_devices():
         try:
             icc = gnuk_token(dev, config, intf)
-            print "Device: ", dev.filename
-            print "Configuration: ", config.value
-            print "Interface: ", intf.interfaceNumber
+            print("Device: %s" % dev.filename)
+            print("Configuration: %d" % config.value)
+            print("Interface: %d" % intf.interfaceNumber)
             break
         except:
             icc = None
@@ -102,10 +102,10 @@ def main(keyno,keygrip, data_regnual, data_upgrade):
     icc.cmd_external_authenticate(keyno, signed)
     icc.stop_gnuk()
     mem_info = icc.mem_info()
-    print "%08x:%08x" % mem_info
-    print "Downloading flash upgrade program..."
+    print("%08x:%08x" % mem_info)
+    print("Downloading flash upgrade program...")
     icc.download(mem_info[0], data_regnual)
-    print "Run flash upgrade program..."
+    print("Run flash upgrade program...")
     icc.execute(mem_info[0] + len(data_regnual) - 4)
     #
     time.sleep(3)
@@ -113,20 +113,20 @@ def main(keyno,keygrip, data_regnual, data_upgrade):
     del icc
     icc = None
     #
-    print "Wait 3 seconds..."
+    print("Wait 3 seconds...")
     time.sleep(3)
     # Then, send upgrade program...
     reg = None
     for dev in gnuk_devices_by_vidpid():
         try:
             reg = regnual(dev)
-            print "Device: ", dev.filename
+            print("Device: %d" % dev.filename)
             break
         except:
             pass
     mem_info = reg.mem_info()
-    print "%08x:%08x" % mem_info
-    print "Downloading the program"
+    print("%08x:%08x" % mem_info)
+    print("Downloading the program")
     reg.download(mem_info[0], data_upgrade)
     reg.protect()
     reg.finish()
@@ -143,12 +143,12 @@ if __name__ == '__main__':
         sys.argv.pop(1)
     filename_regnual = sys.argv[1]
     filename_upgrade = sys.argv[2]
-    f = open(filename_regnual)
+    f = open(filename_regnual, "rb")
     data_regnual = f.read()
     f.close()
-    print "%s: %d" % (filename_regnual, len(data_regnual))
-    f = open(filename_upgrade)
+    print("%s: %d" % (filename_regnual, len(data_regnual)))
+    f = open(filename_upgrade, "rb")
     data_upgrade = f.read()
     f.close()
-    print "%s: %d" % (filename_upgrade, len(data_upgrade))
+    print("%s: %d" % (filename_upgrade, len(data_upgrade)))
     main(keyno, keygrip, data_regnual, data_upgrade[4096:])
index 49e13b3..a9ea320 100755 (executable)
@@ -110,11 +110,11 @@ if __name__ == '__main__':
         passwd = getpass("Admin password: ")
     filename_regnual = sys.argv[1]
     filename_upgrade = sys.argv[2]
-    f = open(filename_regnual)
+    f = open(filename_regnual,"rb")
     data_regnual = f.read()
     f.close()
     print("%s: %d" % (filename_regnual, len(data_regnual)))
-    f = open(filename_upgrade)
+    f = open(filename_upgrade,"rb")
     data_upgrade = f.read()
     f.close()
     print("%s: %d" % (filename_upgrade, len(data_upgrade)))
index e24c0f2..eaeeb31 100755 (executable)
@@ -3,7 +3,7 @@
 """
 usb_strings.py - a tool to dump USB string
 
-Copyright (C) 2012 Free Software Initiative of Japan
+Copyright (C) 2012, 2015 Free Software Initiative of Japan
 Author: NIIBE Yutaka <gniibe@fsij.org>
 
 This file is a part of Gnuk, a GnuPG USB Token implementation.
@@ -27,7 +27,7 @@ import usb, sys
 USB_VENDOR_FSIJ=0x234b
 USB_PRODUCT_GNUK=0x0000
 
-def gnuk_devices():
+def gnuk_devices_by_vidpid():
     busses = usb.busses()
     for bus in busses:
         devices = bus.devices
@@ -43,11 +43,11 @@ field = ['', 'Vendor', 'Product', 'Serial', 'Revision', 'Config', 'Sys', 'Board'
 def main(n):
     for dev in gnuk_devices():
         handle = dev.open()
-        print "Device: ", dev.filename
+        print("Device: " % dev.filename)
         try:
             for i in range(1,n):
-                str = handle.getString(i, 512)
-                print "%10s: %s" % (field[i], str)
+                s = handle.getString(i, 512)
+                print("%10s: %s" % (field[i], s.decode('UTF-8')))
         except:
             pass
         del dev