1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
-
+
|
/*
* tclBinary.c --
*
* This file contains the implementation of the "binary" Tcl built-in
* command and the Tcl binary data object.
*
* Copyright (c) 1997 by Sun Microsystems, Inc.
* Copyright (c) 1998-1999 by Scriptics Corporation.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclBinary.c,v 1.13.4.10 2005/12/02 18:42:06 dgp Exp $
* RCS: @(#) $Id: tclBinary.c,v 1.13.4.11 2006/04/28 16:09:08 dgp Exp $
*/
#include "tclInt.h"
#include <math.h>
/*
|
| ︙ | | |
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
|
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
|
-
+
+
+
-
+
+
+
|
}
/*
*----------------------------------------------------------------------
*
* NeedReversing --
*
* This routine determines, if bytes of a number need to be reversed.
* This routine determines, if bytes of a number need to be re-ordered,
* and returns a numeric code indicating the re-ordering to be done.
* This depends on the endiannes of the machine and the desired format.
* It is in effect a table (whose contents depend on the endianness of
* the system) describing whether a value needs reversing or not. Anyone
* porting the code to a big-endian platform should take care to make
* sure that they define WORDS_BIGENDIAN though this is already done by
* configure for the Unix build; little-endian platforms (including
* Windows) don't need to do anything.
*
* Results:
* 0 No re-ordering needed.
* 1 if reversion is required, 0 if not.
* 1 Reverse the bytes: 01234567 <-> 76543210 (little to big)
* 2 Apply this re-ordering: 01234567 <-> 45670123 (Nokia to little)
* 3 Apply this re-ordering: 01234567 <-> 32107654 (Nokia to big)
*
* Side effects:
* None
*
*----------------------------------------------------------------------
*/
|
| ︙ | | |
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
1483
1484
1485
|
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
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
|
-
+
-
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
case 'S':
case 'W':
#ifdef WORDS_BIGENDIAN
/* native ints: reverse if we're little-endian */
case 'n':
case 't':
case 'm':
/* f+d: reverse if we're little-endian */
/* f: reverse if we're little-endian */
case 'Q':
case 'R':
#else /* !WORDS_BIGENDIAN */
/* small endian floats: reverse if we're big-endian */
case 'q':
case 'r':
#endif /* WORDS_BIGENDIAN */
return 0;
#ifdef WORDS_BIGENDIAN
/* small endian floats: reverse if we're big-endian */
case 'q':
case 'r':
#else /* !WORDS_BIGENDIAN */
/* native ints: reverse if we're little-endian */
case 'n':
case 't':
case 'm':
/* f+d: reverse if we're little-endian */
/* f: reverse if we're little-endian */
case 'Q':
case 'R':
#endif /* WORDS_BIGENDIAN */
/* small endian ints: always reverse */
case 'i':
case 's':
case 'w':
return 1;
#ifndef WORDS_BIGENDIAN
/*
* The Q and q formats need special handling to account for the
* unusual byte ordering of 8-byte floats on Nokia 770 systems, which
* claim to be little-endian, but also reverse word order.
*/
case 'Q':
if (TclNokia770Doubles()) {
return 3;
}
return 1;
case 'q':
if (TclNokia770Doubles()) {
return 2;
}
return 0;
#endif
}
Tcl_Panic("unexpected fall-through");
return 0;
}
/*
|
| ︙ | | |
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
|
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
|
-
+
+
+
+
+
|
static void
CopyNumber(
CONST void *from, /* source */
void *to, /* destination */
unsigned int length, /* Number of bytes to copy */
int type) /* What type of thing are we copying? */
{
if (NeedReversing(type)) {
switch (NeedReversing(type)) {
case 0:
memcpy(to, from, length);
break;
case 1: {
CONST unsigned char *fromPtr = (CONST unsigned char *) from;
unsigned char *toPtr = (unsigned char *) to;
switch (length) {
case 4:
toPtr[0] = fromPtr[3];
toPtr[1] = fromPtr[2];
|
| ︙ | | |
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
|
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
|
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
toPtr[3] = fromPtr[4];
toPtr[4] = fromPtr[3];
toPtr[5] = fromPtr[2];
toPtr[6] = fromPtr[1];
toPtr[7] = fromPtr[0];
break;
}
break;
}
} else {
memcpy(to, from, length);
case 2: {
CONST unsigned char *fromPtr = (CONST unsigned char *) from;
unsigned char *toPtr = (unsigned char *) to;
toPtr[0] = fromPtr[4];
toPtr[1] = fromPtr[5];
toPtr[2] = fromPtr[6];
toPtr[3] = fromPtr[7];
toPtr[4] = fromPtr[0];
toPtr[5] = fromPtr[1];
toPtr[6] = fromPtr[2];
toPtr[7] = fromPtr[3];
break;
}
case 3: {
CONST unsigned char *fromPtr = (CONST unsigned char *) from;
unsigned char *toPtr = (unsigned char *) to;
toPtr[0] = fromPtr[3];
toPtr[1] = fromPtr[2];
toPtr[2] = fromPtr[1];
toPtr[3] = fromPtr[0];
toPtr[4] = fromPtr[7];
toPtr[5] = fromPtr[6];
toPtr[6] = fromPtr[5];
toPtr[7] = fromPtr[4];
break;
}
}
}
/*
*----------------------------------------------------------------------
*
* FormatNumber --
|
| ︙ | | |