Skip to content

Instantly share code, notes, and snippets.

@isoiphone
Created December 30, 2013 21:10
Show Gist options
  • Save isoiphone/8188191 to your computer and use it in GitHub Desktop.
Save isoiphone/8188191 to your computer and use it in GitHub Desktop.
Found this old code
#include "IsControlCharacterFSM.h"
// This is a state machine to determine whether or not the passed character is part of a
// TELNET control sequence.
// It must be called for every character read in order to manage state appropriately.
// Credit where credit is due:
// Ryan Rawson (ryan@netidea.com) wrote this for FreeTrade. some time around 2000.
bool IsControlCharacterFSM(char ch)
{
static enum {norm,riac,rddww,rsb,rsiac} state = norm;
bool IsControlCharacter = true;
switch(state) {
case norm:
if(ch == IAC)
state = riac;
else
IsControlCharacter = false;
break;
case riac:
switch(ch) {
case IAC:
state = norm;
break;
case DO:
case DONT:
case WILL:
case WONT:
state = rddww;
break;
case SB:
state = rsb;
break;
default:
state = norm;
}
break;
case rddww:
state = norm;
break;
case rsb:
if(ch == IAC)
state = rsiac;
break;
case rsiac:
if(ch == SE)
state = norm;
else
state = rsb;
break;
}
return IsControlCharacter;
}
#if !defined(ISCONTROLCHARACTERFSM_H_INCLUDED)
#define ISCONTROLCHARACTERFSM_H_INCLUDED
bool IsControlCharacterFSM(char ch);
/* as per defined by various TELNET RFC's */
/* these are from the basic TELNET RFC */
#define IAC ((char)255)
#define DO ((char)253)
#define WILL ((char)251)
#define WONT ((char)252)
#define DONT ((char)254)
#define SB ((char)250)
#define SE ((char)240)
/* from the TOPT-ECHO RFC */
#define ECHO ((char)1)
/* from the TOPT-SGA RFC */
#define SUPPRESSGA ((char)3)
/* from the TOPT-LINE RFC */
#define LINEMODE ((char)34)
#endif // !defined(ISCONTROLCHARACTERFSM_H_INCLUDED)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment