<commit_before>/* $Id$ */
/*****************************************************************************
File:     asn_pproc.c
Contents: Functions to pre-process ASN.1 files as part of the ASN_GEN
          program.
System:   ASN development.
Created:
Author:   <NAME> <<EMAIL>>

Remarks:

*****************************************************************************/

const char asn_pproc_rcsid[]="$Header: /nfs/sub-rosa/u1/IOS_Project/ASN/Dev/rcs/cmd/asn_gen/asn_pproc.c,v 1.1 1995/01/11 22:43:11 jlowry Exp gardiner $";
char asn_pproc_id[] = "@(#)asn_pproc.c 828P";

#include "asn_gen.h"
#ifdef WIN32
#include <io.h>
#endif

/**
                   Design Notes on the Pre-processor

    For the 1988 version of ASN.1, this module unraveled the nested definitions,
such as:

    XXX ::= SEQUENCE {
	a   SET {
	    b   INTEGER,
	    c   OCTET STRING },
	d   BOOLEAN }

by use of recursive calls to pre_proc().  It also generated the necessary
synthetic names and implemented the ENCRYPTED and SIGNED macros and handled all
imports to produce a single file for the later stages to process.

    For the 1994 additions, the pre-processor was greatly extended to transform
the CLASS notation etc. into the previous TABLE style.  This occurs in several
places.  Consider the built-in definition

    TYPE_IDENTIFIER ::= CLASS {
        &id OBJECT IDENTIFIER UNIQUE,
        &Type }
    WITH SYNTAX { &Type IDENTIFIED BY &id }

When the reserved word CLASS in encountered in the definition state
(function pre_proc_def()), the function add_class_def(), q.v., is called to
process everything up through the final right brace and to set the state to
GLOBAL again.  (The pre-processor and the get_token function were also modified
to do built-in definitions.)

    Next, when a reference to the class is found, e.g.

    ALGORITHM ::= TYPE-IDENTIFIER

by the function find_class_entry() in pre_proc_def(), the function
add_class_member() is called to add this member in such a way that it will
inherit the syntax originally defined.

    When a definition is encountered like

    AlgorithmIdentifier ::= SEQUENCE {
        algorithm   ALGORITHM.&id ( {SupportedAlgorithms} ),
        parameters  ALGORITHM.&Type ( {SupportedAlgorithms } {@algorithm} )
                OPTIONAL }

the function find_class_entry() in pre_proc_item(), the function fill_id_type()
processes the entire item through the final right parenthesis

    At a definition of object identifiers like

    SupportedAlgorithms ALGORITHM ::= {dsa | xxx | rsa-signature }

pre_proc_glob() uses find_class_entry() to detect the class, and fills in the
name.  When the ::= is encountered, pre_proc_glob, noting that the classname is
capitalized, calls collect_ids() to process everything up through the final
right brace.

    At a definition of an instance of the class like:

    dsa ALGORITHM ::= { DSAParameters IDENTIFIED BY id-dsa }

pre_proc_glob(), noting that the classname is NOT capitalized, calls
class_instance() to process everything through the final right brace.

    For "parameterized" items (i.e. macros) like:

    DirectoryString{INTEGER:maxSIZE} ::= CHOICE {
        teletexString   TeletexString(SIZE(1..maxSIZE)),
        printableString PrintableString(SIZE(1..maxSIZE)) }

pre_proc_glob, triggered by the left brace, calls add_macro() to process
everything through the final right brace.  Then when the macro is invoked, as in

    commonName ATTRIBUTE ::=  {
        SUBTYPE OF      name
        WITH SYNTAX     DirectoryString { ub-common-name }
        ID              { id-at-commonName } }

the class_instance() function, noting the left parenthesis in a Type-refernce,
calls do_macro() to perform the substitutions.
**/

struct import_table *imtbp;
               /* pointer to entry for imported file currently being read */

