ObjFW  Diff

Differences From Artifact [843d69d324]:

To Artifact [d3bbe5638b]:


23
24
25
26
27
28
29
30
31


32
33

34
35

36
37
38

39
40
41
42
43
44
45


46
47
48
49


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


66
67
68
69
70
71
72
23
24
25
26
27
28
29


30
31
32

33


34
35
36

37

38
39
40
41


42
43
44
45


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61


62
63
64
65
66
67
68
69
70







-
-
+
+

-
+
-
-
+


-
+
-




-
-
+
+


-
-
+
+














-
-
+
+







#import "OFInvalidFormatException.h"
#import "OFOutOfRangeException.h"

@implementation OFASN1BitString
@synthesize bitStringValue = _bitStringValue;
@synthesize bitStringLength = _bitStringLength;

+ (instancetype)bitStringWithBitStringValue: (OFData *)bitStringValue
			    bitStringLength: (size_t)bitStringLength
+ (instancetype)bitStringWithBitString: (OFData *)bitString
				length: (size_t)length
{
	return [[[self alloc]
	return [[[self alloc] initWithBitString: bitString
	    initWithBitStringValue: bitStringValue
		   bitStringLength: bitStringLength] autorelease];
					 length: length] autorelease];
}

- (instancetype)initWithBitStringValue: (OFData *)bitStringValue
- (instancetype)initWithBitString: (OFData *)bitString length: (size_t)length
		       bitStringLength: (size_t)bitStringLength
{
	self = [super init];

	@try {
		if (bitStringValue.count * bitStringValue.itemSize !=
		    OF_ROUND_UP_POW2(8, bitStringLength) / 8)
		if (bitString.count * bitString.itemSize !=
		    OF_ROUND_UP_POW2(8, length) / 8)
			@throw [OFInvalidFormatException exception];

		_bitStringValue = [bitStringValue copy];
		_bitStringLength = bitStringLength;
		_bitStringValue = [bitString copy];
		_bitStringLength = length;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)initWithTagClass: (of_asn1_tag_class_t)tagClass
		       tagNumber: (of_asn1_tag_number_t)tagNumber
		     constructed: (bool)constructed
	      DEREncodedContents: (OFData *)DEREncodedContents
{
	void *pool = objc_autoreleasePoolPush();
	OFData *bitStringValue;
	size_t bitStringLength;
	OFData *bitString;
	size_t length;

	@try {
		unsigned char unusedBits;
		size_t count = DEREncodedContents.count;

		if (tagClass != OF_ASN1_TAG_CLASS_UNIVERSAL ||
		    tagNumber != OF_ASN1_TAG_NUMBER_BIT_STRING || constructed)
87
88
89
90
91
92
93
94
95


96
97
98
99

100
101
102
103
104
105

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

145
146
147
148
149
150
151
85
86
87
88
89
90
91


92
93
94
95
96

97
98
99
100
101
102

103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

141
142
143
144
145
146
147
148







-
-
+
+



-
+





-
+
-




















-
+
















-
+







		 */
		if (count == 1 && unusedBits != 0)
			@throw [OFInvalidFormatException exception];

		if (SIZE_MAX / 8 < count - 1)
			@throw [OFOutOfRangeException exception];

		bitStringLength = (count - 1) * 8;
		bitStringValue = [DEREncodedContents subdataWithRange:
		length = (count - 1) * 8;
		bitString = [DEREncodedContents subdataWithRange:
		    of_range(1, count - 1)];

		if (unusedBits != 0)
			bitStringLength -= unusedBits;
			length -= unusedBits;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithBitStringValue: bitStringValue
	self = [self initWithBitString: bitString length: length];
			    bitStringLength: bitStringLength];

	objc_autoreleasePoolPop(pool);

	return self;
}

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (void)dealloc
{
	[_bitStringValue release];

	[super dealloc];
}

- (OFData *)ASN1DERRepresentation
{
	size_t bitStringValueCount = [_bitStringValue count];
	size_t bitStringValueCount = _bitStringValue.count;
	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,
		unusedBits
	};
	OFMutableData *data;

	if (bitStringValueCount + 1 > UINT8_MAX ||
	    bitStringValueCount != roundedUpLength / 8)
		@throw [OFInvalidFormatException exception];

	data = [OFMutableData
	    dataWithCapacity: sizeof(header) + bitStringValueCount];
	[data addItems: header count: sizeof(header)];
	[data addItems: [_bitStringValue items] count: bitStringValueCount];
	[data addItems: _bitStringValue.items count: bitStringValueCount];

	[data makeImmutable];

	return data;
}

- (bool)isEqual: (id)object