Commits

Jens Alfke committed e75ce5d

Fixed some Clang warnings. Added MYChecksum (Adler32 checksum utilities for NSData.)

Comments (0)

Files changed (6)

     
     // Encode all the data
     BIO_write(mem, [self bytes], [self length]);
-    BIO_flush(mem);
+    (void)BIO_flush(mem);
     
     // Create a new string from the data in the memory buffer
     char * base64Pointer;
 {
     NSArray *addresses = [self my_callStackReturnAddresses];
     if( addresses ) {
-        unsigned n = [addresses count];
+        unsigned n = (unsigned) [addresses count];
         skip = MIN(skip,n);
         limit = MIN(limit,n-skip);
         addresses = [addresses subarrayWithRange: NSMakeRange(skip,limit)];
+//
+//  MYChecksum.h
+//  MYUtilities
+//
+//  Created by Jens Alfke on 7/3/10.
+//  Copyright 2010 Jens Alfke. All rights reserved.
+//
+
+#import <Foundation/NSData.h>
+
+
+/** NSData add-ons for generating and checking Adler32 checksums/digests/hashes.
+    From the ZLib documentation: "An Adler-32 checksum is almost as reliable as a CRC32
+    but can be computed much faster."
+    Using this category requires linking against libz.dylib. */
+@interface NSData (MYChecksum)
+
+/** Computes the Adler32 checksum/digest/hash of the data. */
+- (UInt32) my_adler32;
+
+/** Updates a running Adler32 checksum/digest/hash with this data. */
+- (UInt32) my_adler32: (UInt32)prevAdler32;
+
+@end
+//
+//  MYChecksum.m
+//  MYUtilities
+//
+//  Created by Jens Alfke on 7/3/10.
+//  Copyright 2010 Jens Alfke. All rights reserved.
+//
+
+#import "MYChecksum.h"
+#include <zlib.h>
+
+@implementation NSData (MYChecksum)
+
+
+/** Computes the Adler32 checksum/digest/hash of the data. */
+- (UInt32) my_adler32 {
+    UInt32 initialAdler = (UInt32) adler32(0L, Z_NULL, 0);
+    return (UInt32) [self my_adler32: initialAdler];
+}
+
+/** Updates a running Adler32 checksum/digest/hash with this data. */
+- (UInt32) my_adler32: (UInt32)prevAdler32 {
+    return (UInt32) adler32(prevAdler32, self.bytes, (uInt)self.length);
+}
+
+
+@end
 //  Copyright 2009 Jens Alfke. All rights reserved.
 //
 
-#import <Foundation/NSObject.h>
+#import <Foundation/NSError.h>
 @class NSError, NSString;
 
 
 BOOL MYMiscError( NSError **outError, NSString *messageFormat, ... )
                                 __attribute__ ((format (__NSString__, 2, 3)));
 
+/** Returns an NSError based on the global variable 'errno'. Useful with POSIX calls. */
+NSError *MYErrorFromErrno();
+
 /** Utilities for converting NSErrors to user-readable (but geeky) strings.
     If your code uses Security or SSL APIs, you will probably want to #define
     the symbol MYERRORUTILS_USE_SECURITY_API in your precompiled header, which
     a dependency on linking against Security.framework.)
  */
 NSString* MYPrintableErrorCode( int code );
-NSString* MYErrorName( NSString *domain, int code );
+NSString* MYErrorName( NSString *domain, NSInteger code );
 
 @interface NSError (MYUtilities)
 /** Prepends a message to the beginning of the receiver's existing message,
 }
 
 
+NSError *MYErrorFromErrno()
+{
+    int err = errno;
+    if (!err)
+        return nil;
+    return MYError(err, NSPOSIXErrorDomain, @"%s", strerror(err));
+}
+
+
 static NSString* printableOSType( OSType t ) {
     if (t < 0x20202020 || t > 0x7e7e7e7e)
         return nil;
 
 
 static NSString* printableErrorCode( NSInteger code ) {
-    if (code < -99999)
+    if ((OSStatus)code < -99999)
         return $sprintf(@"%u", code);       // CSSM errors are huge unsigned values > 0x80000000
     NSString *result = printableOSType((OSType)code);
     if (result)
         return result;                      // CoreAudio errors are OSTypes (4-char strings)
-    return $sprintf(@"%i", code);           // Default: OSStatus and errno values are signed
+    return $sprintf(@"%ld", code);          // Default: OSStatus and errno values are signed
 }
 
 static NSString* MYShortErrorDomainName( NSString *domain ) {
     if ($equal(domain,NSPOSIXErrorDomain)) {
         // Interpret POSIX errors via strerror
         // (which unfortunately returns a description, not the name of the constant)
-        const char *name = strerror(code);
+        const char *name = strerror((int)code);
         if (name) {
             result = [NSString stringWithCString: name encoding: NSASCIIStringEncoding];
             if ([result hasPrefix: @"Unknown error"])
         // If it's an OSStatus, check whether CarbonCore knows its name:
         const char *name = NULL;
 #if !TARGET_OS_IPHONE
-        name = GetMacOSStatusErrorString(code);
+        name = GetMacOSStatusErrorString((int)code);
 #endif
         if (name && *name)
             result = [NSString stringWithCString: name encoding: NSMacOSRomanStringEncoding];
     CAssertEqual(MYErrorName(nil,-1),     @"-1");
     CAssertEqual(MYErrorName(nil,12345),  @"12345");
     CAssertEqual(MYErrorName(nil,-12345), @"-12345");
-    CAssertEqual(MYErrorName(nil,2147549184u), @"2147549184");
+    CAssertEqual(MYErrorName(nil,2147549184u), @"2147549184");  // that's 0x80010000
     
     CAssertEqual(MYErrorName(@"foobar",0), nil);
     CAssertEqual(MYErrorName(@"foobar",'fmt?'), @"foobar fmt?");