Norio Nomura 8 years ago
parent
commit
f269a20fcc
1 changed files with 13 additions and 4 deletions
  1. 13 4
      Sources/Base32/Base32.swift

+ 13 - 4
Sources/Base32/Base32.swift

@@ -362,25 +362,34 @@ private func base32decode(_ string: String, _ table: [UInt8]) -> [UInt8]? {
         case 7:
         case 7:
             value6 = table[Int(encoded[6])]
             value6 = table[Int(encoded[6])]
             value5 = table[Int(encoded[5])]
             value5 = table[Int(encoded[5])]
-            decoded[4] = value6 << 5 | value7
             fallthrough
             fallthrough
         case 5:
         case 5:
             value4 = table[Int(encoded[4])]
             value4 = table[Int(encoded[4])]
-            decoded[3] = value4 << 7 | value5 << 2 | value6 >> 3
             fallthrough
             fallthrough
         case 4:
         case 4:
             value3 = table[Int(encoded[3])]
             value3 = table[Int(encoded[3])]
             value2 = table[Int(encoded[2])]
             value2 = table[Int(encoded[2])]
-            decoded[2] = value3 << 4 | value4 >> 1
             fallthrough
             fallthrough
         case 2:
         case 2:
             value1 = table[Int(encoded[1])]
             value1 = table[Int(encoded[1])]
             value0 = table[Int(encoded[0])]
             value0 = table[Int(encoded[0])]
+        default: break
+        }
+        switch remainEncodedLength {
+        case 7:
+            decoded[3] = value4 << 7 | value5 << 2 | value6 >> 3
+            fallthrough
+        case 5:
+            decoded[2] = value3 << 4 | value4 >> 1
+            fallthrough
+        case 4:
             decoded[1] = value1 << 6 | value2 << 1 | value3 >> 4
             decoded[1] = value1 << 6 | value2 << 1 | value3 >> 4
+            fallthrough
+        case 2:
             decoded[0] = value0 << 3 | value1 >> 2
             decoded[0] = value0 << 3 | value1 >> 2
         default: break
         default: break
         }
         }
-        
+
         return result
         return result
     }
     }
 }
 }