Changes between Version 66 and Version 67 of HashSourceCodes


Ignore:
Timestamp:
Jan 5, 2017, 10:11:09 AM (7 years ago)
Author:
os
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HashSourceCodes

    v66 v67  
    19691969}}}
    19701970
     1971== SWIFT 3 ==
     1972Source codes: https://github.com/niklasberglund/OpenSubtitlesHash.swift
     1973{{{
     1974//
     1975//  This Swift 3 version is based on Swift 2 version by eduo:
     1976//  https://gist.github.com/eduo/7188bb0029f3bcbf03d4
     1977//
     1978//  Created by Niklas Berglund on 2017-01-01.
     1979//
     1980import Foundation
     1981
     1982class OpenSubtitlesHash: NSObject {
     1983    static let chunkSize: Int = 65536
     1984   
     1985    struct VideoHash {
     1986        var fileHash: String
     1987        var fileSize: UInt64
     1988    }
     1989   
     1990    public class func hashFor(_ url: URL) -> VideoHash {
     1991        return self.hashFor(url.path)
     1992    }
     1993   
     1994    public class func hashFor(_ path: String) -> VideoHash {
     1995        var fileHash = VideoHash(fileHash: "", fileSize: 0)
     1996        let fileHandler = FileHandle(forReadingAtPath: path)!
     1997       
     1998        let fileDataBegin: NSData = fileHandler.readData(ofLength: chunkSize) as NSData
     1999        fileHandler.seekToEndOfFile()
     2000       
     2001        let fileSize: UInt64 = fileHandler.offsetInFile
     2002        if (UInt64(chunkSize) > fileSize) {
     2003            return fileHash
     2004        }
     2005       
     2006        fileHandler.seek(toFileOffset: max(0, fileSize - UInt64(chunkSize)))
     2007        let fileDataEnd: NSData = fileHandler.readData(ofLength: chunkSize) as NSData
     2008       
     2009        var hash: UInt64 = fileSize
     2010       
     2011        var data_bytes = UnsafeBufferPointer<UInt64>(
     2012            start: UnsafePointer(fileDataBegin.bytes.assumingMemoryBound(to: UInt64.self)),
     2013            count: fileDataBegin.length/MemoryLayout<UInt64>.size
     2014        )
     2015       
     2016        hash = data_bytes.reduce(hash,&+)
     2017       
     2018        data_bytes = UnsafeBufferPointer<UInt64>(
     2019            start: UnsafePointer(fileDataEnd.bytes.assumingMemoryBound(to: UInt64.self)),
     2020            count: fileDataEnd.length/MemoryLayout<UInt64>.size
     2021        )
     2022       
     2023        hash = data_bytes.reduce(hash,&+)
     2024       
     2025        fileHash.fileHash = String(format:"%016qx", arguments: [hash])
     2026        fileHash.fileSize = fileSize
     2027       
     2028        fileHandler.closeFile()
     2029       
     2030        return fileHash
     2031    }
     2032}
     2033
     2034// Usage example:
     2035// let videoUrl = Bundle.main.url(forResource: "dummy5", withExtension: "rar")
     2036// let videoHash = OpenSubtitlesHash.hashFor(videoUrl!)
     2037// debugPrint("File hash: \(videoHash.fileHash)\nFile size: \(videoHash.fileSize)")
     2038
     2039}}}
     2040
    19712041
    19722042== RUST ==