Source

blake / blake_protocol.c

Full commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
/*
Copyright (C) 2001-2007 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/*
  *------------------------------------------------------------------*
  * OverChat private, internal protocol two way communication layer  *
  * for clients and for the server.                                  *
  *------------------------------------------------------------------*

Warning: Protocol is in development.
*/

/*
  This block of pragmas is only temporary and stops me being
  bombarded with warnings everytime I compile.  They will all eventually
  be eliminated.
*/
#ifdef __BORLANDC__
# pragma warn -aus
# pragma warn -par
# pragma warn -use
# pragma warn -eff
# pragma warn -voi
# pragma warn -ret
# pragma warn -sus
# pragma warn -nak
# pragma warn -rvl
#endif /*__BORLANDC__*/

#define BLAKE_SOURCE

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /*HAVE_CONFIG_H*/

#include <assert.h>
#include <stdio.h>
#include <time.h>

#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif /*HAVE_LIMITS_H*/

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif /*HAVE_STDLIB_H*/

#ifdef HAVE_STRING_H
# include <string.h>
#endif /*HAVE_STRING_H*/

#ifdef BLAKE_HDRSTOP
# pragma hdrstop
#endif /*BLAKE_HDRSTOP*/

#ifdef HAVE_DLSTDINC_H
# include "dlstdinc.h"
#endif /*HAVE_DLSTDINC_H*/

#ifdef HAVE_DPCRTLMM_H
# include "dpcrtlmm.h"
#endif /*HAVE_DPCRTLMM_H*/

#ifdef HAVE_DAYBOCRYPT_H
# include "daybocrypt.h"
#endif /*HAVE_DAYBOCRYPT_H*/

#include "blake_threads.h"
#include "blake.h"
#include "blake_main.h"
#include "blake_cb.h"
#include "blake_sockets.h"
#include "blake_logger.h"
#include "blake_debugtables.h"
#include "blake_protocol.h"
/*-------------------------------------------------------------------------*/
static const char *const rcsid =
  "$Id: blake_protocol.c,v 0.5.5 2007/01/14 16:02:00 ddrp Exp $";
/*-------------------------------------------------------------------------*/
/* Constants grouped so I don't have to search them out */
#define BLAKE_GOTERROR_FIXED (0x70b0)
#define BLAKE_JOINCHAT_DUMMY (0x55)
#define BLAKE_DEFAULT_EXCHANGE generalRooms
#define BLAKE_KICK_DUMMY1 (0x6714)
#define BLAKE_KICK_DUMMY2 (0x1177)
#define BLAKE_CHATMSG_SPOOF (0x12)

/*
  Number of packets for RX/TX queues in Blake protocol interface
*/
#define BLAKE_QUEUESIZE (256)
/*-------------------------------------------------------------------------*/
/* For destinguiting between incoming and outgoing buffer-queues */
enum blakeDirection {
  blakeIncoming,
  blakeOutgoing
};

/*
  The Blake protocol is defined through many types of packets, the first word
  defines the length.  The second word defines the code.
  The rest is packet code dependent.  Packets are delimited
  by length so that the TCP stream is broken up.  Important: Force enums to
  type blake_word16_t during network transport.  After the known packet there
  is a number which is the remaining packet length.  This could be anything
  but is almost always a message such as IM.  The extendedpacket length can
  also be calculated without this extended packet length word member and
  therefore this word may be removed later on.
*/

/* What a queue of packets looks like */
struct packet_queue {
  /* PRIVATE */
  struct {
    bool inuse; /* If false entry is not used and may be overwritten */
    int socket;
    struct blakePacket packet;
    void* exPacketData; /* Dynamic */
  } _packets[BLAKE_QUEUESIZE];
  S_DPCRTLMM_BLOCKDESCARRAY* _blockarray;
  bool _blockarrayOwnage; /* Whether to create/destroy the block array */
  /*
    'tis wise to check 'good' after operations, if it is false, abort
    things and destruct this list.
  */
  bool _good;
  /*--- PUBLIC ---*/
  /*
    Pushes new packet onto the queue if it fails the queue remains good
    but there is no space left, if the queue is not good that means a
    general out of memory error and is more critital
  */
  bool (*Push)(
    struct packet_queue *PQueue,
    const int Socket,
    const struct blakePacket *CPPacket,
    const void *ExPacket
  );
  /*
    Returns the top element ready to take, return is the length of the
    extended packet, this is normally called twice because of that
    unless the extended packet is not needed.  If there are no packets
    on the function just returns zero, I recommend checking count()
    before calling this to be sure.
  */
  size_t (*Top)(
    struct packet_queue *PQueue,
    int *PSocket,
    struct blakePacket *PPacket,
    void *ExPacket
  );
  /*
    Removes the top element
  */
  void (*Pop)(struct packet_queue *PQueue);
  /*
    Returns how many packets are in the queue
  */
  UINT (*Count)(struct packet_queue *PQueue);
  void (*Destruct)(struct packet_queue *PQueue);
};
/*-------------------------------------------------------------------------*/
static const char *const blake_copyright =
  "OverChat Blake protocol (C)Copyright 2001-2006 David Duncan Ross Palmer";
/*-------------------------------------------------------------------------*/
/*
  Shared static data (thread safe), don't touch anything until you own
  the mutex
*/

/*
  Queue buffers so we can wait until it's good to send buffers and receive
  them, packets must be sent in order and mutexes must be owned!
*/
static struct { /* buffers */
  struct {
    struct packet_queue queue;
    #ifdef BLAKE_THREADS
    dtlock_t lock;
    #endif /*BLAKE_THREADS*/
  } incoming, outgoing;
} buffers;

/*
  Can't be thread safe but nevermind, just means blake_IsInited() can't
  be relied upon
*/
static bool _inited;

/*
  All DPCRTLMM operations are thread safe
*/
static PS_DPCRTLMM_BLOCKDESCARRAY _blockarray;
/*-------------------------------------------------------------------------*/
/* Mental macros for speed and/or lazyness */

/* Common memory operations simplified, these are taken from dpcrtlmm.h
but these use _blockarray not the NULL array */
#define malloc(s) \
          dpcrtlmm_Alloc(_blockarray, (s))

#define free(p) \
          dpcrtlmm_Free(_blockarray, (p))

#define calloc(n, s) \
          dpcrtlmm_Calloc(_blockarray, (n), (s))

#define realloc(p, s) \
          dpcrtlmm_Realloc(_blockarray, (p), (s))

/* Used by packet receiver(s) reading from user buffers */
#define RECEIVE_FROM_BUFFER(element) \
          if ( (sizeof((element))+offset) > BuffSizeIn ) { \
            if ( exPacketPointer ) \
              free(exPacketPointer); \
            err = blakeBufferTooSmall; \
            ret = NULL; \
          } \
          memcpy((&(element)), BuffRawIn + offset, sizeof((element))); \
          offset += sizeof((element));
/*-------------------------------------------------------------------------*/
/***********************************
 *    Internal static functions    *
 ***********************************/

static void i_InitPacket(
  struct blakePacket *PPacket
);

/******************************************************************
 * Initialision of fixed sized structures for server side demands *
 ******************************************************************/

static void i_InitSuspend(
  struct blakeSuspend *PSuspend
);

static void i_InitResume(
  struct blakeResume *PResume
);

static void i_InitChallenge(
  struct blakeChallenge *PChallenge
);

static void i_InitGotError(
  struct blakeGotError *PGotError
);

static void i_InitListResponse(
  struct blakeListResponse *PListResponse
);

static void i_InitWhoisInfo(
  struct blakeWhoisInfo *PWhoisInfo
);

static void i_InitVersioned(
  struct blakeVersioned *PVersioned
);

static void i_InitSuper(
  struct blakeSuper *PSuper
);

static void i_InitKilled(
  struct blakeKilled *PKilled
);

static void i_InitChumSignon(
  struct blakeChumSignon *PChumSignon
);

static void i_InitChumSignoff(
  struct blakeChumSignoff *PChumSignoff
);

static void i_InitChumIdle(
  struct blakeChumIdle *PChumIdle
);

static void i_InitLoginReply(
  struct blakeLoginReply *PLoginReply
);

static void i_InitIsOnlineReply(
  struct blakeIsOnlineReply *PIsOnlineReply
);

static void i_InitRegisterReply(
  struct blakeRegisterReply *PRegisterReply
);

/********************************************************************
 * Initialisation of fixed size structures for client side requests *
 ********************************************************************/

static void i_InitLogin(
  struct blakeLogin *PLogin
);

static void i_InitSuperRequest(
  struct blakeSuperRequest *PSuperRequest
);

static void i_InitAddEnemy(
  struct blakeAddEnemy *PAddEnemy
);

static void i_InitNotEnemy(
  struct blakeNotEnemy *PNotEnemy
);

static void i_InitJoinChat(
  struct blakeJoinChat *PJoinChat
);

static void i_InitList(
  struct blakeList *PList
);

static void i_InitKillUser(
  struct blakeKillUser *PKillUser
);

static void i_InitKillChat(
  struct blakeKillChat *PKillChat
);

static void i_InitKick(
  struct blakeKick *PKick
);

static void i_InitVersion(
  struct blakeVersion *PVersion
);

static void i_InitMessage(
  struct blakeMessage *PMessage
);

static void i_InitMessage2(
  struct blakeMessage2 *PMessage2
);

static void i_InitChatMsg(
  struct blakeChatMsg *PChatMsg
);

static void i_InitWhois(
  struct blakeWhois *PWhois
);

static void i_InitRemoteShutdown(
  struct blakeRemoteShutdown *PRemoteShutdown
);

static void i_InitChumAdd(
  struct blakeChumAdd *PChumAdd
);

static void i_InitChumRemove(
  struct blakeChumRemove *PChumRemove
);

static void i_InitIdle(
  struct blakeIdle *PIdle
);

static void i_InitIsOnline(
  struct blakeIsOnline *PIsOnline
);

static void i_InitRegister(
  struct blakeRegister *PRegister
);

/*
  Initialisation of types
*/
static void i_InitVersionType(
  struct blake_ver_t *PVersionType
);

/*-------------------------------------------------------------------------*/

/*****************************************************
 * Transmitters: Will work from client or server,    *
 * these are generic                                 *
 *****************************************************/

static void i_TransmitPacket(
  int Socket,
  const struct blakePacket *PPacket,
  const void *ExPacket
);

static void i_TransmitSuspend(
  int Socket,
  const struct blakeSuspend *PSuspend
);

static void i_TransmitResume(
  int Socket,
  const struct blakeResume *PResume
);

static void i_TransmitChallenge(
  int Socket,
  const struct blakeChallenge *PChallenge
);

static void i_TransmitGotError(
  int Socket,
  const struct blakeGotError *PGotError
);

static void i_TransmitListResponse(
  int Socket,
  const struct blakeListResponse *PListResponse
);

static void i_TransmitWhoisInfo(
  int Socket,
  const struct blakeWhoisInfo *PWhoisInfo
);

static void i_TransmitVersioned(
  int Socket,
  const struct blakeVersioned *PVersioned
);

static void i_TransmitSuper(
  int Socket,
  const struct blakeSuper *PSuper
);

static void i_TransmitKilled(
  int Socket,
  const struct blakeKilled *PKilled
);

static void i_TransmitChumSignon(
  int Socket,
  const struct blakeChumSignon *PChumSignon
);

static void i_TransmitChumSignoff(
  int Socket,
  const struct blakeChumSignoff *PChumSignoff
);

static void i_TransmitChumIdle(
  int Socket,
  const struct blakeChumIdle *PChumIdle
);

static void i_TransmitLoginReply(
  int Socket,
  const struct blakeLoginReply *PLoginReply
);

static void i_TransmitIsOnlineReply(
  int Socket,
  const struct blakeIsOnlineReply *PIsOnlineReply
);

static void i_TransmitRegisterReply(
  int Socket,
  const struct blakeRegisterReply *PRegisterReply
);

/*-------------------------------------------------------------------------*/
/****************************
 * Client side transmission *
 ****************************/

static void i_TransmitLogin(
  int Socket,
  const struct blakeLogin *PLogin
);

static void i_TransmitSuperRequest(
  int Socket,
  const struct blakeSuperRequest *PSuperRequest
);

static void i_TransmitAddEnemy(
  int Socket,
  const struct blakeAddEnemy *PAddEnemy
);

static void i_TransmitNotEnemy(
  int Socket,
  const struct blakeNotEnemy *PNotEnemy
);

static void i_TransmitJoinChat(
  int Socket,
  const struct blakeJoinChat *PJoinChat
);

static void i_TransmitList(
  int Socket,
  const struct blakeList *PList
);

static void i_TransmitKillUser(
  int Socket,
  const struct blakeKillUser *PKillUser
);

static void i_TransmitKillChat(
  int Socket,
  const struct blakeKillChat *PKillChat
);

static void i_TransmitKick(
  int Socket,
  const struct blakeKick *PKick
);

static void i_TransmitVersion(
  int Socket,
  const struct blakeVersion *PVersion
);

static void i_TransmitMessage(
  int Socket,
  const struct blakeMessage *PMessage
);

static void i_TransmitMessage2(
  int Socket,
  const struct blakeMessage2 *PMessage2
);

