|
OpenSS7 SS7 for the Common Man |
© Copyright 1997-2004,OpenSS7 Corporation, All Rights Reserved. |
||||||||||||||||||||||||||
| Home | Overview | Status | News | Documentation | Resources | About | |||||||||||||||||||||
File /code/ss7codec/codec.h
#ifndef __SS7_CODEC_H__
#define __SS7_CODEC_H__
extern "C" {
#include <unistd.h>
}
#include <values.h>
#include <iostream>
#include "defines.h"
#pragma interface
/**
* General Error class.
*
* The Error class is used for throwing errors from the
* encoder/decoder. Each error which is thrown consists of a
* description, a lable, and a virtual member function to print the
* error.
*
* <p>General error classes are derived from this error class. Each
* error class has a general description <i>desc</i> which applies to
* all classes and a specific <i>label</i> which indicates the specific
* error or specific error location.
*
*/
class Error {
public:
/// General error description.
const char* desc;
/// Label defining specfic error or location.
const char* label;
/// Output error text on standard error.
INLINE void errout() const { cerr.form("ERROR at %s: %s\n",label,desc); };
INLINE Error(const char* d,const char* l) : desc(d), label(l) { };
};
/**
* Unexpected value error class.
*
* An error class that indicates that some part of the encoding or
* decoding of the fram was invalid.
*
*/
class Invalid : public Error {
public:
Invalid(char* l) : Error("Unexpected value." ,l) { errout(); };
};
/**
* Out of bounds error class.
*
* An error calss that indicates that the bounds of some subframe
* within the syntax of the message was exceeded.
*
*/
class Bounds : public Error {
public:
Bounds (char* l) : Error("Out of bounds." ,l) { errout(); };
};
/**
* Non-integer repetition error class.
*
* An error class that inidcates that the repeating token or subframe
* within the message exceeded the boundaries of the enclosing subrame
* or that exceeds the bytes remaining at the end of the sequence or
* repetitions.
*
*/
class Repeats : public Error {
public:
Repeats(char* l) : Error("Non-integer repeat." ,l) { errout(); };
};
/**
* Specification error class.
*
* An error that indicates that there must be some problem with the
* specification of the protocol. This should never happen for a valid
* sepcification of a protocol syntax.
*
*/
class SpecErr : public Error {
public:
SpecErr(char* l) : Error("Specification error." ,l) { errout(); };
};
/**
* Null parm pointer error class.
*
* An error class that indicates that a null pointer was encountered
* when it was expected to point to something.
*
*/
class NullPtr : public Error {
public:
NullPtr(char* l) : Error("Null parm pointer." ,l) { errout(); };
};
class NewFlag {
private:
static int crumb;
int value;
public:
INLINE NewFlag() : value(0) { };
INLINE NewFlag(bool b) : value(b?crumb:0) { };
INLINE NewFlag(int i) : value(i?crumb:0) { };
INLINE operator bool() {
if (value>crumb) value=0; // clear really old flags
return (value==crumb);
};
INLINE operator int() {
if (value>crumb) value=0; // clear really old flags
return (value==crumb);
};
INLINE static void clearFlags(void) {
crumb++; if (!crumb) crumb++;
};
};
class FramePointer {
public:
unsigned char* pointer; // pointer to start of frame
int length; // bit length of frame
int offset; // bit offset into frame
int end; // bit offset of end of frame
int size; // bit size of something in frame
INLINE unsigned char* point(void) { // point to offset
return pointer + (offset>>3);
};
INLINE void set(unsigned char* p,int l) { // set the pointer
pointer = p;
length = l*8;
offset = 0;
size = length;
end = 0;
};
INLINE FramePointer(unsigned char* p, int l)
: pointer(p), length(l*8), offset(0), end(0), size(length) { };
INLINE FramePointer(void)
: pointer(0), length(0), offset(0), end(0), size(0) { };
INLINE FramePointer(FramePointer& o)
: pointer(o.pointer), length(o.length), offset(o.offset), end(o.end), size(o.size) { };
INLINE FramePointer& operator=(const FramePointer& o) {
pointer = o.pointer;
length = o.length ;
offset = o.offset ;
end = o.end ;
size = o.size ;
return *this;
};
INLINE operator unsigned char*() { return pointer; };
INLINE operator void*() { return pointer; };
INLINE operator int() { return (size+7)>>3; };
};
class Codec;
#if 0
class Level {
public:
Codec* codec; // codec for this level
virtual void decode(FramePointer&); // decode method for this level
virtual void encode(FramePointer&); // encode method for this level
virtual void report(); // report method for this level
};
#endif
class Field;
/**
* General codec class.
*
* The general codec class is the base class for all thing which can
* be encoded an decoded. All things which can be encoded and decoded
* implement the interface to this class. Of primary importance are
* the encode() and decode() methods and of secondary importance is
* the report() method.
*
* <p>Actual encodable and decodable classes are derived from this
* class. This should probably be an Abstract Base class in at least
* on of the interface implementations to avoid direct instantiation.
* I don't know why I didn't do it this way to begin with.
*
*/
class Codec {
private:
Codec& operator=(const Codec&) { return *this; };
public:
/// build sequence number
static int bldseq;
enum { ccitt, ansi };
static int variant;
/// is this pass 2?
static NewFlag pass2;
/// is second pass required?
static NewFlag pass2req;
char* label;
char* title;
char* mnem;
char* text;
int size;
int spare;
int dfltval;
int value;
/// build order number
int bldord;
FramePointer frame;
/// does this object exist?
NewFlag exists;
/// was this object built?
NewFlag built;
/// was this object bound?
NewFlag bound;
/// is this object valid?
NewFlag invalid;
/// master copy
bool master;
INLINE virtual void decode(FramePointer& p) {
/// decode method for this level
frame = p;
frame.size = size;
exists = true;
};
INLINE virtual void encode(FramePointer& p) {
/// encode method for this level
frame = p;
frame.size = size;
if (!(pass2&&exists)) {
if (built) bldseq = bldord;
built = false;
exists = true;
}
};
/// report method for this level
INLINE virtual void report();
/// report bits
INLINE virtual void bits(int);
/// report more bits
INLINE virtual void morebits();
INLINE virtual void build(void) {
/// build one of these
bldord = bldseq++;
built = true;
};
INLINE virtual bool set(int v) {
/// set value by value
value = v;
bldord = bldseq++;
built = true;
bound = true;
return built;
};
INLINE virtual bool set(char* m) {
/// set value by mnemonic
return false;
};
INLINE virtual bool set(unsigned char* p,int l) {
/// set value by pointer
frame.set(p,l);
bldord = bldseq++;
built = true;
bound = true;
return built;
};
INLINE virtual bool get(int& v) {
/// get value by value
if (exists) v = value;
return exists;
};
INLINE virtual bool get(char*& m) {
/// get value by mnemonic
return false;
};
INLINE virtual bool get(unsigned char*& p,int& l) { // get value by pointer
if (exists) {
p = frame.point();
l = (frame.size+7)/8;
}
return exists;
};
INLINE virtual bool test(int v) { // test value by value
if (!exists) return false;
return value == v;
};
INLINE virtual bool test(char* m) { // test value by mnemonic
return false;
};
INLINE virtual bool test(unsigned char* p,int l) { // test value by pointer
if (!exists) return false;
if (l!=frame.size/8) return false;
return memcmp(p,frame.point(),l)==0;
};
INLINE static bool decode(Codec*,unsigned char*,int); // decode the frame
INLINE static bool encode(Codec*,unsigned char*,int&); // encode the frame
INLINE static void report(Codec*); // report the frame
INLINE static void rebuild(); // rebuild the frame
INLINE Codec(char* l,char* t,int s,int x)
: label(l), title(t), mnem(NULL), text(NULL),
size(s), spare(x), dfltval(0), value(0), bldord(0),
frame(NULL,0),
exists(false), built(false), bound(false), invalid(false),
master(true) { };
INLINE Codec(const Codec& o)
: label(o.label), title(o.title), mnem(NULL), text(NULL),
size(o.size), spare(o.spare), dfltval(o.dfltval), value(0), bldord(0),
frame(NULL,0),
exists(false), built(false), bound(false), invalid(false),
master(false) { };
INLINE virtual Codec* copy(void) {
return new Codec(*this);
};
INLINE virtual Codec* newcopy(char* l,char* t) {
return new Codec(l,t,size,spare);
};
INLINE virtual ~Codec(void) { };
};
class Content;
class List : public Codec {
private:
List& operator=(const List&) { return *this; };
public:
int contents;
int branches;
Codec** content;
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE virtual void report(); // report method for this level
INLINE List(char*,char*,int,int,int,Codec**);
INLINE List(const List&);
INLINE List* newcopy(char*,char*);
INLINE virtual ~List();
};
class Header : public List {
public:
INLINE Header(char*,char*,int,int,int,Codec**);
};
class Content : public List {
public:
INLINE Content(char*,char*,int,int,int,Codec**);
};
class Parameter : public List {
public:
INLINE Parameter(char*,char*,int,int,int,Codec**);
INLINE Parameter(const Parameter&);
};
class ParameterG : public Parameter {
public:
INLINE ParameterG(char*,char*,...);
INLINE ParameterG(const ParameterG&);
INLINE virtual ~ParameterG();
};
class ParameterR : public Parameter {
private:
ParameterR& operator=(const ParameterR&) { return *this; };
public:
ParameterR* next;
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE virtual void report(); // report method for this level
INLINE ParameterR(char*,char*,int,int,int,Codec**);
INLINE ParameterR(ParameterR&);
INLINE ParameterR* copy();
INLINE ParameterR* newcopy(char*,char*);
INLINE virtual ~ParameterR();
};
class ParameterP : public Parameter {
public:
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE ParameterP(char*,char*,int,int,int,Codec**);
};
class ParameterPL : public Parameter {
public:
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE ParameterPL(char*,char*,int,int,int,Codec**);
};
class ParameterPNL : public Parameter {
private:
ParameterPNL& operator=(const ParameterPNL&) { return *this; };
public:
ParameterPNL* next;
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE virtual void report(); // report method for this level
INLINE ParameterPNL(char*,char*,int,int,int,Codec**);
INLINE ParameterPNL(ParameterPNL&);
INLINE ParameterPNL* newcopy(char*,char*);
INLINE virtual ~ParameterPNL();
};
class ParameterNL : public Parameter {
private:
ParameterNL& operator=(const ParameterNL&) { return *this; };
public:
ParameterNL* next;
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE virtual void report(); // report method for this level
INLINE ParameterNL(char*,char*,int,int,int,Codec**);
INLINE ParameterNL(ParameterNL&);
INLINE ParameterNL* newcopy(char*,char*);
INLINE virtual ~ParameterNL();
};
class OctetString : public Codec {
public:
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE virtual void report(); // report method for this level
INLINE OctetString(char*,char*,int);
};
class DigitString : public OctetString {
public:
INLINE void report(); // report method for this level
INLINE DigitString(char*,char*,int);
};
class AsciiString : public OctetString {
public:
INLINE void report(); // report method for this level
INLINE AsciiString(char*,char*,int);
};
class Field : public Codec {
public:
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE virtual void report(); // report method for this level
INLINE Field(char*,char*,int,int);
};
class Set : public Field {
private:
Set& operator=(const Set&) { return *this; };
protected:
int vals;
struct SetStr { int indx; char* mnem; char* text; } *val;
public:
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE virtual void report(); // report method for this level
INLINE virtual bool set(int); // set value by value
INLINE virtual bool set(char*); // set value by mnemonic
INLINE virtual bool set(unsigned char*,int); // set value by pointer
INLINE virtual bool get(int&); // get value by value
INLINE virtual bool get(char*&); // get value by mnemonic
INLINE virtual bool get(unsigned char*&,int&); // get value by pointer
INLINE virtual bool test(int); // test value by value
INLINE virtual bool test(char*); // test value by mnemonic
INLINE virtual bool test(unsigned char*,int); // test value by pointer
INLINE Set(char*,char*,int,int,...);
INLINE virtual ~Set();
};
class Bra : public Codec {
private:
Bra& operator=(const Bra&) { return *this; };
friend ParameterPNL;
friend ParameterNL;
protected:
int vals;
struct BraStr { int indx; Codec* path; } *val;
public:
Codec* branch;
INLINE virtual void decode(FramePointer&); // decode method for this level
INLINE virtual void encode(FramePointer&); // encode method for this level
INLINE virtual void report(); // report method for this level
INLINE virtual bool set(int); // set value by value
INLINE virtual bool set(char*); // set value by mnemonic
INLINE virtual bool set(unsigned char*,int); // set value by pointer
INLINE virtual bool get(int&); // get value by value
INLINE virtual bool get(char*&); // get value by mnemonic
INLINE virtual bool get(unsigned char*&,int&); // get value by pointer
INLINE virtual bool test(int); // test value by value
INLINE virtual bool test(char*); // test value by mnemonic
INLINE virtual bool test(unsigned char*,int); // test value by pointer
INLINE Bra(char*,char*,Codec*,...);
INLINE Bra(const Bra&);
INLINE virtual ~Bra();
};
#endif __SS7_CODEC_H__
|
|||||||||||||||||||||||||||
|
OpenSS7 SS7 for the Common Man |
Home | Overview | Status | News | Documentation | Resources | About | ||||||||||||||||||||
|
© Copyright 1997-2004,OpenSS7 Corporation, All Rights Reserved. |
|||||||||||||||||||||||||||