Menu

[r1477]: / advanced / src / xmlrpc_datetime.c  Maximize  Restore  History

Download this file

372 lines (275 with data), 10.1 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
#include "xmlrpc_config.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#if MSVCRT
#include <windows.h>
#endif
#include "bool.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/base_int.h"
#include "xmlrpc-c/string_int.h"
#include "xmlrpc-c/time_int.h"
/* Future work: the XMLRPC_TYPE_DATETIME xmlrpc_value should store the
datetime as something computation-friendly, not as a string. The
XML-RPC XML parser should parse the string value and reject the XML if
it isn't valid.
But this file should remain the authority on datetimes, so the XML
parser and builder should call on routines in here to do that.
time_t won't work because it can't represent times before 1970 or
after 2038. We need to figure out something better.
*/
#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_str(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP) {
validateDatetimeType(envP, valueP);
if (!envP->fault_occurred) {
const char * const contents =
XMLRPC_MEMBLOCK_CONTENTS(char, &valueP->_block);
*stringValueP = strdup(contents);
if (*stringValueP == NULL)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INTERNAL_ERROR, "Unable to allocate space "
"for datetime string");
}
}
void
xmlrpc_read_datetime_str_old(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP) {
validateDatetimeType(envP, valueP);
if (!envP->fault_occurred) {
*stringValueP = XMLRPC_MEMBLOCK_CONTENTS(char, &valueP->_block);
}
}
static void
parseDateNumbers(const char * const t,
unsigned int * const YP,
unsigned int * const MP,
unsigned int * const DP,
unsigned int * const hP,
unsigned int * const mP,
unsigned int * const sP) {
char year[4+1];
char month[2+1];
char day[2+1];
char hour[2+1];
char minute[2+1];
char second[2+1];
assert(strlen(t) == 17);
year[0] = t[ 0];
year[1] = t[ 1];
year[2] = t[ 2];
year[3] = t[ 3];
year[4] = '\0';
month[0] = t[ 4];
month[1] = t[ 5];
month[2] = '\0';
day[0] = t[ 6];
day[1] = t[ 7];
day[2] = '\0';
assert(t[ 8] == 'T');
hour[0] = t[ 9];
hour[1] = t[10];
hour[2] = '\0';
assert(t[11] == ':');
minute[0] = t[12];
minute[1] = t[13];
minute[2] = '\0';
assert(t[14] == ':');
second[0] = t[15];
second[1] = t[16];
second[2] = '\0';
*YP = atoi(year);
*MP = atoi(month);
*DP = atoi(day);
*hP = atoi(hour);
*mP = atoi(minute);
*sP = atoi(second);
}
static void
validateFormat(xmlrpc_env * const envP,
const char * const t) {
if (strlen(t) != 17)
xmlrpc_faultf(envP, "%u characters instead of 15.", strlen(t));
else if (t[8] != 'T')
xmlrpc_faultf(envP, "9th character is '%c', not 'T'", t[8]);
else {
unsigned int i;
for (i = 0; i < 8 && !envP->fault_occurred; ++i)
if (!isdigit(t[i]))
xmlrpc_faultf(envP, "Not a digit: '%c'", t[i]);
if (!isdigit(t[9]))
xmlrpc_faultf(envP, "Not a digit: '%c'", t[9]);
if (!isdigit(t[10]))
xmlrpc_faultf(envP, "Not a digit: '%c'", t[10]);
if (t[11] != ':')
xmlrpc_faultf(envP, "Not a colon: '%c'", t[11]);
if (!isdigit(t[12]))
xmlrpc_faultf(envP, "Not a digit: '%c'", t[12]);
if (!isdigit(t[13]))
xmlrpc_faultf(envP, "Not a digit: '%c'", t[13]);
if (t[14] != ':')
xmlrpc_faultf(envP, "Not a colon: '%c'", t[14]);
if (!isdigit(t[15]))
xmlrpc_faultf(envP, "Not a digit: '%c'", t[15]);
if (!isdigit(t[16]))
xmlrpc_faultf(envP, "Not a digit: '%c'", t[16]);
}
}
static void
parseDatetime(xmlrpc_env * const envP,
const char * const t,
time_t * const timeValueP) {
/*----------------------------------------------------------------------------
Parse a time in the format stored in an xmlrpc_value and return the
time that it represents.
t[] is the input time string. We return the result as *timeValueP.
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.
The input is capable of representing datetimes that cannot be expressed
as a time_t. In that case, we fail, with fault code
XMLRPC_INTERNAL_ERROR.
And of course the input may not validly represent a datetime at all.
In that case too, we fail with fault code XMLRPC_PARSE_ERROR.
-----------------------------------------------------------------------------*/
validateFormat(envP, t);
if (!envP->fault_occurred) {
unsigned int Y, M, D, h, m, s;
parseDateNumbers(t, &Y, &M, &D, &h, &m, &s);
if (Y < 1970)
xmlrpc_env_set_fault(envP, XMLRPC_INTERNAL_ERROR,
"Year is too early to represent as "
"a standard Unix time");
else {
struct tm brokenTime;
const char * error;
brokenTime.tm_sec = s;
brokenTime.tm_min = m;
brokenTime.tm_hour = h;
brokenTime.tm_mday = D;
brokenTime.tm_mon = M - 1;
brokenTime.tm_year = Y - 1900;
xmlrpc_timegm(&brokenTime, timeValueP, &error);
if (error) {
xmlrpc_env_set_fault(envP, XMLRPC_PARSE_ERROR, error);
xmlrpc_strfree(error);
}
}
}
}
void
xmlrpc_read_datetime_sec(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
time_t * const timeValueP) {
validateDatetimeType(envP, valueP);
if (!envP->fault_occurred)
parseDatetime(envP,
XMLRPC_MEMBLOCK_CONTENTS(char, &valueP->_block),
timeValueP);
}
xmlrpc_value *
xmlrpc_datetime_new_str(xmlrpc_env * const envP,
const char * const value) {
xmlrpc_value * valP;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_DATETIME;
XMLRPC_TYPED_MEM_BLOCK_INIT(
char, envP, &valP->_block, strlen(value) + 1);
if (!envP->fault_occurred) {
char * const contents =
XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, &valP->_block);
strcpy(contents, value);
}
if (envP->fault_occurred)
free(valP);
}
return valP;
}
xmlrpc_value *
xmlrpc_datetime_new_sec(xmlrpc_env * const envP,
time_t const value) {
xmlrpc_value * valP;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
struct tm brokenTime;
char timeString[64];
valP->_type = XMLRPC_TYPE_DATETIME;
xmlrpc_gmtime(value, &brokenTime);
/* Note that this format is NOT ISO 8601 -- it's a bizarre
hybrid of two ISO 8601 formats.
*/
strftime(timeString, sizeof(timeString), "%Y%m%dT%H:%M:%S",
&brokenTime);
XMLRPC_TYPED_MEM_BLOCK_INIT(
char, envP, &valP->_block, strlen(timeString) + 1);
if (!envP->fault_occurred) {
char * const contents =
XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, &valP->_block);
strcpy(contents, timeString);
}
if (envP->fault_occurred)
free(valP);
}
return valP;
}
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.