static void i_TransmitChatMsg(
  int Socket,
  const struct blakeChatMsg *PChatMsg
);

static void i_TransmitWhois(
  int Socket,
  const struct blakeWhois *PWhois
);

static void i_TransmitRemoteShutdown(
  int Socket,
  const struct blakeRemoteShutdown *PRemoteShutdown
);

static void i_TransmitChumAdd(
  int Socket,
  const struct blakeChumAdd *PChumAdd
);

static void i_TransmitChumRemove(
  int socket,
  const struct blakeChumRemove *PChumRemove
);

static void i_TransmitIdle(
  int Socket,
  const struct blakeIdle *PIdle
);

static void i_TransmitIsOnline(
  int Socket,
  const struct blakeIsOnline *PIsOnline
);

static void i_TransmitRegister(
  int Socket,
  const struct blakeRegister *PRegister
);

/*
  Transmission of types
*/
static void i_TransmitIP(
  int Socket,
  const OVERCHAT_IP *PIP
);

static void i_TransmitVersionType(
  int Socket,
  const struct blake_ver_t *PVerType
);

/*------ Receivers: Will work from client or server, these are generic ----*/
/*
  Returns extended packet as dynamic!  Make sure to free it
*/
static BYTE* i_ReceivePacket(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakePacket *PPacketOut,
  enum blakeError *PErrorOut
);

