︙ | | |
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
73
74
75
|
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
|
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
-
|
if (self == [OFMutableUTF8String class])
[self inheritMethodsFromClass: [OFUTF8String class]];
}
- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
freeWhenDone: (bool)freeWhenDone
{
@try {
self = [self initWithUTF8String: UTF8String];
} @finally {
if (freeWhenDone)
free(UTF8String);
self = [self initWithUTF8String: UTF8String];
if (freeWhenDone)
free(UTF8String);
}
return self;
}
- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
length: (size_t)UTF8StringLength
freeWhenDone: (bool)freeWhenDone
{
@try {
self = [self initWithUTF8String: UTF8String
length: UTF8StringLength];
} @finally {
if (freeWhenDone)
free(UTF8String);
self = [self initWithUTF8String: UTF8String
length: UTF8StringLength];
if (freeWhenDone)
free(UTF8String);
}
return self;
}
- (void)of_convertWithWordStartTable: (const of_unichar_t *const[])startTable
wordMiddleTable: (const of_unichar_t *const[])middleTable
wordStartTableSize: (size_t)startTableSize
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
@throw [OFInvalidEncodingException exception];
}
i += cLen;
}
@try {
newCString = [self allocMemoryWithSize: newCStringLength + 1];
newCString = of_malloc(newCStringLength + 1, 1);
} @catch (id e) {
free(unicodeString);
@throw e;
}
j = 0;
for (i = 0; i < unicodeLen; i++) {
size_t d;
if ((d = of_string_utf8_encode(unicodeString[i],
newCString + j)) == 0) {
free(unicodeString);
[self freeMemory: newCString];
free(newCString);
@throw [OFInvalidEncodingException exception];
}
j += d;
}
assert(j == newCStringLength);
newCString[j] = 0;
free(unicodeString);
[self freeMemory: _s->cString];
free(_s->cString);
_s->hashed = false;
_s->cString = newCString;
_s->cStringLength = newCStringLength;
/*
* Even though cStringLength can change, length cannot, therefore no
* need to change it.
|
︙ | | |
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
-
-
-
+
+
|
@throw [OFInvalidEncodingException exception];
_s->hashed = false;
if (lenNew == (size_t)lenOld)
memcpy(_s->cString + idx, buffer, lenNew);
else if (lenNew > (size_t)lenOld) {
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength -
lenOld + lenNew + 1];
_s->cString = of_realloc(_s->cString,
_s->cStringLength - lenOld + lenNew + 1, 1);
memmove(_s->cString + idx + lenNew, _s->cString + idx + lenOld,
_s->cStringLength - idx - lenOld);
memcpy(_s->cString + idx, buffer, lenNew);
_s->cStringLength -= lenOld;
_s->cStringLength += lenNew;
|
︙ | | |
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
-
+
-
-
+
|
_s->cStringLength += lenNew;
_s->cString[_s->cStringLength] = '\0';
if (character >= 0x80)
_s->isUTF8 = true;
@try {
_s->cString = [self
_s->cString = of_realloc(_s->cString,
resizeMemory: _s->cString
size: _s->cStringLength + 1];
_s->cStringLength + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
}
- (void)appendUTF8String: (const char *)UTF8String
|
︙ | | |
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
-
-
-
+
+
|
_s->isUTF8 = true;
break;
case -1:
@throw [OFInvalidEncodingException exception];
}
_s->hashed = false;
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength +
UTF8StringLength + 1];
_s->cString = of_realloc(_s->cString,
_s->cStringLength + UTF8StringLength + 1, 1);
memcpy(_s->cString + _s->cStringLength, UTF8String,
UTF8StringLength + 1);
_s->cStringLength += UTF8StringLength;
_s->length += length;
}
|
︙ | | |
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
-
-
-
+
+
|
_s->isUTF8 = true;
break;
case -1:
@throw [OFInvalidEncodingException exception];
}
_s->hashed = false;
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength +
UTF8StringLength + 1];
_s->cString = of_realloc(_s->cString,
_s->cStringLength + UTF8StringLength + 1, 1);
memcpy(_s->cString + _s->cStringLength, UTF8String, UTF8StringLength);
_s->cStringLength += UTF8StringLength;
_s->length += length;
_s->cString[_s->cStringLength] = 0;
}
|
︙ | | |
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
-
-
-
+
+
|
if (string == nil)
@throw [OFInvalidArgumentException exception];
UTF8StringLength = string.UTF8StringLength;
_s->hashed = false;
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength +
UTF8StringLength + 1];
_s->cString = of_realloc(_s->cString,
_s->cStringLength + UTF8StringLength + 1, 1);
memcpy(_s->cString + _s->cStringLength, string.UTF8String,
UTF8StringLength);
_s->cStringLength += UTF8StringLength;
_s->length += string.length;
_s->cString[_s->cStringLength] = 0;
|
︙ | | |
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
-
-
+
+
|
j += len;
}
tmp[j] = '\0';
_s->hashed = false;
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength + j + 1];
_s->cString = of_realloc(_s->cString,
_s->cStringLength + j + 1, 1);
memcpy(_s->cString + _s->cStringLength, tmp, j + 1);
_s->cStringLength += j;
_s->length += length;
if (isUTF8)
_s->isUTF8 = true;
|
︙ | | |
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
-
-
+
|
if (_s->isUTF8)
idx = of_string_utf8_get_position(_s->cString, idx,
_s->cStringLength);
newCStringLength = _s->cStringLength + string.UTF8StringLength;
_s->hashed = false;
_s->cString = [self resizeMemory: _s->cString
size: newCStringLength + 1];
_s->cString = of_realloc(_s->cString, newCStringLength + 1, 1);
memmove(_s->cString + idx + string.UTF8StringLength,
_s->cString + idx, _s->cStringLength - idx);
memcpy(_s->cString + idx, string.UTF8String,
string.UTF8StringLength);
_s->cString[newCStringLength] = '\0';
|
︙ | | |
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
-
-
+
|
_s->cStringLength - end);
_s->hashed = false;
_s->length -= range.length;
_s->cStringLength -= end - start;
_s->cString[_s->cStringLength] = 0;
@try {
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength + 1];
_s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
- (void)replaceCharactersInRange: (of_range_t)range
withString: (OFString *)replacement
|
︙ | | |
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
-
-
+
-
-
+
|
* memmove() the rest of the string to the end.
*
* We must not resize the string if the new string is smaller, because
* then we can't memmove() the rest of the string forward as the rest is
* lost due to the resize!
*/
if (newCStringLength > _s->cStringLength)
_s->cString = [self resizeMemory: _s->cString
size: newCStringLength + 1];
_s->cString = of_realloc(_s->cString, newCStringLength + 1, 1);
memmove(_s->cString + start + replacement.UTF8StringLength,
_s->cString + end, _s->cStringLength - end);
memcpy(_s->cString + start, replacement.UTF8String,
replacement.UTF8StringLength);
_s->cString[newCStringLength] = '\0';
/*
* If the new string is smaller, we can safely resize it now as we're
* done with memmove().
*/
if (newCStringLength < _s->cStringLength)
_s->cString = [self resizeMemory: _s->cString
size: newCStringLength + 1];
_s->cString = of_realloc(_s->cString, newCStringLength + 1, 1);
_s->cStringLength = newCStringLength;
_s->length = newLength;
if ([replacement isKindOfClass: [OFUTF8String class]] ||
[replacement isKindOfClass: [OFMutableUTF8String class]]) {
if (((OFMutableUTF8String *)replacement)->_s->isUTF8)
|
︙ | | |
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
|
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
-
-
+
-
-
+
+
-
+
-
-
-
+
+
-
+
-
+
|
last = 0;
for (size_t i = range.location; i <= range.length - searchLength; i++) {
if (memcmp(_s->cString + i, searchString, searchLength) != 0)
continue;
@try {
newCString = [self resizeMemory: newCString
size: newCStringLength +
newCString = of_realloc(newCString,
i - last +
replacementLength + 1];
newCStringLength + i - last + replacementLength + 1,
1);
} @catch (id e) {
[self freeMemory: newCString];
free(newCString);
@throw e;
}
memcpy(newCString + newCStringLength, _s->cString + last,
i - last);
memcpy(newCString + newCStringLength + i - last,
replacementString, replacementLength);
newCStringLength += i - last + replacementLength;
newLength = newLength - string.length + replacement.length;
i += searchLength - 1;
last = i + 1;
}
@try {
newCString = [self resizeMemory: newCString
size: newCStringLength +
_s->cStringLength - last + 1];
newCString = of_realloc(newCString,
newCStringLength + _s->cStringLength - last + 1, 1);
} @catch (id e) {
[self freeMemory: newCString];
free(newCString);
@throw e;
}
memcpy(newCString + newCStringLength, _s->cString + last,
_s->cStringLength - last);
newCStringLength += _s->cStringLength - last;
newCString[newCStringLength] = 0;
[self freeMemory: _s->cString];
free(_s->cString);
_s->hashed = false;
_s->cString = newCString;
_s->cStringLength = newCStringLength;
_s->length = newLength;
if ([replacement isKindOfClass: [OFUTF8String class]] ||
[replacement isKindOfClass: [OFMutableUTF8String class]]) {
|
︙ | | |
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
|
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
|
-
-
+
|
_s->cStringLength -= i;
_s->length -= i;
memmove(_s->cString, _s->cString + i, _s->cStringLength);
_s->cString[_s->cStringLength] = '\0';
@try {
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength + 1];
_s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
- (void)deleteTrailingWhitespaces
{
|
︙ | | |
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
|
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
|
-
-
+
|
d++;
}
_s->cStringLength -= d;
_s->length -= d;
@try {
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength + 1];
_s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
- (void)deleteEnclosingWhitespaces
{
|
︙ | | |
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
|
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
-
-
+
|
_s->cStringLength -= i;
_s->length -= i;
memmove(_s->cString, _s->cString + i, _s->cStringLength);
_s->cString[_s->cStringLength] = '\0';
@try {
_s->cString = [self resizeMemory: _s->cString
size: _s->cStringLength + 1];
_s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1);
} @catch (OFOutOfMemoryException *e) {
/* We don't really care, as we only made it smaller */
}
}
- (void)makeImmutable
{
object_setClass(self, [OFUTF8String class]);
}
@end
|
︙ | | |
82
83
84
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
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
-
-
-
-
-
-
-
-
-
-
|
#endif
struct pre_ivar {
int retainCount;
#if !defined(OF_HAVE_ATOMIC_OPS) && !defined(OF_AMIGAOS)
of_spinlock_t retainCountSpinlock;
#endif
struct pre_mem *firstMem, *lastMem;
};
struct pre_mem {
struct pre_mem *prev, *next;
id owner;
};
#define PRE_IVARS_ALIGN ((sizeof(struct pre_ivar) + \
(OF_BIGGEST_ALIGNMENT - 1)) & ~(OF_BIGGEST_ALIGNMENT - 1))
#define PRE_IVARS ((struct pre_ivar *)(void *)((char *)self - PRE_IVARS_ALIGN))
#define PRE_MEM_ALIGN ((sizeof(struct pre_mem) + \
(OF_BIGGEST_ALIGNMENT - 1)) & ~(OF_BIGGEST_ALIGNMENT - 1))
#define PRE_MEM(mem) ((struct pre_mem *)(void *)((char *)mem - PRE_MEM_ALIGN))
static struct {
Class isa;
} allocFailedException;
uint32_t of_hash_seed;
void *
|
︙ | | |
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
|
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
- (OFString *)description
{
/* Classes containing data should reimplement this! */
return [OFString stringWithFormat: @"<%@>", self.className];
}
- (void *)allocMemoryWithSize: (size_t)size
{
void *pointer;
struct pre_mem *preMem;
if OF_UNLIKELY (size == 0)
return NULL;
if OF_UNLIKELY (size > SIZE_MAX - PRE_IVARS_ALIGN)
@throw [OFOutOfRangeException exception];
if OF_UNLIKELY ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL)
@throw [OFOutOfMemoryException
exceptionWithRequestedSize: size];
preMem = pointer;
preMem->owner = self;
preMem->prev = PRE_IVARS->lastMem;
preMem->next = NULL;
if OF_LIKELY (PRE_IVARS->lastMem != NULL)
PRE_IVARS->lastMem->next = preMem;
if OF_UNLIKELY (PRE_IVARS->firstMem == NULL)
PRE_IVARS->firstMem = preMem;
PRE_IVARS->lastMem = preMem;
return (char *)pointer + PRE_MEM_ALIGN;
}
- (void *)allocMemoryWithSize: (size_t)size
count: (size_t)count
{
if OF_UNLIKELY (count > SIZE_MAX / size)
@throw [OFOutOfRangeException exception];
return [self allocMemoryWithSize: size * count];
}
- (void *)resizeMemory: (void *)pointer
size: (size_t)size
{
void *new;
struct pre_mem *preMem;
if OF_UNLIKELY (pointer == NULL)
return [self allocMemoryWithSize: size];
if OF_UNLIKELY (size == 0) {
[self freeMemory: pointer];
return NULL;
}
if OF_UNLIKELY (PRE_MEM(pointer)->owner != self)
@throw [OFMemoryNotPartOfObjectException
exceptionWithPointer: pointer
object: self];
if OF_UNLIKELY ((new = realloc(PRE_MEM(pointer),
PRE_MEM_ALIGN + size)) == NULL)
@throw [OFOutOfMemoryException
exceptionWithRequestedSize: size];
preMem = new;
if OF_UNLIKELY (preMem != PRE_MEM(pointer)) {
if OF_LIKELY (preMem->prev != NULL)
preMem->prev->next = preMem;
if OF_LIKELY (preMem->next != NULL)
preMem->next->prev = preMem;
if OF_UNLIKELY (PRE_IVARS->firstMem == PRE_MEM(pointer))
PRE_IVARS->firstMem = preMem;
if OF_UNLIKELY (PRE_IVARS->lastMem == PRE_MEM(pointer))
PRE_IVARS->lastMem = preMem;
}
return (char *)new + PRE_MEM_ALIGN;
}
- (void *)resizeMemory: (void *)pointer
size: (size_t)size
count: (size_t)count
{
if OF_UNLIKELY (pointer == NULL)
return [self allocMemoryWithSize: size
count: count];
if OF_UNLIKELY (size == 0 || count == 0) {
[self freeMemory: pointer];
return NULL;
}
if OF_UNLIKELY (count > SIZE_MAX / size)
@throw [OFOutOfRangeException exception];
return [self resizeMemory: pointer
size: size * count];
}
- (void)freeMemory: (void *)pointer
{
if OF_UNLIKELY (pointer == NULL)
return;
if OF_UNLIKELY (PRE_MEM(pointer)->owner != self)
@throw [OFMemoryNotPartOfObjectException
exceptionWithPointer: pointer
object: self];
if OF_LIKELY (PRE_MEM(pointer)->prev != NULL)
PRE_MEM(pointer)->prev->next = PRE_MEM(pointer)->next;
if OF_LIKELY (PRE_MEM(pointer)->next != NULL)
PRE_MEM(pointer)->next->prev = PRE_MEM(pointer)->prev;
if OF_UNLIKELY (PRE_IVARS->firstMem == PRE_MEM(pointer))
PRE_IVARS->firstMem = PRE_MEM(pointer)->next;
if OF_UNLIKELY (PRE_IVARS->lastMem == PRE_MEM(pointer))
PRE_IVARS->lastMem = PRE_MEM(pointer)->prev;
/* To detect double-free */
PRE_MEM(pointer)->owner = nil;
free(PRE_MEM(pointer));
}
- (id)forwardingTargetForSelector: (SEL)selector
{
return nil;
}
- (void)doesNotRecognizeSelector: (SEL)selector
{
|
︙ | | |
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
|
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
[self retain];
return true;
}
- (void)dealloc
{
struct pre_mem *iter;
objc_destructInstance(self);
iter = PRE_IVARS->firstMem;
while (iter != NULL) {
struct pre_mem *next = iter->next;
/*
* We can use owner as a sentinel to prevent exploitation in
* case there is a buffer underflow somewhere.
*/
OF_ENSURE(iter->owner == self);
free(iter);
iter = next;
}
free((char *)self - PRE_IVARS_ALIGN);
}
/* Required to use properties with the Apple runtime */
- (id)copyWithZone: (void *)zone
{
if OF_UNLIKELY (zone != NULL) {
|
︙ | | |
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
|
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
|
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
abort();
}
return [(id)self mutableCopy];
}
/*
* Those are needed as the root class is the superclass of the root class's
* metaclass and thus instance methods can be sent to class objects as well.
* The following are needed as the root class is the superclass of the root
* class's metaclass and thus instance methods can be sent to class objects as
* well.
*/
+ (void *)allocMemoryWithSize: (size_t)size
{
OF_UNRECOGNIZED_SELECTOR
}
+ (void *)allocMemoryWithSize: (size_t)size
count: (size_t)count
{
OF_UNRECOGNIZED_SELECTOR
}
+ (void *)resizeMemory: (void *)pointer
size: (size_t)size
{
OF_UNRECOGNIZED_SELECTOR
}
+ (void *)resizeMemory: (void *)pointer
size: (size_t)size
count: (size_t)count
{
OF_UNRECOGNIZED_SELECTOR
}
+ (void)freeMemory: (void *)pointer
{
OF_UNRECOGNIZED_SELECTOR
}
+ (id)retain
{
return self;
}
+ (id)autorelease
|
︙ | | |
︙ | | |
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
+
|
#ifdef OF_HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#import "OFUTF8String.h"
#import "OFUTF8String+Private.h"
#import "OFArray.h"
#import "OFData.h"
#import "OFMutableUTF8String.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
|
︙ | | |
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
-
-
+
+
|
- (instancetype)init
{
self = [super init];
@try {
_s = &_storage;
_s->cString = [self allocMemoryWithSize: 1];
_s->cString[0] = '\0';
_s->cString = of_calloc(1, 1);
_s->freeWhenDone = true;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
︙ | | |
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
-
+
+
|
memcmp(cString, "\xEF\xBB\xBF", 3) == 0) {
cString += 3;
cStringLength -= 3;
}
_s = &_storage;
_s->cString = [self allocMemoryWithSize: cStringLength + 1];
_s->cString = of_malloc(cStringLength + 1, 1);
_s->cStringLength = cStringLength;
_s->freeWhenDone = true;
if (encoding == OF_STRING_ENCODING_UTF_8 ||
encoding == OF_STRING_ENCODING_ASCII) {
switch (of_string_utf8_check(cString, cStringLength,
&_s->length)) {
case 1:
if (encoding == OF_STRING_ENCODING_ASCII)
|
︙ | | |
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
-
+
-
-
+
-
-
-
-
-
+
+
+
+
+
|
(uint8_t)cString[i], buffer);
if (bytes == 0)
@throw [OFInvalidEncodingException
exception];
_s->cStringLength += bytes - 1;
_s->cString = [self
_s->cString = of_realloc(_s->cString,
resizeMemory: _s->cString
size: _s->cStringLength + 1];
_s->cStringLength + 1, 1);
memcpy(_s->cString + j, buffer, bytes);
j += bytes;
}
_s->cString[_s->cStringLength] = 0;
return self;
}
switch (encoding) {
#define CASE(encoding, var) \
case encoding: \
table = var; \
tableOffset = var##_offset; \
break;
#define CASE(encoding, var) \
case encoding: \
table = var; \
tableOffset = var##_offset; \
break;
#ifdef HAVE_ISO_8859_2
CASE(OF_STRING_ENCODING_ISO_8859_2, of_iso_8859_2_table)
#endif
#ifdef HAVE_ISO_8859_3
CASE(OF_STRING_ENCODING_ISO_8859_3, of_iso_8859_3_table)
#endif
#ifdef HAVE_ISO_8859_15
|
︙ | | |
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
-
+
-
-
+
|
_s->isUTF8 = true;
byteLength = of_string_utf8_encode(unichar, buffer);
if (byteLength == 0)
@throw [OFInvalidEncodingException exception];
_s->cStringLength += byteLength - 1;
_s->cString = [self
_s->cString = of_realloc(_s->cString,
resizeMemory: _s->cString
size: _s->cStringLength + 1];
_s->cStringLength + 1, 1);
memcpy(_s->cString + j, buffer, byteLength);
j += byteLength;
}
_s->cString[_s->cStringLength] = 0;
} @catch (id e) {
|
︙ | | |
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
-
-
-
+
|
break;
case -1:
@throw [OFInvalidEncodingException exception];
}
_s->cString = (char *)UTF8String;
_s->cStringLength = UTF8StringLength;
if (freeWhenDone)
_s->freeWhenDone = UTF8String;
_s->freeWhenDone = freeWhenDone;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
︙ | | |
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
-
+
+
-
+
+
-
+
-
|
[string isKindOfClass: [OFMutableUTF8String class]])
_s->isUTF8 = ((OFUTF8String *)string)->_s->isUTF8;
else
_s->isUTF8 = true;
_s->length = string.length;
_s->cString = [self allocMemoryWithSize: _s->cStringLength + 1];
_s->cString = of_malloc(_s->cStringLength + 1, 1);
memcpy(_s->cString, string.UTF8String, _s->cStringLength + 1);
_s->freeWhenDone = true;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (instancetype)initWithCharacters: (const of_unichar_t *)characters
length: (size_t)length
{
self = [super init];
@try {
size_t j;
_s = &_storage;
_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
_s->cString = of_malloc((length * 4) + 1, 1);
_s->length = length;
_s->freeWhenDone = true;
j = 0;
for (size_t i = 0; i < length; i++) {
size_t len = of_string_utf8_encode(characters[i],
_s->cString + j);
if (len == 0)
@throw [OFInvalidEncodingException exception];
if (len > 1)
_s->isUTF8 = true;
j += len;
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = [self resizeMemory: _s->cString
_s->cString = of_realloc(_s->cString, j + 1, 1);
size: j + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | |
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
-
+
+
|
string++;
length--;
} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
swap = true;
_s = &_storage;
_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
_s->cString = of_malloc((length * 4) + 1, 1);
_s->length = length;
_s->freeWhenDone = true;
j = 0;
for (size_t i = 0; i < length; i++) {
of_unichar_t character =
(swap ? OF_BSWAP16(string[i]) : string[i]);
size_t len;
|
︙ | | |
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
-
+
-
|
j += len;
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = [self resizeMemory: _s->cString
_s->cString = of_realloc(_s->cString, j + 1, 1);
size: j + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | |
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
-
+
+
|
characters++;
length--;
} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
swap = true;
_s = &_storage;
_s->cString = [self allocMemoryWithSize: (length * 4) + 1];
_s->cString = of_malloc((length * 4) + 1, 1);
_s->length = length;
_s->freeWhenDone = true;
j = 0;
for (size_t i = 0; i < length; i++) {
char buffer[4];
size_t len = of_string_utf8_encode(
(swap ? OF_BSWAP32(characters[i]) : characters[i]),
buffer);
|
︙ | | |
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
|
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
-
+
-
|
}
}
_s->cString[j] = '\0';
_s->cStringLength = j;
@try {
_s->cString = [self resizeMemory: _s->cString
_s->cString = of_realloc(_s->cString, j + 1, 1);
size: j + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | |
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
-
+
-
+
-
-
+
+
|
case 1:
_s->isUTF8 = true;
break;
case -1:
@throw [OFInvalidEncodingException exception];
}
_s->cString = [self
_s->cString = of_malloc(cStringLength + 1, 1);
allocMemoryWithSize: cStringLength + 1];
memcpy(_s->cString, tmp, cStringLength + 1);
_s->freeWhenDone = true;
} @finally {
free(tmp);
}
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
if (_s != NULL && _s->freeWhenDone != NULL)
free(_s->freeWhenDone);
if (_s != NULL && _s->freeWhenDone)
free(_s->cString);
[super dealloc];
}
- (size_t)getCString: (char *)cString
maxLength: (size_t)maxLength
encoding: (of_string_encoding_t)encoding
|
︙ | | |
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
|
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
|
-
-
-
+
+
-
-
-
-
-
-
+
+
-
-
+
+
+
-
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
+
-
+
+
-
+
+
-
+
-
+
-
+
-
+
+
+
+
|
objc_autoreleasePoolPop(pool);
return array;
}
- (const of_unichar_t *)characters
{
OFObject *object = [[[OFObject alloc] init] autorelease];
of_unichar_t *ret;
size_t i, j;
of_unichar_t *buffer = of_malloc(_s->length, sizeof(of_unichar_t));
size_t i = 0, j = 0;
ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
count: _s->length];
i = j = 0;
while (i < _s->cStringLength) {
of_unichar_t c;
ssize_t cLen;
cLen = of_string_utf8_decode(_s->cString + i,
_s->cStringLength - i, &c);
if (cLen <= 0 || c > 0x10FFFF)
if (cLen <= 0 || c > 0x10FFFF) {
free(buffer);
@throw [OFInvalidEncodingException exception];
ret[j++] = c;
}
buffer[j++] = c;
i += cLen;
}
return ret;
return [[OFData dataWithItemsNoCopy: buffer
count: _s->length
itemSize: sizeof(of_unichar_t)
freeWhenDone: true] items];
}
- (const of_char32_t *)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder
{
OFObject *object = [[[OFObject alloc] init] autorelease];
of_char32_t *ret;
size_t i, j;
of_char32_t *buffer = of_malloc(_s->length + 1, sizeof(of_char32_t));
size_t i = 0, j = 0;
ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
count: _s->length + 1];
i = j = 0;
while (i < _s->cStringLength) {
of_unichar_t c;
of_char32_t c;
ssize_t cLen;
cLen = of_string_utf8_decode(_s->cString + i,
_s->cStringLength - i, &c);
if (cLen <= 0 || c > 0x10FFFF)
if (cLen <= 0 || c > 0x10FFFF) {
free(buffer);
@throw [OFInvalidEncodingException exception];
}
if (byteOrder != OF_BYTE_ORDER_NATIVE)
ret[j++] = OF_BSWAP32(c);
buffer[j++] = OF_BSWAP32(c);
else
ret[j++] = c;
buffer[j++] = c;
i += cLen;
}
ret[j] = 0;
buffer[j] = 0;
return ret;
return [[OFData dataWithItemsNoCopy: buffer
count: _s->length + 1
itemSize: sizeof(of_char32_t)
freeWhenDone: true] items];
}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
void *pool;
const char *cString = _s->cString;
|
︙ | | |
︙ | | |
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
|
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
|
-
+
-
-
+
|
environment: (OFDictionary *)environment
{
self = [super init];
@try {
void *pool = objc_autoreleasePoolPush();
const char *path;
char **argv;
char **argv, **env = NULL;
_pid = -1;
_readPipe[0] = _writePipe[1] = -1;
if (pipe(_readPipe) != 0 || pipe(_writePipe) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: self.class];
path = [program cStringWithEncoding: [OFLocale encoding]];
[self of_getArgv: &argv
forProgramName: programName
andArguments: arguments];
@try {
char **env = [self
of_environmentForDictionary: environment];
env = [self of_environmentForDictionary: environment];
#ifdef HAVE_POSIX_SPAWNP
posix_spawn_file_actions_t actions;
posix_spawnattr_t attr;
if (posix_spawn_file_actions_init(&actions) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: self.class];
|
︙ | | |
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
+
+
+
-
+
+
+
+
+
|
}
if (_pid == -1)
@throw [OFInitializationFailedException
exceptionWithClass: self.class];
#endif
} @finally {
char **iter;
close(_readPipe[1]);
close(_writePipe[0]);
free(argv);
[self freeMemory: argv];
for (iter = env; *iter != NULL; iter++)
free(*iter);
free(env);
}
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | |
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
-
+
-
-
-
+
-
+
-
+
-
-
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
+
+
+
-
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
+
+
+
|
forProgramName: (OFString *)programName
andArguments: (OFArray *)arguments
{
OFString *const *objects = arguments.objects;
size_t i, count = arguments.count;
of_string_encoding_t encoding;
*argv = [self allocMemoryWithSize: sizeof(char *)
*argv = of_malloc(count + 2, sizeof(char *));
count: count + 2];
encoding = [OFLocale encoding];
(*argv)[0] = (char *)[programName cStringWithEncoding: encoding];
for (i = 0; i < count; i++)
(*argv)[i + 1] =
(char *)[objects[i] cStringWithEncoding: encoding];
(*argv)[i + 1] = NULL;
}
- (char **)of_environmentForDictionary: (OFDictionary *)environment
{
OFEnumerator *keyEnumerator, *objectEnumerator;
char **envp;
size_t i, count;
size_t count;
of_string_encoding_t encoding;
if (environment == nil)
return NULL;
encoding = [OFLocale encoding];
count = environment.count;
envp = [self allocMemoryWithSize: sizeof(char *)
envp = of_calloc(count + 1, sizeof(char *));
count: count + 1];
@try {
keyEnumerator = [environment keyEnumerator];
objectEnumerator = [environment objectEnumerator];
OFEnumerator *keyEnumerator = [environment keyEnumerator];
OFEnumerator *objectEnumerator = [environment objectEnumerator];
for (i = 0; i < count; i++) {
OFString *key;
OFString *object;
size_t keyLen, objectLen;
for (size_t i = 0; i < count; i++) {
OFString *key;
OFString *object;
size_t keyLen, objectLen;
key = [keyEnumerator nextObject];
object = [objectEnumerator nextObject];
key = [keyEnumerator nextObject];
object = [objectEnumerator nextObject];
keyLen = [key cStringLengthWithEncoding: encoding];
objectLen = [object cStringLengthWithEncoding: encoding];
keyLen = [key cStringLengthWithEncoding: encoding];
objectLen = [object
cStringLengthWithEncoding: encoding];
envp[i] = [self allocMemoryWithSize: keyLen + objectLen + 2];
envp[i] = of_malloc(keyLen + objectLen + 2, 1);
memcpy(envp[i],
memcpy(envp[i], [key cStringWithEncoding: encoding], keyLen);
envp[i][keyLen] = '=';
memcpy(envp[i] + keyLen + 1,
[object cStringWithEncoding: encoding], objectLen);
envp[i][keyLen + objectLen + 1] = '\0';
}
[key cStringWithEncoding: encoding], keyLen);
envp[i][keyLen] = '=';
memcpy(envp[i] + keyLen + 1,
[object cStringWithEncoding: encoding], objectLen);
envp[i][keyLen + objectLen + 1] = '\0';
}
} @catch (id e) {
for (size_t i = 0; i < count; i++)
free(envp[i]);
free(envp);
envp[i] = NULL;
@throw e;
}
return envp;
}
- (bool)lowlevelIsAtEndOfStream
{
if (_readPipe[0] == -1)
|
︙ | | |