OpenVAS Scanner  7.0.0~git
nasl_isotime.c File Reference

Implementation of an API for ISOTIME values. More...

#include "nasl_isotime.h"
#include "nasl_debug.h"
#include "nasl_global_ctxt.h"
#include "nasl_lex_ctxt.h"
#include "nasl_tree.h"
#include "nasl_var.h"
#include <ctype.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
Include dependency graph for nasl_isotime.c:

Go to the source code of this file.

Macros

#define DIM(v)   (sizeof (v) / sizeof ((v)[0]))
 
#define DIMof(type, member)   DIM (((type *) 0)->member)
 
#define ISOTIME_SIZE   19
 
#define JD_DIFF   1721060L
 
#define spacep(p)   (*(p) == ' ' || *(p) == '\t')
 
#define digitp(p)   (*(p) >= '0' && *(p) <= '9')
 
#define atoi_1(p)   (*(p) - '0')
 
#define atoi_2(p)   ((atoi_1 (p) * 10) + atoi_1 ((p) + 1))
 
#define atoi_4(p)   ((atoi_2 (p) * 100) + atoi_2 ((p) + 2))
 

Typedefs

typedef char my_isotime_t[ISOTIME_SIZE]
 

Functions

static void epoch2isotime (my_isotime_t timebuf, time_t atime)
 
static void get_current_isotime (my_isotime_t timebuf)
 
static int check_isotime (const my_isotime_t atime)
 
static int isotime_p (const char *string)
 
static int isotime_human_p (const char *string)
 
static int string2isotime (my_isotime_t atime, const char *string)
 
static int days_per_year (int y)
 
static int days_per_month (int y, int m)
 
static unsigned long date2jd (int year, int month, int day)
 
static int jd2date (unsigned long jd, int *year, int *month, int *day)
 
static int add_seconds_to_isotime (my_isotime_t atime, int nseconds)
 
static int add_days_to_isotime (my_isotime_t atime, int ndays)
 
static int add_years_to_isotime (my_isotime_t atime, int nyears)
 
tree_cellnasl_isotime_now (lex_ctxt *lexic)
 Return the current time in ISO format. More...
 
tree_cellnasl_isotime_is_valid (lex_ctxt *lexic)
 Check whether an ISO time string is valid. More...
 
tree_cellnasl_isotime_scan (lex_ctxt *lexic)
 Convert a string into an ISO time string. More...
 
tree_cellnasl_isotime_print (lex_ctxt *lexic)
 Convert an SIO time string into a better readable string. More...
 
tree_cellnasl_isotime_add (lex_ctxt *lexic)
 Add days or seconds to an ISO time string. More...
 

Detailed Description

Implementation of an API for ISOTIME values.

This file contains the implementation of the isotime_* NASL builtin functions.

Background:

Most 32 bit systems use a signed 32 bit time_t to represent the system time. The problem is that in 2038 this time type will overflow. However, we sometimes need to compute dates in the future; for example some certificates are (for whatever reasons) valid for 30 years. To solve this problem in a platform independent way, we represent the time as a string and provide functions to work with them. This is not an elegant solution, but all proposed new time APIs have never been implemented on main stream systems - we can't expect that this will happen any time soon.

Definition in file nasl_isotime.c.

Macro Definition Documentation

◆ atoi_1

#define atoi_1 (   p)    (*(p) - '0')

Definition at line 84 of file nasl_isotime.c.

◆ atoi_2

#define atoi_2 (   p)    ((atoi_1 (p) * 10) + atoi_1 ((p) + 1))

Definition at line 85 of file nasl_isotime.c.

◆ atoi_4

#define atoi_4 (   p)    ((atoi_2 (p) * 100) + atoi_2 ((p) + 2))

Definition at line 86 of file nasl_isotime.c.

◆ digitp

#define digitp (   p)    (*(p) >= '0' && *(p) <= '9')

Definition at line 81 of file nasl_isotime.c.

◆ DIM

#define DIM (   v)    (sizeof (v) / sizeof ((v)[0]))

Definition at line 67 of file nasl_isotime.c.

◆ DIMof

#define DIMof (   type,
  member 
)    DIM (((type *) 0)->member)

Definition at line 68 of file nasl_isotime.c.

◆ ISOTIME_SIZE

#define ISOTIME_SIZE   19

Definition at line 73 of file nasl_isotime.c.

◆ JD_DIFF

#define JD_DIFF   1721060L

Definition at line 77 of file nasl_isotime.c.

◆ spacep

#define spacep (   p)    (*(p) == ' ' || *(p) == '\t')

Definition at line 80 of file nasl_isotime.c.

Typedef Documentation

◆ my_isotime_t

typedef char my_isotime_t[ISOTIME_SIZE]

Definition at line 74 of file nasl_isotime.c.

Function Documentation

◆ add_days_to_isotime()

static int add_days_to_isotime ( my_isotime_t  atime,
int  ndays 
)
static

Definition at line 443 of file nasl_isotime.c.

444 {
445  int year, month, day, hour, minute, sec;
446  unsigned long jd;
447 
448  if (check_isotime (atime))
449  return 1;
450 
451  if (ndays < 0 || ndays >= 9999 * 366)
452  return 1;
453 
454  year = atoi_4 (atime + 0);
455  month = atoi_2 (atime + 4);
456  day = atoi_2 (atime + 6);
457  hour = atoi_2 (atime + 9);
458  minute = atoi_2 (atime + 11);
459  sec = atoi_2 (atime + 13);
460 
461  /* The julian date functions don't support this. */
462  if (year < 1582 || (year == 1582 && month < 10)
463  || (year == 1582 && month == 10 && day < 15))
464  return 1;
465 
466  jd = date2jd (year, month, day) + ndays;
467  jd2date (jd, &year, &month, &day);
468 
469  if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
470  return 1;
471 
472  snprintf (atime, ISOTIME_SIZE, "%04d%02d%02dT%02d%02d%02d", year, month, day,
473  hour, minute, sec);
474  return 0;
475 }

References atoi_2, atoi_4, check_isotime(), date2jd(), ISOTIME_SIZE, and jd2date().

Referenced by nasl_isotime_add().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_seconds_to_isotime()

static int add_seconds_to_isotime ( my_isotime_t  atime,
int  nseconds 
)
static

Definition at line 399 of file nasl_isotime.c.

400 {
401  int year, month, day, hour, minute, sec, ndays;
402  unsigned long jd;
403 
404  if (check_isotime (atime))
405  return 1;
406 
407  if (nseconds < 0 || nseconds >= (0x7fffffff - 61))
408  return 1;
409 
410  year = atoi_4 (atime + 0);
411  month = atoi_2 (atime + 4);
412  day = atoi_2 (atime + 6);
413  hour = atoi_2 (atime + 9);
414  minute = atoi_2 (atime + 11);
415  sec = atoi_2 (atime + 13);
416 
417  /* The julian date functions don't support this. */
418  if (year < 1582 || (year == 1582 && month < 10)
419  || (year == 1582 && month == 10 && day < 15))
420  return 1;
421 
422  sec += nseconds;
423  minute += sec / 60;
424  sec %= 60;
425  hour += minute / 60;
426  minute %= 60;
427  ndays = hour / 24;
428  hour %= 24;
429 
430  jd = date2jd (year, month, day) + ndays;
431  jd2date (jd, &year, &month, &day);
432 
433  if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
434  return 1;
435 
436  snprintf (atime, ISOTIME_SIZE, "%04d%02d%02dT%02d%02d%02d", year, month, day,
437  hour, minute, sec);
438  return 0;
439 }

References atoi_2, atoi_4, check_isotime(), date2jd(), ISOTIME_SIZE, and jd2date().

Referenced by nasl_isotime_add().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_years_to_isotime()

static int add_years_to_isotime ( my_isotime_t  atime,
int  nyears 
)
static

Definition at line 479 of file nasl_isotime.c.

