Skip to content
Browse files

Adds error reporting

  • Loading branch information...
1 parent 748c82c commit 4cebd1b8fbbb43cebda91646f24580031eff4a2a Bavarious committed Oct 25, 2011
View
24 BVPlistExtractor/BVPlistExtractor.h
@@ -23,6 +23,21 @@
#import <Foundation/Foundation.h>
+#ifndef __has_feature
+#define __has_feature(x) 0 // Compatibility with non-clang compilers
+#endif
+#ifndef __has_extension
+#define __has_extension __has_feature // Compatibility with pre-3.0 compilers
+#endif
+
+#if __has_feature(objc_arc)
+#define BVAUTORELEASE(obj) obj
+#define BVAUTORELEASING __autoreleasing
+#else
+#define BVAUTORELEASE(obj) [obj autorelease]
+#define BVAUTORELEASING
+#endif
+
// BVExtractPlist()
//
// Extracts a plist that has been embedded in the __TEXT __info_plist section
@@ -36,7 +51,6 @@
//
// error: if different from NULL and the return value is nil, an error object
// describing the reason why the plist couldn't be extracted.
-// *** Currently this parameter is not used
//
// RETURN VALUE:
//
@@ -52,4 +66,10 @@
// . Fat/universal containing at least one of
// Mach-O i386, Mach-O x86_64
-id BVExtractPlist(NSURL *url, NSError **error);
+id BVExtractPlist(NSURL *url, NSError * BVAUTORELEASING *error);
+
+// Constants for error reporting
+extern NSString * const BVPlistExtractorErrorDomain;
+extern const NSInteger BVPlistExtractorErrorOpenFile;
+extern const NSInteger BVPlistExtractorErrorEmptyFile;
+extern const NSInteger BVPlistExtractorErrorCorruptMachO;
View
166 BVPlistExtractor/BVPlistExtractor.m
@@ -21,6 +21,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
+#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -35,23 +36,25 @@
#include "BVPlistExtractor.h"
+#pragma mark - Declaration of private functions
static NSData *_BVMachOSection(NSURL *url, char *segname, char *sectname, NSError **error);
-static NSData *_BVMachOSectionFromMachOHeader(char *addr, long bytes_left, char *segname, char *sectname, NSError **error);
-static NSData *_BVMachOSectionFromMachOHeader32(char *addr, long bytes_left, char *segname, char *sectname, NSError **error);
-static NSData *_BVMachOSectionFromMachOHeader64(char *addr, long bytes_left, char *segname, char *sectname, NSError **error);
+static NSData *_BVMachOSectionFromMachOHeader(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error);
+static NSData *_BVMachOSectionFromMachOHeader32(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error);
+static NSData *_BVMachOSectionFromMachOHeader64(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error);
-// TODO: review this
-/*
-static inline bool _BVReadAddress(char **addr, size_t *bytes_left, size_t offset) {
- if (offset > bytes_left) return false;
-
- *addr += offset;
- *bytes_left -= offset;
-
- return true;
-}
- */
+#pragma mark - Declaration of private functions for error reporting
+static NSError *_BVPOSIXError(NSURL *url);
+static NSError *_BVGenericError(NSURL *url, NSString *fileQualifier, NSInteger errorCode);
+static NSError *_BVEmptyFileError(NSURL *url);
+static NSError *_BVCorruptMachOError(NSURL *url);
+#pragma mark - Definition of constants for error reporting
+NSString * const BVPlistExtractorErrorDomain = @"com.bavarious.PlistExtractor.ErrorDomain";
+const NSInteger BVPlistExtractorErrorOpenFile = 1;
+const NSInteger BVPlistExtractorErrorEmptyFile = 2;
+const NSInteger BVPlistExtractorErrorCorruptMachO = 3;
+
+#pragma mark - Definition of public Functions
id BVExtractPlist(NSURL *url, NSError **error) {
id plist = nil;
NSData *data = _BVMachOSection(url, "__TEXT", "__info_plist", error);
@@ -65,6 +68,7 @@ id BVExtractPlist(NSURL *url, NSError **error) {
return plist;
}
+#pragma mark - Definition of private functions
NSData *_BVMachOSection(NSURL *url, char *segname, char *sectname, NSError **error) {
NSData *data = nil;
int fd;
@@ -76,15 +80,31 @@ id BVExtractPlist(NSURL *url, NSError **error) {
// Open the file and get its size
fd = open([[url path] UTF8String], O_RDONLY);
- if (fd == -1) goto END_FUNCTION;
- if (fstat(fd, &stat_buf) == -1) goto END_FILE;
+ if (fd == -1) {
+ if (error) *error = _BVPOSIXError(url);
+ goto END_FUNCTION;
+ }
+
+ if (fstat(fd, &stat_buf) == -1) {
+ if (error) *error = _BVPOSIXError(url);
+ goto END_FILE;
+ }
+
size = stat_buf.st_size;
- if (size == 0) goto END_FILE;
+
+ if (size == 0) {
+ if (error) *error = _BVEmptyFileError(url);
+ goto END_FILE;
+ }
+
bytes_left = size;
// Map the file to memory
addr = start_addr = mmap(0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
- if (addr == MAP_FAILED) goto END_FILE;
+ if (addr == MAP_FAILED) {
+ if (error) *error = _BVPOSIXError(url);
+ goto END_FILE;
+ }
// Check if it's a fat file
// Make sure the file is long enough to hold a fat_header
@@ -99,23 +119,29 @@ id BVExtractPlist(NSURL *url, NSError **error) {
bytes_left -= sizeof(struct fat_header);
addr += sizeof(struct fat_header);
- if (bytes_left < (nfat_arch * sizeof(struct fat_arch))) goto END_MMAP;
+ if (bytes_left < (nfat_arch * sizeof(struct fat_arch))) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_MMAP;
+ }
// Read the architectures
for (int ifat_arch = 0; ifat_arch < nfat_arch; ifat_arch++) {
struct fat_arch *fa = (struct fat_arch *)addr;
int offset = NSSwapBigIntToHost(fa->offset);
addr += sizeof(struct fat_arch);
- if (bytes_left < offset) goto END_MMAP;
+ if (bytes_left < offset) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_MMAP;
+ }
- data = _BVMachOSectionFromMachOHeader(start_addr + offset, bytes_left, segname, sectname, error);
+ data = _BVMachOSectionFromMachOHeader(start_addr + offset, bytes_left, segname, sectname, url, error);
if (data) break;
}
}
// It's a thin file
else {
- data = _BVMachOSectionFromMachOHeader(start_addr, bytes_left,segname, sectname, error);
+ data = _BVMachOSectionFromMachOHeader(start_addr, bytes_left,segname, sectname, url, error);
}
END_MMAP:
@@ -128,26 +154,70 @@ id BVExtractPlist(NSURL *url, NSError **error) {
return data;
}
-NSData *_BVMachOSectionFromMachOHeader(char *addr, long bytes_left, char *segname, char *sectname, NSError **error) {
+NSData *_BVMachOSectionFromMachOHeader(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error) {
NSData *data = nil;
struct mach_header *mh;
- if (bytes_left < sizeof(struct mach_header)) return nil;
+ if (bytes_left < sizeof(struct mach_header)) {
+ if (error) *error = _BVCorruptMachOError(url);
+ return nil;
+ }
// The first bytes are the Mach-O header
mh = (struct mach_header *)addr;
if (mh->magic == MH_MAGIC) { // 32-bit
- data = _BVMachOSectionFromMachOHeader32(addr, bytes_left, segname, sectname, error);
+ data = _BVMachOSectionFromMachOHeader32(addr, bytes_left, segname, sectname, url, error);
}
else if (mh->magic == MH_MAGIC_64) { // 64-bit
- data = _BVMachOSectionFromMachOHeader64(addr, bytes_left, segname, sectname, error);
+ data = _BVMachOSectionFromMachOHeader64(addr, bytes_left, segname, sectname, url, error);
}
return data;
}
-NSData *_BVMachOSectionFromMachOHeader32(char *addr, long bytes_left, char *segname, char *sectname, NSError **error) {
+#pragma mark - Definition of private functions for error reporting
+NSError *_BVPOSIXError(NSURL *url) {
+ NSError *underlyingError = [[NSError alloc] initWithDomain:NSPOSIXErrorDomain
+ code:errno
+ userInfo:nil];
+ NSString *errorDescription = [NSString stringWithFormat:@"File %@ could not be opened. %s.",
+ [url path], strerror(errno)];
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ errorDescription, NSLocalizedDescriptionKey,
+ underlyingError, NSUnderlyingErrorKey,
+ [url path], NSFilePathErrorKey,
+ nil];
+ NSError *error = [[NSError alloc] initWithDomain:BVPlistExtractorErrorDomain
+ code:BVPlistExtractorErrorOpenFile
+ userInfo:userInfo];
+
+ return BVAUTORELEASE(error);
+}
+
+NSError *_BVGenericError(NSURL *url, NSString *fileQualifier, NSInteger errorCode) {
+ NSString *errorDescription = [NSString stringWithFormat:@"File %@ is %@.", [url path], fileQualifier];
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ errorDescription, NSLocalizedDescriptionKey,
+ [url path], NSFilePathErrorKey,
+ nil];
+ NSError *error = [[NSError alloc] initWithDomain:BVPlistExtractorErrorDomain
+ code:errorCode
+ userInfo:userInfo];
+
+ return BVAUTORELEASE(error);
+}
+
+NSError *_BVEmptyFileError(NSURL *url) {
+ return _BVGenericError(url, @"empty", BVPlistExtractorErrorEmptyFile);
+}
+
+NSError *_BVCorruptMachOError(NSURL *url) {
+ return _BVGenericError(url, @"corrupt", BVPlistExtractorErrorCorruptMachO);
+}
+
+#pragma mark - Instantiation of private template functions
+NSData *_BVMachOSectionFromMachOHeader32(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error) {
NSData *data = nil;
char *base_macho_header_addr = addr;
struct mach_header *mh = NULL;
@@ -157,20 +227,29 @@ id BVExtractPlist(NSURL *url, NSError **error) {
long bytes_left_from_macho_header = bytes_left;
- if (bytes_left < sizeof(struct mach_header)) return nil;
+ if (bytes_left < sizeof(struct mach_header)) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
mh = (struct mach_header *)addr;
addr += sizeof(struct mach_header);
bytes_left -= sizeof(struct mach_header);
for (int icmd = 0; icmd < mh->ncmds; icmd++) {
- if (bytes_left < 0) goto END_FUNCTION;
+ if (bytes_left < 0) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
lc = (struct load_command *)addr;
if (lc->cmdsize == 0) continue;
bytes_left -= lc->cmdsize;
- if (bytes_left < 0) goto END_FUNCTION;
+ if (bytes_left < 0) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
if (lc->cmd != LC_SEGMENT) {
addr += lc->cmdsize;
@@ -199,7 +278,10 @@ id BVExtractPlist(NSURL *url, NSError **error) {
if (strcmp(sectname, sect->sectname) != 0) continue;
// It's the section we want
- if (bytes_left_from_macho_header < (sect->offset + sect->size)) goto END_FUNCTION;
+ if (bytes_left_from_macho_header < (sect->offset + sect->size)) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
data = [NSData dataWithBytes:(base_macho_header_addr + sect->offset) length:sect->size];
goto END_FUNCTION;
}
@@ -209,7 +291,7 @@ id BVExtractPlist(NSURL *url, NSError **error) {
return data;
}
-NSData *_BVMachOSectionFromMachOHeader64(char *addr, long bytes_left, char *segname, char *sectname, NSError **error) {
+NSData *_BVMachOSectionFromMachOHeader64(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error) {
NSData *data = nil;
char *base_macho_header_addr = addr;
struct mach_header_64 *mh = NULL;
@@ -219,20 +301,29 @@ id BVExtractPlist(NSURL *url, NSError **error) {
long bytes_left_from_macho_header = bytes_left;
- if (bytes_left < sizeof(struct mach_header_64)) return nil;
+ if (bytes_left < sizeof(struct mach_header_64)) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
mh = (struct mach_header_64 *)addr;
addr += sizeof(struct mach_header_64);
bytes_left -= sizeof(struct mach_header_64);
for (int icmd = 0; icmd < mh->ncmds; icmd++) {
- if (bytes_left < 0) goto END_FUNCTION;
+ if (bytes_left < 0) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
lc = (struct load_command *)addr;
if (lc->cmdsize == 0) continue;
bytes_left -= lc->cmdsize;
- if (bytes_left < 0) goto END_FUNCTION;
+ if (bytes_left < 0) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
if (lc->cmd != LC_SEGMENT) {
addr += lc->cmdsize;
@@ -261,7 +352,10 @@ id BVExtractPlist(NSURL *url, NSError **error) {
if (strcmp(sectname, sect->sectname) != 0) continue;
// It's the section we want
- if (bytes_left_from_macho_header < (sect->offset + sect->size)) goto END_FUNCTION;
+ if (bytes_left_from_macho_header < (sect->offset + sect->size)) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
data = [NSData dataWithBytes:(base_macho_header_addr + sect->offset) length:sect->size];
goto END_FUNCTION;
}
View
22 BVPlistExtractor/_BVMachOSectionFromMachOHeader.m4
@@ -1,4 +1,4 @@
-NSData *BV_FUNCTION_NAME`'(char *addr, long bytes_left, char *segname, char *sectname, NSError **error) {
+NSData *BV_FUNCTION_NAME`'(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error) {
NSData *data = nil;
char *base_macho_header_addr = addr;
struct `mach_header'BV_STRUCT_SUFFIX *mh = NULL;
@@ -8,20 +8,29 @@ NSData *BV_FUNCTION_NAME`'(char *addr, long bytes_left, char *segname, char *sec
long bytes_left_from_macho_header = bytes_left;
- if (bytes_left < sizeof(struct `mach_header'BV_STRUCT_SUFFIX)) return nil;
+ if (bytes_left < sizeof(struct `mach_header'BV_STRUCT_SUFFIX)) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
mh = (struct `mach_header'BV_STRUCT_SUFFIX *)addr;
addr += sizeof(struct `mach_header'BV_STRUCT_SUFFIX);
bytes_left -= sizeof(struct `mach_header'BV_STRUCT_SUFFIX);
for (int icmd = 0; icmd < mh->ncmds; icmd++) {
- if (bytes_left < 0) goto END_FUNCTION;
+ if (bytes_left < 0) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
lc = (struct load_command *)addr;
if (lc->cmdsize == 0) continue;
bytes_left -= lc->cmdsize;
- if (bytes_left < 0) goto END_FUNCTION;
+ if (bytes_left < 0) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
if (lc->cmd != LC_SEGMENT) {
addr += lc->cmdsize;
@@ -50,7 +59,10 @@ NSData *BV_FUNCTION_NAME`'(char *addr, long bytes_left, char *segname, char *sec
if (strcmp(sectname, sect->sectname) != 0) continue;
// It's the section we want
- if (bytes_left_from_macho_header < (sect->offset + sect->size)) goto END_FUNCTION;
+ if (bytes_left_from_macho_header < (sect->offset + sect->size)) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_FUNCTION;
+ }
data = [NSData dataWithBytes:(base_macho_header_addr + sect->offset) length:sect->size];
goto END_FUNCTION;
}
View
112 BVPlistExtractor/_BVPlistExtractor.m4
@@ -21,6 +21,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
+#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -35,11 +36,25 @@
#include "BVPlistExtractor.h"
+#pragma mark - Declaration of private functions
static NSData *_BVMachOSection(NSURL *url, char *segname, char *sectname, NSError **error);
-static NSData *_BVMachOSectionFromMachOHeader(char *addr, long bytes_left, char *segname, char *sectname, NSError **error);
-static NSData *_BVMachOSectionFromMachOHeader32(char *addr, long bytes_left, char *segname, char *sectname, NSError **error);
-static NSData *_BVMachOSectionFromMachOHeader64(char *addr, long bytes_left, char *segname, char *sectname, NSError **error);
+static NSData *_BVMachOSectionFromMachOHeader(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error);
+static NSData *_BVMachOSectionFromMachOHeader32(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error);
+static NSData *_BVMachOSectionFromMachOHeader64(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error);
+#pragma mark - Declaration of private functions for error reporting
+static NSError *_BVPOSIXError(NSURL *url);
+static NSError *_BVGenericError(NSURL *url, NSString *fileQualifier, NSInteger errorCode);
+static NSError *_BVEmptyFileError(NSURL *url);
+static NSError *_BVCorruptMachOError(NSURL *url);
+
+#pragma mark - Definition of constants for error reporting
+NSString * const BVPlistExtractorErrorDomain = @"com.bavarious.PlistExtractor.ErrorDomain";
+const NSInteger BVPlistExtractorErrorOpenFile = 1;
+const NSInteger BVPlistExtractorErrorEmptyFile = 2;
+const NSInteger BVPlistExtractorErrorCorruptMachO = 3;
+
+#pragma mark - Definition of public Functions
id BVExtractPlist(NSURL *url, NSError **error) {
id plist = nil;
NSData *data = _BVMachOSection(url, "__TEXT", "__info_plist", error);
@@ -53,6 +68,7 @@ id BVExtractPlist(NSURL *url, NSError **error) {
return plist;
}
+#pragma mark - Definition of private functions
NSData *_BVMachOSection(NSURL *url, char *segname, char *sectname, NSError **error) {
NSData *data = nil;
int fd;
@@ -64,15 +80,31 @@ NSData *_BVMachOSection(NSURL *url, char *segname, char *sectname, NSError **err
// Open the file and get its size
fd = open([[url path] UTF8String], O_RDONLY);
- if (fd == -1) goto END_FUNCTION;
- if (fstat(fd, &stat_buf) == -1) goto END_FILE;
+ if (fd == -1) {
+ if (error) *error = _BVPOSIXError(url);
+ goto END_FUNCTION;
+ }
+
+ if (fstat(fd, &stat_buf) == -1) {
+ if (error) *error = _BVPOSIXError(url);
+ goto END_FILE;
+ }
+
size = stat_buf.st_size;
- if (size == 0) goto END_FILE;
+
+ if (size == 0) {
+ if (error) *error = _BVEmptyFileError(url);
+ goto END_FILE;
+ }
+
bytes_left = size;
// Map the file to memory
addr = start_addr = mmap(0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
- if (addr == MAP_FAILED) goto END_FILE;
+ if (addr == MAP_FAILED) {
+ if (error) *error = _BVPOSIXError(url);
+ goto END_FILE;
+ }
// Check if it's a fat file
// Make sure the file is long enough to hold a fat_header
@@ -87,23 +119,29 @@ NSData *_BVMachOSection(NSURL *url, char *segname, char *sectname, NSError **err
bytes_left -= sizeof(struct fat_header);
addr += sizeof(struct fat_header);
- if (bytes_left < (nfat_arch * sizeof(struct fat_arch))) goto END_MMAP;
+ if (bytes_left < (nfat_arch * sizeof(struct fat_arch))) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_MMAP;
+ }
// Read the architectures
for (int ifat_arch = 0; ifat_arch < nfat_arch; ifat_arch++) {
struct fat_arch *fa = (struct fat_arch *)addr;
int offset = NSSwapBigIntToHost(fa->offset);
addr += sizeof(struct fat_arch);
- if (bytes_left < offset) goto END_MMAP;
+ if (bytes_left < offset) {
+ if (error) *error = _BVCorruptMachOError(url);
+ goto END_MMAP;
+ }
- data = _BVMachOSectionFromMachOHeader(start_addr + offset, bytes_left, segname, sectname, error);
+ data = _BVMachOSectionFromMachOHeader(start_addr + offset, bytes_left, segname, sectname, url, error);
if (data) break;
}
}
// It's a thin file
else {
- data = _BVMachOSectionFromMachOHeader(start_addr, bytes_left,segname, sectname, error);
+ data = _BVMachOSectionFromMachOHeader(start_addr, bytes_left,segname, sectname, url, error);
}
END_MMAP:
@@ -116,25 +154,69 @@ END_FUNCTION:
return data;
}
-NSData *_BVMachOSectionFromMachOHeader(char *addr, long bytes_left, char *segname, char *sectname, NSError **error) {
+NSData *_BVMachOSectionFromMachOHeader(char *addr, long bytes_left, char *segname, char *sectname, NSURL *url, NSError **error) {
NSData *data = nil;
struct mach_header *mh;
- if (bytes_left < sizeof(struct mach_header)) return nil;
+ if (bytes_left < sizeof(struct mach_header)) {
+ if (error) *error = _BVCorruptMachOError(url);
+ return nil;
+ }
// The first bytes are the Mach-O header
mh = (struct mach_header *)addr;
if (mh->magic == MH_MAGIC) { // 32-bit
- data = _BVMachOSectionFromMachOHeader32(addr, bytes_left, segname, sectname, error);
+ data = _BVMachOSectionFromMachOHeader32(addr, bytes_left, segname, sectname, url, error);
}
else if (mh->magic == MH_MAGIC_64) { // 64-bit
- data = _BVMachOSectionFromMachOHeader64(addr, bytes_left, segname, sectname, error);
+ data = _BVMachOSectionFromMachOHeader64(addr, bytes_left, segname, sectname, url, error);
}
return data;
}
+#pragma mark - Definition of private functions for error reporting
+NSError *_BVPOSIXError(NSURL *url) {
+ NSError *underlyingError = [[NSError alloc] initWithDomain:NSPOSIXErrorDomain
+ code:errno
+ userInfo:nil];
+ NSString *errorDescription = [NSString stringWithFormat:@"File %@ could not be opened. %s.",
+ [url path], strerror(errno)];
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ errorDescription, NSLocalizedDescriptionKey,
+ underlyingError, NSUnderlyingErrorKey,
+ [url path], NSFilePathErrorKey,
+ nil];
+ NSError *error = [[NSError alloc] initWithDomain:BVPlistExtractorErrorDomain
+ code:BVPlistExtractorErrorOpenFile
+ userInfo:userInfo];
+
+ return BVAUTORELEASE(error);
+}
+
+NSError *_BVGenericError(NSURL *url, NSString *fileQualifier, NSInteger errorCode) {
+ NSString *errorDescription = [NSString stringWithFormat:@"File %@ is %@.", [url path], fileQualifier];
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ errorDescription, NSLocalizedDescriptionKey,
+ [url path], NSFilePathErrorKey,
+ nil];
+ NSError *error = [[NSError alloc] initWithDomain:BVPlistExtractorErrorDomain
+ code:errorCode
+ userInfo:userInfo];
+
+ return BVAUTORELEASE(error);
+}
+
+NSError *_BVEmptyFileError(NSURL *url) {
+ return _BVGenericError(url, @"empty", BVPlistExtractorErrorEmptyFile);
+}
+
+NSError *_BVCorruptMachOError(NSURL *url) {
+ return _BVGenericError(url, @"corrupt", BVPlistExtractorErrorCorruptMachO);
+}
+
+#pragma mark - Instantiation of private template functions
define(`BV_FUNCTION_NAME', `_BVMachOSectionFromMachOHeader32')dnl
define(`BV_STRUCT_SUFFIX', `')dnl
include(`_BVMachOSectionFromMachOHeader.m4')
View
20 BVPlistExtractor/main.m
@@ -6,17 +6,21 @@
#import <Foundation/Foundation.h>
#import "BVPlistExtractor.h"
+static void test(NSString *path) {
+ @autoreleasepool {
+ NSURL *url = [NSURL fileURLWithPath:path];
+ NSError *error = nil;
+ id plist = BVExtractPlist(url, &error);
+ NSLog(@"\n%@\n\n%@\n\n%@\n-----\n", url, plist, error);
+ }
+}
+
int main (int argc, const char * argv[])
{
-
@autoreleasepool {
- NSURL *urli386 = [NSURL fileURLWithPath:@"/tmp/i386.out"];
- NSURL *urlx86_64 = [NSURL fileURLWithPath:@"/tmp/x86_64.out"];
- NSURL *urlfat = [NSURL fileURLWithPath:@"/tmp/fat.out"];
-
- NSLog(@"i386: %p", BVExtractPlist(urli386, NULL));
- NSLog(@"x86_64 %p", BVExtractPlist(urlx86_64, NULL));
- NSLog(@"fat %p", BVExtractPlist(urlfat, NULL));
+ test(@"/tmp/i386.out");
+ test(@"/tmp/x86_64.out");
+ test(@"/tmp/fat.out");
}
return 0;
}

0 comments on commit 4cebd1b

Please sign in to comment.
Something went wrong with that request. Please try again.