int mkor(int first, int second) { int eps, orend; if ( first == NIL ) return ( second ); else if ( second == NIL ) return ( first ); else { /* see comment in mkopt() about why we can't use the first state * of "first" or "second" if they satisfy "FREE_EPSILON" */ eps = mkstate( SYM_EPSILON ); first = link_machines( eps, first ); mkxtion( first, second ); if ( SUPER_FREE_EPSILON(finalst[first]) && accptnum[finalst[first]] == NIL ) { orend = finalst[first]; mkxtion( finalst[second], orend ); } else if ( SUPER_FREE_EPSILON(finalst[second]) && accptnum[finalst[second]] == NIL ) { orend = finalst[second]; mkxtion( finalst[first], orend ); } else { eps = mkstate( SYM_EPSILON ); first = link_machines( first, eps ); orend = finalst[first]; mkxtion( finalst[second], orend ); } } finalst[first] = orend; return ( first ); }
int mkrep(int mach, int lb, int ub) { int base_mach, tail, copy, i; base_mach = copysingl( mach, lb - 1 ); if ( ub == INFINITY ) { copy = dupmachine( mach ); mach = link_machines( mach, link_machines( base_mach, mkclos( copy ) ) ); } else { tail = mkstate( SYM_EPSILON ); for ( i = lb; i < ub; ++i ) { copy = dupmachine( mach ); tail = mkopt( link_machines( copy, tail ) ); } mach = link_machines( mach, link_machines( base_mach, tail ) ); } return ( mach ); }
int dupmachine(int mach) { int i, init, state_offset; int state = 0; int last = lastst[mach]; for ( i = firstst[mach]; i <= last; ++i ) { state = mkstate( transchar[i] ); if ( trans1[i] != NO_TRANSITION ) { mkxtion( finalst[state], trans1[i] + state - i ); if ( transchar[i] == SYM_EPSILON && trans2[i] != NO_TRANSITION ) mkxtion( finalst[state], trans2[i] + state - i ); } accptnum[state] = accptnum[i]; } if ( state == 0 ) flexfatal( "empty machine in dupmachine()" ); state_offset = state - i + 1; init = mach + state_offset; firstst[init] = firstst[mach] + state_offset; finalst[init] = finalst[mach] + state_offset; lastst[init] = lastst[mach] + state_offset; return ( init ); }
int copysingl(int singl, int num) { int copy, i; copy = mkstate( SYM_EPSILON ); for ( i = 1; i <= num; ++i ) copy = link_machines( copy, dupmachine( singl ) ); return ( copy ); }
void mkdfa(info_t *infos, int ninfos, int useprefix, int debug) { /* static const char *names[] = { "foo", "bar", "foobar", "argh", "filep", }; static int nnames = SIZEOF (names); const char *accept; */ int i; start = mkstate(); for (i = 0; i < (ninfos - 1); i++) infos[i].namelen = strlen(infos[i].name); for (i = 0; i < (ninfos - 1); i++) add_states(start, &infos[i], 0); mktables(start, infos[ninfos - 1].value, useprefix); if (debug) { printf("\n/*\n"); prstate(start); printf("*/\n"); /* for (i = 0; i < ninfos; i++) { accept = rundfa (infos[i].name, infos[i].namelen); if (accept) printf ("%s\n", accept); else printf ("%s not accepted\n", infos[i].name); } for (i = 0; i < nnames; i++) { accept = rundfa (names[i], strlen (names[i])); if (accept) printf ("%s\n", accept); else printf ("%s not accepted\n", names[i]); } */ } }
int mkopt(int mach) { int eps; if ( ! SUPER_FREE_EPSILON(finalst[mach]) ) { eps = mkstate( SYM_EPSILON ); mach = link_machines( mach, eps ); } /* can't skimp on the following if FREE_EPSILON(mach) is true because * some state interior to "mach" might point back to the beginning * for a closure */ eps = mkstate( SYM_EPSILON ); mach = link_machines( eps, mach ); mkxtion( mach, finalst[mach] ); return ( mach ); }
void scinstal(char *str, int xcluflg) { char *copy_string(); /* Generate start condition definition, for use in BEGIN et al. */ action_define( str, lastsc ); if ( ++lastsc >= current_max_scs ) scextend(); scname[lastsc] = copy_string( str ); if ( addsym( scname[lastsc], NULL, lastsc, sctbl, START_COND_HASH_SIZE ) ) format_pinpoint_message( _( "start condition %s declared twice" ), str ); scset[lastsc] = mkstate( SYM_EPSILON ); scbol[lastsc] = mkstate( SYM_EPSILON ); scxclu[lastsc] = xcluflg; sceof[lastsc] = false; }
/* mkposcl - convert a machine into a positive closure * * synopsis * new = mkposcl( state ); * * new - a machine matching the positive closure of "state" */ int mkposcl(int state) { int eps; if (SUPER_FREE_EPSILON(finalst[state])) { mkxtion(finalst[state], state); return state; } else { eps = mkstate(SYM_EPSILON); mkxtion(eps, state); return link_machines(state, eps); } }
int mkbranch(int first, int second) { int eps; if ( first == NO_TRANSITION ) return ( second ); else if ( second == NO_TRANSITION ) return ( first ); eps = mkstate( SYM_EPSILON ); mkxtion( eps, first ); mkxtion( eps, second ); return ( eps ); }
void add_accept(int mach, int accepting_number) { /* hang the accepting number off an epsilon state. if it is associated * with a state that has a non-epsilon out-transition, then the state * will accept BEFORE it makes that transition, i.e., one character * too soon */ if ( transchar[finalst[mach]] == SYM_EPSILON ) accptnum[finalst[mach]] = accepting_number; else { int astate = mkstate( SYM_EPSILON ); accptnum[astate] = accepting_number; mach = link_machines( mach, astate ); } }
void add_states(state_t *state, info_t *info, int pos) { transition_t *transitions; if (info->namelen == pos) { state->value = info->value; return; } transitions = state->transitions; while (transitions) { if (tolower(transitions->value) == tolower(info->name[pos])) { if ((transitions->state->value && (info->namelen == (pos + 1))) || (info->namelen != (pos + 1))) { add_states(transitions->state, info, pos + 1); return; } } transitions = transitions->next; } if (state->transitions) { transitions = state->transitions; while (transitions->next) transitions = transitions->next; transitions->next = mktransition(); transitions = transitions->next; } else { transitions = mktransition(); state->transitions = transitions; } transitions->value = info->name[pos]; transitions->state = mkstate(); add_states(transitions->state, info, pos + 1); }
void *lnc_aes_init(uint8_t *msg, uint8_t *key, int *status) { lnc_aes_ctx_t *ctx = malloc(sizeof(lnc_aes_ctx_t)); uint32_t int_key[Nk]; int i; if(ctx == NULL) { *status = LNC_ERR_MALLOC; return NULL; } for(i = 0; i < Nk; i++) int_key[i] = key[i * 4] << 24 | key[i * 4 + 1] << 16 | key[i * 4 + 2] << 8 | key[i * 4 + 3]; ctx->expkey = expand_key(int_key, status); if(*status != LNC_OK) return NULL; ctx->state = mkstate(msg, status); return ctx; }
void lnc_aes_update(void *context, uint8_t *msg, uint8_t *key, int *status) { lnc_aes_ctx_t *ctx = context; uint32_t int_key[Nk]; int i; *status = LNC_OK; if(key) { free(ctx->expkey); for(i = 0; i < Nk; i++) int_key[i] = key[i * 4] << 24 | key[i * 4 + 1] << 16 | key[i * 4 + 2] << 8 | key[i * 4 + 3]; ctx->expkey = expand_key(int_key, status); if(status != LNC_OK) return; } if(msg) { free(ctx->state); ctx->state = mkstate(msg, status); } }