480 {
481  int year, month, day, hour, minute, sec;
482  unsigned long jd;
483 
484  if (check_isotime (atime))
485  return 1;
486 
487  if (nyears < 0 || nyears >= 9999)
488  return 1;
489 
490  year = atoi_4 (atime + 0);
491  month = atoi_2 (atime + 4);
492  day = atoi_2 (atime + 6);
493  hour = atoi_2 (atime + 9);
494  minute = atoi_2 (atime + 11);
495  sec = atoi_2 (atime + 13);
496 
497  /* The julian date functions don't support this. */
498  if (year < 1582 || (year == 1582 && month < 10)
499  || (year == 1582 && month == 10 && day < 15))
500  return 1;
501 
502  jd = date2jd (year + nyears, month, day);
503  jd2date (jd, &year, &month, &day);
504 
505  if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
506  return 1;
507 
508  snprintf (atime, ISOTIME_SIZE, "%04d%02d%02dT%02d%02d%02d", year, month, day,
509  hour, minute, sec);
510  return 0;
511 }

References atoi_2, atoi_4, check_isotime(), date2jd(), ISOTIME_SIZE, and jd2date().

Referenced by nasl_isotime_add().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_isotime()

static int check_isotime ( const my_isotime_t  atime)
static

Definition at line 117 of file nasl_isotime.c.

118 {
119  int i;
120  const char *s;
121 
122  if (!*atime)
123  return 1;
124 
125  for (s = atime, i = 0; i < 8; i++, s++)
126  if (!digitp (s))
127  return 1;
128  if (*s != 'T')
129  return 1;
130  for (s++, i = 9; i < 15; i++, s++)
131  if (!digitp (s))
132  return 1;
133  return 0;
134 }

References digitp.

Referenced by add_days_to_isotime(), add_seconds_to_isotime(), add_years_to_isotime(), nasl_isotime_add(), and nasl_isotime_print().

Here is the caller graph for this function:

◆ date2jd()

static unsigned long date2jd ( int  year,
int  month,
int  day 
)
static

Definition at line 329 of file nasl_isotime.c.

330 {
331  unsigned long jd;
332 
333  jd = 365L * year + 31 * (month - 1) + day + JD_DIFF;
334  if (month < 3)
335  year--;
336  else
337  jd -= (4 * month + 23) / 10;
338 
339  jd += year / 4 - ((year / 100 + 1) * 3) / 4;
340 
341  return jd;
342 }

References JD_DIFF.

Referenced by add_days_to_isotime(), add_seconds_to_isotime(), add_years_to_isotime(), and jd2date().

Here is the caller graph for this function:

◆ days_per_month()

static int days_per_month ( int  y,
int  m 
)
static

Definition at line 296 of file nasl_isotime.c.

297 {
298  int s;
299 
300  switch (m)
301  {
302  case 1:
303  case 3:
304  case 5:
305  case 7:
306  case 8:
307  case 10:
308  case 12:
309  return 31;
310  case 2:
311  s = !(y % 4);
312  if (!(y % 100))
313  if ((y % 400))
314  s = 0;
315  return s ? 29 : 28;
316  case 4:
317  case 6:
318  case 9:
319  case 11:
320  return 30;
321  default:
322  abort ();
323  }
324 }

Referenced by jd2date().

Here is the caller graph for this function:

◆ days_per_year()

static int days_per_year ( int  y)
static

Definition at line 283 of file nasl_isotime.c.

284 {
285  int s;
286 
287  s = !(y % 4);
288  if (!(y % 100))
289  if ((y % 400))
290  s = 0;
291  return s ? 366 : 365;
292 }

Referenced by jd2date().

Here is the caller graph for this function:

◆ epoch2isotime()

static void epoch2isotime ( my_isotime_t  timebuf,
time_t  atime 
)
static

Definition at line 90 of file nasl_isotime.c.

91 {
92  if (atime == (time_t) (-1))
93  *timebuf = 0;
94  else
95  {
96  struct tm *tp;
97 
98  tp = gmtime (&atime);
99  snprintf (timebuf, ISOTIME_SIZE, "%04d%02d%02dT%02d%02d%02d",
100  1900 + tp->tm_year, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour,
101  tp->tm_min, tp->tm_sec);
102  }
103 }

References ISOTIME_SIZE.

Referenced by get_current_isotime().

Here is the caller graph for this function:

◆ get_current_isotime()

static void get_current_isotime ( my_isotime_t  timebuf)
static

Definition at line 107 of file nasl_isotime.c.

108 {
109  epoch2isotime (timebuf, time (NULL));
110 }

References epoch2isotime().

