2007-06-11 13:01:04 +01:00
|
|
|
/*
|
|
|
|
* Scyther : An automatic verifier for security protocols.
|
2020-10-28 14:19:47 +00:00
|
|
|
* Copyright (C) 2007-2020 Cas Cremers
|
2007-06-11 13:01:04 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2004-07-29 13:36:24 +01:00
|
|
|
#include <stdarg.h>
|
2006-03-08 13:58:46 +00:00
|
|
|
#include <string.h>
|
2004-08-20 20:16:56 +01:00
|
|
|
#include <limits.h>
|
2004-04-23 11:58:43 +01:00
|
|
|
|
2004-07-24 20:07:29 +01:00
|
|
|
#include "symbol.h"
|
2004-08-20 20:16:56 +01:00
|
|
|
#include "debug.h"
|
2006-08-07 11:52:17 +01:00
|
|
|
#include "error.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Symbol processor.
|
|
|
|
|
|
|
|
Stores symbols for the lexical scanner. Can later print them.
|
|
|
|
Implementation uses a hashtable, the size of which is defined in
|
|
|
|
symbols.h.
|
|
|
|
*/
|
|
|
|
|
2004-07-29 13:36:24 +01:00
|
|
|
/* accessible for externals */
|
|
|
|
|
|
|
|
int globalError; //!< If >0, stdout output goes to stderr (for e.g. terms)
|
2005-12-27 10:49:22 +00:00
|
|
|
char *globalStream; //!< Defaults to stdout
|
2004-07-29 13:36:24 +01:00
|
|
|
|
|
|
|
/* external declarations */
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
extern int yylineno;
|
|
|
|
|
|
|
|
/* global declarations */
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Symbol hash table.
|
2004-04-23 11:58:43 +01:00
|
|
|
Symbol symbtab[HASHSIZE];
|
2004-05-15 16:45:08 +01:00
|
|
|
//! List of available (freed) symbol blocks.
|
2004-04-23 11:58:43 +01:00
|
|
|
Symbol symb_list;
|
2004-05-15 16:45:08 +01:00
|
|
|
//! List of all allocated symbol blocks.
|
2004-04-23 11:58:43 +01:00
|
|
|
Symbol symb_alloc;
|
|
|
|
|
|
|
|
/* main code */
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Open symbols code.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
symbolsInit (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < HASHSIZE; i++)
|
|
|
|
symbtab[i] = NULL;
|
|
|
|
symb_list = NULL;
|
|
|
|
symb_alloc = NULL;
|
2004-07-29 13:36:24 +01:00
|
|
|
globalError = 0;
|
2006-01-02 19:55:34 +00:00
|
|
|
globalStream = (char *) stdout;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Close symbols code.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
symbolsDone (void)
|
|
|
|
{
|
|
|
|
Symbol s;
|
|
|
|
|
|
|
|
while (symb_alloc != NULL)
|
|
|
|
{
|
|
|
|
s = symb_alloc;
|
|
|
|
symb_alloc = s->allocnext;
|
2006-03-08 13:58:46 +00:00
|
|
|
free (s);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Create a memory block for a symbol.
|
|
|
|
/**
|
|
|
|
* Internal memory management is used.
|
|
|
|
*@return A pointer to a memory block of size struct.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Symbol
|
|
|
|
get_symb (void)
|
|
|
|
{
|
|
|
|
Symbol t;
|
|
|
|
if (symb_list != NULL)
|
|
|
|
{
|
|
|
|
t = symb_list;
|
|
|
|
symb_list = symb_list->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-08 13:58:46 +00:00
|
|
|
t = (Symbol) malloc (sizeof (struct symbol));
|
2004-04-23 11:58:43 +01:00
|
|
|
t->allocnext = symb_alloc;
|
|
|
|
symb_alloc = t;
|
|
|
|
}
|
2004-08-20 20:16:56 +01:00
|
|
|
t->keylevel = INT_MAX;
|
2004-04-23 11:58:43 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Declare a symbol to be freed.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
2004-08-09 10:42:58 +01:00
|
|
|
free_symb (const Symbol s)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
s->next = symb_list;
|
|
|
|
symb_list = s;
|
|
|
|
}
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Return the index in the hash table for the string.
|
2004-04-23 11:58:43 +01:00
|
|
|
int
|
2004-08-09 10:42:58 +01:00
|
|
|
hash (const char *s)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
int hv = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; s[i] != EOS; i++)
|
|
|
|
{
|
|
|
|
int v = (hv >> 28) ^ (s[i] & 0xf);
|
|
|
|
hv = (hv << 4) | v;
|
|
|
|
}
|
|
|
|
hv = hv & 0x7fffffff;
|
|
|
|
return hv % HASHSIZE;
|
|
|
|
}
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Insert a string into the hash table.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
2004-08-09 10:42:58 +01:00
|
|
|
insert (const Symbol s)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-05-26 13:17:09 +01:00
|
|
|
int hv;
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
if (s == NULL)
|
|
|
|
return; /* illegal insertion of empty stuff */
|
|
|
|
|
2004-05-26 13:17:09 +01:00
|
|
|
hv = hash (s->text);
|
2004-04-23 11:58:43 +01:00
|
|
|
s->next = symbtab[hv];
|
|
|
|
symbtab[hv] = s;
|
|
|
|
}
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Find a string in the hash table.
|
2004-04-23 11:58:43 +01:00
|
|
|
Symbol
|
2004-08-09 10:42:58 +01:00
|
|
|
lookup (const char *s)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-05-26 13:17:09 +01:00
|
|
|
int hv;
|
|
|
|
Symbol t;
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
if (s == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2004-05-26 13:17:09 +01:00
|
|
|
hv = hash (s);
|
|
|
|
t = symbtab[hv];
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
while (t != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp (t->text, s) == 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
t = t->next;
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Print a symbol.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
2004-08-09 10:42:58 +01:00
|
|
|
symbolPrint (const Symbol s)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* TODO maybe action depending on type? */
|
2004-07-29 13:36:24 +01:00
|
|
|
eprintf ("%s", s->text);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-08-09 10:42:58 +01:00
|
|
|
//! Print all symbols
|
|
|
|
void
|
|
|
|
symbolPrintAll (void)
|
|
|
|
{
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
eprintf ("List of all symbols\n");
|
|
|
|
count = 0;
|
|
|
|
for (i = 0; i < HASHSIZE; i++)
|
|
|
|
{
|
|
|
|
Symbol sym;
|
|
|
|
|
|
|
|
sym = symbtab[i];
|
|
|
|
if (sym != NULL)
|
|
|
|
{
|
|
|
|
eprintf ("H%i:\t", i);
|
|
|
|
while (sym != NULL)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
eprintf ("[%s]\t", sym->text);
|
|
|
|
sym = sym->next;
|
|
|
|
}
|
|
|
|
eprintf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eprintf ("Total:\t%i\n", count);
|
|
|
|
}
|
|
|
|
|
2004-05-15 16:45:08 +01:00
|
|
|
//! Insert a string into the symbol table, if it wasn't there yet.
|
|
|
|
/**
|
|
|
|
* Also sets line numbers and type.
|
|
|
|
*\sa T_SYSCONST
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Symbol
|
2004-08-09 10:42:58 +01:00
|
|
|
symbolSysConst (const char *str)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
Symbol symb;
|
|
|
|
|
|
|
|
symb = lookup (str);
|
|
|
|
if (symb == NULL)
|
|
|
|
{
|
|
|
|
symb = get_symb ();
|
|
|
|
symb->lineno = yylineno;
|
|
|
|
symb->type = T_SYSCONST;
|
|
|
|
symb->text = str;
|
2004-08-09 10:42:58 +01:00
|
|
|
insert (symb);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
return symb;
|
|
|
|
}
|
2004-07-29 13:36:24 +01:00
|
|
|
|
2005-12-28 11:50:17 +00:00
|
|
|
//! Generate the first fresh free number symbol, prefixed by a certain symbol's string.
|
|
|
|
/**
|
|
|
|
* Note that there is an upper limit to this, to avoid some problems with buffer overflows etc.
|
|
|
|
*/
|
|
|
|
Symbol
|
|
|
|
symbolNextFree (Symbol prefixsymbol)
|
|
|
|
{
|
|
|
|
char *prefixstr;
|
|
|
|
int n;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (prefixsymbol != NULL)
|
|
|
|
{
|
|
|
|
prefixstr = (char *) prefixsymbol->text;
|
2006-03-08 13:58:46 +00:00
|
|
|
len = strlen (prefixstr);
|
2005-12-28 11:50:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prefixstr = "";
|
2006-03-08 13:58:46 +00:00
|
|
|
len = 0;
|
2005-12-28 11:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n = 1;
|
|
|
|
while (n <= 9999)
|
|
|
|
{
|
2007-01-06 18:01:36 +00:00
|
|
|
/*
|
|
|
|
* The construction below (variable buffer length) is not allowed in ISO C90
|
|
|
|
*/
|
2005-12-28 11:50:17 +00:00
|
|
|
char buffer[len + 5]; // thus we must enforce a maximum of 9.999 (allowing for storage of \0 )
|
|
|
|
Symbol symb;
|
|
|
|
int slen;
|
|
|
|
|
|
|
|
slen = sprintf (buffer, "%s%i", prefixstr, n);
|
2006-05-16 16:00:21 +01:00
|
|
|
buffer[slen] = EOS;
|
2005-12-28 11:50:17 +00:00
|
|
|
symb = lookup (buffer);
|
|
|
|
if (symb == NULL)
|
|
|
|
{
|
|
|
|
char *newstring;
|
|
|
|
// Copy the buffer to something that will survive
|
|
|
|
/**
|
|
|
|
* Memory leak: although this routine should not be called recursively, it will never de-allocate this memory.
|
|
|
|
* Thus, some precaution is necessary.
|
|
|
|
* [x][CC]
|
|
|
|
*/
|
2006-03-08 13:58:46 +00:00
|
|
|
newstring = (char *) malloc (slen + 1);
|
2005-12-28 11:50:17 +00:00
|
|
|
memcpy (newstring, buffer, slen + 1);
|
|
|
|
|
|
|
|
/* This persistent string can be used to return a fresh symbol */
|
|
|
|
|
|
|
|
return symbolSysConst (newstring);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try next one
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
error ("We ran out of numbers (%i) when trying to generate a fresh symbol.",
|
|
|
|
n);
|
2007-01-06 14:45:29 +00:00
|
|
|
return NULL;
|
2005-12-28 11:50:17 +00:00
|
|
|
}
|
|
|
|
|
2011-01-27 13:12:51 +00:00
|
|
|
//! Return symbol according to integer
|
|
|
|
Symbol
|
|
|
|
symbolFromInt (int n, Symbol prefixsymbol)
|
|
|
|
{
|
|
|
|
char *prefixstr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!(n <= 9999))
|
|
|
|
{
|
|
|
|
error ("Can only make symbol from int when smaller than 10000");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefixsymbol != NULL)
|
|
|
|
{
|
|
|
|
prefixstr = (char *) prefixsymbol->text;
|
|
|
|
len = strlen (prefixstr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prefixstr = "";
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The construction below (variable buffer length) is not allowed in ISO C90
|
|
|
|
*/
|
|
|
|
char buffer[len + 5]; // thus we must enforce a maximum of 9.999 (allowing for storage of \0 )
|
|
|
|
Symbol symb;
|
|
|
|
int slen;
|
|
|
|
|
|
|
|
slen = sprintf (buffer, "%s%i", prefixstr, n);
|
|
|
|
buffer[slen] = EOS;
|
|
|
|
symb = lookup (buffer);
|
|
|
|
if (symb == NULL)
|
|
|
|
{
|
|
|
|
char *newstring;
|
|
|
|
// Copy the buffer to something that will survive
|
|
|
|
/**
|
|
|
|
* Memory leak: although this routine should not be called recursively, it will never de-allocate this memory.
|
|
|
|
* Thus, some precaution is necessary.
|
|
|
|
* [x][CC]
|
|
|
|
*/
|
|
|
|
newstring = (char *) malloc (slen + 1);
|
|
|
|
memcpy (newstring, buffer, slen + 1);
|
|
|
|
|
|
|
|
/* This persistent string can be used to return a fresh symbol */
|
|
|
|
|
|
|
|
symb = symbolSysConst (newstring);
|
|
|
|
}
|
|
|
|
|
|
|
|
return symb;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-08-20 20:16:56 +01:00
|
|
|
//! Fix all the unset keylevels
|
|
|
|
void
|
|
|
|
symbol_fix_keylevels (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < HASHSIZE; i++)
|
|
|
|
{
|
|
|
|
Symbol sym;
|
|
|
|
|
|
|
|
sym = symbtab[i];
|
|
|
|
while (sym != NULL)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (DEBUGL (5))
|
|
|
|
{
|
|
|
|
eprintf ("Symbol ");
|
|
|
|
symbolPrint (sym);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (sym->keylevel == INT_MAX)
|
|
|
|
{
|
|
|
|
// Nothing currently, this simply does not originate on a strand.
|
2004-08-31 13:35:05 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (DEBUGL (5))
|
|
|
|
{
|
|
|
|
eprintf (" doesn't have a keylevel yet.\n");
|
|
|
|
}
|
|
|
|
#endif
|
2004-08-20 20:16:56 +01:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (DEBUGL (5))
|
|
|
|
{
|
|
|
|
eprintf (" has keylevel %i\n", sym->keylevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
sym = sym->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-30 12:03:19 +00:00
|
|
|
//! Get output stream pointer
|
|
|
|
FILE *
|
|
|
|
getOutputStream (void)
|
|
|
|
{
|
|
|
|
if (globalError == 0)
|
|
|
|
return (FILE *) globalStream;
|
|
|
|
else
|
2006-08-06 19:01:23 +01:00
|
|
|
#ifdef USESTDERR
|
2005-12-30 12:03:19 +00:00
|
|
|
return stderr;
|
2006-08-02 11:29:40 +01:00
|
|
|
#else
|
2006-08-06 19:01:23 +01:00
|
|
|
// we simply omit it
|
2006-08-02 11:29:40 +01:00
|
|
|
return NULL;
|
|
|
|
#endif
|
2005-12-30 12:03:19 +00:00
|
|
|
}
|
|
|
|
|
2004-07-29 13:36:24 +01:00
|
|
|
//! Print out according to globalError
|
|
|
|
/**
|
2005-12-27 10:49:22 +00:00
|
|
|
* Input is comparable to printf, only depends on globalError. This should be
|
|
|
|
* used by any function trying to do output.
|
|
|
|
*
|
|
|
|
* Furthermore, if globalError == 0, it can still be overriden by
|
|
|
|
* globalStream, which can be another stream pointer. If it is null, stdout
|
|
|
|
* is assumed.
|
|
|
|
*
|
2004-07-29 13:36:24 +01:00
|
|
|
*\sa globalError
|
|
|
|
*/
|
|
|
|
void
|
2004-08-09 11:05:58 +01:00
|
|
|
eprintf (char *fmt, ...)
|
2004-07-29 13:36:24 +01:00
|
|
|
{
|
|
|
|
va_list args;
|
2006-08-02 11:29:40 +01:00
|
|
|
FILE *stream;
|
2004-07-29 13:36:24 +01:00
|
|
|
|
|
|
|
va_start (args, fmt);
|
2006-08-02 11:29:40 +01:00
|
|
|
stream = getOutputStream ();
|
|
|
|
if (stream != NULL)
|
|
|
|
{
|
|
|
|
vfprintf (stream, fmt, args);
|
|
|
|
}
|
2004-07-29 13:36:24 +01:00
|
|
|
va_end (args);
|
|
|
|
}
|
2005-12-30 12:03:19 +00:00
|
|
|
|
|
|
|
// Variable list variant
|
|
|
|
void
|
|
|
|
veprintf (const char *fmt, va_list args)
|
|
|
|
{
|
2006-08-02 11:29:40 +01:00
|
|
|
FILE *stream;
|
|
|
|
|
|
|
|
stream = getOutputStream ();
|
|
|
|
if (stream != NULL)
|
|
|
|
{
|
|
|
|
vfprintf (stream, fmt, args);
|
|
|
|
}
|
2005-12-30 12:03:19 +00:00
|
|
|
}
|