Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1700 lines
52 KiB

  1. /*
  2. ** Copyright 1995-2095, Silicon Graphics, Inc.
  3. ** All Rights Reserved.
  4. **
  5. ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6. ** the contents of this file may not be disclosed to third parties, copied or
  7. ** duplicated in any form, in whole or in part, without the prior written
  8. ** permission of Silicon Graphics, Inc.
  9. **
  10. ** RESTRICTED RIGHTS LEGEND:
  11. ** Use, duplication or disclosure by the Government is subject to restrictions
  12. ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13. ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14. ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15. ** rights reserved under the Copyright Laws of the United States.
  16. */
  17. #include "glslib.h"
  18. #include <stdlib.h>
  19. #include <string.h>
  20. /******************************************************************************
  21. Array
  22. ******************************************************************************/
  23. void __glsArray_final(__GLSarray *inoutArray) {
  24. free(inoutArray->base);
  25. __glsArray_init(inoutArray);
  26. }
  27. void __glsArray_init(__GLSarray *outArray) {
  28. outArray->base = GLS_NONE;
  29. outArray->bufCount = outArray->count = 0;
  30. }
  31. void __glsArray_compact(__GLSarray *inoutArray, size_t inElemSize) {
  32. if (inoutArray->bufCount == inoutArray->count) return;
  33. if (inoutArray->count) {
  34. GLvoid *const newBase = realloc(
  35. inoutArray->base, inoutArray->count * inElemSize
  36. );
  37. if (newBase) {
  38. inoutArray->base = newBase;
  39. inoutArray->bufCount = inoutArray->count;
  40. }
  41. } else {
  42. __glsArray_final(inoutArray);
  43. }
  44. }
  45. void __glsArray_delete(
  46. __GLSarray *inoutArray,
  47. size_t inIndex,
  48. size_t inCount,
  49. size_t inElemSize
  50. ) {
  51. if (inIndex >= inoutArray->count) return;
  52. if (inIndex + inCount >= inoutArray->count) {
  53. inoutArray->count = inIndex;
  54. } else {
  55. GLubyte *const p = (GLubyte *)inoutArray->base + inIndex * inElemSize;
  56. memmove(
  57. p,
  58. p + inCount * inElemSize,
  59. (inoutArray->count - (inIndex + inCount)) * inElemSize
  60. );
  61. inoutArray->count -= inCount;
  62. }
  63. }
  64. GLboolean __glsArray_insert(
  65. __GLSarray *inoutArray,
  66. size_t inIndex,
  67. size_t inCount,
  68. GLboolean inInit,
  69. size_t inElemSize
  70. ) {
  71. const size_t newCount = inoutArray->count + inCount;
  72. if (newCount > inoutArray->bufCount) {
  73. size_t newBufCount = __glsCeilBase2(newCount);
  74. GLvoid *buf = malloc(newBufCount * inElemSize);
  75. if (!buf) {
  76. newBufCount = newCount;
  77. buf = __glsMalloc(newBufCount * inElemSize);
  78. if (!buf) return GL_FALSE;
  79. }
  80. memmove(buf, inoutArray->base, inoutArray->count);
  81. free(inoutArray->base);
  82. inoutArray->base = buf;
  83. inoutArray->bufCount = newBufCount;
  84. }
  85. if (inIndex < inoutArray->count) {
  86. GLubyte *const p = (GLubyte *)inoutArray->base + inIndex * inElemSize;
  87. memmove(
  88. p + inCount * inElemSize,
  89. p,
  90. (inoutArray->count - inIndex) * inElemSize
  91. );
  92. } else {
  93. inIndex = inoutArray->count;
  94. }
  95. if (inInit) {
  96. memset(
  97. (GLubyte *)inoutArray->base + inIndex * inElemSize,
  98. 0,
  99. inCount * inElemSize
  100. );
  101. }
  102. inoutArray->count = newCount;
  103. return GL_TRUE;
  104. }
  105. /******************************************************************************
  106. Checksum
  107. ******************************************************************************/
  108. #define __GLS_CRC32_POLY 0x04c11db7 /* AUTODIN II, Ethernet, & FDDI */
  109. #if __GLS_UNUSED
  110. static void __glsCRC32tableInit(void) {
  111. GLuint c, i, j;
  112. for (i = 0; i < 256; ++i) {
  113. for (c = i << 24, j = 8; j ; --j) {
  114. c = c & 0x80000000 ? (c << 1) ^ __GLS_CRC32_POLY : (c << 1);
  115. }
  116. __glsCRC32table[i] = c;
  117. }
  118. }
  119. #endif /* __GLS_UNUSED */
  120. const GLuint __glsCRC32table[256] = {
  121. 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
  122. 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
  123. 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
  124. 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
  125. 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9,
  126. 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
  127. 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011,
  128. 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
  129. 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
  130. 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
  131. 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81,
  132. 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
  133. 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49,
  134. 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
  135. 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
  136. 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
  137. 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae,
  138. 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
  139. 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
  140. 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
  141. 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
  142. 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
  143. 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066,
  144. 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
  145. 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e,
  146. 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
  147. 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
  148. 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
  149. 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
  150. 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
  151. 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686,
  152. 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
  153. 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
  154. 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
  155. 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f,
  156. 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
  157. 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47,
  158. 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
  159. 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
  160. 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
  161. 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7,
  162. 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
  163. 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f,
  164. 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
  165. 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
  166. 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
  167. 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f,
  168. 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
  169. 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
  170. 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
  171. 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
  172. 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
  173. 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30,
  174. 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
  175. 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088,
  176. 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
  177. 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
  178. 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
  179. 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
  180. 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
  181. 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0,
  182. 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
  183. 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
  184. 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4,
  185. };
  186. /******************************************************************************
  187. Dict
  188. ******************************************************************************/
  189. typedef struct {
  190. __GLS_LIST_ELEM;
  191. union {
  192. GLint intKey;
  193. const GLubyte *strKey;
  194. } key;
  195. union {
  196. GLint intVal;
  197. GLvoid *ptrVal;
  198. } val;
  199. } __GLSdictEntry;
  200. typedef __GLS_LIST(__GLSdictEntry) __GLSdictEntryList;
  201. typedef __GLS_LIST_ITER(__GLSdictEntry) __GLSdictEntryIter;
  202. struct __GLSdict{
  203. size_t count;
  204. __GLSdictEntryList *table;
  205. size_t tableMax;
  206. GLboolean staticKeys;
  207. };
  208. static __GLSdict* __glsDict_create(
  209. size_t inTableCount, GLboolean inStaticKeys
  210. ) {
  211. __GLSdict *outDict;
  212. if (inTableCount < 1) return GLS_NONE;
  213. outDict = __glsCalloc(1, sizeof(__GLSdict));
  214. if (!outDict) return GLS_NONE;
  215. outDict->staticKeys = inStaticKeys;
  216. inTableCount = __glsCeilBase2(inTableCount);
  217. outDict->table = __glsCalloc(inTableCount, sizeof(__GLSdictEntryList));
  218. if (!outDict->table) return __glsStrDict_destroy(outDict);
  219. outDict->tableMax = inTableCount - 1;
  220. return outDict;
  221. }
  222. __GLS_FORWARD static __GLSdictEntry* __glsIntDictEntry_destroy(
  223. __GLSdictEntry *inEntry
  224. );
  225. static __GLSdictEntry* __glsIntDictEntry_create(GLint inKey) {
  226. __GLSdictEntry *const outEntry = __glsCalloc(1, sizeof(__GLSdictEntry));
  227. if (!outEntry) return GLS_NONE;
  228. outEntry->key.intKey = inKey;
  229. return outEntry;
  230. }
  231. static __GLSdictEntry* __glsIntDictEntry_destroy(__GLSdictEntry *inEntry) {
  232. free(inEntry);
  233. return GLS_NONE;
  234. }
  235. static size_t __glsIntHash(const __GLSdict *inDict, GLint inKey) {
  236. return inKey & inDict->tableMax;
  237. }
  238. static GLboolean __glsIntDict_add(
  239. __GLSdict *inoutDict, GLint inKey, __GLSdictEntry *inEntry
  240. ) {
  241. if (inoutDict->count++ > inoutDict->tableMax) {
  242. const size_t newTableSize = (inoutDict->tableMax + 1) * 2;
  243. __GLSdictEntryList *const newTable = (
  244. __glsCalloc(newTableSize, sizeof(__GLSdictEntryList))
  245. );
  246. if (newTable) {
  247. size_t i = inoutDict->tableMax;
  248. inoutDict->tableMax = newTableSize - 1;
  249. for ( ; i != (size_t)-1; --i) {
  250. __GLSdictEntry *oldEntry;
  251. while (oldEntry = inoutDict->table[i].head) {
  252. __GLS_LIST_REMOVE(inoutDict->table + i, oldEntry);
  253. __GLS_LIST_APPEND(
  254. newTable + __glsIntHash(inoutDict, oldEntry->key.intKey),
  255. oldEntry
  256. );
  257. }
  258. }
  259. free(inoutDict->table);
  260. inoutDict->table = newTable;
  261. }
  262. }
  263. __GLS_LIST_PREPEND(
  264. inoutDict->table + __glsIntHash(inoutDict, inKey), inEntry
  265. );
  266. return GL_TRUE;
  267. }
  268. __GLSdict* __glsIntDict_create(size_t inTableCount) {
  269. return __glsDict_create(inTableCount, GL_FALSE);
  270. }
  271. __GLSdict* __glsIntDict_destroy(__GLSdict *inDict) {
  272. size_t i;
  273. if (!inDict) return GLS_NONE;
  274. if (inDict->table) for (i = inDict->tableMax ; i != (size_t)-1; --i) {
  275. __GLS_LIST_CLEAR_DESTROY(inDict->table + i, __glsIntDictEntry_destroy);
  276. }
  277. free(inDict->table);
  278. free(inDict);
  279. return GLS_NONE;
  280. }
  281. void __glsIntDict_print(__GLSdict *inoutDict, const GLubyte *inName) {
  282. size_t i;
  283. fprintf(stdout, "%s(\n", inName);
  284. for (i = 0 ; i <= inoutDict->tableMax ; ++i) {
  285. __GLSdictEntryIter iter;
  286. __GLS_LIST_FIRST(inoutDict->table + i, &iter);
  287. if (iter.elem) {
  288. fprintf(stdout, " ");
  289. while (iter.elem) {
  290. fprintf(stdout, " %d", iter.elem->key.intKey);
  291. __GLS_LIST_NEXT(inoutDict->table + i, &iter);
  292. }
  293. fprintf(stdout, "\n");
  294. }
  295. }
  296. fprintf(stdout, ")\n");
  297. }
  298. void __glsIntDict_remove(__GLSdict *inoutDict, GLint inKey) {
  299. __GLSdictEntryIter iter;
  300. __GLSdictEntryList *const list = (
  301. inoutDict->table + __glsIntHash(inoutDict, inKey)
  302. );
  303. __GLS_LIST_FIRST(list, &iter);
  304. while (iter.elem) {
  305. if (inKey == iter.elem->key.intKey) {
  306. __GLS_LIST_REMOVE_DESTROY(
  307. list, iter.elem, __glsIntDictEntry_destroy
  308. );
  309. --inoutDict->count;
  310. return;
  311. }
  312. __GLS_LIST_NEXT(list, &iter);
  313. }
  314. }
  315. GLboolean __glsInt2IntDict_add(
  316. __GLSdict *inoutDict, GLint inKey, GLint inVal
  317. ) {
  318. __GLSdictEntry *entry;
  319. if (__glsInt2IntDict_find(inoutDict, inKey, GLS_NONE)) return GL_FALSE;
  320. entry = __glsIntDictEntry_create(inKey);
  321. if (!entry) return GL_FALSE;
  322. entry->val.intVal = inVal;
  323. return __glsIntDict_add(inoutDict, inKey, entry);
  324. }
  325. GLboolean __glsInt2PtrDict_add(
  326. __GLSdict *inoutDict, GLint inKey, GLvoid *inVal
  327. ) {
  328. __GLSdictEntry *entry;
  329. if (__glsInt2PtrDict_find(inoutDict, inKey)) return GL_FALSE;
  330. entry = __glsIntDictEntry_create(inKey);
  331. if (!entry) return GL_FALSE;
  332. entry->val.ptrVal = inVal;
  333. return __glsIntDict_add(inoutDict, inKey, entry);
  334. }
  335. GLboolean __glsInt2IntDict_find(
  336. const __GLSdict *inDict, GLint inKey, GLint *optoutVal
  337. ) {
  338. __GLSdictEntryIter iter;
  339. __GLSdictEntryList *const list = (
  340. inDict->table + __glsIntHash(inDict, inKey)
  341. );
  342. __GLS_LIST_FIRST(list, &iter);
  343. while (iter.elem) {
  344. if (inKey == iter.elem->key.intKey) {
  345. if (optoutVal) *optoutVal = iter.elem->val.intVal;
  346. return GL_TRUE;
  347. }
  348. __GLS_LIST_NEXT(list, &iter);
  349. }
  350. return GL_FALSE;
  351. }
  352. GLvoid* __glsInt2PtrDict_find(const __GLSdict *inDict, GLint inKey) {
  353. __GLSdictEntryIter iter;
  354. __GLSdictEntryList *const list = (
  355. inDict->table + __glsIntHash(inDict, inKey)
  356. );
  357. __GLS_LIST_FIRST(list, &iter);
  358. while (iter.elem) {
  359. if (inKey == iter.elem->key.intKey) {
  360. return iter.elem->val.ptrVal;
  361. }
  362. __GLS_LIST_NEXT(list, &iter);
  363. }
  364. return GLS_NONE;
  365. }
  366. GLboolean __glsInt2IntDict_replace(
  367. __GLSdict *inoutDict, GLint inKey, GLint inVal
  368. ) {
  369. __GLSdictEntryIter iter;
  370. __GLSdictEntryList *const list = (
  371. inoutDict->table + __glsIntHash(inoutDict, inKey)
  372. );
  373. __GLS_LIST_FIRST(list, &iter);
  374. while (iter.elem) {
  375. if (inKey == iter.elem->key.intKey) {
  376. iter.elem->val.intVal = inVal;
  377. return GL_TRUE;
  378. }
  379. __GLS_LIST_NEXT(list, &iter);
  380. }
  381. return GL_FALSE;
  382. }
  383. GLboolean __glsInt2PtrDict_replace(
  384. __GLSdict *inoutDict, GLint inKey, GLvoid *inVal
  385. ) {
  386. __GLSdictEntryIter iter;
  387. __GLSdictEntryList *const list = (
  388. inoutDict->table + __glsIntHash(inoutDict, inKey)
  389. );
  390. __GLS_LIST_FIRST(list, &iter);
  391. while (iter.elem) {
  392. if (inKey == iter.elem->key.intKey) {
  393. iter.elem->val.ptrVal = inVal;
  394. return GL_TRUE;
  395. }
  396. __GLS_LIST_NEXT(list, &iter);
  397. }
  398. return GL_FALSE;
  399. }
  400. __GLS_FORWARD static __GLSdictEntry* __glsStrDictEntry_destroy(
  401. __GLSdictEntry *inEntry, GLboolean inStaticKey
  402. );
  403. static __GLSdictEntry* __glsStrDictEntry_create(
  404. const GLubyte *inKey, GLboolean inStaticKey
  405. ) {
  406. __GLSdictEntry *const outEntry = __glsCalloc(1, sizeof(__GLSdictEntry));
  407. if (!outEntry) return GLS_NONE;
  408. if (inStaticKey) {
  409. outEntry->key.strKey = inKey;
  410. } else {
  411. GLubyte *const buf = __glsMalloc(strlen((const char *)inKey) + 1);
  412. if (!buf) {
  413. return __glsStrDictEntry_destroy(outEntry, GL_FALSE);
  414. }
  415. strcpy((char *)buf, (const char *)inKey);
  416. outEntry->key.strKey = buf;
  417. }
  418. return outEntry;
  419. }
  420. static __GLSdictEntry* __glsStrDictEntry_destroy(
  421. __GLSdictEntry *inEntry, GLboolean inStaticKey
  422. ) {
  423. if (!inEntry) return GLS_NONE;
  424. if (!inStaticKey) free((GLvoid *)inEntry->key.strKey);
  425. free(inEntry);
  426. return GLS_NONE;
  427. }
  428. static size_t __glsStrHash(const __GLSdict *inDict, const GLubyte *inKey) {
  429. GLubyte charVal;
  430. size_t outHash = 0;
  431. while (charVal = *inKey++) outHash += charVal;
  432. return outHash & inDict->tableMax;
  433. }
  434. static GLboolean __glsStrDict_add(
  435. __GLSdict *inoutDict, const GLubyte *inKey, __GLSdictEntry *inEntry
  436. ) {
  437. if (inoutDict->count++ > inoutDict->tableMax) {
  438. const size_t newTableSize = (inoutDict->tableMax + 1) * 2;
  439. __GLSdictEntryList *const newTable = (
  440. __glsCalloc(newTableSize, sizeof(__GLSdictEntryList))
  441. );
  442. if (newTable) {
  443. size_t i = inoutDict->tableMax;
  444. inoutDict->tableMax = newTableSize - 1;
  445. for ( ; i != (size_t)-1; --i) {
  446. __GLSdictEntry *oldEntry;
  447. while (oldEntry = inoutDict->table[i].head) {
  448. __GLS_LIST_REMOVE(inoutDict->table + i, oldEntry);
  449. __GLS_LIST_APPEND(
  450. newTable + __glsStrHash(inoutDict, oldEntry->key.strKey),
  451. oldEntry
  452. );
  453. }
  454. }
  455. free(inoutDict->table);
  456. inoutDict->table = newTable;
  457. }
  458. }
  459. __GLS_LIST_PREPEND(
  460. inoutDict->table + __glsStrHash(inoutDict, inKey), inEntry
  461. );
  462. return GL_TRUE;
  463. }
  464. __GLSdict* __glsStrDict_create(size_t inTableCount, GLboolean inStaticKeys) {
  465. return __glsDict_create(inTableCount, inStaticKeys);
  466. }
  467. __GLSdict* __glsStrDict_destroy(__GLSdict *inDict) {
  468. size_t i;
  469. if (!inDict) return GLS_NONE;
  470. if (inDict->table) for (i = inDict->tableMax ; i != (size_t)-1; --i) {
  471. __GLSdictEntry *entry;
  472. while (entry = inDict->table[i].head) {
  473. __GLS_LIST_REMOVE(inDict->table + i, entry);
  474. __glsStrDictEntry_destroy(entry, inDict->staticKeys);
  475. }
  476. }
  477. free(inDict->table);
  478. free(inDict);
  479. return GLS_NONE;
  480. }
  481. void __glsStrDict_print(__GLSdict *inoutDict, const GLubyte *inName) {
  482. size_t i;
  483. fprintf(stdout, "%s(\n", inName);
  484. for (i = 0 ; i <= inoutDict->tableMax ; ++i) {
  485. __GLSdictEntryIter iter;
  486. __GLS_LIST_FIRST(inoutDict->table + i, &iter);
  487. if (iter.elem) {
  488. fprintf(stdout, " ");
  489. while (iter.elem) {
  490. fprintf(stdout, " %s", iter.elem->key.strKey);
  491. __GLS_LIST_NEXT(inoutDict->table + i, &iter);
  492. }
  493. fprintf(stdout, "\n");
  494. }
  495. }
  496. fprintf(stdout, ")\n");
  497. }
  498. void __glsStrDict_remove(__GLSdict *inoutDict, const GLubyte *inKey) {
  499. __GLSdictEntryIter iter;
  500. __GLSdictEntryList *const list = (
  501. inoutDict->table + __glsStrHash(inoutDict, inKey)
  502. );
  503. __GLS_LIST_FIRST(list, &iter);
  504. while (iter.elem) {
  505. if (
  506. !strcmp((const char *)inKey, (const char *)iter.elem->key.strKey)
  507. ) {
  508. __GLS_LIST_REMOVE(list, iter.elem);
  509. __glsStrDictEntry_destroy(iter.elem, inoutDict->staticKeys);
  510. --inoutDict->count;
  511. return;
  512. }
  513. __GLS_LIST_NEXT(list, &iter);
  514. }
  515. }
  516. GLboolean __glsStr2IntDict_add(
  517. __GLSdict *inoutDict, const GLubyte *inKey, GLint inVal
  518. ) {
  519. __GLSdictEntry *entry;
  520. if (__glsStr2IntDict_find(inoutDict, inKey, GLS_NONE)) return GL_FALSE;
  521. entry = __glsStrDictEntry_create(inKey, inoutDict->staticKeys);
  522. if (!entry) return GL_FALSE;
  523. entry->val.intVal = inVal;
  524. return __glsStrDict_add(inoutDict, inKey, entry);
  525. }
  526. GLboolean __glsStr2PtrDict_add(
  527. __GLSdict *inoutDict, const GLubyte *inKey, GLvoid *inVal
  528. ) {
  529. __GLSdictEntry *entry;
  530. if (__glsStr2PtrDict_find(inoutDict, inKey)) return GL_FALSE;
  531. entry = __glsStrDictEntry_create(inKey, inoutDict->staticKeys);
  532. if (!entry) return GL_FALSE;
  533. entry->val.ptrVal = inVal;
  534. return __glsStrDict_add(inoutDict, inKey, entry);
  535. }
  536. GLboolean __glsStr2IntDict_find(
  537. const __GLSdict *inDict, const GLubyte *inKey, GLint *optoutVal
  538. ) {
  539. __GLSdictEntryIter iter;
  540. __GLSdictEntryList *const list = (
  541. inDict->table + __glsStrHash(inDict, inKey)
  542. );
  543. __GLS_LIST_FIRST(list, &iter);
  544. while (iter.elem) {
  545. if (
  546. !strcmp((const char *)inKey, (const char *)iter.elem->key.strKey)
  547. ) {
  548. if (optoutVal) *optoutVal = iter.elem->val.intVal;
  549. return GL_TRUE;
  550. }
  551. __GLS_LIST_NEXT(list, &iter);
  552. }
  553. return GL_FALSE;
  554. }
  555. GLvoid* __glsStr2PtrDict_find(const __GLSdict *inDict, const GLubyte *inKey) {
  556. __GLSdictEntryIter iter;
  557. __GLSdictEntryList *const list = (
  558. inDict->table + __glsStrHash(inDict, inKey)
  559. );
  560. __GLS_LIST_FIRST(list, &iter);
  561. while (iter.elem) {
  562. if (
  563. !strcmp((const char *)inKey, (const char *)iter.elem->key.strKey)
  564. ) {
  565. return iter.elem->val.ptrVal;
  566. }
  567. __GLS_LIST_NEXT(list, &iter);
  568. }
  569. return GLS_NONE;
  570. }
  571. GLboolean __glsStr2IntDict_replace(
  572. __GLSdict *inoutDict, const GLubyte *inKey, GLint inVal
  573. ) {
  574. __GLSdictEntryIter iter;
  575. __GLSdictEntryList *const list = (
  576. inoutDict->table + __glsStrHash(inoutDict, inKey)
  577. );
  578. __GLS_LIST_FIRST(list, &iter);
  579. while (iter.elem) {
  580. if (
  581. !strcmp((const char *)inKey, (const char *)iter.elem->key.strKey)
  582. ) {
  583. iter.elem->val.intVal = inVal;
  584. return GL_TRUE;
  585. }
  586. __GLS_LIST_NEXT(list, &iter);
  587. }
  588. return GL_FALSE;
  589. }
  590. GLboolean __glsStr2PtrDict_replace(
  591. __GLSdict *inoutDict, const GLubyte *inKey, GLvoid *inVal
  592. ) {
  593. __GLSdictEntryIter iter;
  594. __GLSdictEntryList *const list = (
  595. inoutDict->table + __glsStrHash(inoutDict, inKey)
  596. );
  597. __GLS_LIST_FIRST(list, &iter);
  598. while (iter.elem) {
  599. if (
  600. !strcmp((const char *)inKey, (const char *)iter.elem->key.strKey)
  601. ) {
  602. iter.elem->val.ptrVal = inVal;
  603. return GL_TRUE;
  604. }
  605. __GLS_LIST_NEXT(list, &iter);
  606. }
  607. return GL_FALSE;
  608. }
  609. /******************************************************************************
  610. List
  611. ******************************************************************************/
  612. void __glsListAppend(__GLSlist *inoutList, __GLSlistElem *inoutElem) {
  613. if (inoutList->head) {
  614. inoutList->head->prev->next = inoutElem;
  615. inoutElem->prev = inoutList->head->prev;
  616. inoutElem->next = inoutList->head;
  617. inoutList->head->prev = inoutElem;
  618. } else {
  619. inoutList->head = inoutElem;
  620. inoutElem->prev = inoutElem->next = inoutElem;
  621. }
  622. }
  623. void __glsListClearDestroy(
  624. __GLSlist *inoutList, __GLSlistElemDestructor inDestructor
  625. ) {
  626. while (inoutList->head) {
  627. __glsListRemoveDestroy(inoutList, inoutList->head, inDestructor);
  628. }
  629. }
  630. void __glsListFirst(__GLSlist *inList, __GLSlistIter *inoutIter) {
  631. inoutIter->elem = inList->head;
  632. }
  633. void __glsListLast(__GLSlist *inList, __GLSlistIter *inoutIter) {
  634. inoutIter->elem = inList->head ? inList->head->prev : GLS_NONE;
  635. }
  636. void __glsListNext(__GLSlist *inList, __GLSlistIter *inoutIter) {
  637. if (inoutIter->elem) {
  638. inoutIter->elem = inoutIter->elem->next;
  639. if (inoutIter->elem == inList->head) inoutIter->elem = GLS_NONE;
  640. }
  641. }
  642. void __glsListPrepend(__GLSlist *inoutList, __GLSlistElem *inoutElem) {
  643. if (inoutList->head) {
  644. inoutList->head->prev->next = inoutElem;
  645. inoutElem->prev = inoutList->head->prev;
  646. inoutElem->next = inoutList->head;
  647. inoutList->head->prev = inoutElem;
  648. inoutList->head = inoutElem;
  649. } else {
  650. inoutList->head = inoutElem;
  651. inoutElem->prev = inoutElem->next = inoutElem;
  652. }
  653. }
  654. void __glsListPrev(__GLSlist *inList, __GLSlistIter *inoutIter) {
  655. if (inoutIter->elem) {
  656. inoutIter->elem = (
  657. (inoutIter->elem == inList->head) ?
  658. GLS_NONE :
  659. inoutIter->elem->prev
  660. );
  661. }
  662. }
  663. void __glsListRemove(__GLSlist *inoutList, __GLSlistElem *inoutElem) {
  664. if (inoutList->head == inoutList->head->next) {
  665. inoutList->head = GLS_NONE;
  666. } else {
  667. inoutElem->next->prev = inoutElem->prev;
  668. inoutElem->prev->next = inoutElem->next;
  669. if (inoutList->head == inoutElem) {
  670. inoutList->head = inoutList->head->next;
  671. }
  672. }
  673. }
  674. void __glsListRemoveDestroy(
  675. __GLSlist *inoutList,
  676. __GLSlistElem *inElem,
  677. __GLSlistElemDestructor inDestructor
  678. ) {
  679. __glsListRemove(inoutList, inElem);
  680. inDestructor(inElem);
  681. }
  682. /******************************************************************************
  683. IterList
  684. ******************************************************************************/
  685. void __glsIterListAppend(__GLSiterList *inoutList, __GLSlistElem *inoutElem) {
  686. __glsListAppend((__GLSlist *)&inoutList->head, inoutElem);
  687. ++inoutList->count;
  688. }
  689. void __glsIterListClearDestroy(
  690. __GLSiterList *inoutList, __GLSlistElemDestructor inDestructor
  691. ) {
  692. __glsListClearDestroy((__GLSlist *)&inoutList->head, inDestructor);
  693. inoutList->iterElem = GLS_NONE;
  694. inoutList->count = inoutList->iterIndex = 0;
  695. }
  696. void __glsIterListFirst(__GLSiterList *inoutList) {
  697. __glsListFirst(
  698. (__GLSlist *)&inoutList->head, (__GLSlistIter *)&inoutList->iterElem
  699. );
  700. inoutList->iterIndex = 0;
  701. }
  702. void __glsIterListLast(__GLSiterList *inoutList) {
  703. __glsListLast(
  704. (__GLSlist *)&inoutList->head, (__GLSlistIter *)&inoutList->iterElem
  705. );
  706. inoutList->iterIndex = inoutList->iterElem ? inoutList->count - 1 : 0;
  707. }
  708. void __glsIterListNext(__GLSiterList *inoutList) {
  709. __glsListNext(
  710. (__GLSlist *)&inoutList->head, (__GLSlistIter *)&inoutList->iterElem
  711. );
  712. inoutList->iterIndex = inoutList->iterElem ? inoutList->iterIndex + 1 : 0;
  713. }
  714. void __glsIterListPrepend(__GLSiterList *inoutList, __GLSlistElem *inoutElem) {
  715. __glsListPrepend((__GLSlist *)&inoutList->head, inoutElem);
  716. ++inoutList->count;
  717. if (inoutList->iterElem) ++inoutList->iterIndex;
  718. }
  719. void __glsIterListPrev(__GLSiterList *inoutList) {
  720. __glsListPrev(
  721. (__GLSlist *)&inoutList->head, (__GLSlistIter *)&inoutList->iterElem
  722. );
  723. inoutList->iterIndex = inoutList->iterElem ? inoutList->iterIndex - 1 : 0;
  724. }
  725. void __glsIterListRemove(__GLSiterList *inoutList, __GLSlistElem *inoutElem) {
  726. __glsListRemove((__GLSlist *)&inoutList->head, inoutElem);
  727. inoutList->iterElem = GLS_NONE;
  728. --inoutList->count;
  729. inoutList->iterIndex = 0;
  730. }
  731. void __glsIterListRemoveDestroy(
  732. __GLSiterList *inoutList,
  733. __GLSlistElem *inElem,
  734. __GLSlistElemDestructor inDestructor
  735. ) {
  736. __glsListRemoveDestroy(
  737. (__GLSlist *)&inoutList->head, inElem, inDestructor
  738. );
  739. inoutList->iterElem = GLS_NONE;
  740. --inoutList->count;
  741. inoutList->iterIndex = 0;
  742. }
  743. void __glsIterListSeek(__GLSiterList *inoutList, size_t inIndex) {
  744. if (inIndex < inoutList->count) {
  745. if (!inoutList->iterElem) inoutList->iterElem = inoutList->head;
  746. while (inoutList->iterIndex < inIndex) __glsIterListNext(inoutList);
  747. while (inoutList->iterIndex > inIndex) __glsIterListPrev(inoutList);
  748. }
  749. }
  750. /******************************************************************************
  751. Memory
  752. ******************************************************************************/
  753. GLvoid* __glsCalloc(size_t inCount, size_t inSize) {
  754. GLvoid *outAddr;
  755. if (!inCount) inCount = 1;
  756. if (!inSize) inSize = 1;
  757. outAddr = calloc(inCount, inSize);
  758. if (!outAddr) __GLS_RAISE_ERROR(GLS_OUT_OF_MEMORY);
  759. return outAddr;
  760. }
  761. GLvoid* __glsMalloc(size_t inSize) {
  762. GLvoid *outAddr;
  763. if (!inSize) inSize = 1;
  764. outAddr = malloc(inSize);
  765. if (!outAddr) __GLS_RAISE_ERROR(GLS_OUT_OF_MEMORY);
  766. return outAddr;
  767. }
  768. /******************************************************************************
  769. Nop
  770. ******************************************************************************/
  771. void __glsNop(void) {}
  772. /******************************************************************************
  773. Number
  774. ******************************************************************************/
  775. const GLubyte __glsBitReverse[256] = {
  776. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  777. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  778. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  779. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  780. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  781. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  782. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  783. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  784. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  785. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  786. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  787. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  788. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  789. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  790. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  791. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  792. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  793. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  794. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  795. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  796. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  797. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  798. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  799. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  800. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  801. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  802. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  803. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  804. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  805. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  806. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  807. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
  808. };
  809. const GLubyte __glsQuietNaN[8] = {
  810. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  811. };
  812. size_t __glsCeilBase2(size_t inVal) {
  813. const size_t outVal = 1 << __glsLogBase2(inVal);
  814. return outVal < inVal ? outVal << 1 : outVal;
  815. }
  816. size_t __glsLogBase2(size_t inVal) {
  817. size_t outLog = 0;
  818. while (inVal >>= 1) ++outLog;
  819. return outLog;
  820. }
  821. GLulong __glsPtrToULong(const GLvoid *inPtr) {
  822. #if __GLS_INT64
  823. return (GLulong)(SIZE_T)inPtr;
  824. #else /* !__GLS_INT64 */
  825. return glsULong(0, (GLuint)inPtr);
  826. #endif /* __GLS_INT64 */
  827. }
  828. GLlong __glsSizeToLong(size_t inSize) {
  829. #if __GLS_INT64
  830. return (GLlong)inSize;
  831. #else /* !__GLS_INT64 */
  832. if (sizeof(GLuint) >= sizeof(size_t)) {
  833. return glsLong(0, inSize);
  834. } else {
  835. return glsLong(inSize >> 32, inSize & 0xffffffff);
  836. }
  837. #endif /* __GLS_INT64 */
  838. }
  839. void __glsSwap2(GLvoid *inoutVec) {
  840. __GLS_SWAP_DECLARE;
  841. __GLS_SWAP2(inoutVec);
  842. }
  843. void __glsSwap2v(size_t inCount, GLvoid *inoutVec) {
  844. __GLS_SWAP_DECLARE;
  845. GLubyte *ptr = (GLubyte *)inoutVec;
  846. while (inCount--) {
  847. __GLS_SWAP2(ptr);
  848. ptr += 2;
  849. }
  850. }
  851. void __glsSwap4(GLvoid *inoutVec) {
  852. __GLS_SWAP_DECLARE;
  853. __GLS_SWAP4(inoutVec);
  854. }
  855. void __glsSwap4v(size_t inCount, GLvoid *inoutVec) {
  856. __GLS_SWAP_DECLARE;
  857. GLubyte *ptr = (GLubyte *)inoutVec;
  858. while (inCount--) {
  859. __GLS_SWAP4(ptr);
  860. ptr += 4;
  861. }
  862. }
  863. void __glsSwap8(GLvoid *inoutVec) {
  864. __GLS_SWAP_DECLARE;
  865. __GLS_SWAP8(inoutVec);
  866. }
  867. void __glsSwap8v(size_t inCount, GLvoid *inoutVec) {
  868. __GLS_SWAP_DECLARE;
  869. GLubyte *ptr = (GLubyte *)inoutVec;
  870. while (inCount-- > 0) {
  871. __GLS_SWAP8(ptr);
  872. ptr += 8;
  873. }
  874. }
  875. GLint __glsSwapi(GLint inVal) {
  876. __GLS_SWAP_DECLARE;
  877. __GLS_SWAP4(&inVal);
  878. return inVal;
  879. }
  880. GLshort __glsSwaps(GLshort inVal) {
  881. __GLS_SWAP_DECLARE;
  882. __GLS_SWAP2(&inVal);
  883. return inVal;
  884. }
  885. void __glsSwapv(GLenum inType, size_t inBytes, GLvoid *inoutVec) {
  886. switch (inType) {
  887. case GL_FLOAT:
  888. case GL_INT:
  889. case GL_UNSIGNED_INT:
  890. #if __GL_EXT_packed_pixels
  891. case GL_UNSIGNED_INT_8_8_8_8_EXT:
  892. case GL_UNSIGNED_INT_10_10_10_2_EXT:
  893. #endif /* __GL_EXT_packed_pixels */
  894. __glsSwap4v(inBytes / 4, (GLubyte *)inoutVec);
  895. break;
  896. case GL_SHORT:
  897. case GL_UNSIGNED_SHORT:
  898. #if __GL_EXT_packed_pixels
  899. case GL_UNSIGNED_SHORT_4_4_4_4_EXT:
  900. case GL_UNSIGNED_SHORT_5_5_5_1_EXT:
  901. #endif /* __GL_EXT_packed_pixels */
  902. __glsSwap2v(inBytes / 2, (GLubyte *)inoutVec);
  903. break;
  904. #if __GL_EXT_vertex_array
  905. case GL_DOUBLE_EXT:
  906. __glsSwap8v(inBytes / 8, (GLubyte *)inoutVec);
  907. break;
  908. #endif /* __GL_EXT_vertex_array */
  909. }
  910. }
  911. /******************************************************************************
  912. String
  913. ******************************************************************************/
  914. static GLboolean __glsString_addRoom(
  915. __GLSstring *inoutString, size_t inCount
  916. ) {
  917. const size_t newSize = __glsCeilBase2(
  918. (size_t)((ULONG_PTR)(inoutString->bufTail - inoutString->head) + 1 + inCount)
  919. );
  920. GLubyte *buf = __glsMalloc(newSize);
  921. if (!buf) return GL_FALSE;
  922. strcpy((char *)buf, (const char *)inoutString->head);
  923. inoutString->tail = buf + (inoutString->tail - inoutString->head);
  924. if (inoutString->head != inoutString->buf) free(inoutString->head);
  925. inoutString->head = buf;
  926. inoutString->bufTail = buf + newSize - 1;
  927. return GL_TRUE;
  928. }
  929. GLboolean __glsString_append(
  930. __GLSstring *inoutString, const GLubyte* inAppend
  931. ) {
  932. const size_t count = strlen((const char *)inAppend);
  933. if (
  934. inoutString->tail + count > inoutString->bufTail &&
  935. !__glsString_addRoom(inoutString, count)
  936. ) {
  937. return GL_FALSE;
  938. }
  939. strcpy((char *)inoutString->tail, (const char *)inAppend);
  940. inoutString->tail += count;
  941. return GL_TRUE;
  942. }
  943. GLboolean __glsString_appendChar(
  944. __GLSstring *inoutString, GLubyte inAppend
  945. ) {
  946. *inoutString->tail++ = inAppend;
  947. if (inoutString->tail > inoutString->bufTail) {
  948. *--inoutString->tail = 0;
  949. if (!__glsString_addRoom(inoutString, 1)) return GL_FALSE;
  950. *inoutString->tail++ = inAppend;
  951. }
  952. *inoutString->tail = 0;
  953. return GL_TRUE;
  954. }
  955. GLboolean __glsString_appendCounted(
  956. __GLSstring *inoutString, const GLubyte* inAppend, size_t inCount
  957. ) {
  958. if (
  959. inoutString->tail + inCount > inoutString->bufTail &&
  960. !__glsString_addRoom(inoutString, inCount)
  961. ) {
  962. return GL_FALSE;
  963. }
  964. strncpy((char *)inoutString->tail, (const char *)inAppend, inCount);
  965. inoutString->tail += inCount;
  966. *inoutString->tail = 0;
  967. return GL_TRUE;
  968. }
  969. GLboolean __glsString_appendInt(
  970. __GLSstring *inoutString, const GLubyte* inFormat, GLint inVal
  971. ) {
  972. __GLSstringBuf buf;
  973. size_t count;
  974. if (sprintf((char *)buf, (const char *)inFormat, inVal) < 0) {
  975. fprintf(stderr, "GLS fatal: sprintf failed\n");
  976. exit(EXIT_FAILURE);
  977. }
  978. count = strlen((const char *)buf);
  979. if (
  980. inoutString->tail + count > inoutString->bufTail &&
  981. !__glsString_addRoom(inoutString, count)
  982. ) {
  983. return GL_FALSE;
  984. }
  985. strcpy((char *)inoutString->tail, (const char *)buf);
  986. inoutString->tail += count;
  987. return GL_TRUE;
  988. }
  989. GLboolean __glsString_assign(
  990. __GLSstring *inoutString, const GLubyte *inAssign
  991. ) {
  992. inoutString->tail = inoutString->head;
  993. return __glsString_append(inoutString, inAssign);
  994. }
  995. GLboolean __glsString_assignCounted(
  996. __GLSstring *inoutString, const GLubyte *inAssign, size_t inCount
  997. ) {
  998. inoutString->tail = inoutString->head;
  999. return __glsString_appendCounted(inoutString, inAssign, inCount);
  1000. }
  1001. void __glsString_final(__GLSstring *inoutString) {
  1002. if (inoutString->head != inoutString->buf) {
  1003. free(inoutString->head);
  1004. inoutString->head = inoutString->buf;
  1005. }
  1006. }
  1007. void __glsString_init(__GLSstring *outString) {
  1008. outString->head = outString->tail = outString->buf;
  1009. outString->bufTail = outString->buf + __GLS_STRING_BUF_BYTES - 1;
  1010. outString->buf[0] = 0;
  1011. }
  1012. size_t __glsString_length(const __GLSstring *inString) {
  1013. return (size_t)((ULONG_PTR)(inString->tail - inString->head));
  1014. }
  1015. void __glsString_reset(__GLSstring *inoutString) {
  1016. __glsString_final(inoutString);
  1017. __glsString_init(inoutString);
  1018. }
  1019. const GLubyte* __glsUCS1String(const GLubyte *inUTF8String) {
  1020. GLuint b;
  1021. const GLubyte *cp = inUTF8String;
  1022. GLubyte *outVal, *p;
  1023. while (b = *cp++) if (b & 0x80) break;
  1024. if (!b) return inUTF8String;
  1025. p = outVal = (GLubyte *)__glsMalloc(strlen((const char *)inUTF8String));
  1026. if (!p) return GLS_NONE;
  1027. while (*inUTF8String) {
  1028. const GLint n = glsUTF8toUCS4(inUTF8String, &b);
  1029. if (n && b <= 0xff) {
  1030. inUTF8String += n;
  1031. *p++ = (GLubyte)b;
  1032. } else {
  1033. free(outVal);
  1034. if (!n) __GLS_RAISE_ERROR(GLS_INVALID_STRING);
  1035. return GLS_NONE;
  1036. }
  1037. }
  1038. *p = 0;
  1039. return outVal;
  1040. }
  1041. GLboolean __glsValidateString(const GLubyte *inString) {
  1042. if (!glsIsUTF8String(inString)) {
  1043. __GLS_RAISE_ERROR(GLS_INVALID_STRING);
  1044. return GL_FALSE;
  1045. }
  1046. return GL_TRUE;
  1047. }
  1048. /******************************************************************************
  1049. Vertex array
  1050. ******************************************************************************/
  1051. void __glsGetArrayState(__GLScontext *ctx, __GLSarrayState *arrayState)
  1052. {
  1053. __GLS_BEGIN_CAPTURE_EXEC(ctx, GLS_OP_glIsEnabled);
  1054. __GLS_BEGIN_CAPTURE_EXEC(ctx, GLS_OP_glGetIntegerv);
  1055. __GLS_BEGIN_CAPTURE_EXEC(ctx, GLS_OP_glGetPointerv);
  1056. arrayState->enabled = 0;
  1057. if (glIsEnabled(GL_VERTEX_ARRAY))
  1058. {
  1059. arrayState->enabled |= __GLS_VERTEX_ARRAY_ENABLE;
  1060. glGetIntegerv(GL_VERTEX_ARRAY_SIZE, &arrayState->vertex.size);
  1061. glGetIntegerv(GL_VERTEX_ARRAY_TYPE, &arrayState->vertex.type);
  1062. glGetIntegerv(GL_VERTEX_ARRAY_STRIDE, &arrayState->vertex.stride);
  1063. glGetPointerv(GL_VERTEX_ARRAY_POINTER,
  1064. (GLvoid **)&arrayState->vertex.data);
  1065. }
  1066. if (glIsEnabled(GL_NORMAL_ARRAY))
  1067. {
  1068. arrayState->enabled |= __GLS_NORMAL_ARRAY_ENABLE;
  1069. arrayState->normal.size = 3;
  1070. glGetIntegerv(GL_NORMAL_ARRAY_TYPE, &arrayState->normal.type);
  1071. glGetIntegerv(GL_NORMAL_ARRAY_STRIDE, &arrayState->normal.stride);
  1072. glGetPointerv(GL_NORMAL_ARRAY_POINTER,
  1073. (GLvoid **)&arrayState->normal.data);
  1074. }
  1075. if (glIsEnabled(GL_COLOR_ARRAY))
  1076. {
  1077. arrayState->enabled |= __GLS_COLOR_ARRAY_ENABLE;
  1078. glGetIntegerv(GL_COLOR_ARRAY_SIZE, &arrayState->color.size);
  1079. glGetIntegerv(GL_COLOR_ARRAY_TYPE, &arrayState->color.type);
  1080. glGetIntegerv(GL_COLOR_ARRAY_STRIDE, &arrayState->color.stride);
  1081. glGetPointerv(GL_COLOR_ARRAY_POINTER,
  1082. (GLvoid **)&arrayState->color.data);
  1083. }
  1084. if (glIsEnabled(GL_INDEX_ARRAY))
  1085. {
  1086. arrayState->enabled |= __GLS_INDEX_ARRAY_ENABLE;
  1087. arrayState->index.size = 1;
  1088. glGetIntegerv(GL_INDEX_ARRAY_TYPE, &arrayState->index.type);
  1089. glGetIntegerv(GL_INDEX_ARRAY_STRIDE, &arrayState->index.stride);
  1090. glGetPointerv(GL_INDEX_ARRAY_POINTER,
  1091. (GLvoid **)&arrayState->index.data);
  1092. }
  1093. if (glIsEnabled(GL_TEXTURE_COORD_ARRAY))
  1094. {
  1095. arrayState->enabled |= __GLS_TEXTURE_COORD_ARRAY_ENABLE;
  1096. glGetIntegerv(GL_TEXTURE_COORD_ARRAY_SIZE,
  1097. &arrayState->textureCoord.size);
  1098. glGetIntegerv(GL_TEXTURE_COORD_ARRAY_TYPE,
  1099. &arrayState->textureCoord.type);
  1100. glGetIntegerv(GL_TEXTURE_COORD_ARRAY_STRIDE,
  1101. &arrayState->textureCoord.stride);
  1102. glGetPointerv(GL_TEXTURE_COORD_ARRAY_POINTER,
  1103. (GLvoid **)&arrayState->textureCoord.data);
  1104. }
  1105. if (glIsEnabled(GL_EDGE_FLAG_ARRAY))
  1106. {
  1107. arrayState->enabled |= __GLS_EDGE_FLAG_ARRAY_ENABLE;
  1108. arrayState->edgeFlag.size = 1;
  1109. arrayState->edgeFlag.type = __GLS_BOOLEAN;
  1110. glGetIntegerv(GL_EDGE_FLAG_ARRAY_STRIDE, &arrayState->edgeFlag.stride);
  1111. glGetPointerv(GL_EDGE_FLAG_ARRAY_POINTER,
  1112. (GLvoid **)&arrayState->edgeFlag.data);
  1113. }
  1114. __GLS_END_CAPTURE_EXEC(ctx, GLS_OP_glIsEnabled);
  1115. __GLS_END_CAPTURE_EXEC(ctx, GLS_OP_glGetIntegerv);
  1116. __GLS_END_CAPTURE_EXEC(ctx, GLS_OP_glGetPointerv);
  1117. }
  1118. GLint __glsArrayDataSize(GLsizei count, __GLSarrayState *arrayState)
  1119. {
  1120. GLint size;
  1121. GLint i;
  1122. __GLSsingleArrayState *array;
  1123. GLuint arrayBit;
  1124. /* Start with space for the data size, count and enables */
  1125. size = 12;
  1126. /* Every array stores its size and type fields even
  1127. for the cases where size and type are fixed. This
  1128. allows one piece of code to handle any array */
  1129. array = &arrayState->vertex;
  1130. arrayBit = __GLS_VERTEX_ARRAY_ENABLE;
  1131. for (i = 0; i < __GLS_ARRAY_COUNT; i++)
  1132. {
  1133. if (arrayState->enabled & arrayBit)
  1134. {
  1135. size += 8 + __GLS_ARRAY_SIZE(count, array->size, array->type);
  1136. }
  1137. array++;
  1138. arrayBit <<= 1;
  1139. }
  1140. return size;
  1141. }
  1142. typedef struct _DeHashEntry
  1143. {
  1144. GLuint original;
  1145. struct _DeHashEntry *next;
  1146. } DeHashEntry;
  1147. typedef GLubyte DeHashIndex;
  1148. #define DE_HASH_SIZE 256
  1149. #define DE_HASH(ui) ((DeHashIndex)(ui))
  1150. GLint __glsDrawElementsDataSize(GLsizei count, GLenum type,
  1151. const GLvoid *indices,
  1152. __GLSarrayState *arrayState,
  1153. __GLSdrawElementsState *deState)
  1154. {
  1155. GLubyte *allData;
  1156. DeHashEntry *hashTable[DE_HASH_SIZE];
  1157. GLint vtxCount;
  1158. GLint i;
  1159. GLubyte *idxData;
  1160. GLuint idx;
  1161. DeHashEntry *hashEntries;
  1162. GLuint *outIndices;
  1163. DeHashEntry *ent;
  1164. DeHashIndex hash;
  1165. // Determine the set of unique vertex indices by hashing
  1166. // all the input indices and checking for duplicates
  1167. // There can't be more unique indices than input indices
  1168. // so count is an upper bound for our allocations
  1169. allData = __glsMalloc(count * (sizeof(GLuint)+sizeof(DeHashEntry)));
  1170. if (allData == NULL)
  1171. {
  1172. return -1;
  1173. }
  1174. deState->freePtr = allData;
  1175. outIndices = (GLuint *)allData;
  1176. deState->indices = outIndices;
  1177. hashEntries = (DeHashEntry *)(outIndices+count);
  1178. memset(hashTable, 0, DE_HASH_SIZE*sizeof(DeHashEntry *));
  1179. vtxCount = 0;
  1180. idxData = (GLubyte *)indices;
  1181. for (i = 0; i < count; i++)
  1182. {
  1183. // Get incoming index
  1184. switch(type)
  1185. {
  1186. case GL_UNSIGNED_BYTE:
  1187. idx = *idxData++;
  1188. break;
  1189. case GL_UNSIGNED_SHORT:
  1190. idx = *(GLushort *)idxData;
  1191. idxData += sizeof(GLushort);
  1192. break;
  1193. case GL_UNSIGNED_INT:
  1194. idx = *(GLuint *)idxData;
  1195. idxData += sizeof(GLuint);
  1196. break;
  1197. }
  1198. // Look for a matching index in the hash table
  1199. hash = DE_HASH(idx);
  1200. ent = hashTable[hash];
  1201. while (ent != NULL && ent->original != idx)
  1202. {
  1203. ent = ent->next;
  1204. }
  1205. // If we didn't find a match, add a new vertex
  1206. // reference
  1207. if (ent == NULL)
  1208. {
  1209. ent = &hashEntries[vtxCount++];
  1210. ent->original = idx;
  1211. ent->next = hashTable[hash];
  1212. hashTable[hash] = ent;
  1213. }
  1214. // Create index into unique vertex set
  1215. *outIndices++ = (GLuint)((ULONG_PTR)(ent-hashEntries));
  1216. }
  1217. // Overwrite hash entries with just the vertex mappings for return
  1218. outIndices = (GLuint *)hashEntries;
  1219. deState->vertices = outIndices;
  1220. deState->vtxCount = vtxCount;
  1221. for (i = 0; i < vtxCount; i++)
  1222. {
  1223. *outIndices++ = hashEntries->original;
  1224. hashEntries++;
  1225. }
  1226. // Return the combined size of the unique vertex data and
  1227. // the new element indices
  1228. return __glsArrayDataSize(vtxCount, arrayState) + count*sizeof(GLuint);
  1229. }
  1230. void __glsWriteArrayValues(__GLSwriter *writer, GLint first,
  1231. GLsizei count, __GLSsingleArrayState *array)
  1232. {
  1233. GLint elStep;
  1234. const GLvoid *data;
  1235. if (array->stride > 0)
  1236. {
  1237. elStep = array->stride;
  1238. }
  1239. else
  1240. {
  1241. elStep = array->size * __glsTypeSize(array->type);
  1242. }
  1243. data = (const GLvoid *)((GLubyte *)array->data + first*elStep);
  1244. __glsWriter_putVertexv(writer, array->size, array->type, array->stride,
  1245. count, data);
  1246. }
  1247. // This routine must be called with an odd writer alignment
  1248. void __glsWriteArrayData(__GLSwriter *writer, GLint size,
  1249. GLint first, GLsizei count,
  1250. GLenum type, const GLvoid *indices,
  1251. __GLSarrayState *arrayState)
  1252. {
  1253. GLint i, el;
  1254. __GLSsingleArrayState *array;
  1255. GLuint arrayBit;
  1256. GLubyte *ubIndices;
  1257. GLushort *usIndices;
  1258. GLuint *uiIndices;
  1259. writer->putGLint(writer, size);
  1260. writer->putGLint(writer, count);
  1261. writer->putGLuint(writer, arrayState->enabled);
  1262. array = &arrayState->vertex;
  1263. arrayBit = __GLS_VERTEX_ARRAY_ENABLE;
  1264. for (i = 0; i < __GLS_ARRAY_COUNT; i++)
  1265. {
  1266. if (arrayState->enabled & arrayBit)
  1267. {
  1268. writer->putGLint(writer, array->size);
  1269. writer->putGLint(writer, array->type);
  1270. if (indices != NULL)
  1271. {
  1272. switch(type)
  1273. {
  1274. case GL_UNSIGNED_BYTE:
  1275. ubIndices = (GLubyte *)indices;
  1276. for (el = 0; el < count; el++)
  1277. {
  1278. __glsWriteArrayValues(writer, ubIndices[el], 1, array);
  1279. }
  1280. break;
  1281. case GL_UNSIGNED_SHORT:
  1282. usIndices = (GLshort *)indices;
  1283. for (el = 0; el < count; el++)
  1284. {
  1285. __glsWriteArrayValues(writer, usIndices[el], 1, array);
  1286. }
  1287. break;
  1288. case GL_UNSIGNED_INT:
  1289. uiIndices = (GLuint *)indices;
  1290. for (el = 0; el < count; el++)
  1291. {
  1292. __glsWriteArrayValues(writer, uiIndices[el], 1, array);
  1293. }
  1294. break;
  1295. }
  1296. }
  1297. else
  1298. {
  1299. __glsWriteArrayValues(writer, first, count, array);
  1300. }
  1301. // Pad data out to an eight-byte boundary
  1302. if (writer->type != GLS_TEXT)
  1303. {
  1304. GLint pad;
  1305. double padValue = 0.0;
  1306. pad = 8 - (__GLS_EXACT_ARRAY_SIZE(count, array->size,
  1307. array->type) & 7);
  1308. if (pad < 8)
  1309. {
  1310. writer->putGLubytev(writer, pad,
  1311. (const GLubyte *)&padValue);
  1312. }
  1313. }
  1314. }
  1315. array++;
  1316. arrayBit <<= 1;
  1317. }
  1318. }
  1319. // This routine must be called with an odd writer alignment
  1320. void __glsWriteDrawElementsData(__GLSwriter *writer, GLint size,
  1321. GLsizei count, __GLSarrayState *arrayState,
  1322. __GLSdrawElementsState *deState)
  1323. {
  1324. __glsWriteArrayData(writer, size, 0, deState->vtxCount, GL_UNSIGNED_INT,
  1325. deState->vertices, arrayState);
  1326. writer->putGLuintv(writer, count, deState->indices);
  1327. free(deState->freePtr);
  1328. }
  1329. typedef void (*__GLSdispatchVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
  1330. typedef void (*__GLSdispatchColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
  1331. typedef void (*__GLSdispatchEdgeFlagPointer)(GLsizei stride, const GLvoid *pointer);
  1332. typedef void (*__GLSdispatchIndexPointer)(GLenum type, GLsizei stride, const GLvoid *pointer);
  1333. typedef void (*__GLSdispatchNormalPointer)(GLenum type, GLsizei stride, const GLvoid *pointer);
  1334. typedef void (*__GLSdispatchTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
  1335. void __glsSetArrayState(__GLScontext *ctx, GLubyte *data)
  1336. {
  1337. GLsizei count;
  1338. GLuint enabled;
  1339. count = *(GLsizei *)(data+4);
  1340. enabled = *(GLuint *)(data+8);
  1341. data += 12;
  1342. // Enable/DisableClientState produce their own records so
  1343. // the current enable state should be correct
  1344. if (enabled & __GLS_VERTEX_ARRAY_ENABLE)
  1345. {
  1346. ((__GLSdispatchVertexPointer)ctx->dispatchCall[385])
  1347. (*(GLint *)(data+0),
  1348. *(GLenum *)(data+4),
  1349. 0,
  1350. data+8);
  1351. data += 8 + __GLS_ARRAY_SIZE(count, *(GLint *)(data+0),
  1352. *(GLenum *)(data+4));
  1353. }
  1354. if (enabled & __GLS_NORMAL_ARRAY_ENABLE)
  1355. {
  1356. ((__GLSdispatchNormalPointer)ctx->dispatchCall[382])
  1357. (*(GLenum *)(data+4),
  1358. 0,
  1359. data+8);
  1360. data += 8 + __GLS_ARRAY_SIZE(count, 3, *(GLenum *)(data+4));
  1361. }
  1362. if (enabled & __GLS_COLOR_ARRAY_ENABLE)
  1363. {
  1364. ((__GLSdispatchColorPointer)ctx->dispatchCall[372])
  1365. (*(GLint *)(data+0),
  1366. *(GLenum *)(data+4),
  1367. 0,
  1368. data+8);
  1369. data += 8 + __GLS_ARRAY_SIZE(count, *(GLint *)(data+0),
  1370. *(GLenum *)(data+4));
  1371. }
  1372. if (enabled & __GLS_INDEX_ARRAY_ENABLE)
  1373. {
  1374. ((__GLSdispatchIndexPointer)ctx->dispatchCall[378])
  1375. (*(GLenum *)(data+4),
  1376. 0,
  1377. data+8);
  1378. data += 8 + __GLS_ARRAY_SIZE(count, 1, *(GLenum *)(data+4));
  1379. }
  1380. if (enabled & __GLS_TEXTURE_COORD_ARRAY_ENABLE)
  1381. {
  1382. ((__GLSdispatchTexCoordPointer)ctx->dispatchCall[384])
  1383. (*(GLint *)(data+0),
  1384. *(GLenum *)(data+4),
  1385. 0,
  1386. data+8);
  1387. data += 8 + __GLS_ARRAY_SIZE(count, *(GLint *)(data+0),
  1388. *(GLenum *)(data+4));
  1389. }
  1390. if (enabled & __GLS_EDGE_FLAG_ARRAY_ENABLE)
  1391. {
  1392. ((__GLSdispatchEdgeFlagPointer)ctx->dispatchCall[376])
  1393. (0,
  1394. data+8);
  1395. data += 8 + __GLS_ARRAY_SIZE(count, 1, __GLS_BOOLEAN);
  1396. }
  1397. }
  1398. GLvoid *__glsSetArrayStateText(__GLScontext *ctx, __GLSreader *reader,
  1399. GLuint *enabled, GLsizei *count)
  1400. {
  1401. GLint size;
  1402. GLubyte *ptr;
  1403. int i;
  1404. GLuint arrayBit;
  1405. GLvoid *data = GLS_NONE;
  1406. __glsReader_getGLint_text(reader, &size);
  1407. __glsReader_getGLint_text(reader, count);
  1408. __glsReader_getGLuint_text(reader, enabled);
  1409. data = __glsMalloc(size);
  1410. if (!data) goto end;
  1411. ptr = (GLubyte *)data;
  1412. *(GLint *)(ptr+0) = size;
  1413. *(GLsizei *)(ptr+4) = *count;
  1414. *(GLuint *)(ptr+8) = *enabled;
  1415. ptr += 12;
  1416. arrayBit = __GLS_VERTEX_ARRAY_ENABLE;
  1417. for (i = 0; i < __GLS_ARRAY_COUNT; i++)
  1418. {
  1419. if (*enabled & arrayBit)
  1420. {
  1421. __glsReader_getGLint_text(reader, (GLint *)(ptr+0));
  1422. __glsReader_getGLenum_text(reader, (GLenum *)(ptr+4));
  1423. size = __GLS_EXACT_ARRAY_SIZE(*count, *(GLint *)(ptr+0),
  1424. *(GLenum *)(ptr+4));
  1425. __glsReader_getGLcompv_text(reader, *(GLenum *)(ptr+4),
  1426. size, ptr+8);
  1427. ptr += 8 + __GLS_PAD_EIGHT(size);
  1428. }
  1429. arrayBit <<= 1;
  1430. }
  1431. if (reader->error)
  1432. {
  1433. free(data);
  1434. data = NULL;
  1435. goto end;
  1436. }
  1437. __glsSetArrayState(ctx, data);
  1438. end:
  1439. return data;
  1440. }
  1441. void __glsDisableArrayState(__GLScontext *ctx, GLuint enabled)
  1442. {
  1443. // Doesn't currently need to do anything because
  1444. // enable/disable are handled by their own records
  1445. }
  1446. void __glsSwapArrayData(GLubyte *data)
  1447. {
  1448. int i;
  1449. GLuint arrayBit;
  1450. GLsizei count;
  1451. GLuint enabled;
  1452. GLint size;
  1453. __glsSwap4(data+0);
  1454. __glsSwap4(data+4);
  1455. count = *(GLsizei *)(data+4);
  1456. __glsSwap4(data+8);
  1457. enabled = *(GLuint *)(data+8);
  1458. data += 12;
  1459. arrayBit = __GLS_VERTEX_ARRAY_ENABLE;
  1460. for (i = 0; i < __GLS_ARRAY_COUNT; i++)
  1461. {
  1462. if (enabled & arrayBit)
  1463. {
  1464. __glsSwap4(data+0);
  1465. __glsSwap4(data+4);
  1466. size = __GLS_EXACT_ARRAY_SIZE(count, *(GLint *)(data+0),
  1467. *(GLenum *)(data+4));
  1468. __glsSwapv(*(GLenum *)(data+4), size, data+8);
  1469. data += 8 + __GLS_PAD_EIGHT(size);
  1470. }
  1471. arrayBit <<= 1;
  1472. }
  1473. }