Referenced by nasl_isotime_now().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isotime_human_p()

static int isotime_human_p ( const char *  string)
static

Definition at line 167 of file nasl_isotime.c.

168 {
169  const char *s;
170  int i;
171 
172  if (!*string)
173  return 0;
174  for (s = string, i = 0; i < 4; i++, s++)
175  if (!digitp (s))
176  return 0;
177  if (*s != '-')
178  return 0;
179  s++;
180  if (!digitp (s) || !digitp (s + 1) || s[2] != '-')
181  return 0;
182  i = atoi_2 (s);
183  if (i < 1 || i > 12)
184  return 0;
185  s += 3;
186  if (!digitp (s) || !digitp (s + 1))
187  return 0;
188  i = atoi_2 (s);
189  if (i < 1 || i > 31)
190  return 0;
191  s += 2;
192  if (!*s || *s == ',')
193  return 1; /* Okay; only date given. */
194  if (!spacep (s))
195  return 0;
196  s++;
197  if (spacep (s))
198  return 1; /* Okay, second space stops scanning. */
199  if (!digitp (s) || !digitp (s + 1))
200  return 0;
201  i = atoi_2 (s);
202  if (i < 0 || i > 23)
203  return 0;
204  s += 2;
205  if (!*s || *s == ',')
206  return 1; /* Okay; only date and hour given. */
207  if (*s != ':')
208  return 0;
209  s++;
210  if (!digitp (s) || !digitp (s + 1))
211  return 0;
212  i = atoi_2 (s);
213  if (i < 0 || i > 59)
214  return 0;
215  s += 2;
216  if (!*s || *s == ',')
217  return 1; /* Okay; only date, hour and minute given. */
218  if (*s != ':')
219  return 0;
220  s++;
221  if (!digitp (s) || !digitp (s + 1))
222  return 0;
223  i = atoi_2 (s);
224  if (i < 0 || i > 60)
225  return 0;
226  s += 2;
227  if (!*s || *s == ',' || spacep (s))
228  return 1; /* Okay; date, hour and minute and second given. */
229 
230  return 0; /* Unexpected delimiter. */
231 }

References atoi_2, digitp, and spacep.

Referenced by nasl_isotime_is_valid(), and string2isotime().

Here is the caller graph for this function:

◆ isotime_p()

static int isotime_p ( const char *  string)
static

Definition at line 141 of file nasl_isotime.c.

142 {
143  const char *s;
144  int i;
145 
146  if (!*string)
147  return 0;
148  for (s = string, i = 0; i < 8; i++, s++)
149  if (!digitp (s))
150  return 0;
151  if (*s != 'T')
152  return 0;
153  for (s++, i = 9; i < 15; i++, s++)
154  if (!digitp (s))
155  return 0;
156  if (!(!*s || (isascii (*s) && isspace (*s)) || *s == ':' || *s == ','))
157  return 0; /* Wrong delimiter. */
158 
159  return 1;
160 }

References digitp.

Referenced by nasl_isotime_is_valid(), and string2isotime().

Here is the caller graph for this function:

◆ jd2date()

static int jd2date ( unsigned long  jd,
int *  year,
int *  month,
int *  day 
)
static

Definition at line 349 of file nasl_isotime.c.

350 {
351  int y, m, d;
352  long delta;
353 
354  if (!jd)
355  return 0;
356  if (jd < 1721425 || jd > 2843085)
357  return 0;
358 
359  y = (jd - JD_DIFF) / 366;
360  d = m = 1;
361 
362  while ((delta = jd - date2jd (y, m, d)) > days_per_year (y))
363  y++;
364 
365  m = (delta / 31) + 1;
366  while ((delta = jd - date2jd (y, m, d)) > days_per_month (y, m))
367  if (++m > 12)
368  {
369  m = 1;
370  y++;
371  }
372 
373  d = delta + 1;
374  if (d > days_per_month (y, m))
375  {
376  d = 1;
377  m++;
378  }
379  if (m > 12)
380  {
381  m = 1;
382  y++;
383  }
384 
385  if (year)
386  *year = y;
387  if (month)
388  *month = m;
389  if (day)
390  *day = d;
391 
392  return (jd - date2jd (y, 1, 1)) + 1;
393 }

