︙ | | |
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
-
+
-
|
_s->cString[i] = t;
}
return;
}
unicodeLen = self.length;
unicodeString = [self allocMemoryWithSize: sizeof(of_unichar_t)
unicodeString = of_malloc(unicodeLen, sizeof(of_unichar_t));
count: unicodeLen];
i = j = 0;
newCStringLength = 0;
while (i < _s->cStringLength) {
const of_unichar_t *const *table;
size_t tableSize;
|
︙ | | |
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
-
+
|
tableSize = middleTableSize;
}
cLen = of_string_utf8_decode(_s->cString + i,
_s->cStringLength - i, &c);
if (cLen <= 0 || c > 0x10FFFF) {
[self freeMemory: unicodeString];
free(unicodeString);
@throw [OFInvalidEncodingException exception];
}
isStart = of_ascii_isspace(c);
if (c >> 8 < tableSize) {
of_unichar_t tc = table[c >> 8][c & 0xFF];
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
else if (c < 0x800)
newCStringLength += 2;
else if (c < 0x10000)
newCStringLength += 3;
else if (c < 0x110000)
newCStringLength += 4;
else {
[self freeMemory: unicodeString];
free(unicodeString);
@throw [OFInvalidEncodingException exception];
}
i += cLen;
}
@try {
newCString = [self allocMemoryWithSize: newCStringLength + 1];
} @catch (id e) {
[self freeMemory: unicodeString];
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) {
[self freeMemory: unicodeString];
free(unicodeString);
[self freeMemory: newCString];
@throw [OFInvalidEncodingException exception];
}
j += d;
}
assert(j == newCStringLength);
newCString[j] = 0;
[self freeMemory: unicodeString];
free(unicodeString);
[self freeMemory: _s->cString];
_s->hashed = false;
_s->cString = newCString;
_s->cStringLength = newCStringLength;
/*
|
︙ | | |
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
-
+
-
|
} else
_s->isUTF8 = true;
}
- (void)appendCharacters: (const of_unichar_t *)characters
length: (size_t)length
{
char *tmp;
char *tmp = of_malloc((length * 4) + 1, 1);
tmp = [self allocMemoryWithSize: (length * 4) + 1];
@try {
size_t j = 0;
bool isUTF8 = false;
for (size_t i = 0; i < length; i++) {
size_t len = of_string_utf8_encode(characters[i],
tmp + j);
|
︙ | | |
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
-
+
|
_s->cStringLength += j;
_s->length += length;
if (isUTF8)
_s->isUTF8 = true;
} @finally {
[self freeMemory: tmp];
free(tmp);
}
}
- (void)appendFormat: (OFConstantString *)format
arguments: (va_list)arguments
{
char *UTF8String;
|
︙ | | |
︙ | | |
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
|
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
+
+
|
return ret;
}
- (OFData *)readDataUntilEndOfStream
{
OFMutableData *data = [OFMutableData data];
size_t pageSize = [OFSystemInfo pageSize];
char *buffer = [self allocMemoryWithSize: pageSize];
char *buffer = of_malloc(1, pageSize);
@try {
while (!self.atEndOfStream) {
size_t length;
length = [self readIntoBuffer: buffer
length: pageSize];
[data addItems: buffer
count: length];
}
} @finally {
[self freeMemory: buffer];
free(buffer);
}
[data makeImmutable];
return data;
}
- (OFString *)readStringWithLength: (size_t)length
{
return [self readStringWithLength: length
encoding: OF_STRING_ENCODING_UTF_8];
}
- (OFString *)readStringWithLength: (size_t)length
encoding: (of_string_encoding_t)encoding
{
OFString *ret;
char *buffer = [self allocMemoryWithSize: length + 1];
char *buffer = of_malloc(length + 1, 1);
buffer[length] = 0;
@try {
[self readIntoBuffer: buffer
exactLength: length];
ret = [OFString stringWithCString: buffer
encoding: encoding];
} @finally {
[self freeMemory: buffer];
free(buffer);
}
return ret;
}
- (OFString *)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
size_t pageSize, bufferLength, retLength;
char *retCString, *buffer, *readBuffer;
size_t pageSize, bufferLength;
char *buffer, *readBuffer;
OFString *ret;
/* Look if there's a line or \0 in our buffer */
if (!_waitingForDelimiter && _readBuffer != NULL) {
for (size_t i = 0; i < _readBufferLength; i++) {
if OF_UNLIKELY (_readBuffer[i] == '\n' ||
_readBuffer[i] == '\0') {
retLength = i;
size_t retLength = i;
if (i > 0 && _readBuffer[i - 1] == '\r')
retLength--;
ret = [OFString stringWithCString: _readBuffer
encoding: encoding
length: retLength];
_readBuffer += i + 1;
_readBufferLength -= i + 1;
_waitingForDelimiter = false;
return ret;
}
}
}
/* Read and see if we got a newline or \0 */
pageSize = [OFSystemInfo pageSize];
buffer = [self allocMemoryWithSize: pageSize];
buffer = of_malloc(1, pageSize);
@try {
if ([self lowlevelIsAtEndOfStream]) {
size_t retLength;
if (_readBuffer == NULL) {
_waitingForDelimiter = false;
return nil;
}
retLength = _readBufferLength;
|
︙ | | |
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
|
-
-
+
+
-
-
-
-
-
+
-
+
|
bufferLength = [self lowlevelReadIntoBuffer: buffer
length: pageSize];
/* Look if there's a newline or \0 */
for (size_t i = 0; i < bufferLength; i++) {
if OF_UNLIKELY (buffer[i] == '\n' ||
buffer[i] == '\0') {
retLength = _readBufferLength + i;
retCString = [self
size_t retLength = _readBufferLength + i;
char *retCString = of_malloc(retLength, 1);
allocMemoryWithSize: retLength];
if (_readBuffer != NULL)
memcpy(retCString, _readBuffer,
_readBufferLength);
memcpy(retCString + _readBufferLength,
buffer, i);
if (retLength > 0 &&
retCString[retLength - 1] == '\r')
retLength--;
@try {
char *rcs = retCString;
size_t rl = retLength;
ret = [OFString
stringWithCString: rcs
stringWithCString: retCString
encoding: encoding
length: rl];
length: retLength];
} @catch (id e) {
if (bufferLength > 0) {
/*
* Append data to _readBuffer
* to prevent loss of data.
*/
readBuffer = [self
|
︙ | | |
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
|
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
|
-
+
|
_readBufferMemory = readBuffer;
_readBufferLength +=
bufferLength;
}
@throw e;
} @finally {
[self freeMemory: retCString];
free(retCString);
}
readBuffer = [self
allocMemoryWithSize: bufferLength - i - 1];
if (readBuffer != NULL)
memcpy(readBuffer, buffer + i + 1,
bufferLength - i - 1);
|
︙ | | |
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
|
-
+
|
buffer, bufferLength);
[self freeMemory: _readBufferMemory];
_readBuffer = _readBufferMemory = readBuffer;
_readBufferLength += bufferLength;
}
} @finally {
[self freeMemory: buffer];
free(buffer);
}
_waitingForDelimiter = true;
return nil;
}
- (OFString *)readLine
|
︙ | | |
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
|
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
|
-
-
+
+
|
return [self tryReadLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}
- (OFString *)tryReadTillDelimiter: (OFString *)delimiter
encoding: (of_string_encoding_t)encoding
{
const char *delimiterCString;
size_t j, delimiterLength, pageSize, bufferLength, retLength;
char *retCString, *buffer, *readBuffer;
size_t j, delimiterLength, pageSize, bufferLength;
char *buffer, *readBuffer;
OFString *ret;
delimiterCString = [delimiter cStringWithEncoding: encoding];
delimiterLength = [delimiter cStringLengthWithEncoding: encoding];
j = 0;
if (delimiterLength == 0)
|
︙ | | |
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
|
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
-
+
|
return ret;
}
}
}
/* Read and see if we got a delimiter or \0 */
pageSize = [OFSystemInfo pageSize];
buffer = [self allocMemoryWithSize: pageSize];
buffer = of_malloc(1, pageSize);
@try {
if ([self lowlevelIsAtEndOfStream]) {
if (_readBuffer == NULL) {
_waitingForDelimiter = false;
return nil;
}
|
︙ | | |
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
|
+
+
+
-
+
-
-
-
-
-
+
-
+
|
/* Look if there's a delimiter or \0 */
for (size_t i = 0; i < bufferLength; i++) {
if (buffer[i] != delimiterCString[j++])
j = 0;
if (j == delimiterLength || buffer[i] == '\0') {
size_t retLength;
char *retCString;
if (buffer[i] == '\0')
delimiterLength = 1;
retLength = _readBufferLength + i + 1 -
delimiterLength;
retCString = [self
retCString = of_malloc(retLength, 1);
allocMemoryWithSize: retLength];
if (_readBuffer != NULL &&
_readBufferLength <= retLength)
memcpy(retCString, _readBuffer,
_readBufferLength);
else if (_readBuffer != NULL)
memcpy(retCString, _readBuffer,
retLength);
if (i >= delimiterLength)
memcpy(retCString + _readBufferLength,
buffer, i + 1 - delimiterLength);
@try {
char *rcs = retCString;
size_t rl = retLength;
ret = [OFString
stringWithCString: rcs
stringWithCString: retCString
encoding: encoding
length: rl];
length: retLength];
} @catch (id e) {
if (bufferLength > 0) {
/*
* Append data to _readBuffer
* to prevent loss of data.
*/
readBuffer = [self
|
︙ | | |
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
|
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
|
-
+
|
_readBufferMemory = readBuffer;
_readBufferLength +=
bufferLength;
}
@throw e;
} @finally {
[self freeMemory: retCString];
free(retCString);
}
readBuffer = [self allocMemoryWithSize:
bufferLength - i - 1];
if (readBuffer != NULL)
memcpy(readBuffer, buffer + i + 1,
bufferLength - i - 1);
|
︙ | | |
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
|
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
|
-
+
|
buffer, bufferLength);
[self freeMemory: _readBufferMemory];
_readBuffer = _readBufferMemory = readBuffer;
_readBufferLength += bufferLength;
}
} @finally {
[self freeMemory: buffer];
free(buffer);
}
_waitingForDelimiter = true;
return nil;
}
|
︙ | | |
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
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
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
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
|
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
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
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
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
|
-
+
-
-
+
-
+
-
-
+
-
+
-
-
+
-
+
-
-
+
-
+
-
-
+
|
size = count * sizeof(uint16_t);
#ifdef OF_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
uint16_t *tmp = [self allocMemoryWithSize: sizeof(uint16_t)
uint16_t *tmp = of_malloc(count, sizeof(uint16_t));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP16(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeBigEndianInt32s: (const uint32_t *)buffer
count: (size_t)count
{
size_t size;
if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint32_t))
@throw [OFOutOfRangeException exception];
size = count * sizeof(uint32_t);
#ifdef OF_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
uint32_t *tmp = [self allocMemoryWithSize: sizeof(uint32_t)
uint32_t *tmp = of_malloc(count, sizeof(uint32_t));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP32(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeBigEndianInt64s: (const uint64_t *)buffer
count: (size_t)count
{
size_t size;
if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint64_t))
@throw [OFOutOfRangeException exception];
size = count * sizeof(uint64_t);
#ifdef OF_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
uint64_t *tmp = [self allocMemoryWithSize: sizeof(uint64_t)
uint64_t *tmp = of_malloc(count, sizeof(uint64_t));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP64(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeBigEndianFloats: (const float *)buffer
count: (size_t)count
{
size_t size;
if OF_UNLIKELY (count > SIZE_MAX / sizeof(float))
@throw [OFOutOfRangeException exception];
size = count * sizeof(float);
#ifdef OF_FLOAT_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
float *tmp = [self allocMemoryWithSize: sizeof(float)
float *tmp = of_malloc(count, sizeof(float));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP_FLOAT(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeBigEndianDoubles: (const double *)buffer
count: (size_t)count
{
size_t size;
if OF_UNLIKELY (count > SIZE_MAX / sizeof(double))
@throw [OFOutOfRangeException exception];
size = count * sizeof(double);
#ifdef OF_FLOAT_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
double *tmp = [self allocMemoryWithSize: sizeof(double)
double *tmp = of_malloc(count, sizeof(double));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP_DOUBLE(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (void)writeLittleEndianInt16: (uint16_t)int16
|
︙ | | |
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
|
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
|
-
+
-
-
+
-
+
-
-
+
-
+
-
-
+
-
+
-
-
+
-
+
-
-
+
|
size = count * sizeof(uint16_t);
#ifndef OF_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
uint16_t *tmp = [self allocMemoryWithSize: sizeof(uint16_t)
uint16_t *tmp = of_malloc(count, sizeof(uint16_t));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP16(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeLittleEndianInt32s: (const uint32_t *)buffer
count: (size_t)count
{
size_t size;
if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint32_t))
@throw [OFOutOfRangeException exception];
size = count * sizeof(uint32_t);
#ifndef OF_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
uint32_t *tmp = [self allocMemoryWithSize: sizeof(uint32_t)
uint32_t *tmp = of_malloc(count, sizeof(uint32_t));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP32(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeLittleEndianInt64s: (const uint64_t *)buffer
count: (size_t)count
{
size_t size;
if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint64_t))
@throw [OFOutOfRangeException exception];
size = count * sizeof(uint64_t);
#ifndef OF_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
uint64_t *tmp = [self allocMemoryWithSize: sizeof(uint64_t)
uint64_t *tmp = of_malloc(count, sizeof(uint64_t));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP64(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeLittleEndianFloats: (const float *)buffer
count: (size_t)count
{
size_t size;
if OF_UNLIKELY (count > SIZE_MAX / sizeof(float))
@throw [OFOutOfRangeException exception];
size = count * sizeof(float);
#ifndef OF_FLOAT_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
float *tmp = [self allocMemoryWithSize: sizeof(float)
float *tmp = of_malloc(count, sizeof(float));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP_FLOAT(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeLittleEndianDoubles: (const double *)buffer
count: (size_t)count
{
size_t size;
if OF_UNLIKELY (count > SIZE_MAX / sizeof(double))
@throw [OFOutOfRangeException exception];
size = count * sizeof(double);
#ifndef OF_FLOAT_BIG_ENDIAN
[self writeBuffer: buffer
length: size];
#else
double *tmp = [self allocMemoryWithSize: sizeof(double)
double *tmp = of_malloc(count, sizeof(double));
count: count];
@try {
for (size_t i = 0; i < count; i++)
tmp[i] = OF_BSWAP_DOUBLE(buffer[i]);
[self writeBuffer: tmp
length: size];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
#endif
return size;
}
- (size_t)writeData: (OFData *)data
|
︙ | | |
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
|
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
|
-
+
-
+
|
- (size_t)writeLine: (OFString *)string
encoding: (of_string_encoding_t)encoding
{
size_t stringLength = [string cStringLengthWithEncoding: encoding];
char *buffer;
buffer = [self allocMemoryWithSize: stringLength + 1];
buffer = of_malloc(stringLength + 1, 1);
@try {
memcpy(buffer, [string cStringWithEncoding: encoding],
stringLength);
buffer[stringLength] = '\n';
[self writeBuffer: buffer
length: stringLength + 1];
} @finally {
[self freeMemory: buffer];
free(buffer);
}
return stringLength + 1;
}
- (size_t)writeFormat: (OFConstantString *)format, ...
{
|
︙ | | |
︙ | | |
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
-
+
-
|
char *buffer = buffer_;
of_char16_t *UTF16;
size_t j = 0;
if (length > UINT32_MAX)
@throw [OFOutOfRangeException exception];
UTF16 = [self allocMemoryWithSize: sizeof(of_char16_t)
UTF16 = of_malloc(length, sizeof(of_char16_t));
count: length];
@try {
DWORD UTF16Len;
OFMutableData *rest = nil;
size_t i = 0;
if ([OFSystemInfo isWindowsNT]) {
if (!ReadConsoleW(_handle, UTF16, (DWORD)length,
|
︙ | | |
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
-
+
|
}
}
if (rest != nil)
[self unreadFromBuffer: rest.items
length: rest.count];
} @finally {
[self freeMemory: UTF16];
free(UTF16);
}
objc_autoreleasePoolPop(pool);
return j;
}
|
︙ | | |
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
-
+
-
|
bytesWritten: bytesWritten * 2
errNo: 0];
_incompleteUTF8SurrogateLen = 0;
i += toCopy;
}
tmp = [self allocMemoryWithSize: sizeof(of_char16_t)
tmp = of_malloc(length * 2, sizeof(of_char16_t));
count: length * 2];
@try {
DWORD bytesWritten;
while (i < length) {
of_unichar_t c;
ssize_t UTF8Len;
|
︙ | | |
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
-
+
|
if (bytesWritten != j)
@throw [OFWriteFailedException
exceptionWithObject: self
requestedLength: j * 2
bytesWritten: bytesWritten * 2
errNo: 0];
} @finally {
[self freeMemory: tmp];
free(tmp);
}
/*
* We do not count in bytes when writing to the Win32 console. But
* since any incomplete write is an exception here anyway, we can just
* return length.
*/
|
︙ | | |
︙ | | |
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
-
-
-
|
/* References for static linking */
void
_references_to_categories_of_OFXMLElement(void)
{
_OFXMLElement_Serialization_reference = 1;
}
static Class charactersClass = Nil;
static Class CDATAClass = Nil;
@interface OFXMLElementElementBuilderDelegate: OFObject
<OFXMLElementBuilderDelegate>
{
@public
OFXMLElement *_element;
}
@end
|
︙ | | |
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
-
-
-
-
-
-
-
-
|
}
@end
@implementation OFXMLElement
@synthesize name = _name, namespace = _namespace;
@synthesize defaultNamespace = _defaultNamespace;
+ (void)initialize
{
if (self == [OFXMLElement class]) {
charactersClass = [OFXMLCharacters class];
CDATAClass = [OFXMLCDATA class];
}
}
+ (instancetype)elementWithName: (OFString *)name
{
return [[[self alloc] initWithName: name] autorelease];
}
+ (instancetype)elementWithName: (OFString *)name
stringValue: (OFString *)stringValue
|
︙ | | |
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
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
633
634
635
636
637
638
639
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
+
-
-
+
-
-
-
-
-
+
-
-
+
+
-
-
+
-
-
-
-
-
-
-
+
+
+
+
+
-
-
+
+
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
+
+
-
+
+
-
-
+
+
+
+
-
+
-
+
+
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
+
-
-
+
|
[ret makeImmutable];
return ret;
}
- (OFString *)of_XMLStringWithParent: (OFXMLElement *)parent
namespaces: (OFDictionary *)allNamespaces
namespaces: (OFDictionary *)allNS
indentation: (unsigned int)indentation
level: (unsigned int)level OF_DIRECT
{
void *pool;
char *cString;
size_t length, i;
OFString *prefix, *parentPrefix;
OFString *ret;
OFString *defaultNS;
pool = objc_autoreleasePoolPush();
parentPrefix = [allNamespaces objectForKey:
parentPrefix = [allNS objectForKey:
(parent != nil && parent->_namespace != nil
? parent->_namespace : (OFString *)@"")];
/* Add the namespaces of the current element */
if (allNamespaces != nil) {
if (allNS != nil) {
OFEnumerator *keyEnumerator = [_namespaces keyEnumerator];
OFEnumerator *objectEnumerator = [_namespaces objectEnumerator];
OFMutableDictionary *tmp;
OFString *key, *object;
tmp = [[allNamespaces mutableCopy] autorelease];
tmp = [[allNS mutableCopy] autorelease];
while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil)
[tmp setObject: object
forKey: key];
allNamespaces = tmp;
allNS = tmp;
} else
allNamespaces = _namespaces;
allNS = _namespaces;
prefix = [allNamespaces objectForKey:
prefix = [allNS objectForKey:
(_namespace != nil ? _namespace : (OFString *)@"")];
if (parent != nil && parent->_namespace != nil && parentPrefix == nil)
defaultNS = parent->_namespace;
else if (parent != nil && parent->_defaultNamespace != nil)
defaultNS = parent->_defaultNamespace;
else
defaultNS = _defaultNamespace;
i = 0;
length = _name.UTF8StringLength + 3 + (level * indentation);
cString = [self allocMemoryWithSize: length];
cString = of_malloc(length, 1);
@try {
memset(cString + i, ' ', level * indentation);
i += level * indentation;
/* Start of tag */
cString[i++] = '<';
if (prefix != nil && ![_namespace isEqual: defaultNS]) {
length += prefix.UTF8StringLength + 1;
@try {
cString = [self resizeMemory: cString
cString = of_realloc(cString, length, 1);
size: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, prefix.UTF8String, prefix.UTF8StringLength);
memcpy(cString + i, prefix.UTF8String,
prefix.UTF8StringLength);
i += prefix.UTF8StringLength;
cString[i++] = ':';
}
memcpy(cString + i, _name.UTF8String, _name.UTF8StringLength);
i += _name.UTF8StringLength;
/* xmlns if necessary */
if (prefix == nil && ((_namespace != nil &&
![_namespace isEqual: defaultNS]) ||
(_namespace == nil && defaultNS != nil))) {
length += _namespace.UTF8StringLength + 9;
@try {
cString = [self resizeMemory: cString
cString = of_realloc(cString, length, 1);
size: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, " xmlns='", 8);
i += 8;
memcpy(cString + i, _namespace.UTF8String,
_namespace.UTF8StringLength);
i += _namespace.UTF8StringLength;
cString[i++] = '\'';
}
/* Attributes */
for (OFXMLAttribute *attribute in _attributes) {
void *pool2 = objc_autoreleasePoolPush();
const char *attributeNameCString = attribute->_name.UTF8String;
size_t attributeNameLength = attribute->_name.UTF8StringLength;
const char *attributeNameCString =
attribute->_name.UTF8String;
size_t attributeNameLength =
attribute->_name.UTF8StringLength;
OFString *attributePrefix = nil;
OFString *tmp =
OFString *tmp = attribute.stringValue.stringByXMLEscaping;
char delimiter = (attribute->_useDoubleQuotes ? '"' : '\'');
attribute.stringValue.stringByXMLEscaping;
char delimiter = (attribute->_useDoubleQuotes
? '"' : '\'');
if (attribute->_namespace != nil &&
(attributePrefix = [allNamespaces objectForKey:
(attributePrefix = [allNS objectForKey:
attribute->_namespace]) == nil)
@throw [OFUnboundNamespaceException
exceptionWithNamespace: [attribute namespace]
exceptionWithNamespace: attribute.namespace
element: self];
length += attributeNameLength + (attributePrefix != nil
? attributePrefix.UTF8StringLength + 1 : 0) +
tmp.UTF8StringLength + 4;
cString = of_realloc(cString, length, 1);
@try {
cString = [self resizeMemory: cString
size: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
cString[i++] = ' ';
if (attributePrefix != nil) {
memcpy(cString + i, attributePrefix.UTF8String,
attributePrefix.UTF8StringLength);
i += attributePrefix.UTF8StringLength;
cString[i++] = ':';
}
memcpy(cString + i, attributeNameCString, attributeNameLength);
memcpy(cString + i, attributeNameCString,
attributeNameLength);
i += attributeNameLength;
cString[i++] = '=';
cString[i++] = delimiter;
memcpy(cString + i, tmp.UTF8String, tmp.UTF8StringLength);
memcpy(cString + i, tmp.UTF8String,
tmp.UTF8StringLength);
i += tmp.UTF8StringLength;
cString[i++] = delimiter;
objc_autoreleasePoolPop(pool2);
}
/* Children */
if (_children != nil) {
OFMutableData *tmp = [OFMutableData data];
bool indent;
if (indentation > 0) {
indent = true;
for (OFXMLNode *child in _children) {
if ([child isKindOfClass: charactersClass] ||
[child isKindOfClass: CDATAClass]) {
if ([child isKindOfClass:
[OFXMLCharacters class]] ||
[child isKindOfClass:
[OFXMLCDATA class]]) {
indent = false;
break;
}
}
} else
indent = false;
for (OFXMLNode *child in _children) {
OFString *childString;
unsigned int ind = (indent ? indentation : 0);
if (ind)
[tmp addItem: "\n"];
if ([child isKindOfClass: [OFXMLElement class]])
childString = [(OFXMLElement *)child
of_XMLStringWithParent: self
namespaces: allNamespaces
namespaces: allNS
indentation: ind
level: level + 1];
else
childString = [child
XMLStringWithIndentation: ind
level: level + 1];
level: level +
1];
[tmp addItems: childString.UTF8String
count: childString.UTF8StringLength];
}
if (indent)
[tmp addItem: "\n"];
length += tmp.count + _name.UTF8StringLength + 2 +
(indent ? level * indentation : 0);
@try {
cString = [self resizeMemory: cString
cString = of_realloc(cString, length, 1);
size: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
cString[i++] = '>';
memcpy(cString + i, tmp.items, tmp.count);
i += tmp.count;
if (indent) {
memset(cString + i, ' ', level * indentation);
i += level * indentation;
}
cString[i++] = '<';
cString[i++] = '/';
if (prefix != nil) {
length += prefix.UTF8StringLength + 1;
@try {
cString = [self resizeMemory: cString
cString = of_realloc(cString, length, 1);
size: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, prefix.UTF8String,
prefix.UTF8StringLength);
i += prefix.UTF8StringLength;
cString[i++] = ':';
}
memcpy(cString + i, _name.UTF8String, _name.UTF8StringLength);
memcpy(cString + i, _name.UTF8String,
_name.UTF8StringLength);
i += _name.UTF8StringLength;
} else
cString[i++] = '/';
cString[i++] = '>';
assert(i == length);
objc_autoreleasePoolPop(pool);
@try {
ret = [OFString stringWithUTF8String: cString
length: length];
} @finally {
[self freeMemory: cString];
free(cString);
}
return ret;
}
- (OFString *)XMLString
{
return [self of_XMLStringWithParent: nil
|
︙ | | |