static int num_imports;
static int append_default(int, char *, char *),
    append_token(char *, char *, char *),
    pre_proc_get_token(int, FILE *, char *),
    pre_proc_glob(int, FILE *, char *, char **, char *, int *),
    pre_proc_def(int, FILE *, char *, char **, char *, int *),
    pre_proc_item(int, FILE *, char *, char **, char *, int *, int),
    recurs(int fd, FILE *str, char *newclass, int newstate, int in_sub);

FILE *make_substr(char *);

void empty_substr(FILE *, FILE *, char *, int, char *);

static void glob_type(int, long, int),
    print_signed(FILE *, char *, char *),
    putout(FILE*, char *),
    scan_modules(int), skip_to(int, int, int);

static char table[128], topclass[128],
    tobesigned[] = "    toBeSigned  %sToBeSigned,\n",
    tabledef[] =    /* used in SIGNED SEQUENCE TABLE ... */
"%sAlgorithmIdentifier ::= SEQUENCE {\n\
    algorithm OBJECT IDENTIFIER TABLE %s,\n\
    parameters ANY DEFINED BY algorithm OPTIONAL}\n\n",
    signed_def[] = "%s ::= %s %s",
    thatword[] = "    toBeSigned  %s,\n",
    algsig[] = 
"    algorithm  %sAlgorithmIdentifier,\n\
    signature BIT STRING }\n\n",
    instance_of[] =
"%s ::= INSTANCE OF {\n\
    type_id OBJECT IDENTIFIER TABLE %s,\n\
    value   ANY DEFINED BY type_id }\n\n",
    sub_opener[] = "%s ::= %s {\n";

