@@ -65,42 +65,42 @@ void *pool = objc_autoreleasePoolPush(); OFData *bitStringValue; size_t bitStringLength; @try { - unsigned char lastByteBits; + unsigned char unusedBits; size_t count = DEREncodedContents.count; if (tagClass != OF_ASN1_TAG_CLASS_UNIVERSAL || tagNumber != OF_ASN1_TAG_NUMBER_BIT_STRING || constructed) @throw [OFInvalidArgumentException exception]; if (DEREncodedContents.itemSize != 1 || count == 0) @throw [OFInvalidFormatException exception]; - lastByteBits = + unusedBits = *(unsigned char *)[DEREncodedContents itemAtIndex: 0]; - if (lastByteBits > 7) + if (unusedBits > 7) @throw [OFInvalidFormatException exception]; /* - * Can't have any bits of the last byte used if we have no byte. + * Can't have any bits of the last byte unused if we have no + * byte. */ - if (count == 1 && lastByteBits != 0) + if (count == 1 && unusedBits != 0) @throw [OFInvalidFormatException exception]; - if (SIZE_MAX / 8 < count - 1 || - SIZE_MAX - (count - 1) * 8 < lastByteBits) + if (SIZE_MAX / 8 < count - 1) @throw [OFOutOfRangeException exception]; bitStringLength = (count - 1) * 8; bitStringValue = [[DEREncodedContents subdataWithRange: of_range(1, count - 1)] copy]; - if (lastByteBits != 0) - bitStringLength -= (8 - lastByteBits); + if (unusedBits != 0) + bitStringLength -= unusedBits; } @catch (id e) { [self release]; @throw e; } @@ -125,23 +125,25 @@ } - (OFData *)ASN1DERRepresentation { size_t bitStringValueCount = [_bitStringValue count]; - unsigned char lastByteBits = _bitStringLength % 8; + size_t roundedUpLength = OF_ROUND_UP_POW2(8, _bitStringLength); + unsigned char unusedBits = roundedUpLength - _bitStringLength; unsigned char header[] = { OF_ASN1_TAG_NUMBER_BIT_STRING, bitStringValueCount + 1, - lastByteBits + unusedBits }; OFMutableData *data; if (bitStringValueCount + 1 > UINT8_MAX || - bitStringValueCount != OF_ROUND_UP_POW2(8, _bitStringLength) / 8) + bitStringValueCount != roundedUpLength / 8) @throw [OFInvalidFormatException exception]; - data = [OFMutableData dataWithCapacity: 3 + bitStringValueCount]; + data = [OFMutableData + dataWithCapacity: sizeof(header) + bitStringValueCount]; [data addItems: header count: sizeof(header)]; [data addItems: [_bitStringValue items] count: bitStringValueCount];