codec.c
来自「VC6.0环境下」· C语言 代码 · 共 1,346 行 · 第 1/3 页
C
1,346 行
Parameter(l,t,s,c,b,a) { next = NULL;};ParameterR::ParameterR(ParameterR& original) : Parameter(original) { next = original.next; original.next = this;};ParameterR*ParameterR::copy() { return new ParameterR(*this);};ParameterR*ParameterR::newcopy(char* l,char* t) { return new ParameterR(l,t,size,contents,branches,content);};ParameterR::~ParameterR() { if (next) delete next;};voidParameterR::decode(FramePointer& p) { if (p.length<p.offset+size) return; if (master) { if (next) delete next; next = NULL; } frame = p; int i; try { for (i=0;i<contents;i++) content[i]->decode(p); } catch (...) { invalid = true; for (;i<contents;i++) content[i]->invalid = true; frame.size = p.offset - frame.offset; throw; } frame.size = p.offset - frame.offset; exists = true; ParameterR* temp = this->copy(); temp->decode(p);};voidParameterR::encode(FramePointer& p) { if (p.length<p.offset+size) return; if (master) { if (next) delete next; next = NULL; } frame = p; int i; for (i=0;i<contents;i++) content[i]->encode(p); frame.size = p.offset - frame.offset; if (!(pass2&&exists)) { if (built) bldseq = bldord; built = false; exists = true; }};voidParameterR::report() { Parameter::report(); if (next) next->report();};ParameterG::ParameterG(char* l,char* t,...) : Parameter(l,t,0,0,0,NULL) { int i; va_list argptr; va_start(argptr,t); while (va_arg(argptr,void*)) contents++; va_end (argptr); if (contents) content = new (Codec*)[contents]; va_start(argptr,t); for (i=0;i<contents;i++) { content[i] = va_arg(argptr,class Codec*); if (content[i]->size==-1) size = -1; if ((size!=-1)&&(size<content[i]->size)) size = content[i]->size; } va_end (argptr);};ParameterG::ParameterG(const ParameterG& original) : Parameter(original) { content = original.content; contents = original.contents; branches = original.branches; int i; for (i=0;i<contents;i++) content[i] = original.content[i];};ParameterG::~ParameterG() { if (contents) delete[] content; contents = 0; branches = 0;};ParameterP::ParameterP(char* l,char* t,int s,int c,int b,Codec** a): Parameter(l,t,s,c,b,a) {};voidParameterP::decode(FramePointer& p) { if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } if (contents<2) { invalid = true; throw SpecErr(label); } frame = p; frame.size = size; FramePointer f(p); content[0]->decode(p); // this is the pointer if (content[0]->value!=0) { f.offset += content[0]->value*8; int i; for (i=1;i<contents;i++) content[i]->decode(f); if (p.end<f.end) p.end = f.end; } else { content[0]->text = "End of Optional Parameters"; content[0]->mnem = "EOP"; } exists = true;};voidParameterP::encode(FramePointer& p) { if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } if (contents<2) { invalid = true; throw SpecErr(label); } frame = p; frame.size = size; FramePointer f(p); f.offset = ((f.end+7)>>3)*8; // point octet beyond current encoding FramePointer s(f),l(p); content[0]->set((f.offset-p.offset)>>3); // calculate and set offset content[0]->encode(p); // encode the pointer if (pass2) { int i; for (i=1;i<contents;i++) content[i]->encode(f); // encode frame if (p.end<f.end) p.end = f.end; // touch up end of frame if (f.offset==s.offset) { // nothing encoded inside content[0]->set(0); // zero the pointer content[0]->encode(l); // reencode the pointer content[0]->text = "End of Optional Parameters"; content[0]->mnem = "EOP"; } } else { pass2req = true; } if (!(pass2&&exists)) { if (built) bldseq = bldord; built = false; exists = true; }};ParameterPL::ParameterPL(char* l,char* t,int s,int c,int b,Codec** a) : Parameter(l,t,s,c,b,a) {};voidParameterPL::decode(FramePointer& p) { if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } if (contents<3) { invalid = true; throw SpecErr(label); } frame = p; frame.size = size; FramePointer f(p); content[0]->decode(p); // this is the pointer if (content[0]->value==0) { invalid = true; throw NullPtr(label); } f.offset += content[0]->value*8; content[1]->decode(f); // this is the length if (f.length<f.offset+content[1]->value*8) { invalid = true; throw Bounds(label); } f.length = f.offset+content[1]->value*8; int i; for (i=2;i<contents;i++) content[i]->decode(f); if (p.end<f.end) p.end = f.end; exists = true;};voidParameterPL::encode(FramePointer& p) { if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } if (contents<3) { invalid = true; throw SpecErr(label); } frame = p; frame.size = size; FramePointer f(p); f.offset = ((f.end+7)>>3)*8; // point octet beyond current encoding content[0]->set((f.offset-p.offset)>>3); // calculate and set offset content[0]->encode(p); // encode the pointer if (pass2) { FramePointer s(f); // get pointer to size indicator for later content[1]->encode(f); // preencode the length int i; for (i=2;i<contents;i++) content[i]->encode(f); // encode frame content[1]->set((f.offset-s.offset-8)>>3); // calculate and set length content[1]->encode(s); // reencode the length if (p.end<f.end) p.end = f.end; // touch up end of frame } else { pass2req = true; } if (!(pass2&&exists)) { if (built) bldseq = bldord; built = false; exists = true; }};ParameterPNL::ParameterPNL(char* l,char* t,int s,int c,int b,Codec** a) : Parameter(l,t,s,c,b,a) { next = NULL;};ParameterPNL::ParameterPNL(ParameterPNL& original) : Parameter(original) { next = original.next; original.next = this;};ParameterPNL*ParameterPNL::newcopy(char* l,char* t) { return new ParameterPNL(l,t,size,contents,branches,content);};ParameterPNL::~ParameterPNL() { if (next) delete next;};voidParameterPNL::decode(FramePointer& p) { if (master) { if (next) delete next; next = NULL; } if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } if (contents<4) { invalid = true; throw SpecErr(label); } frame = p; frame.size = size; FramePointer f; int i; f = p; // mark where pointer is content[0]->decode(p); // this is the pointer if (content[0]->value!=0) { // not end of parameters f.offset += content[0]->value*8; // offset frame content[1]->decode(f); // this is the label if (content[1]->value!=0) { // not end of parameters content[2]->decode(f); // this is the length if (f.length<f.offset+content[2]->value*8) { // check length content[2]->invalid = true; throw Bounds(content[2]->label); } f.length = f.offset+content[2]->value*8; // set the length try { for (i=3;i<contents;i++) content[i]->decode(f); // decode } catch (...) { }; Codec* temp = this->copy(); temp->decode(p); } else { content[1]->text = "End of Optional Parameters"; content[1]->mnem = "EOP"; } if (p.end<f.end) p.end = f.end; } else { content[0]->text = "End of Optional Parameters"; content[0]->mnem = "EOP"; } exists = true;};voidParameterPNL::encode(FramePointer& p) { if (master) { if (next) delete next; next = NULL; } if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } if (contents<4) { invalid = true; throw SpecErr(label); } frame = p; frame.size = size; FramePointer l,s,f; int i,j; f = p; // mark where pointer is f.offset = ((f.end+7)>>3)*8;// point octet beyond current encoding l = f; // mark where frame starts content[0]->set((f.offset-p.offset)>>3); // calculate and set offset content[0]->encode(p); // encode the pointer bool found = false; // check for parameters to encode for (j=0;j<((Bra*)content[3])->vals;j++) { if ( ((Bra*)content[3])->val[j].path->built ) { found = true; break; } } if (found) { if (pass2) { content[1]->encode(f); // preencode the label s = f; // get pointer to size for later content[2]->encode(f); // preencode the length for (i=3;i<contents;i++) content[i]->encode(f); // encode content[2]->set((f.offset-s.offset-8)>>3); // calc&set length content[2]->encode(s); // reencode the length if (p.end<f.end) p.end = f.end; // touch up end of frame Codec* temp = this->copy(); temp->encode(p); } else { pass2req = true; ((Bra*)content[3])->val[j].path->built = false; Codec* temp = this->copy(); temp->encode(p); ((Bra*)content[3])->val[j].path->built = true; } } else { if (pass2) { content[1]->set(0); // set for end of parameters content[1]->encode(f); // encode the label content[1]->text = "End of Optional Parameters"; content[1]->mnem = "EOP"; if (p.end<f.end) p.end = f.end; // touch up end of frame } else { pass2req = true; } } if (built) bldseq = bldord; built = false; exists = true;};voidParameterPNL::report() { Parameter::report(); if (next) next->report();};ParameterNL::ParameterNL(char* l,char* t,int s,int c,int b,Codec** a) : Parameter(l,t,s,c,b,a) { next = NULL;};ParameterNL::ParameterNL(ParameterNL& original) : Parameter(original) { next = original.next; original.next = this;};ParameterNL*ParameterNL::newcopy(char* l,char* t) { return new ParameterNL(l,t,size,contents,branches,content);};ParameterNL::~ParameterNL() { if (next) delete next;};voidParameterNL::decode(FramePointer& p) { if (master) { if (next) delete next; next = NULL; } if (p.length<p.offset+size) return; // assume end of list if (contents<3) { invalid = true; throw SpecErr(label); } frame = p; frame.size = size; int i; content[0]->decode(p); // this is the label if (content[0]->value!=0) { // not end of parameters content[1]->decode(p); // this is the length if (p.length<p.offset+content[1]->value*8) { // check length content[1]->invalid = true; throw Bounds(content[1]->label); } FramePointer f(p); // new sized frame f.length = f.offset+content[1]->value*8; // set the length try { for (i=2;i<contents;i++) content[i]->decode(f); // decode } catch (...) { }; frame.size = f.offset - p.offset; p.offset = f.length; // jump past sized frame Codec* temp = this->copy(); temp->decode(p); } else { content[0]->text = "End of Optional Parameters"; content[0]->mnem = "EOP"; } exists = true;};voidParameterNL::encode(FramePointer& p) { if (master) { if (next) delete next; next = NULL; } if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } if (contents<3) { invalid = true; throw SpecErr(label); } frame = p; frame.size = size; int i,j; bool found = false; // check for parameters to encode for (j=0;j<((Bra*)content[2])->vals;j++) { if ( ((Bra*)content[2])->val[j].path->built ) { found = true; break; } } if (found) { content[0]->encode(p); // preencode the label FramePointer s(p); // get pointer to size for later content[1]->encode(p); // preencode the length for (i=2;i<contents;i++) content[i]->encode(p); // encode content[1]->set((p.offset-s.offset-8)>>3); // calc&set length content[1]->encode(s); // reencode the length Codec* temp = this->copy(); temp->encode(p); } else { if (!master) { content[0]->set(0); // set for end of parameters content[0]->encode(p); // encode the label content[0]->text = "End of Optional Parameters"; content[0]->mnem = "EOP"; } else { return; } } if (p.end<p.offset) p.end = p.offset; if (built) bldseq = bldord; built = false; exists = true;};voidParameterNL::report() { Parameter::report(); if (next) next->report();};Field::Field(char* l, char* t, int s, int x) : Codec(l,t,s,x) {};voidField::decode(FramePointer& p) { if (p.length<(p.offset+size+spare)) { invalid = true; cout.form("length = %d, offset = %d, size = %d, spare = %d\n", p.length,p.offset,size,spare); throw Bounds(label); } if (!exists) { text = NULL; mnem = NULL; } char* ptr = p.point(); frame = p; value = 0; value |= ptr[3]; value = value << 8; value |= ptr[2]; value = value << 8; value |= ptr[1]; value = value << 8; value |= ptr[0]; value = value >> (p.offset&0x7); int mask; if ((size>0)&&(size<=32)) mask = (0x1<<size)-1; else mask = -1; value &= mask; frame.size = size; p.offset += frame.size+spare; if (p.end < p.offset) p.end = p.offset; exists = true;};voidField::encode(FramePointer& p) { if (!exists&&!bound) value = dfltval; if (p.length<p.offset+size+spare) { invalid = true; throw Bounds(label); } if (!exists) { text = NULL; mnem = NULL; } char* ptr = p.point(); frame = p; int oldvalue = 0; oldvalue |= ptr[3]&0xff; oldvalue = oldvalue << 8; oldvalue |= ptr[2]&0xff; oldvalue = oldvalue << 8; oldvalue |= ptr[1]&0xff; oldvalue = oldvalue << 8; oldvalue |= ptr[0]&0xff; int mask; if (((size+spare)>0)&&((size+spare)<=32)) mask = (0x1<<(size+spare))-1; else mask = -1; oldvalue &= ~(mask << (p.offset&0x7));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?