codec.c
来自「VC6.0环境下」· C语言 代码 · 共 1,346 行 · 第 1/3 页
C
1,346 行
oldvalue |= ((value&mask) << (p.offset&0x7)); ptr[0] = oldvalue&0xff; oldvalue = oldvalue >> 8; ptr[1] = oldvalue&0xff; oldvalue = oldvalue >> 8; ptr[2] = oldvalue&0xff; oldvalue = oldvalue >> 8; ptr[3] = oldvalue&0xff; frame.size = size; p.offset += frame.size+spare; if (p.end < p.offset) p.end = p.offset; if (!(pass2&&exists)) { if (built) bldseq = bldord; built = false; exists = true; }};voidField::report() { if (!exists&&!invalid) return; Codec::report(); if (invalid) cout.form("BAD "); if (size) cout.form("%d",value); if (text) cout.form(" - %s",text); cout << '\n'; Codec::morebits();};OctetString::OctetString(char* l, char *t, int s) : Codec(l,t,s,0) { dfltval = 0;};voidOctetString::decode(FramePointer& p) { p.offset = (p.offset+7)&(~0x7); if (p.length<p.offset) { invalid = true; throw Bounds(label); } frame = p; frame.size = p.length - p.offset; p.offset += frame.size; if (p.end < p.offset) p.end = p.offset; exists = true;};voidOctetString::encode(FramePointer& p) { p.offset = (p.offset+7)&(~0x7); if (p.length<p.offset+frame.size) { invalid = true; throw Bounds(label); } char* ptr = p.point(); if (built) { int i; for (i=0;i<(frame.size>>3);i++) ptr[i] = frame.point()[i]; } else { if (size>0) { frame.size = size; int i; for (i=0;i<(frame.size>>3);i++) ptr[i] = dfltval; } else { frame.size = 0; } } p.size = frame.size; frame = p; p.offset += p.size; if (p.end < p.offset) p.end = p.offset; if (!(pass2&&exists)) { if (built) bldseq = bldord; built = false; exists = true; }};voidOctetString::report() { if (!exists&&!invalid) return; int i,j; char* ptr = frame.point(); int len = (frame.size+7)/8; i=0; j=0; value = ptr[0]; Codec::report(); if (invalid) cout << "BAD"; else while (i<len) { if (j) cout.form("--- ******** | . : "); j++; for (;(i<len)&&(i<12*j);i++) { cout.width(2); cout.fill('0'); cout.setf(ios::right,ios::adjustfield); cout << hex << (unsigned int)ptr[i] << ' '; } } if (!len) cout << "(null)"; cout << dec; cout << '\n';};DigitString::DigitString(char* l,char* t,int s) : OctetString(l,t,s) { dfltval = 15;};voidDigitString::report() { static char a[] = "0123456789*?#KS_"; if (!exists&&!invalid) return; int i; char* ptr = frame.point(); int len = (frame.size+7)/8; Codec::report(); if (invalid) cout << "BAD"; else for (i=0;i<len;i++) { cout << a[ptr[i]&0xf]; cout << a[(ptr[i]>>4)&0xf]; } cout << '\n';};AsciiString::AsciiString(char* l,char* t,int s) : OctetString(l,t,s) { dfltval = (int)'?';};voidAsciiString::report() { if (!exists&&!invalid) return; int i; char* ptr = frame.point(); int len = (frame.size+7)/8; Codec::report(); if (invalid) cout << "BAD"; else for (i=0;i<len;i++) { cout << ptr[i]; } cout << '\n';};Set::Set(char* l, char* t, int s, int x, ...) : Field(l,t,s,x) { // set up mnemonics and text for various values va_list argptr; int index; char* mnem; char* text; vals = 0; va_start(argptr,x); mnem = va_arg(argptr,char*); while (mnem) { index = va_arg(argptr,int); text = va_arg(argptr,char*); mnem = va_arg(argptr,char*); vals++; } va_end(argptr); if (vals) { val = new SetStr[vals]; va_start(argptr,x); int i; for (i=0;i<vals;i++) { val[i].mnem = va_arg(argptr,char*); val[i].indx = va_arg(argptr,int); val[i].text = va_arg(argptr,char*); } va_end(argptr); } else { val = NULL; }};Set::~Set() { if (val) delete[] val;};voidSet::decode(FramePointer& p) { Field::decode(p); // just decode as a field};voidSet::encode(FramePointer& p) { Field::encode(p); // just encode as a field};voidSet::report() { if (!exists&&!invalid) return; bool found = false; int i; for (i=0;i<vals;i++) { if ( (val[i].indx == value) || (val[i].indx == -1) ) { found = true; break; } } Codec::report(); if (invalid) cout.form("BAD "); cout.form("%d",value); if (found) cout.form(" - %s",val[i].text); cout << '\n'; Codec::morebits();};bool Set::set(int v) { return Codec::set(v); };bool Set::set(char* p, int l) { return Codec::set(p,l); };boolSet::set(char* m) { bool found = false; int i; for (i=0;i<vals;i++) { if ( (val[i].indx!=-1) && (strcmp(m,val[i].mnem)==0) ) { found = true; break; } } if (found) { set(val[i].indx); } return found;};bool Set::get(int& v) { return Codec::get(v); };bool Set::get(char*& p, int& l) { return Codec::get(p,l); };boolSet::get(char*& m) { if (!exists&&!invalid) return false; bool found = false; int i; for (i=0;i<vals;i++) { if ( (val[i].indx == value) || (val[i].indx == -1) ) { found = true; break; } } if (found) { m = val[i].mnem; } return found;};bool Set::test(int v) { return Codec::test(v); };bool Set::test(char* p, int l) { return Codec::test(p,l); };boolSet::test(char* m) { if (!exists&&!invalid) return false; bool found = false; int i; for (i=0;i<vals;i++) { if ( (val[i].indx!=-1) && (strcmp(m,val[i].mnem)==0) ) { found = true; break; } } if (found) return (value==val[i].indx); return found;};Bra::Bra(char* l, char* t, Codec* c, ...) : Codec(l,t,0,0) { branch = c; // set up paths for various values va_list argptr; int indx; Codec* path; vals = 0; va_start(argptr,c); path = va_arg(argptr,Codec*); while (path) { indx = va_arg(argptr,int); path = va_arg(argptr,Codec*); vals++; } va_end(argptr); if (vals) { val = new BraStr[vals]; va_start(argptr,c); int i; for (i=0;i<vals;i++) { val[i].path = va_arg(argptr,Codec*); val[i].indx = va_arg(argptr,int); } va_end(argptr); } else { val = NULL; }};Bra::Bra(const Bra& original) : Codec(original) { branch = original.branch; vals = original.vals; val = original.val;};Bra::~Bra() { if (master) if (val) delete[] val;};voidBra::decode(FramePointer& p) { bool found = false; frame = p; frame.size = size; int i; for (i=0;i<vals;i++) { if ( (val[i].indx == branch->value) || (val[i].indx == -1) ) { found = true; break; } } if (found) { if (!branch->mnem) branch->mnem = val[i].path->label; if (!branch->text) branch->text = val[i].path->title; try { val[i].path->decode(p); } catch (...) { invalid = true; throw; } } else { invalid = true; branch->invalid = true; } exists = found;};voidBra::encode(FramePointer& p) { bool found = false; frame = p; frame.size = size; int i; if (branch->bound) { // value was bound and built, find value and encode out for (i=0;i<vals;i++) { if ( (val[i].indx == branch->value) || (val[i].indx == -1) ) { found = true; break; } } } else { // value not bound, find out which path was built first int first; int order = MAXINT; for (i=0;i<vals;i++) { if ( val[i].path->built ) { found = true; if (val[i].path->bldord<order) { first = i; order = val[i].path->bldord; }; } } if (found) { // rebuild new value :note: this will also bind the value i = first; branch->set(val[i].indx); if (branch->exists) { FramePointer t = branch->frame; branch->encode(t); } } else { // value not bound, branch not built, encode default path for (i=0;i<vals;i++) { if ( (val[i].indx == branch->value) || (val[i].indx == -1) ) { found = true; break; } } } } if (found) { if (!branch->mnem) branch->mnem = val[i].path->label; if (!branch->text) branch->text = val[i].path->title; val[i].path->encode(p); if (!(pass2&&exists)) { if (built) bldseq = bldord; built = false; exists = true; } }};voidBra::report() { if (!exists&&!invalid) return; bool found = false; int i; for (i=0;i<vals;i++) { if ( (val[i].indx == branch->value) || (val[i].indx == -1) ) { found = true; break; } } if (found) { val[i].path->report(); return; }};boolBra::set(char* m) { bool found = false; int i; for (i=0;i<vals;i++) { if ( (val[i].indx!=-1) && (strcmp(m,val[i].path->label)==0) ) { found = true; break; } } if (found) { branch->set(val[i].indx); } return found;};boolBra::get(char*& m) { if (!exists&&!invalid) return false; bool found = false; int i; for (i=0;i<vals;i++) { if ( (val[i].indx = branch->value) || (val[i].indx == -1) ) { found = true; break; } } if (found) { m = val[i].path->label; } return found;};boolBra::test(char* m) { if (!exists&&!invalid) return false; bool found = false; int i; for (i=0;i<vals;i++) { if ( (val[i].indx!=-1) && (strcmp(m,val[i].path->label)==0) ) { found = true; break; } } if (found) return (branch->value==val[i].indx); return found;};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?