Changes between Version 32 and Version 33 of HashSourceCodes


Ignore:
Timestamp:
Jan 5, 2011, 11:15:58 AM (13 years ago)
Author:
os
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HashSourceCodes

    v32 v33  
    11601160Calc_Hash endp
    11611161}}}
     1162
     1163== Objective-C ==
     1164This is implementation of hash for Objective-C for Mac by subsmarine.com
     1165
     1166'''OSHashAlgorithm.m'''
     1167{{{
     1168#import "OSHashAlgorithm.h"
     1169
     1170
     1171@implementation OSHashAlgorithm
     1172
     1173+(NSString*)stringForHash:(uint64_t)hash
     1174{
     1175        return [[NSString stringWithFormat:@"%qx", hash ] autorelease];
     1176}
     1177+(VideoHash)hashForPath:(NSString*)path
     1178{
     1179        VideoHash hash;
     1180        hash.fileHash =0;
     1181        hash.fileSize =0;
     1182       
     1183        NSFileHandle *readFile = [NSFileHandle fileHandleForReadingAtPath:path];
     1184        hash = [OSHashAlgorithm hashForFile:readFile];
     1185        [readFile closeFile];
     1186        return hash;   
     1187}
     1188+(VideoHash)hashForURL:(NSURL*)url
     1189{
     1190        VideoHash hash;
     1191        hash.fileHash =0;
     1192        hash.fileSize =0;
     1193       
     1194        NSFileHandle *readfile = [NSFileHandle fileHandleForReadingFromURL:url error:NULL];
     1195        hash = [OSHashAlgorithm hashForFile:readfile];
     1196        return hash;
     1197}
     1198
     1199+(VideoHash)hashForFile:(NSFileHandle*)handle
     1200{
     1201        VideoHash retHash;
     1202        retHash.fileHash =0;
     1203        retHash.fileSize =0;
     1204       
     1205        if( handle == nil )
     1206                return retHash;
     1207       
     1208        const NSUInteger CHUNK_SIZE=65536;
     1209        NSData *fileDataBegin, *fileDataEnd;
     1210        uint64_t hash=0;
     1211       
     1212       
     1213        fileDataBegin = [handle readDataOfLength:(NSUInteger)CHUNK_SIZE];
     1214        [handle seekToEndOfFile];
     1215        unsigned long long fileSize = [handle offsetInFile];
     1216        if(fileSize < CHUNK_SIZE )
     1217                return retHash;
     1218       
     1219        [handle seekToFileOffset:MAX(0,fileSize-CHUNK_SIZE) ];
     1220        fileDataEnd = [handle readDataOfLength:(NSUInteger)CHUNK_SIZE];
     1221       
     1222        //
     1223        // Calculate hash
     1224        //
     1225       
     1226        // 1st. File size
     1227        hash += fileSize;
     1228        // 2nd. Begining data block
     1229        uint64_t * data_bytes= (uint64_t*)[fileDataBegin bytes];
     1230        for( int i=0; i< CHUNK_SIZE/sizeof(uint64_t); i++ )
     1231                hash+=data_bytes[i];;
     1232        // 3rd. Ending data block
     1233        data_bytes= (uint64_t*)[fileDataEnd bytes];
     1234        for( int i=0; i< CHUNK_SIZE/sizeof(uint64_t); i++ )
     1235                hash+= data_bytes[i];
     1236       
     1237        retHash.fileHash = hash;
     1238        retHash.fileSize = fileSize;
     1239       
     1240        return retHash;
     1241       
     1242}
     1243
     1244@end
     1245}}}
     1246
     1247
     1248
     1249'''OSHashAlgorithm.h'''
     1250{{{
     1251#import <Cocoa/Cocoa.h>
     1252
     1253typedef struct
     1254{
     1255        uint64_t fileHash;
     1256        uint64_t fileSize;
     1257} VideoHash;
     1258
     1259@interface OSHashAlgorithm : NSObject {
     1260
     1261}
     1262+(VideoHash)hashForPath:(NSString*)path;
     1263+(VideoHash)hashForURL:(NSURL*)url;
     1264+(VideoHash)hashForFile:(NSFileHandle*)handle;
     1265+(NSString*)stringForHash:(uint64_t)hash;
     1266
     1267@end
     1268}}}