From db14a127fb7c5bdbf7b4fb1c6f351a7ddc5341d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DEZEIRAUD=20Ga=C3=ABtan?= Date: Fri, 27 Sep 2019 10:41:49 +0200 Subject: [PATCH] Allow larger than 2GB files to be read. https://github.com/mendsley/bsdiff/pull/24 --- package.json | 2 +- src/DiffWorkerCallback.cpp | 24 +++++++++++++++++++++ src/DiffWorkerCallback.hpp | 25 +++++++++++++++++++++ src/Main.cpp | 25 +++++++++++++++++++++ src/PatchWorkerCallback.cpp | 24 +++++++++++++++++++++ src/PatchWorkerCallback.hpp | 25 +++++++++++++++++++++ src/c/bsdiff/bsdiff.c | 23 ++++++++++++++++++-- src/c/bspatch/bspatch.c | 43 +++++++++++++++++++++++++++++++++++-- src/c/shared.h | 3 +-- 9 files changed, 187 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 18df3ef..383d01a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "bsdiff-nodejs", "description": "An binary diff and patch library based on bsdiff algorithm for NodeJS (Windows, Mac, Linux).", - "version": "2.0.8", + "version": "2.1.0", "repository": { "type": "git", "url": "git+https://github.com/Exoway/bsdiff-nodejs.git" diff --git a/src/DiffWorkerCallback.cpp b/src/DiffWorkerCallback.cpp index 3d4747d..b59c0c5 100644 --- a/src/DiffWorkerCallback.cpp +++ b/src/DiffWorkerCallback.cpp @@ -1,3 +1,27 @@ +/* +** MIT License +** +** Copyright (c) 2018-2019 Exoway +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in all +** copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +*/ #include "DiffWorkerCallback.hpp" diff --git a/src/DiffWorkerCallback.hpp b/src/DiffWorkerCallback.hpp index dd83c25..bb02b35 100644 --- a/src/DiffWorkerCallback.hpp +++ b/src/DiffWorkerCallback.hpp @@ -1,3 +1,28 @@ +/* +** MIT License +** +** Copyright (c) 2018-2019 Exoway +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in all +** copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +*/ + #ifndef DiffWorkerCallback_H #define DiffWorkerCallback_H diff --git a/src/Main.cpp b/src/Main.cpp index da3a623..50bb348 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,3 +1,28 @@ +/* +** MIT License +** +** Copyright (c) 2018-2019 Exoway +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in all +** copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +*/ + #include #include "DiffWorkerCallback.hpp" #include "PatchWorkerCallback.hpp" diff --git a/src/PatchWorkerCallback.cpp b/src/PatchWorkerCallback.cpp index 94437ff..9a70297 100644 --- a/src/PatchWorkerCallback.cpp +++ b/src/PatchWorkerCallback.cpp @@ -1,3 +1,27 @@ +/* +** MIT License +** +** Copyright (c) 2018-2019 Exoway +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in all +** copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +*/ #include "PatchWorkerCallback.hpp" diff --git a/src/PatchWorkerCallback.hpp b/src/PatchWorkerCallback.hpp index db009c3..cb87aae 100644 --- a/src/PatchWorkerCallback.hpp +++ b/src/PatchWorkerCallback.hpp @@ -1,3 +1,28 @@ +/* +** MIT License +** +** Copyright (c) 2018-2019 Exoway +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in all +** copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +*/ + #ifndef PatchWorkerCallback_H #define PatchWorkerCallback_H diff --git a/src/c/bsdiff/bsdiff.c b/src/c/bsdiff/bsdiff.c index 8608503..4c08cac 100644 --- a/src/c/bsdiff/bsdiff.c +++ b/src/c/bsdiff/bsdiff.c @@ -233,6 +233,25 @@ static void offtout(off_t x,u_char *buf) buf[7]|= 0x80; } +static off_t readFileToBuffer(int fd, uint8_t* buffer, off_t bufferSize) +{ + off_t bytesRead = 0; + int ret; + while (bytesRead < bufferSize) + { + ret = read(fd, buffer + bytesRead, bufferSize - bytesRead); + if (ret > 0) + { + bytesRead += ret; + } + else + { + break; + } + } + return bytesRead; +} + int bsdiff(const char* error, const char* oldfile, const char* newfile, const char* patchfile, void* progressWorker, void(*callback)(off_t, off_t, void*)) { int fd; @@ -259,7 +278,7 @@ int bsdiff(const char* error, const char* oldfile, const char* newfile, const ch ((oldsize=lseek(fd,0,SEEK_END))==-1) || ((old=malloc(oldsize+1))==NULL) || (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,old,oldsize)!=oldsize) || + (readFileToBuffer(fd,old,oldsize)!=oldsize) || (close(fd)==-1)) { sprintf((char*)error, "\"%s\" %s", oldfile, strerror(errno)); @@ -283,7 +302,7 @@ int bsdiff(const char* error, const char* oldfile, const char* newfile, const ch ((newsize=lseek(fd,0,SEEK_END))==-1) || ((new=malloc(newsize+1))==NULL) || (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,new,newsize)!=newsize) || + (readFileToBuffer(fd,new,newsize)!=newsize) || (close(fd)==-1)) { sprintf((char*)error, "\"%s\" %s", newfile, strerror(errno)); diff --git a/src/c/bspatch/bspatch.c b/src/c/bspatch/bspatch.c index 9690bec..de21240 100644 --- a/src/c/bspatch/bspatch.c +++ b/src/c/bspatch/bspatch.c @@ -46,6 +46,44 @@ static off_t offtin(u_char *buf) return y; } +static off_t readFileToBuffer(int fd, uint8_t* buffer, off_t bufferSize) +{ + off_t bytesRead = 0; + int ret; + while (bytesRead < bufferSize) + { + ret = read(fd, buffer + bytesRead, bufferSize - bytesRead); + if (ret > 0) + { + bytesRead += ret; + } + else + { + break; + } + } + return bytesRead; +} + +static off_t writeFileFromBuffer(int fd, uint8_t* buffer, off_t bufferSize) +{ + off_t bytesWritten = 0; + int ret; + while (bytesWritten < bufferSize) + { + ret = write(fd, buffer + bytesWritten, bufferSize - bytesWritten); + if (ret > 0) + { + bytesWritten += ret; + } + else + { + break; + } + } + return bytesWritten; +} + int bspatch(const char* error, const char* oldfile, const char* newfile, const char* patchfile, void* progressWorker, void(*callback)(off_t, off_t, void*)) { FILE * f, * cpf, * dpf, * epf; @@ -160,7 +198,7 @@ int bspatch(const char* error, const char* oldfile, const char* newfile, const c ((oldsize=lseek(fd,0,SEEK_END))==-1) || ((old=malloc(oldsize+1))==NULL) || (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,old,oldsize)!=oldsize) || + (readFileToBuffer(fd,old,oldsize)!=oldsize) || (close(fd)==-1)) { sprintf((char*)error, "\"%s\" %s", oldfile, strerror(errno)); return -1; @@ -243,7 +281,8 @@ int bspatch(const char* error, const char* oldfile, const char* newfile, const c } /* Write the new file */ - if(((fd=open(newfile,OPEN_FLAGS_CREATE,0666))<0) || (write(fd,new,header.newsize)!=header.newsize) || (close(fd)==-1)) + if(((fd=open(newfile,OPEN_FLAGS_CREATE,0666))<0) || + (writeFileFromBuffer(fd,new,header.newsize)!=header.newsize) || (close(fd)==-1)) { sprintf((char*)error, "\"%s\" %s", newfile, strerror(errno)); return -1; diff --git a/src/c/shared.h b/src/c/shared.h index 14b4d34..be26536 100644 --- a/src/c/shared.h +++ b/src/c/shared.h @@ -1,7 +1,7 @@ /* ** MIT License ** -** Copyright (c) 2018 Dezeiraud +** Copyright (c) 2018-2019 Exoway ** ** Permission is hereby granted, free of charge, to any person obtaining a copy ** of this software and associated documentation files (the "Software"), to deal @@ -21,7 +21,6 @@ ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ** SOFTWARE. ** -** last modified by pribault at 07/10/2018 */ #ifndef SHARED_H