From 7056ccb3213649ff22050c835bb51c96d7f00d84 Mon Sep 17 00:00:00 2001 From: Zorg Date: Mon, 16 May 2016 18:21:52 -0400 Subject: [PATCH] Use FILE* functions in bspatch.c --- Sparkle.xcodeproj/project.pbxproj | 10 ++++++ Vendor/bsdiff/bscommon.c | 56 +++++++++++++++++++++++++++++++ Vendor/bsdiff/bscommon.h | 16 +++++++++ Vendor/bsdiff/bsdiff.c | 47 ++------------------------ Vendor/bsdiff/bspatch.c | 37 ++++++++++++++------ 5 files changed, 111 insertions(+), 55 deletions(-) create mode 100644 Vendor/bsdiff/bscommon.c create mode 100644 Vendor/bsdiff/bscommon.h diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 7b2bd15b..b3def310 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -142,6 +142,7 @@ 7229E1BA1C97CC4D00CB50D0 /* SUScheduledUpdateDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 7229E1B81C97CC4D00CB50D0 /* SUScheduledUpdateDriver.m */; }; 7229E1BD1C98EFF200CB50D0 /* SUDownloadDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1BB1C98EFF200CB50D0 /* SUDownloadDriver.h */; }; 7229E1BE1C98EFF200CB50D0 /* SUDownloadDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 7229E1BC1C98EFF200CB50D0 /* SUDownloadDriver.m */; }; + 723B252A1CEA7F3B00909873 /* bscommon.c in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE91CEA18900052EF9E /* bscommon.c */; }; 7246E0A31C83B685003B4E75 /* SUStandardUpdaterController.h in Headers */ = {isa = PBXBuildFile; fileRef = 7246E0A11C83B685003B4E75 /* SUStandardUpdaterController.h */; settings = {ATTRIBUTES = (Public, ); }; }; 7246E0A41C83B685003B4E75 /* SUStandardUpdaterController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7246E0A21C83B685003B4E75 /* SUStandardUpdaterController.m */; }; 725602D51C83551C00DAA70E /* SUApplicationInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 725602D31C83551C00DAA70E /* SUApplicationInfo.h */; }; @@ -245,6 +246,8 @@ 72AC6B2E1B9B218C00F62325 /* SparkleTestCodeSignApp.dmg in Resources */ = {isa = PBXBuildFile; fileRef = 72AC6B2D1B9B218C00F62325 /* SparkleTestCodeSignApp.dmg */; }; 72B09CE71CE6BCBD0052EF9E /* SUURLDownload.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B09CE51CE6BCBD0052EF9E /* SUURLDownload.h */; }; 72B09CE81CE6BCBD0052EF9E /* SUURLDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE61CE6BCBD0052EF9E /* SUURLDownload.m */; }; + 72B09CEC1CEA18900052EF9E /* bscommon.c in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE91CEA18900052EF9E /* bscommon.c */; }; + 72B09CEE1CEA21CD0052EF9E /* bscommon.c in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE91CEA18900052EF9E /* bscommon.c */; }; 72B767CA1C9B707000A07552 /* SUAppcastDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B767C81C9B707000A07552 /* SUAppcastDriver.h */; }; 72B767CB1C9B707000A07552 /* SUAppcastDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B767C91C9B707000A07552 /* SUAppcastDriver.m */; }; 72B767CE1C9B924900A07552 /* SUInstallerDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B767CC1C9B924900A07552 /* SUInstallerDriver.h */; }; @@ -877,6 +880,8 @@ 72AFC6121B9A944200F6B565 /* Sparkle Unit Tests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Sparkle Unit Tests-Bridging-Header.h"; sourceTree = ""; }; 72B09CE51CE6BCBD0052EF9E /* SUURLDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUURLDownload.h; sourceTree = ""; }; 72B09CE61CE6BCBD0052EF9E /* SUURLDownload.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUURLDownload.m; sourceTree = ""; }; + 72B09CE91CEA18900052EF9E /* bscommon.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bscommon.c; sourceTree = ""; }; + 72B09CEA1CEA18900052EF9E /* bscommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bscommon.h; sourceTree = ""; }; 72B767C81C9B707000A07552 /* SUAppcastDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUAppcastDriver.h; sourceTree = ""; }; 72B767C91C9B707000A07552 /* SUAppcastDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUAppcastDriver.m; sourceTree = ""; }; 72B767CC1C9B924900A07552 /* SUInstallerDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUInstallerDriver.h; sourceTree = ""; }; @@ -1171,6 +1176,8 @@ 14732BB61960ECE800593899 /* bsdiff */ = { isa = PBXGroup; children = ( + 72B09CE91CEA18900052EF9E /* bscommon.c */, + 72B09CEA1CEA18900052EF9E /* bscommon.h */, 5D06E8DB0FD68CB9005AE3F6 /* bsdiff.c */, 5D06E8DC0FD68CB9005AE3F6 /* bspatch.c */, 611142E810FB1BE5009810AA /* bspatch.h */, @@ -2395,6 +2402,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 72B09CEE1CEA21CD0052EF9E /* bscommon.c in Sources */, 72D9547E1CBA221E006F28BD /* SUAppcastItem.m in Sources */, 726E07671CA61648001A286B /* SUBinaryDeltaCommon.m in Sources */, 726E07661CA61621001A286B /* bspatch.c in Sources */, @@ -2434,6 +2442,7 @@ 5D06E8E90FD68CDB005AE3F6 /* bsdiff.c in Sources */, 14652F7E19A9728A00959E44 /* bspatch.c in Sources */, 7223E7631AD1AEFF008E3161 /* sais.c in Sources */, + 72B09CEC1CEA18900052EF9E /* bscommon.c in Sources */, 14652F7D19A9726700959E44 /* SUBinaryDeltaApply.m in Sources */, 14652F7C19A9725300959E44 /* SUBinaryDeltaCommon.m in Sources */, 7268AC631AD634C200C3E0C1 /* SUBinaryDeltaCreate.m in Sources */, @@ -2448,6 +2457,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 723B252A1CEA7F3B00909873 /* bscommon.c in Sources */, 726E076D1CA66180001A286B /* SUBinaryDeltaUnarchiver.m in Sources */, 72A6F9871C94C4AE005F404C /* SUDiskImageUnarchiver.m in Sources */, 72A6F9861C94C49C005F404C /* SUPipedUnarchiver.m in Sources */, diff --git a/Vendor/bsdiff/bscommon.c b/Vendor/bsdiff/bscommon.c new file mode 100644 index 00000000..c1bd7016 --- /dev/null +++ b/Vendor/bsdiff/bscommon.c @@ -0,0 +1,56 @@ +/* + * bscommon.c + * Sparkle + * + * Created by Mayur Pawashe on 5/16/16. + */ + +#include "bscommon.h" +#include + +u_char *readfile(const char *filename, off_t *outSize) +{ + FILE *file = fopen(filename, "r"); + if (file == NULL) { + return NULL; + } + + if (fseek(file, 0L, SEEK_END) != 0) { + fclose(file); + return NULL; + } + + long offset = ftell(file); + if (offset == -1) { + fclose(file); + return NULL; + } + + size_t size = (size_t)offset; + + if (outSize != NULL) { + *outSize = (off_t)size; + } + + /* Allocate size + 1 bytes instead of newsize bytes to ensure + that we never try to malloc(0) and get a NULL pointer */ + u_char *buffer = malloc(size + 1); + if (buffer == NULL) { + fclose(file); + return NULL; + } + + if (fseek(file, 0L, SEEK_SET) != 0) { + fclose(file); + return NULL; + } + + if (fread(buffer, 1, size, file) < size) { + fclose(file); + return NULL; + } + + fclose(file); + + return buffer; +} diff --git a/Vendor/bsdiff/bscommon.h b/Vendor/bsdiff/bscommon.h new file mode 100644 index 00000000..d90273fc --- /dev/null +++ b/Vendor/bsdiff/bscommon.h @@ -0,0 +1,16 @@ +/* + * bscommon.h + * Sparkle + * + * Created by Mayur Pawashe on 5/16/16. + */ + +#ifndef BS_COMMON_H +#define BS_COMMON_H + +#include +#include + +u_char *readfile(const char *filename, off_t *outSize); + +#endif diff --git a/Vendor/bsdiff/bsdiff.c b/Vendor/bsdiff/bsdiff.c index 32e5c1e0..8bad3451 100644 --- a/Vendor/bsdiff/bsdiff.c +++ b/Vendor/bsdiff/bsdiff.c @@ -38,6 +38,8 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c, v 1.1 2005/08/06 01:59:0 #include #include +#include "bscommon.h" + #define MIN(x, y) (((x)<(y)) ? (x) : (y)) /* matchlen(old, oldsize, new, newsize) @@ -115,51 +117,6 @@ static void offtout(off_t x, u_char *buf) buf[7] |= 0x80; } -static u_char *readfile(const char *filename, off_t *outSize) -{ - FILE *file = fopen(filename, "r"); - if (file == NULL) { - return NULL; - } - - if (fseek(file, 0L, SEEK_END) != 0) { - fclose(file); - return NULL; - } - - off_t size = ftell(file); - if (size == -1) { - fclose(file); - return NULL; - } - - if (outSize != NULL) { - *outSize = size; - } - - /* Allocate size + 1 bytes instead of newsize bytes to ensure - that we never try to malloc(0) and get a NULL pointer */ - u_char *buffer = malloc(size + 1); - if (buffer == NULL) { - fclose(file); - return NULL; - } - - if (fseek(file, 0L, SEEK_SET) != 0) { - fclose(file); - return NULL; - } - - if (fread(buffer, 1, size, file) < size) { - fclose(file); - return NULL; - } - - fclose(file); - - return buffer; -} - int bsdiff(int argc, char *argv[]); // Added by AMM: suppresses a warning about the following not having a prototype. int bsdiff(int argc, char *argv[]) diff --git a/Vendor/bsdiff/bspatch.c b/Vendor/bsdiff/bspatch.c index 4ba2e6cc..bfde7208 100644 --- a/Vendor/bsdiff/bspatch.c +++ b/Vendor/bsdiff/bspatch.c @@ -37,6 +37,8 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59: #include #include +#include "bscommon.h" + /* Compatibility layer for reading either the old BSDIFF40 or the new BSDIFN40 patch formats: */ @@ -127,7 +129,6 @@ int bspatch(int argc,const char * const argv[]) { FILE * f, * cpf, * dpf, * epf; stream_t cstream, dstream, estream; - int fd; ssize_t oldsize,newsize; ssize_t bzctrllen,bzdatalen; u_char header[32],buf[8]; @@ -202,12 +203,15 @@ int bspatch(int argc,const char * const argv[]) (long long)(32 + bzctrllen + bzdatalen)); estream = io->open(epf); - if(((fd=open(argv[1],O_RDONLY,0))<0) || - ((oldsize=lseek(fd,0,SEEK_END))==-1) || - ((old=malloc((size_t)oldsize+1))==NULL) || - (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,old,(size_t)oldsize)!=oldsize) || - (close(fd)==-1)) err(1,"%s",argv[1]); + off_t size = 0; + old = readfile(argv[1], &size); + if (old == NULL) { + warn("old file: %s", argv[1]); + return -1; + } + + oldsize = size; + if((new=malloc((size_t)newsize+1))==NULL) err(1,NULL); oldpos=0;newpos=0; @@ -260,9 +264,22 @@ int bspatch(int argc,const char * const argv[]) err(1, "fclose(%s)", argv[3]); /* Write the new file */ - if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) || - (write(fd,new,(size_t)newsize)!=newsize) || (close(fd)==-1)) - err(1,"%s",argv[2]); + FILE *writeFile = fopen(argv[2], "w"); + if (writeFile == NULL) { + warn("failed to write new file: %s", argv[2]); + return -1; + } + + if (fwrite(new, 1, (size_t)newsize, writeFile) < (size_t)newsize) { + warn("failed to write to new file: %s", argv[2]); + fclose(writeFile); + return -1; + } + + if (fclose(writeFile) != 0) { + warn("failed to close new file: %s", argv[2]); + return -1; + } free(new); free(old);