Debian Codegraph @

terps/glulxe/glkop.c

raw symbols SCIP
1 /* glkop.c: Glulxe code for Glk API dispatching.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 /* This code is actually very general; it could work for almost any
7 32-bit VM which remotely resembles Glulxe or the Z-machine in design.
8
9 To be precise, we make the following assumptions:
10
11 - An argument list is an array of 32-bit values, which can represent
12 either integers or addresses.
13 - We can read or write to a 32-bit integer in VM memory using the macros
14 ReadMemory(addr) and WriteMemory(addr), where addr is an address
15 taken from the argument list.
16 - A character array is a sequence of bytes somewhere in VM memory.
17 The array can be turned into a C char array by the macro
18 CaptureCArray(addr, len), and released by ReleaseCArray().
19 The passin, passout hints may be used to avoid unnecessary copying.
20 - An integer array is a sequence of integers somewhere in VM memory.
21 The array can be turned into a C integer array by the macro
22 CaptureIArray(addr, len), and released by ReleaseIArray().
23 These macros are responsible for fixing byte-order and alignment
24 (if the C ABI does not match the VM's). The passin, passout hints
25 may be used to avoid unnecessary copying.
26 - A Glk object array is a sequence of integers in VM memory. It is
27 turned into a C pointer array (remember that C pointers may be more
28 than 4 bytes!) The pointer array is allocated by
29 CapturePtrArray(addr, len, objclass) and released by ReleasePtrArray().
30 Again, the macros handle the conversion.
31 - A Glk structure (such as event_t) is a set of integers somewhere
32 in VM memory, which can be read and written with the macros
33 ReadStructField(addr, fieldnum) and WriteStructField(addr, fieldnum).
34 The fieldnum is an integer (from 0 to 3, for event_t.)
35 - A VM string can be turned into a C-style string with the macro
36 ptr = DecodeVMString(addr). After the string is used, this code
37 calls ReleaseVMString(ptr), which should free any memory that
38 DecodeVMString allocates.
39 - A VM Unicode string can be turned into a zero-terminated array
40 of 32-bit integers, in the same way, with DecodeVMUstring
41 and ReleaseVMUstring.
42
43 To work this code over for a new VM, just diddle the macros.
44 */
45
46 #define ReadMemory(addr) \
47 (((addr) == 0xffffffff) \
48 ? (stackptr -= 4, Stk4(stackptr)) \
49 : (Mem4(addr)))
50 #define WriteMemory(addr, val) \
51 (((addr) == 0xffffffff) \
52 ? (StkW4(stackptr, (val)), stackptr += 4) \
53 : (MemW4((addr), (val))))
54 #define CaptureCArray(addr, len, passin) \
55 (grab_temp_c_array(addr, len, passin))
56 #define ReleaseCArray(ptr, addr, len, passout) \
57 (release_temp_c_array(ptr, addr, len, passout))
58 #define CaptureIArray(addr, len, passin) \
59 (grab_temp_i_array(addr, len, passin))
60 #define ReleaseIArray(ptr, addr, len, passout) \
61 (release_temp_i_array(ptr, addr, len, passout))
62 #define CapturePtrArray(addr, len, objclass, passin) \
63 (grab_temp_ptr_array(addr, len, objclass, passin))
64 #define ReleasePtrArray(ptr, addr, len, objclass, passout) \
65 (release_temp_ptr_array(ptr, addr, len, objclass, passout))
66 #define ReadStructField(addr, fieldnum) \
67 (((addr) == 0xffffffff) \
68 ? (stackptr -= 4, Stk4(stackptr)) \
69 : (Mem4((addr)+(fieldnum)*4)))
70 #define WriteStructField(addr, fieldnum, val) \
71 (((addr) == 0xffffffff) \
72 ? (StkW4(stackptr, (val)), stackptr += 4) \
73 : (MemW4((addr)+(fieldnum)*4, (val))))
74 #define DecodeVMString(addr) \
75 (make_temp_string(addr))
76 #define ReleaseVMString(ptr) \
77 (free_temp_string(ptr))
78 #define DecodeVMUstring(addr) \
79 (make_temp_ustring(addr))
80 #define ReleaseVMUstring(ptr) \
81 (free_temp_ustring(ptr))
82
83 #include <time.h>
84 #include "glk.h"
85 #include "glulxe.h"
86 #include "gi_dispa.h"
87
88 typedef struct dispatch_splot_struct {
89 int numwanted;
90 int maxargs;
91 gluniversal_t *garglist;
92 glui32 *varglist;
93 int numvargs;
94 glui32 *retval;
95 } dispatch_splot_t;
96
97 /* We maintain a linked list of arrays being used for Glk calls. It is
98 only used for integer (glui32) arrays -- char arrays are handled in
99 place. It's not worth bothering with a hash table, since most
100 arrays appear here only momentarily. */
101
102 typedef struct arrayref_struct arrayref_t;
103 struct arrayref_struct {
104 void *array;
105 glui32 addr;
106 glui32 elemsize;
107 glui32 len; /* elements */
108 int retained;
109 arrayref_t *next;
110 };
111
112 static arrayref_t *arrays = NULL;
113
114 /* We maintain a hash table for each opaque Glk class. classref_t are the
115 nodes of the table, and classtable_t are the tables themselves. */
116
117 typedef struct classref_struct classref_t;
118 struct classref_struct {
119 void *obj;
120 glui32 id;
121 int bucknum;
122 classref_t *next;
123 };
124
125 #define CLASSHASH_SIZE (31)
126 typedef struct classtable_struct {
127 glui32 lastid;
128 classref_t *bucket[CLASSHASH_SIZE];
129 } classtable_t;
130
131 /* The list of hash tables, for the classes. */
132 static int num_classes = 0;
133 classtable_t **classes = NULL;
134
135 static classtable_t *new_classtable(glui32 firstid);
136 static void *classes_get(int classid, glui32 objid);
137 static classref_t *classes_put(int classid, void *obj, glui32 origid);
138 static void classes_remove(int classid, void *obj);
139
140 static gidispatch_rock_t glulxe_classtable_register(void *obj,
141 glui32 objclass);
142 static void glulxe_classtable_unregister(void *obj, glui32 objclass,
143 gidispatch_rock_t objrock);
144 static gidispatch_rock_t glulxe_retained_register(void *array,
145 glui32 len, char *typecode);
146 static void glulxe_retained_unregister(void *array, glui32 len,
147 char *typecode, gidispatch_rock_t objrock);
148 static long glulxe_array_locate(void *array, glui32 len,
149 char *typecode, gidispatch_rock_t objrock, int *elemsizeref);
150 static gidispatch_rock_t glulxe_array_restore(long bufkey,
151 glui32 len, char *typecode, void **arrayref);
152
153 /* This is only needed for autorestore. */
154 extern gidispatch_rock_t glulxe_classtable_register_existing(void *obj,
155 glui32 objclass, glui32 dispid);
156
157 /* The library_select_hook is called every time the VM blocks for input.
158 The app might take this opportunity to autosave, for example. */
159 static void (*library_select_hook)(glui32, glui32, glui32, glui32) = NULL;
160
161 static char *grab_temp_c_array(glui32 addr, glui32 len, int passin);
162 static void release_temp_c_array(char *arr, glui32 addr, glui32 len, int passout);
163 static glui32 *grab_temp_i_array(glui32 addr, glui32 len, int passin);
164 static void release_temp_i_array(glui32 *arr, glui32 addr, glui32 len, int passout);
165 static void **grab_temp_ptr_array(glui32 addr, glui32 len, int objclass, int passin);
166 static void release_temp_ptr_array(void **arr, glui32 addr, glui32 len, int objclass, int passout);
167
168 static void prepare_glk_args(char *proto, dispatch_splot_t *splot);
169 static void parse_glk_args(dispatch_splot_t *splot, char **proto, int depth,
170 int *argnumptr, glui32 subaddress, int subpassin);
171 static void unparse_glk_args(dispatch_splot_t *splot, char **proto, int depth,
172 int *argnumptr, glui32 subaddress, int subpassout);
173
174 static char *get_game_id(void);
175
176 /* init_dispatch():
177 Set up the class hash tables and other startup-time stuff.
178 */
179 int init_dispatch()
180 {
181 int ix;
182 int randish;
183
184 /* What with one thing and another, this *could* be called more than
185 once. We only need to allocate the tables once. */
186 if (classes)
187 return TRUE;
188
189 /* Set up the game-ID hook. (This is ifdeffed because not all Glk
190 libraries have this call.) */
191 #ifdef GI_DISPA_GAME_ID_AVAILABLE
192 gidispatch_set_game_id_hook(&get_game_id);
193 #endif /* GI_DISPA_GAME_ID_AVAILABLE */
194
195 /* Allocate the class hash tables. */
196 num_classes = gidispatch_count_classes();
197 classes = (classtable_t **)glulx_malloc(num_classes
198 * sizeof(classtable_t *));
199 if (!classes)
200 return FALSE;
201
202 randish = time(NULL) % 101;
203 for (ix=0; ix<num_classes; ix++) {
204 classes[ix] = new_classtable(1+120*ix+randish);
205 if (!classes[ix])
206 return FALSE;
207 }
208
209 /* Set up the two callbacks. */
210 gidispatch_set_object_registry(&glulxe_classtable_register,
211 &glulxe_classtable_unregister);
212 gidispatch_set_retained_registry(&glulxe_retained_register,
213 &glulxe_retained_unregister);
214
215 /* If the library supports autorestore callbacks, set those up too.
216 (These are only used in iosglk and remglk, currently.) */
217 #ifdef GIDISPATCH_AUTORESTORE_REGISTRY
218 gidispatch_set_autorestore_registry(&glulxe_array_locate,
219 &glulxe_array_restore);
220 #endif /* GIDISPATCH_AUTORESTORE_REGISTRY */
221
222 return TRUE;
223 }
224
225 /* perform_glk():
226 Turn a list of Glulx arguments into a list of Glk arguments,
227 dispatch the function call, and return the result.
228 */
229 glui32 perform_glk(glui32 funcnum, glui32 numargs, glui32 *arglist)
230 {
231 glui32 retval = 0;
232
233 switch (funcnum) {
234 /* To speed life up, we implement commonly-used Glk functions
235 directly -- instead of bothering with the whole prototype
236 mess. */
237
238 case 0x0047: /* stream_set_current */
239 if (numargs != 1)
240 goto WrongArgNum;
241 glk_stream_set_current(find_stream_by_id(arglist[0]));
242 break;
243 case 0x0048: /* stream_get_current */
244 if (numargs != 0)
245 goto WrongArgNum;
246 retval = find_id_for_stream(glk_stream_get_current());
247 break;
248 case 0x0062: /* fileref_create_by_prompt */
249 /* call a library hook on every glk_fileref_create_by_prompt(),
250 because it blocks and waits like glk_select() */
251 if (library_select_hook)
252 library_select_hook(0x0062, arglist[0], arglist[1], arglist[2]);
253 /* but then fall through to full dispatcher, because there's no real
254 need for speed here */
255 goto FullDispatcher;
256 case 0x0080: /* put_char */
257 if (numargs != 1)
258 goto WrongArgNum;
259 glk_put_char(arglist[0] & 0xFF);
260 break;
261 case 0x0081: /* put_char_stream */
262 if (numargs != 2)
263 goto WrongArgNum;
264 glk_put_char_stream(find_stream_by_id(arglist[0]), arglist[1] & 0xFF);
265 break;
266 case 0x00C0: /* select */
267 /* call a library hook on every glk_select() */
268 if (library_select_hook)
269 library_select_hook(0x00C0, arglist[0], 0, 0);
270 /* but then fall through to full dispatcher, because there's no real
271 need for speed here */
272 goto FullDispatcher;
273 case 0x00A0: /* char_to_lower */
274 if (numargs != 1)
275 goto WrongArgNum;
276 retval = glk_char_to_lower(arglist[0] & 0xFF);
277 break;
278 case 0x00A1: /* char_to_upper */
279 if (numargs != 1)
280 goto WrongArgNum;
281 retval = glk_char_to_upper(arglist[0] & 0xFF);
282 break;
283 case 0x0128: /* put_char_uni */
284 if (numargs != 1)
285 goto WrongArgNum;
286 glk_put_char_uni(arglist[0]);
287 break;
288 case 0x012B: /* put_char_stream_uni */
289 if (numargs != 2)
290 goto WrongArgNum;
291 glk_put_char_stream_uni(find_stream_by_id(arglist[0]), arglist[1]);
292 break;
293
294 WrongArgNum:
295 fatal_error("Wrong number of arguments to Glk function.");
296 break;
297
298 FullDispatcher:
299 default: {
300 /* Go through the full dispatcher prototype foo. */
301 char *proto, *cx;
302 dispatch_splot_t splot;
303 int argnum, argnum2;
304
305 /* Grab the string. */
306 proto = gidispatch_prototype(funcnum);
307 if (!proto)
308 fatal_error("Unknown Glk function.");
309
310 splot.varglist = arglist;
311 splot.numvargs = numargs;
312 splot.retval = &retval;
313
314 /* The work goes in four phases. First, we figure out how many
315 arguments we want, and allocate space for the Glk argument
316 list. Then we go through the Glulxe arguments and load them
317 into the Glk list. Then we call. Then we go through the
318 arguments again, unloading the data back into Glulx memory. */
319
320 /* Phase 0. */
321 prepare_glk_args(proto, &splot);
322
323 /* Phase 1. */
324 argnum = 0;
325 cx = proto;
326 parse_glk_args(&splot, &cx, 0, &argnum, 0, 0);
327
328 /* Phase 2. */
329 gidispatch_call(funcnum, argnum, splot.garglist);
330
331 /* Phase 3. */
332 argnum2 = 0;
333 cx = proto;
334 unparse_glk_args(&splot, &cx, 0, &argnum2, 0, 0);
335 if (argnum != argnum2)
336 fatal_error("Argument counts did not match.");
337
338 break;
339 }
340 }
341
342 return retval;
343 }
344
345 /* read_prefix():
346 Read the prefixes of an argument string -- the "<>&+:#!" chars.
347 */
348 static char *read_prefix(char *cx, int *isref, int *isarray,
349 int *passin, int *passout, int *nullok, int *isretained,
350 int *isreturn)
351 {
352 *isref = FALSE;
353 *passin = FALSE;
354 *passout = FALSE;
355 *nullok = TRUE;
356 *isarray = FALSE;
357 *isretained = FALSE;
358 *isreturn = FALSE;
359 while (1) {
360 if (*cx == '<') {
361 *isref = TRUE;
362 *passout = TRUE;
363 }
364 else if (*cx == '>') {
365 *isref = TRUE;
366 *passin = TRUE;
367 }
368 else if (*cx == '&') {
369 *isref = TRUE;
370 *passout = TRUE;
371 *passin = TRUE;
372 }
373 else if (*cx == '+') {
374 *nullok = FALSE;
375 }
376 else if (*cx == ':') {
377 *isref = TRUE;
378 *passout = TRUE;
379 *nullok = FALSE;
380 *isreturn = TRUE;
381 }
382 else if (*cx == '#') {
383 *isarray = TRUE;
384 }
385 else if (*cx == '!') {
386 *isretained = TRUE;
387 }
388 else {
389 break;
390 }
391 cx++;
392 }
393 return cx;
394 }
395
396 /* prepare_glk_args():
397 This reads through the prototype string, and pulls Floo objects off the
398 stack. It also works out the maximal number of gluniversal_t objects
399 which could be used by the Glk call in question. It then allocates
400 space for them.
401 */
402 static void prepare_glk_args(char *proto, dispatch_splot_t *splot)
403 {
404 static gluniversal_t *garglist = NULL;
405 static int garglist_size = 0;
406
407 int ix;
408 int numwanted, numvargswanted, maxargs;
409 char *cx;
410
411 cx = proto;
412 numwanted = 0;
413 while (*cx >= '0' && *cx <= '9') {
414 numwanted = 10 * numwanted + (*cx - '0');
415 cx++;
416 }
417 splot->numwanted = numwanted;
418
419 maxargs = 0;
420 numvargswanted = 0;
421 for (ix = 0; ix < numwanted; ix++) {
422 int isref, passin, passout, nullok, isarray, isretained, isreturn;
423 cx = read_prefix(cx, &isref, &isarray, &passin, &passout, &nullok,
424 &isretained, &isreturn);
425 if (isref) {
426 maxargs += 2;
427 }
428 else {
429 maxargs += 1;
430 }
431 if (!isreturn) {
432 if (isarray) {
433 numvargswanted += 2;
434 }
435 else {
436 numvargswanted += 1;
437 }
438 }
439
440 if (*cx == 'I' || *cx == 'C') {
441 cx += 2;
442 }
443 else if (*cx == 'Q') {
444 cx += 2;
445 }
446 else if (*cx == 'S' || *cx == 'U') {
447 cx += 1;
448 }
449 else if (*cx == '[') {
450 int refdepth, nwx;
451 cx++;
452 nwx = 0;
453 while (*cx >= '0' && *cx <= '9') {
454 nwx = 10 * nwx + (*cx - '0');
455 cx++;
456 }
457 maxargs += nwx; /* This is *only* correct because all structs contain
458 plain values. */
459 refdepth = 1;
460 while (refdepth > 0) {
461 if (*cx == '[')
462 refdepth++;
463 else if (*cx == ']')
464 refdepth--;
465 cx++;
466 }
467 }
468 else {
469 fatal_error("Illegal format string.");
470 }
471 }
472
473 if (*cx != ':' && *cx != '\0')
474 fatal_error("Illegal format string.");
475
476 splot->maxargs = maxargs;
477
478 if (splot->numvargs != numvargswanted)
479 fatal_error("Wrong number of arguments to Glk function.");
480
481 if (garglist && garglist_size < maxargs) {
482 glulx_free(garglist);
483 garglist = NULL;
484 garglist_size = 0;
485 }
486 if (!garglist) {
487 garglist_size = maxargs + 16;
488 garglist = (gluniversal_t *)glulx_malloc(garglist_size
489 * sizeof(gluniversal_t));
490 }
491 if (!garglist)
492 fatal_error("Unable to allocate storage for Glk arguments.");
493
494 splot->garglist = garglist;
495 }
496
497 /* parse_glk_args():
498 This long and unpleasant function translates a set of Floo objects into
499 a gluniversal_t array. It's recursive, too, to deal with structures.
500 */
501 static void parse_glk_args(dispatch_splot_t *splot, char **proto, int depth,
502 int *argnumptr, glui32 subaddress, int subpassin)
503 {
504 char *cx;
505 int ix, argx;
506 int gargnum, numwanted;
507 void *opref;
508 gluniversal_t *garglist;
509 glui32 *varglist;
510
511 garglist = splot->garglist;
512 varglist = splot->varglist;
513 gargnum = *argnumptr;
514 cx = *proto;
515
516 numwanted = 0;
517 while (*cx >= '0' && *cx <= '9') {
518 numwanted = 10 * numwanted + (*cx - '0');
519 cx++;
520 }
521
522 for (argx = 0, ix = 0; argx < numwanted; argx++, ix++) {
523 char typeclass;
524 int skipval;
525 int isref, passin, passout, nullok, isarray, isretained, isreturn;
526 cx = read_prefix(cx, &isref, &isarray, &passin, &passout, &nullok,
527 &isretained, &isreturn);
528
529 typeclass = *cx;
530 cx++;
531
532 skipval = FALSE;
533 if (isref) {
534 if (!isreturn && varglist[ix] == 0) {
535 if (!nullok)
536 fatal_error("Zero passed invalidly to Glk function.");
537 garglist[gargnum].ptrflag = FALSE;
538 gargnum++;
539 skipval = TRUE;
540 }
541 else {
542 garglist[gargnum].ptrflag = TRUE;
543 gargnum++;
544 }
545 }
546 if (!skipval) {
547 glui32 thisval;
548
549 if (typeclass == '[') {
550
551 parse_glk_args(splot, &cx, depth+1, &gargnum, varglist[ix], passin);
552
553 }
554 else if (isarray) {
555 /* definitely isref */
556
557 switch (typeclass) {
558 case 'C':
559 /* This test checks for a giant array length, which is
560 deprecated. It displays a warning and cuts it down to
561 something reasonable. Future releases of this interpreter
562 may remove this test and go on to verify_array_addresses(),
563 which treats this case as a fatal error. */
564 if (varglist[ix+1] > endmem
565 || varglist[ix]+varglist[ix+1] > endmem) {
566 nonfatal_warning_i("Memory access was much too long -- perhaps a print_to_array call with only one argument", varglist[ix+1]);
567 varglist[ix+1] = endmem - varglist[ix];
568 }
569 verify_array_addresses(varglist[ix], varglist[ix+1], 1);
570 garglist[gargnum].array = CaptureCArray(varglist[ix], varglist[ix+1], passin);
571 gargnum++;
572 ix++;
573 garglist[gargnum].uint = varglist[ix];
574 gargnum++;
575 cx++;
576 break;
577 case 'I':
578 /* See comment above. */
579 if (varglist[ix+1] > endmem/4
580 || varglist[ix+1] > (endmem-varglist[ix])/4) {
581 nonfatal_warning_i("Memory access was much too long -- perhaps a print_to_array call with only one argument", varglist[ix+1]);
582 varglist[ix+1] = (endmem - varglist[ix]) / 4;
583 }
584 verify_array_addresses(varglist[ix], varglist[ix+1], 4);
585 garglist[gargnum].array = CaptureIArray(varglist[ix], varglist[ix+1], passin);
586 gargnum++;
587 ix++;
588 garglist[gargnum].uint = varglist[ix];
589 gargnum++;
590 cx++;
591 break;
592 case 'Q':
593 /* This case was added after the giant arrays were deprecated,
594 so we don't bother to allow for that case. We just verify
595 the length. */
596 verify_array_addresses(varglist[ix], varglist[ix+1], 4);
597 garglist[gargnum].array = CapturePtrArray(varglist[ix], varglist[ix+1], (*cx-'a'), passin);
598 gargnum++;
599 ix++;
600 garglist[gargnum].uint = varglist[ix];
601 gargnum++;
602 cx++;
603 break;
604 default:
605 fatal_error("Illegal format string.");
606 break;
607 }
608 }
609 else {
610 /* a plain value or a reference to one. */
611
612 if (isreturn) {
613 thisval = 0;
614 }
615 else if (depth > 0) {
616 /* Definitely not isref or isarray. */
617 if (subpassin)
618 thisval = ReadStructField(subaddress, ix);
619 else
620 thisval = 0;
621 }
622 else if (isref) {
623 if (passin)
624 thisval = ReadMemory(varglist[ix]);
625 else
626 thisval = 0;
627 }
628 else {
629 thisval = varglist[ix];
630 }
631
632 switch (typeclass) {
633 case 'I':
634 if (*cx == 'u')
635 garglist[gargnum].uint = (glui32)(thisval);
636 else if (*cx == 's')
637 garglist[gargnum].sint = (glsi32)(thisval);
638 else
639 fatal_error("Illegal format string.");
640 gargnum++;
641 cx++;
642 break;
643 case 'Q':
644 if (thisval) {
645 opref = classes_get(*cx-'a', thisval);
646 if (!opref) {
647 fatal_error("Reference to nonexistent Glk object.");
648 }
649 }
650 else {
651 opref = NULL;
652 }
653 garglist[gargnum].opaqueref = opref;
654 gargnum++;
655 cx++;
656 break;
657 case 'C':
658 if (*cx == 'u')
659 garglist[gargnum].uch = (unsigned char)(thisval);
660 else if (*cx == 's')
661 garglist[gargnum].sch = (signed char)(thisval);
662 else if (*cx == 'n')
663 garglist[gargnum].ch = (char)(thisval);
664 else
665 fatal_error("Illegal format string.");
666 gargnum++;
667 cx++;
668 break;
669 case 'S':
670 garglist[gargnum].charstr = DecodeVMString(thisval);
671 gargnum++;
672 break;
673 #ifdef GLK_MODULE_UNICODE
674 case 'U':
675 garglist[gargnum].unicharstr = DecodeVMUstring(thisval);
676 gargnum++;
677 break;
678 #endif /* GLK_MODULE_UNICODE */
679 default:
680 fatal_error("Illegal format string.");
681 break;
682 }
683 }
684 }
685 else {
686 /* We got a null reference, so we have to skip the format element. */
687 if (typeclass == '[') {
688 int numsubwanted, refdepth;
689 numsubwanted = 0;
690 while (*cx >= '0' && *cx <= '9') {
691 numsubwanted = 10 * numsubwanted + (*cx - '0');
692 cx++;
693 }
694 refdepth = 1;
695 while (refdepth > 0) {
696 if (*cx == '[')
697 refdepth++;
698 else if (*cx == ']')
699 refdepth--;
700 cx++;
701 }
702 }
703 else if (typeclass == 'S' || typeclass == 'U') {
704 /* leave it */
705 }
706 else {
707 cx++;
708 if (isarray)
709 ix++;
710 }
711 }
712 }
713
714 if (depth > 0) {
715 if (*cx != ']')
716 fatal_error("Illegal format string.");
717 cx++;
718 }
719 else {
720 if (*cx != ':' && *cx != '\0')
721 fatal_error("Illegal format string.");
722 }
723
724 *proto = cx;
725 *argnumptr = gargnum;
726 }
727
728 /* unparse_glk_args():
729 This is about the reverse of parse_glk_args().
730 */
731 static void unparse_glk_args(dispatch_splot_t *splot, char **proto, int depth,
732 int *argnumptr, glui32 subaddress, int subpassout)
733 {
734 char *cx;
735 int ix, argx;
736 int gargnum, numwanted;
737 void *opref;
738 gluniversal_t *garglist;
739 glui32 *varglist;
740
741 garglist = splot->garglist;
742 varglist = splot->varglist;
743 gargnum = *argnumptr;
744 cx = *proto;
745
746 numwanted = 0;
747 while (*cx >= '0' && *cx <= '9') {
748 numwanted = 10 * numwanted + (*cx - '0');
749 cx++;
750 }
751
752 for (argx = 0, ix = 0; argx < numwanted; argx++, ix++) {
753 char typeclass;
754 int skipval;
755 int isref, passin, passout, nullok, isarray, isretained, isreturn;
756 cx = read_prefix(cx, &isref, &isarray, &passin, &passout, &nullok,
757 &isretained, &isreturn);
758
759 typeclass = *cx;
760 cx++;
761
762 skipval = FALSE;
763 if (isref) {
764 if (!isreturn && varglist[ix] == 0) {
765 if (!nullok)
766 fatal_error("Zero passed invalidly to Glk function.");
767 garglist[gargnum].ptrflag = FALSE;
768 gargnum++;
769 skipval = TRUE;
770 }
771 else {
772 garglist[gargnum].ptrflag = TRUE;
773 gargnum++;
774 }
775 }
776 if (!skipval) {
777 glui32 thisval = 0;
778
779 if (typeclass == '[') {
780
781 unparse_glk_args(splot, &cx, depth+1, &gargnum, varglist[ix], passout);
782
783 }
784 else if (isarray) {
785 /* definitely isref */
786
787 switch (typeclass) {
788 case 'C':
789 ReleaseCArray(garglist[gargnum].array, varglist[ix], varglist[ix+1], passout);
790 gargnum++;
791 ix++;
792 gargnum++;
793 cx++;
794 break;
795 case 'I':
796 ReleaseIArray(garglist[gargnum].array, varglist[ix], varglist[ix+1], passout);
797 gargnum++;
798 ix++;
799 gargnum++;
800 cx++;
801 break;
802 case 'Q':
803 ReleasePtrArray(garglist[gargnum].array, varglist[ix], varglist[ix+1], (*cx-'a'), passout);
804 gargnum++;
805 ix++;
806 gargnum++;
807 cx++;
808 break;
809 default:
810 fatal_error("Illegal format string.");
811 break;
812 }
813 }
814 else {
815 /* a plain value or a reference to one. */
816
817 if (isreturn || (depth > 0 && subpassout) || (isref && passout)) {
818 skipval = FALSE;
819 }
820 else {
821 skipval = TRUE;
822 }
823
824 switch (typeclass) {
825 case 'I':
826 if (!skipval) {
827 if (*cx == 'u')
828 thisval = (glui32)garglist[gargnum].uint;
829 else if (*cx == 's')
830 thisval = (glui32)garglist[gargnum].sint;
831 else
832 fatal_error("Illegal format string.");
833 }
834 gargnum++;
835 cx++;
836 break;
837 case 'Q':
838 if (!skipval) {
839 opref = garglist[gargnum].opaqueref;
840 if (opref) {
841 gidispatch_rock_t objrock =
842 gidispatch_get_objrock(opref, *cx-'a');
843 thisval = ((classref_t *)objrock.ptr)->id;
844 }
845 else {
846 thisval = 0;
847 }
848 }
849 gargnum++;
850 cx++;
851 break;
852 case 'C':
853 if (!skipval) {
854 if (*cx == 'u')
855 thisval = (glui32)garglist[gargnum].uch;
856 else if (*cx == 's')
857 thisval = (glui32)garglist[gargnum].sch;
858 else if (*cx == 'n')
859 thisval = (glui32)garglist[gargnum].ch;
860 else
861 fatal_error("Illegal format string.");
862 }
863 gargnum++;
864 cx++;
865 break;
866 case 'S':
867 if (garglist[gargnum].charstr)
868 ReleaseVMString(garglist[gargnum].charstr);
869 gargnum++;
870 break;
871 #ifdef GLK_MODULE_UNICODE
872 case 'U':
873 if (garglist[gargnum].unicharstr)
874 ReleaseVMUstring(garglist[gargnum].unicharstr);
875 gargnum++;
876 break;
877 #endif /* GLK_MODULE_UNICODE */
878 default:
879 fatal_error("Illegal format string.");
880 break;
881 }
882
883 if (isreturn) {
884 *(splot->retval) = thisval;
885 }
886 else if (depth > 0) {
887 /* Definitely not isref or isarray. */
888 if (subpassout)
889 WriteStructField(subaddress, ix, thisval);
890 }
891 else if (isref) {
892 if (passout)
893 WriteMemory(varglist[ix], thisval);
894 }
895 }
896 }
897 else {
898 /* We got a null reference, so we have to skip the format element. */
899 if (typeclass == '[') {
900 int numsubwanted, refdepth;
901 numsubwanted = 0;
902 while (*cx >= '0' && *cx <= '9') {
903 numsubwanted = 10 * numsubwanted + (*cx - '0');
904 cx++;
905 }
906 refdepth = 1;
907 while (refdepth > 0) {
908 if (*cx == '[')
909 refdepth++;
910 else if (*cx == ']')
911 refdepth--;
912 cx++;
913 }
914 }
915 else if (typeclass == 'S' || typeclass == 'U') {
916 /* leave it */
917 }
918 else {
919 cx++;
920 if (isarray)
921 ix++;
922 }
923 }
924 }
925
926 if (depth > 0) {
927 if (*cx != ']')
928 fatal_error("Illegal format string.");
929 cx++;
930 }
931 else {
932 if (*cx != ':' && *cx != '\0')
933 fatal_error("Illegal format string.");
934 }
935
936 *proto = cx;
937 *argnumptr = gargnum;
938 }
939
940 /* find_stream_by_id():
941 This is used by some interpreter code which has to, well, find a Glk
942 stream given its ID.
943 */
944 strid_t find_stream_by_id(glui32 objid)
945 {
946 if (!objid)
947 return NULL;
948
949 /* Recall that class 1 ("b") is streams. */
950 return classes_get(gidisp_Class_Stream, objid);
951 }
952
953 /* find_id_for_window():
954 Return the ID of a given Glk window.
955 */
956 glui32 find_id_for_window(winid_t win)
957 {
958 gidispatch_rock_t objrock;
959
960 if (!win)
961 return 0;
962
963 objrock = gidispatch_get_objrock(win, gidisp_Class_Window);
964 if (!objrock.ptr)
965 return 0;
966 return ((classref_t *)objrock.ptr)->id;
967 }
968
969 /* find_id_for_stream():
970 Return the ID of a given Glk stream.
971 */
972 glui32 find_id_for_stream(strid_t str)
973 {
974 gidispatch_rock_t objrock;
975
976 if (!str)
977 return 0;
978
979 objrock = gidispatch_get_objrock(str, gidisp_Class_Stream);
980 if (!objrock.ptr)
981 return 0;
982 return ((classref_t *)objrock.ptr)->id;
983 }
984
985 /* find_id_for_fileref():
986 Return the ID of a given Glk fileref.
987 */
988 glui32 find_id_for_fileref(frefid_t fref)
989 {
990 gidispatch_rock_t objrock;
991
992 if (!fref)
993 return 0;
994
995 objrock = gidispatch_get_objrock(fref, gidisp_Class_Fileref);
996 if (!objrock.ptr)
997 return 0;
998 return ((classref_t *)objrock.ptr)->id;
999 }
1000
1001 /* find_id_for_schannel():
1002 Return the ID of a given Glk schannel.
1003 */
1004 glui32 find_id_for_schannel(schanid_t schan)
1005 {
1006 gidispatch_rock_t objrock;
1007
1008 if (!schan)
1009 return 0;
1010
1011 objrock = gidispatch_get_objrock(schan, gidisp_Class_Schannel);
1012 if (!objrock.ptr)
1013 return 0;
1014 return ((classref_t *)objrock.ptr)->id;
1015 }
1016
1017 /* Build a hash table to hold a set of Glk objects. */
1018 static classtable_t *new_classtable(glui32 firstid)
1019 {
1020 int ix;
1021 classtable_t *ctab = (classtable_t *)glulx_malloc(sizeof(classtable_t));
1022 if (!ctab)
1023 return NULL;
1024
1025 for (ix=0; ix<CLASSHASH_SIZE; ix++)
1026 ctab->bucket[ix] = NULL;
1027
1028 ctab->lastid = firstid;
1029
1030 return ctab;
1031 }
1032
1033 /* Find a Glk object in the appropriate hash table. */
1034 static void *classes_get(int classid, glui32 objid)
1035 {
1036 classtable_t *ctab;
1037 classref_t *cref;
1038 if (classid < 0 || classid >= num_classes)
1039 return NULL;
1040 ctab = classes[classid];
1041 cref = ctab->bucket[objid % CLASSHASH_SIZE];
1042 for (; cref; cref = cref->next) {
1043 if (cref->id == objid)
1044 return cref->obj;
1045 }
1046 return NULL;
1047 }
1048
1049 /* Put a Glk object in the appropriate hash table. If origid is zero,
1050 invent a new unique ID for it. */
1051 static classref_t *classes_put(int classid, void *obj, glui32 origid)
1052 {
1053 int bucknum;
1054 classtable_t *ctab;
1055 classref_t *cref;
1056 if (classid < 0 || classid >= num_classes)
1057 return NULL;
1058 ctab = classes[classid];
1059 cref = (classref_t *)glulx_malloc(sizeof(classref_t));
1060 if (!cref)
1061 return NULL;
1062 cref->obj = obj;
1063 if (!origid) {
1064 cref->id = ctab->lastid;
1065 ctab->lastid++;
1066 }
1067 else {
1068 cref->id = origid;
1069 if (ctab->lastid <= origid)
1070 ctab->lastid = origid+1;
1071 }
1072 bucknum = cref->id % CLASSHASH_SIZE;
1073 cref->bucknum = bucknum;
1074 cref->next = ctab->bucket[bucknum];
1075 ctab->bucket[bucknum] = cref;
1076 return cref;
1077 }
1078
1079 /* Delete a Glk object from the appropriate hash table. */
1080 static void classes_remove(int classid, void *obj)
1081 {
1082 classtable_t *ctab;
1083 classref_t *cref;
1084 classref_t **crefp;
1085 gidispatch_rock_t objrock;
1086 if (classid < 0 || classid >= num_classes)
1087 return;
1088 ctab = classes[classid];
1089 objrock = gidispatch_get_objrock(obj, classid);
1090 cref = objrock.ptr;
1091 if (!cref)
1092 return;
1093 crefp = &(ctab->bucket[cref->bucknum]);
1094 for (; *crefp; crefp = &((*crefp)->next)) {
1095 if ((*crefp) == cref) {
1096 *crefp = cref->next;
1097 if (!cref->obj) {
1098 nonfatal_warning("attempt to free NULL object!");
1099 }
1100 cref->obj = NULL;
1101 cref->id = 0;
1102 cref->next = NULL;
1103 glulx_free(cref);
1104 return;
1105 }
1106 }
1107 return;
1108 }
1109
1110 /* The object registration/unregistration callbacks that the library calls
1111 to keep the hash tables up to date. */
1112
1113 static gidispatch_rock_t glulxe_classtable_register(void *obj,
1114 glui32 objclass)
1115 {
1116 classref_t *cref;
1117 gidispatch_rock_t objrock;
1118 cref = classes_put(objclass, obj, 0);
1119 objrock.ptr = cref;
1120 return objrock;
1121 }
1122
1123 static void glulxe_classtable_unregister(void *obj, glui32 objclass,
1124 gidispatch_rock_t objrock)
1125 {
1126 classes_remove(objclass, obj);
1127 }
1128
1129 gidispatch_rock_t glulxe_classtable_register_existing(void *obj,
1130 glui32 objclass, glui32 dispid)
1131 {
1132 classref_t *cref;
1133 gidispatch_rock_t objrock;
1134 cref = classes_put(objclass, obj, dispid);
1135 objrock.ptr = cref;
1136 return objrock;
1137 }
1138
1139 static char *grab_temp_c_array(glui32 addr, glui32 len, int passin)
1140 {
1141 arrayref_t *arref = NULL;
1142 char *arr = NULL;
1143 glui32 ix, addr2;
1144
1145 if (len) {
1146 arr = (char *)glulx_malloc(len * sizeof(char));
1147 arref = (arrayref_t *)glulx_malloc(sizeof(arrayref_t));
1148 if (!arr || !arref)
1149 fatal_error("Unable to allocate space for array argument to Glk call.");
1150
1151 arref->array = arr;
1152 arref->addr = addr;
1153 arref->elemsize = 1;
1154 arref->retained = FALSE;
1155 arref->len = len;
1156 arref->next = arrays;
1157 arrays = arref;
1158
1159 if (passin) {
1160 for (ix=0, addr2=addr; ix<len; ix++, addr2+=1) {
1161 arr[ix] = Mem1(addr2);
1162 }
1163 }
1164 }
1165
1166 return arr;
1167 }
1168
1169 static void release_temp_c_array(char *arr, glui32 addr, glui32 len, int passout)
1170 {
1171 arrayref_t *arref = NULL;
1172 arrayref_t **aptr;
1173 glui32 ix, val, addr2;
1174
1175 if (arr) {
1176 for (aptr=(&arrays); (*aptr); aptr=(&((*aptr)->next))) {
1177 if ((*aptr)->array == arr)
1178 break;
1179 }
1180 arref = *aptr;
1181 if (!arref)
1182 fatal_error("Unable to re-find array argument in Glk call.");
1183 if (arref->addr != addr || arref->len != len)
1184 fatal_error("Mismatched array argument in Glk call.");
1185
1186 if (arref->retained) {
1187 return;
1188 }
1189
1190 *aptr = arref->next;
1191 arref->next = NULL;
1192
1193 if (passout) {
1194 for (ix=0, addr2=addr; ix<len; ix++, addr2+=1) {
1195 val = arr[ix];
1196 MemW1(addr2, val);
1197 }
1198 }
1199 glulx_free(arr);
1200 glulx_free(arref);
1201 }
1202 }
1203
1204 static glui32 *grab_temp_i_array(glui32 addr, glui32 len, int passin)
1205 {
1206 arrayref_t *arref = NULL;
1207 glui32 *arr = NULL;
1208 glui32 ix, addr2;
1209
1210 if (len) {
1211 arr = (glui32 *)glulx_malloc(len * sizeof(glui32));
1212 arref = (arrayref_t *)glulx_malloc(sizeof(arrayref_t));
1213 if (!arr || !arref)
1214 fatal_error("Unable to allocate space for array argument to Glk call.");
1215
1216 arref->array = arr;
1217 arref->addr = addr;
1218 arref->elemsize = 4;
1219 arref->retained = FALSE;
1220 arref->len = len;
1221 arref->next = arrays;
1222 arrays = arref;
1223
1224 if (passin) {
1225 for (ix=0, addr2=addr; ix<len; ix++, addr2+=4) {
1226 arr[ix] = Mem4(addr2);
1227 }
1228 }
1229 }
1230
1231 return arr;
1232 }
1233
1234 static void release_temp_i_array(glui32 *arr, glui32 addr, glui32 len, int passout)
1235 {
1236 arrayref_t *arref = NULL;
1237 arrayref_t **aptr;
1238 glui32 ix, val, addr2;
1239
1240 if (arr) {
1241 for (aptr=(&arrays); (*aptr); aptr=(&((*aptr)->next))) {
1242 if ((*aptr)->array == arr)
1243 break;
1244 }
1245 arref = *aptr;
1246 if (!arref)
1247 fatal_error("Unable to re-find array argument in Glk call.");
1248 if (arref->addr != addr || arref->len != len)
1249 fatal_error("Mismatched array argument in Glk call.");
1250
1251 if (arref->retained) {
1252 return;
1253 }
1254
1255 *aptr = arref->next;
1256 arref->next = NULL;
1257
1258 if (passout) {
1259 for (ix=0, addr2=addr; ix<len; ix++, addr2+=4) {
1260 val = arr[ix];
1261 MemW4(addr2, val);
1262 }
1263 }
1264 glulx_free(arr);
1265 glulx_free(arref);
1266 }
1267 }
1268
1269 static void **grab_temp_ptr_array(glui32 addr, glui32 len, int objclass, int passin)
1270 {
1271 arrayref_t *arref = NULL;
1272 void **arr = NULL;
1273 glui32 ix, addr2;
1274
1275 if (len) {
1276 arr = (void **)glulx_malloc(len * sizeof(void *));
1277 arref = (arrayref_t *)glulx_malloc(sizeof(arrayref_t));
1278 if (!arr || !arref)
1279 fatal_error("Unable to allocate space for array argument to Glk call.");
1280
1281 arref->array = arr;
1282 arref->addr = addr;
1283 arref->elemsize = sizeof(void *);
1284 arref->retained = FALSE;
1285 arref->len = len;
1286 arref->next = arrays;
1287 arrays = arref;
1288
1289 if (passin) {
1290 for (ix=0, addr2=addr; ix<len; ix++, addr2+=4) {
1291 glui32 thisval = Mem4(addr2);
1292 if (thisval)
1293 arr[ix] = classes_get(objclass, thisval);
1294 else
1295 arr[ix] = NULL;
1296 }
1297 }
1298 }
1299
1300 return arr;
1301 }
1302
1303 static void release_temp_ptr_array(void **arr, glui32 addr, glui32 len, int objclass, int passout)
1304 {
1305 arrayref_t *arref = NULL;
1306 arrayref_t **aptr;
1307 glui32 ix, val, addr2;
1308
1309 if (arr) {
1310 for (aptr=(&arrays); (*aptr); aptr=(&((*aptr)->next))) {
1311 if ((*aptr)->array == arr)
1312 break;
1313 }
1314 arref = *aptr;
1315 if (!arref)
1316 fatal_error("Unable to re-find array argument in Glk call.");
1317 if (arref->addr != addr || arref->len != len)
1318 fatal_error("Mismatched array argument in Glk call.");
1319
1320 if (arref->retained) {
1321 return;
1322 }
1323
1324 *aptr = arref->next;
1325 arref->next = NULL;
1326
1327 if (passout) {
1328 for (ix=0, addr2=addr; ix<len; ix++, addr2+=4) {
1329 void *opref = arr[ix];
1330 if (opref) {
1331 gidispatch_rock_t objrock =
1332 gidispatch_get_objrock(opref, objclass);
1333 val = ((classref_t *)objrock.ptr)->id;
1334 }
1335 else {
1336 val = 0;
1337 }
1338 MemW4(addr2, val);
1339 }
1340 }
1341 glulx_free(arr);
1342 glulx_free(arref);
1343 }
1344 }
1345
1346 static gidispatch_rock_t glulxe_retained_register(void *array,
1347 glui32 len, char *typecode)
1348 {
1349 gidispatch_rock_t rock;
1350 arrayref_t *arref = NULL;
1351 arrayref_t **aptr;
1352 int elemsize = 0;
1353
1354 if (typecode[4] == 'C')
1355 elemsize = 1;
1356 else if (typecode[4] == 'I')
1357 elemsize = 4;
1358
1359 if (!elemsize || array == NULL) {
1360 rock.ptr = NULL;
1361 return rock;
1362 }
1363
1364 for (aptr=(&arrays); (*aptr); aptr=(&((*aptr)->next))) {
1365 if ((*aptr)->array == array)
1366 break;
1367 }
1368 arref = *aptr;
1369 if (!arref)
1370 fatal_error("Unable to re-find array argument in Glk call.");
1371 if (arref->elemsize != elemsize || arref->len != len)
1372 fatal_error("Mismatched array argument in Glk call.");
1373
1374 arref->retained = TRUE;
1375
1376 rock.ptr = arref;
1377 return rock;
1378 }
1379
1380 static void glulxe_retained_unregister(void *array, glui32 len,
1381 char *typecode, gidispatch_rock_t objrock)
1382 {
1383 arrayref_t *arref = NULL;
1384 arrayref_t **aptr;
1385 glui32 ix, addr2, val;
1386 int elemsize = 0;
1387
1388 if (typecode[4] == 'C')
1389 elemsize = 1;
1390 else if (typecode[4] == 'I')
1391 elemsize = 4;
1392
1393 if (!elemsize || array == NULL) {
1394 return;
1395 }
1396
1397 for (aptr=(&arrays); (*aptr); aptr=(&((*aptr)->next))) {
1398 if ((*aptr)->array == array)
1399 break;
1400 }
1401 arref = *aptr;
1402 if (!arref)
1403 fatal_error("Unable to re-find array argument in Glk call.");
1404 if (arref != objrock.ptr)
1405 fatal_error("Mismatched array reference in Glk call.");
1406 if (!arref->retained)
1407 fatal_error("Unretained array reference in Glk call.");
1408 if (arref->elemsize != elemsize || arref->len != len)
1409 fatal_error("Mismatched array argument in Glk call.");
1410
1411 *aptr = arref->next;
1412 arref->next = NULL;
1413
1414 if (elemsize == 1) {
1415 for (ix=0, addr2=arref->addr; ix<arref->len; ix++, addr2+=1) {
1416 val = ((char *)array)[ix];
1417 MemW1(addr2, val);
1418 }
1419 }
1420 else if (elemsize == 4) {
1421 for (ix=0, addr2=arref->addr; ix<arref->len; ix++, addr2+=4) {
1422 val = ((glui32 *)array)[ix];
1423 MemW4(addr2, val);
1424 }
1425 }
1426
1427 glulx_free(array);
1428 glulx_free(arref);
1429 }
1430
1431 static long glulxe_array_locate(void *array, glui32 len,
1432 char *typecode, gidispatch_rock_t objrock, int *elemsizeref)
1433 {
1434 arrayref_t *arref = NULL;
1435 arrayref_t **aptr;
1436 int elemsize = 0;
1437
1438 if (typecode[4] == 'C')
1439 elemsize = 1;
1440 else if (typecode[4] == 'I')
1441 elemsize = 4;
1442
1443 if (!elemsize || array == NULL) {
1444 *elemsizeref = 0; /* No need to save the array separately */
1445 return (unsigned char *)array - memmap;
1446 }
1447
1448 for (aptr=(&arrays); (*aptr); aptr=(&((*aptr)->next))) {
1449 if ((*aptr)->array == array)
1450 break;
1451 }
1452 arref = *aptr;
1453 if (!arref)
1454 fatal_error("Unable to re-find array argument in array_locate.");
1455 if (arref != objrock.ptr)
1456 fatal_error("Mismatched array reference in array_locate.");
1457 if (!arref->retained)
1458 fatal_error("Unretained array reference in array_locate.");
1459 if (arref->elemsize != elemsize || arref->len != len)
1460 fatal_error("Mismatched array argument in array_locate.");
1461
1462 *elemsizeref = arref->elemsize;
1463 return arref->addr;
1464 }
1465
1466 static gidispatch_rock_t glulxe_array_restore(long bufkey,
1467 glui32 len, char *typecode, void **arrayref)
1468 {
1469 gidispatch_rock_t rock;
1470 int elemsize = 0;
1471
1472 if (typecode[4] == 'C')
1473 elemsize = 1;
1474 else if (typecode[4] == 'I')
1475 elemsize = 4;
1476
1477 if (!elemsize) {
1478 unsigned char *buf = memmap + bufkey;
1479 *arrayref = buf;
1480 rock.ptr = NULL;
1481 return rock;
1482 }
1483
1484 if (elemsize == 1) {
1485 char *cbuf = grab_temp_c_array(bufkey, len, FALSE);
1486 rock = glulxe_retained_register(cbuf, len, typecode);
1487 *arrayref = cbuf;
1488 }
1489 else {
1490 glui32 *ubuf = grab_temp_i_array(bufkey, len, FALSE);
1491 rock = glulxe_retained_register(ubuf, len, typecode);
1492 *arrayref = ubuf;
1493 }
1494 return rock;
1495 }
1496
1497 void set_library_select_hook(void (*func)(glui32, glui32, glui32, glui32))
1498 {
1499 library_select_hook = func;
1500 }
1501
1502 /* Create a string identifying this game. We use the first 64 bytes of the
1503 memory map, encoded as hex,
1504 */
1505 static char *get_game_id()
1506 {
1507 /* This buffer gets rewritten on every call, but that's okay -- the caller
1508 is supposed to copy out the result. */
1509 static char buf[2*64+2];
1510 int ix, jx;
1511
1512 if (!memmap)
1513 return NULL;
1514
1515 for (ix=0, jx=0; ix<64; ix++) {
1516 char ch = memmap[ix];
1517 int val = ((ch >> 4) & 0x0F);
1518 buf[jx++] = ((val < 10) ? (val + '0') : (val + 'A' - 10));
1519 val = (ch & 0x0F);
1520 buf[jx++] = ((val < 10) ? (val + '0') : (val + 'A' - 10));
1521 }
1522 buf[jx++] = '\0';
1523
1524 return buf;
1525 }
1526