-
Notifications
You must be signed in to change notification settings - Fork 10
/
gtb-probe.c
executable file
·8039 lines (6375 loc) · 178 KB
/
gtb-probe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This Software is distributed with the following X11 License,
sometimes also known as MIT license.
Copyright (c) 2010 Miguel A. Ballicora
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/* NBBOTF will remove the internal bitbase on the fly */
#ifdef NBBOTF
#ifdef WDL_PROBE
#undef WDL_PROBE
#endif
#else
#define WDL_PROBE
#endif
/*-- Intended to be modified to make public --> Supporting functions the TB generator ---------------------*/
#ifdef GTB_SHARE
#define SHARED_forbuilding
#endif
/*---------------------------------------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gtb-probe.h"
#if defined(SHARED_forbuilding)
#include "gtb-prob2.h"
#else
#define mySHARED static
typedef unsigned char SQ_CONTENT;
typedef unsigned int SQUARE;
#endif
#include "sysport.h"
#include "gtb-att.h"
#include "gtb-types.h"
/*---------------------------------------------------------------------------------------------------------*/
/*#include "posit_t.h"*/
#define MAX_LISTSIZE 17
#if 0
typedef unsigned sq_t;
typedef unsigned char pc_t;
typedef uint32_t mv_t;
#endif
struct posit {
sq_t ws[MAX_LISTSIZE];
sq_t bs[MAX_LISTSIZE];
pc_t wp[MAX_LISTSIZE];
pc_t bp[MAX_LISTSIZE];
sq_t ep;
unsigned int stm;
unsigned int cas;
};
typedef struct posit posit_t;
#if 0
typedef long int tbkey_t;
#endif
/*---------------------------------------------------------------------------------------------------------*/
/*#include "bool_t.h"*/
#if !defined(H_BOOL)
typedef int bool_t;
#endif
#if !defined(TRUE)
#define TRUE ((bool_t)1)
#endif
#if !defined(FALSE)
#define FALSE ((bool_t)0)
#endif
/*--------- private if external building code is not present ----------------------------------------------*/
#if !defined(SHARED_forbuilding)
#define MAX_EGKEYS 145
#define SLOTSIZE 1
#define NOINDEX ((index_t)(-1))
#if 0
typedef unsigned short int dtm_t;
typedef size_t index_t;
/*typedef int index_t;*/
#endif
enum Loading_status {
STATUS_ABSENT = 0,
STATUS_STATICRAM = 1,
STATUS_MALLOC = 2,
STATUS_FILE = 3,
STATUS_REJECT = 4
};
struct endgamekey {
int id;
const char *str;
index_t maxindex;
index_t slice_n;
void (*itopc) (index_t, SQUARE *, SQUARE *);
bool_t (*pctoi) (const SQUARE *, const SQUARE *, index_t *);
dtm_t * egt_w;
dtm_t * egt_b;
FILE * fd;
int status;
int pathn;
};
#endif
/*----------------------------------------------------------------------------------------------------------*/
/* array for better moves */
#ifdef GTB_SHARE
mySHARED int bettarr [2] [8] [8];
#endif
/*------------ ENUMS ----------------------------------------------------------*/
enum Mask_values {
RESMASK = tb_RESMASK,
INFOMASK = tb_INFOMASK,
PLYSHIFT = tb_PLYSHIFT
};
enum Info_values {
iDRAW = tb_DRAW,
iWMATE = tb_WMATE,
iBMATE = tb_BMATE,
iFORBID = tb_FORBID,
iDRAWt = tb_DRAW |4,
iWMATEt = tb_WMATE |4,
iBMATEt = tb_BMATE |4,
iUNKNOWN = tb_UNKNOWN,
iUNKNBIT = (1<<2)
};
/*-------------------------- inherited from a previous maindef.h -----------*/
#define WHITES (1u<<6)
#define BLACKS (1u<<7)
#define NOPIECE 0u
#define PAWN 1u
#define KNIGHT 2u
#define BISHOP 3u
#define ROOK 4u
#define QUEEN 5u
#define KING 6u
#define WH 0
#define BL 1
#define Opp(x) ((x)^1)
#define wK (KING | WHITES)
/*-------------------
SQUARES
-------------------*/
/* from 1-63 different squares posibles */
/*squares*/
enum SQUARES {
A1,B1,C1,D1,E1,F1,G1,H1,
A2,B2,C2,D2,E2,F2,G2,H2,
A3,B3,C3,D3,E3,F3,G3,H3,
A4,B4,C4,D4,E4,F4,G4,H4,
A5,B5,C5,D5,E5,F5,G5,H5,
A6,B6,C6,D6,E6,F6,G6,H6,
A7,B7,C7,D7,E7,F7,G7,H7,
A8,B8,C8,D8,E8,F8,G8,H8,
NOSQUARE,
ERRSQUARE = 128
};
/*------------------- end of inherited from a previous maindef.h -----------*/
#if !defined(NDEBUG)
#define NDEBUG
#endif
#ifdef DEBUG
#undef NDEBUG
#endif
#include "assert.h"
/*------------------- general DEFINES--------------------------- -----------*/
#define gtbNOSIDE ((unsigned)-1)
#define gtbNOINDEX ((index_t)-1)
/*************************************************\
|
| COMPRESSION SCHEMES
|
\*************************************************/
#include "gtb-dec.h"
static const char *const Extension[] = {
".gtb.cp0"
,".gtb.cp1"
,".gtb.cp2"
,".gtb.cp3"
,".gtb.cp4"
,".gtb.cp5"
,".gtb.cp6"
,".gtb.cp7"
,".gtb.cp8"
,".gtb.cp9"
};
/*************************************************\
|
| MOVES
|
\*************************************************/
enum move_kind {
NORMAL_MOVE = 0,
CASTLE_MOVE,
PASSNT_MOVE,
PROMOT_MOVE
};
enum move_content {
NOMOVE = 0
};
#define MV_TYPE(mv) ( (BYTE) ((mv) >>6 & 3 ) )
#define MV_TO(mv) ( (SQUARE) ((mv) >>8 & 63) )
#define MV_PT(mv) ( (SQ_CONTENT) ((mv) >>(3+16) &7 ) )
#define MV_TK(mv) ( (SQ_CONTENT) ((mv) >>(6+16) &7 ) )
#define MV_FROM(mv) ( (SQUARE) ((mv) & 63) )
/*
| move,type,color,piece,from,to,taken,promoted
*------------------------------------------------------------------*/
#define MV_BUILD(mv,ty,co,pc,fr,to,tk,pm) ( \
(mv) = (fr) | (to)<< 8 | (ty)<< 6 | (co)<<8 \
| (pc)<<16 | (pm)<< (3+16) | (tk)<< (6+16) \
)
#define MV_ADD_TOTK(mv,to,tk) ( \
mv |= (uint32_t)(to) << 8 \
| (uint32_t)(tk) << (6+16) \
)
#define map88(x) ( (x) + ((x)&070) )
#define unmap88(x) ( ( (x) + ((x)& 07) ) >> 1 )
/*************************************************\
|
| STATIC VARIABLES
|
\*************************************************/
static int GTB_scheme = 4;
/*************************************************\
|
| needed for
| PRE LOAD CACHE AND DEPENDENT FUNCTIONS
|
\*************************************************/
#define EGTB_MAXBLOCKSIZE 65536
static int GTB_MAXOPEN = 4;
static bool_t Uncompressed = TRUE;
static unsigned char Buffer_zipped [EGTB_MAXBLOCKSIZE];
static unsigned char Buffer_packed [EGTB_MAXBLOCKSIZE];
static unsigned int zipinfo_init (void);
static void zipinfo_done (void);
enum Flip_flags {
WE_FLAG = 1, NS_FLAG = 2, NW_SE_FLAG = 4
}; /* used in flipt */
struct filesopen {
int n;
tbkey_t *key;
};
/* STATIC GLOBALS */
static struct filesopen fd = {0, NULL};
static bool_t TB_INITIALIZED = FALSE;
static bool_t DTM_CACHE_INITIALIZED = FALSE;
static int WDL_FRACTION = 64;
static int WDL_FRACTION_MAX = 128;
static size_t DTM_cache_size = 0;
static size_t WDL_cache_size = 0;
static unsigned int TB_AVAILABILITY = 0;
/* LOCKS */
static mythread_mutex_t Egtb_lock;
/****************************************************************************\
*
*
* DEBUGGING or PRINTING ZONE
*
*
****************************************************************************/
#if 0
#define FOLLOW_EGTB
#ifndef DEBUG
#define DEBUG
#endif
#endif
#define validsq(x) ((x) >= A1 && (x) <= H8)
#if defined(DEBUG)
static void print_pos (const sq_t *ws, const sq_t *bs, const pc_t *wp, const pc_t *bp);
#endif
#if defined(DEBUG) || defined(FOLLOW_EGTB)
static void output_state (unsigned stm, const SQUARE *wSQ, const SQUARE *bSQ,
const SQ_CONTENT *wPC, const SQ_CONTENT *bPC);
static const char *Square_str[64] = {
"a1","b1","c1","d1","e1","f1","g1","h1",
"a2","b2","c2","d2","e2","f2","g2","h2",
"a3","b3","c3","d3","e3","f3","g3","h3",
"a4","b4","c4","d4","e4","f4","g4","h4",
"a5","b5","c5","d5","e5","f5","g5","h5",
"a6","b6","c6","d6","e6","f6","g6","h6",
"a7","b7","c7","d7","e7","f7","g7","h7",
"a8","b8","c8","d8","e8","f8","g8","h8"
};
static const char *P_str[] = {
"--", "P", "N", "B", "R", "Q", "K"
};
#endif
#ifdef FOLLOW_EGTB
#define STAB
#define STABCONDITION 1 /*(stm == BL && whiteSQ[0]==H1 && whiteSQ[1]==D1 && whiteSQ[2]==D3 && blackSQ[0]==C2 )*/
static bool_t GLOB_REPORT = TRUE;
#endif
#if defined(FOLLOW_EGTB)
static const char *Info_str[8] = {
" Draw", " Wmate", " Bmate", "Illegal",
"~Draw", "~Wmate", "~Bmate", "Unknown"
};
#endif
static void list_index (void);
static void fatal_error(void) {
exit(EXIT_FAILURE);
}
#ifdef STAB
#define FOLLOW_LU(x,y) {if (GLOB_REPORT) printf ("************** %s: %lu\n", (x), (long unsigned)(y));}
#else
#define FOLLOW_LU(x,y)
#endif
#ifdef STAB
#define FOLLOW_LULU(x,y,z) {if (GLOB_REPORT) printf ("************** %s: %lu, %lu\n", (x), (long unsigned)(y), (long unsigned)(z));}
#else
#define FOLLOW_LULU(x,y,z)
#endif
#ifdef STAB
#define FOLLOW_label(x) {if (GLOB_REPORT) printf ("************** %s\n", (x));}
#else
#define FOLLOW_label(x)
#endif
#ifdef STAB
#define FOLLOW_DTM(msg,dtm) {if (GLOB_REPORT) printf ("************** %s: %lu, info:%s, plies:%lu \n"\
, (msg), (long unsigned)(dtm), (Info_str[(dtm)&INFOMASK]), (long unsigned)((dtm)>>PLYSHIFT)\
);}
#else
#define FOLLOW_DTM(msg,dtm)
#endif
/*--------------------------------*\
|
|
| INDEXING FUNCTIONS
|
|
*---------------------------------*/
#define IDX_set_empty(x) {x=0;x--;}
#define IDX_is_empty(x) (0==(1+(x)))
#define NO_KKINDEX NOINDEX
#define MAX_KKINDEX 462
#define MAX_PPINDEX 576
#define MAX_PpINDEX (24 * 48)
/*1128*/
#define MAX_AAINDEX ((63-62) + (62 * (127-62)/2) - 1 + 1)
#define MAX_AAAINDEX (64*21*31)
#define MAX_PP48_INDEX (1128)
/* (24*23*22/6) + 24 * (24*23/2) */
#define MAX_PPP48_INDEX 8648
/* VARIABLES */
static index_t kkidx [64] [64];
static index_t ppidx [24] [48];
static index_t pp48_idx[48][48];
static index_t ppp48_idx[48][48][48];
static sq_t wksq [MAX_KKINDEX];
static sq_t bksq [MAX_KKINDEX];
static sq_t pp48_sq_x[MAX_PP48_INDEX];
static sq_t pp48_sq_y[MAX_PP48_INDEX];
static index_t pp_hi24 [MAX_PPINDEX]; /* was unsigned int */
static index_t pp_lo48 [MAX_PPINDEX];
static unsigned int flipt [64] [64];
static index_t aaidx [64] [64]; /* was unsigned int */
static unsigned char aabase [MAX_AAINDEX];
static uint8_t ppp48_sq_x[MAX_PPP48_INDEX];
static uint8_t ppp48_sq_y[MAX_PPP48_INDEX];
static uint8_t ppp48_sq_z[MAX_PPP48_INDEX];
/* FUNCTIONS */
static void init_indexing (int verbosity);
static void norm_kkindex (SQUARE x, SQUARE y, /*@out@*/ SQUARE *pi, /*@out@*/ SQUARE *pj);
static void pp_putanchorfirst (SQUARE a, SQUARE b, /*@out@*/ SQUARE *out_anchor, /*@out@*/ SQUARE *out_loosen);
static index_t wsq_to_pidx24 (SQUARE pawn);
static index_t wsq_to_pidx48 (SQUARE pawn);
static SQUARE pidx24_to_wsq (index_t a);
static SQUARE pidx48_to_wsq (index_t a);
static SQUARE flipWE (SQUARE x) { return x ^ 07;}
static SQUARE flipNS (SQUARE x) { return x ^ 070;}
static SQUARE flipNW_SE (SQUARE x) { return ((x&7)<<3) | (x>>3);}
static SQUARE getcol (SQUARE x) { return x & 7;}
static SQUARE getrow (SQUARE x) { return x >> 3;}
static bool_t in_queenside (sq_t x) { return 0 == (x & (1<<2));}
/* 1:0 */
static void kxk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 2:0 */
static void kabk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kakb_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kaak_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 2:1 */
static void kabkc_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kaakb_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 3:0 */
static void kabck_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kaabk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kaaak_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kabbk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* one pawn */
static void kpk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 1:1 one pawn */
static void kakp_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 2:0 one pawn */
static void kapk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 2:0 two pawns */
static void kppk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 2:1 one pawn */
static void kapkb_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kabkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kaakp_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 2:1 + 3:0 two pawns */
static void kppka_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kappk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kapkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 3:0 one pawn */
static void kabpk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kaapk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* three pawns */
static void kpppk_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
static void kppkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* 1:1 two pawns */
static void kpkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb);
/* corresponding pc to index */
static bool_t kxk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kabk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kakb_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kpk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kakp_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kapk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kppk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kaak_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kabkc_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kaakb_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);/**/
static bool_t kabck_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kaabk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);/**/
static bool_t kaaak_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kabbk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);/**/
static bool_t kapkb_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kabkp_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
static bool_t kaakp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out);
static bool_t kppka_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out);
static bool_t kappk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out);
static bool_t kapkp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out);
static bool_t kabpk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out);
static bool_t kaapk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out);
static bool_t kppkp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out);
static bool_t kpppk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out);
static bool_t kpkp_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);
/* testing functions */
static bool_t test_kppk (void);
static bool_t test_kaakb (void);
static bool_t test_kaabk (void);
static bool_t test_kaaak (void);
static bool_t test_kabbk (void);
static bool_t test_kapkb (void);
static bool_t test_kabkp (void);
static bool_t test_kppka (void);
static bool_t test_kappk (void);
static bool_t test_kapkp (void);
static bool_t test_kabpk (void);
static bool_t test_kaapk (void);
static bool_t test_kaakp (void);
static bool_t test_kppkp (void);
static bool_t test_kpppk (void);
static unsigned flip_type (SQUARE x, SQUARE y);
static index_t init_kkidx (void);
static index_t init_ppidx (void);
static void init_flipt (void);
static index_t init_aaidx (void);
static index_t init_aaa (void);
static index_t init_pp48_idx (void);
static index_t init_ppp48_idx (void);
enum TB_INDEXES
{ MAX_KXK = MAX_KKINDEX*64
,MAX_kabk = MAX_KKINDEX*64*64
,MAX_kakb = MAX_KKINDEX*64*64
,MAX_kpk = 24*64*64
,MAX_kakp = 24*64*64*64
,MAX_kapk = 24*64*64*64
,MAX_kppk = MAX_PPINDEX*64*64
,MAX_kpkp = MAX_PpINDEX*64*64
,MAX_kaak = MAX_KKINDEX*MAX_AAINDEX
,MAX_kabkc = MAX_KKINDEX*64*64*64
,MAX_kabck = MAX_KKINDEX*64*64*64
,MAX_kaakb = MAX_KKINDEX*MAX_AAINDEX*64
,MAX_kaabk = MAX_KKINDEX*MAX_AAINDEX*64
,MAX_kabbk = MAX_KKINDEX*MAX_AAINDEX*64
,MAX_kaaak = MAX_KKINDEX*MAX_AAAINDEX
,MAX_kapkb = 24*64*64*64*64
,MAX_kabkp = 24*64*64*64*64
,MAX_kabpk = 24*64*64*64*64
,MAX_kppka = MAX_kppk*64
,MAX_kappk = MAX_kppk*64
,MAX_kapkp = MAX_kpkp*64
,MAX_kaapk = 24*MAX_AAINDEX*64*64
,MAX_kaakp = 24*MAX_AAINDEX*64*64
,MAX_kppkp = 24*MAX_PP48_INDEX*64*64
,MAX_kpppk = MAX_PPP48_INDEX*64*64
};
#if defined(SHARED_forbuilding)
extern index_t
biggest_memory_needed (void) {
return MAX_kabkc;
}
#endif
/*--------------------------------*\
|
|
| CACHE PROTOTYPES
|
|
*---------------------------------*/
#if !defined(SHARED_forbuilding)
mySHARED bool_t get_dtm (tbkey_t key, unsigned side, index_t idx, dtm_t *out, bool_t probe_hard);
#endif
static bool_t get_dtm_from_cache (tbkey_t key, unsigned side, index_t idx, dtm_t *out);
/*--------------------------------*\
|
|
| INIT
|
|
*---------------------------------*/
static bool_t fd_init (struct filesopen *pfd);
static void fd_done (struct filesopen *pfd);
static void RAM_egtbfree (void);
/*--------------------------------------------------------------------------*/
#if !defined(SHARED_forbuilding)
mySHARED void egtb_freemem (int i);
#endif
mySHARED struct endgamekey egkey[] = {
{0, "kqk", MAX_KXK, 1, kxk_indextopc, kxk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{1, "krk", MAX_KXK, 1, kxk_indextopc, kxk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{2, "kbk", MAX_KXK, 1, kxk_indextopc, kxk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{3, "knk", MAX_KXK, 1, kxk_indextopc, kxk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{4, "kpk", MAX_kpk, 24,kpk_indextopc, kpk_pctoindex, NULL , NULL ,NULL ,0, 0 },
/* 4 pieces */
{5, "kqkq", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{6, "kqkr", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{7, "kqkb", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{8, "kqkn", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{9, "krkr", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{10,"krkb", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{11,"krkn", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{12,"kbkb", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{13,"kbkn", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{14,"knkn", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
/**/
{15,"kqqk", MAX_kaak, 1, kaak_indextopc, kaak_pctoindex, NULL , NULL ,NULL ,0, 0 },
{16,"kqrk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{17,"kqbk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{18,"kqnk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{19,"krrk", MAX_kaak, 1, kaak_indextopc, kaak_pctoindex, NULL , NULL ,NULL ,0, 0 },
{20,"krbk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{21,"krnk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{22,"kbbk", MAX_kaak, 1, kaak_indextopc, kaak_pctoindex, NULL , NULL ,NULL ,0, 0 },
{23,"kbnk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{24,"knnk", MAX_kaak, 1, kaak_indextopc, kaak_pctoindex, NULL , NULL ,NULL ,0, 0 },
/**/
/**/
{25,"kqkp", MAX_kakp, 24,kakp_indextopc, kakp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{26,"krkp", MAX_kakp, 24,kakp_indextopc, kakp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{27,"kbkp", MAX_kakp, 24,kakp_indextopc, kakp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{28,"knkp", MAX_kakp, 24,kakp_indextopc, kakp_pctoindex, NULL , NULL ,NULL ,0, 0 },
/**/
{29,"kqpk", MAX_kapk, 24,kapk_indextopc, kapk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{30,"krpk", MAX_kapk, 24,kapk_indextopc, kapk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{31,"kbpk", MAX_kapk, 24,kapk_indextopc, kapk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{32,"knpk", MAX_kapk, 24,kapk_indextopc, kapk_pctoindex, NULL , NULL ,NULL ,0, 0 },
/**/
{33,"kppk", MAX_kppk, MAX_PPINDEX ,kppk_indextopc, kppk_pctoindex, NULL , NULL ,NULL ,0, 0 },
/**/
{34,"kpkp", MAX_kpkp, MAX_PpINDEX ,kpkp_indextopc, kpkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
/**/
/**/
/* 5 pieces */
{ 35,"kqqqk", MAX_kaaak, 1, kaaak_indextopc, kaaak_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 36,"kqqrk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 37,"kqqbk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 38,"kqqnk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 39,"kqrrk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 40,"kqrbk", MAX_kabck, 1, kabck_indextopc, kabck_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 41,"kqrnk", MAX_kabck, 1, kabck_indextopc, kabck_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 42,"kqbbk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 43,"kqbnk", MAX_kabck, 1, kabck_indextopc, kabck_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 44,"kqnnk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 45,"krrrk", MAX_kaaak, 1, kaaak_indextopc, kaaak_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 46,"krrbk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 47,"krrnk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 48,"krbbk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 49,"krbnk", MAX_kabck, 1, kabck_indextopc, kabck_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 50,"krnnk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 51,"kbbbk", MAX_kaaak, 1, kaaak_indextopc, kaaak_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 52,"kbbnk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 53,"kbnnk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 54,"knnnk", MAX_kaaak, 1, kaaak_indextopc, kaaak_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 55,"kqqkq", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 56,"kqqkr", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 57,"kqqkb", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 58,"kqqkn", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 59,"kqrkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 60,"kqrkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 61,"kqrkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 62,"kqrkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 63,"kqbkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 64,"kqbkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 65,"kqbkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 66,"kqbkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 67,"kqnkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 68,"kqnkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 69,"kqnkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 70,"kqnkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 71,"krrkq", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 72,"krrkr", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 73,"krrkb", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 74,"krrkn", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 75,"krbkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 76,"krbkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 77,"krbkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 78,"krbkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 79,"krnkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 80,"krnkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 81,"krnkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 82,"krnkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 83,"kbbkq", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 84,"kbbkr", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 85,"kbbkb", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 86,"kbbkn", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 87,"kbnkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 88,"kbnkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 89,"kbnkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 90,"kbnkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 91,"knnkq", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 92,"knnkr", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 93,"knnkb", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 94,"knnkn", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 95,"kqqpk", MAX_kaapk, 24, kaapk_indextopc, kaapk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 96,"kqrpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 97,"kqbpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 98,"kqnpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{ 99,"krrpk", MAX_kaapk, 24, kaapk_indextopc, kaapk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{100,"krbpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{101,"krnpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{102,"kbbpk", MAX_kaapk, 24, kaapk_indextopc, kaapk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{103,"kbnpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{104,"knnpk", MAX_kaapk, 24, kaapk_indextopc, kaapk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{105,"kqppk", MAX_kappk, MAX_PPINDEX, kappk_indextopc, kappk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{106,"krppk", MAX_kappk, MAX_PPINDEX, kappk_indextopc, kappk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{107,"kbppk", MAX_kappk, MAX_PPINDEX, kappk_indextopc, kappk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{108,"knppk", MAX_kappk, MAX_PPINDEX, kappk_indextopc, kappk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{109,"kqpkq", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{110,"kqpkr", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{111,"kqpkb", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{112,"kqpkn", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{113,"krpkq", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{114,"krpkr", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{115,"krpkb", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{116,"krpkn", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{117,"kbpkq", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{118,"kbpkr", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{119,"kbpkb", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{120,"kbpkn", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{121,"knpkq", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{122,"knpkr", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{123,"knpkb", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{124,"knpkn", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 },
{125,"kppkq", MAX_kppka, MAX_PPINDEX, kppka_indextopc, kppka_pctoindex, NULL , NULL ,NULL ,0, 0 },
{126,"kppkr", MAX_kppka, MAX_PPINDEX, kppka_indextopc, kppka_pctoindex, NULL , NULL ,NULL ,0, 0 },
{127,"kppkb", MAX_kppka, MAX_PPINDEX, kppka_indextopc, kppka_pctoindex, NULL , NULL ,NULL ,0, 0 },
{128,"kppkn", MAX_kppka, MAX_PPINDEX, kppka_indextopc, kppka_pctoindex, NULL , NULL ,NULL ,0, 0 },
{129,"kqqkp", MAX_kaakp, 24, kaakp_indextopc, kaakp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{130,"kqrkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{131,"kqbkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{132,"kqnkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{133,"krrkp", MAX_kaakp, 24, kaakp_indextopc, kaakp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{134,"krbkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{135,"krnkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{136,"kbbkp", MAX_kaakp, 24, kaakp_indextopc, kaakp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{137,"kbnkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{138,"knnkp", MAX_kaakp, 24, kaakp_indextopc, kaakp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{139,"kqpkp", MAX_kapkp, MAX_PpINDEX, kapkp_indextopc, kapkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{140,"krpkp", MAX_kapkp, MAX_PpINDEX, kapkp_indextopc, kapkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{141,"kbpkp", MAX_kapkp, MAX_PpINDEX, kapkp_indextopc, kapkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{142,"knpkp", MAX_kapkp, MAX_PpINDEX, kapkp_indextopc, kapkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{143,"kppkp", MAX_kppkp, 24*MAX_PP48_INDEX, kppkp_indextopc, kppkp_pctoindex, NULL , NULL ,NULL ,0, 0 },
{144,"kpppk", MAX_kpppk, MAX_PPP48_INDEX, kpppk_indextopc, kpppk_pctoindex, NULL , NULL ,NULL ,0, 0 },
{MAX_EGKEYS, NULL, 0, 1, NULL, NULL, NULL, NULL ,NULL ,0 ,0}
};
static int eg_was_open[MAX_EGKEYS];
static uint64_t Bytes_read = 0;
/****************************************************************************\
|
|
| PATH MANAGEMENT ZONE
|
|
\****************************************************************************/
#define MAXPATHLEN tb_MAXPATHLEN
#define MAX_GTBPATHS 10
static int Gtbpath_end_index = 0;
static const char ** Gtbpath = NULL;
/*---------------- EXTERNAL PATH MANAGEMENT --------------------------------*/
extern const char *tbpaths_getmain (void) { return Gtbpath[0];}
extern const char **
tbpaths_init(void)
{
const char **newps;
newps = (const char **) malloc (sizeof (char *));
if (newps != NULL) {
newps[0] = NULL;
}
return newps;
}
static const char **
tbpaths_add_single(const char **ps, const char *newpath)
{
size_t counter;
const char **newps;
size_t i, psize;
char *ppath;
if (NULL == ps)
return NULL;
psize = strlen(newpath) + 1;
ppath = (char *) malloc (psize * sizeof (char));
if (NULL == ppath)
return ps; /* failed to incorporate a new path */
for (i = 0; i < psize; i++) ppath[i] = newpath[i];
for (counter = 0; ps[counter] != NULL; counter++)
;
/* cast to deal with const poisoning */
newps = (const char **) realloc ((char **)ps, sizeof(char *) * (counter+2));
if (newps != NULL) {
newps [counter] = ppath;
newps [counter+1] = NULL;
}
return newps;
}
extern const char **
tbpaths_add(const char **ps, const char *newpath)
{
size_t i, psize;
char *mpath;
if (NULL == ps)
return NULL;
psize = strlen(newpath) + 1;
mpath = (char *) malloc (psize * sizeof (char));
if (NULL == mpath) {
return ps; /* failed to incorporate a new path */
}
for (i = 0; i < psize; i++) mpath[i] = newpath[i];
for (i = 0; i < psize; i++) {
if(';' == mpath[i])
mpath[i] = '\0';
}
for (i = 0;;) {
while (i < psize && mpath[i] == '\0') i++;
if (i >= psize) break;
ps = tbpaths_add_single (ps, &mpath[i]);
while (i < psize && mpath[i] != '\0') i++;
}
free(mpath);
return ps;
}
extern const char **
tbpaths_done(const char **ps)
{
int counter;
void *q;
if (ps != NULL) {
for (counter = 0; ps[counter] != NULL; counter++) {
/* cast to deal with const poisoning */
void *p = (void *) ps[counter];
free(p);
}
/* cast to deal with const poisoning */
q = (void *) ps;
free(q);
}
return NULL;
}
/*---------------- PATH INITIALIZATION ROUTINES ----------------------------*/
static void path_system_reset(void) {Gtbpath_end_index = 0;}
static bool_t
path_system_init (const char **path)
{
size_t i;
size_t sz;
const char *x;
bool_t ok = TRUE;
path_system_reset();
if (path == NULL) {
return FALSE;
}
/* calculate needed size for Gtbpath */
i = 0;
do {
x = path[i++];
} while (x != NULL);
sz = i; /* sz includes the NULL */
Gtbpath = (const char **) malloc (sz * sizeof(char *));
if (Gtbpath) {
ok = TRUE;
/* point to the same strings provided */
Gtbpath_end_index = 0;
for (i = 0; i < sz; i++) {
Gtbpath[i] = path[i];
Gtbpath_end_index++;
}
} else {
ok = FALSE;
}
return ok;
}
static void
path_system_done (void)
{
/* before we free Gtbpath, we have to deal with the
"const poisoning" and cast it. free() does not accept
const pointers */
char ** p = (char **) Gtbpath;
/* clean up */
if (p != NULL)
free(p);