ObjFW  Check-in [a7ef90581c]

Overview
Comment:OFINIFile: Fix parsing `=` within `"`
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 1.1
Files: files | file ages | folders
SHA3-256: a7ef90581c5bc8af616adea15ea2e130971d0dbf302d02c24ea7ba75f5564ab7
User & Date: js on 2024-08-25 21:29:28
Other Links: branch diff | manifest | tags
Context
2024-08-25
21:35
Update ChangeLog for 1.1.7 check-in: 52e6f4b604 user: js tags: 1.1
21:29
OFINIFile: Fix parsing `=` within `"` check-in: a7ef90581c user: js tags: 1.1
21:27
OFINIFile: Fix parsing `=` within `"` check-in: 95fcd0f05b user: js tags: trunk
2024-08-24
23:43
Don't assume all custom string classes use Unicode check-in: cacfcf1b9d user: js tags: 1.1
Changes

Modified src/OFINICategory.m from [3ff03f8457] to [06ae24a12e].

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{
	OFMutableString *mutableString;

	/* FIXME: Optimize */
	if (![string hasPrefix: @" "] && ![string hasPrefix: @"\t"] &&
	    ![string hasPrefix: @"\f"] && ![string hasSuffix: @" "] &&
	    ![string hasSuffix: @"\t"] && ![string hasSuffix: @"\f"] &&
	    ![string containsString: @"\""])
		return string;

	mutableString = [[string mutableCopy] autorelease];

	[mutableString replaceOccurrencesOfString: @"\\" withString: @"\\\\"];
	[mutableString replaceOccurrencesOfString: @"\f" withString: @"\\f"];
	[mutableString replaceOccurrencesOfString: @"\r" withString: @"\\r"];
	[mutableString replaceOccurrencesOfString: @"\n" withString: @"\\n"];
	[mutableString replaceOccurrencesOfString: @"\"" withString: @"\\\""];

	[mutableString insertString: @"\"" atIndex: 0];
	[mutableString appendString: @"\""];

	[mutableString makeImmutable];

	return mutableString;
}

static OFString *
unescapeString(OFString *string)
{
	OFMutableString *mutableString;

	if (![string hasPrefix: @"\""] || ![string hasSuffix: @"\""])
		return string;

	string = [string substringWithRange: OFMakeRange(1, string.length - 2)];
	mutableString = [[string mutableCopy] autorelease];

	[mutableString replaceOccurrencesOfString: @"\\f" withString: @"\f"];
	[mutableString replaceOccurrencesOfString: @"\\r" withString: @"\r"];
	[mutableString replaceOccurrencesOfString: @"\\n" withString: @"\n"];
	[mutableString replaceOccurrencesOfString: @"\\\"" withString: @"\""];
	[mutableString replaceOccurrencesOfString: @"\\\\" withString: @"\\"];

	[mutableString makeImmutable];

	return mutableString;
}

@implementation OFINICategoryPair
- (void)dealloc







|













<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







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
73
74
{
	OFMutableString *mutableString;

	/* FIXME: Optimize */
	if (![string hasPrefix: @" "] && ![string hasPrefix: @"\t"] &&
	    ![string hasPrefix: @"\f"] && ![string hasSuffix: @" "] &&
	    ![string hasSuffix: @"\t"] && ![string hasSuffix: @"\f"] &&
	    ![string containsString: @"\""] && ![string containsString: @"="])
		return string;

	mutableString = [[string mutableCopy] autorelease];

	[mutableString replaceOccurrencesOfString: @"\\" withString: @"\\\\"];
	[mutableString replaceOccurrencesOfString: @"\f" withString: @"\\f"];
	[mutableString replaceOccurrencesOfString: @"\r" withString: @"\\r"];
	[mutableString replaceOccurrencesOfString: @"\n" withString: @"\\n"];
	[mutableString replaceOccurrencesOfString: @"\"" withString: @"\\\""];

	[mutableString insertString: @"\"" atIndex: 0];
	[mutableString appendString: @"\""];























	[mutableString makeImmutable];

	return mutableString;
}

@implementation OFINICategoryPair
- (void)dealloc
147
148
149
150
151
152
153








































154
155
156
157
158

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

183






























































184
185
186
187
188
189
190
- (void)dealloc
{
	[_name release];
	[_lines release];

	[super dealloc];
}









































- (void)of_parseLine: (OFString *)line
{
	if (![line hasPrefix: @";"] && ![line hasPrefix: @"#"]) {
		OFINICategoryPair *pair;

		OFString *key, *value;
		size_t pos;

		pair = [[[OFINICategoryPair alloc] init] autorelease];

		if ((pos = [line rangeOfString: @"="].location) == OFNotFound)
			@throw [OFInvalidFormatException exception];

		key = unescapeString([line substringToIndex: pos]
		    .stringByDeletingEnclosingWhitespaces);
		value = unescapeString([line substringFromIndex: pos + 1]
		    .stringByDeletingEnclosingWhitespaces);

		pair->_key = [key copy];
		pair->_value = [value copy];

		[_lines addObject: pair];
	} else {
		OFINICategoryComment *comment =
		    [[[OFINICategoryComment alloc] init] autorelease];

		comment->_comment = [line copy];

		[_lines addObject: comment];

	}






























































}

- (OFString *)stringValueForKey: (OFString *)key
{
	return [self stringValueForKey: key defaultValue: nil];
}








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|
|
>
|
|
|
|

<
<
|
|
<
<
<

<
<
|
<
<


<

<

>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182


183
184