void pre_proc(int fd, FILE *str, int in_sub)
{
/**
Function: Preprocesses file fd to translate SIGNED macro and produce file str
Inputs: fd is file descriptor for ASN.1 file.  IF this is > zero, we are in
	    an imported file
	str is the stream descriptor for the output
	in_sub is used for recursiveness
Outputs: ASN.1 code written to 'str'
Procedure:
0. Scan for modules
1. Starting with lots of things cleared, DO forever
        IF no token AND no more tokens, break out of DO
        Switch on state
2.    Case GLOBAL
	IF processing global state returns GLOBAL state, return
      Case IN_DEFINITION
	    IF processing the definition returns GLOBAL state
		Clean up a few variables
      Case IN_ITEM
	    IF in a sub-item AND pre-processing the items returns GLOBAL
		state, return
**/
char linebuf[20*ASN_BSIZE], *linend = linebuf, *elinebuf = &linebuf[sizeof(linebuf)];
int signflag,
    active;   /* -1= in main file,
                  0= in imported file but not imported class
		  1= in imported file in an imported class, no details needed
		  2=  "   "        "  "   "    "      " , but details needed */
ulong loctag;
if (!in_sub) *classname = *token = 0;
if (fd >= 0 && !in_sub && !real_start) scan_modules(fd);
if (fd <= 0) active = -1;
else active = 0;
for (signflag = loctag = 0,
    *linebuf = *itemname = 0; pre_proc_get_token(fd, str, linebuf); )
    {
    switch (state)
    	{
    case PRE_GLOBAL:
	if (*token == ':') syntax(definitions_w);
	if (!strcmp(token, definitions_w)) state = GLOBAL;
	else *token = 0;   /* discard up to DEFINITIONS */
	break;

    case GLOBAL:			 
	state = pre_proc_glob(fd, str, linebuf, &linend, elinebuf, &active);
	break;

    case IN_DEFINITION:                                     /* got ::= */
	if ((state = pre_proc_def(fd, str, linebuf, &linend, elinebuf,
            &active)) == GLOBAL)
	    {
            active = (fd <= 0)? -1: 0;
            *subclass = *classname = 0;
	    if (fd >= 0) fflush(str);
	    if (in_sub) return;
	    }
	else if (state == IN_DEFINITION)
	    {<commit_msg>remove another unused variable<commit_after>/* $Id$ */
/*****************************************************************************
File:     asn_pproc.c
Contents: Functions to pre-process ASN.1 files as part of the ASN_GEN
          program.
System:   ASN development.
Created:
Author:   <NAME> <<EMAIL>>

Remarks:

*****************************************************************************/

const char asn_pproc_rcsid[]="$Header: /nfs/sub-rosa/u1/IOS_Project/ASN/Dev/rcs/cmd/asn_gen/asn_pproc.c,v 1.1 1995/01/11 22:43:11 jlowry Exp gardiner $";
char asn_pproc_id[] = "@(#)asn_pproc.c 828P";

#include "asn_gen.h"
#ifdef WIN32
#include <io.h>
#endif

/**
                   Design Notes on the Pre-processor

    For the 1988 version of ASN.1, this module unraveled the nested definitions,
such as:

    XXX ::= SEQUENCE {
	a   SET {
	    b   INTEGER,
	    c   OCTET STRING },
	d   BOOLEAN }

by use of recursive calls to pre_proc().  It also generated the necessary
synthetic names and implemented the ENCRYPTED and SIGNED macros and handled all
imports to produce a single file for the later stages to process.

    For the 1994 additions, the pre-processor was greatly extended to transform
the CLASS notation etc. into the previous TABLE style.  This occurs in several
places.  Consider the built-in definition

    TYPE_IDENTIFIER ::= CLASS {
        &id OBJECT IDENTIFIER UNIQUE,
        &Type }
    WITH SYNTAX { &Type IDENTIFIED BY &id }

When the reserved word CLASS in encountered in the definition state
(function pre_proc_def()), the function add_class_def(), q.v., is called to
process everything up through the final right brace and to set the state to
GLOBAL again.  (The pre-processor and the get_token function were also modified
to do built-in definitions.)

    Next, when a reference to the class is found, e.g.

    ALGORITHM ::= TYPE-IDENTIFIER

by the function find_class_entry() in pre_proc_def(), the function
add_class_member() is called to add this member in such a way that it will
inherit the syntax originally defined.

    When a definition is encountered like

    AlgorithmIdentifier ::= SEQUENCE {
        algorithm   ALGORITHM.&id ( {SupportedAlgorithms} ),
        parameters  ALGORITHM.&Type ( {SupportedAlgorithms } {@algorithm} )
                OPTIONAL }

the function find_class_entry() in pre_proc_item(), the function fill_id_type()
processes the entire item through the final right parenthesis

    At a definition of object identifiers like

    SupportedAlgorithms ALGORITHM ::= {dsa | xxx | rsa-signature }

pre_proc_glob() uses find_class_entry() to detect the class, and fills in the
name.  When the ::= is encountered, pre_proc_glob, noting that the classname is
capitalized, calls collect_ids() to process everything up through the final
right brace.

    At a definition of an instance of the class like:

    dsa ALGORITHM ::= { DSAParameters IDENTIFIED BY id-dsa }

pre_proc_glob(), noting that the classname is NOT capitalized, calls
class_instance() to process everything through the final right brace.

    For "parameterized" items (i.e. macros) like:

    DirectoryString{INTEGER:maxSIZE} ::= CHOICE {
        teletexString   TeletexString(SIZE(1..maxSIZE)),
        printableString PrintableString(SIZE(1..maxSIZE)) }

pre_proc_glob, triggered by the left brace, calls add_macro() to process
everything through the final right brace.  Then when the macro is invoked, as in

    commonName ATTRIBUTE ::=  {
        SUBTYPE OF      name
        WITH SYNTAX     DirectoryString { ub-common-name }
        ID              { id-at-commonName } }

the class_instance() function, noting the left parenthesis in a Type-refernce,
calls do_macro() to perform the substitutions.
**/

struct import_table *imtbp;
               /* pointer to entry for imported file currently being read */

static int num_imports;
static int append_default(int, char *, char *),
    append_token(char *, char *, char *),
    pre_proc_get_token(int, FILE *, char *),
    pre_proc_glob(int, FILE *, char *, char **, char *, int *),
    pre_proc_def(int, FILE *, char *, char **, char *, int *),
    pre_proc_item(int, FILE *, char *, char **, char *, int *, int),
    recurs(int fd, FILE *str, char *newclass, int newstate, int in_sub);

FILE *make_substr(char *);

void empty_substr(FILE *, FILE *, char *, int, char *);

static void glob_type(int, long, int),
    print_signed(FILE *, char *, char *),
    putout(FILE*, char *),
    scan_modules(int), skip_to(int, int, int);

static char table[128], topclass[128],
    tobesigned[] = "    toBeSigned  %sToBeSigned,\n",
    tabledef[] =    /* used in SIGNED SEQUENCE TABLE ... */
"%sAlgorithmIdentifier ::= SEQUENCE {\n\
    algorithm OBJECT IDENTIFIER TABLE %s,\n\
    parameters ANY DEFINED BY algorithm OPTIONAL}\n\n",
    signed_def[] = "%s ::= %s %s",
    thatword[] = "    toBeSigned  %s,\n",
    algsig[] = 
"    algorithm  %sAlgorithmIdentifier,\n\
    signature BIT STRING }\n\n",
    instance_of[] =
"%s ::= INSTANCE OF {\n\
    type_id OBJECT IDENTIFIER TABLE %s,\n\
    value   ANY DEFINED BY type_id }\n\n",
    sub_opener[] = "%s ::= %s {\n";

void pre_proc(int fd, FILE *str, int in_sub)
{
/**
Function: Preprocesses file fd to translate SIGNED macro and produce file str
Inputs: fd is file descriptor for ASN.1 file.  IF this is > zero, we are in
	    an imported file
	str is the stream descriptor for the output
	in_sub is used for recursiveness
Outputs: ASN.1 code written to 'str'
Procedure:
0. Scan for modules
1. Starting with lots of things cleared, DO forever
        IF no token AND no more tokens, break out of DO
        Switch on state
2.    Case GLOBAL
	IF processing global state returns GLOBAL state, return
      Case IN_DEFINITION
	    IF processing the definition returns GLOBAL state
		Clean up a few variables
      Case IN_ITEM
	    IF in a sub-item AND pre-processing the items returns GLOBAL
		state, return
**/
char linebuf[20*ASN_BSIZE], *linend = linebuf, *elinebuf = &linebuf[sizeof(linebuf)];
int active;   /* -1= in main file,
                  0= in imported file but not imported class
		  1= in imported file in an imported class, no details needed
		  2=  "   "        "  "   "    "      " , but details needed */
ulong loctag;
if (!in_sub) *classname = *token = 0;
if (fd >= 0 && !in_sub && !real_start) scan_modules(fd);
if (fd <= 0) active = -1;
else active = 0;
for (loctag = 0,
    *linebuf = *itemname = 0; pre_proc_get_token(fd, str, linebuf); )
    {
    switch (state)
    	{
    case PRE_GLOBAL:
	if (*token == ':') syntax(definitions_w);
	if (!strcmp(token, definitions_w)) state = GLOBAL;
	else *token = 0;   /* discard up to DEFINITIONS */
	break;

    case GLOBAL:			 
	state = pre_proc_glob(fd, str, linebuf, &linend, elinebuf, &active);
	break;

    case IN_DEFINITION:                                     /* got ::= */
	if ((state = pre_proc_def(fd, str, linebuf, &linend, elinebuf,
            &active)) == GLOBAL)
	    {
            active = (fd <= 0)? -1: 0;
            *subclass = *classname = 0;
	    if (fd >= 0) fflush(str);
	    if (in_sub) return;
	    }
	else if (state == IN_DEFINITION)
	    {
{'fraction_non_alphanumeric': 0.12156315422191208, 'fraction_numerical': 0.008932309839497558, 'mean_word_length': 3.1989452094931146, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7210954', 'n_tokens_mistral': 4650, 'n_tokens_neox': 4407, 'n_words': 1613}