1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 73 74 75 76 77 78 79 80 81 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 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 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 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 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 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 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 750 751 752 753 754 755 756 757 758 759 760 761 762 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 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 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 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 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 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 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 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 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 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 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
|
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "gsCommon.h"
#include "gsPlatformUtil.h"
// Include platform separated functions
#if defined(_X360)
//#include "x360/gsUtilX360.c"
#elif defined(_XBOX)
//#include "xbox/gsUtilXBox.c"
#elif defined(_WIN32)
#include "win32/gsUtilWin32.c"
#elif defined(_LINUX)
#include "linux/gsUtilLinux.c"
#elif defined(_MACOSX)
#include "macosx/gsUtilMacOSX.c"
#elif defined(_NITRO)
#include "nitro/gsUtilNitro.c"
#elif defined(_PS2)
#include "ps2/gsUtilPs2.c"
#elif defined(_PS3)
#include "ps3/gsUtilPs3.c"
#elif defined(_PSP)
#include "psp/gsUtilPSP.c"
#elif defined(_REVOLUTION)
#include "revolution/gsUtilRevolution.c"
#else
#error "Missing or unsupported platform"
#endif
#if defined(__cplusplus)
extern "C" {
#endif
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// ********** ASYNC DNS ********** //
//struct is used in both threaded and non-threaded versions
typedef struct GSIResolveHostnameInfo
{
char * hostname;
unsigned int ip;
#if defined(_WIN32) /*|| defined(_PS2)*/ || defined(_UNIX) || defined (_REVOLUTION)
int finishedResolving;
GSIThreadID threadID;
#endif
/*#if defined(_PSP)
int finishedResolving;
GSIThreadID threadID;
#endif*/
} GSIResolveHostnameInfo;
////////////////////////////////////////////////////////////////////////////////
// for asynch DNS, must have:
// * platform that supports threaded lookup AND
// * threading enabled
// * and async lookup enabled
////////////////////////////////////////////////////////////////////////////////
#if (defined(_WIN32) || /*defined(_PS2) ||*/ defined(_UNIX) || defined (_REVOLUTION)) && !defined(GSI_NO_THREADS) && !defined(GSI_NO_ASYNC_DNS)
////////////////////////////////////////////////////////////////////////////////
#if defined(_WIN32) /*|| defined(_PS2)*/
#if defined(_WIN32)
DWORD WINAPI gsiResolveHostnameThread(void * arg)
/*#elif defined(_PS2)
static void gsiResolveHostnameThread(void * arg)*/
#endif
{
HOSTENT * hostent;
GSIResolveHostnameHandle handle = (GSIResolveHostnameHandle)arg;
SocketStartUp();
#ifdef SN_SYSTEMS
sockAPIregthr();
#endif
// do the gethostbyname
hostent = gethostbyname(handle->hostname);
if(hostent)
{
// got the ip
handle->ip = *(unsigned int *)hostent->h_addr_list[0];
}
else
{
// didn't resolve
handle->ip = GSI_ERROR_RESOLVING_HOSTNAME;
}
SocketShutDown();
// finished resolving
handle->finishedResolving = 1;
#ifdef SN_SYSTEMS
sockAPIderegthr();
#endif
// explicitly exit the thread to free resources
gsiExitThread(handle->threadID);
#if defined(_WIN32)
return 0;
#endif
}
#endif //defined _WIN32
////////////////////////////////////////////////////////////////////////////////
#ifdef _REVOLUTION
///////////////////////////////////////////////////////////////////////////////
static void *gsiResolveHostnameThread(void * arg)
{
static GSICriticalSection aHostnameCrit;
static int aInitialized = 0;
//SOAddrInfo *aHostAddr;
HOSTENT *aHostAddr;
//int retval;
GSIResolveHostnameHandle handle = (GSIResolveHostnameHandle)arg;
if (!aInitialized)
{
gsiInitializeCriticalSection(&aHostnameCrit);
aInitialized = 1;
}
gsiEnterCriticalSection(&aHostnameCrit);
//retval = getaddrinfo(handle->hostname, NULL, NULL, &aHostAddr);
aHostAddr = gethostbyname(handle->hostname);
if (aHostAddr != 0)
{
char * ip;
// first convert to character string for debug output
ip = inet_ntoa(*(in_addr *)aHostAddr->addrList[0]);
gsDebugFormat(GSIDebugCat_HTTP, GSIDebugType_State, GSIDebugLevel_Comment,
"Resolved host '%s' to ip '%s'\n", handle->hostname, ip);
handle->ip = inet_addr(ip);
//freeaddrinfo(aHostAddr);
}
else
{
// couldnt reach host - debug output is printed later
handle->ip = GSI_ERROR_RESOLVING_HOSTNAME;
}
// finished resolving
handle->finishedResolving = 1;
gsiLeaveCriticalSection(&aHostnameCrit);
}
#endif // _REVOLUTION
////////////////////////////////////////////////////////////////////////////////
//
// Linux/MacOSX implementation of multithreaded DNS lookup
// Uses getaddrinfo instead of gethostbyname - since the latter
// has static declarations and is thus un-safe for pthreads
//
// NOTE: The compiler option "-lpthread" must used for this
#if defined(_UNIX)
////////////////////////////////////////////////////////////////////////////////
static void gsiResolveHostnameThread(void * arg)
{
GSIResolveHostnameHandle handle = (GSIResolveHostnameHandle)arg;
struct addrinfo hints, *result = NULL;
int error;
char *ip;
SocketStartUp();
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
// DNS lookup (works with pthreads)
error = getaddrinfo(handle->hostname, "http", &hints, &result);
if (!error)
{
// first convert to character string for debug output
ip = inet_ntoa((*(struct sockaddr_in*)result->ai_addr).sin_addr);
gsDebugFormat(GSIDebugCat_HTTP, GSIDebugType_State, GSIDebugLevel_Comment,
"Resolved host '%s' to ip '%s'\n", handle->hostname, ip);
// now convert to unsigned int and store it
handle->ip = inet_addr(ip);
// free the memory used
freeaddrinfo(result);
}
else
{
// couldnt reach host - debug output is printed later
handle->ip = GSI_ERROR_RESOLVING_HOSTNAME;
}
SocketShutDown();
// finished resolving
handle->finishedResolving = 1;
// explicitly exit the thread to free resources
gsiExitThread(handle->threadID);
}
#endif //_UNIX
////////////////////////////////////////////////////////////////////////////////
int gsiStartResolvingHostname(const char * hostname, GSIResolveHostnameHandle * handle)
{
GSIResolveHostnameInfo * info;
//PS2 Threading unsupported in current build - this should never be reached
#if defined(_PS2)
GS_ASSERT_STR(gsi_false, "PS2 Threading unsupported in current version of the SDK\n");
#endif
// allocate a handle
info = (GSIResolveHostnameInfo *)gsimalloc(sizeof(GSIResolveHostnameInfo));
if(!info)
return -1;
// make a copy of the hostname so the thread has access to it
info->hostname = goastrdup(hostname);
if(!info->hostname)
{
gsifree(info);
return -1;
}
// not resolved yet
info->finishedResolving = 0;
gsDebugFormat(GSIDebugCat_Common, GSIDebugType_State, GSIDebugLevel_Comment,
"(Asynchrounous) DNS lookup starting\n");
// start the thread
if(gsiStartThread(gsiResolveHostnameThread, (0x1000), info, &info->threadID) == -1)
{
gsifree(info->hostname);
info->hostname = NULL;
gsifree(info);
info = NULL;
return -1;
}
// set the handle to the info
*handle = info;
return 0;
}
void gsiCancelResolvingHostname(GSIResolveHostnameHandle handle)
{
// cancel the thread
gsiCancelThread(handle->threadID);
if (handle->hostname)
{
gsifree(handle->hostname);
handle->hostname = NULL;
}
gsifree(handle);
handle = NULL;
}
unsigned int gsiGetResolvedIP(GSIResolveHostnameHandle handle)
{
unsigned int ip;
// check if we haven't finished
if(!handle->finishedResolving)
return GSI_STILL_RESOLVING_HOSTNAME;
// save the ip
ip = handle->ip;
// free resources
gsiCleanupThread(handle->threadID);
gsifree(handle->hostname);
gsifree(handle);
handle = NULL;
return ip;
}
#else // if * not a supported platform OR * no threads allowed OR * no async lookup allowed
///////////////////////////////////////////////////////////////////////////////////
// if !(_WIN32 ||_PS2 || _LINUX || _MACOSX || _REVOLUTION) || GSI_NO_THREADS || GSI_NO_ASYNC_DNS
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// ********** NON-ASYNC DNS ********** //
//
// These are the non-threaded version of the above functions.
// The following platforms have synchronous DNS lookups:
// _NITRO || _XBOX || _X360 || _PS3 || _PS2 || _PSP
///////////////////////////////////////////////////////////////////////////////
int gsiStartResolvingHostname(const char * hostname, GSIResolveHostnameHandle * handle)
{
GSIResolveHostnameInfo * info;
HOSTENT * hostent;
gsDebugFormat(GSIDebugCat_HTTP, GSIDebugType_State, GSIDebugLevel_Comment,
"(NON-Asynchrounous) DNS lookup starting\n");
// do the lookup now
hostent = gethostbyname(hostname);
if(hostent == NULL)
return -1;
// allocate info to store the result
info = (GSIResolveHostnameHandle)gsimalloc(sizeof(GSIResolveHostnameInfo));
if(!info)
return -1;
// we already have the ip
info->ip = *(unsigned int *)hostent->h_addr_list[0];
// set the handle to the info
*handle = info;
return 0;
}
void gsiCancelResolvingHostname(GSIResolveHostnameHandle handle)
{
gsifree(handle);
handle = NULL;
}
unsigned int gsiGetResolvedIP(GSIResolveHostnameHandle handle)
{
// we always do the resolve in the initial call for systems without
// an async version, so we'll always have the IP at this point
unsigned int ip = handle->ip;
gsifree(handle);
handle = NULL;
return ip;
}
///////////////////////////////////////////////////////////////////////////////
#endif // synch DNS lookup
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
char * goastrdup(const char *src)
{
char *res;
if(src == NULL) //PANTS|02.11.00|check for NULL before strlen
return NULL;
res = (char *)gsimalloc(strlen(src) + 1);
if(res != NULL) //PANTS|02.02.00|check for NULL before strcpy
strcpy(res, src);
return res;
}
unsigned short * goawstrdup(const unsigned short *src)
{
unsigned short *res;
if(src == NULL)
return NULL;
res = (unsigned short *)gsimalloc((wcslen((wchar_t*)src) + 1) * sizeof(unsigned short));
if(res != NULL)
wcscpy((wchar_t*)res, (const wchar_t*)src);
return res;
}
#if !defined(_WIN32)
char *_strlwr(char *string)
{
char *hold = string;
while (*string)
{
*string = (char)tolower(*string);
string++;
}
return hold;
}
char *_strupr(char *string)
{
char *hold = string;
while (*string)
{
*string = (char)toupper(*string);
string++;
}
return hold;
}
#endif
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void SocketStartUp()
{
#if defined(_WIN32)
WSADATA data;
#if defined(_X360)
XNetStartupParams xnsp;
memset(&xnsp,0,sizeof(xnsp));
xnsp.cfgSizeOfStruct=sizeof(xnsp);
xnsp.cfgFlags=XNET_STARTUP_BYPASS_SECURITY;
if(0 != XNetStartup(&xnsp))
{
OutputDebugString("XNetStartup failed\n");
}
#endif
// added support for winsock2
#if (!defined(_XBOX) || defined(_X360)) && (defined(GSI_WINSOCK2) || defined(_X360))
WSAStartup(MAKEWORD(2,2), &data);
#else
WSAStartup(MAKEWORD(1,1), &data);
#endif
// end added
#endif
}
void SocketShutDown()
{
#if defined(_WIN32)
WSACleanup();
#if defined(_X360)
XNetCleanup();
#endif
#endif
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#ifdef _PS2
extern int sceCdReadClock();
#if !defined(__MWERKS__) && !defined(_PS2)
typedef unsigned char u_char;
#endif
typedef struct {
u_char stat; /* status */
u_char second; /* second */
u_char minute; /* minute */
u_char hour; /* hour */
u_char pad; /* pad */
u_char day; /* day */
u_char month; /* month */
u_char year; /* year */
} sceCdCLOCK;
static unsigned long GetTicks()
{
unsigned long ticks;
asm volatile (" mfc0 %0, $9 " : "=r" (ticks));
return ticks;
}
#define DEC(x) (10*(x/16)+(x%16))
#define _BASE_YEAR 70L
#define _MAX_YEAR 138L
#define _LEAP_YEAR_ADJUST 17L
int _days[] = {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364};
static time_t _gmtotime_t (
int yr, /* 0 based */
int mo, /* 1 based */
int dy, /* 1 based */
int hr,
int mn,
int sc
)
{
int tmpdays;
long tmptim;
struct tm tb;
if ( ((long)(yr -= 1900) < _BASE_YEAR) || ((long)yr > _MAX_YEAR) )
return (time_t)(-1);
tmpdays = dy + _days[mo - 1];
if ( !(yr & 3) && (mo > 2) )
tmpdays++;
tmptim = (long)yr - _BASE_YEAR;
tmptim = ( ( ( ( tmptim ) * 365L
+ ((long)(yr - 1) >> 2) - (long)_LEAP_YEAR_ADJUST
+ (long)tmpdays )
* 24L + (long)hr )
* 60L + (long)mn )
* 60L + (long)sc;
tb.tm_yday = tmpdays;
tb.tm_year = yr;
tb.tm_mon = mo - 1;
tb.tm_hour = hr;
return (tmptim >= 0) ? (time_t)tmptim : (time_t)(-1);
}
time_t time(time_t *timer)
{
time_t tim;
sceCdCLOCK clocktime; /* defined in libcdvd.h */
sceCdReadClock(&clocktime); /* libcdvd.a */
tim = _gmtotime_t ( DEC(clocktime.year)+2000,
DEC(clocktime.month),
DEC(clocktime.day),
DEC(clocktime.hour),
DEC(clocktime.minute),
DEC(clocktime.second));
if(timer)
*timer = tim;
return tim;
}
#endif /* _PS2 */
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_time current_time() //returns current time in milliseconds
{
#if defined(_WIN32)
return (GetTickCount());
#elif defined(_PS2)
unsigned int ticks;
static unsigned int msec = 0;
static unsigned int lastticks = 0;
sceCdCLOCK lasttimecalled; /* defined in libcdvd.h */
if(!msec)
{
sceCdReadClock(&lasttimecalled); /* libcdvd.a */
msec = (unsigned int)(DEC(lasttimecalled.day) * 86400000) +
(unsigned int)(DEC(lasttimecalled.hour) * 3600000) +
(unsigned int)(DEC(lasttimecalled.minute) * 60000) +
(unsigned int)(DEC(lasttimecalled.second) * 1000);
}
ticks = (unsigned int)GetTicks();
if(lastticks > ticks)
msec += (unsigned int)(((unsigned int)(-1) - lastticks) + ticks) / 300000;
else
msec += (unsigned int)(ticks-lastticks) / 300000;
lastticks = ticks;
return msec;
#elif defined(_UNIX)
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000 + time.tv_usec / 1000);
#elif defined(_NITRO)
assert(OS_IsTickAvailable() == TRUE);
return (gsi_time)OS_TicksToMilliSeconds(OS_GetTick());
#elif defined(_PSP)
struct SceRtcTick ticks;
int result = 0;
result = sceRtcGetCurrentTick(&ticks);
if (result < 0)
{
ScePspDateTime time;
result = sceRtcGetCurrentClock(&time, 0);
if (result < 0)
return 0; // um...error handling? //Nope, should return zero since time cannot be zero
result = sceRtcGetTick(&time, &ticks);
if (result < 0)
return 0; //Nope, should return zero since time cannot be zero
}
return (gsi_time)(ticks.tick / 1000);
#elif defined(_PS3)
return (gsi_time)(sys_time_get_system_time()/1000);
#elif defined(_REVOLUTION)
OSTick aTickNow= OSGetTick();
gsi_time aMilliseconds = (gsi_time)OSTicksToMilliseconds(aTickNow);
return aMilliseconds;
#else
// unrecognized platform! contact devsupport
assert(0);
#endif
}
gsi_time current_time_hires() // returns current time in microseconds
{
#ifdef _WIN32
#if (!defined(_M_IX86) || (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 64))
static LARGE_INTEGER counterFrequency;
static BOOL haveCounterFrequency = FALSE;
static BOOL haveCounter = FALSE;
LARGE_INTEGER count;
if(!haveCounterFrequency)
{
haveCounter = QueryPerformanceFrequency(&counterFrequency);
haveCounterFrequency = TRUE;
}
if(haveCounter)
{
if(QueryPerformanceCounter(&count))
{
return (gsi_time)(count.QuadPart * 1000000 / counterFrequency.QuadPart);
}
}
#endif
return (current_time() / 1000);
#endif
#ifdef _PS2
unsigned int ticks;
static unsigned int msec = 0;
static unsigned int lastticks = 0;
sceCdCLOCK lasttimecalled; /* defined in libcdvd.h */
if(!msec)
{
sceCdReadClock(&lasttimecalled); /* libcdvd.a */
msec = (unsigned int)(DEC(lasttimecalled.day) * 86400000) +
(unsigned int)(DEC(lasttimecalled.hour) * 3600000) +
(unsigned int)(DEC(lasttimecalled.minute) * 60000) +
(unsigned int)(DEC(lasttimecalled.second) * 1000);
msec *= 1000;
}
ticks = (unsigned int)GetTicks();
if(lastticks > ticks)
msec += ((sizeof(unsigned int) - lastticks) + ticks) / 300;
else
msec += (unsigned int)(ticks-lastticks) / 300;
lastticks = ticks;
return msec;
#endif
#ifdef _PSP
struct SceRtcTick ticks;
int result = 0;
result = sceRtcGetCurrentTick(&ticks);
if (result < 0)
{
ScePspDateTime time;
result = sceRtcGetCurrentClock(&time, 0);
if (result < 0)
return 0; // um...error handling? //Nope, should return zero since time cannot be zero
result = sceRtcGetTick(&time, &ticks);
if (result < 0)
return 0; //Nope, should return zero since time cannot be zero
}
return (gsi_time)(ticks.tick);
#endif
#ifdef _UNIX
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000000 + time.tv_usec);
#endif
#ifdef _NITRO
assert(OS_IsTickAvailable() == TRUE);
return (gsi_time)OS_TicksToMicroSeconds(OS_GetTick());
#endif
#ifdef _PS3
return (gsi_time)sys_time_get_system_time();
#endif
}
void msleep(gsi_time msec)
{
#if defined(_WIN32)
Sleep(msec);
#elif defined(_PS2)
#ifdef SN_SYSTEMS
sn_delay((int)msec);
#endif
#ifdef EENET
if(msec >= 1000)
{
sleep(msec / 1000);
msec -= (msec / 1000);
}
if(msec)
usleep(msec * 1000);
#endif
#ifdef INSOCK
DelayThread(msec * 1000);
#endif
#elif defined(_PSP)
sceKernelDelayThread(msec * 1000);
#elif defined(_UNIX)
usleep(msec * 1000);
#elif defined(_NITRO)
OS_Sleep(msec);
#elif defined(_PS3)
sys_timer_usleep(msec* 1000);
#elif defined (_REVOLUTION)
OSSleepMilliseconds(msec);
#else
assert(0); // missing platform handler, contact devsupport
#endif
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Cross-platform GSI wrapper time conversion functions
//
// NOTE: some portions of this copied from standard C library
#if defined(_NITRO) || defined(_REVOLUTION)
// if an error occurs when calling mktime, return -1
#define MKTIME_ERROR (time_t)(-1)
// define common conversions for mktime
#define DAY_SEC (24L * 60L * 60L) /* secs in a day */
#define YEAR_SEC (365L * DAY_SEC) /* secs in a year */
#define FOUR_YEAR_SEC (1461L * DAY_SEC) /* secs in a 4 year interval */
#define DEC_SEC 315532800L /* secs in 1970-1979 */
#define BASE_DOW 4 /* 01-01-70 was a Thursday */
#define BASE_YEAR 70L /* 1970 is the base year */
#define LEAP_YEAR_ADJUST 17L /* Leap years 1900 - 1970 */
#define MAX_YEAR 138L /* 2038 is the max year */
// ChkAdd evaluates to TRUE if dest = src1 + src2 has overflowed
#define ChkAdd(dest, src1, src2) ( ((src1 >= 0L) && (src2 >= 0L) \
&& (dest < 0L)) || ((src1 < 0L) && (src2 < 0L) && (dest >= 0L)) )
// ChkMul evaluates to TRUE if dest = src1 * src2 has overflowed
#define ChkMul(dest, src1, src2) ( src1 ? (dest/src1 != src2) : 0 )
int _lpdays[] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
int _days[] = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 };
const char _dnames[] = { "SunMonTueWedThuFriSat" };
/* Month names must be Three character abbreviations strung together */
const char _mnames[] = { "JanFebMarAprMayJunJulAugSepOctNovDec" };
static struct tm tb = { 0 }; /* time block used in SecondsToDate */
static char buf[26]; /* buffer used to store string in SecondsToString */
static char * store_dt(char *, int);
static char * store_dt(char *p, int val)
{
*p++ = (char)(_T('0') + val / 10);
*p++ = (char)(_T('0') + val % 10);
return(p);
}
#endif //_NITRO || _REVOLUTION
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// GSI equivalent of Standard C-lib "gmtime function"
struct tm * gsiSecondsToDate(const time_t *timp)
{
#if !defined(_NITRO) && !defined(_REVOLUTION)
// for all platforms that support the standard C 'gmtime' use that
return gmtime(timp);
#else
time_t caltim = *timp; /* calendar time to convert */
int islpyr = 0; /* is-current-year-a-leap-year flag */
int tmptim;
int *mdays; /* pointer to days or lpdays */
struct tm *ptb = &tb;
if ( caltim < 0L )
return(NULL);
/*
* Determine years since 1970. First, identify the four-year interval
* since this makes handling leap-years easy (note that 2000 IS a
* leap year and 2100 is out-of-range).
*/
tmptim = (int)(caltim / FOUR_YEAR_SEC);
caltim -= ((long)tmptim * FOUR_YEAR_SEC);
/*
* Determine which year of the interval
*/
tmptim = (tmptim * 4) + 70; /* 1970, 1974, 1978,...,etc. */
if ( caltim >= YEAR_SEC )
{
tmptim++; /* 1971, 1975, 1979,...,etc. */
caltim -= YEAR_SEC;
if ( caltim >= YEAR_SEC )
{
tmptim++; /* 1972, 1976, 1980,...,etc. */
caltim -= YEAR_SEC;
/*
* Note, it takes 366 days-worth of seconds to get past a leap
* year.
*/
if ( caltim >= (YEAR_SEC + DAY_SEC) )
{
tmptim++; /* 1973, 1977, 1981,...,etc. */
caltim -= (YEAR_SEC + DAY_SEC);
}
else
{
/*
* In a leap year after all, set the flag.
*/
islpyr++;
}
}
}
/*
* tmptim now holds the value for tm_year. caltim now holds the
* number of elapsed seconds since the beginning of that year.
*/
ptb->tm_year = tmptim;
/*
* Determine days since January 1 (0 - 365). This is the tm_yday value.
* Leave caltim with number of elapsed seconds in that day.
*/
ptb->tm_yday = (int)(caltim / DAY_SEC);
caltim -= (long)(ptb->tm_yday) * DAY_SEC;
/*
* Determine months since January (0 - 11) and day of month (1 - 31)
*/
if ( islpyr )
mdays = _lpdays;
else
mdays = _days;
for ( tmptim = 1 ; mdays[tmptim] < ptb->tm_yday ; tmptim++ ) ;
ptb->tm_mon = --tmptim;
ptb->tm_mday = ptb->tm_yday - mdays[tmptim];
/*
* Determine days since Sunday (0 - 6)
*/
ptb->tm_wday = ((int)(*timp / DAY_SEC) + BASE_DOW) % 7;
/*
* Determine hours since midnight (0 - 23), minutes after the hour
* (0 - 59), and seconds after the minute (0 - 59).
*/
ptb->tm_hour = (int)(caltim / 3600);
caltim -= (long)ptb->tm_hour * 3600L;
ptb->tm_min = (int)(caltim / 60);
ptb->tm_sec = (int)(caltim - (ptb->tm_min) * 60);
ptb->tm_isdst = 0;
return( (struct tm *)ptb );
#endif
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// GSI equivalent of Standard C-lib "mktime function"
time_t gsiDateToSeconds(struct tm *tb)
{
#if !defined(_NITRO) && !defined(_REVOLUTION)
// for all platforms that support the standard C 'mktime' use that
return mktime(tb);
#else
time_t tmptm1, tmptm2, tmptm3;
struct tm *tbtemp;
/*
* First, make sure tm_year is reasonably close to being in range.
*/
if ( ((tmptm1 = tb->tm_year) < BASE_YEAR - 1) || (tmptm1 > MAX_YEAR + 1) )
return MKTIME_ERROR;
/*
* Adjust month value so it is in the range 0 - 11. This is because
* we don't know how many days are in months 12, 13, 14, etc.
*/
if ( (tb->tm_mon < 0) || (tb->tm_mon > 11) ) {
/*
* no danger of overflow because the range check above.
*/
tmptm1 += (tb->tm_mon / 12);
if ( (tb->tm_mon %= 12) < 0 ) {
tb->tm_mon += 12;
tmptm1--;
}
/*
* Make sure year count is still in range.
*/
if ( (tmptm1 < BASE_YEAR - 1) || (tmptm1 > MAX_YEAR + 1) )
return MKTIME_ERROR;
}
/***** HERE: tmptm1 holds number of elapsed years *****/
/*
* Calculate days elapsed minus one, in the given year, to the given
* month. Check for leap year and adjust if necessary.
*/
tmptm2 = _days[tb->tm_mon];
if ( !(tmptm1 & 3) && (tb->tm_mon > 1) )
tmptm2++;
/*
* Calculate elapsed days since base date (midnight, 1/1/70, UTC)
*
*
* 365 days for each elapsed year since 1970, plus one more day for
* each elapsed leap year. no danger of overflow because of the range
* check (above) on tmptm1.
*/
tmptm3 = (tmptm1 - BASE_YEAR) * 365L + ((tmptm1 - 1L) >> 2)
- LEAP_YEAR_ADJUST;
/*
* elapsed days to current month (still no possible overflow)
*/
tmptm3 += tmptm2;
/*
* elapsed days to current date. overflow is now possible.
*/
tmptm1 = tmptm3 + (tmptm2 = (long)(tb->tm_mday));
if ( ChkAdd(tmptm1, tmptm3, tmptm2) )
return MKTIME_ERROR;
/***** HERE: tmptm1 holds number of elapsed days *****/
/*
* Calculate elapsed hours since base date
*/
tmptm2 = tmptm1 * 24L;
if ( ChkMul(tmptm2, tmptm1, 24L) )
return MKTIME_ERROR;
tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_hour);
if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
return MKTIME_ERROR;
/***** HERE: tmptm1 holds number of elapsed hours *****/
/*
* Calculate elapsed minutes since base date
*/
tmptm2 = tmptm1 * 60L;
if ( ChkMul(tmptm2, tmptm1, 60L) )
return MKTIME_ERROR;
tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_min);
if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
return MKTIME_ERROR;
/***** HERE: tmptm1 holds number of elapsed minutes *****/
/*
* Calculate elapsed seconds since base date
*/
tmptm2 = tmptm1 * 60L;
if ( ChkMul(tmptm2, tmptm1, 60L) )
return MKTIME_ERROR;
tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_sec);
if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
return MKTIME_ERROR;
/***** HERE: tmptm1 holds number of elapsed seconds *****/
if ( (tbtemp = gsiSecondsToDate(&tmptm1)) == NULL )
return MKTIME_ERROR;
/***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/
/***** for local time if requested *****/
*tb = *tbtemp;
return (time_t)tmptm1;
#endif
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// GSI equivalent of Standard C-lib "ctime function"
char * gsiSecondsToString(const time_t *timp)
{
#if !defined(_NITRO) && !defined(_REVOLUTION)
// for all platforms that support the standard C 'ctime' use that
return ctime(timp);
#else
char *p = buf;
int day, mon;
int i;
struct tm *ptm;
ptm = gsiSecondsToDate(timp); /* parse seconds into date structure */
p = buf; /* use static buffer */
/* copy day and month names into the buffer */
day = ptm->tm_wday * 3; /* index to correct day string */
mon = ptm->tm_mon * 3; /* index to correct month string */
for (i=0; i < 3; i++,p++) {
*p = *(_dnames + day + i);
*(p+4) = *(_mnames + mon + i);
}
*p = _T(' '); /* blank between day and month */
p += 4;
*p++ = _T(' ');
p = store_dt(p, ptm->tm_mday); /* day of the month (1-31) */
*p++ = _T(' ');
p = store_dt(p, ptm->tm_hour); /* hours (0-23) */
*p++ = _T(':');
p = store_dt(p, ptm->tm_min); /* minutes (0-59) */
*p++ = _T(':');
p = store_dt(p, ptm->tm_sec); /* seconds (0-59) */
*p++ = _T(' ');
p = store_dt(p, 19 + (ptm->tm_year/100)); /* year (after 1900) */
p = store_dt(p, ptm->tm_year%100);
*p++ = _T('\n');
*p = _T('\0');
return ((char *) buf);
#endif
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Cross platform random number generator
#define RANa 16807 // multiplier
#define LONGRAND_MAX 2147483647L // 2**31 - 1
static long randomnum = 1;
static long nextlongrand(long seed)
{
unsigned
long lo, hi;
lo = RANa *(unsigned long)(seed & 0xFFFF);
hi = RANa *((unsigned long)seed >> 16);
lo += (hi & 0x7FFF) << 16;
if (lo > LONGRAND_MAX)
{
lo &= LONGRAND_MAX;
++lo;
}
lo += hi >> 15;
if (lo > LONGRAND_MAX)
{
lo &= LONGRAND_MAX;
++lo;
}
return(long)lo;
}
// return next random long
static long longrand(void)
{
randomnum = nextlongrand(randomnum);
return randomnum;
}
// to seed it
void Util_RandSeed(unsigned long seed)
{
// nonzero seed
randomnum = seed ? (long)(seed & LONGRAND_MAX) : 1;
}
int Util_RandInt(int low, int high)
{
unsigned int range = (unsigned int)high-low;
int num;
if (range == 0)
return (low); // Prevent divide by zero
num = (int)(longrand() % range);
return(num + low);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/*****************************
UNICODE ENCODING
******************************/
static void QuartToTrip(char *quart, char *trip, int inlen)
{
if (inlen >= 2)
trip[0] = (char)(quart[0] << 2 | quart[1] >> 4);
if (inlen >= 3)
trip[1] = (char)((quart[1] & 0x0F) << 4 | quart[2] >> 2);
if (inlen >= 4)
trip[2] = (char)((quart[2] & 0x3) << 6 | quart[3]);
}
static void TripToQuart(const char *trip, char *quart, int inlen)
{
unsigned char triptemp[3];
int i;
for (i = 0; i < inlen ; i++)
{
triptemp[i] = (unsigned char)trip[i];
}
while (i < 3) //fill the rest with 0
{
triptemp[i] = 0;
i++;
}
quart[0] = (char)(triptemp[0] >> 2);
quart[1] = (char)(((triptemp[0] & 3) << 4) | (triptemp[1] >> 4));
quart[2] = (char)((triptemp[1] & 0x0F) << 2 | (triptemp[2] >> 6));
quart[3] = (char)(triptemp[2] & 0x3F);
}
const char defaultEncoding[] = {'+','/','='};
const char alternateEncoding[] = {'[',']','_'};
const char urlSafeEncodeing[] = {'-','_','='};
void B64Decode(const char *input, char *output, int inlen, int * outlen, int encodingType)
{
const char *encoding = NULL;
const char *holdin = input;
int readpos = 0;
int writepos = 0;
char block[4];
//int outlen = -1;
//int inlen = (int)strlen(input);
// 10-31-2004 : Added by Saad Nader
// now supports URL safe encoding
////////////////////////////////////////////////
switch(encodingType)
{
case 1:
encoding = alternateEncoding;
break;
case 2:
encoding = urlSafeEncodeing;
break;
default: encoding = defaultEncoding;
}
GS_ASSERT(inlen >= 0);
if (inlen <= 0)
{
if (outlen)
*outlen = 0;
output[0] = '\0';
return;
}
// Break at end of string or padding character
while (readpos < inlen && input[readpos] != encoding[2])
{
// 'A'-'Z' maps to 0-25
// 'a'-'z' maps to 26-51
// '0'-'9' maps to 52-61
// 62 maps to encoding[0]
// 63 maps to encoding[1]
if (input[readpos] >= '0' && input[readpos] <= '9')
block[readpos%4] = (char)(input[readpos] - 48 + 52);
else if (input[readpos] >= 'a' && input[readpos] <= 'z')
block[readpos%4] = (char)(input[readpos] - 71);
else if (input[readpos] >= 'A' && input[readpos] <= 'Z')
block[readpos%4] = (char)(input[readpos] - 65);
else if (input[readpos] == encoding[0])
block[readpos%4] = 62;
else if (input[readpos] == encoding[1])
block[readpos%4] = 63;
// padding or '\0' characters also mark end of input
else if (input[readpos] == encoding[2])
break;
else if (input[readpos] == '\0')
break;
else
{
// (assert(0)); //bad input data
if (outlen)
*outlen = 0;
output[0] = '\0';
return; //invaid data
}
// every 4 bytes, convert QuartToTrip into destination
if (readpos%4==3) // zero based, so (3%4) means four bytes, 0-1-2-3
{
QuartToTrip(block, &output[writepos], 4);
writepos += 3;
}
readpos++;
}
// Convert any leftover characters in block
if ((readpos != 0) && (readpos%4 != 0))
{
// fill block with pad (required for QuartToTrip)
memset(&block[readpos%4], encoding[2], (unsigned int)4-(readpos%4));
QuartToTrip(block, &output[writepos], readpos%4);
// output bytes depend on the number of non-pad input bytes
if (readpos%4 == 3)
writepos += 2;
else
writepos += 1;
}
if (outlen)
*outlen = writepos;
GSI_UNUSED(holdin);
}
void B64Encode(const char *input, char *output, int inlen, int encodingType)
{
const char *encoding;
char *holdout = output;
char *lastchar;
int todo = inlen;
// 10-31-2004 : Added by Saad Nader
// now supports URL safe encoding
////////////////////////////////////////////////
switch(encodingType)
{
case 1:
encoding = alternateEncoding;
break;
case 2:
encoding = urlSafeEncodeing;
break;
default: encoding = defaultEncoding;
}
//assume interval of 3
while (todo > 0)
{
TripToQuart(input, output, min(todo, 3));
output += 4;
input += 3;
todo -= 3;
}
lastchar = output;
if (inlen % 3 == 1)
lastchar -= 2;
else if (inlen % 3 == 2)
lastchar -= 1;
*output = 0; //null terminate!
while (output > holdout)
{
output--;
if (output >= lastchar) //pad the end
*output = encoding[2];
else if (*output <= 25)
*output = (char)(*output + 65);
else if (*output <= 51)
*output = (char)(*output + 71);
else if (*output <= 61)
*output = (char)(*output + 48 - 52);
else if (*output == 62)
*output = encoding[0];
else if (*output == 63)
*output = encoding[1];
}
}
int B64DecodeLen(const char *input, int encodingType)
{
const char *encoding;
const char *holdin = input;
switch(encodingType)
{
case 1:
encoding = alternateEncoding;
break;
case 2:
encoding = urlSafeEncodeing;
break;
default: encoding = defaultEncoding;
}
while (*input)
{
if (*input == encoding[2])
return (input - holdin) / 4 * 3 + (input - holdin - 1) % 4;
input++;
}
return (input - holdin) / 4 * 3;
}
void B64InitEncodeStream(B64StreamData *data, const char *input, int len, int encodingType)
{
data->input = input;
data->len = len;
data->encodingType = encodingType;
}
gsi_bool B64EncodeStream(B64StreamData *data, char output[4])
{
const char *encoding;
char *c;
int i;
if(data->len <= 0)
return gsi_false;
// 10-31-2004 : Added by Saad Nader
// now supports URL safe encoding
////////////////////////////////////////////////
switch(data->encodingType)
{
case 1:
encoding = alternateEncoding;
break;
case 2:
encoding = urlSafeEncodeing;
break;
default: encoding = defaultEncoding;
}
TripToQuart(data->input, output, min(data->len, 3));
data->input += 3;
data->len -= 3;
for(i = 0 ; i < 4 ; i++)
{
c = &output[i];
if (*c <= 25)
*c = (char)(*c + 65);
else if (*c <= 51)
*c = (char)(*c + 71);
else if (*c <= 61)
*c = (char)(*c + 48 - 52);
else if (*c == 62)
*c = encoding[0];
else if (*c == 63)
*c = encoding[1];
}
if(data->len < 0)
{
output[3] = encoding[2];
if(data->len == -2)
output[2] = encoding[2];
}
return gsi_true;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void gsiPadRight(char *cArray, char padChar, int cLength);
char * gsiXxteaAlg(const char *sIn, int nIn, char key[XXTEA_KEY_SIZE], int bEnc, int *nOut);
void gsiPadRight(char *cArray, char padChar, int cLength)
{
int diff;
int length = (int)strlen(cArray);
diff = cLength - length;
memset(&cArray[length], padChar, (size_t)diff);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// The heart of the XXTEA encryption/decryption algorithm.
//
// sIn: Input stream.
// nIn: Input length (bytes).
// key: Key (only first 128 bits are significant).
// bEnc: Encrypt (else decrypt)?
char * gsiXxteaAlg(const char *sIn, int nIn, char key[XXTEA_KEY_SIZE], int bEnc, int *nOut)
{
int i, p, n1;
unsigned int *k, *v, z, y;
char *oStr = NULL, *pStr = NULL;
char *sIn2 = NULL;
/////////////////////////////////
// ERROR CHECK!
if (!sIn || !key[0] || nIn == 0)
return NULL;
// Convert stream length to a round number of 32-bit words
// Convert byte count to 32-bit word count
if (nIn % 4 == 0) // Fix for null terminated strings divisible by 4
nIn = (nIn/4)+1;
else
nIn = (nIn + 3)/4;
if ( nIn <= 1 ) // XXTEA requires at least 64 bits
nIn = 2;
// Load and zero-pad first 16 characters (128 bits) of key
gsiPadRight( key , '\0', XXTEA_KEY_SIZE);
k = (unsigned int *)key;
// Load and zero-pad entire input stream as 32-bit words
sIn2 = (char *)gsimalloc((size_t)(4 * nIn));
strcpy(sIn2, sIn);
gsiPadRight( sIn2, '\0', 4*nIn);
v = (unsigned int *)sIn2;
// Prepare to encrypt or decrypt
n1 = nIn - 1;
z = v[ n1 ];
y = v[ 0 ];
i = ( int )( 6 + 52/nIn );
if (bEnc == 1) // Encrypt
{
unsigned int sum = 0;
while ( i-- != 0 )
{
int e;
sum += 0x9E3779B9;
e = ( int )( sum >> 2 );
for ( p = -1; ++p < nIn; )
{
y = v[( p < n1 ) ? p + 1 : 0 ];
z = ( v[ p ] +=
( (( z >> 5 ) ^ ( y << 2 ))
+ (( y >> 3 ) ^ ( z << 4 )))
^ ( ( sum ^ y )
+ ( k[( p ^ e ) & 3 ] ^ z )));
}
}
}
else if (bEnc == 0) // Decrypt
{
unsigned int sum = ( unsigned int ) i * 0x9E3779B9;
while ( sum != 0 )
{
int e = ( int )( sum >> 2 );
for ( p = nIn; p-- != 0; )
{
z = v[( p != 0 ) ? p - 1 : n1 ];
y = ( v[ p ] -=
( (( z >> 5 ) ^ ( y << 2 ))
+ (( y >> 3 ) ^ ( z << 4 )))
^ ( ( sum ^ y )
+ ( k[( p ^ e ) & 3 ] ^ z )));
}
sum -= 0x9E3779B9;
}
}
else return NULL;
// Convert result from 32-bit words to a byte stream
oStr = (char *)gsimalloc((size_t)(4 * nIn + 1));
pStr = oStr;
*nOut = 4 *nIn;
for ( i = -1; ++i < nIn; )
{
unsigned int q = v[ i ];
*pStr++ = (char)(q & 0xFF);
*pStr++ = (char)(( q >> 8 ) & 0xFF);
*pStr++ = (char)(( q >> 16 ) & 0xFF);
*pStr++ = (char)(( q >> 24 ) & 0xFF);
}
*pStr = '\0';
gsifree(sIn2);
return oStr;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// XXTEA Encrpyt
// params
// iStr : the input string to be encrypted
// iLength : the length of the input string
// key : the key used to encrypt
char * gsXxteaEncrypt(const char * iStr, int iLength, char key[XXTEA_KEY_SIZE], int *oLength)
{
return gsiXxteaAlg( iStr, iLength, key, 1, oLength );
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// XXTEA Decrypt
// params
// iStr : the input string to be decrypted
// iLength : the length of the input string
// key : the key used to decrypt
char * gsXxteaDecrypt(const char * iStr, int iLength, char key[XXTEA_KEY_SIZE], int *oLength)
{
return gsiXxteaAlg( iStr, iLength, key, 0, oLength);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#if defined(_DEBUG)
void gsiCheckStack(void)
{
#if defined(_NITRO)
#if 1
OS_CheckStack(OS_GetCurrentThread());
#elif 1
static gsi_bool checkFailed = gsi_false;
if(!checkFailed)
{
OSStackStatus status = OS_GetStackStatus(OS_GetCurrentThread());
if(status != 0)
{
const char * reason;
if(status == OS_STACK_OVERFLOW)
reason = "OVERFLOW";
else if(status == OS_STACK_ABOUT_TO_OVERFLOW)
reason = "ABOUT TO OVERFLOW";
else if(status == OS_STACK_UNDERFLOW)
reason = "UNDERFLOW";
else
reason = "UNKOWN REASON";
OS_TPrintf("STACK CHECK FAILED!: %s\n", reason);
checkFailed = gsi_true;
}
}
#endif
#endif // nitro
}
#endif // _DEBUG
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#ifdef SN_SYSTEMS
int GOAGetLastError(SOCKET s)
{
int val = 0;
int soval = sizeof(val);
if (0 != getsockopt(s,SOL_SOCKET,SO_ERROR,&val,&soval))
return 0; // getsockopt failed
else
return val;
}
#endif
#ifdef _NITRO
static const char * GOAGetUniqueID_Internal(void)
{
static char keyval[17];
u8 MAC[MAC_ALEN];
// check if we already have the Unique ID
if(keyval[0])
return keyval;
// get the MAC
IP_GetMacAddr(NULL, MAC);
// format it
sprintf(keyval, "%02X%02X%02X%02X%02X%02X0000",
MAC[0] & 0xFF,
MAC[1] & 0xFF,
MAC[2] & 0xFF,
MAC[3] & 0xFF,
MAC[4] & 0xFF,
MAC[5] & 0xFF);
return keyval;
}
#endif
#ifdef _PS2
#ifdef UNIQUEID
#if defined(EENET)
#include <net/if_dl.h>
// Removed due to updated sony libraries, Saad Nader
//#include <net/if_types.h>
#include <net/if_ether.h>
static const char * GetMAC(void)
{
static struct sceEENetEtherAddr linkAddress;
struct sceEENetIfname * interfaces;
struct sceEENetIfname * interface;
int num;
int type;
int len;
int i;
const unsigned char * MAC = NULL;
// get the local interfaces
sceEENetGetIfnames(NULL, &num);
interfaces = (struct sceEENetIfname *)gsimalloc(num * sizeof(struct sceEENetIfname));
if(!interfaces)
return NULL;
sceEENetGetIfnames(interfaces, &num);
// loop through the interfaces
for(i = 0 ; i < num ; i++)
{
// the next interface
interface = &interfaces[i];
//printf("eenet%d: %s\n", i, interface->ifn_name);
// get the type
len = sizeof(type);
if(sceEENetGetIfinfo(interface->ifn_name, sceEENET_IFINFO_IFTYPE, &type, &len) != 0)
continue;
//printf("eenet%d type: %d\n", i, type);
// check for ethernet
if(type != sceEENET_IFTYPE_ETHER)
continue;
//printf("eenet%d: ethernet\n", i);
// get the address
len = sizeof(linkAddress);
if(sceEENetGetIfinfo(interface->ifn_name, sceEENET_IFINFO_MACADDR, &linkAddress, &len) != 0)
continue;
MAC = linkAddress.ether_addr_octet;
//printf("eenet%d: MAC: %02X-%02X-%02X-%02X-%02X-%02X\n", i, MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
break;
}
// free the interfaces
gsifree(interfaces);
return MAC;
}
#elif defined(SN_SYSTEMS)
static const char * GetMAC(void)
{
static char MAC[6];
int len = sizeof(MAC);
int rcode;
// get the MAC
rcode = sndev_get_status(0, SN_DEV_STAT_MAC, MAC, &len);
if((rcode != 0) || (len != 6))
return NULL;
return MAC;
}
#elif defined(INSOCK)
static const char * GetMAC(void)
{
// Get the MAC address using the interface control
static char MAC[16];
extern sceSifMClientData gGSIInsockClientData;
extern u_int gGSIInsockSocketBuffer[NETBUFSIZE] __attribute__((aligned(64)));
int result = sceInetInterfaceControl(&gGSIInsockClientData, &gGSIInsockSocketBuffer,
1, sceInetCC_GetHWaddr, MAC, sizeof(MAC));
if (result == sceINETE_OK)
return MAC;
// error
return NULL;
}
#endif
static const char * GOAGetUniqueID_Internal(void)
{
static char keyval[17];
const char * MAC;
// check if we already have the Unique ID
if(keyval[0])
return keyval;
// get the MAC
MAC = GetMAC();
if(!MAC)
{
// error getting the MAC
static char errorMAC[6] = { 1, 2, 3, 4, 5, 6 };
MAC = errorMAC;
}
// format it
sprintf(keyval, "%02X%02X%02X%02X%02X%02X0000",
MAC[0] & 0xFF,
MAC[1] & 0xFF,
MAC[2] & 0xFF,
MAC[3] & 0xFF,
MAC[4] & 0xFF,
MAC[5] & 0xFF);
return keyval;
}
#endif // UNIQUEID
#endif // _PS2
#if ((defined(_WIN32) && !defined(_XBOX)) || defined(_UNIX))
static void GenerateID(char *keyval)
{
int i;
const char crypttab[63] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
#ifdef _WIN32
LARGE_INTEGER l1;
UINT seed;
if (QueryPerformanceCounter(&l1))
seed = (l1.LowPart ^ l1.HighPart);
else
seed = 0;
Util_RandSeed(seed ^ GetTickCount() ^ (unsigned long)time(NULL) ^ clock());
#else
Util_RandSeed(time(NULL) ^ clock());
#endif
for (i = 0; i < 19; i++)
if (i == 4 || i == 9 || i == 14)
keyval[i] = '-';
else
keyval[i] = crypttab[Util_RandInt(0, 62)];
keyval[19] = 0;
}
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#ifdef _WIN32
#define REG_KEY "Software\\GameSpy\\GameSpy 3D\\Registration"
#endif
const char * GOAGetUniqueID_Internal(void)
{
static char keyval[PATH_MAX] = "";
unsigned int ret;
#ifdef _WIN32
int docreate;
HKEY thekey;
DWORD thetype = REG_SZ;
DWORD len = MAX_PATH;
DWORD disp;
if (RegOpenKeyExA(HKEY_CURRENT_USER, REG_KEY, 0, KEY_ALL_ACCESS, &thekey) != ERROR_SUCCESS)
docreate = 1;
else
docreate = 0;
ret = RegQueryValueExA(thekey, (LPCSTR)"Crypt", 0, &thetype, (LPBYTE)keyval, &len);
#else
FILE *f;
f = fopen("id.bin","r");
if (!f)
ret = 0;
else
{
ret = fread(keyval,1,19,f);
keyval[ret] = 0;
fclose(f);
}
#endif
if (ret != 0 || strlen(keyval) != 19)//need to generate a new key
{
GenerateID(keyval);
#ifdef _WIN32
if (docreate)
{
ret = RegCreateKeyExA(HKEY_CURRENT_USER, REG_KEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &thekey, &disp);
}
RegSetValueExA(thekey, (LPCSTR)"Crypt", 0, REG_SZ, (const LPBYTE)keyval, strlen(keyval)+1);
#else
f = fopen("id.bin","w");
if (f)
{
fwrite(keyval,1,19,f);
fclose(f);
} else
keyval[0] = 0; //don't generate one each time!!
#endif
}
#ifdef _WIN32
RegCloseKey(thekey);
#endif
// Strip out the -'s.
/////////////////////
memmove(keyval + 4, keyval + 5, 4);
memmove(keyval + 8, keyval + 10, 4);
memmove(keyval + 12, keyval + 15, 4);
keyval[16] = '\0';
return keyval;
}
#endif
#ifdef _PSP
// Included here so that the implementation can appear in gsPlatformPSP.c
const char * GOAGetUniqueID_Internal(void);
#endif
#if (!defined(_PS2) && !defined(_PS3) && !defined(_XBOX) && !defined(_PSP)) || defined(UNIQUEID)
GetUniqueIDFunction GOAGetUniqueID = GOAGetUniqueID_Internal;
#endif
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#if defined(__cplusplus)
}
#endif
|