diff --git a/applications/test/base64/Make/files b/applications/test/base64/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..4c6f18ce4cd5d4abc1e7d0cbcd32a1296d5588d0 --- /dev/null +++ b/applications/test/base64/Make/files @@ -0,0 +1,3 @@ +Test-base64Encoding.C + +EXE = $(FOAM_USER_APPBIN)/Test-base64Encoding diff --git a/applications/test/base64/Make/options b/applications/test/base64/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/applications/test/base64/Test-base64Encoding.C b/applications/test/base64/Test-base64Encoding.C new file mode 100644 index 0000000000000000000000000000000000000000..478a1d9848e42941cd1391bffcfaab75dd285022 --- /dev/null +++ b/applications/test/base64/Test-base64Encoding.C @@ -0,0 +1,169 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +Application + Test-base64Encoding + +Description + Test base64 encoding layer. + + Target values generated with "base64 encode ..." in Google. + A simple independent source for comparison. + +\*---------------------------------------------------------------------------*/ + +#include "base64Layer.H" +#include "List.H" +#include "Pair.H" + +#include <sstream> +#include <initializer_list> + +using namespace Foam; + +bool test(const Pair<string>& unit) +{ + const string& input = unit.first(); + const string& expected = unit.second(); + + std::ostringstream os; + + base64Layer b64(os); + b64.write(input.data(), input.size()); + b64.close(); + + const string encoded = os.str(); + + Info<< input << nl; + + if (encoded == expected) + { + Info<< " encoded: " << encoded << " (OK)" << nl + << endl; + return true; + } + else + { + Info<< " encoded: " << encoded << " (ERROR)" << nl + << " expected: " << expected << nl + << endl; + + return false; + } +} + + +bool test(std::initializer_list<Pair<string>> list) +{ + bool good = true; + + for (const Pair<string>& t : list) + { + good = test(t) && good; + } + + return good; +} + + +bool test(const UList<Pair<string>>& list) +{ + bool good = true; + + for (const Pair<string>& t : list) + { + good = test(t) && good; + } + + return good; +} + + +void testMixed(std::ostream& os, const UList<Pair<string>>& list) +{ + base64Layer b64(os); + + os << "<test-mixed>" << nl; + + int i=0; + for (const Pair<string>& t : list) + { + const string& input = t.first(); + + os << "<input" << ++i << " value='" << input << "'>" << nl; + os << " "; + + b64.write(input.data(), input.size()); + b64.close(); + + os << nl + << "</input" << i << ">" << nl; + } + + os << "</test-mixed>" << nl + << nl; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +int main(int argc, char * argv[]) +{ + Info<< "Test base64 encode layer" << nl << endl; + + List<Pair<string>> testList + { + { + "abcdef", // 6 input, 8 output + "YWJjZGVm" + }, + { + "OpenFOAM", + "T3BlbkZPQU0=" + }, + { + "OpenFOAM: The Open Source CFD Toolbox", + "T3BlbkZPQU06IFRoZSBPcGVuIFNvdXJjZSBDRkQgVG9vbGJveA==" + } + }; + + + bool good = test(testList); + + + // Test mixing output + testMixed(std::cout, testList); + + if (good) + { + Info<< "All tests passed" << endl; + return 0; + } + else + { + Info<< "One or more tests failed" << endl; + return 1; + } +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/applications/test/sha1/Test-SHA1.C b/applications/test/sha1/Test-SHA1.C index 05d8f6bc620965fb0f91479c8f6b2c8537588e4b..4d81dc8148827a08f22b68911753ab480550b7a8 100644 --- a/applications/test/sha1/Test-SHA1.C +++ b/applications/test/sha1/Test-SHA1.C @@ -42,28 +42,28 @@ int main(int argc, char * argv[]) SHA1 sha; SHA1Digest shaDig; - std::string str("The quick brown fox jumps over the lazy dog"); - Info<< shaDig << nl; - Info<< SHA1("The quick brown fox jumps over the lazy dog") << nl; + const std::string str("The quick brown fox jumps over the lazy dog"); + Info<< shaDig << " : empty" << nl; + Info<< SHA1(str) << " : " << str << nl; - sha.append("The quick brown fox jumps over the lazy dog"); - Info<< sha << nl; + sha.append(str); + Info<< sha << " : appended to empty" << nl; sha.clear(); - sha.append("The quick brown fox jumps over the lazy dog"); + sha.append(str); shaDig = sha; sha.append("\n"); - Info<< sha << nl; + Info<< sha << " : with trailing newline" << nl; Info<< shaDig << nl; if (sha == shaDig) { - Info<<"SHA1 digests are identical\n"; + Info<<"SHA1 digests are identical (unexpected)\n"; } else { - Info<<"SHA1 digests are different\n"; + Info<<"SHA1 digests are different (expected)\n"; } Info<<"lhs:" << sha << " rhs:" << shaDig << endl; @@ -99,7 +99,6 @@ int main(int argc, char * argv[]) os.rewind(); os << "The quick brown fox jumps over the lazy dog"; Info<< os.digest() << endl; - } { diff --git a/applications/test/sysInfo/Test-sysInfo.C b/applications/test/sysInfo/Test-sysInfo.C index 3c04ce0187d584ecdf3ea0c535f484b9dad51847..80ba5c0cdd64c21d87c273ddf4f99c085dc44921 100644 --- a/applications/test/sysInfo/Test-sysInfo.C +++ b/applications/test/sysInfo/Test-sysInfo.C @@ -27,8 +27,9 @@ Description \*---------------------------------------------------------------------------*/ -#include "ProfilingSysInfo.H" +#include "profilingSysInfo.H" #include "IOstreams.H" +#include "endian.H" using namespace Foam; @@ -37,7 +38,22 @@ using namespace Foam; int main(int argc, char *argv[]) { - Profiling::sysInfo().write(Info); + profiling::sysInfo().write(Info); + +#ifdef WM_BIG_ENDIAN + Info + << "WM_BIG_ENDIAN is defined" + << nl; +#endif +#ifdef WM_LITTLE_ENDIAN + Info + << "WM_LITTLE_ENDIAN is defined" + << nl; +#endif + + Info<< "Runtime endian check: big=" << endian::isBig() + << " little=" << endian::isLittle() + << nl; return 0; } diff --git a/applications/utilities/mesh/manipulation/setSet/writeFuns.C b/applications/utilities/mesh/manipulation/setSet/writeFuns.C index 720a6dff9427b591f4728c5d016e2be3c6791462..2a104cb01ecfa72cbb1c2a41f1bbaec0275c3be0 100644 --- a/applications/utilities/mesh/manipulation/setSet/writeFuns.C +++ b/applications/utilities/mesh/manipulation/setSet/writeFuns.C @@ -24,22 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "writeFuns.H" - -#if defined(__mips) - #include <standards.h> - #include <sys/endian.h> -#endif - -#if defined(LITTLE_ENDIAN) \ - || defined(_LITTLE_ENDIAN) \ - || defined(__LITTLE_ENDIAN) - #define LITTLEENDIAN 1 -#elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN) - #undef LITTLEENDIAN -#else - #error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined." - #error "Please add to compilation options" -#endif +#include "endian.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -75,7 +60,7 @@ void Foam::writeFuns::write { if (binary) { - #ifdef LITTLEENDIAN + #ifdef WM_LITTLE_ENDIAN swapWords(fField.size(), reinterpret_cast<int32_t*>(fField.begin())); #endif @@ -124,7 +109,7 @@ void Foam::writeFuns::write { if (binary) { - #ifdef LITTLEENDIAN + #ifdef WM_LITTLE_ENDIAN swapWords ( (sizeof(label)/4)*elems.size(), diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeFuns.C b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeFuns.C index d9e9f9cfc895558b7a5f9648936f3185b52550d7..24eef341f641234798bf037d68f45b757178d7b7 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeFuns.C +++ b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeFuns.C @@ -25,31 +25,7 @@ License #include "writeFuns.H" #include "vtkTopo.H" - -#if defined(__mips) - #include <standards.h> - #include <sys/endian.h> -#endif - -// MacOSX -#ifdef __DARWIN_BYTE_ORDER - #if __DARWIN_BYTE_ORDER==__DARWIN_BIG_ENDIAN - #undef LITTLE_ENDIAN - #else - #undef BIG_ENDIAN - #endif -#endif - -#if defined(LITTLE_ENDIAN) \ - || defined(_LITTLE_ENDIAN) \ - || defined(__LITTLE_ENDIAN) - #define LITTLEENDIAN 1 -#elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN) - #undef LITTLEENDIAN -#else - #error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined." - #error "Please add to compilation options" -#endif +#include "endian.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -85,7 +61,7 @@ void Foam::writeFuns::write { if (binary) { - #ifdef LITTLEENDIAN + #ifdef WM_LITTLE_ENDIAN swapWords(fField.size(), reinterpret_cast<label*>(fField.begin())); #endif os.write @@ -138,7 +114,7 @@ void Foam::writeFuns::write { if (binary) { - #ifdef LITTLEENDIAN + #ifdef WM_LITTLE_ENDIAN swapWords(elems.size(), reinterpret_cast<label*>(elems.begin())); #endif os.write diff --git a/src/OSspecific/POSIX/endian.H b/src/OSspecific/POSIX/endian.H new file mode 100644 index 0000000000000000000000000000000000000000..3828db56a1c7fb913c9028372e58fe30e23e8612 --- /dev/null +++ b/src/OSspecific/POSIX/endian.H @@ -0,0 +1,134 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +Description + Help with architecture-specific aspects. + + Defines WM_BIG_ENDIAN or WM_LITTLE_ENDIAN as well as providing a + few runtime methods. Primarily used as a namespace, but provided + as a class for possible future expansion. + +SourceFiles + endianI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef foamEndian_H // prefixed with 'foam' to avoid potential collisions +#define foamEndian_H + +#include <cstdint> + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef __GNUC__ + #include <endian.h> +#elif defined __mips + #include <standards.h> + #include <sys/endian.h> +#endif + +#ifdef __BYTE_ORDER + #if (__BYTE_ORDER == __LITTLE_ENDIAN) + #define WM_LITTLE_ENDIAN + #elif (__BYTE_ORDER == __BIG_ENDIAN) + #define WM_BIG_ENDIAN + #else + #error "__BYTE_ORDER defined, but not __LITTLE_ENDIAN or __BIG_ENDIAN." + #endif +#elif defined(__LITTLE_ENDIAN) \ + || defined(_LITTLE_ENDIAN) \ + || defined(LITTLE_ENDIAN) + #define WM_LITTLE_ENDIAN +#elif defined(__BIG_ENDIAN) \ + || defined(_BIG_ENDIAN) \ + || defined(BIG_ENDIAN) + #define WM_BIG_ENDIAN +#endif + +// Special handling for OS-X +#ifdef __DARWIN_BYTE_ORDER + #if (__DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN) + #define WM_BIG_ENDIAN + #undef WM_LITTLE_ENDIAN + #elif (__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN) + #define WM_LITTLE_ENDIAN + #undef WM_BIG_ENDIAN + #endif +#endif + +// Could also downgrade to a warning, but then user always needs runtime check. +#if !defined(WM_BIG_ENDIAN) && !defined(WM_LITTLE_ENDIAN) + #error "Cannot determine BIG or LITTLE endian." + #error "Please add to compilation options" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class endian Declaration +\*---------------------------------------------------------------------------*/ + +class endian +{ + // Private Member Functions + + //- Disallow default bitwise copy construct + endian(const endian&) = delete; + + //- Disallow default bitwise assignment + void operator=(const endian&) = delete; + +public: + + // Public data + + //- Runtime check for big endian. + inline static bool isBig(); + + //- Runtime check for little endian. + inline static bool isLittle(); + + //- Byte endian swapping for 32-bits + inline static uint32_t swap32(uint32_t); + + //- Byte endian swapping for 64-bits + inline static uint64_t swap64(uint64_t); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "endianI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/endianI.H b/src/OSspecific/POSIX/endianI.H new file mode 100644 index 0000000000000000000000000000000000000000..5f47fbd780844ec40804128102316277e50ce7af --- /dev/null +++ b/src/OSspecific/POSIX/endianI.H @@ -0,0 +1,108 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +\*---------------------------------------------------------------------------*/ + +#include "endian.H" + +#ifdef __GNUC__ + #define USE_BUILTIN_BYTESWAP +#else + #undef USE_BUILTIN_BYTESWAP +#endif +// for Debugging: +// #undef USE_BUILTIN_BYTESWAP + + +// Some ideas about a templated approach for swapping bytes: +// http://www.cplusplus.com/forum/general/27544/ + +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +inline bool Foam::endian::isBig() +{ + const short testBig = 0x0100; + + // Yields 0x01 for big endian + return *(reinterpret_cast<const char*>(&testBig)); +} + + +inline bool Foam::endian::isLittle() +{ + const short testLittle = 0x0001; + + // Yields 0x01 for little endian + return *(reinterpret_cast<const char*>(&testLittle)); +} + + +inline uint32_t Foam::endian::swap32(uint32_t u) +{ +#ifdef USE_BUILTIN_BYTESWAP + return __builtin_bswap32(u); +#else + return + ( + (((u) & 0xff000000) >> 24) // 3 -> 0 + | (((u) & 0x00ff0000) >> 8) // 2 -> 1 + | (((u) & 0x0000ff00) << 8) // 2 <- 1 + | (((u) & 0x000000ff) << 24) // 3 <- 0 + ); + + // alternative formulation + // + // u = ((u<<8) & 0xFF00FF00) | ((u>>8) & 0x00FF00FF); + // u = (u>>16) | (u<<16); + // return u; +#endif +} + + +inline uint64_t Foam::endian::swap64(uint64_t u) +{ +#ifdef USE_BUILTIN_BYTESWAP + return __builtin_bswap64(u); +#else + return + ( + (((u) & 0xff00000000000000ull) >> 56) // 7 -> 0 + | (((u) & 0x00ff000000000000ull) >> 40) // 6 -> 1 + | (((u) & 0x0000ff0000000000ull) >> 24) // 5 -> 2 + | (((u) & 0x000000ff00000000ull) >> 8) // 4 -> 3 + | (((u) & 0x00000000ff000000ull) << 8) // 4 <- 3 + | (((u) & 0x0000000000ff0000ull) << 24) // 5 <- 2 + | (((u) & 0x000000000000ff00ull) << 40) // 6 <- 1 + | (((u) & 0x00000000000000ffull) << 56) // 7 <- 0 + ); + + // alternative formulation + // + // u = ((u<< 8) & 0xFF00FF00FF00FF00ull) | ((u>> 8) & 0x00FF00FF00FF00FFull); + // u = ((u<<16) & 0xFFFF0000FFFF0000ull) | ((u>>16) & 0x0000FFFF0000FFFFull); + // return (u >> 32) | (u << 32); +#endif +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 13a9595fcf1210923d2a98a5d436054bd5674147..8b6be8d8953b09a095c296d0d0a65ede9f909136 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -144,6 +144,9 @@ $(Sstreams)/SstreamsPrint.C $(Sstreams)/readHexLabel.C $(Sstreams)/prefixOSstream.C +hashes = $(Streams)/hashes +$(hashes)/base64Layer.C + gzstream = $(Streams)/gzstream $(gzstream)/gzstream.C diff --git a/src/OpenFOAM/db/IOstreams/hashes/base64Layer.C b/src/OpenFOAM/db/IOstreams/hashes/base64Layer.C new file mode 100644 index 0000000000000000000000000000000000000000..f1123f7519fc37981d54359bd0fa91dab0926001 --- /dev/null +++ b/src/OpenFOAM/db/IOstreams/hashes/base64Layer.C @@ -0,0 +1,168 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +\*---------------------------------------------------------------------------*/ + +#include "base64Layer.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +//! \cond fileScope +//- The characters used for base-64 +static const unsigned char base64Chars[64] = +{ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' +}; +//! \endcond + + +// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // + +inline unsigned char Foam::base64Layer::encode0() +{ + // Top 6 bits of char0 + return base64Chars[((group_[0] & 0xFC) >> 2)]; +} + +inline unsigned char Foam::base64Layer::encode1() +{ + // Bottom 2 bits of char0, Top 4 bits of char1 + return base64Chars[((group_[0] & 0x03) << 4) | ((group_[1] & 0xF0) >> 4)]; +} + +inline unsigned char Foam::base64Layer::encode2() +{ + // Bottom 4 bits of char1, Top 2 bits of char2 + return base64Chars[((group_[1] & 0x0F) << 2) | ((group_[2] & 0xC0) >> 6)]; +} + +inline unsigned char Foam::base64Layer::encode3() +{ + // Bottom 6 bits of char2 + return base64Chars[(group_[2] & 0x3F)]; +} + + +void Foam::base64Layer::add(char c) +{ + group_[groupLen_++] = static_cast<unsigned char>(c); + if (groupLen_ == 3) + { + unsigned char out[4]; + + out[0] = encode0(); + out[1] = encode1(); + out[2] = encode2(); + out[3] = encode3(); + os_.write(reinterpret_cast<char*>(out), 4); + + groupLen_ = 0; + } + + dirty_ = true; +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::base64Layer::base64Layer(std::ostream& os) +: + os_(os), + group_(), + groupLen_(0), + dirty_(false) +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // +Foam::base64Layer::~base64Layer() +{ + close(); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +void Foam::base64Layer::write(const char* s, std::streamsize n) +{ + for (std::streamsize i=0; i < n; ++i) + { + add(s[i]); + } +} + + +void Foam::base64Layer::reset() +{ + groupLen_ = 0; + dirty_ = false; +} + + +bool Foam::base64Layer::close() +{ + if (!dirty_) + { + return false; + } + + unsigned char out[4]; + if (groupLen_ == 1) + { + group_[1] = 0; + + out[0] = encode0(); + out[1] = encode1(); + out[2] = '='; + out[3] = '='; + os_.write(reinterpret_cast<char*>(out), 4); + } + else if (groupLen_ == 2) + { + group_[2] = 0; + + out[0] = encode0(); + out[1] = encode1(); + out[2] = encode2(); + out[3] = '='; + os_.write(reinterpret_cast<char*>(out), 4); + } + + // group-length == 0 (no content) + // group-length == 3 is not possible, already reset in add() + + groupLen_ = 0; + dirty_ = false; + + return true; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/IOstreams/hashes/base64Layer.H b/src/OpenFOAM/db/IOstreams/hashes/base64Layer.H new file mode 100644 index 0000000000000000000000000000000000000000..9aa4bc8d7968905e31d7e333beb59ffd737f685f --- /dev/null +++ b/src/OpenFOAM/db/IOstreams/hashes/base64Layer.H @@ -0,0 +1,127 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +Class + base64Layer + +Description + An output filter layer to write base-64 encoded content. + + Base64 encoding accoding to RFC 4648 specification + (https://tools.ietf.org/html/rfc4648#page-5). + It is the obligation of the caller to avoid using normal output + while the base-64 encoding layer is actively being used. + +SourceFiles + base64Layer.C + +\*---------------------------------------------------------------------------*/ + +#ifndef base64Layer_H +#define base64Layer_H + +#include <iostream> + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class base64Layer Declaration +\*---------------------------------------------------------------------------*/ + +class base64Layer +{ + // Private data + + //- The output stream for the layer + std::ostream& os_; + + //- Buffer of characters to encode + unsigned char group_[3]; + + //- Current length of the encode buffer + unsigned char groupLen_; + + //- Track if anything has been encoded. + bool dirty_; + + + // Private Member Functions + + inline unsigned char encode0(); + inline unsigned char encode1(); + inline unsigned char encode2(); + inline unsigned char encode3(); + + //- Disallow default bitwise copy construct + base64Layer(const base64Layer&) = delete; + + //- Disallow default bitwise assignment + void operator=(const base64Layer&) = delete; + + +protected: + + // Protected Member Functions + + //- Add a character to the group, outputting when the group is full. + void add(char c); + + +public: + + // Constructors + + //- Construct and attach to an output stream + base64Layer(std::ostream&); + + + //- Destructor + ~base64Layer(); + + + // Member Functions + + //- Encode the character sequence, writing when possible. + void write(const char* s, std::streamsize n); + + //- Restart a new encoding sequence. + void reset(); + + //- End the encoding sequence, padding the final characters with '='. + // Return false if no encoding layer was actually used. + bool close(); + +}; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/hashes/Hasher/Hasher.C b/src/OpenFOAM/primitives/hashes/Hasher/Hasher.C index f0c51aa45a7f7f2ec0de3e1469174772df4b7d6e..56b35b8e6158a501d5bb947d145cc26f8816b3b5 100644 --- a/src/OpenFOAM/primitives/hashes/Hasher/Hasher.C +++ b/src/OpenFOAM/primitives/hashes/Hasher/Hasher.C @@ -27,10 +27,7 @@ Description #include "Hasher.H" #include "HasherInt.H" - -#if defined (__GLIBC__) - #include <endian.h> -#endif +#include "endian.H" // Left-rotate a 32-bit value and carry by nBits #define bitRotateLeft(x, nBits) (((x) << (nBits)) | ((x) >> (32 - (nBits)))) @@ -188,7 +185,7 @@ Description // ---------------------------------------------------------------------------- // Specialized little-endian code -#if !defined (__BYTE_ORDER) || (__BYTE_ORDER == __LITTLE_ENDIAN) +#ifdef WM_LITTLE_ENDIAN static unsigned jenkins_hashlittle ( const void *key, @@ -357,8 +354,6 @@ static unsigned jenkins_hashlittle #endif - - // ---------------------------------------------------------------------------- // hashbig(): // This is the same as hashword() on big-endian machines. It is different @@ -366,7 +361,7 @@ static unsigned jenkins_hashlittle // big-endian byte ordering. // ---------------------------------------------------------------------------- // specialized big-endian code -#if !defined (__BYTE_ORDER) || (__BYTE_ORDER == __BIG_ENDIAN) +#ifdef WM_BIG_ENDIAN static unsigned jenkins_hashbig ( const void *key, @@ -479,25 +474,12 @@ unsigned Foam::Hasher unsigned initval ) { -#ifdef __BYTE_ORDER - #if (__BYTE_ORDER == __BIG_ENDIAN) - return jenkins_hashbig(key, length, initval); - #else - return jenkins_hashlittle(key, length, initval); - #endif +#if defined (WM_BIG_ENDIAN) + return jenkins_hashbig(key, length, initval); +#elif defined (WM_LITTLE_ENDIAN) + return jenkins_hashlittle(key, length, initval); #else - // endian-ness not known at compile-time: runtime endian test - const short endianTest = 0x0100; - - // yields 0x01 for big endian - if (*(reinterpret_cast<const char*>(&endianTest))) - { - return jenkins_hashbig(key, length, initval); - } - else - { - return jenkins_hashlittle(key, length, initval); - } + #error "Cannot determine WM_BIG_ENDIAN or WM_LITTLE_ENDIAN." #endif } diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C index 704c7e663725790107d462710eb62c7fff425a66..f159256982f72bfe3474686fc3526d512043fdb0 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C @@ -35,14 +35,10 @@ Description #include "SHA1.H" #include "IOstreams.H" +#include "endian.H" #include <cstring> -#if defined (__GLIBC__) - #include <endian.h> -#endif - - // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // //! \cond fileScope @@ -54,46 +50,25 @@ static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // -inline uint32_t Foam::SHA1::swapBytes(uint32_t n) +//! \cond fileScope +//- Swap bytes from internal to network (big-endian) order +static inline uint32_t swapBytes(uint32_t n) { - #ifdef __BYTE_ORDER - #if (__BYTE_ORDER == __BIG_ENDIAN) - return n; - #else - return - ( - ((n) << 24) - | (((n) & 0xff00) << 8) - | (((n) >> 8) & 0xff00) - | ((n) >> 24) - ); - #endif - #else - const short x = 0x0100; - - // yields 0x01 for big endian - if (*(reinterpret_cast<const char*>(&x))) - { - return n; - } - else - { - return - ( - ((n) << 24) - | (((n) & 0xff00) << 8) - | (((n) >> 8) & 0xff00) - | ((n) >> 24) - ); - } - #endif +#ifdef WM_LITTLE_ENDIAN + return Foam::endian::swap32(n); +#else + return n; +#endif } - -inline void Foam::SHA1::set_uint32(unsigned char *cp, uint32_t v) +//- Copy the 4-byte value into the memory location pointed to by *dst. +// If the architecture allows unaligned access this is equivalent to +// *(uint32_t *) cp = val +static inline void set_uint32(unsigned char *dst, uint32_t v) { - memcpy(cp, &v, sizeof(uint32_t)); + memcpy(dst, &v, sizeof(uint32_t)); } +//! \endcond // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // @@ -150,7 +125,7 @@ void Foam::SHA1::processBytes(const void *data, size_t len) unsigned char* bufp = reinterpret_cast<unsigned char*>(buffer_); size_t remaining = bufLen_; - memcpy (&bufp[remaining], data, len); + memcpy(&bufp[remaining], data, len); remaining += len; if (remaining >= 64) { diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H index e86b045d1933ee69f8bf512be5dc29d21d4eaf8c..dfbd26b2cce5af6f405a1ecfe227feed6ed3416b 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H @@ -91,14 +91,6 @@ class SHA1 // Private Member Functions - //- Swap bytes from internal to network (big-endian) order - static inline uint32_t swapBytes(uint32_t); - - //- Copy the 4-byte value into the memory location pointed to by *dst. - // If the architecture allows unaligned access this is equivalent to - // *(uint32_t *) cp = val - static inline void set_uint32(unsigned char *cp, uint32_t); - //- Process data block-wise, LEN must be a multiple of 64! void processBlock(const void *data, size_t len); diff --git a/src/lagrangian/intermediate/submodels/CloudFunctionObjects/CloudToVTK/vtkTools.C b/src/lagrangian/intermediate/submodels/CloudFunctionObjects/CloudToVTK/vtkTools.C index 71de62552669d9452cb5ea132d984f6f1ff7bd6e..911fa1f5341a9b4bdac418c043f14ccc2f5a9883 100644 --- a/src/lagrangian/intermediate/submodels/CloudFunctionObjects/CloudToVTK/vtkTools.C +++ b/src/lagrangian/intermediate/submodels/CloudFunctionObjects/CloudToVTK/vtkTools.C @@ -24,31 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "vtkTools.H" - -#if defined(__mips) -#include <standards.h> -#include <sys/endian.h> -#endif - -// MacOSX -#ifdef __DARWIN_BYTE_ORDER -#if __DARWIN_BYTE_ORDER==__DARWIN_BIG_ENDIAN -#undef LITTLE_ENDIAN -#else -#undef BIG_ENDIAN -#endif -#endif - -#if defined(LITTLE_ENDIAN) \ - || defined(_LITTLE_ENDIAN) \ - || defined(__LITTLE_ENDIAN) -# define LITTLEENDIAN 1 -#elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN) -# undef LITTLEENDIAN -#else -# error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined." -# error "Please add to compilation options" -#endif +#include "endian.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -84,9 +60,9 @@ void Foam::vtkTools::write { if (binary) { -# ifdef LITTLEENDIAN + #ifdef WM_LITTLE_ENDIAN swapWords(fField.size(), reinterpret_cast<label*>(fField.begin())); -# endif + #endif os.write ( reinterpret_cast<char*>(fField.begin()), @@ -137,9 +113,9 @@ void Foam::vtkTools::write { if (binary) { -# ifdef LITTLEENDIAN + #ifdef WM_LITTLE_ENDIAN swapWords(elems.size(), reinterpret_cast<label*>(elems.begin())); -# endif + #endif os.write ( reinterpret_cast<char*>(elems.begin()),