185


186


187
188

189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
- (void)dealloc
{
	[_name release];
	[_lines release];

	[super dealloc];
}

static void
parseQuoted(const char **cString, const char **start, size_t *length)
{
	bool inEscape = false;

	(*cString)++;
	*start = *cString;

	while (**cString != '\0') {
		if (inEscape)
			inEscape = false;
		else {
			if (**cString == '\\')
				inEscape = true;
			else if (**cString == '"')
				break;
		}
		(*cString)++;
	}
	if (**cString == '\0')
		@throw [OFInvalidFormatException exception];

	*length = *cString - *start;

	(*cString)++;

	while (OFASCIIIsSpace(**cString))
		(*cString)++;
}

static void
unescapeMutableString(OFMutableString *string)
{
	[string replaceOccurrencesOfString: @"\\f" withString: @"\f"];
	[string replaceOccurrencesOfString: @"\\r" withString: @"\r"];
	[string replaceOccurrencesOfString: @"\\n" withString: @"\n"];
	[string replaceOccurrencesOfString: @"\\\"" withString: @"\""];
	[string replaceOccurrencesOfString: @"\\\\" withString: @"\\"];
}

- (void)of_parseLine: (OFString *)line
{
	void *pool = objc_autoreleasePoolPush();
	const char *cString = line.UTF8String;
	bool keyIsQuoted = false, valueIsQuoted = false;
	const char *keyStart, *valueStart;
	size_t keyLength, valueLength;
	OFMutableString *key, *value;
	OFINICategoryPair *pair;



	while (OFASCIIIsSpace(*cString))
		cString++;






	if (*cString == ';' || *cString == '#') {


		OFINICategoryComment *comment =
		    [[[OFINICategoryComment alloc] init] autorelease];

		comment->_comment = [line copy];

		[_lines addObject: comment];
		return;
	}

	if (*cString == '"') {
		keyIsQuoted = true;
		parseQuoted(&cString, &keyStart, &keyLength);
	} else {
		keyStart = cString;

		while (*cString != '=' && *cString != '\0')
			cString++;

		keyLength = cString - keyStart;
	}

	if (*cString != '=')
		@throw [OFInvalidFormatException exception];
	cString++;

	while (OFASCIIIsSpace(*cString))
		cString++;

	if (*cString == '"') {
		valueIsQuoted = true;
		parseQuoted(&cString, &valueStart, &valueLength);
	} else {
		valueStart = cString;

		while (*cString != '\0')
			cString++;

		valueLength = cString - valueStart;
	}

	while (*cString != '\0') {
		if (!OFASCIIIsSpace(*cString))
			@throw [OFInvalidFormatException exception];

		cString++;
	}

	key = [OFMutableString stringWithUTF8String: keyStart
					     length: keyLength];
	value = [OFMutableString stringWithUTF8String: valueStart
					       length: valueLength];

	if (keyIsQuoted)
		unescapeMutableString(key);
	else
		[key deleteEnclosingWhitespaces];
	if (valueIsQuoted)
		unescapeMutableString(value);
	else
		[value deleteEnclosingWhitespaces];

	[key makeImmutable];
	[value makeImmutable];

	pair = [[[OFINICategoryPair alloc] init] autorelease];
	pair->_key = [key copy];
	pair->_value = [value copy];
	[_lines addObject: pair];

	objc_autoreleasePoolPop(pool);
}

- (OFString *)stringValueForKey: (OFString *)key
{
	return [self stringValueForKey: key defaultValue: nil];
}

Modified tests/OFINIFileTests.m from [33885c1876] to [80cce0e553].

121
122
123
124
125
126
127

128
129
130
131
132
133
134
	    @"new=new\r\n"
	    @"\r\n"
	    @"[foobar]\r\n"
	    @"#foobarcomment\r\n"
	    @"qux=\" asd\"\r\n"
	    @"quxquxqux=\"hello\\\"wörld\"\r\n"
	    @"qux2=\"a\\f\"\r\n"

	    @"qux3=a\fb\r\n"
	    @"\r\n"
	    @"[types]\r\n"
	    @"integer=16\r\n"
	    @"bool=false\r\n"
	    @"float=0.25\r\n"
	    @"array1=foo\r\n"







>







121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	    @"new=new\r\n"
	    @"\r\n"
	    @"[foobar]\r\n"
	    @"#foobarcomment\r\n"
	    @"qux=\" asd\"\r\n"
	    @"quxquxqux=\"hello\\\"wörld\"\r\n"
	    @"qux2=\"a\\f\"\r\n"
	    @"\"asd=asd\"=foobar\r\n"
	    @"qux3=a\fb\r\n"
	    @"\r\n"
	    @"[types]\r\n"
	    @"integer=16\r\n"
	    @"bool=false\r\n"
	    @"float=0.25\r\n"
	    @"array1=foo\r\n"

Modified tests/testfile.ini from [01d8ae7a98] to [f3a28cbc21].

8
9
10
11
12
13
14

15
16
17
18
19
20
21

[foobar]
#foobarcomment
qux=" asd"
"quxqux " = asd
quxquxqux="hello\"wörld"
qux2="a\f"


[types]
integer = 0x20
bool = true
float = 0.5
array1 = 1
array2 = 1







>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

[foobar]
#foobarcomment
qux=" asd"
"quxqux " = asd
quxquxqux="hello\"wörld"
qux2="a\f"
"asd=asd"=foobar

[types]
integer = 0x20
bool = true
float = 0.5
array1 = 1
array2 = 1