Selaa lähdekoodia

sizeof() parameter now is T.self

Marcin Krzyżanowski 9 vuotta sitten
vanhempi
commit
2e805df552

+ 1 - 1
Sources/CryptoSwift/AES.swift

@@ -326,7 +326,7 @@ extension AES {
                 tmp[wordIdx] = w[4*(i-1)+wordIdx]
             }
             if ((i % variant.Nk) == 0) {
-                tmp = subWord(rotateLeft(UInt32.with(bytes: tmp), by: 8).bytes(totalBytes: sizeof(UInt32)))
+                tmp = subWord(rotateLeft(UInt32.with(bytes: tmp), by: 8).bytes(totalBytes: sizeof(UInt32.self)))
                 tmp[0] = tmp.first! ^ Rcon[i/variant.Nk]
             } else if (variant.Nk > 6 && (i % variant.Nk) == 4) {
                 tmp = subWord(tmp)

+ 5 - 5
Sources/CryptoSwift/Generics.swift

@@ -36,14 +36,14 @@ func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T
 /// This method may be slow
 func integerWith<T:Integer where T:ByteConvertible, T: BitshiftOperationsType>(_ bytes: Array<UInt8>) -> T {
     var bytes = bytes.reversed() as Array<UInt8> //FIXME: check it this is equivalent of Array(...)
-    if bytes.count < sizeof(T) {
-        let paddingCount = sizeof(T) - bytes.count
+    if bytes.count < sizeof(T.self) {
+        let paddingCount = sizeof(T.self) - bytes.count
         if (paddingCount > 0) {
             bytes += Array<UInt8>(repeating: 0, count: paddingCount)
         }
     }
     
-    if sizeof(T) == 1 {
+    if sizeof(T.self) == 1 {
         return T(truncatingBitPattern: UInt64(bytes.first!))
     }
     
@@ -57,14 +57,14 @@ func integerWith<T:Integer where T:ByteConvertible, T: BitshiftOperationsType>(_
 /// Array of bytes, little-endian representation. Don't use if not necessary.
 /// I found this method slow
 func arrayOfBytes<T>(value:T, length:Int? = nil) -> Array<UInt8> {
-    let totalBytes = length ?? sizeof(T)
+    let totalBytes = length ?? sizeof(T.self)
 
     let valuePointer = UnsafeMutablePointer<T>(allocatingCapacity: 1)
     valuePointer.pointee = value
     
     let bytesPointer = UnsafeMutablePointer<UInt8>(valuePointer)
     var bytes = Array<UInt8>(repeating: 0, count: totalBytes)
-    for j in 0..<min(sizeof(T),totalBytes) {
+    for j in 0..<min(sizeof(T.self),totalBytes) {
         bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
     }
     

+ 1 - 1
Sources/CryptoSwift/IntExtension.swift

@@ -31,7 +31,7 @@ extension Int {
 /* array of bytes */
 extension Int {
     /** Array of bytes with optional padding (little-endian) */
-    public func bytes(totalBytes: Int = sizeof(Int)) -> Array<UInt8> {
+    public func bytes(totalBytes: Int = sizeof(Int.self)) -> Array<UInt8> {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 

+ 2 - 2
Sources/CryptoSwift/UInt32Extension.swift

@@ -18,7 +18,7 @@ extension UInt32: _UInt32Type {}
 
 /** array of bytes */
 extension UInt32 {
-    public func bytes(totalBytes: Int = sizeof(UInt32)) -> Array<UInt8> {
+    public func bytes(totalBytes: Int = sizeof(UInt32.self)) -> Array<UInt8> {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 
@@ -41,7 +41,7 @@ extension UInt32 {
             return
         }
         
-        let bitsCount = UInt32(sizeof(UInt32) * 8)
+        let bitsCount = UInt32(sizeof(UInt32.self) * 8)
         let shiftCount = Swift.min(count, bitsCount - 1)
         var shiftedValue:UInt32 = 0;
         

+ 1 - 1
Sources/CryptoSwift/UInt64Extension.swift

@@ -8,7 +8,7 @@
 
 /** array of bytes */
 extension UInt64 {
-    public func bytes(totalBytes: Int = sizeof(UInt64)) -> Array<UInt8> {
+    public func bytes(totalBytes: Int = sizeof(UInt64.self)) -> Array<UInt8> {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 

+ 1 - 1
Sources/CryptoSwift/UInt8Extension.swift

@@ -80,7 +80,7 @@ extension UInt8 {
             return
         }
 
-        let bitsCount = UInt8(sizeof(UInt8) * 8)
+        let bitsCount = UInt8(sizeof(UInt8.self) * 8)
 
         if (count >= bitsCount) {
             return

+ 2 - 2
Sources/CryptoSwift/Utils.swift

@@ -55,7 +55,7 @@ func reversed(_ uint32 : UInt32) -> UInt32 {
 func sliceToUInt32Array(_ slice: ArraySlice<UInt8>) -> Array<UInt32> {
     var result = Array<UInt32>()
     result.reserveCapacity(16)
-    for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt32)) {
+    for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt32.self)) {
         let val1:UInt32 = (UInt32(slice[idx.advanced(by: 3)]) << 24)
         let val2:UInt32 = (UInt32(slice[idx.advanced(by: 2)]) << 16)
         let val3:UInt32 = (UInt32(slice[idx.advanced(by: 1)]) << 8)
@@ -69,7 +69,7 @@ func sliceToUInt32Array(_ slice: ArraySlice<UInt8>) -> Array<UInt32> {
 func sliceToUInt64Array(_ slice: ArraySlice<UInt8>) -> Array<UInt64> {
     var result = Array<UInt64>()
     result.reserveCapacity(32)
-    for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt64)) {
+    for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt64.self)) {
         var val:UInt64 = 0
         val |= UInt64(slice[idx.advanced(by: 7)]) << 56
         val |= UInt64(slice[idx.advanced(by: 6)]) << 48