Menu

[r3316]: / trunk / src / xmlrpc_datetime.c  Maximize  Restore  History

Download this file

673 lines (490 with data), 19.2 kB

  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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include "xmlrpc_config.h"
#define _XOPEN_SOURCE 600 /* Make sure strdup() is in <string.h> */
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <stdio.h>
#if MSVCRT
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
#include "bool.h"
#include "mallocvar.h"
#include "xmlrpc-c/c_util.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/base_int.h"
#include "xmlrpc-c/string_int.h"
#include "xmlrpc-c/time_int.h"
#if HAVE_REGEX
#include <regex.h>
#endif
#if MSVCRT
static const __int64 SECS_BETWEEN_EPOCHS = 11644473600;
static const __int64 SECS_TO_100NS = 10000000; /* 10^7 */
void
UnixTimeToFileTime(time_t const t,
LPFILETIME const pft) {
int64_t const ll =
Int32x32To64(t, SECS_TO_100NS) + SECS_BETWEEN_EPOCHS * SECS_TO_100NS;
pft->dwLowDateTime = (DWORD)ll;
pft->dwHighDateTime = (DWORD)(ll >> 32);
}
void
UnixTimeToSystemTime(time_t const t,
LPSYSTEMTIME const pst) {
FILETIME ft;
UnixTimeToFileTime(t, &ft);
FileTimeToSystemTime(&ft, pst);
}
static void
UnixTimeFromFileTime(xmlrpc_env * const envP,
LPFILETIME const pft,
time_t * const timeValueP) {
int64_t const WinEpoch100Ns =
((int64_t)pft->dwHighDateTime << 32) + pft->dwLowDateTime;
int64_t const unixEpoch100Ns =
WinEpoch100Ns - (SECS_BETWEEN_EPOCHS * SECS_TO_100NS);
int64_t const unixEpochSeconds =
unixEpoch100Ns / SECS_TO_100NS;
if ((time_t)unixEpochSeconds != unixEpochSeconds) {
/* Value is too big for a time_t; fail. */
xmlrpc_faultf(envP, "Does not indicate a valid date");
*timeValueP = (time_t)(-1);
} else
*timeValueP = (time_t)unixEpochSeconds;
}
static void
UnixTimeFromSystemTime(xmlrpc_env * const envP,
LPSYSTEMTIME const pst,
time_t * const timeValueP) {
FILETIME filetime;
SystemTimeToFileTime(pst, &filetime);
UnixTimeFromFileTime(envP, &filetime, timeValueP);
}
#endif /* MSVCRT */
static void
validateDatetimeType(xmlrpc_env * const envP,
const xmlrpc_value * const valueP) {
if (valueP->_type != XMLRPC_TYPE_DATETIME) {
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR, "Value of type %s supplied where "
"type %s was expected.",
xmlrpc_type_name(valueP->_type),
xmlrpc_type_name(XMLRPC_TYPE_DATETIME));
}
}
void
xmlrpc_read_datetime(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
xmlrpc_datetime * const dtP) {
validateDatetimeType(envP, valueP);
if (!envP->fault_occurred) {
*dtP = valueP->_value.dt;
}
}
void
xmlrpc_read_datetime_str(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP) {
/*----------------------------------------------------------------------------
This exists for backward compatibility. No normal modern program would
want to see a datetime value in this format. Note that the format isn't
even ISO 8601 -- it's a bizarre hybrid of two ISO 8601 formats.
Do not extend this.
This exists because Xmlrpc-c was at one time lazy and this was the only way
to extract the value. An xmlrpc_value in those days represented a datetime
with the actual XML-RPC wire format of a datetime, and this function simply
returned a copy of it.
-----------------------------------------------------------------------------*/
validateDatetimeType(envP, valueP);
if (!envP->fault_occurred) {
time_t secs;
unsigned int usecs;
xmlrpc_read_datetime_usec(envP, valueP, &secs, &usecs);
if (!envP->fault_occurred) {
struct tm brokenTime;
char dtString[64];
xmlrpc_gmtime(secs, &brokenTime);
/* Note that this format is NOT ISO 8601 -- it's a bizarre
hybrid of two ISO 8601 formats.
*/
strftime(dtString, sizeof(dtString), "%Y%m%dT%H:%M:%S",
&brokenTime);
if (usecs != 0) {
char usecString[32];
assert(usecs < 1000000);
XMLRPC_SNPRINTF(usecString, sizeof(usecString),
".%06u", usecs);
STRSCAT(dtString, usecString);
}
*stringValueP = strdup(dtString);
if (*stringValueP == NULL)
xmlrpc_faultf(envP,
"Unable to allocate memory for datetime string");
}
}
}
void
xmlrpc_read_datetime_str_old(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP) {
assert(valueP->_cache);
validateDatetimeType(envP, valueP);
if (!envP->fault_occurred) {
const char ** const readBufferP = valueP->_cache;
if (!*readBufferP)
/* Nobody's asked for the internal buffer before. Set it up. */
xmlrpc_read_datetime_str(envP, valueP, readBufferP);
*stringValueP = *readBufferP;
}
}
void
xmlrpc_read_datetime_usec(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
time_t * const secsP,
unsigned int * const usecsP) {
validateDatetimeType(envP, valueP);
if (!envP->fault_occurred) {
if (valueP->_value.dt.Y < 1970)
xmlrpc_faultf(envP, "Year (%u) is too early to represent as "
"a standard Unix time",
valueP->_value.dt.Y);
else {
struct tm brokenTime;
const char * error;
brokenTime.tm_sec = valueP->_value.dt.s;
brokenTime.tm_min = valueP->_value.dt.m;
brokenTime.tm_hour = valueP->_value.dt.h;
brokenTime.tm_mday = valueP->_value.dt.D;
brokenTime.tm_mon = valueP->_value.dt.M - 1;
brokenTime.tm_year = valueP->_value.dt.Y - 1900;
xmlrpc_timegm(&brokenTime, secsP, &error);
if (error) {
/* Ideally, this wouldn't be possible - it wouldn't be
possible to create an xmlrpc_value that doesn't actually
represent a real datetime. But today, we're lazy and
don't fully validate incoming XML-RPC <dateTime.iso8601>
elements, and we also have the legacy
xmlrpc_datetime_new_str() constructor to which the user
may feed garbage.
We should tighten that up and then simply assert here that
xmlrpc_timegm() succeeded.
*/
xmlrpc_env_set_fault_formatted(envP, XMLRPC_PARSE_ERROR,
"A datetime received in an XML-RPC message "
"or generated with legacy Xmlrpc-c facilities "
"does not validly describe a datetime. %s",
error);
xmlrpc_strfree(error);
} else
*usecsP = valueP->_value.dt.u;
}
}
}
void
xmlrpc_read_datetime_sec(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
time_t * const timeValueP) {
unsigned int usecs;
xmlrpc_read_datetime_usec(envP, valueP, timeValueP, &usecs);
}
#if XMLRPC_HAVE_TIMEVAL
void
xmlrpc_read_datetime_timeval(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
struct timeval * const timeValueP) {
time_t secs;
unsigned int usecs;
xmlrpc_read_datetime_usec(envP, valueP, &secs, &usecs);
timeValueP->tv_sec = secs;
timeValueP->tv_usec = usecs;
}
#endif
#if XMLRPC_HAVE_TIMESPEC
void
xmlrpc_read_datetime_timespec(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
struct timespec * const timeValueP) {
time_t secs;
unsigned int usecs;
xmlrpc_read_datetime_usec(envP, valueP, &secs, &usecs);
timeValueP->tv_sec = secs;
timeValueP->tv_nsec = usecs * 1000;
}
#endif
void
xmlrpc_read_datetime_8601(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const iso8601ValueP) {
/*----------------------------------------------------------------------------
Get the datetime in ISO 8601 format.
ISO 8601 allows a variety of representations for each datetime.
The particular one we return is as in the following example.
19930214T131030,250000Z
(13:10:30.25 on February 14, 1993)
There are always 4 digits for the year. There are always 6 digits after the
comma (microseconds). Midnight is hour 0, not 24.
-----------------------------------------------------------------------------*/
validateDatetimeType(envP, valueP);
if (!envP->fault_occurred) {
xmlrpc_datetime dt;
xmlrpc_read_datetime(envP, valueP, &dt);
if (!envP->fault_occurred) {
if (dt.Y > 9999)
xmlrpc_faultf(envP, "Too far in future (year %u). "
"ISO 8601 cannot "
"represent years after AD 9999", dt.Y);
else {
xmlrpc_asprintf(iso8601ValueP,
"%04u%02u%02uT%02u%02u%02u,%06uZ",
dt.Y, dt.M, dt.D, dt.h, dt.m, dt.s, dt.u);
if (xmlrpc_strnomem(*iso8601ValueP))
xmlrpc_faultf(envP,
"Unable to allocate memory "
"for datetime string");
if (envP->fault_occurred)
xmlrpc_strfree(*iso8601ValueP);
}
}
}
}
xmlrpc_value *
xmlrpc_datetime_new(xmlrpc_env * const envP,
xmlrpc_datetime const dt) {
xmlrpc_value * valP;
const char ** readBufferP;
MALLOCVAR(readBufferP);
if (!readBufferP)
xmlrpc_faultf(envP, "Couldn't get memory for the cache part of the "
"XML-RPC datetime value object");
else {
*readBufferP = NULL;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_DATETIME;
valP->_value.dt = dt;
valP->_cache = readBufferP;
}
if (envP->fault_occurred)
free(readBufferP);
}
return valP;
}
static void
parseDatetimeString(const char * const datetimeString,
xmlrpc_datetime * const dtP) {
size_t 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];
/* Because we require input to be valid: */
assert(dtStrlen >= 17 && dtStrlen != 18 && dtStrlen <= 24);
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) {
size_t const pad = 24 - dtStrlen;
size_t 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_faultf(envP, "Not a digit: '%c'", dt[i]);
if (dt[8] != 'T')
xmlrpc_faultf(envP, "9th character is '%c', not 'T'", dt[8]);
if (!isdigit(dt[9]))
xmlrpc_faultf(envP, "Not a digit: '%c'", dt[9]);
if (!isdigit(dt[10]))
xmlrpc_faultf(envP, "Not a digit: '%c'", dt[10]);
if (dt[11] != ':')
xmlrpc_faultf(envP, "Not a colon: '%c'", dt[11]);
if (!isdigit(dt[12]))
xmlrpc_faultf(envP, "Not a digit: '%c'", dt[12]);
if (!isdigit(dt[13]))
xmlrpc_faultf(envP, "Not a digit: '%c'", dt[13]);
if (dt[14] != ':')
xmlrpc_faultf(envP, "Not a colon: '%c'", dt[14]);
if (!isdigit(dt[15]))
xmlrpc_faultf(envP, "Not a digit: '%c'", dt[15]);
if (!isdigit(dt[16]))
xmlrpc_faultf(envP, "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_faultf(envP, "'%c' where only a period is valid", dt[17]);
} else {
if (dt[18] == '\0')
xmlrpc_faultf(envP, "Nothing after decimal point");
else {
unsigned int i;
for (i = 18; dt[i] != '\0' && !envP->fault_occurred; ++i) {
if (!isdigit(dt[i]))
xmlrpc_faultf(envP,
"Non-digit in fractional seconds: '%c'",
dt[i]);
}
}
}
}
}
static void
validateFormat(xmlrpc_env * const envP,
const char * const dt) {
if (strlen(dt) < 17)
xmlrpc_faultf(envP,
"Invalid length of %u of datetime string. "
"Must be at least 17 characters",
(unsigned)strlen(dt));
else {
validateFirst17(envP, dt);
if (!envP->fault_occurred)
validateFractionalSeconds(envP, dt);
}
}
/* Microsoft Visual C in debug mode produces code that complains about
returning an undefined value from xmlrpc_datetime_new_str(). It's a bogus
complaint, because this function is defined to return nothing meaningful
those cases. So we disable the check.
*/
#pragma runtime_checks("u", off)
xmlrpc_value *
xmlrpc_datetime_new_str(xmlrpc_env * const envP,
const char * const datetimeString) {
/*----------------------------------------------------------------------------
This exists only for backward compatibility. Originally, this was the
only way to create a datetime XML-RPC value, because we had a really
lazy implementation of XML-RPC serialization and parsing (basically, the
user did it!).
Do not extend this. The user should use more normal C representations
of datetimes.
-----------------------------------------------------------------------------*/
xmlrpc_value * retval;
validateFormat(envP, datetimeString);
if (!envP->fault_occurred) {
xmlrpc_datetime dt;
parseDatetimeString(datetimeString, &dt);
/* Note that parseDatetimeString() can generate an invalid datetime
value, e.g. Hour 25 or February 30. Ideally, we would catch that
here, but because of laziness, we simply accept the possibility of
invalid xmlrpc_datetime in xmlrpc_value and whoever uses the the
xmlrpc_value has to deal with it.
*/
retval = xmlrpc_datetime_new(envP, dt);
}
return retval;
}
#pragma runtime_checks("u", restore)
xmlrpc_value *
xmlrpc_datetime_new_usec(xmlrpc_env * const envP,
time_t const secs,
unsigned int const usecs) {
xmlrpc_value * valueP;
if (usecs >= 1000000)
xmlrpc_faultf(envP, "Number of fractional microseconds must be less "
"than one million. You specified %u", usecs);
else {
struct tm brokenTime;
xmlrpc_datetime dt;
xmlrpc_gmtime(secs, &brokenTime);
dt.s = brokenTime.tm_sec;
dt.m = brokenTime.tm_min;
dt.h = brokenTime.tm_hour;
dt.D = brokenTime.tm_mday;
dt.M = brokenTime.tm_mon + 1;
dt.Y = 1900 + brokenTime.tm_year;
dt.u = usecs;
valueP = xmlrpc_datetime_new(envP, dt);
}
return valueP;
}
xmlrpc_value *
xmlrpc_datetime_new_sec(xmlrpc_env * const envP,
time_t const value) {
return xmlrpc_datetime_new_usec(envP, value, 0);
}
#if XMLRPC_HAVE_TIMEVAL
xmlrpc_value *
xmlrpc_datetime_new_timeval(xmlrpc_env * const envP,
struct timeval const value) {
return xmlrpc_datetime_new_usec(envP, value.tv_sec, value.tv_usec);
}
#endif
#if XMLRPC_HAVE_TIMESPEC
xmlrpc_value *
xmlrpc_datetime_new_timespec(xmlrpc_env * const envP,
struct timespec const value) {
return xmlrpc_datetime_new_usec(envP, value.tv_sec, value.tv_nsec/1000);
}
#endif
xmlrpc_value *
xmlrpc_datetime_new_value(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
xmlrpc_value * retval;
if (valueP->_type != XMLRPC_TYPE_DATETIME) {
xmlrpc_env_set_fault_formatted(envP, XMLRPC_TYPE_ERROR,
"Value is not a datetime. "
"It is type #%d", valueP->_type);
retval = NULL;
} else
retval = xmlrpc_datetime_new(envP, valueP->_value.dt);
return retval;
}
void
xmlrpc_destroyDatetime(xmlrpc_value * const datetimeP) {
const char ** const readBufferP = datetimeP->_cache;
if (*readBufferP)
xmlrpc_strfree(*readBufferP);
free(datetimeP->_cache);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.