static enum blakeError i_ReceiveSuspend(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeSuspend *SuspendOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveResume(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeResume *ResumeOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveChallenge(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChallenge *ChallengeOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveGotError(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeGotError *GotErrorOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveListResponse(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeListResponse *ListResponseOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveWhoisInfo(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeWhoisInfo *WhoisInfoOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveVersioned(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeVersioned *VersionedOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveSuper(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeSuper *SuperOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveKilled(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeKilled *KilledOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveChumSignon(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumSignon *ChumSignonOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveChumSignoff(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumSignoff *ChumSignoffOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveChumIdle(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumIdle *ChumIdleOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveLoginReply(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeLoginReply *LoginReplyOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveIsOnlineReply(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeIsOnlineReply *IsOnlineReplyOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveRegisterReply(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeRegisterReply *RegisterReplyOut,
  unsigned int *PCurrOffset
);

/*------------------ Receivers for the server side only -------------------*/

static enum blakeError i_ReceiveLogin(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeLogin *LoginOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveSuperRequest(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeSuperRequest *SuperRequestOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveAddEnemy(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeAddEnemy *AddEnemyOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveNotEnemy(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeNotEnemy *NotEnemyOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveJoinChat(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeJoinChat *JoinChatOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveList(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeList *ListOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveKillUser(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeKillUser *KillUserOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveKillChat(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeKillChat *KillChatOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveKick(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeKick *KickOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveVersion(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeVersion *VersionOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveMessage(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeMessage *MessageOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveMessage2(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeMessage2 *Message2Out,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveChatMsg(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChatMsg *ChatMsgOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveWhois(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeWhois *WhoisOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveRemoteShutdown(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeRemoteShutdown *RemoteShutdownOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveChumAdd(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumAdd *ChumAddOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveChumRemove(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumRemove *ChumRemoveOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveIdle(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeIdle *IdleOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveIsOnline(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeIsOnline *IsOnlineOut,
  unsigned int *PCurrOffset
);

static enum blakeError i_ReceiveRegister(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeRegister *RegisterOut,
  unsigned int *PCurrOffset
);

#define i_ReceiveIP(socket, ptr_ip) \
          _network_GetFormattedIP((socket), (ptr_ip))

/*--------------------------- Misc helpers --------------------------------*/
/*
  Fill a buffer with random rubbish
*/
static void i_CrapFill(
  void *CrapSpace,
  size_t CrapSpaceSize
);

static void i_InitMutexes(void);

static void i_DestroyMutexes(void);

static bool i_Queue(
  enum blakeDirection Direction,
  const int Socket,
  const struct blakePacket *CPPacket,
  const void *CPExPacket
);

/*
  Transmit all packets in the outgoing queue
*/
static void i_PurgeOutgoing(void);

/*
  Process all packets in incoming queue aand call appropriate callbacks
*/
static enum blakeError i_ProcessIncoming(void);

static inline void i_CopyPacket(
  struct blakePacket *PDest,
  const struct blakePacket *PSource
);

/*
  Copies a string into a fixed field in a packet,
  as documented in the protocol
*/
static void i_SetString(
  blake_byte_t *PStringField,
  size_t StringFieldSize,
  const char *SourceString
);

static bool i_BlakeCopyright(void);

/* ------ Packet queue ----------------------------------------------------*/
/*
  Packet queue construction/destruction;
  Constructor, PblockArray is optional
*/
static void i_packet_queue_Construct(
  struct packet_queue *PQueue,
  S_DPCRTLMM_BLOCKDESCARRAY *PBlockArray
);

static void i_packet_queue_Destruct(
  struct packet_queue *PQueue
);

/*------------------- Packet queue internal functions ---------------------*/
/*
  Link up member functions
*/
static void i_packet_queue_VTableLink(
  struct packet_queue *PQueue
);

/*
  Initialises everything
*/
static void i_packet_queue_Init(
  struct packet_queue *PQueue
);

/*
  Initialise a packet in the queue at a specific index
*/
static void i_packet_queue_InitIndex(
  struct packet_queue *PQueue,
  UINT Index
);

/*
  Cleans everything
*/
static void i_packet_queue_Reinit(
  struct packet_queue *PQueue
);

/*
  Cleans an index for re-use, the packet will be lost
*/
static void i_packet_queue_ReinitIndex(
  struct packet_queue *PQueue,
  UINT Index
);

/*
  Allocate memory
*/
static void* i_packet_queue_Malloc(
  struct packet_queue *PQueue,
  size_t ChunkSize
);

/*
  Deallocate (free) memory
*/
static void i_packet_queue_Free(
  struct packet_queue *PQueue,
  void *Ptr
);

/*
  Returns first free index or one past end to indicate none left
*/
static UINT i_packet_queue_FindFree(
  struct packet_queue *PQueue
);

/*------- Functions for linkage into the queue object ---------------------*/
static bool l_packet_queue_Push(
  struct packet_queue *PQueue,
  const int Socket,
  const struct blakePacket *CPPacket,
  const void *ExPacket
);

static size_t l_packet_queue_Top(
  struct packet_queue *PQueue,
  int *PSocket,
  struct blakePacket *PPacket,
  void *ExPacket
);

static void l_packet_queue_Pop(
  struct packet_queue *PQueue
);

static UINT l_packet_queue_Count(
  struct packet_queue *PQueue
);

#define l_packet_queue_Destruct i_packet_queue_Destruct
/*-------------------------------------------------------------------------*/
static void i_InitPacket(
  struct blakePacket *PPacket
)
{
  if ( PPacket )
    memset(PPacket, 0, sizeof(struct blakePacket));
  return;
}
/*-------------------------------------------------------------------------*/
static void i_InitSuspend(
  struct blakeSuspend *PSuspend
)
{
  if ( PSuspend ) {
    memset(PSuspend, 0, sizeof(struct blakeSuspend));
    PSuspend->dummy_time = 0;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitResume(
  struct blakeResume *PResume
)
{
  if ( PResume ) {
    memset(PResume, 0, sizeof(struct blakeResume));
    PResume->reserved = 0;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitChallenge(
  struct blakeChallenge *PChallenge
)
{
  if ( PChallenge ) {
    memset(PChallenge, 0, sizeof(struct blakeChallenge));
    PChallenge->secs = 0;
    PChallenge->_reserved = 0;
    /*PChallenge->rnum;*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitGotError(
  struct blakeGotError *PGotError
)
{
  if ( PGotError ) {
    memset(PGotError, 0, sizeof(struct blakeGotError));
    PGotError->errnum = 0;
    /*memset(PGotError, PGotError->username, sizeof(PGotError->username));*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitListResponse(
  struct blakeListResponse *PListResponse
)
{
  if ( PListResponse ) {
    memset(PListResponse, 0, sizeof(struct blakeListResponse));
    PListResponse->thread_id = 0;
    /*PListResponse->username*/   /*Superfluous*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitWhoisInfo(
  struct blakeWhoisInfo *PWhoisInfo
)
{
  if ( PWhoisInfo ) {
    memset(PWhoisInfo, 0, sizeof(struct blakeWhoisInfo));

    /*PWhoisInfo->username*/
    #ifdef OVERCHAT_IPV6
      PWhoisInfo->usingIpv6 = 1;
    #else /* ipv4 */
      PWhoisInfo->usingIpv6 = 0;
    #endif /*OVERCHAT_IPV6*/
    network_LoadIP(&PWhoisInfo->ip, NULL); /* Set to a zeroed IP */
    PWhoisInfo->clearance = CLEARANCE_ZERO;
    PWhoisInfo->warning = 0;
    PWhoisInfo->limiter = 0;
    PWhoisInfo->flags.invisible = 0;
    PWhoisInfo->flags.invinsible = 0;
    PWhoisInfo->memberSince = 0;
    PWhoisInfo->onlineSince = 0;
    PWhoisInfo->idleSince = 0;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitVersioned(
  struct blakeVersioned *PVersioned
)
{
  if ( PVersioned ) {
    memset(PVersioned, 0, sizeof(struct blakeVersioned));
    i_InitVersionType(&PVersioned->server);
    /*PVersioned->os_description*/
    i_InitVersionType(&PVersioned->os);
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitSuper(struct blakeSuper* PSuper)
{
  if ( PSuper ) {
    memset(PSuper, 0, sizeof(struct blakeSuper));
    PSuper->clearance = 0;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitKilled(
  struct blakeKilled *PKilled
)
{
  if ( PKilled ) {
    memset(PKilled, 0, sizeof(struct blakeKilled));
    /*PKilled->username*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitChumSignon(
  struct blakeChumSignon *PChumSignon
)
{
  if ( PChumSignon ) {
    memset(PChumSignon, 0, sizeof(struct blakeChumSignon));
    /*PChumSignon->username
    PChumSignon->groupname*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitChumSignoff(
  struct blakeChumSignoff *PChumSignoff
)
{
  if ( PChumSignoff ) {
    memset(PChumSignoff, 0, sizeof(struct blakeChumSignoff));
    /*PChumSignoff->username
    PChumSignoff->secs_on*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitChumIdle(
  struct blakeChumIdle *PChumIdle
)
{
  if ( PChumIdle ) {
    memset(PChumIdle, 0, sizeof(struct blakeChumIdle));
    /*PChumIdle->username
    PChumIdle->back_flag
    PChumIdle->idle_secs*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitLoginReply(
  struct blakeLoginReply *PLoginReply
)
{
  if ( PLoginReply ) {
    memset(PLoginReply, 0, sizeof(struct blakeLoginReply));
    /*PLoginReply->failcode = blrSuccess;*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitIsOnlineReply(
  struct blakeIsOnlineReply *PIsOnlineReply
)
{
  if ( PIsOnlineReply )
    memset(PIsOnlineReply, 0, sizeof(struct blakeIsOnlineReply));
}
/*-------------------------------------------------------------------------*/
static void i_InitRegisterReply(
  struct blakeRegisterReply *PRegisterReply
)
{
  if ( PRegisterReply )
    memset(PRegisterReply, 0, sizeof(struct blakeRegisterReply));
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
static void i_InitLogin(
  struct blakeLogin *PLogin
)
{
  if ( PLogin ) {
    memset(PLogin, 0, sizeof(struct blakeLogin));
    /*PLogin->username
    PLogin->password*/
    PLogin->language = OVERCHAT_DEFLANG;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitSuperRequest(
  struct blakeSuperRequest *PSuperRequest
)
{
  if ( PSuperRequest ) {
    memset(PSuperRequest, 0, sizeof(struct blakeSuperRequest));
    PSuperRequest->reserved = 0;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitAddEnemy(
  struct blakeAddEnemy *PAddEnemy
)
{
  if ( PAddEnemy ) {
    memset(PAddEnemy, 0, sizeof(struct blakeAddEnemy));
    /*PAddEnemy->username*/
    PAddEnemy->blocktime = 0; /* Forever as default */
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitNotEnemy(
  struct blakeNotEnemy *PNotEnemy
)
{
  if ( PNotEnemy ) {
    memset(PNotEnemy, 0, sizeof(struct blakeNotEnemy));
    /*PNotEnemy->username*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitJoinChat(
  struct blakeJoinChat *PJoinChat
)
{
  if ( PJoinChat ) {
    memset(PJoinChat, 0, sizeof(struct blakeJoinChat));
    /*PJoinChat->chatname*/
    PJoinChat->dummy = BLAKE_JOINCHAT_DUMMY;
    i_CrapFill(&PJoinChat->random, sizeof(PJoinChat->random));
    PJoinChat->reserved = 0;
    PJoinChat->exchange = BLAKE_DEFAULT_EXCHANGE;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitList(
  struct blakeList *PList
)
{
  if ( PList ) {
    memset(PList, 0, sizeof(struct blakeList));
    PList->type = 0;
    PList->meaningless = 0x7720; /* Blah */
    PList->exchange = 0; /* Can't assume on user's behalf */
    /*PList->chatname*/
    PList->thread_id = 0;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitKillUser(
  struct blakeKillUser *PKillUser
)
{
  if ( PKillUser ) {
    memset(PKillUser, 0, sizeof(struct blakeKillUser));
    /*PKillUser->username*/
    network_LoadIP(&PKillUser->ip, NULL); /* 0.0.0.0 or ipv6 equivillent */
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitKillChat(
  struct blakeKillChat *PKillChat
)
{
  if ( PKillChat ) {
    memset(PKillChat, 0, sizeof(struct blakeKillChat));
    /*PKillChat->chatname*/
    /*
      Might want to adjust this after the call, to avoid disrupting other
      other chats with the same name on a different exchange
    */
    PKillChat->exchange = allRooms;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitKick(
  struct blakeKick *PKick
)
{
  if ( PKick ) {
    memset(PKick, 0, sizeof(struct blakeKick));
    /*PKick->username
    PKick->chatname*/
    PKick->exchange = allRooms; /* Illegal, user changes this or is killed */
    PKick->dummy = BLAKE_KICK_DUMMY1; /* could use DUMMY2 too */
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitVersion(struct blakeVersion* PVersion)
{
  if ( PVersion ) {
    memset(PVersion, 0, sizeof(struct blakeVersion));
    PVersion->reserved = 0;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitMessage(
  struct blakeMessage *PMessage
)
{
  if ( PMessage ) {
    memset(PMessage, 0, sizeof(struct blakeMessage));
    /*PMessage->username*/
    /*PMessage->cipher*/
    /*PMessage->flags1*/
    /*PMessage->reserved*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitMessage2(struct blakeMessage2* PMessage2)
{
  if ( PMessage2 ) {
    memset(PMessage2, 0, sizeof(struct blakeMessage2));
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitChatMsg(
  struct blakeChatMsg *PChatMsg
)
{
  if ( PChatMsg ) {
    memset(PChatMsg, 0, sizeof(struct blakeChatMsg));
    /*PChatMsg->chatname*/
    PChatMsg->spoof = BLAKE_CHATMSG_SPOOF;
    /*
      The exchange must (later) be set correctly, leaving it as the follow-
      int default of allRooms will get you killed!
    */
    PChatMsg->exchange = allRooms;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitWhois(struct blakeWhois* PWhois)
{
  if ( PWhois ) {
    memset(PWhois, 0, sizeof(struct blakeWhois));
    /*PWhois->username*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitRemoteShutdown(
  struct blakeRemoteShutdown *PRemoteShutdown
)
{
  if ( PRemoteShutdown ) {
    memset(PRemoteShutdown, 0, sizeof(struct blakeRemoteShutdown));
    /*
      Seeing as the name of the function is restart, restart and don't
      kill, by default
    */
    PRemoteShutdown->dorestart = 1;
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitChumAdd(
  struct blakeChumAdd *PChumAdd
)
{
  if ( PChumAdd ) {
    memset(PChumAdd, 0, sizeof(struct blakeChumAdd));
    /*PChumAdd->username
    PChumAdd->groupname*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitChumRemove(
  struct blakeChumRemove *PChumRemove
)
{
  if ( PChumRemove ) {
    memset(PChumRemove, 0, sizeof(struct blakeChumRemove));
    /*PChumRemove->username*/
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitIdle(
  struct blakeIdle *PIdle
)
{
  if ( PIdle ) {
    memset(PIdle, 0, sizeof(struct blakeIdle));
    PIdle->idle_flag = 1; /* Going idle by default */
  }
}
/*-------------------------------------------------------------------------*/
static void i_InitIsOnline(
  struct blakeIsOnline *PIsOnline
)
{
  if ( PIsOnline )
    memset(PIsOnline, 0, sizeof(struct blakeIsOnline));
}
/*-------------------------------------------------------------------------*/
static void i_InitRegister(
  struct blakeRegister *PRegister
)
{
  if ( PRegister )
    memset(PRegister, 0, sizeof(struct blakeRegister));
}
/*-------------------------------------------------------------------------*/
static void i_InitVersionType(
  struct blake_ver_t *PVersionType
)
{
  if ( PVersionType ) {
    memset(PVersionType, 0, sizeof(struct blake_ver_t));
    PVersionType->sz = sizeof(struct blake_ver_t);
    PVersionType->major = 0;
    PVersionType->minor = 0;
    PVersionType->patch = 0;
    PVersionType->exFlags = 0;
  }
}
/*-------------------------------------------------------------------------*/
static void i_TransmitPacket(
  int Socket,
  const struct blakePacket *PPacket,
  const void *ExPacket
)
{
  if ( PPacket ) {
    blake_word16_t enumSwapper;

    enumSwapper = (blake_word16_t)network_HostToNetShort(PPacket->len);
    assert(enumSwapper); /* Never transmit zero length packet */
    blake_int_NetworkOutput(Socket, &enumSwapper, sizeof(enumSwapper));

    blake_int_NetworkOutput(
      Socket,
      &PPacket->server,
      sizeof(PPacket->server)
    );

    if ( PPacket->server ) /* Using server codes? */
      enumSwapper = (blake_word16_t)PPacket->code.serverCode; /* Grab */
    else /* Using client codes */
      enumSwapper = (blake_word16_t)PPacket->code.clientCode;

    /* Swap client/server code to network order */
    enumSwapper = (blake_word16_t)network_HostToNetShort(
      (unsigned short int)enumSwapper
    );

    /* Send bytes */
    blake_int_NetworkOutput(
      Socket,
      &enumSwapper,
      sizeof(enumSwapper)
    );

    if ( PPacket->server ) {
      switch ( PPacket->code.serverCode ) {
        case bscsuspend : {
          i_TransmitSuspend(Socket, &PPacket->info._suspend);
          break;
        }
        case bscresume : {
          i_TransmitResume(Socket, &PPacket->info._resume);
          break;
        }
        case bscchallenge : {
          i_TransmitChallenge(Socket, &PPacket->info._challenge);
          break;
        }
        case bscgoterror : {
          i_TransmitGotError(Socket, &PPacket->info._goterror);
          break;
        }
        case bsclist_response : {
          i_TransmitListResponse(Socket, &PPacket->info._listresponse);
          break;
        }
        case bscwhois_info : {
          i_TransmitWhoisInfo(Socket, &PPacket->info._whoisinfo);
          break;
        }
        case bscversioned : {
          i_TransmitVersioned(Socket, &PPacket->info._versioned);
          break;
        }
        case bscsuper : {
          i_TransmitSuper(Socket, &PPacket->info._super);
          break;
        }
        case bsckilled : {
          i_TransmitKilled(Socket, &PPacket->info._killed);
          break;
        }
        case bscchum_signon : {
          i_TransmitChumSignon(Socket, &PPacket->info._chumsignon);
          break;
        }
        case bscchum_signoff : {
          i_TransmitChumSignoff(Socket, &PPacket->info._chumsignoff);
          break;
        }
        case bscchum_idle : {
          i_TransmitChumIdle(Socket, &PPacket->info._chumidle);
          break;
        }
        case bscloginreply : {
          i_TransmitLoginReply(Socket, &PPacket->info._loginreply);
          break;
        }
        case bscisonlinereply : {
          i_TransmitIsOnlineReply(Socket, &PPacket->info._isonlinereply);
          break;
        }
        case bscregisterreply : {
          i_TransmitRegisterReply(Socket, &PPacket->info._registerreply);
          break;
        }
        default : {
          char msgbuff[MSG_BUFF_SIZE];
          sprintf(
            msgbuff,
            "Blake - Unknown server code %u, cannot transmit\n",
            PPacket->code.serverCode
          );
          i_LogMsg(msgbuff);
        }
      }
    }
    else { /* Must be a client code then */
      switch ( PPacket->code.clientCode ) {
        case bcclogin : {
          i_TransmitLogin(Socket, &PPacket->info._login);
          break;
        }
        case bccsuper_request : {
          i_TransmitSuperRequest(Socket, &PPacket->info._superrequest);
          break;
        }
        case bccadd_enemy : {
          i_TransmitAddEnemy(Socket, &PPacket->info._addenemy);
          break;
        }
        case bccnot_enemy : {
          i_TransmitNotEnemy(Socket, &PPacket->info._notenemy);
          break;
        }
        case bccjoin_chat : {
          i_TransmitJoinChat(Socket, &PPacket->info._joinchat);
          break;
        }
        case bcclist : {
          i_TransmitList(Socket, &PPacket->info._list);
          break;
        }
        case bcckilluser : {
          i_TransmitKillUser(Socket, &PPacket->info._killuser);
          break;
        }
        case bcckillchat : {
          i_TransmitKillChat(Socket, &PPacket->info._killchat);
          break;
        }
        case bcckick : {
          i_TransmitKick(Socket, &PPacket->info._kick);
          break;
        }
        case bccversion : {
          i_TransmitVersion(Socket, &PPacket->info._version);
          break;
        }
        case bccmessage : {
          i_TransmitMessage(Socket, &PPacket->info._message);
          break;
        }
        case bccmessage2 : {
          i_TransmitMessage2(Socket, &PPacket->info._message2);
          break;
        }
        case bccchatmsg : {
          i_TransmitChatMsg(Socket, &PPacket->info._chatmsg);
          break;
        }
        case bccwhois : {
          i_TransmitWhois(Socket, &PPacket->info._whois);
          break;
        }
        case bccremoteshutdown : {
          i_TransmitRemoteShutdown(Socket, &PPacket->info._remoteshutdown);
          break;
        }
        case bccchum_add : {
          i_TransmitChumAdd(Socket, &PPacket->info._chumadd);
          break;
        }
        case bccchum_remove : {
          i_TransmitChumRemove(Socket, &PPacket->info._chumremove);
          break;
        }
        case bccidle : {
          i_TransmitIdle(Socket, &PPacket->info._idle);
          break;
        }
        case bccisonline : {
          i_TransmitIsOnline(Socket, &PPacket->info._isonline);
          break;
        }
        case bccregister : {
          i_TransmitRegister(Socket, &PPacket->info._register);
          break;
        }
        default : {
          char msgbuff[MSG_BUFF_SIZE];
          sprintf(
            msgbuff,
            "Blake - Unknown client code %u, cannot transmit\n",
            PPacket->code.clientCode
          );
          i_LogMsg(msgbuff);
        }
      }
    }
    if ( PPacket->extendedPacketLength && ExPacket ) {
      /* Transmit the extended packet, reuse enumSwapper */
      enumSwapper = (blake_word16_t)network_HostToNetShort(
        (unsigned short int)PPacket->extendedPacketLength
      );
      blake_int_NetworkOutput(Socket, &enumSwapper, sizeof(enumSwapper));
      /* Write data of extended packet */
      blake_int_NetworkOutput(
        Socket,
        ExPacket,
        PPacket->extendedPacketLength
      );
    }
    else { /* No extended packet or details broken */
      enumSwapper = 0;
      blake_int_NetworkOutput(Socket, &enumSwapper, sizeof(enumSwapper));
    }
    /* That's it!   Entire packet transmitted */
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitSuspend(
  int Socket,
  const struct blakeSuspend *PSuspend
)
{
  if ( PSuspend ) {
    struct blakeSuspend swappedSuspend;

    /* Swap binary elements */
    swappedSuspend.dummy_time =
      (blake_word32_t)network_HostToNetLong(
        (unsigned long int)PSuspend->dummy_time
      );

    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      &swappedSuspend.dummy_time,
      sizeof(swappedSuspend.dummy_time)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitResume(
  int Socket,
  const struct blakeResume *PResume
)
{
  if ( PResume ) {
    struct blakeResume swappedResume;

    /* Swap binary elements */
    swappedResume.reserved =
      (blake_word32_t)network_HostToNetLong(
        (unsigned long int)PResume->reserved
      );

    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      &swappedResume.reserved,
      sizeof(swappedResume.reserved)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitChallenge(
  int Socket,
  const struct blakeChallenge *PChallenge
)
{
  if ( PChallenge ) {
    struct blakeChallenge swappedChallenge;

    /* Swap binary elements */
    swappedChallenge.secs =
      (blake_word16_t)network_HostToNetShort(
        (unsigned short int)PChallenge->secs
      );
    swappedChallenge._reserved =
      (blake_word32_t)network_HostToNetLong(
        (unsigned long int)PChallenge->_reserved
      );
    /* No swap neccersary for the rnum */

    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      &swappedChallenge.secs,
      sizeof(swappedChallenge.secs)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedChallenge._reserved,
      sizeof(swappedChallenge._reserved)
    );
    blake_int_NetworkOutput(
      Socket,
      PChallenge->rnum,
      sizeof(PChallenge->rnum)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitGotError(
  int Socket,
  const struct blakeGotError *PGotError
)
{
  if ( PGotError ) {
    struct blakeGotError swappedGotError;

    swappedGotError = *PGotError; /* Copy */

    /* Swap binary elements */
    swappedGotError.errnum =
      (blake_word16_t)network_HostToNetShort(
        (unsigned short int)swappedGotError.errnum
      );

    /* Transmit all elements */
    blake_int_NetworkOutput(
      Socket,
      &swappedGotError.errnum,
      sizeof(swappedGotError.errnum)
    );
    blake_int_NetworkOutput(
      Socket,
      swappedGotError.username,
      sizeof(swappedGotError.username)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitListResponse(
  int Socket,
  const struct blakeListResponse *PListResponse
)
{
  if ( PListResponse ) {
    struct blakeListResponse swappedListResponse;

    swappedListResponse = *PListResponse; /* Copy */

    /* Swap binary elements */
    swappedListResponse.thread_id =
      (blake_word32_t)network_HostToNetLong(
        (unsigned long int)swappedListResponse.thread_id
      );

    /* Transmit all */
    blake_int_NetworkOutput(
      Socket,
      &swappedListResponse.thread_id,
      sizeof(swappedListResponse.thread_id)
    );
    blake_int_NetworkOutput(
      Socket,
      swappedListResponse.username,
      sizeof(swappedListResponse.username)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitWhoisInfo(
  int Socket,
  const struct blakeWhoisInfo *PWhoisInfo
)
{
  if ( PWhoisInfo ) {
    struct blakeWhoisInfo swappedWhoisInfo;

    swappedWhoisInfo = *PWhoisInfo; /* Copy */

    /* Swap binary elements but no touch IP */
    swappedWhoisInfo.memberSince =
      (blake_word32_t)network_HostToNetLong(
        (unsigned long int)swappedWhoisInfo.memberSince
      );
    swappedWhoisInfo.onlineSince =
      (blake_word32_t)network_HostToNetLong(
        (unsigned long int)swappedWhoisInfo.onlineSince
      );
    swappedWhoisInfo.idleSince =
      (blake_word32_t)network_HostToNetLong(
        (unsigned long int)swappedWhoisInfo.idleSince
      );

    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      swappedWhoisInfo.username,
      sizeof(swappedWhoisInfo.username)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.usingIpv6,
      sizeof(swappedWhoisInfo.usingIpv6)
    );
    i_TransmitIP(Socket, &swappedWhoisInfo.ip); /* Do this in bits */
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.clearance,
      sizeof(swappedWhoisInfo.clearance)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.warning,
      sizeof(swappedWhoisInfo.warning)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.limiter,
      sizeof(swappedWhoisInfo.limiter)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.flags.invisible,
      sizeof(swappedWhoisInfo.flags.invisible)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.flags.invinsible,
      sizeof(swappedWhoisInfo.flags.invinsible)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.memberSince,
      sizeof(swappedWhoisInfo.memberSince)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.onlineSince,
      sizeof(swappedWhoisInfo.onlineSince)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedWhoisInfo.idleSince,
      sizeof(swappedWhoisInfo.idleSince)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitVersioned(
  int Socket,
  const struct blakeVersioned *PVersioned
)
{
  if ( PVersioned ) {
    struct blakeVersioned swappedVersioned;

    swappedVersioned = *PVersioned; /* Copy */

    /* No binary members to swap (not handled here anyway) */

    /* Transmit */
    i_TransmitVersionType(Socket, &swappedVersioned.server);
    blake_int_NetworkOutput(
      Socket,
      swappedVersioned.os_description,
      sizeof(swappedVersioned.os_description)
    );
    i_TransmitVersionType(Socket, &swappedVersioned.os);
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitSuper(
  int Socket,
  const struct blakeSuper *PSuper
)
{
  if ( PSuper ) {
    struct blakeSuper swappedSuper;

    swappedSuper = *PSuper; /* Copy */

    /* No binary members to swap */

    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      &swappedSuper.clearance,
      sizeof(swappedSuper.clearance)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitKilled(
  int Socket,
  const struct blakeKilled *PKilled
)
{
  if ( PKilled ) {
    /* Nothing to swap */
    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      PKilled->username,
      sizeof(PKilled->username)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitChumSignon(
  int Socket,
  const struct blakeChumSignon *PChumSignon
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitChumSignoff(
  int Socket,
  const struct blakeChumSignoff *PChumSignoff
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitChumIdle(
  int Socket,
  const struct blakeChumIdle *PChumIdle
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitLoginReply(
  int Socket,
  const struct blakeLoginReply *PLoginReply
)
{
  if ( PLoginReply ) {
    /* Nothing to swap */
    blake_int_NetworkOutput(
      Socket,
      &PLoginReply->failcode,
      sizeof(PLoginReply->failcode)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitIsOnlineReply(
  int Socket,
  const struct blakeIsOnlineReply *PIsOnlineReply
)
{
  if ( PIsOnlineReply ) {
    /* Nothing to swap */
    blake_int_NetworkOutput(
      Socket,
      PIsOnlineReply->username,
      sizeof(PIsOnlineReply->username)
    );
    blake_int_NetworkOutput(
      Socket,
      &PIsOnlineReply->boolean,
      sizeof(PIsOnlineReply->boolean)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitRegisterReply(
  int Socket,
  const struct blakeRegisterReply *PRegisterReply
)
{
  if ( PRegisterReply ) {
    /* Nothing to swap */
    blake_int_NetworkOutput(
      Socket,
      &PRegisterReply->failcode,
      sizeof(PRegisterReply->failcode)
    );
    blake_int_NetworkOutput(
      Socket,
      PRegisterReply->password,
      sizeof(PRegisterReply->password)
    );
    blake_int_NetworkOutput(
      Socket,
      PRegisterReply->reserved,
      sizeof(PRegisterReply->reserved)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
static void i_TransmitLogin(
  int Socket,
  const struct blakeLogin *PLogin
)
{
  if ( PLogin ) {
    /* Nothing to swap */
    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      PLogin->_username,
      sizeof(PLogin->_username)
    );
    blake_int_NetworkOutput(
      Socket,
      &PLogin->hashtype,
      sizeof(PLogin->hashtype)
    );
    blake_int_NetworkOutput(
      Socket,
      PLogin->_passworddigest,
      sizeof(PLogin->_passworddigest)
    );
    blake_int_NetworkOutput(
      Socket,
      PLogin->reserved,
      sizeof(PLogin->reserved)
    );
    blake_int_NetworkOutput(
      Socket,
      &PLogin->clientid,
      sizeof(PLogin->clientid)
    );
    blake_int_NetworkOutput(
      Socket,
      &PLogin->clientver,
      sizeof(PLogin->clientver)
    );
    blake_int_NetworkOutput(
      Socket,
      &PLogin->language,
      sizeof(PLogin->language)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitSuperRequest(
  int Socket,
  const struct blakeSuperRequest *PSuperRequest
)
{
  if ( PSuperRequest ) {
    /* Nothing to swap */
    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      &PSuperRequest->reserved,
      sizeof(PSuperRequest->reserved)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitAddEnemy(
  int Socket,
  const struct blakeAddEnemy *PAddEnemy
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitNotEnemy(
  int Socket,
  const struct blakeNotEnemy *PNotEnemy
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitJoinChat(
  int Socket,
  const struct blakeJoinChat *PJoinChat
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitList(
  int Socket,
  const struct blakeList *PList
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitKillUser(
  int Socket,
  const struct blakeKillUser *PKillUser
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitKillChat(
  int Socket,
  const struct blakeKillChat *PKillChat
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitKick(
  int Socket,
  const struct blakeKick *PKick
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitVersion(
  int Socket,
  const struct blakeVersion *PVersion
)
{
  /* Only member is 8-bits, so no need to swap */
  if ( PVersion ) {
    blake_int_NetworkOutput(
      Socket,
      &PVersion->reserved,
      sizeof(PVersion->reserved)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitMessage(
  int Socket,
  const struct blakeMessage *PMessage
)
{
  if ( PMessage ) {
    /* Nothing to swap */
    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      PMessage->username,
      sizeof(PMessage->username)
    );
    blake_int_NetworkOutput(
      Socket,
      &PMessage->cipher,
      sizeof(PMessage->cipher)
    );
    blake_int_NetworkOutput(
      Socket,
      &PMessage->flags1,
      sizeof(PMessage->flags1)
    );
    blake_int_NetworkOutput(
      Socket,
      PMessage->reserved,
      sizeof(PMessage->reserved)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitMessage2(
  int Socket,
  const struct blakeMessage2 *PMessage2
)
{
  if ( PMessage2 ) {
    /* Nothing to swap */
    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      &PMessage2->reserved0,
      sizeof(PMessage2->reserved0)
    );
    blake_int_NetworkOutput(
      Socket,
      &PMessage2->reserved1,
      sizeof(PMessage2->reserved1)
    );
    blake_int_NetworkOutput(
      Socket,
      &PMessage2->reserved2,
      sizeof(PMessage2->reserved2)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitChatMsg(
  int Socket,
  const struct blakeChatMsg *PChatMsg
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitWhois(
  int Socket,
  const struct blakeWhois *PWhois
)
{
  if ( PWhois ) {
    blake_int_NetworkOutput(
      Socket,
      &PWhois->username,
      sizeof(PWhois->username)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitRemoteShutdown(
  int Socket,
  const struct blakeRemoteShutdown *PRemoteShutdown
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitChumAdd(
  int Socket,
  const struct blakeChumAdd *PChumAdd
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitChumRemove(
  int socket,
  const struct blakeChumRemove *PChumRemove
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitIdle(
  int Socket,
  const struct blakeIdle *PIdle
)
{
}
/*-------------------------------------------------------------------------*/
static void i_TransmitIsOnline(
  int Socket,
  const struct blakeIsOnline *PIsOnline
)
{
  if ( PIsOnline ) {
    blake_int_NetworkOutput(
      Socket,
      PIsOnline->username,
      sizeof(PIsOnline->username)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitRegister(
  int Socket,
  const struct blakeRegister *PRegister
)
{
  if ( PRegister ) {
    /* Nothing to swap */
    blake_int_NetworkOutput(
      Socket,
      PRegister->username,
      sizeof(PRegister->username)
    );
    blake_int_NetworkOutput(
      Socket,
      PRegister->email,
      sizeof(PRegister->email)
    );
    blake_int_NetworkOutput(
      Socket,
      PRegister->reserved,
      sizeof(PRegister->reserved)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitIP(
  int Socket,
  const OVERCHAT_IP *PIP
)
{
  if ( PIP ) {
    unsigned int i;

    for ( i = 0U; i < sizeof(PIP->_parts)/sizeof(PIP->_parts[0]); i++ )
      blake_int_NetworkOutput(
        Socket,
        &PIP->_parts[i],
        sizeof(PIP->_parts[i])
      );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_TransmitVersionType(
  int Socket,
  const struct blake_ver_t *PVerType
)
{
  if ( PVerType ) {
    struct blake_ver_t swappedVersionType;

    swappedVersionType = *PVerType; /* Copy */

    /* Swap (> 8 bit) binary elements */
    swappedVersionType.sz =
      (blake_word16_t)network_HostToNetShort(
        (unsigned short int)swappedVersionType.sz
      );
    swappedVersionType.exFlags =
      (blake_word16_t)network_HostToNetShort(
        (unsigned short int)swappedVersionType.exFlags
      );


    /* Transmit */
    blake_int_NetworkOutput(
      Socket,
      &swappedVersionType.sz,
      sizeof(swappedVersionType.sz)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedVersionType.major,
      sizeof(swappedVersionType.major)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedVersionType.minor,
      sizeof(swappedVersionType.minor)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedVersionType.patch,
      sizeof(swappedVersionType.patch)
    );
    blake_int_NetworkOutput(
      Socket,
      &swappedVersionType.exFlags,
      sizeof(swappedVersionType.exFlags)
    );
  }
  return;
}
/*-------------------------------------------------------------------------*/
static BYTE* i_ReceivePacket(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakePacket *PPacketOut,
  enum blakeError *PErrorOut
)
{
  /* Locals */
  enum blakeError err = blakeSuccess;
  BYTE* ret = (BYTE*)~0;
  BYTE* exPacketPointer = NULL;

  if ( BuffRawIn && BuffSizeIn && PPacketOut ) {
    blake_word16_t enumSwapper;
    unsigned int offset = 0U;
    /*
      The lead string would have already been received before we were
      called, which is how we know how to read a packet in the first place.
      The caller should have initialised their packet holder so it's set
      anyway.  Receive the remaining elements of the packet and swap
      bytes to host order as we go.
    */

    /* Read length of packet */
    RECEIVE_FROM_BUFFER(PPacketOut->len)
    if ( !ret ) goto ret;

    PPacketOut->len =
      (blake_word16_t)network_NetToHostShort(
        (unsigned short int)PPacketOut->len
      );

    /* Read server/client indicator */
    RECEIVE_FROM_BUFFER(PPacketOut->server)
    if ( !ret ) goto ret;

    /* Read server or client code */
    RECEIVE_FROM_BUFFER(enumSwapper)
    if ( !ret ) goto ret;

    enumSwapper = (blake_word16_t)network_NetToHostShort(
      (unsigned short int)enumSwapper
    );
    if ( PPacketOut->server ) /* Code will be a server code */
      PPacketOut->code.serverCode = (enum blakeServerCodes)enumSwapper;
    else /* Code will be a client code */
      PPacketOut->code.clientCode = (enum blakeClientCodes)enumSwapper;

    if ( PPacketOut->server ) {
      switch ( PPacketOut->code.serverCode ) {
        case bscsuspend : {
          err = i_ReceiveSuspend(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._suspend,
            &offset
          );
          break;
        }
        case bscresume : {
          err = i_ReceiveResume(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._resume,
            &offset
          );
          break;
        }
        case bscchallenge : {
          err = i_ReceiveChallenge(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._challenge,
            &offset
          );
          break;
        }
        case bscgoterror : {
          err = i_ReceiveGotError(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._goterror,
            &offset
          );
          break;
        }
        case bsclist_response : {
          err = i_ReceiveListResponse(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._listresponse,
            &offset
          );
          break;
        }
        case bscwhois_info : {
          err = i_ReceiveWhoisInfo(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._whoisinfo,
            &offset
          );
          break;
        }
        case bscversioned : {
          err = i_ReceiveVersioned(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._versioned,
            &offset
          );
          break;
        }
        case bscsuper : {
          err = i_ReceiveSuper(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._super,
            &offset
          );
          break;
        }
        case bsckilled : {
          err = i_ReceiveKilled(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._killed,
            &offset
          );
          break;
        }
        case bscchum_signon : {
          err = i_ReceiveChumSignon(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._chumsignon,
            &offset
          );
          break;
        }
        case bscchum_signoff : {
          err = i_ReceiveChumSignoff(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._chumsignoff,
            &offset
          );
          break;
        }
        case bscchum_idle : {
          err = i_ReceiveChumIdle(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._chumidle,
            &offset
          );
          break;
        }
        case bscloginreply : {
          err = i_ReceiveLoginReply(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._loginreply,
            &offset
          );
          break;
        }
        case bscisonlinereply : {
          err = i_ReceiveIsOnlineReply(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._isonlinereply,
            &offset
          );
          break;
        }
        case bscregisterreply : {
          err = i_ReceiveRegisterReply(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._registerreply,
            &offset
          );
          break;
        }
        default : {
          char msgbuff[MSG_BUFF_SIZE];
          sprintf(
            msgbuff,
            "Blake - Received unknown server code %u\n",
            PPacketOut->code.serverCode
          );
          i_LogMsg(msgbuff);
        }
      }
    }
    else { /* Must be a client code then */
      switch ( PPacketOut->code.clientCode ) {
        case bcclogin : {
          err = i_ReceiveLogin(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._login,
            &offset
          );
          break;
        }
        case bccsuper_request : {
          err = i_ReceiveSuperRequest(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._superrequest,
            &offset
          );
          break;
        }
        case bccadd_enemy : {
          err = i_ReceiveAddEnemy(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._addenemy,
            &offset
          );
          break;
        }
        case bccnot_enemy : {
          err = i_ReceiveNotEnemy(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._notenemy,
            &offset
          );
          break;
        }
        case bccjoin_chat : {
          err = i_ReceiveJoinChat(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._joinchat,
            &offset
          );
          break;
        }
        case bcclist : {
          err = i_ReceiveList(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._list,
            &offset
          );
          break;
        }
        case bcckilluser : {
          err = i_ReceiveKillUser(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._killuser,
            &offset
          );
          break;
        }
        case bcckillchat : {
          err = i_ReceiveKillChat(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._killchat,
            &offset
          );
          break;
        }
        case bcckick : {
          err = i_ReceiveKick(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._kick,
            &offset
          );
          break;
        }
        case bccversion : {
          err = i_ReceiveVersion(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._version,
            &offset
          );
          break;
        }
        case bccmessage : {
          err = i_ReceiveMessage(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._message,
            &offset
          );
          break;
        }
        case bccmessage2 : {
          err = i_ReceiveMessage2(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._message2,
            &offset
          );
          break;
        }
        case bccchatmsg : {
          err = i_ReceiveChatMsg(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._chatmsg,
            &offset
          );
          break;
        }
        case bccwhois : {
          err = i_ReceiveWhois(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._whois,
            &offset
          );
          break;
        }
        case bccremoteshutdown : {
          err = i_ReceiveRemoteShutdown(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._remoteshutdown,
            &offset
          );
          break;
        }
        case bccchum_add : {
          err = i_ReceiveChumAdd(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._chumadd,
            &offset
          );
          break;
        }
        case bccchum_remove : {
          err = i_ReceiveChumRemove(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._chumremove,
            &offset
          );
          break;
        }
        case bccidle : {
          err = i_ReceiveIdle(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._idle,
            &offset
          );
          break;
        }
        case bccisonline : {
          err = i_ReceiveIsOnline(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._isonline,
            &offset
          );
          break;
        }
        case bccregister : {
          err = i_ReceiveRegister(
            BuffRawIn + offset,
            BuffSizeIn,
            &PPacketOut->info._register,
            &offset
          );
          break;
        }
        default : {
          char msgbuff[MSG_BUFF_SIZE];
          sprintf(
            msgbuff,
            "Blake - Received unknown client code %u\n",
            PPacketOut->code.clientCode
          );
          i_LogMsg(msgbuff);
        }
      }
    }

    /* Allocate the extended packet on behalf of the caller */
    RECEIVE_FROM_BUFFER(PPacketOut->extendedPacketLength)
    if ( !ret ) goto ret;

    if ( PPacketOut->extendedPacketLength ) {
      PPacketOut->extendedPacketLength =
        (blake_word16_t)network_NetToHostShort(
          (unsigned short int)PPacketOut->extendedPacketLength
        );

      exPacketPointer = malloc(PPacketOut->extendedPacketLength);
      if ( !exPacketPointer )
        err = blakeNotEnoughMem;
    }

    /* Read in the extended packet */
    if ( exPacketPointer ) {
      if ( (PPacketOut->extendedPacketLength + offset) > BuffSizeIn ) {
        if ( exPacketPointer )
          free(exPacketPointer);
        err = blakeBufferTooSmall;
      }
      memcpy(
        exPacketPointer,
        BuffRawIn + offset,
        PPacketOut->extendedPacketLength
      );
      /*offset += sizeof((element));  <- Redundant */
    }
    /* That's it! Entire packet Received */
  }
  else
    err = blakeInvalidParam;

ret:
  if ( PErrorOut ) /* Caller allowed for an error return */
    *PErrorOut = err;

  return exPacketPointer;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveSuspend(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeSuspend *SuspendOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveResume(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeResume *ResumeOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveChallenge(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChallenge *ChallengeOut,
  unsigned int *PCurrOffset
)
{
  enum blakeError err = blakeSuccess;
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  void* ret = (void*)~0; /* Dummy to keep macro happy */

  RECEIVE_FROM_BUFFER(ChallengeOut->secs);
  if ( !ret ) return err;
  ChallengeOut->secs =
    (blake_word16_t)network_NetToHostShort(ChallengeOut->secs);

  RECEIVE_FROM_BUFFER(ChallengeOut->_reserved);
  if ( !ret ) return err;
  ChallengeOut->_reserved =
    (blake_word32_t)network_NetToHostLong(ChallengeOut->_reserved);

  RECEIVE_FROM_BUFFER(ChallengeOut->rnum);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveGotError(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeGotError *GotErrorOut,
  unsigned int *PCurrOffset
)
{
  enum blakeError err = blakeSuccess;
  unsigned int offset = 0U;
  char *exPacketPointer = NULL;
  void *ret = (void*)~0; /* Dummy to keep macro happy */

  RECEIVE_FROM_BUFFER(GotErrorOut->errnum);
  if ( !ret ) return err;
  GotErrorOut->errnum =
    (blake_word16_t)network_NetToHostShort(GotErrorOut->errnum);

  RECEIVE_FROM_BUFFER(GotErrorOut->username);
  if ( !ret ) return err;

  /* Update offset */
  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveListResponse(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeListResponse *ListResponseOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveWhoisInfo(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeWhoisInfo *WhoisInfoOut,
  unsigned int *PCurrOffset
)
{
  unsigned int i;
  enum blakeError err = blakeSuccess;
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  void* ret = (void*)~0; /* Dummy to keep macro happy */

  RECEIVE_FROM_BUFFER(WhoisInfoOut->username);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(WhoisInfoOut->usingIpv6);
  if ( !ret ) return err;

  for (
    i = 0U;
    i < sizeof(WhoisInfoOut->ip._parts)/sizeof(WhoisInfoOut->ip._parts[0]);
    i++
  ) {
    RECEIVE_FROM_BUFFER(WhoisInfoOut->ip._parts[i]);
    if ( !ret ) return err;
  }

  RECEIVE_FROM_BUFFER(WhoisInfoOut->clearance);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(WhoisInfoOut->warning);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(WhoisInfoOut->limiter);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(WhoisInfoOut->flags.invisible);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(WhoisInfoOut->flags.invinsible);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(WhoisInfoOut->memberSince);
  if ( !ret ) return err;
  WhoisInfoOut->memberSince =
    (blake_word32_t)network_NetToHostLong(
      (unsigned long int)WhoisInfoOut->memberSince
    );

  RECEIVE_FROM_BUFFER(WhoisInfoOut->onlineSince);
  if ( !ret ) return err;
  WhoisInfoOut->onlineSince =
    (blake_word32_t)network_NetToHostLong(
      (unsigned long int)WhoisInfoOut->onlineSince
    );

  RECEIVE_FROM_BUFFER(WhoisInfoOut->idleSince);
  if ( !ret ) return err;
  WhoisInfoOut->idleSince =
    (blake_word32_t)network_NetToHostLong(
      (unsigned long int)WhoisInfoOut->idleSince
    );

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveVersioned(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeVersioned *VersionedOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char *exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void *ret = (void*)~0;

  RECEIVE_FROM_BUFFER(VersionedOut->server.sz);
  if ( !ret ) return err;
  VersionedOut->server.sz =
    (blake_word16_t)network_NetToHostShort(
      (unsigned short int)VersionedOut->server.sz
    );

  RECEIVE_FROM_BUFFER(VersionedOut->server.major);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(VersionedOut->server.minor);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(VersionedOut->server.patch);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(VersionedOut->server.exFlags);
  if ( !ret ) return err;
  VersionedOut->server.exFlags =
    (blake_word16_t)network_NetToHostShort(
      (unsigned short int)VersionedOut->server.exFlags
    );

  RECEIVE_FROM_BUFFER(VersionedOut->os_description);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(VersionedOut->os.sz);
  if ( !ret ) return err;
  VersionedOut->os.sz =
    (blake_word16_t)network_NetToHostShort(
      (unsigned short int)VersionedOut->os.sz
    );

  RECEIVE_FROM_BUFFER(VersionedOut->os.major);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(VersionedOut->os.minor);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(VersionedOut->os.patch);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(VersionedOut->os.exFlags);
  if ( !ret ) return err;
  VersionedOut->os.exFlags =
    (blake_word16_t)network_NetToHostShort(
      (unsigned short int)VersionedOut->os.exFlags
    );

  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveSuper(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeSuper *SuperOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(SuperOut->clearance);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveKilled(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeKilled *KilledOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveChumSignon(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumSignon *ChumSignonOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveChumSignoff(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumSignoff *ChumSignoffOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveChumIdle(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumIdle *ChumIdleOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveLoginReply(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeLoginReply *LoginReplyOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(LoginReplyOut->failcode);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveIsOnlineReply(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeIsOnlineReply *IsOnlineReplyOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(IsOnlineReplyOut->username);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(IsOnlineReplyOut->boolean);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveRegisterReply(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeRegisterReply *RegisterReplyOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(RegisterReplyOut->failcode);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(RegisterReplyOut->password);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(RegisterReplyOut->reserved);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveLogin(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeLogin *LoginOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(LoginOut->_username);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(LoginOut->hashtype);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(LoginOut->_passworddigest);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(LoginOut->reserved);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(LoginOut->clientid);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(LoginOut->clientver);
  if ( !ret ) return err;

  RECEIVE_FROM_BUFFER(LoginOut->language);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveSuperRequest(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeSuperRequest *SuperRequestOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(SuperRequestOut->reserved);

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveAddEnemy(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeAddEnemy *AddEnemyOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveNotEnemy(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeNotEnemy *NotEnemyOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveJoinChat(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeJoinChat *JoinChatOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveList(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeList *ListOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveKillUser(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeKillUser *KillUserOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveKillChat(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeKillChat *KillChatOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveKick(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeKick *KickOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveVersion(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeVersion *VersionOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char *exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void *ret = (void*)~0;

  RECEIVE_FROM_BUFFER(VersionOut->reserved);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveMessage(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeMessage *MessageOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(MessageOut->username);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(MessageOut->cipher);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(MessageOut->flags1);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(MessageOut->reserved);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveMessage2(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeMessage2 *Message2Out,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(Message2Out->reserved0);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(Message2Out->reserved1);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(Message2Out->reserved2);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveChatMsg(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChatMsg *ChatMsgOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveWhois(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeWhois *WhoisOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(WhoisOut->username);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveRemoteShutdown(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeRemoteShutdown *RemoteShutdownOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveChumAdd(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumAdd *ChumAddOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveChumRemove(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeChumRemove *ChumRemoveOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveIdle(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeIdle *IdleOut,
  unsigned int *PCurrOffset
)
{
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveIsOnline(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeIsOnline *IsOnlineOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(IsOnlineOut->username);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ReceiveRegister(
  const BYTE *BuffRawIn,
  const size_t BuffSizeIn,
  struct blakeRegister *RegisterOut,
  unsigned int *PCurrOffset
)
{
  unsigned int offset = 0U;
  char* exPacketPointer = NULL;
  enum blakeError err = blakeSuccess;
  void* ret = (void*)~0;

  RECEIVE_FROM_BUFFER(RegisterOut->username);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(RegisterOut->email);
  if ( !ret ) return err;
  RECEIVE_FROM_BUFFER(RegisterOut->reserved);
  if ( !ret ) return err;

  assert(PCurrOffset);
  *PCurrOffset = (*PCurrOffset) + offset;
  return err;
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
static void i_CrapFill(
  void *CrapSpace,
  size_t CrapSpaceSize
)
{
  time_t seedTime;
  size_t i;
  char* castptr = (char*)CrapSpace;

  /* Seed the random number generator */
  time(&seedTime);
  srand((unsigned int)seedTime);

  for ( i = 0U; i < CrapSpaceSize; i++ )
    castptr[i] = (char)rand();

  return;
}
/*-------------------------------------------------------------------------*/
static void i_InitMutexes()
{
  daybothreads_CreateLock(&buffers.incoming.lock);
  daybothreads_CreateLock(&buffers.outgoing.lock);
}
/*-------------------------------------------------------------------------*/
static void i_DestroyMutexes()
{
  daybothreads_DestroyLock(&buffers.incoming.lock);
  daybothreads_DestroyLock(&buffers.outgoing.lock);
}
/*-------------------------------------------------------------------------*/
static bool i_Queue(
  enum blakeDirection Direction,
  const int Socket,
  const struct blakePacket *CPPacket,
  const void *
  CPExPacket
)
{
  if ( CPPacket ) {
    struct packet_queue* queueptr;
    bool pushResult;

    switch ( Direction ) {
      case blakeIncoming : {
        queueptr = &buffers.incoming.queue;
        daybothreads_EnterLock(&buffers.incoming.lock); /* Prelock */
        break;
      }
      case blakeOutgoing : {
        queueptr = &buffers.outgoing.queue;
        daybothreads_EnterLock(&buffers.outgoing.lock); /* Prelock */
        break;
      }
      default : {
        queueptr = NULL;
      }
    }
    assert(queueptr);
    if ( !queueptr ) {
      /* Don't worry, it's impossible we're holding any mutexes */
      return false;
    }

    /* Add to the (obsolete) packet queue */
    pushResult = queueptr->Push(queueptr, Socket, CPPacket, CPExPacket);

    /* Release the mutex */
    if ( Direction == blakeIncoming )
      daybothreads_LeaveLock(&buffers.incoming.lock);
    else
      daybothreads_LeaveLock(&buffers.outgoing.lock);

    return pushResult;
  }
  return false;
}
/*-------------------------------------------------------------------------*/
static void i_PurgeOutgoing()
{
  /* Lock the outgoing queue then send until empty before releasing it
  to other threads again. */
  struct packet_queue *queueptr;

  blake_Trace("In Blake i_PurgeOutgoing!");
  daybothreads_EnterLock(&buffers.outgoing.lock);
  queueptr = &buffers.outgoing.queue;
  while ( queueptr->Count(queueptr) ) { /* Whilst items remain */
    int socket;
    struct blakePacket packet;
    void* exPacket = NULL;
    size_t exPacketSize;
    /* First call to Top() is just to get the size of the expacket */
    exPacketSize = queueptr->Top(queueptr, &socket, &packet, NULL);
    if ( exPacketSize ) {
      exPacket = dpcrtlmm_Alloc(_blockarray, exPacketSize);

      if ( exPacket )
        queueptr->Top(queueptr, &socket, &packet, exPacket);
      else {
        packet.extendedPacketLength = 0; /* Pretend it never existed */
        i_LogMsg(
          "Warning, extended packet was discarded due to lack of memory"
        );
      }
    }

    /* Transmit! */
    i_TransmitPacket(socket, &packet, exPacket);

    /* Cleanup */
    if ( exPacket )
      dpcrtlmm_Free(_blockarray, exPacket);

    queueptr->Pop(queueptr); /* Remove top element */
  }
  daybothreads_LeaveLock(&buffers.outgoing.lock);
  return;
}
/*-------------------------------------------------------------------------*/
static enum blakeError i_ProcessIncoming()
{
  enum blakeError err = blakeSuccess;
  struct packet_queue *queueptr;

  blake_Trace("In Blake i_ProcessIncoming!");
  daybothreads_EnterLock(&buffers.incoming.lock);
  queueptr = &buffers.incoming.queue;

  while ( queueptr->Count(queueptr) ) {
    int Socket;
    struct blakePacket packet;
    void* exPacket = NULL;
    size_t exPacketSize;
    /* First call to Top() is just to get the size of the expacket */
    exPacketSize = queueptr->Top(queueptr, &Socket, &packet, NULL);
    if ( exPacketSize ) {
      exPacket = dpcrtlmm_Alloc(_blockarray, exPacketSize);

      if ( exPacket )
        queueptr->Top(queueptr, &Socket, &packet, exPacket);
      else {
        packet.extendedPacketLength = 0; /* Pretend it never existed */
        i_LogMsg(
          "Warning, extended packet was discarded due to lack of memory"
        );
      }
    }

    /*
      Execute user-callbacks for the packet
    */
    blake_cb_AcquireLock();
    if (
      blake_cb_IsInstalled(
        packet.server,
        (WORD)((packet.server) ?
          (packet.code.serverCode) :
          (packet.code.clientCode)
        )
      )
    ) {
      blake_Trace("Calling user callback");
      if ( blake_cb_Call(Socket, &packet, exPacket) )
        blake_Trace("User callback reports success");
      else
        blake_Trace("User callback reports failure");
    }
    else {
      fprintf(
        stderr,
        "Received unknown packet (no user callback) for %s code %s(%d)\n",
        packet.server ? "server" : "client",
        blake_dbgtab_ProtocolPacketCode(
          packet.server,
          ( (packet.server) ?
            (packet.code.serverCode) :
            (packet.code.clientCode)
          )
        ),
        packet.server ?
        packet.code.serverCode :
        packet.code.clientCode
      ); /* DEBUG */
    }
    queueptr->Pop(queueptr);
    blake_cb_ReleaseLock();

    if ( exPacket )
      dpcrtlmm_Free(_blockarray, exPacket);
  }

  daybothreads_LeaveLock(&buffers.incoming.lock);
  return err;
}
/*-------------------------------------------------------------------------*/
static inline void i_CopyPacket(
  struct blakePacket *PDest,
  const struct blakePacket *PSource
)
{
  assert(PDest);
  assert(PSource);
  if ( PDest && PSource )
    memcpy(PDest, PSource, sizeof(struct blakePacket));
}
/*-------------------------------------------------------------------------*/
static void i_SetString(
  blake_byte_t *PStringField,
  size_t StringFieldSize,
  const char *SourceString
)
{
  memset(PStringField, 0, StringFieldSize); /* All blank */
  memcpy(
    PStringField,
    SourceString,
    BLAKE_MIN(StringFieldSize, strlen(SourceString))
  );
  /*
    Result is desirable, no NULL terminator and padded NULLs if string
    shorter than field
  */
  return;
}
/*-------------------------------------------------------------------------*/
static bool i_BlakeCopyright()
{
  /*
    This password has been summed with algorithm Rivest RSA/MD5 from
    libdaybocrypt, source password is: OverChat Blake protocol
    (C)Copyright 2001-2006 David Duncan Ross Palmer
  */
  /*
    0a51aaf5015ca842df3c3eb52f909454
  */
  const unsigned char expectedSum[16] = {
    '\x0a', '\x51', '\xaa', '\xf5', '\x01', '\x5c', '\xa8', '\x42',
    '\xdf', '\x3c', '\x3e', '\xb5', '\x2f', '\x90', '\x94', '\x54'
  };

  MD5_CTX context;
  unsigned char actualSum[DAYBOCRYPT_MD5_HASHLEN];

  daybocrypt_MD5Init(&context);
  daybocrypt_MD5Update(
    &context,
    (const unsigned char *const)blake_copyright,
    strlen(blake_copyright)
  );
  daybocrypt_MD5Final(actualSum, &context);

  if (
    daybocrypt_CompareDigests(
      expectedSum,
      actualSum,
      DAYBOCRYPT_MD5_HASHLEN
    )
  ) {
    return true;
  }

  puts(
    "\n"
    "\n"
    "\n"
    "Nice try chum, editing my copyright is not a good plan."
  );
  return false;
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
void blake_int_protocol_NCI()
{
  blake_Unused(rcsid);
}
/*-------------------------------------------------------------------------*/
bool blake_ProtocolInit()
{
  /*
    This is the first function to be called in the Blake protocol library
  */
  if ( !i_BlakeCopyright() ) return false;
  i_InitMutexes();
  i_packet_queue_Construct(&buffers.incoming.queue, NULL);
  i_packet_queue_Construct(&buffers.outgoing.queue, NULL);
  _inited = true;
  return true;
}
/*-------------------------------------------------------------------------*/
bool blake_ProtocolCleanup()
{
  buffers.incoming.queue.Destruct(&buffers.incoming.queue);
  buffers.outgoing.queue.Destruct(&buffers.outgoing.queue);
  i_DestroyMutexes();
  _inited = false;
  return true;
}
/*-------------------------------------------------------------------------*/
bool blake_ProtocolIsInited()
{
  return _inited;
}
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(blake_bool_t) blake_Challenge(
  int Sock,
  USHORT MaxSecs,
  const unsigned char ChallengeDigest[BLAKE_MD5HASHLEN]
)
{
  struct blakePacket packet;

  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscchallenge;
  i_InitChallenge(&packet.info._challenge);
  packet.info._challenge.secs = (blake_word16_t)MaxSecs;
  if ( ChallengeDigest )
    memcpy(packet.info._challenge.rnum, ChallengeDigest, BLAKE_MD5HASHLEN);
  packet.len = blake_RecalculateRealPktSz(&packet);

  /*
    Adds to the transmit queue, this is obsolete
  */
  if ( i_Queue(blakeOutgoing, Sock, &packet, NULL) ) {
    i_PurgeOutgoing();
    return true;
  }
  return false;
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(blake_bool_t) blake_LoginReply(
  int Sock,
  enum blakeLoginReplyCode LoginReplyCode
)
{
  struct blakePacket packet;

  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscloginreply;
  i_InitLoginReply(&packet.info._loginreply);
  packet.info._loginreply.failcode = (blake_byte_t)LoginReplyCode;
  packet.len = blake_RecalculateRealPktSz(&packet);

  /*
    This is obsolete crap
  */
  if ( i_Queue(blakeOutgoing, Sock, &packet, NULL) ) {
    i_PurgeOutgoing();
    return true;
  }
  return false;
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
BLAKEAPI(blake_bool_t) blake_Login(
  int Sock,
  const char *Username,
  const char *Password,
  enum blakeLang Language,
  const unsigned char ChallengeDigest[BLAKE_MD5HASHLEN],
  const enum blakeClientID ClientID,
  const unsigned long ClientVersion,
  enum blakeHashType LoginHashType
)
{
  struct blakePacket packet;

  i_InitPacket(&packet);
  packet.server = false;
  packet.code.clientCode = bcclogin;
  i_InitLogin(&packet.info._login);
  i_SetString(
    packet.info._login._username,
    sizeof(packet.info._login._username),
    Username
  );
  packet.info._login.hashtype = LoginHashType;

  switch ( LoginHashType ) {
    case bhtMd5 : {
      blake_MD5SecureDoublePasswordHash(
        packet.info._login._passworddigest,
        Password,
        ChallengeDigest
      );
      break;
    }
    case bhtSha1 : {
      blake_SHA1SecureDoublePasswordHash(
        packet.info._login._passworddigest,
        Password,
        ChallengeDigest
      );
      break;
    }
    case bhtSha256 : {
      blake_SHA256SecureDoublePasswordHash(
        packet.info._login._passworddigest,
        Password,
        ChallengeDigest
      );
      break;
    }
    case bhtSha384 : {
      blake_SHA384SecureDoublePasswordHash(
        packet.info._login._passworddigest,
        Password,
        ChallengeDigest
      );
      break;
    }
    case bhtSha512 : {
      blake_SHA512SecureDoublePasswordHash(
        packet.info._login._passworddigest,
        Password,
        ChallengeDigest
      );
      break;
    }
    default : { return false; }
  }
  /* Reserved field(s) must be zeroed */
  memset(
    packet.info._login.reserved,
    0,
    sizeof(packet.info._login.reserved)
  );
  packet.info._login.clientid = (blake_word32_t)ClientID;
  packet.info._login.clientver = (blake_word32_t)ClientVersion;
  packet.info._login.language = (blake_byte_t)Language;
  packet.len = blake_RecalculateRealPktSz(&packet);

  /* Add to the (obsolete) transmit queue */
  if ( i_Queue(blakeOutgoing, Sock, &packet, NULL) ) {
    i_PurgeOutgoing();
    return true;
  }
  return false;
}
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(blake_bool_t) blake_Suspend(
  int Sock,
  const unsigned long Seconds
)
{
  struct blakePacket packet;

  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscsuspend;
  i_InitSuspend(&packet.info._suspend);
  /*
    It's called dummy_time because clients ignore it at the moment,
    it tricks people trying to write clients without permission though.
    (20040906 - This comment is maintained for historical record of the
    development of the source code).
  */
  packet.info._suspend.dummy_time = (blake_word32_t)Seconds;
  packet.len = blake_RecalculateRealPktSz(&packet);

  /* Add to the (obsolete) transmit queue */
  if ( i_Queue(blakeOutgoing, Sock, &packet, NULL) ) {
    i_PurgeOutgoing();
    return true;
  }
  return false;
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(blake_bool_t) blake_Resume(int Sock)
{
  struct blakePacket packet;

  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscresume;
  i_InitResume(&packet.info._resume);
  packet.info._resume.reserved = 0; /* Blah */
  packet.len = blake_RecalculateRealPktSz(&packet);

  /* Add to the (obsolete) transmit queue */
  if ( i_Queue(blakeOutgoing, Sock, &packet, NULL) ) {
    i_PurgeOutgoing();
    return true;
  }
  return false;
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_Msg(
  int Sock,
  const char *NickName,
  const char *Msg,
  enum blakeCipherID CipherID,
  blake_word8_t Flags1
)
{
  struct blakePacket packet;
  BYTE* exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 || !NickName || !Msg )
    return blakeInvalidParam;

  blake_Trace("In blake_Msg");
  i_InitPacket(&packet);
  packet.server = false;
  packet.code.clientCode = bccmessage;
  i_InitMessage(&packet.info._message);
  i_SetString(
    packet.info._message.username,
    sizeof(packet.info._message.username),
    NickName
  );
  packet.extendedPacketLength = (unsigned short int)strlen(Msg);
  packet.info._message.cipher = (blake_word8_t)CipherID;
  packet.info._message.flags1 = Flags1;
  /*packet.info._message.reserved*/ /* No need to do anything with this */
  packet.len = blake_RecalculateRealPktSz(&packet);

  blake_Trace(Msg);
  /*
    Add packet and extended packet to the (obsolete) transmit queue
  */
  if ( !i_Queue(blakeOutgoing, Sock, &packet, Msg) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_Msg2(
  int Sock,
  const char *Command
)
{
  struct blakePacket packet;
  BYTE* exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 || !Command )
    return blakeInvalidParam;

  blake_Trace("in blake_Msg2 (sub-protocol)");
  i_InitPacket(&packet);
  packet.server = false;
  packet.code.clientCode = bccmessage2;
  i_InitMessage2(&packet.info._message2);
  packet.extendedPacketLength = (unsigned short int)strlen(Command);
  packet.len = blake_RecalculateRealPktSz(&packet);

  blake_Trace(Command);
  /*
    Add packet and extended packet to the (obsolete) transmit queue
  */
  if ( !i_Queue(blakeOutgoing, Sock, &packet, Command) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_Kill(
  int Sock,
  const char* NickName,
  const char* Sneer
)
{
}
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(enum blakeError) blake_Killed(
  int Sock,
  const char* NickOfKiller,
  const char* Sneer
)
{
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_Whois(
  int Sock,
  const char *NickName
)
{
  struct blakePacket packet;
  BYTE* exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 || !NickName )
    return blakeInvalidParam;

  blake_Trace("In blake_Whois");
  i_InitPacket(&packet);
  packet.server = false;
  packet.code.clientCode = bccwhois;
  i_InitWhois(&packet.info._whois);
  i_SetString(
    packet.info._whois.username,
    sizeof(packet.info._whois.username),
    NickName
  );
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_Version(
  int Sock
)
{
  struct blakePacket packet;
  BYTE *exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 )
    return blakeInvalidParam;

  blake_Trace("In blake_Version()");
  i_InitPacket(&packet);
  packet.server = false;
  packet.code.clientCode = bccversion;
  i_InitVersion(&packet.info._version);
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(enum blakeError) blake_WhoisInfo(
  int Sock,
  const struct blakeWhoisInfo *PWhoisInfo
)
{
  struct blakePacket packet;
  BYTE* exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 || !PWhoisInfo )
    return blakeInvalidParam;

  blake_Trace("In blake_WhoisInfo");
  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscwhois_info;
  i_InitWhoisInfo(&packet.info._whoisinfo);
  memcpy(
    &packet.info._whoisinfo, PWhoisInfo, sizeof(packet.info._whoisinfo)
  );
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(enum blakeError) blake_Versioned(
  int Sock,
  const struct blakeVersioned *PVersioned
)
{
  struct blakePacket packet;
  BYTE *exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 || !PVersioned )
    return blakeInvalidParam;

  blake_Trace("In blake_Versioned()");
  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscversioned;
  i_InitVersioned(&packet.info._versioned);
  memcpy(
    &packet.info._versioned, PVersioned, sizeof(packet.info._versioned)
  );
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_GotError(
  int Sock,
  const enum blakeErrorCodes ErrorCode,
  const char *Nick
)
{
  struct blakePacket packet;
  BYTE *exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 )
    return blakeInvalidParam;

  blake_Trace("In blake_GotError()");
  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscgoterror;
  i_InitGotError(&packet.info._goterror);
  packet.info._goterror.errnum = ErrorCode;
  if ( Nick ) {
    i_SetString(
      packet.info._goterror.username,
      sizeof(packet.info._goterror.username),
      Nick
    );
  }
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_IsOnline(
  int Sock,
  const char *NickName
)
{
  struct blakePacket packet;
  BYTE* exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 || !NickName )
    return blakeInvalidParam;

  blake_Trace("In blake_IsOnline");
  i_InitPacket(&packet);
  packet.server = false;
  packet.code.clientCode = bccisonline;
  i_InitIsOnline(&packet.info._isonline);
  i_SetString(
    packet.info._isonline.username,
    sizeof(packet.info._isonline.username),
    NickName
  );
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(enum blakeError) blake_IsOnlineReply(
  int Sock,
  const char *NickName,
  blake_bool_t Yes
)
{
  struct blakePacket packet;
  BYTE* exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 )
    return blakeInvalidParam;

  blake_Trace("In blake_IsOnlineReply");
  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscisonlinereply;
  i_InitIsOnlineReply(&packet.info._isonlinereply);
  i_SetString(
    packet.info._isonlinereply.username,
    sizeof(packet.info._isonlinereply.username),
    NickName
  );
  packet.info._isonlinereply.boolean = (blake_byte_t)Yes;
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_SuperRequest(
  int Sock,
  blake_word8_t Reserved
)
{
  struct blakePacket packet;
  BYTE* exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 )
    return blakeInvalidParam;

  blake_Trace("In blake_SuperRequest");
  i_InitPacket(&packet);
  packet.server = false;
  packet.code.clientCode = bccsuper_request;
  i_InitSuperRequest(&packet.info._superrequest);
  /* Put in reserved parameter */
  packet.info._superrequest.reserved = Reserved;
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
/*-------------------------------------------------------------------------*/
#ifdef BLAKE_SERVER
BLAKEAPI(enum blakeError) blake_Super(
  int Sock,
  blake_word8_t Clearance
)
{
  struct blakePacket packet;
  BYTE* exPacket = NULL;

  if ( !blake_int_IsInited() ) return blakeNotStarted;
  if ( !Sock || Sock == -1 )
    return blakeInvalidParam;

  blake_Trace("In blake_Super (reply to super request)");
  i_InitPacket(&packet);
  packet.server = true;
  packet.code.serverCode = bscsuper;
  i_InitSuper(&packet.info._super);
  packet.info._super.clearance = Clearance; /* Set clearance in packet */
  packet.len = blake_RecalculateRealPktSz(&packet);

  if ( !i_Queue(blakeOutgoing, Sock, &packet, exPacket) )
    return blakeQueueFull;

  i_PurgeOutgoing();
  return blakeSuccess;
}
#endif /*BLAKE_SERVER*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_CrackPacket(
  int Socket,
  const void *PacketBuffer,
  const size_t BufferLength
)
{
  struct blakePacket packet;
  enum blakeError err;
  BYTE *exPacket = NULL;

#ifdef DEBUG
  printf(
    "(DEBUG)In blake_CrackPacket(Socket=%d, "
    "PacketBuffer=%p, BufferLength=%u\n)",
    Socket,
    PacketBuffer,
    (unsigned int)BufferLength
  );
#endif /*DEBUG*/
  if ( !PacketBuffer || !BufferLength )
    return blakeInvalidParam; /* Protection */

  i_InitPacket(&packet);
  /* Now let's officially receive from the buffer */
  exPacket = i_ReceivePacket(
    (const BYTE*)PacketBuffer,
    BufferLength,
    &packet,
    &err
  );
  if ( err != blakeSuccess ) {
    if ( exPacket ) free(exPacket);
    return err;
  }
  /* Now let's stick the packet onto the queue */
  if ( i_Queue(blakeIncoming, Socket, &packet, exPacket) )
    err = i_ProcessIncoming();
  else
    err = blakeNotEnoughMem;

  /* Cleanup */
  if ( exPacket )
    free(exPacket);
  return err;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(blake_word16_t) blake_RecalculateRealPktSz(
  struct blakePacket *PPacket
)
{
  blake_word16_t sz = 0;

  if ( PPacket ) {
    sz = sizeof(PPacket->len);
    sz += (blake_word16_t)sizeof(PPacket->server);

    /* 16-bits for the serverCode or clientCode */
    sz += (blake_word16_t)sizeof(blake_word16_t);

    if ( PPacket->server ) {
      switch ( PPacket->code.serverCode ) {
        case bscsuspend : {
          sz += (blake_word16_t)sizeof(PPacket->info._suspend);
          break;
        }
        case bscresume : {
          sz += (blake_word16_t)sizeof(PPacket->info._resume);
          break;
        }
        case bscchallenge : {
          sz += (blake_word16_t)sizeof(PPacket->info._challenge.secs);
          sz += (blake_word16_t)sizeof(PPacket->info._challenge._reserved);
          sz += (blake_word16_t)sizeof(PPacket->info._challenge.rnum);
          break;
        }
        case bscgoterror : {
          sz += (blake_word16_t)sizeof(PPacket->info._goterror.errnum);
          sz += (blake_word16_t)sizeof(PPacket->info._goterror.username);
          break;
        }
        case bsclist_response : {
          sz += (blake_word16_t)sizeof(PPacket->info._listresponse);
          break;
        }
        case bscwhois_info : {
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.username);
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.usingIpv6);
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.ip);
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.clearance);
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.warning);
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.limiter);
          sz += (blake_word16_t)sizeof(
            PPacket->info._whoisinfo.flags.invisible
          );
          sz += (blake_word16_t)sizeof(
            PPacket->info._whoisinfo.flags.invinsible
          );
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.memberSince);
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.onlineSince);
          sz += (blake_word16_t)sizeof(PPacket->info._whoisinfo.idleSince);
          break;
        }
        case (blake_word16_t)bscversioned : {
          blake_word16_t versz;
          versz  = sizeof(PPacket->info._versioned.server.sz);
          versz += sizeof(PPacket->info._versioned.server.major);
          versz += sizeof(PPacket->info._versioned.server.minor);
          versz += sizeof(PPacket->info._versioned.server.patch);
          versz += sizeof(PPacket->info._versioned.server.exFlags);

          sz += versz;
          sz += (blake_word16_t)sizeof(
            PPacket->info._versioned.os_description
          );
          sz += versz;
          break;
        }
        case bscsuper : {
          sz += (blake_word16_t)sizeof(PPacket->info._super);
          break;
        }
        case bsckilled : {
          sz += (blake_word16_t)sizeof(PPacket->info._killed);
          break;
        }
        case bscchum_signon : {
          sz += (blake_word16_t)sizeof(PPacket->info._chumsignon);
          break;
        }
        case bscchum_signoff : {
          sz += (blake_word16_t)sizeof(PPacket->info._chumsignoff);
          break;
        }
        case bscchum_idle : {
          sz += (blake_word16_t)sizeof(PPacket->info._chumidle);
          break;
        }
        case bscloginreply : {
          sz += (blake_word16_t)sizeof(PPacket->info._loginreply.failcode);
          break;
        }
        case bscisonlinereply : {
          sz += (blake_word16_t)sizeof(
            PPacket->info._isonlinereply.username
          );
          sz += (blake_word16_t)sizeof(PPacket->info._isonlinereply.boolean);
          break;
        }
        case bscregisterreply : {
          sz += (blake_word16_t)sizeof(
            PPacket->info._registerreply.failcode
          );
          sz += (blake_word16_t)sizeof(
            PPacket->info._registerreply.password
          );
          sz += (blake_word16_t)sizeof(
            PPacket->info._registerreply.reserved
          );
          break;
        }
      }
    }
    else { /* Client code */
      switch ( PPacket->code.clientCode ) {
        case bccreserved0 : {}
        case bccreserved1 : {}
        case bccreserved2 : {}
        case bcclogin : {
          sz += (blake_word16_t)sizeof(PPacket->info._login._username);
          sz += (blake_word16_t)sizeof(PPacket->info._login.hashtype);
          sz += (blake_word16_t)sizeof(PPacket->info._login._passworddigest);
          sz += (blake_word16_t)sizeof(PPacket->info._login.reserved);
          sz += (blake_word16_t)sizeof(PPacket->info._login.clientid);
          sz += (blake_word16_t)sizeof(PPacket->info._login.clientver);
          sz += (blake_word16_t)sizeof(PPacket->info._login.language);
          break;
        }
        case bccsuper_request : {
          sz += (blake_word16_t)sizeof(PPacket->info._superrequest);
          break;
        }
        case bccadd_enemy : {
          sz += (blake_word16_t)sizeof(PPacket->info._addenemy);
          break;
        }
        case bccnot_enemy : {
          sz += (blake_word16_t)sizeof(PPacket->info._notenemy);
          break;
        }
        case bccjoin_chat : {
          sz += (blake_word16_t)sizeof(PPacket->info._joinchat);
          break;
        }
        case bcclist : {
          sz += (blake_word16_t)sizeof(PPacket->info._list);
          break;
        }
        case bcckilluser : {
          sz += (blake_word16_t)sizeof(PPacket->info._killuser);
          break;
        }
        case bcckillchat : {
          sz += (blake_word16_t)sizeof(PPacket->info._killchat);
          break;
        }
        case bcckick : {
          sz += (blake_word16_t)sizeof(PPacket->info._kick);
          break;
        }
        case bccversion : {
          sz += (blake_word16_t)sizeof(PPacket->info._version);
          break;
        }
        case bccmessage : {
          sz += (blake_word16_t)sizeof(PPacket->info._message.username);
          sz += (blake_word16_t)sizeof(PPacket->info._message.cipher);
          sz += (blake_word16_t)sizeof(PPacket->info._message.flags1);
          sz += (blake_word16_t)sizeof(PPacket->info._message.reserved);
          break;
        }
        case bccmessage2 : {
          sz += (blake_word8_t)sizeof(PPacket->info._message2.reserved0);
          sz += (blake_word8_t)sizeof(PPacket->info._message2.reserved1);
          sz += (blake_word8_t)sizeof(PPacket->info._message2.reserved2);
          break;
        }
        case bccchatmsg : {
          sz += (blake_word16_t)sizeof(PPacket->info._chatmsg);
          break;
        }
        case bccwhois : {
          sz += (blake_word16_t)sizeof(PPacket->info._whois);
          break;
        }
        case bccremoteshutdown : {
          sz += (blake_word16_t)sizeof(PPacket->info._remoteshutdown);
          break;
        }
        case bccchum_add : {
          sz += (blake_word16_t)sizeof(PPacket->info._chumadd);
          break;
        }
        case bccchum_remove : {
          sz += (blake_word16_t)sizeof(PPacket->info._chumremove);
          break;
        }
        case bccidle : {
          sz += (blake_word16_t)sizeof(PPacket->info._idle);
          break;
        }
        case bccisonline : {
          sz += (blake_word16_t)sizeof(PPacket->info._isonline.username);
          break;
        }
        case bccregister : {
          sz += (blake_word16_t)sizeof(PPacket->info._register.username);
          sz += (blake_word16_t)sizeof(PPacket->info._register.email);
          sz += (blake_word16_t)sizeof(PPacket->info._register.reserved);
          break;
        }
      }
    }

    sz += (blake_word16_t)sizeof(PPacket->extendedPacketLength);
    /*
      Size the extended packet itself
    */
    sz += (blake_word16_t)PPacket->extendedPacketLength;
  }

#ifdef DEBUG
  printf(
    "DEBUG (Blake protocol) %u = blake_RecalculateRealPktSz(%p)\n",
    sz,
    PPacket
  );
#endif
  assert(sz);
  return sz;
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
void i_packet_queue_Construct(
  struct packet_queue *PQueue,
  S_DPCRTLMM_BLOCKDESCARRAY *PBlockArray
)
{
  assert(PQueue);
  if ( PQueue ) {
    i_packet_queue_VTableLink(PQueue);
    i_packet_queue_Init(PQueue);
    if ( PBlockArray ) {
      PQueue->_blockarray = PBlockArray;
    }
    else { /* Caller does not own a block array */
      PQueue->_blockarray = dpcrtlmm_CreateBlockArray();
      if ( !PQueue->_blockarray ) {
        i_LogMsg("Queue constructor failed");
        PQueue->_good = false;
      }
      PQueue->_blockarrayOwnage = true;
    }
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_packet_queue_Destruct(struct packet_queue* PQueue)
{
  assert(PQueue);
  if ( PQueue )
    i_packet_queue_Reinit(PQueue);
}
/*-------------------------------------------------------------------------*/
static void i_packet_queue_VTableLink(struct packet_queue* PQueue)
{
  assert(PQueue);
  if ( PQueue ) {
    PQueue->Push = l_packet_queue_Push;
    PQueue->Top = l_packet_queue_Top;
    PQueue->Pop = l_packet_queue_Pop;
    PQueue->Count = l_packet_queue_Count;
    PQueue->Destruct = l_packet_queue_Destruct;
  }
}
/*-------------------------------------------------------------------------*/
static void i_packet_queue_Init(struct packet_queue* PQueue)
{
  assert(PQueue);
  if ( PQueue ) {
    UINT i;

    for ( i = 0U; i < BLAKE_QUEUESIZE; i++ )
      i_packet_queue_InitIndex(PQueue, i);

    PQueue->_blockarray = NULL;
    PQueue->_blockarrayOwnage = false;
    PQueue->_good = true;
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_packet_queue_InitIndex(struct packet_queue* PQueue, UINT Index)
{
  assert(PQueue);
  if ( PQueue ) {
    assert(Index < BLAKE_QUEUESIZE);
    if ( Index < BLAKE_QUEUESIZE ) {
      PQueue->_packets[Index].inuse = false;
      PQueue->_packets[Index].socket = 0;
      i_InitPacket(&PQueue->_packets[Index].packet);
      PQueue->_packets[Index].exPacketData = NULL;
    }
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_packet_queue_Reinit(struct packet_queue* PQueue)
{
  assert(PQueue);
  if ( PQueue ) {
    UINT i;

    for ( i = 0U; i < BLAKE_QUEUESIZE; i++ )
      i_packet_queue_ReinitIndex(PQueue, i);

    if ( PQueue->_blockarrayOwnage ) /* Our responsibillty? */
      dpcrtlmm_DestroyBlockArray(PQueue->_blockarray);
    PQueue->_blockarrayOwnage = false;
    PQueue->_blockarray = NULL;
    PQueue->_good = true;
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void i_packet_queue_ReinitIndex(
  struct packet_queue *PQueue,
  UINT Index
)
{
  assert(PQueue);
  if ( PQueue ) {
    assert(Index < BLAKE_QUEUESIZE);
    if ( Index < BLAKE_QUEUESIZE ) {
      if ( PQueue->_packets[Index].inuse ) {
        PQueue->_packets[Index].socket = 0;
        i_InitPacket(&PQueue->_packets[Index].packet);
        if ( PQueue->_packets[Index].exPacketData ) {
          i_packet_queue_Free(
            PQueue,
            PQueue->_packets[Index].exPacketData
          );
          PQueue->_packets[Index].exPacketData = NULL;
        }
        PQueue->_packets[Index].inuse = false; /* No longer in use */
      }
    }
  }
  return;
}
/*-------------------------------------------------------------------------*/
static void* i_packet_queue_Malloc(
  struct packet_queue *PQueue,
  size_t ChunkSize
)
{
  void* ptr = NULL;

  assert(PQueue);
  assert(ChunkSize);
  if ( PQueue && ChunkSize ) {
    if ( PQueue->_blockarray ) {
      ptr = dpcrtlmm_Alloc(PQueue->_blockarray, ChunkSize);
      if ( !ptr )
        i_LogMsg("Packet queue out of memory");
    }
    else {
      PQueue->_good = false;
    }
  }
  return ptr;
}
/*-------------------------------------------------------------------------*/
static void i_packet_queue_Free(
  struct packet_queue *PQueue,
  void *Ptr
)
{
  assert(PQueue);
  assert(Ptr);
  if ( PQueue ) {
    if ( PQueue->_blockarray && Ptr )
      dpcrtlmm_Free(PQueue->_blockarray, Ptr);
    else
      PQueue->_good = false;
  }
  return;
}
/*-------------------------------------------------------------------------*/
static UINT i_packet_queue_FindFree(
  struct packet_queue *PQueue
)
{
  assert(PQueue);

  if ( PQueue ) {
    UINT i;

    for ( i = 0U; i < BLAKE_QUEUESIZE; i++ ) {
      if ( !PQueue->_packets[i].inuse ) /* Not in use */
        return i; /* That's it! */
    }
  }
  return BLAKE_QUEUESIZE; /* Out of range; out of space, buffer overrun */
}
/*-------------------------------------------------------------------------*/
static bool l_packet_queue_Push(
  struct packet_queue *PQueue,
  const int Socket,
  const struct blakePacket *CPPacket,
  const void *ExPacket
)
{
  UINT freeIndex; /* First free index */

  assert(PQueue);
  assert(CPPacket);

  freeIndex = i_packet_queue_FindFree(PQueue);
  if ( freeIndex >= BLAKE_QUEUESIZE ) /* Out of range means none left */
    return false;

  PQueue->_packets[freeIndex].inuse = true; /* Now in use by us */
  /* Save the socket this packet is due for/sent from */
  PQueue->_packets[freeIndex].socket = Socket;
  i_CopyPacket(&PQueue->_packets[freeIndex].packet, CPPacket);
  if ( ExPacket && CPPacket->extendedPacketLength ) {
    /*
      We have extended data to add to the packet, the extended packet
      data must be an initialised, clean packet, my fault if not,
      assert to be sure.
    */
    assert(!PQueue->_packets[freeIndex].exPacketData);
    PQueue->_packets[freeIndex].exPacketData =
      i_packet_queue_Malloc(PQueue, CPPacket->extendedPacketLength);
    if ( !PQueue->_packets[freeIndex].exPacketData ) {
      PQueue->_good = false; /* Set error flag */
      /* Clean up, in case caller ignores it */
      i_packet_queue_ReinitIndex(PQueue, freeIndex);
      return false; /* Get out of here */
    }
    /* Copy extended packet data */
    memcpy(
      PQueue->_packets[freeIndex].exPacketData,
      ExPacket,
      CPPacket->extendedPacketLength
    );
  }
  return true; /* Safe */
}
/*-------------------------------------------------------------------------*/
static size_t l_packet_queue_Top(
  struct packet_queue *PQueue,
  int *PSocket,
  struct blakePacket *PPacket,
  void *ExPacket
)
{
  UINT endIndex;
  size_t exPacketSize = 0U;

  assert(PQueue);
#ifdef NDEBUG
  if ( !PQueue ) return exPacketSize;
#endif /*NDEBUG*/

  /* Be careful, both parameters are optional */
  endIndex = i_packet_queue_FindFree(PQueue); /* Get the free element */
  if ( endIndex == 0U ) { /* Queue is empty */
    /* Callers should use Count(), so they know what's going on here! */
    return exPacketSize;
  }
  endIndex--; /* Translate last free into last used (top) */

  exPacketSize = PQueue->_packets[endIndex].packet.extendedPacketLength;

  /* Copy data to caller */
  if ( PSocket )
    *PSocket = PQueue->_packets[endIndex].socket;
  if ( PPacket )
    i_CopyPacket(PPacket, &PQueue->_packets[endIndex].packet);

  /* Three conditions must be met for this */
  if ( ExPacket && /* Want expacket */
       exPacketSize && /* expacket has size in packet */
       PQueue->_packets[endIndex].exPacketData /* Stored */
  )
    memcpy(ExPacket, PQueue->_packets[endIndex].exPacketData, exPacketSize);

  return exPacketSize;
}
/*-------------------------------------------------------------------------*/
static void l_packet_queue_Pop(
  struct packet_queue *PQueue
)
{
  UINT i; /* Compact loop controller */

  assert(PQueue);
  if ( PQueue ) {
    /* Get the free element */
    UINT endIndex = i_packet_queue_FindFree(PQueue);
    if ( endIndex == 0U ) /* Queue is empty */
      return; /* Blah, popping with nothing to do */

    endIndex--; /* Translate last free into last used (top) */
    /* Clean up any trash in this node */
    i_packet_queue_ReinitIndex(PQueue, endIndex);

    /* Shift up the list to fill the gap */
    for ( i = endIndex+1; i < BLAKE_QUEUESIZE; i++ ) {
      i_CopyPacket(
        &PQueue->_packets[i-1].packet,
        &PQueue->_packets[i].packet
      );
    }
  }
  return;
}
/*-------------------------------------------------------------------------*/
static UINT l_packet_queue_Count(struct packet_queue* PQueue)
{
  /* For speed, as soon as we hit the first non-in-use packet entry in
  the queue, we return. */

  UINT i, count = 0U;

  assert(PQueue);
  if ( PQueue ) {
    for ( i = 0U; i < BLAKE_QUEUESIZE; i++ ) {
      if ( PQueue->_packets[i].inuse ) count++;
      else break;
    }
  }
  return count;
}
/*-------------------------------------------------------------------------*/