File size: 14,468 Bytes
fd49381 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
#include "xmlrpc_config.h"
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#if HAVE_REGEX
#include <sys/types.h> /* Missing from regex.h in GNU libc */
#include <regex.h>
#endif
#include "bool.h"
#include "c_util.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/base_int.h"
#include "parse_datetime.h"
#if HAVE_REGEX
static unsigned int
digitStringValue(const char * const string,
regmatch_t const match) {
/*----------------------------------------------------------------------------
Return the numerical value of the decimal whole number substring of
'string' identified by 'match'. E.g. if 'string' is 'abc34d' and
'match' says start at 3 and end at 5, we return 34.
-----------------------------------------------------------------------------*/
unsigned int i;
unsigned int accum;
assert(match.rm_so >= 0);
assert(match.rm_eo >= 0);
for (i = match.rm_so, accum = 0; i < (unsigned)match.rm_eo; ++i) {
accum *= 10;
assert(isdigit(string[i]));
accum += string[i] - '0';
}
return accum;
}
#endif /* HAVE_REGEX */
#if HAVE_REGEX
static unsigned int
digitStringMillionths(const char * const string,
regmatch_t const match) {
/*----------------------------------------------------------------------------
Return the number of millionths represented by the digits after the
decimal point in a decimal string, where thse digits are the substring
of 'string' identified by 'match'. E.g. if the substring is
34, we return 340,000.
-----------------------------------------------------------------------------*/
unsigned int i;
unsigned int accum;
assert(match.rm_so >= 0);
assert(match.rm_eo >= 0);
for (i = match.rm_so, accum = 0; i < (unsigned)match.rm_so+6; ++i) {
accum *= 10;
if (i < (unsigned)match.rm_eo) {
assert(isdigit(string[i]));
accum += string[i] - '0';
}
}
return accum;
}
#endif /* HAVE_REGEX */
#if HAVE_REGEX
static void
subParseDtRegex_standard(regmatch_t * const matches,
const char * const datetimeString,
xmlrpc_datetime * const dtP) {
dtP->Y = digitStringValue(datetimeString, matches[1]);
dtP->M = digitStringValue(datetimeString, matches[2]);
dtP->D = digitStringValue(datetimeString, matches[3]);
dtP->h = digitStringValue(datetimeString, matches[4]);
dtP->m = digitStringValue(datetimeString, matches[5]);
dtP->s = digitStringValue(datetimeString, matches[6]);
if (matches[7].rm_so == -1)
dtP->u = 0;
else
dtP->u = digitStringMillionths(datetimeString, matches[7]);
}
static void
subParseDtRegex_standardtzd(regmatch_t * const matches,
const char * const datetimeString,
xmlrpc_datetime * const dtP) {
dtP->Y = digitStringValue(datetimeString, matches[1]);
dtP->M = digitStringValue(datetimeString, matches[2]);
dtP->D = digitStringValue(datetimeString, matches[3]);
dtP->h = digitStringValue(datetimeString, matches[4]);
dtP->m = digitStringValue(datetimeString, matches[5]);
dtP->s = digitStringValue(datetimeString, matches[6]);
dtP->u = 0;
}
#endif /* HAVE_REGEX */
#if HAVE_REGEX
typedef void (*regparsefunc_t)(regmatch_t * const matches,
const char * const datetimeString,
xmlrpc_datetime * const dtP);
struct regexParser {
const char * const regex;
regparsefunc_t func;
};
static const struct regexParser iso8601Regex[]
/* Each entry of this table is instructions for recognizing and parsing
some form of a "dateTime.iso8601" XML element.
(Note that we recognize far more than just the XML-RPC standard
dateTime.iso8601).
*/
= {
{
/* Examples:
YYYYMMDD[T]HHMMSS
YYYY-MM-DD[T]HH:MM:SS
YYYY-MM-DD[T]HH:MM:SS.ssss
*/
"^([0-9]{4})\\-?([0-9]{2})\\-?([0-9]{2})T"
"([0-9]{2}):?([0-9]{2}):?([0-9]{2})\\.?([0-9]+)?$",
subParseDtRegex_standard
},
{
/* Examples:
YYYYMMDD[T]HHMMSS[Z]
YYYYMMDD[T]HHMMSS[+-]hh
YYYYMMDD[T]HHMMSS[+-]hhmm
*/
"^([0-9]{4})\\-?([0-9]{2})\\-?([0-9]{2})T"
"([0-9]{2}):?([0-9]{2}):?([0-9]{2})[Z\\+\\-]([0-9]{2,4})?$",
subParseDtRegex_standardtzd
},
{ NULL, NULL }
};
#endif /* HAVE_REGEX */
#if HAVE_REGEX
static void
parseDtRegex(xmlrpc_env * const envP,
const char * const datetimeString,
xmlrpc_datetime * const dtP) {
unsigned int i;
const struct regexParser * parserP;
/* The parser that matches 'datetimeString'. Null if no match yet
found.
*/
regmatch_t matches[1024];
for (i = 0, parserP = NULL; iso8601Regex[i].regex && !parserP; ++i) {
const struct regexParser * const thisParserP = &iso8601Regex[i];
regex_t re;
int status;
status = regcomp(&re, thisParserP->regex, REG_ICASE | REG_EXTENDED);
/* Our regex is valid, so it must have compiled: */
assert(status == 0); if (status){}; /* quiet compiler warning */
{
int status;
status = regexec(&re, datetimeString, ARRAY_SIZE(matches),
matches, 0);
if (status == 0) {
assert(matches[0].rm_so != -1); /* Match of whole regex */
parserP = thisParserP;
}
}
regfree(&re);
}
if (parserP) {
parserP->func(matches, datetimeString, dtP);
} else {
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"value '%s' is not of any form we recognize "
"for a <dateTime.iso8601> element",
datetimeString);
}
}
#endif /* HAVE_REGEX */
static __inline__ void
parseDtNoRegex(xmlrpc_env * const envP,
const char * const datetimeString,
xmlrpc_datetime * const dtP) {
unsigned int const dtStrlen = strlen(datetimeString);
char year[4+1];
char month[2+1];
char day[2+1];
char hour[2+1];
char minute[2+1];
char second[2+1];
if (dtStrlen < 17 || dtStrlen == 18 || dtStrlen > 24)
xmlrpc_faultf(envP, "could not parse date, size incompatible: '%d'",
dtStrlen);
else {
year[0] = datetimeString[ 0];
year[1] = datetimeString[ 1];
year[2] = datetimeString[ 2];
year[3] = datetimeString[ 3];
year[4] = '\0';
month[0] = datetimeString[ 4];
month[1] = datetimeString[ 5];
month[2] = '\0';
day[0] = datetimeString[ 6];
day[1] = datetimeString[ 7];
day[2] = '\0';
assert(datetimeString[ 8] == 'T');
hour[0] = datetimeString[ 9];
hour[1] = datetimeString[10];
hour[2] = '\0';
assert(datetimeString[11] == ':');
minute[0] = datetimeString[12];
minute[1] = datetimeString[13];
minute[2] = '\0';
assert(datetimeString[14] == ':');
second[0] = datetimeString[15];
second[1] = datetimeString[16];
second[2] = '\0';
if (dtStrlen > 17) {
unsigned int const pad = 24 - dtStrlen;
unsigned int i;
dtP->u = atoi(&datetimeString[18]);
for (i = 0; i < pad; ++i)
dtP->u *= 10;
} else
dtP->u = 0;
dtP->Y = atoi(year);
dtP->M = atoi(month);
dtP->D = atoi(day);
dtP->h = atoi(hour);
dtP->m = atoi(minute);
dtP->s = atoi(second);
}
}
static void
validateFirst17(xmlrpc_env * const envP,
const char * const dt) {
/*----------------------------------------------------------------------------
Assuming 'dt' is at least 17 characters long, validate that the first
17 characters are a valid XML-RPC datetime, e.g.
"20080628T16:35:02"
-----------------------------------------------------------------------------*/
unsigned int i;
for (i = 0; i < 8 && !envP->fault_occurred; ++i)
if (!isdigit(dt[i]))
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a digit: '%c'", dt[i]);
if (dt[8] != 'T')
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "9th character is '%c', not 'T'",
dt[8]);
if (!isdigit(dt[9]))
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a digit: '%c'", dt[9]);
if (!isdigit(dt[10]))
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a digit: '%c'", dt[10]);
if (dt[11] != ':')
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a colon: '%c'", dt[11]);
if (!isdigit(dt[12]))
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a digit: '%c'", dt[12]);
if (!isdigit(dt[13]))
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a digit: '%c'", dt[13]);
if (dt[14] != ':')
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a colon: '%c'", dt[14]);
if (!isdigit(dt[15]))
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a digit: '%c'", dt[15]);
if (!isdigit(dt[16]))
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Not a digit: '%c'", dt[16]);
}
static void
validateFractionalSeconds(xmlrpc_env * const envP,
const char * const dt) {
/*----------------------------------------------------------------------------
Validate the fractional seconds part of the XML-RPC datetime string
'dt', if any. That's the decimal point and everything following
it.
-----------------------------------------------------------------------------*/
if (strlen(dt) > 17) {
if (dt[17] != '.') {
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"'%c' where only a period is valid", dt[17]);
} else {
if (dt[18] == '\0')
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR, "Nothing after decimal point");
else {
unsigned int i;
for (i = 18; dt[i] != '\0' && !envP->fault_occurred; ++i) {
if (!isdigit(dt[i]))
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"Non-digit in fractional seconds: '%c'", dt[i]);
}
}
}
}
}
static __inline__ void
validateFormatNoRegex(xmlrpc_env * const envP,
const char * const dt) {
if (strlen(dt) < 17)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"Invalid length of %u of datetime. "
"Must be at least 17 characters",
(unsigned)strlen(dt));
else {
validateFirst17(envP, dt);
validateFractionalSeconds(envP, dt);
}
}
static void
validateXmlrpcDatetimeSome(xmlrpc_env * const envP,
xmlrpc_datetime const dt) {
/*----------------------------------------------------------------------------
Type xmlrpc_datetime is defined such that it can represent a nonexistent
datetime such as February 30.
Validate that 'dt' doesn't have glaring invalidities such as Hour 25.
We leave the possibility of more subtle invalidity such as February 30.
-----------------------------------------------------------------------------*/
if (dt.M < 1 || dt.M > 12)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"Month of year value %u is not in the range 1-12", dt.M);
else if (dt.D < 1 || dt.D > 31)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"Day of month value %u is not in the range 1-31", dt.D);
else if (dt.h > 23)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"Hour of day value %u is not in the range 0-23", dt.h);
else if (dt.m > 59)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"Minute of hour value %u is not in the range 0-59", dt.m);
else if (dt.s > 59)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"Second of minute value %u is not in the range 0-59", dt.s);
else if (dt.u > 999999)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_PARSE_ERROR,
"Microsecond of second value %u is not in the range 0-1M", dt.u);
}
void
xmlrpc_parseDatetime(xmlrpc_env * const envP,
const char * const datetimeString,
xmlrpc_value ** const valuePP) {
/*----------------------------------------------------------------------------
Parse the content of a <datetime.iso8601> XML-RPC XML element, e.g.
"20000301T00:00:00".
'str' is that content.
Example of the format we parse: "19980717T14:08:55"
Note that this is not quite ISO 8601. It's a bizarre combination of
two ISO 8601 formats.
Note that Xmlrpc-c recognizes various extensions of the XML-RPC
<datetime.iso8601> element type.
'str' may not be valid XML-RPC (with extensions). In that case we fail
with fault code XMLRPC_PARSE_ERROR.
-----------------------------------------------------------------------------*/
xmlrpc_datetime dt;
#if HAVE_REGEX
parseDtRegex(envP, datetimeString, &dt);
#else
/* Note: validation is not as strong without regex */
validateFormatNoRegex(envP, datetimeString);
if (!envP->fault_occurred)
parseDtNoRegex(envP, datetimeString, &dt);
#endif
if (!envP->fault_occurred) {
validateXmlrpcDatetimeSome(envP, dt);
if (!envP->fault_occurred)
*valuePP = xmlrpc_datetime_new(envP, dt);
}
}
|