Mercurial > hg > orthanc-webviewer
diff Orthanc/Core/Toolbox.cpp @ 141:9362080e5e3d
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 15 Jul 2016 12:00:11 +0200 |
parents | d73006baca3f |
children | d850500b8ca6 |
line wrap: on
line diff
--- a/Orthanc/Core/Toolbox.cpp Thu Jun 30 20:45:54 2016 +0200 +++ b/Orthanc/Core/Toolbox.cpp Fri Jul 15 12:00:11 2016 +0200 @@ -111,6 +111,7 @@ namespace Orthanc { +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 static bool finish_; static ServerBarrierEvent barrierEvent_; @@ -188,6 +189,7 @@ const bool stopFlag = false; return ServerBarrierInternal(&stopFlag); } +#endif /* ORTHANC_SANDBOXED */ void Toolbox::ToUpperCase(std::string& s) @@ -228,6 +230,7 @@ } +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 void Toolbox::ReadFile(std::string& content, const std::string& path) { @@ -253,8 +256,10 @@ f.close(); } +#endif +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 bool Toolbox::ReadHeader(std::string& header, const std::string& path, size_t headerSize) @@ -298,8 +303,10 @@ return full; } +#endif +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 void Toolbox::WriteFile(const void* content, size_t size, const std::string& path) @@ -318,16 +325,20 @@ f.close(); } +#endif +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 void Toolbox::WriteFile(const std::string& content, const std::string& path) { WriteFile(content.size() > 0 ? content.c_str() : NULL, content.size(), path); } +#endif +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 void Toolbox::RemoveFile(const std::string& path) { if (boost::filesystem::exists(path)) @@ -342,7 +353,7 @@ } } } - +#endif void Toolbox::SplitUriComponents(UriComponents& components, @@ -513,6 +524,7 @@ +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 uint64_t Toolbox::GetFileSize(const std::string& path) { try @@ -524,6 +536,7 @@ throw OrthancException(ErrorCode_InexistentFile); } } +#endif #if !defined(ORTHANC_ENABLE_MD5) || ORTHANC_ENABLE_MD5 == 1 @@ -677,11 +690,15 @@ return std::string(pathbuf); } +#elif defined(ORTHANC_SANDBOXED) && ORTHANC_SANDBOXED == 1 + // Sandboxed Orthanc, no access to the executable + #else #error Support your platform here #endif +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 std::string Toolbox::GetPathToExecutable() { boost::filesystem::path p(GetPathToExecutableInternal()); @@ -694,6 +711,7 @@ boost::filesystem::path p(GetPathToExecutableInternal()); return boost::filesystem::absolute(p.parent_path()).string(); } +#endif static const char* GetBoostLocaleEncoding(const Encoding sourceEncoding) @@ -1137,6 +1155,7 @@ } +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 void Toolbox::MakeDirectory(const std::string& path) { if (boost::filesystem::exists(path)) @@ -1154,12 +1173,15 @@ } } } +#endif +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 bool Toolbox::IsExistingFile(const std::string& path) { return boost::filesystem::exists(path); } +#endif #if ORTHANC_PUGIXML_ENABLED == 1 @@ -1284,6 +1306,7 @@ #endif +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 void Toolbox::ExecuteSystemCommand(const std::string& command, const std::vector<std::string>& arguments) { @@ -1341,6 +1364,7 @@ throw OrthancException(ErrorCode_SystemCommand); } } +#endif bool Toolbox::IsInteger(const std::string& str) @@ -1461,6 +1485,7 @@ } +#if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 bool Toolbox::IsRegularFile(const std::string& path) { namespace fs = boost::filesystem; @@ -1480,5 +1505,172 @@ return false; } +#endif + + + FILE* Toolbox::OpenFile(const std::string& path, + FileMode mode) + { +#if defined(_WIN32) + // TODO Deal with special characters by converting to the current locale +#endif + + const char* m; + switch (mode) + { + case FileMode_ReadBinary: + m = "rb"; + break; + + case FileMode_WriteBinary: + m = "wb"; + break; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + + return fopen(path.c_str(), m); + } + + + + static bool IsUnreservedCharacter(char c) + { + // This function checks whether "c" is an unserved character + // wrt. an URI percent-encoding + // https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding%5Fin%5Fa%5FURI + + return ((c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || + c == '-' || + c == '_' || + c == '.' || + c == '~'); + } + + void Toolbox::UriEncode(std::string& target, + const std::string& source) + { + // Estimate the length of the percent-encoded URI + size_t length = 0; + + for (size_t i = 0; i < source.size(); i++) + { + if (IsUnreservedCharacter(source[i])) + { + length += 1; + } + else + { + // This character must be percent-encoded + length += 3; + } + } + + target.clear(); + target.reserve(length); + + for (size_t i = 0; i < source.size(); i++) + { + if (IsUnreservedCharacter(source[i])) + { + target.push_back(source[i]); + } + else + { + // This character must be percent-encoded + uint8_t byte = static_cast<uint8_t>(source[i]); + uint8_t a = byte >> 4; + uint8_t b = byte & 0x0f; + + target.push_back('%'); + target.push_back(a < 10 ? a + '0' : a - 10 + 'A'); + target.push_back(b < 10 ? b + '0' : b - 10 + 'A'); + } + } + } + + + static bool HasField(const Json::Value& json, + const std::string& key, + Json::ValueType expectedType) + { + if (json.type() != Json::objectValue || + !json.isMember(key)) + { + return false; + } + else if (json[key].type() == expectedType) + { + return true; + } + else + { + throw OrthancException(ErrorCode_BadParameterType); + } + } + + + std::string Toolbox::GetJsonStringField(const Json::Value& json, + const std::string& key, + const std::string& defaultValue) + { + if (HasField(json, key, Json::stringValue)) + { + return json[key].asString(); + } + else + { + return defaultValue; + } + } + + + bool Toolbox::GetJsonBooleanField(const ::Json::Value& json, + const std::string& key, + bool defaultValue) + { + if (HasField(json, key, Json::booleanValue)) + { + return json[key].asBool(); + } + else + { + return defaultValue; + } + } + + + int Toolbox::GetJsonIntegerField(const ::Json::Value& json, + const std::string& key, + int defaultValue) + { + if (HasField(json, key, Json::intValue)) + { + return json[key].asInt(); + } + else + { + return defaultValue; + } + } + + + unsigned int Toolbox::GetJsonUnsignedIntegerField(const ::Json::Value& json, + const std::string& key, + unsigned int defaultValue) + { + int v = GetJsonIntegerField(json, key, defaultValue); + + if (v < 0) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + else + { + return static_cast<unsigned int>(v); + } + } } -