OpenVAS Libraries  9.0.3
nasl_isotime.c File Reference

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

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "nasl_tree.h"
#include "nasl_global_ctxt.h"
#include "nasl_var.h"
#include "nasl_lex_ctxt.h"
#include "nasl_debug.h"
#include "nasl_isotime.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   16
 
#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

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 89 of file nasl_isotime.c.

◆ atoi_2

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

Definition at line 90 of file nasl_isotime.c.

◆ atoi_4

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

Definition at line 91 of file nasl_isotime.c.

◆ digitp

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

Definition at line 86 of file nasl_isotime.c.

◆ DIM

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

Definition at line 72 of file nasl_isotime.c.

◆ DIMof

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

Definition at line 73 of file nasl_isotime.c.

◆ ISOTIME_SIZE

#define ISOTIME_SIZE   16

Definition at line 78 of file nasl_isotime.c.

Referenced by nasl_isotime_add(), nasl_isotime_is_valid(), and nasl_isotime_scan().

◆ JD_DIFF

#define JD_DIFF   1721060L

Definition at line 82 of file nasl_isotime.c.

◆ spacep

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

Definition at line 85 of file nasl_isotime.c.

Typedef Documentation

◆ my_isotime_t

typedef char my_isotime_t[ISOTIME_SIZE]

Definition at line 79 of file nasl_isotime.c.

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

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:
  • An ISO time string
NASL Named Parameters:
  • 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: 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 776 of file nasl_isotime.c.

References get_str_var_by_num(), get_var_size_by_num(), and ISOTIME_SIZE.

777 {
778  tree_cell *retc;
779  my_isotime_t timebuf;
780  const char *string;
781  int nyears, ndays, nseconds;
782 
783  string = get_str_var_by_num (lexic, 0);
784  if (!string
785  || get_var_size_by_num (lexic, 0) < ISOTIME_SIZE -1
786  || check_isotime (string))
787  return NULL;
788  memcpy (timebuf, string, ISOTIME_SIZE -1);
789  timebuf[ISOTIME_SIZE - 1] = 0;
790 
791  nyears = get_int_local_var_by_name (lexic, "years", 0);
792  ndays = get_int_local_var_by_name (lexic, "days", 0);
793  nseconds = get_int_local_var_by_name (lexic, "seconds", 0);
794 
795  if (nyears && add_years_to_isotime (timebuf, nyears))
796  return NULL;
797  if (ndays && add_days_to_isotime (timebuf, ndays))
798  return NULL;
799  if (nseconds && add_seconds_to_isotime (timebuf, nseconds))
800  return NULL;
801  /* If nothing was added, explicitly add 0 years. */
802  if (!nyears && !ndays && !nseconds && add_years_to_isotime (timebuf, 0))
803  return NULL;
804 
805  retc = alloc_typed_cell (CONST_STR);
806  retc->x.str_val = g_strdup (timebuf);
807  retc->size = strlen (timebuf);
808  return retc;
809 }
char * str_val
Definition: nasl_tree.h:113
char my_isotime_t[ISOTIME_SIZE]
Definition: nasl_isotime.c:79
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
#define ISOTIME_SIZE
Definition: nasl_isotime.c:78
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
gchar * string
int size
Definition: nasl_tree.h:110
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
NASL Unnamed Parameters:
  • 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: 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 620 of file nasl_isotime.c.

References get_str_var_by_num(), get_var_size_by_num(), get_var_type_by_num(), ISOTIME_SIZE, VAR2_DATA, and VAR2_STRING.