References date2jd(), days_per_month(), days_per_year(), and JD_DIFF.

Referenced by add_days_to_isotime(), add_seconds_to_isotime(), and add_years_to_isotime().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_isotime_add()

tree_cell* nasl_isotime_add ( lex_ctxt lexic)

Add days or seconds to an ISO time string.

NASL Function: isotime_add\n

This function adds days or seconds to an ISO time string and returns the resulting time string. The number of days or seconds are given using the named parameters; if none are given nothing is added; if both are given both additions are performed. This function won't work for dates before the Gregorian calendar switch.

NASL Unnamed Parameters:\n
  • An ISO time string
NASL Named Parameters:\n
  • years An integer with the number of years to add to the timestamp.
  • days An integer with the number of days to add to the timestamp.
  • seconds An integer with the number of seconds to add to the timestamp.
NASL Returns:\n The resulting ISO time string or NULL if the provided ISO
time string is not valid or the result would overflow (i.e. year > 9999).
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

Definition at line 713 of file nasl_isotime.c.

714 {
715  tree_cell *retc;
716  my_isotime_t timebuf;
717  const char *string;
718  int nyears, ndays, nseconds;
719 
720  string = get_str_var_by_num (lexic, 0);
721  if (!string || get_var_size_by_num (lexic, 0) < ISOTIME_SIZE - 1
722  || check_isotime (string))
723  return NULL;
724  memcpy (timebuf, string, ISOTIME_SIZE - 1);
725  timebuf[ISOTIME_SIZE - 1] = 0;
726 
727  nyears = get_int_var_by_name (lexic, "years", 0);
728  ndays = get_int_var_by_name (lexic, "days", 0);
729  nseconds = get_int_var_by_name (lexic, "seconds", 0);
730 
731  if (nyears && add_years_to_isotime (timebuf, nyears))
732  return NULL;
733  if (ndays && add_days_to_isotime (timebuf, ndays))
734  return NULL;
735  if (nseconds && add_seconds_to_isotime (timebuf, nseconds))
736  return NULL;
737  /* If nothing was added, explicitly add 0 years. */
738  if (!nyears && !ndays && !nseconds && add_years_to_isotime (timebuf, 0))
739  return NULL;
740 
741  retc = alloc_typed_cell (CONST_STR);
742  retc->x.str_val = g_strdup (timebuf);
743  retc->size = strlen (timebuf);
744  return retc;
745 }

References add_days_to_isotime(), add_seconds_to_isotime(), add_years_to_isotime(), alloc_typed_cell(), check_isotime(), CONST_STR, get_int_var_by_name(), get_str_var_by_num(), get_var_size_by_num(), ISOTIME_SIZE, TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_isotime_is_valid()

tree_cell* nasl_isotime_is_valid ( lex_ctxt lexic)

Check whether an ISO time string is valid.

NASL Function: isotime_is_valid\n
NASL Unnamed Parameters:\n
  • A string. Both, the standard 15 byte string and the better human readable up to 19 byte format are accepted here. If a plain data type is is provided only the 15 byte format is accepted.
NASL Returns:\n True is this is an ISO string; false if not.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

Definition at line 561 of file nasl_isotime.c.

562 {
563  int result = 0;
564  tree_cell *retc;
565  my_isotime_t timebuf;
566  const char *string;
567  int datalen;
568 
569  string = get_str_var_by_num (lexic, 0);
570  if (string)
571  {
572  switch (get_var_type_by_num (lexic, 0))
573  {
574  case VAR2_DATA:
575  datalen = get_var_size_by_num (lexic, 0);
576  if (datalen < ISOTIME_SIZE - 1)
577  break; /* Too short */
578  memcpy (timebuf, string, ISOTIME_SIZE - 1);
579  timebuf[ISOTIME_SIZE - 1] = 0;
580  string = timebuf;
581  /* FALLTHRU */
582  case VAR2_STRING:
583  if (isotime_p (string) || isotime_human_p (string))
584  result = 1;
585  break;
586  default:
587  break;
588  }
589  }
590 
591  retc = alloc_typed_cell (CONST_INT);
592  retc->x.i_val = result;
593  return retc;
594 }

