AsnLen AsnBits::EncodeWithSizeConstraint (AsnBufBits &b)const { AsnLen len = 0; int numSizeConstraints; const SizeConstraint* sizeConstraints = SizeConstraints(numSizeConstraints); int iSCLowerBound = sizeConstraints[0].lowerBound; int iSCUpperBound = iSCLowerBound; int minBitsNeeded = 0; int minBytesNeeded = 0; long Range = FindSizeConstraintBounds(iSCLowerBound, iSCUpperBound); long tempRange = Range - 1; long size = bitLen; unsigned char* temp = NULL; long tempLength = 0; unsigned char* pStr = new unsigned char[1]; while(tempRange > 0) { tempRange -= (long)(1 << minBitsNeeded); minBitsNeeded += 1; } if(Range > 1) { if( (iSCUpperBound > 16) && b.IsAligned()) { len += b.OctetAlignWrite(); } if(size > iSCUpperBound) { size = iSCUpperBound; } if(size < iSCLowerBound) { size = iSCLowerBound; } minBytesNeeded = minBitsNeeded / 8; minBitsNeeded = minBitsNeeded % 8; size -= iSCLowerBound; if(minBytesNeeded > 0) { pStr[0] = (unsigned char)(size >> minBitsNeeded); len += b.PutBits(pStr, 8); } pStr[0] = (unsigned char)size; pStr[0] <<= 8 - minBitsNeeded; len += b.PutBits(pStr, minBitsNeeded); }
AsnLen AsnString::EncodeWithSizeConstraint(AsnBufBits &b)const { FUNC("AsnString::EncodeWithSizeConstraint"); AsnLen len = 0; int B = numBits(); int B2 = findB2(B); int numSizeConstraints; const SizeConstraint* sizeConstraints = SizeConstraints(numSizeConstraints); int iSCLowerBound = sizeConstraints[0].lowerBound; int iSCUpperBound = iSCLowerBound; int minBitsNeeded = 0; int minBytesNeeded = 0; long Range = FindSizeConstraintBounds(iSCLowerBound, iSCUpperBound); long tempRange = Range - 1; long size = length(); long count = 0; unsigned char* pStr = new unsigned char[1]; while(tempRange > 0) { tempRange -= (long)(1 << minBitsNeeded); minBitsNeeded += 1; } if(size < iSCLowerBound || size > iSCUpperBound) { delete [] pStr; throw EXCEPT("String size not withing restricted bounds", RESTRICTED_TYPE_ERROR); } if(Range > 1) { if( ((iSCUpperBound * B2) > 16) && b.IsAligned()) { len += b.OctetAlignWrite(); } minBytesNeeded = minBitsNeeded / 8; minBitsNeeded = minBitsNeeded % 8; size -= iSCLowerBound; if(minBytesNeeded > 0) { pStr[0] = (unsigned char)(size >> minBitsNeeded); len += b.PutBits(pStr, 8); } pStr[0] = (unsigned char)size; pStr[0] <<= 8 - minBitsNeeded; len += b.PutBits(pStr, minBitsNeeded); }
void AsnAny::PDec(AsnBufBits &b, AsnLen &bitsDecoded) { AsnBufBits tmpBufBits; AsnOcts tmpAnyLoadOcts; AsnLen tmpBitsDecoded=0; FUNC("AsnAny::PDec"); // ai will be NULL if this is an ANY (not an ANY DEFINED BY) if (ai != NULL) { // the type is already known clone it and use it's BDec to decode the // ASN.1 // value = ai->typeToClone->Clone(); if (value == NULL) { throw SnaccException(STACK_ENTRY, "typeToClone->Clone() failed", INVALID_ANY); } // IF value == NULL else { tmpAnyLoadOcts.PDec(b, bitsDecoded); // OUTER OctetString // OUTER "bitsDecoded" returned to caller. if (tmpAnyLoadOcts.length()) { tmpBufBits.PutBits((unsigned char *)tmpAnyLoadOcts.c_ustr(), tmpAnyLoadOcts.length()*8); value->PDec(tmpBufBits, tmpBitsDecoded); // DECODE actual known value. } // END IF tmpBitsDecoded } // END IF value == NULL } // IF ai != NULL else // JUST load BLOB of data in "anyBuf" { tmpAnyLoadOcts.PDec(b, bitsDecoded); // OUTER OctetString // OUTER "bitsDecoded" returned to caller. if (tmpAnyLoadOcts.length()) { if(this->anyBuf) delete this->anyBuf; this->anyBuf = new AsnBuf((char *)tmpAnyLoadOcts.c_str(), tmpAnyLoadOcts.length()); } // END IF any data in ANY. } // END IF ai != NULL } // END AsnAny::PDec(...)
AsnLen AsnBits::EncodeGeneral(AsnBufBits &b)const { AsnLen len = 0; unsigned long l_64kFrag = l_16k * 4; unsigned long count = 0; unsigned long x = 0; unsigned long tempLen = encLen(); unsigned char ch = 0x00; unsigned char *c = NULL; long offset = 0; if(tempLen >= l_16k) { /*there is more than 16k bits of data*/ count = (tempLen / l_64kFrag); for(x=0; x < count; x++) { len += b.OctetAlignWrite(); len += PEncLen_16kFragment(b, 4); len += b.OctetAlignWrite(); len += b.PutBits((unsigned char*)&bits[offset / 8], l_64kFrag); offset += l_64kFrag; } tempLen -= count * l_64kFrag; count = tempLen / l_16k; if(count != 0) { len += b.OctetAlignWrite(); len += PEncLen_16kFragment(b, count); len += b.OctetAlignWrite(); len += b.PutBits((unsigned char*)&bits[offset / 8], (count * l_16k) ); offset += (count * l_16k); } tempLen -= (l_16k * count); if(tempLen == 0) { ch = 0x00; c = &ch; len += b.OctetAlignWrite(); len += b.PutBits(c, 8); return len; } } /*if there are less than 128 bits of data*/ if(tempLen < 128) { len += b.OctetAlignWrite(); len += PEncDefLenTo127(b, tempLen); len += b.OctetAlignWrite(); len += b.PutBits((unsigned char*)&bits[offset / 8], tempLen); offset += tempLen; } else if(tempLen >= 128 && tempLen < l_16k) { len += b.OctetAlignWrite(); /*if there is less than 16k bits of data*/ /*and more than 127 bits of data*/ len += PEncLen_1to16k(b, tempLen); len += b.OctetAlignWrite(); len += b.PutBits((unsigned char*)&bits[offset / 8], tempLen); offset += tempLen; } return len; }
_BEGIN_SNACC_NAMESPACE AsnLen PERGeneral::EncodeGeneral(AsnBufBits &b)const { AsnLen len = 0; unsigned long l_64kFrag = l_16k * 4; unsigned long count = 0; unsigned long x = 0; unsigned long y = 0; unsigned long tempLen = lEncLen(); unsigned char ch = 0x00; unsigned char *c = NULL; long offset = 0; if(tempLen >= l_16k) { /*there is more than 16k bytes of data*/ count = (tempLen / l_64kFrag); for(x=0; x < count; x++) { len += b.OctetAlignWrite(); len += PEncLen_16kFragment(b, 4); len += b.OctetAlignWrite(); for(y = 0; y < l_64kFrag; y++) { len += Interpret(b, offset); offset++; } } tempLen -= count * l_64kFrag; count = tempLen / l_16k; if(count != 0) { len += b.OctetAlignWrite(); len += PEncLen_16kFragment(b, count); len += b.OctetAlignWrite(); for(y = 0; y < (l_16k * count); y++) { len += Interpret(b, offset); offset++; } } tempLen -= (l_16k * count); if(tempLen == 0) { ch = 0x00; c = &ch; len += b.OctetAlignWrite(); len += b.PutBits(c, 8); return len; } } /*if there are less than 128 bytes of data*/ if(tempLen < 128) { len += b.OctetAlignWrite(); len += PEncDefLenTo127(b, tempLen); len += b.OctetAlignWrite(); for(y = 0; y < tempLen; y++) { len += Interpret(b, offset); offset++; } } else if(tempLen >= 128 && tempLen < l_16k) { len += b.OctetAlignWrite(); /*if there is less than 16k bytes of data*/ /*and more than 127 bytes of data*/ len += PEncLen_1to16k(b, tempLen); len += b.OctetAlignWrite(); for(y = 0; y < tempLen; y++) { len += Interpret(b, offset); offset++; } } return len; }