/**
 * @brief Convert a string into an ISO time string.
 * @naslfn{isotime_scan}
 *
 *
 * @nasluparam
 *
 * - A string
 *
 * @naslret A ISO time string on success or NULL on error.
 *
 * @param[in] lexic  Lexical context of the NASL interpreter.
 *
 * @return A tree cell.
 */
tree_cell *
nasl_isotime_scan (lex_ctxt *lexic)
{
  tree_cell *retc;
  my_isotime_t timebuf;
  int datalen;
  const char *string;

  *timebuf = 0;
  string = get_str_var_by_num (lexic, 0);
  if (!string)
    return NULL;
  switch (get_var_type_by_num (lexic, 0))
    {
    case VAR2_DATA:
      datalen = get_var_size_by_num (lexic, 0);
      if (datalen < ISOTIME_SIZE - 1)
        return NULL; /* Too short */
      memcpy (timebuf, string, ISOTIME_SIZE - 1);
      timebuf[ISOTIME_SIZE - 1] = 0;
      string = timebuf;
      /* FALLTHRU */
    case VAR2_STRING:
      if (!string2isotime (timebuf, string))
        return NULL;
      break;
    default:
      return NULL;
    }

  retc = alloc_typed_cell (CONST_STR);
  retc->x.str_val = estrdup (timebuf);
  retc->size = strlen (timebuf);
  return retc;
}
示例#2
0
static void
test_string2isotime (void)
{
  struct {
    const char *string;
    size_t result;
    const char *expected;
  } array [] = {
    { "19700101T000001",      15, "19700101T000001" },
    { "19700101T235959",      15, "19700101T235959" },
    { "19980815T143712",      15, "19980815T143712" },
    { "19700101T000000",      15, "19700101T000000" },
    { "19691231T235959",      15, "19691231T235959" },
    { "19000101T000000",      15, "19000101T000000" },
    { "",                      0, ""                },
    { "19000101T00000",        0, ""                },
    { "20010101t123456",       0, ""                },
    { "20010101T123456",      15, "20010101T123456" },
    { "20070629T160000",      15, "20070629T160000" },
    { "20070629T160000:",     15, "20070629T160000" },
    { "20070629T160000,",     15, "20070629T160000" },
    { "20070629T160000 ",     15, "20070629T160000" },
    { "20070629T160000\n",    15,"20070629T160000"  },
    { "20070629T160000.",      0, ""                },
    { "1066-03-20",           10, "10660320T000000" },
    { "1066-03-20,",          10, "10660320T000000" },
    { "1066-03-20:",           0, ""                },
    { "1066-03-20 00",        13, "10660320T000000" },
    { "1066-03-20 01",        13, "10660320T010000" },
    { "1066-03-20 23",        13, "10660320T230000" },
    { "1066-03-20 24",         0, ""                },
    { "1066-03-20 00:",        0, ""                },
    { "1066-03-20 00:3",       0, ""                },
    { "1066-03-20 00:31",     16, "10660320T003100" },
    { "1066-03-20 00:31:47",  19, "10660320T003147" },
    { "1066-03-20 00:31:47 ", 19, "10660320T003147" },
    { "1066-03-20 00:31:47,", 19, "10660320T003147" },
    { "1066-03-20 00:31:47:",  0, ""                },
    { "1-03-20 00:31:47:",     0, ""                },
    { "10-03-20 00:31:47:",    0, ""                },
    { "106-03-20 00:31:47:",   0, ""                },
    { "1066-23-20 00:31:47:",  0, ""                },
    { "1066-00-20 00:31:47:",  0, ""                },
    { "1066-0-20 00:31:47:",   0, ""                },
    { "1066-01-2 00:31:47:",   0, ""                },
    { "1066-01-2  00:31:47:",  0, ""                },
    { "1066-01-32 00:31:47:",  0, ""                },
    { "1066-01-00 00:31:47:",  0, ""                },
    { "1066-03-20  00:31:47:",11, "10660320T000000" },
    { "1066-03-2000:31:47:",   0, ""                },
    { "10666-03-20 00:31:47:", 0, ""                },
    { NULL, 0 }
  };
  int idx;
  size_t result;
  gnupg_isotime_t tbuf;

  for (idx=0; array[idx].string; idx++)
    {
      result = string2isotime (tbuf, array[idx].string);
      if (result != array[idx].result)
        {
          fail (idx);
          if (verbose)
            fprintf (stderr, "string `%s' expected: %d, got: %d\n",
                     array[idx].string, (int)array[idx].result, (int)result);
        }
      else if (result && strlen (tbuf) != 15)
        {
          fail (idx);
          if (verbose)
            fprintf (stderr, "string `%s' invalid isotime returned\n",
                     array[idx].string);
        }
      else if (result && strcmp (array[idx].expected, tbuf))
        {
          fail (idx);
          if (verbose)
            fprintf (stderr, "string `%s' bad isotime '%s' returned\n",
                     array[idx].string, tbuf);
        }
    }
}