621 {
622  int result = 0;
623  tree_cell *retc;
624  my_isotime_t timebuf;
625  const char *string;
626  int datalen;
627 
628  string = get_str_var_by_num (lexic, 0);
629  if (string)
630  {
631  switch (get_var_type_by_num (lexic, 0))
632  {
633  case VAR2_DATA:
634  datalen = get_var_size_by_num (lexic, 0);
635  if (datalen < ISOTIME_SIZE - 1)
636  break; /* Too short */
637  memcpy (timebuf, string, ISOTIME_SIZE - 1);
638  timebuf[ISOTIME_SIZE -1] = 0;
639  string = timebuf;
640  /* FALLTHRU */
641  case VAR2_STRING:
642  if (isotime_p (string) || isotime_human_p (string))
643  result = 1;
644  break;
645  default:
646  break;
647  }
648  }
649 
650  retc = alloc_typed_cell (CONST_INT);
651  retc->x.i_val = result;
652  return retc;
653 }
char my_isotime_t[ISOTIME_SIZE]
Definition: nasl_isotime.c:79
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
#define ISOTIME_SIZE
Definition: nasl_isotime.c:78
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
long int i_val
Definition: nasl_tree.h:114
gchar * string
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1315
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
NASL Unnamed Parameters:
  • None
NASL Returns: 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 588 of file nasl_isotime.c.

589 {
590  tree_cell *retc;
591  my_isotime_t timebuf;
592 
593  get_current_isotime (timebuf);
594 
595  retc = alloc_typed_cell (CONST_STR);
596  retc->x.str_val = g_strdup (timebuf);
597  retc->size = strlen (timebuf);
598  return retc;
599 }
char * str_val
Definition: nasl_tree.h:113
char my_isotime_t[ISOTIME_SIZE]
Definition: nasl_isotime.c:79
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
int size
Definition: nasl_tree.h:110

◆ 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
NASL Unnamed Parameters:
  • An ISO time string.
NASL Returns: 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 724 of file nasl_isotime.c.

References get_str_var_by_num(), and get_var_size_by_num().

725 {
726  tree_cell *retc;
727  const char *string;
728  char helpbuf[20];
729 
730  string = get_str_var_by_num (lexic, 0);
731  if (!string || get_var_size_by_num (lexic, 0) < 15 || check_isotime (string))
732  strcpy (helpbuf, "[none]");
733  else
734  snprintf (helpbuf, sizeof helpbuf,
735  "%.4s-%.2s-%.2s %.2s:%.2s:%.2s",
736  string, string+4, string+6, string+9, string+11, string+13);
737  retc = alloc_typed_cell (CONST_STR);
738  retc->x.str_val = g_strdup (helpbuf);
739  retc->size = strlen (helpbuf);
740  return retc;
741 }
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
gchar * string
int size
Definition: nasl_tree.h:110
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
NASL Unnamed Parameters:
  • A string
NASL Returns: 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 672 of file nasl_isotime.c.

References get_str_var_by_num(), get_var_size_by_num(), get_var_type_by_num(), ISOTIME_SIZE, VAR2_DATA, and VAR2_STRING.

673 {
674  tree_cell *retc;
675  my_isotime_t timebuf;
676  int datalen;
677  const char *string;
678 
679  *timebuf = 0;
680  string = get_str_var_by_num (lexic, 0);
681  if (!string)
682  return NULL;
683  switch (get_var_type_by_num (lexic, 0))
684  {
685  case VAR2_DATA:
686  datalen = get_var_size_by_num (lexic, 0);
687  if (datalen < ISOTIME_SIZE - 1)
688  return NULL; /* Too short */
689  memcpy (timebuf, string, ISOTIME_SIZE - 1);
690  timebuf[ISOTIME_SIZE - 1] = 0;
691  string = timebuf;
692  /* FALLTHRU */
693  case VAR2_STRING:
694  if (!string2isotime (timebuf, string))
695  return NULL;
696  break;
697  default:
698  return NULL;
699  }
700 
701  retc = alloc_typed_cell (CONST_STR);
702  retc->x.str_val = g_strdup (timebuf);
703  retc->size = strlen (timebuf);
704  return retc;
705 }
char * str_val
Definition: nasl_tree.h:113
char my_isotime_t[ISOTIME_SIZE]
Definition: nasl_isotime.c:79
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
#define ISOTIME_SIZE
Definition: nasl_isotime.c:78
Definition: nasl_tree.h:105
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1305
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
gchar * string
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1315
int size
Definition: nasl_tree.h:110
Here is the call graph for this function: