| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// flxmlrpc Copyright (c) 2015 by W1HKJ, Dave Freese <[email protected]> |
| 4 |
// |
| 5 |
// XmlRpc++ Copyright (c) 2002-2008 by Chris Morley |
| 6 |
// |
| 7 |
// This file is part of fldigi |
| 8 |
// |
| 9 |
// flxmlrpc is free software; you can redistribute it and/or modify |
| 10 |
// it under the terms of the GNU Lesser General Public License as published by |
| 11 |
// the Free Software Foundation; either version 3 of the License, or |
| 12 |
// (at your option) any later version. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
// ---------------------------------------------------------------------------- |
| 17 |
|
| 18 |
#include <config.h> |
| 19 |
|
| 20 |
#include "XmlRpcUtil.h" |
| 21 |
|
| 22 |
#include <ctype.h> |
| 23 |
#include <iostream> |
| 24 |
#include <stdarg.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include "XmlRpc.h" |
| 29 |
|
| 30 |
using namespace XmlRpc; |
| 31 |
|
| 32 |
|
| 33 |
//#define USE_WINDOWS_DEBUG // To make the error and log messages go to VC++ debug output |
| 34 |
#ifdef USE_WINDOWS_DEBUG |
| 35 |
#define WIN32_LEAN_AND_MEAN |
| 36 |
#include <windows.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
// Version id |
| 40 |
const char XmlRpc::XMLRPC_VERSION[] = "XMLRPC++ 0.8"; |
| 41 |
|
| 42 |
// Default log verbosity: 0 for no messages through 5 (writes everything) |
| 43 |
int XmlRpcLogHandler::_verbosity = 0; |
| 44 |
|
| 45 |
// Default log handler |
| 46 |
static class DefaultLogHandler : public XmlRpcLogHandler { |
| 47 |
public: |
| 48 |
|
| 49 |
void log(int level, const char* msg) { |
| 50 |
#ifdef USE_WINDOWS_DEBUG |
| 51 |
if (level <= _verbosity) { OutputDebugString(msg); OutputDebugString("\n"); } |
| 52 |
#else |
| 53 |
if (level <= _verbosity) std::cout << msg << std::endl; |
| 54 |
#endif |
| 55 |
} |
| 56 |
|
| 57 |
} defaultLogHandler; |
| 58 |
|
| 59 |
// Message log singleton |
| 60 |
XmlRpcLogHandler* XmlRpcLogHandler::_logHandler = &defaultLogHandler; |
| 61 |
|
| 62 |
|
| 63 |
// Default error handler |
| 64 |
static class DefaultErrorHandler : public XmlRpcErrorHandler { |
| 65 |
public: |
| 66 |
|
| 67 |
void error(const char* msg) { |
| 68 |
#ifdef USE_WINDOWS_DEBUG |
| 69 |
// OutputDebugString(msg); OutputDebugString("\n"); |
| 70 |
#else |
| 71 |
// std::cerr << msg << std::endl; |
| 72 |
#endif |
| 73 |
} |
| 74 |
} defaultErrorHandler; |
| 75 |
|
| 76 |
|
| 77 |
// Error handler singleton |
| 78 |
XmlRpcErrorHandler* XmlRpcErrorHandler::_errorHandler = &defaultErrorHandler; |
| 79 |
|
| 80 |
|
| 81 |
// Easy API for log verbosity |
| 82 |
int XmlRpc::getVerbosity() { return XmlRpcLogHandler::getVerbosity(); } |
| 83 |
void XmlRpc::setVerbosity(int level) { XmlRpcLogHandler::setVerbosity(level); } |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
void XmlRpcUtil::log(int level, const char* fmt, ...) |
| 88 |
{ |
| 89 |
if (level <= XmlRpcLogHandler::getVerbosity()) |
| 90 |
{ |
| 91 |
va_list va; |
| 92 |
char buf[1024]; |
| 93 |
va_start( va, fmt); |
| 94 |
vsnprintf(buf,sizeof(buf)-1,fmt,va); |
| 95 |
buf[sizeof(buf)-1] = 0; |
| 96 |
XmlRpcLogHandler::getLogHandler()->log(level, buf); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
void XmlRpcUtil::error(const char* fmt, ...) |
| 102 |
{ |
| 103 |
va_list va; |
| 104 |
va_start(va, fmt); |
| 105 |
char buf[1024]; |
| 106 |
vsnprintf(buf,sizeof(buf)-1,fmt,va); |
| 107 |
buf[sizeof(buf)-1] = 0; |
| 108 |
XmlRpcErrorHandler::getErrorHandler()->error(buf); |
| 109 |
} |
| 110 |
|
| 111 |
// Returns true if the tag is parsed. No attributes are parsed. |
| 112 |
// Sets val to the contents between <tag> and </tag>, or an empty string if <tag/> is found. |
| 113 |
// Updates offset to char after </tag> |
| 114 |
bool |
| 115 |
XmlRpcUtil::parseTag(const char* tag, std::string const& xml, int* offset, std::string &val) |
| 116 |
{ |
| 117 |
size_t nxml = xml.length(); |
| 118 |
if (*offset >= int(nxml)) return false; |
| 119 |
|
| 120 |
// Find <tag (skips over anything preceeding...) |
| 121 |
std::string stag = "<"; |
| 122 |
stag += tag; |
| 123 |
size_t istart = xml.find(stag, *offset); |
| 124 |
if (istart == std::string::npos) return false; |
| 125 |
|
| 126 |
istart += stag.length(); |
| 127 |
|
| 128 |
// Advance istart past > or /> |
| 129 |
bool lastSlash = false; |
| 130 |
while (istart < nxml && xml[istart] != '>') |
| 131 |
{ |
| 132 |
lastSlash = (xml[istart] == '/'); |
| 133 |
++ istart; |
| 134 |
} |
| 135 |
|
| 136 |
if (istart == nxml) return false; |
| 137 |
|
| 138 |
val.clear(); |
| 139 |
if (lastSlash) // <tag/> |
| 140 |
{ |
| 141 |
*offset = int(istart+1); // 1 after > |
| 142 |
} |
| 143 |
else // Find </tag> |
| 144 |
{ |
| 145 |
std::string etag = "</"; |
| 146 |
etag += tag; |
| 147 |
etag += ">"; |
| 148 |
size_t iend = xml.find(etag, istart); |
| 149 |
|
| 150 |
if (iend == std::string::npos) return false; |
| 151 |
|
| 152 |
*offset = int(iend + etag.length()); |
| 153 |
++ istart; |
| 154 |
val = xml.substr(istart, iend-istart); |
| 155 |
} |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
// Returns true if the tag is found and updates offset to the char after the tag |
| 162 |
bool |
| 163 |
XmlRpcUtil::findTag(const char* tag, std::string const& xml, int* offset, bool* emptyTag) |
| 164 |
{ |
| 165 |
size_t nxml = xml.length(); |
| 166 |
if (*offset >= int(nxml)) return false; |
| 167 |
std::string stag = "<"; |
| 168 |
stag += tag; |
| 169 |
size_t istart = xml.find(stag, *offset); |
| 170 |
if (istart == std::string::npos) |
| 171 |
return false; |
| 172 |
|
| 173 |
istart += stag.length(); |
| 174 |
|
| 175 |
// Advance istart past > or />, skips attribs |
| 176 |
bool lastSlash = false; |
| 177 |
while (istart < nxml && xml[istart] != '>') |
| 178 |
{ |
| 179 |
lastSlash = (xml[istart] == '/'); |
| 180 |
++ istart; |
| 181 |
} |
| 182 |
|
| 183 |
if (istart == nxml) |
| 184 |
return false; |
| 185 |
|
| 186 |
*emptyTag = lastSlash; |
| 187 |
*offset = int(istart+1); // char after > |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
// Returns true if the <tag> or <tag/> is found at the specified offset (modulo any whitespace) |
| 193 |
// and updates offset to the char after the tag. *emptyTag is set to true if <tag/> is found, false otherwise |
| 194 |
bool |
| 195 |
XmlRpcUtil::nextTagIs(const char* tag, std::string const& xml, int* offset, bool *emptyTag) |
| 196 |
{ |
| 197 |
if (*offset >= int(xml.length())) |
| 198 |
return false; |
| 199 |
|
| 200 |
const char* cp = xml.c_str() + *offset; |
| 201 |
int nc = 0; |
| 202 |
while (*cp && isspace(*cp)) |
| 203 |
{ |
| 204 |
++cp; |
| 205 |
++nc; |
| 206 |
} |
| 207 |
|
| 208 |
int len = int(strlen(tag)); |
| 209 |
if (*cp == '<' && (strncmp(cp+1, tag, len) == 0)) |
| 210 |
{ |
| 211 |
cp += len + 1; |
| 212 |
if (*cp == '>') // <tag> |
| 213 |
{ |
| 214 |
*offset += nc + len + 2; |
| 215 |
*emptyTag = false; |
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
while (*cp && isspace(*cp)) { ++cp; ++nc; } |
| 220 |
|
| 221 |
if (*cp == '/' && *(cp + 1) == '>') // <tag /> |
| 222 |
{ |
| 223 |
*offset += nc + len + 3; |
| 224 |
*emptyTag = true; |
| 225 |
return true; |
| 226 |
} |
| 227 |
} |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
// Returns true if the next tag found at the specified offset is </tag> |
| 233 |
// and updates offset to the char after the tag. |
| 234 |
bool |
| 235 |
XmlRpcUtil::nextTagIsEnd(const char* tag, std::string const& xml, int* offset) |
| 236 |
{ |
| 237 |
if (*offset >= int(xml.length())) |
| 238 |
return false; |
| 239 |
|
| 240 |
const char* cp = xml.c_str() + *offset; |
| 241 |
int nc = 0; |
| 242 |
while (*cp && *cp != '<') |
| 243 |
{ |
| 244 |
++cp; |
| 245 |
++nc; |
| 246 |
} |
| 247 |
|
| 248 |
int len = int(strlen(tag)); |
| 249 |
if (*cp == '<' && *(cp+1) == '/' && (strncmp(cp+2, tag, len) == 0) && *(cp + len + 2) == '>') |
| 250 |
{ |
| 251 |
*offset += nc + len + 3; |
| 252 |
return true; |
| 253 |
} |
| 254 |
|
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
// xml encodings (xml-encoded entities are preceded with '&') |
| 260 |
static const char AMP = '&'; |
| 261 |
static const char rawEntity[] = { '<', '>', '&', '\'', '\"', 0 }; |
| 262 |
static const char* xmlEntity[] = { "lt;", "gt;", "amp;", "apos;", "quot;", 0 }; |
| 263 |
static const int xmlEntLen[] = { 3, 3, 4, 5, 5 }; |
| 264 |
|
| 265 |
|
| 266 |
// Replace xml-encoded entities with the raw text equivalents. |
| 267 |
|
| 268 |
std::string |
| 269 |
XmlRpcUtil::xmlDecode(const std::string& encoded) |
| 270 |
{ |
| 271 |
std::string::size_type iAmp = encoded.find(AMP); |
| 272 |
if (iAmp == std::string::npos) |
| 273 |
return encoded; |
| 274 |
|
| 275 |
std::string decoded(encoded, 0, iAmp); |
| 276 |
std::string::size_type iSize = encoded.size(); |
| 277 |
decoded.reserve(iSize); |
| 278 |
|
| 279 |
const char* ens = encoded.c_str(); |
| 280 |
while (iAmp != iSize) { |
| 281 |
if (encoded[iAmp] == AMP && iAmp+1 < iSize) { |
| 282 |
int iEntity; |
| 283 |
for (iEntity=0; xmlEntity[iEntity] != 0; ++iEntity) |
| 284 |
//if (encoded.compare(iAmp+1, xmlEntLen[iEntity], xmlEntity[iEntity]) == 0) |
| 285 |
if (strncmp(ens+iAmp+1, xmlEntity[iEntity], xmlEntLen[iEntity]) == 0) |
| 286 |
{ |
| 287 |
decoded += rawEntity[iEntity]; |
| 288 |
iAmp += xmlEntLen[iEntity]+1; |
| 289 |
break; |
| 290 |
} |
| 291 |
if (xmlEntity[iEntity] == 0) // unrecognized sequence |
| 292 |
decoded += encoded[iAmp++]; |
| 293 |
|
| 294 |
} else { |
| 295 |
decoded += encoded[iAmp++]; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return decoded; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
// Replace raw text with xml-encoded entities. |
| 304 |
|
| 305 |
std::string |
| 306 |
XmlRpcUtil::xmlEncode(const std::string& raw) |
| 307 |
{ |
| 308 |
std::string::size_type iRep = raw.find_first_of(rawEntity); |
| 309 |
if (iRep == std::string::npos) |
| 310 |
return raw; |
| 311 |
|
| 312 |
std::string encoded(raw, 0, iRep); |
| 313 |
std::string::size_type iSize = raw.size(); |
| 314 |
|
| 315 |
while (iRep != iSize) { |
| 316 |
int iEntity; |
| 317 |
for (iEntity=0; rawEntity[iEntity] != 0; ++iEntity) |
| 318 |
if (raw[iRep] == rawEntity[iEntity]) |
| 319 |
{ |
| 320 |
encoded += AMP; |
| 321 |
encoded += xmlEntity[iEntity]; |
| 322 |
break; |
| 323 |
} |
| 324 |
if (rawEntity[iEntity] == 0) |
| 325 |
encoded += raw[iRep]; |
| 326 |
++iRep; |
| 327 |
} |
| 328 |
return encoded; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|