From 96821139689cce3d0d4b362c28e187e5e23cd1b3 Mon Sep 17 00:00:00 2001 From: Michael Phan-Ba Date: Thu, 23 Feb 2012 12:42:33 -0800 Subject: [PATCH] Move endian ops out of bsdiff.cc --- src/cpp/binding.cc | 20 ++++++++++++++++++-- src/cpp/bsdiff.cc | 13 ++++++------- src/cpp/endian.h | 31 ------------------------------- 3 files changed, 24 insertions(+), 40 deletions(-) delete mode 100644 src/cpp/endian.h diff --git a/src/cpp/binding.cc b/src/cpp/binding.cc index 2a290fb..565a4f1 100644 --- a/src/cpp/binding.cc +++ b/src/cpp/binding.cc @@ -22,6 +22,11 @@ #include #include +#include "boost/endian.hpp" +#ifdef BOOST_BIG_ENDIAN +# include "gnuclib/byteswap.h" +#endif + #include "bsdiff.h" using namespace node; @@ -83,6 +88,11 @@ static void AfterDiff(uv_work_t *req) { if (shim->err != 0) return Error(shim); +#ifdef BOOST_BIG_ENDIAN + for (size_t i = shim->ctrl.size() - 1; i >= 0; --it) + shim->ctrl[i] = bswap_32(shim->ctrl[i]); +#endif + Buffer *ctrl = Buffer::New(reinterpret_cast(shim->ctrl.data()), shim->ctrl.size() * sizeof(int)); Buffer *diff = Buffer::New(shim->diff, shim->difflen, DeleteMemory, NULL); @@ -177,8 +187,14 @@ Handle Patch(const Arguments& args) { async_stub *shim = new async_stub; const int *ctrldat = reinterpret_cast(Buffer::Data(ctrl)); - const uint32_t ctrllen = Buffer::Length(ctrl); - for (uint32_t i = 0; i < ctrllen; ++i) shim->ctrl.push_back(ctrldat[i]); + const size_t ctrllen = Buffer::Length(ctrl); + +#ifdef BOOST_BIG_ENDIAN + for (size_t i = ctrllen - 1; i >= 0; --it) + ctrldat[i] = bswap_32(ctrldat[i]); +#endif + + shim->ctrl.assign(ctrldat, ctrldat + ctrllen); shim->refdat = Buffer::Data(ref); shim->diff = Buffer::Data(diff); diff --git a/src/cpp/bsdiff.cc b/src/cpp/bsdiff.cc index 556d0f8..6e97d3d 100644 --- a/src/cpp/bsdiff.cc +++ b/src/cpp/bsdiff.cc @@ -30,7 +30,6 @@ #include #include -#include "endian.h" #include "bsdiff.h" namespace node_bsdiff { @@ -306,13 +305,13 @@ int bsdiff(bsdiff_dat *args) { xtralen += (scan - lenb) - (lastscan + lenf); /* add x bytes from oldfile to x bytes from the diff block */ - ctrl.push_back(b32le(lenf)); + ctrl.push_back(lenf); /* copy y bytes from the xtra block */ - ctrl.push_back(b32le((scan - lenb) - (lastscan + lenf))); + ctrl.push_back((scan - lenb) - (lastscan + lenf)); /* seek forwards in oldfile by z bytes */ - ctrl.push_back(b32le((pos - lenb) - (lastpos + lenf))); + ctrl.push_back((pos - lenb) - (lastpos + lenf)); lastscan = scan - lenb; lastpos = pos - lenb; @@ -369,9 +368,9 @@ int bspatch(bsdiff_dat *args) { while (destIdx < curlen) { /* Unpack control data */ - addN = b32le(ctrl[ctrlpos++]); - copyN = b32le(ctrl[ctrlpos++]); - seekN = b32le(ctrl[ctrlpos++]); + addN = ctrl[ctrlpos++]; + copyN = ctrl[ctrlpos++]; + seekN = ctrl[ctrlpos++]; /* Sanity-check */ if (destIdx + addN > curlen || srcIdx + addN > reflen || diff --git a/src/cpp/endian.h b/src/cpp/endian.h deleted file mode 100644 index e497172..0000000 --- a/src/cpp/endian.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - - Copyright 2012 Michael Phan-Ba - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -#ifndef NODE_BSDIFF_ENDIAN_H -#define NODE_BSDIFF_ENDIAN_H - -#include "boost/endian.hpp" - -#ifdef BOOST_LITTLE_ENDIAN -# define b32le(x) (x) -#else -# include "gnuclib/byteswap.h" -# define b32le(x) bswap_32(x) -#endif - -#endif // NODE_BSDIFF_ENDIAN_H