long AsnBits::FindSizeConstraintBounds(int &iSCLowerBound, int &iSCUpperBound)const { int count = 0; int numsizeconstraints; const SizeConstraint* sizeConstraints = SizeConstraints(numsizeconstraints); while(count < numsizeconstraints) { if((unsigned)iSCUpperBound < sizeConstraints[count].lowerBound) { iSCUpperBound = sizeConstraints[count].lowerBound; } if( sizeConstraints[count].upperBoundExists == 1 && (unsigned)iSCUpperBound < sizeConstraints[count].upperBound) { iSCUpperBound = sizeConstraints[count].upperBound; } if( (unsigned)iSCLowerBound > sizeConstraints[count].lowerBound ) { iSCLowerBound = sizeConstraints[count].lowerBound; } count++; } return ( (iSCUpperBound - iSCLowerBound) + 1); }
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); }
SizeConstraints RotSizeConstraints(const SizeConstraints &sc, AXIS axis) { return axis == AXIS_X ? sc : SizeConstraints(sc.available_h, sc.available_w); }