References alloc_typed_cell(), CONST_INT, get_str_var_by_num(), get_var_size_by_num(), get_var_type_by_num(), TC::i_val, isotime_human_p(), isotime_p(), ISOTIME_SIZE, VAR2_DATA, VAR2_STRING, and TC::x.

Here is the call graph for this function:

◆ nasl_isotime_now()

tree_cell* nasl_isotime_now ( lex_ctxt lexic)

Return the current time in ISO format.

NASL Function: isotime_now\n
NASL Unnamed Parameters:\n
  • None
NASL Returns:\n A string with the ISO time. If the current time is not
available an empty string is returned.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

Definition at line 529 of file nasl_isotime.c.

530 {
531  tree_cell *retc;
532  my_isotime_t timebuf;
533 
534  (void) lexic;
535  get_current_isotime (timebuf);
536 
537  retc = alloc_typed_cell (CONST_STR);
538  retc->x.str_val = g_strdup (timebuf);
539  retc->size = strlen (timebuf);
540  return retc;
541 }

References alloc_typed_cell(), CONST_STR, get_current_isotime(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_isotime_print()

tree_cell* nasl_isotime_print ( lex_ctxt lexic)

Convert an SIO time string into a better readable string.

NASL Function: isotime_print\n
NASL Unnamed Parameters:\n
  • An ISO time string.
NASL Returns:\n A string in the format "YYYY-MM-DD HH:MM:SS" or "[none]"
if the provided time string is not valid.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

Definition at line 663 of file nasl_isotime.c.

664 {
665  tree_cell *retc;
666  const char *string;
667  char helpbuf[20];
668 
669  string = get_str_var_by_num (lexic, 0);
670  if (!string || get_var_size_by_num (lexic, 0) < 15 || check_isotime (string))
671  strcpy (helpbuf, "[none]");
672  else
673  snprintf (helpbuf, sizeof helpbuf, "%.4s-%.2s-%.2s %.2s:%.2s:%.2s", string,
674  string + 4, string + 6, string + 9, string + 11, string + 13);
675  retc = alloc_typed_cell (CONST_STR);
676  retc->x.str_val = g_strdup (helpbuf);
677  retc->size = strlen (helpbuf);
678  return retc;
679 }

References alloc_typed_cell(), check_isotime(), CONST_STR, get_str_var_by_num(), get_var_size_by_num(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_isotime_scan()

tree_cell* nasl_isotime_scan ( lex_ctxt lexic)

Convert a string into an ISO time string.

NASL Function: isotime_scan\n
NASL Unnamed Parameters:\n
  • A string
NASL Returns:\n A ISO time string on success or NULL on error.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

Definition at line 612 of file nasl_isotime.c.

613 {
614  tree_cell *retc;
615  my_isotime_t timebuf;
616  int datalen;
617  const char *string;
618 
619  *timebuf = 0;
620  string = get_str_var_by_num (lexic, 0);
621  if (!string)
622  return NULL;
623  switch (get_var_type_by_num (lexic, 0))
624  {
625  case VAR2_DATA:
626  datalen = get_var_size_by_num (lexic, 0);
627  if (datalen < ISOTIME_SIZE - 1)
628  return NULL; /* Too short */
629  memcpy (timebuf, string, ISOTIME_SIZE - 1);
630  timebuf[ISOTIME_SIZE - 1] = 0;
631  string = timebuf;
632  /* FALLTHRU */
633  case VAR2_STRING:
634  if (!string2isotime (timebuf, string))
635  return NULL;
636  break;
637  default:
638  return NULL;
639  }
640 
641  retc = alloc_typed_cell (CONST_STR);
642  retc->x.str_val = g_strdup (timebuf);
643  retc->size = strlen (timebuf);
644  return retc;
645 }

References alloc_typed_cell(), CONST_STR, get_str_var_by_num(), get_var_size_by_num(), get_var_type_by_num(), ISOTIME_SIZE, TC::size, TC::str_val, string2isotime(), VAR2_DATA, VAR2_STRING, and TC::x.

Here is the call graph for this function:

◆ string2isotime()

static int string2isotime ( my_isotime_t  atime,
const char *  string 
)
static

Definition at line 238 of file nasl_isotime.c.

239 {
240  my_isotime_t dummyatime;
241 
242  if (!atime)
243  atime = dummyatime;
244 
245  memset (atime, '\0', sizeof (my_isotime_t));
246  atime[0] = 0;
247  if (isotime_p (string))
248  {
249  memcpy (atime, string, 15);
250  atime[15] = 0;
251  return 15;
252  }
253  if (!isotime_human_p (string))
254  return 0;
255  atime[0] = string[0];
256  atime[1] = string[1];
257  atime[2] = string[2];
258  atime[3] = string[3];
259  atime[4] = string[5];
260  atime[5] = string[6];
261  atime[6] = string[8];
262  atime[7] = string[9];
263  atime[8] = 'T';
264  if (!spacep (string + 10))
265  return 10;
266  if (spacep (string + 11))
267  return 11; /* As per def, second space stops scanning. */
268  atime[9] = string[11];
269  atime[10] = string[12];
270  if (string[13] != ':')
271  return 13;
272  atime[11] = string[14];
273  atime[12] = string[15];
274  if (string[16] != ':')
275  return 16;
276  atime[13] = string[17];
277  atime[14] = string[18];
278  return 19;
279 }

References isotime_human_p(), isotime_p(), and spacep.

Referenced by nasl_isotime_scan().

Here is the call graph for this function:
Here is the caller graph for this function:
date2jd
static unsigned long date2jd(int year, int month, int day)
Definition: nasl_isotime.c:329
TC::str_val
char * str_val
Definition: nasl_tree.h:112
days_per_month
static int days_per_month(int y, int m)
Definition: nasl_isotime.c:296
string2isotime
static int string2isotime(my_isotime_t atime, const char *string)
Definition: nasl_isotime.c:238
CONST_STR
@ CONST_STR
Definition: nasl_tree.h:91
isotime_p
static int isotime_p(const char *string)
Definition: nasl_isotime.c:141
check_isotime
static int check_isotime(const my_isotime_t atime)
Definition: nasl_isotime.c:117
TC::x
union TC::@2 x
my_isotime_t
char my_isotime_t[ISOTIME_SIZE]
Definition: nasl_isotime.c:74
add_days_to_isotime
static int add_days_to_isotime(my_isotime_t atime, int ndays)
Definition: nasl_isotime.c:443
TC::size
int size
Definition: nasl_tree.h:109
atoi_2
#define atoi_2(p)
Definition: nasl_isotime.c:85
JD_DIFF
#define JD_DIFF
Definition: nasl_isotime.c:77
get_int_var_by_name
long int get_int_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1113
VAR2_DATA
@ VAR2_DATA
Definition: nasl_var.h:29
ISOTIME_SIZE
#define ISOTIME_SIZE
Definition: nasl_isotime.c:73
add_seconds_to_isotime
static int add_seconds_to_isotime(my_isotime_t atime, int nseconds)
Definition: nasl_isotime.c:399
digitp
#define digitp(p)
Definition: nasl_isotime.c:81
get_str_var_by_num
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1120
atoi_4
#define atoi_4(p)
Definition: nasl_isotime.c:86
days_per_year
static int days_per_year(int y)
Definition: nasl_isotime.c:283
TC
Definition: nasl_tree.h:104
add_years_to_isotime
static int add_years_to_isotime(my_isotime_t atime, int nyears)
Definition: nasl_isotime.c:479
get_current_isotime
static void get_current_isotime(my_isotime_t timebuf)
Definition: nasl_isotime.c:107
epoch2isotime
static void epoch2isotime(my_isotime_t timebuf, time_t atime)
Definition: nasl_isotime.c:90
CONST_INT
@ CONST_INT
Definition: nasl_tree.h:90
get_var_size_by_num
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1154
isotime_human_p
static int isotime_human_p(const char *string)
Definition: nasl_isotime.c:167
get_var_type_by_num
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1164
jd2date
static int jd2date(unsigned long jd, int *year, int *month, int *day)
Definition: nasl_isotime.c:349
VAR2_STRING
@ VAR2_STRING
Definition: nasl_var.h:28
alloc_typed_cell
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:40
spacep
#define spacep(p)
Definition: nasl_isotime.c:80
TC::i_val
long int i_val
Definition: nasl_tree.h:113