OpenVAS Scanner  7.0.0~git
nasl_isotime.h File Reference

Protos and data structures for ISOTIME functions used by NASL scripts. More...

#include "nasl_lex_ctxt.h"
Include dependency graph for nasl_isotime.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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

Protos and data structures for ISOTIME functions used by NASL scripts.

This file contains the protos for nasl_isotime.c

Definition in file nasl_isotime.h.

Function Documentation

◆ 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:
TC::str_val
char * str_val
Definition: nasl_tree.h:112
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
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
get_str_var_by_num
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1120
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
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
VAR2_STRING
@ VAR2_STRING
Definition: nasl_var.h:28
alloc_typed_cell
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:40
TC::i_val
long int i_val
Definition: nasl_tree.h:113