// Encodes the content (included unused bits octet) of the BIT STRING // to the given buffer. AsnLen AsnBits::BEncContent (AsnBuf &b) const { size_t unusedBits; size_t byteLen; unsigned int i; //bool bStop; // IF bitstring is a NamedBitList if (nblFlag) { // Calculate last octet. // size_t finalOctet; if (bitLen <= 8) finalOctet = 0; else if (bitLen % 8 == 0) finalOctet = ((bitLen / 8) - 1); else finalOctet = bitLen / 8; // The final octet is the last octet which has at least // one bit set. Loop backwards starting with the // last octet (calculated above) until you find an // that has at least one bit set (it's value is not 0). // if (bits != NULL) for (; finalOctet > 0 && bits[finalOctet] == 0; finalOctet--); // Calculate unsigned bits in final octet // if (bits == NULL || (finalOctet == 0 && bits[0] == 0)) { byteLen = 0; unusedBits = 0; } else { // Calculate how many trailing bits are unset in the // last octet. unusedBits=0; for (i = 0; i < 8 && ! (bits[finalOctet] & (0x01 << i)); i++) unusedBits++; byteLen = finalOctet + 1; } } else { // If this is not a NamedBitList Calculate the unusedBits // as ( (BitLen() / 8) + 1) * 8 ) - BitLen(); // // In other words it's the number of bits not used by // the BIT STRING specification, not the number of unset // bits in the the final subsequent octet. unusedBits = bitLen % 8; if (unusedBits != 0) unusedBits = 8 - unusedBits; byteLen = (bitLen+7)/8; } b.PutSegRvs (bits, byteLen); // unusedBits = (bitLen % 8); // if (unusedBits != 0) // unusedBits = 8 - unusedBits; b.PutByteRvs ((unsigned char)unusedBits); return byteLen + 1; } /* AsnBits::BEncContent */