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
  
/* 
 * $Id: tds.pike,v 1.6 2006/02/10 14:45:56 grubba Exp $ 
 * 
 * A Pike implementation of the TDS protocol. 
 * 
 * Henrik Grubbström 2006-02-08. 
 */ 
 
#define TDS_DEBUG 
 
#ifdef TDS_DEBUG 
#define TDS_WERROR(X...)    werror("TDS:" + X) 
#else 
#define TDS_WERROR(X...) 
#endif 
 
static int filter_noprint(int char) 
{ 
  return ((char == 32) || ((char != 0x7f) && (char & 0x60)))?char:'.'; 
} 
 
static string hex_dump(string data) { 
  array(string) lines = data/16.0; 
  int off; 
  foreach(lines; int i; string line) { 
    array(array(int)) halves = (array(array(int)))((line + ("\0"*16))/8); 
    lines[i] = sprintf("%04x:%{ %02x%}:%{%02x %} %s:%s\n", 
                       off, halves[0], halves[1], 
                       (string)map(halves[0], filter_noprint), 
                       (string)map(halves[1], filter_noprint)); 
    off += 16; 
  } 
  return lines * ""; 
} 
 
#if (__REAL_MAJOR__ > 7) || ((__REAL_MAJOR__ == 7) && (__REAL_MINOR__ >= 6)) 
// Static blocks affect nested classes in Pike 7.4. 
// We don't want that... 
static { 
#endif /* Pike 7.6 or later */ 
  constant DEF_MAJOR = 8; 
  constant DEF_MINOR = 0; 
  constant DEF_PORT = 1433; 
  constant DEF_DOMAIN = ""; 
  constant DEFAULT_CAPABILITIES = 
    "\x01\x09\x00\x00\x06\x6d\x7f\xff" 
    "\xff\xff\xfe\x02\x09\x00\x00\x00" 
    "\x00\x0a\x68\x00\x00\x00"; 
  constant TDS8VERSION = "\x01\x00\x00\x71"; 
  constant CLIENTVERSION = "\x06\x83\xf2\xf8"; 
  constant CONNECTIONID = "\0\0\0\0"; 
  constant TIMEZONE = "\x88\xff\xff\xff"; 
  constant COLLATION = "\x36\x04\x00\x00"; 
 
  enum Token { 
    TDS_ERROR                   = 3, 
    TDS_DONT_RETURN             = 42, 
 
    TDS5_PARAMFMT2_TOKEN        = 32,   /* 0x20 */ 
    TDS_LANGUAGE_TOKEN          = 33,   /* 0x21    TDS 5.0 only              */ 
    TDS_ORDERBY2_TOKEN          = 34,   /* 0x22 */ 
    TDS_ROWFMT2_TOKEN           = 97,   /* 0x61    TDS 5.0 only              */ 
    TDS_LOGOUT_TOKEN            = 113,  /* 0x71    TDS 5.0 only? ct_close()  */ 
    TDS_RETURNSTATUS_TOKEN      = 121,  /* 0x79                              */ 
    TDS_PROCID_TOKEN            = 124,  /* 0x7C    TDS 4.2 only - TDS_PROCID */ 
    TDS7_RESULT_TOKEN           = 129,  /* 0x81    TDS 7.0 only              */ 
    TDS7_COMPUTE_RESULT_TOKEN   = 136,  /* 0x88    TDS 7.0 only              */ 
    TDS_COLNAME_TOKEN           = 160,  /* 0xA0    TDS 4.2 only              */ 
    TDS_COLFMT_TOKEN            = 161,  /* 0xA1    TDS 4.2 only - TDS_COLFMT */ 
    TDS_DYNAMIC2_TOKEN          = 163,  /* 0xA3 */ 
    TDS_TABNAME_TOKEN           = 164,  /* 0xA4 */ 
    TDS_COLINFO_TOKEN           = 165,  /* 0xA5 */ 
    TDS_OPTIONCMD_TOKEN         = 166,  /* 0xA6 */ 
    TDS_COMPUTE_NAMES_TOKEN     = 167,  /* 0xA7 */ 
    TDS_COMPUTE_RESULT_TOKEN    = 168,  /* 0xA8 */ 
    TDS_ORDERBY_TOKEN           = 169,  /* 0xA9    TDS_ORDER                 */ 
    TDS_ERROR_TOKEN             = 170,  /* 0xAA                              */ 
    TDS_INFO_TOKEN              = 171,  /* 0xAB                              */ 
    TDS_PARAM_TOKEN             = 172,  /* 0xAC    RETURNVALUE?              */ 
    TDS_LOGINACK_TOKEN          = 173,  /* 0xAD                              */ 
    TDS_CONTROL_TOKEN           = 174,  /* 0xAE    TDS_CONTROL               */ 
    TDS_ROW_TOKEN               = 209,  /* 0xD1                              */ 
    TDS_CMP_ROW_TOKEN           = 211,  /* 0xD3                              */ 
    TDS5_PARAMS_TOKEN           = 215,  /* 0xD7    TDS 5.0 only              */ 
    TDS_CAPABILITY_TOKEN        = 226,  /* 0xE2                              */ 
    TDS_ENVCHANGE_TOKEN         = 227,  /* 0xE3                              */ 
    TDS_EED_TOKEN               = 229,  /* 0xE5                              */ 
    TDS_DBRPC_TOKEN             = 230,  /* 0xE6                              */ 
    TDS5_DYNAMIC_TOKEN          = 231,  /* 0xE7    TDS 5.0 only              */ 
    TDS5_PARAMFMT_TOKEN         = 236,  /* 0xEC    TDS 5.0 only              */ 
    TDS_AUTH_TOKEN              = 237,  /* 0xED                              */ 
    TDS_RESULT_TOKEN            = 238,  /* 0xEE                              */ 
    TDS_DONE_TOKEN              = 253,  /* 0xFD    TDS_DONE                  */ 
    TDS_DONEPROC_TOKEN          = 254,  /* 0xFE    TDS_DONEPROC              */ 
    TDS_DONEINPROC_TOKEN        = 255,  /* 0xFF    TDS_DONEINPROC            */ 
    /* CURSOR support: TDS 5.0 only*/ 
    TDS_CURCLOSE_TOKEN          = 128,  /* 0x80    TDS 5.0 only              */ 
    TDS_CURFETCH_TOKEN          = 130,  /* 0x82    TDS 5.0 only              */ 
    TDS_CURINFO_TOKEN           = 131,  /* 0x83    TDS 5.0 only              */ 
    TDS_CUROPEN_TOKEN           = 132,  /* 0x84    TDS 5.0 only              */ 
    TDS_CURDECLARE_TOKEN        = 134,  /* 0x86    TDS 5.0 only              */ 
  }; 
 
  enum EnvType { 
    /* environment type field */ 
    TDS_ENV_DATABASE            = 1, 
    TDS_ENV_LANG                = 2, 
    TDS_ENV_CHARSET             = 3, 
    TDS_ENV_PACKSIZE            = 4, 
    TDS_ENV_LCID                = 5, 
    TDS_ENV_SQLCOLLATION        = 7, 
  }; 
 
  enum ColType { 
    SYBBINARY           = 45,   /* 0x2d */ 
    SYBBIT              = 50,   /* 0x32 */ 
    SYBBITN             = 104,  /* 0x68 */ 
    SYBCHAR             = 47,   /* 0x2f */ 
    SYBDATETIME         = 61,   /* 0x3d */ 
    SYBDATETIME4        = 58,   /* 0x3a */ 
    SYBDATETIMN         = 111,  /* 0x6f */ 
    SYBDECIMAL          = 106,  /* 0x6a */ 
    SYBFLT8             = 62,   /* 0x3e */ 
    SYBFLTN             = 109,  /* 0x6d */ 
    SYBIMAGE            = 34,   /* 0x22 */ 
    SYBINT1             = 48,   /* 0x30 */ 
    SYBINT2             = 52,   /* 0x34 */ 
    SYBINT4             = 56,   /* 0x38 */ 
    SYBINT8             = 127,  /* 0x7f */ 
    SYBINTN             = 38,   /* 0x26 */ 
    SYBLONGBINARY       = 225,  /* 0xe1 */ 
    SYBMONEY            = 60,   /* 0x3c */ 
    SYBMONEY4           = 122,  /* 0x7a */ 
    SYBMONEYN           = 110,  /* 0x6e */ 
    SYBNTEXT            = 99,   /* 0x63 */ 
    SYBNUMERIC          = 108,  /* 0x6c */ 
    SYBNVARCHAR         = 103,  /* 0x67 */ 
    SYBREAL             = 59,   /* 0x3b */ 
    SYBSINT1            = 64,   /* 0x40 */ 
    SYBTEXT             = 35,   /* 0x23 */ 
    SYBUINT2            = 65,   /* 0x41 */ 
    SYBUINT4            = 66,   /* 0x42 */ 
    SYBUINT8            = 67,   /* 0x43 */ 
    SYBUNIQUE           = 36,   /* 0x24 */ 
    SYBVARBINARY        = 37,   /* 0x25 */ 
    SYBVARCHAR          = 39,   /* 0x27 */ 
    SYBVARIANT          = 98,   /* 0x62 */ 
    SYBVOID             = 31,   /* 0x1f */ 
    XSYBBINARY          = 173,  /* 0xad */ 
    XSYBCHAR            = 175,  /* 0xaf */ 
    XSYBNCHAR           = 239,  /* 0xef */ 
    XSYBNVARCHAR        = 231,  /* 0xe7 */ 
    XSYBVARBINARY       = 165,  /* 0xa5 */ 
    XSYBVARCHAR         = 167,  /* 0xa7 */ 
 
    TDS_UT_TIMESTAMP    = 80,   /* User type. */ 
  }; 
 
  enum ResultTypes { 
    TDS_ROW_RESULT              = 4040, 
    TDS_PARAM_RESULT            = 4042, 
    TDS_STATUS_RESULT           = 4043, 
    TDS_MSG_RESULT              = 4044, 
    TDS_COMPUTE_RESULT          = 4045, 
    TDS_CMD_DONE                = 4046, 
    TDS_CMD_SUCCEED             = 4047, 
    TDS_CMD_FAIL                = 4048, 
    TDS_ROWFMT_RESULT           = 4049, 
    TDS_COMPUTEFMT_RESULT       = 4050, 
    TDS_DESCRIBE_RESULT         = 4051, 
    TDS_DONE_RESULT             = 4052, 
    TDS_DONEPROC_RESULT         = 4053, 
    TDS_DONEINPROC_RESULT       = 4054, 
  }; 
 
  string server_data; 
  string last_error; 
  array(mapping(string:mixed)) column_info; 
 
  void tds_error(string msg, mixed ... args) 
  { 
    if (sizeof(args)) msg = sprintf(msg, @args); 
    predef::error(last_error = msg); 
  } 
 
  static object utf16enc = Locale.Charset.encoder("UTF16LE"); 
  static string string_to_utf16(string s) 
  { 
    return utf16enc->feed(s)->drain(); 
  } 
  static object utf16dec = Locale.Charset.decoder("UTF16LE"); 
  static string utf16_to_string(string s) 
  { 
    return utf16dec->feed(s)->drain(); 
  } 
 
  class Connection { 
    int major_version = DEF_MAJOR; 
    int minor_version = DEF_MINOR; 
    int port = DEF_PORT; 
    int block_size = 4096; 
    string server = ""; 
    string server_charset = ""; 
    string hostname = gethostname(); 
    string appname = ""; 
    string username = ""; 
    string password = ""; 
    string language = "";//"us_english"; 
    string library_name = "TDS-Library"; 
    string database = ""; 
    string domain; 
    string capabilities = DEFAULT_CAPABILITIES; 
 
    Stdio.File socket; 
 
#define FMT_SMALLINT  "%-2c" 
#define FMT_INT             "%-4c" 
#define FMT_INT8    "%-4c" 
 
    class InPacket 
    { 
      int inpos = 0; 
      string inbuf = ""; 
      int done; 
 
      static void fill_buf() 
      { 
        if (done) { 
          TDS_WERROR("Filling buffer on finished packet!\n" 
                     "%s\n", hex_dump(inbuf)); 
        } 
 
        string header = socket->read(8); 
        if (!header || sizeof(header) < 8) { 
          busy = !(done = 1); 
          tds_error("Failed to read packet header: %O, %s.\n", 
                    header, strerror(socket->errno())); 
        } 
        TDS_WERROR("Read header:\n%s\n", hex_dump(header)); 
        int packet_type; 
        int last_packet; 
        int len; 
        // NOTE: Network byteorder!! 
        sscanf(header, "%-1c%-1c%2c", packet_type, last_packet, len); 
        len -= 8; 
 
        busy = !(done = last_packet); 
 
        string data = socket->read(len); 
        if (!data || sizeof(data) < len) { 
          tds_error("Failed to read packet data (%d bytes), got %O (%d bytes), %s\n", 
                    len, data, sizeof(data||""), strerror(socket->errno())); 
        } 
        TDS_WERROR("Read packet with %d bytes.\n%s\n", 
                   sizeof(data), hex_dump(data)); 
        inbuf = inbuf[inpos..] + data; 
        inpos = 0; 
      } 
 
      string get_raw(int bytes) 
      { 
        while (inpos + bytes > sizeof(inbuf)) { 
          fill_buf(); 
        } 
        string raw = inbuf[inpos..inpos + bytes - 1]; 
        inpos += bytes; 
        return raw; 
      } 
 
      string peek_raw(int bytes) 
      { 
        while (inpos + bytes > sizeof(inbuf)) { 
          fill_buf(); 
        } 
        string raw = inbuf[inpos..inpos + bytes - 1]; 
        return raw; 
      } 
 
      int get_int8() 
      { 
        return array_sscanf(get_raw(8), "%-8c")[0]; 
      } 
      int get_int() 
      { 
        return array_sscanf(get_raw(4), "%-4c")[0]; 
      } 
      int get_int_be() 
      { 
        return array_sscanf(get_raw(4), "%4c")[0]; 
      } 
      int get_smallint() 
      { 
        return array_sscanf(get_raw(2), "%-2c")[0]; 
      } 
      int get_byte() 
      { 
        return array_sscanf(get_raw(1), "%-1c")[0]; 
      } 
      int peek_byte() 
      { 
        return array_sscanf(peek_raw(1), "%-1c")[0]; 
      } 
 
      string get_string(int len) 
      { 
        if (!len) return ""; 
        TDS_WERROR("get_string(%d)...\n", len); 
        return utf16_to_string(get_raw(len*2)); 
      } 
 
      void expect(string s) 
      { 
        string r = get_raw(sizeof(s)); 
        if (r != s) { 
          tds_error("Expectation failed: Got %O, expected %O\n", 
                    r, s); 
        } 
      } 
 
      static void create() 
      { 
        if (!busy) { 
          TDS_WERROR("Creating input packet on idle connection!\n"); 
        } 
      } 
    } 
 
    //static InPacket login_answer; 
 
    class Packet 
    { 
      array(string|int) segments = ({}); 
      array(string) strings = ({}); 
      int flags; 
 
      static void create(int|void flags) 
      { 
        this_program::flags = flags; 
      } 
 
      void put_int8(int i) 
      { 
        segments += ({ sprintf("%-8c", i) }); 
      } 
      void put_int(int i) 
      { 
        segments += ({ sprintf("%-4c", i) }); 
      } 
      void put_smallint(int i) 
      { 
        segments += ({ sprintf("%-2c", i) }); 
      } 
      void put_byte(int i) 
      { 
        segments += ({ sprintf("%-1c", i) }); 
      } 
      void put_raw(string raw, int len) 
      { 
        if (sizeof(raw) != len) { 
          tds_error("Internal error: unexpected raw length: %O (expected %d)\n", 
                    raw, len); 
        } 
        segments += ({raw}); 
      } 
      void put_raw_string(string s) 
      { 
        if (1 || sizeof(s)) { 
          segments += ({ sizeof(strings) }); 
          strings += ({ s }); 
        } else { 
          segments += ({ sprintf("%-2c%-2c", 0, 0) }); 
        } 
      } 
      void put_string(string s) 
      { 
        put_raw_string(string_to_utf16(s)); 
      } 
      void put_long_raw_string(string s) 
      { 
        if (sizeof(s)) { 
          segments += ({ ~sizeof(strings) }); 
          strings += ({ s }); 
        } else { 
          segments += ({ sprintf("%-2c%-2c%-4c", 0, 0, 0) }); 
        } 
      } 
      void put_long_string(string s) 
      { 
        put_long_raw_string(string_to_utf16(s)); 
      } 
 
      void put_domain_login(string domain, string hostname) 
      { 
        string raw = sprintf("NTLMSSP\0s%-4c%-4c%-2c%-2c%-4c%-2c%-2c%-4c%s%s", 
                             1, 0xb201, 
                             sizeof(domain), sizeof(domain), 
                             32 + sizeof(hostname), 
                             sizeof(hostname), sizeof(hostname), 
                             32, 
                             hostname, domain); 
        segments += ({ sizeof(strings) }); 
        strings += ({ raw }); 
      } 
 
      mixed cast(string s) 
      { 
        int trailer_start = flags && 4; 
        foreach(segments, string|int seg) { 
          trailer_start += stringp(seg)?sizeof(seg):(seg<0)?8:4; 
        } 
        foreach(segments; int i; string|int seg) { 
          if (intp(seg)) { 
            if (seg < 0) { 
              seg = ~seg; 
              segments[i] = sprintf("%-2c%-2c%-4c", 
                                    sizeof(strings[seg]), 
                                    sizeof(strings[seg]), 
                                    trailer_start); 
              TDS_WERROR("Long string %O at offset %d\n", 
                         strings[seg], trailer_start); 
            } else { 
              segments[i] = sprintf("%-2c%-2c", 
                                    trailer_start, sizeof(strings[seg])/2); 
              TDS_WERROR("Short string %O at offset %d\n", 
                         strings[seg], trailer_start); 
            } 
            segments += ({ strings[seg] }); 
            trailer_start += sizeof(strings[seg]); 
          } 
        } 
        string res = segments * ""; 
        TDS_WERROR("Generated packet: %O (%d bytes)\n", 
                   res, sizeof(res)); 
        if (!flags) return res; 
        return sprintf("%-4c%s", sizeof(res)+4, res); 
      } 
    } 
 
    static void send_packet(Packet p, int flag, int|void last) 
    { 
      string raw = (string) p; 
 
      if (busy) { 
        TDS_WERROR("Sending packet on busy connection!\n"); 
      } 
      busy = last; 
 
      // FIXME: Split large packets! 
 
      // NOTE: Network byteorder!! 
      raw = 
        sprintf("%-1c%-1c%2c\0\0%-1c\0%s", 
                flag, last, 
                sizeof(raw) + 8, 
                1,              /* TDS 7 or 8. */ 
                (string)raw); 
      TDS_WERROR("Wrapped packet: %O\n%s\n", raw, hex_dump(raw)); 
      if (socket->write(raw) != sizeof(raw)) { 
        tds_error("Failed to send packet.\n" 
                  "raw: %O\n", raw); 
      } 
      //Stdio.write_file("packet.bin", raw); 
    } 
 
    static string crypt_pass(string password) 
    { 
      password = string_to_utf16(password); 
      password ^= "\x5a"*sizeof(password); 
      return (string)map((array)password, 
                         lambda(int byte) { 
                           return ((byte >> 4) | (byte << 4)) & 0xff; 
                         }); 
    } 
 
    static InPacket send_login() 
    { 
      password = password[..127]; 
 
      Packet p = Packet(1); 
 
      p->put_raw(TDS8VERSION, 4);       // TDS version 
      p->put_int(0);                    // block size 
      p->put_raw(CLIENTVERSION, 4);     // client version 
      p->put_int(getpid());             // pid 
      p->put_raw(CONNECTIONID, 4);      // connection identifier 
      p->put_byte(0x80|0x40|0x20);      // warnings + force select db 
      p->put_byte(3|(domain && 0x80));  // domain login 
      p->put_byte(0);                   // sql type 
      p->put_byte(0);                   // reserved 
      p->put_raw(TIMEZONE, 4);          // time zone 
      p->put_raw(COLLATION, 4);         // collation 
 
      p->put_string(hostname);          // hostname 
       
      if (domain) { 
        p->put_string(""); 
        p->put_string(""); 
      } else { 
        p->put_string(username);        // user name 
        p->put_raw_string(crypt_pass(password));        // password 
      } 
      p->put_string(appname); 
      p->put_string(server); 
      p->put_smallint(0);               // Unknown 
      p->put_smallint(0);               // Unknown 
      p->put_string(""); 
      p->put_string(language); 
      p->put_string(database); 
      p->put_raw("\0"*6, 6);                // MAC address 
      if (domain) { 
        p->put_domain_login(domain, hostname); 
      } else { 
        p->put_string(""); 
      } 
      p->put_string(""); 
       
      TDS_WERROR("Sending login packet.\n"); 
      send_packet(p, 0x10, 1); 
      return InPacket(); 
    } 
 
    string ecb_encrypt(string data, string key) 
    { 
#if constant(Crypto.DES) 
      Crypto.DES des = Crypto.DES(); 
      des->set_encrypt_key(des->fix_parity(key)); 
#else 
      Crypto.des des = Crypto.des(); 
      des->set_encrypt_key(Crypto.des_parity(key)); 
#endif 
      return des->crypt(data); 
    } 
 
    string encrypt_answer(string hash, string nonce) 
    { 
      return ecb_encrypt(nonce, hash[..6]) + 
        ecb_encrypt(nonce, hash[7..13]) + 
        ecb_encrypt(nonce, hash[14..]); 
    } 
 
    string answer_lan_mgr_challenge(string passwd, string nonce) 
    { 
      string magic = "KGS!@#$%"; 
 
      /* FIXME: Filter wide characters in passwd! */ 
      passwd = upper_case((passwd + "\0"*14)[..13]); 
      string hash = 
        ecb_encrypt(magic, passwd[..6]) + 
        ecb_encrypt(magic, passwd[7..]); 
      return encrypt_answer(hash, nonce); 
    } 
 
    string answer_nt_challenge(string passwd, string nonce) 
    { 
      string nt_passwd = string_to_utf16(passwd); 
#if constant(Crypto.MD4) 
      Crypto.MD4 md4 = Crypto.MD4(); 
#else 
      Crypto.md4 md4 = Crypto.md4(); 
#endif 
      md4->update(nt_passwd); 
      return encrypt_answer(md4->digest() + "\0"*16, nonce); 
    } 
 
    static void send_auth(string nonce) 
    { 
      int out_flag = 0x11; 
      Packet p = Packet(); 
      p->put_raw("NTLMSSP\0", 8); 
      p->put_int(3);            /* sequence 3 */ 
      p->put_long_raw_string(answer_lan_mgr_challenge(password, nonce)); 
      p->put_long_raw_string(answer_nt_challenge(password, nonce)); 
      p->put_long_string(domain); 
      p->put_long_string(username); 
      p->put_long_string(hostname); 
      p->put_long_string("");       /* Unknown */ 
      p->put_int(0x8201);       /* flags */ 
 
      TDS_WERROR("Sending auth packet.\n"); 
      send_packet(p, 0x11); 
    } 
 
    static void process_auth(InPacket inp) 
    { 
      int pdu_size = inp->get_smallint(); 
      if (pdu_size < 32) tds_error("Bad pdu size: %d\n", pdu_size); 
      inp->expect("NTLMSSP\0"); 
      inp->get_int();   /* sequence -> 2 */ 
      inp->get_int();   /* domain len * 2 */ 
      inp->get_int();   /* domain offset */ 
      inp->get_int();   /* flags */ 
      string nonce = inp->get_raw(8); 
      /* Discard context, target and data info */ 
      inp->get_raw(pdu_size - 32); 
      TDS_WERROR("Got nonce: %O\n", nonce); 
      send_auth(nonce); 
    } 
 
    static void process_msg(InPacket inp, int token_type) 
    { 
      TDS_WERROR("TDS_ERROR_TOKEN | TDS_INFO_TOKEN | TDS_EED_TOKEN\n"); 
      int len = inp->get_smallint(); 
      int no = inp->get_int(); 
      int state = inp->get_byte(); 
      int level = inp->get_byte(); 
      int is_error = 0; 
      int has_eed = 0; 
      switch(token_type) { 
      case TDS_EED_TOKEN: 
        TDS_WERROR("TDS_EED_TOKEN\n"); 
        if (level > 10) is_error = 1; 
 
        int state_len = inp->get_byte(); 
        string state = inp->get_raw(state_len); 
        has_eed = inp->get_byte(); 
        /* Ignore status and transaction state */ 
        inp->get_smallint(); 
        break; 
      case TDS_INFO_TOKEN: 
        TDS_WERROR("TDS_INFO_TOKEN\n"); 
        break; 
      case TDS_ERROR_TOKEN: 
        TDS_WERROR("TDS_ERROR_TOKEN\n"); 
        is_error = 1; 
        break; 
      } 
      string message = inp->get_string(inp->get_smallint()); 
      string server = inp->get_string(inp->get_byte()); 
      string proc_name = inp->get_string(inp->get_byte()); 
      int line = inp->get_smallint(); 
      if (has_eed) { 
        while (1) { 
          switch(inp->peek_byte()) { 
          case TDS5_PARAMFMT_TOKEN: 
          case TDS5_PARAMFMT2_TOKEN: 
          case TDS5_PARAMS_TOKEN: 
            process_default_tokens(inp, inp->get_byte()); 
            continue; 
          } 
          break; 
        } 
      } 
      if (is_error) { 
        tds_error("%d: %s:%s:%d %s\n", 
                  level, proc_name, server, line, message); 
      } else { 
        TDS_WERROR("%d: %s:%s:%d %s\n", 
                   level, proc_name, server, line, message); 
      } 
    } 
 
    static void process_env_chg(InPacket inp) 
    { 
      int size = inp->get_smallint(); 
      int env_type = inp->get_byte(); 
      if (env_type == TDS_ENV_SQLCOLLATION) { 
        size = inp->get_byte(); 
        string block = inp->get_raw(size); 
        if (size >= 5) { 
          string collation = block[..4]; 
          int lcid; 
          sscanf(collation, "%-3c", lcid); 
          /* FIXME: Should we care? */ 
        } 
        /* Discard old collation. */ 
        inp->get_raw(inp->get_byte()); 
        return; 
      } 
      string new_val = inp->get_string(inp->get_byte()); 
      string old_val = inp->get_string(inp->get_byte()); 
      switch(env_type) { 
      case TDS_ENV_PACKSIZE: 
        int new_block_size = (int)new_val; 
        TDS_WERROR("Change block size from %O to %O\n", old_val, new_val); 
        break; 
      case TDS_ENV_DATABASE: 
        TDS_WERROR("Database changed to %O\n", new_val); 
        break; 
      case TDS_ENV_LANG: 
        TDS_WERROR("Language changed to %O from %O\n", new_val, old_val); 
        break; 
      case TDS_ENV_CHARSET: 
        TDS_WERROR("Charset changed from %O to %O\n", new_val, old_val); 
        break; 
      } 
    } 
 
    int get_size_by_type(int col_type) 
    { 
      switch (col_type) { 
      case SYBINT1: 
      case SYBBIT: 
      case SYBBITN: 
        return 1; 
      case SYBINT2: 
        return 2; 
      case SYBINT4: 
      case SYBREAL: 
      case SYBDATETIME4: 
      case SYBMONEY4: 
        return 4; 
      case SYBINT8: 
      case SYBFLT8: 
      case SYBDATETIME: 
      case SYBMONEY: 
        return 8; 
      case SYBUNIQUE: 
        return 16; 
      default: 
        return -1; 
      } 
    } 
 
    int get_cardinal_type(int col_type) 
    { 
      switch (col_type) { 
      case XSYBVARBINARY: 
        return SYBVARBINARY; 
      case XSYBBINARY: 
        return SYBBINARY; 
      case SYBNTEXT: 
        return SYBTEXT; 
      case XSYBNVARCHAR: 
      case XSYBVARCHAR: 
        return SYBVARCHAR; 
      case XSYBNCHAR: 
      case XSYBCHAR: 
        return SYBCHAR; 
      } 
      return col_type; 
    } 
 
    int get_varint_size(int col_type) 
    { 
      switch (col_type) { 
      case SYBLONGBINARY: 
      case SYBTEXT: 
      case SYBNTEXT: 
      case SYBIMAGE: 
      case SYBVARIANT: 
        return 4; 
      case SYBVOID: 
      case SYBINT1: 
      case SYBBIT: 
      case SYBINT2: 
      case SYBINT4: 
      case SYBINT8: 
      case SYBDATETIME4: 
      case SYBREAL: 
      case SYBMONEY: 
      case SYBDATETIME: 
      case SYBFLT8: 
      case SYBMONEY4: 
      case SYBSINT1: 
      case SYBUINT2: 
      case SYBUINT4: 
      case SYBUINT8: 
        return 0; 
      case XSYBNCHAR: 
      case XSYBNVARCHAR: 
      case XSYBCHAR: 
      case XSYBVARCHAR: 
      case XSYBBINARY: 
      case XSYBVARBINARY: 
        return 2; 
      default: 
        return 1; 
      } 
    } 
 
    int is_unicode_type(int col_type) 
    { 
      return (col_type == XSYBNVARCHAR) || 
        (col_type == XSYBNCHAR) || 
        (col_type == SYBNTEXT); 
    } 
 
    int is_ascii_type(int col_type) 
    { 
      return (<XSYBCHAR,XSYBVARCHAR,SYBTEXT,SYBCHAR,SYBVARCHAR>)[col_type]; 
    } 
 
    int is_char_type(int col_type) 
    { 
      return is_unicode_type(col_type) || is_ascii_type(col_type); 
    } 
 
    int is_blob_type(int col_type) 
    { 
      return (col_type == SYBTEXT) || 
        (col_type == SYBIMAGE) || 
        (col_type == SYBNTEXT); 
    } 
 
    int is_collate_type(int col_type) 
    { 
      return (col_type==XSYBVARCHAR) || 
        (col_type==XSYBCHAR) || 
        (col_type==SYBTEXT) || 
        (col_type==XSYBNVARCHAR) || 
        (col_type==XSYBNCHAR) || 
        (col_type==SYBNTEXT); 
    } 
 
    int is_numeric_type(int col_type) 
    { 
      return (col_type == SYBNUMERIC) || (col_type == SYBDECIMAL); 
    } 
 
    mapping(string:mixed) tds7_get_data_info(InPacket inp) 
    { 
      mapping(string:mixed) res = ([ 
        "usertype":inp->get_smallint(), 
        "flags":inp->get_smallint(), 
        "column_type":inp->get_byte(), 
      ]); 
      res->nullable == res->flags & 0x01; 
      res->writeable == !!(res->flags & 0x08); 
      res->identity == !!(res->flags & 0x10); 
 
      res->cardinal_type = get_cardinal_type(res->column_type); 
      res->varint_size = get_varint_size(res->column_type); 
      TDS_WERROR("Intermediate info: %O\n", res); 
      switch(res->varint_size) { 
      case 0: 
        res->column_size = get_size_by_type(res->column_type); 
        break; 
      case 4: 
        res->column_size = inp->get_int(); 
        break; 
      case 2: 
        res->column_size = inp->get_smallint(); 
        break; 
      case 1: 
        res->column_size = inp->get_byte(); 
        break; 
      } 
      TDS_WERROR("Column size: %d bytes\n", res->column_size); 
 
      res->timestamp = (res->cardinal_type == SYBBINARY) || 
        (res->cardinal_type == TDS_UT_TIMESTAMP); 
 
      if (is_numeric_type(res->cardinal_type)) { 
        TDS_WERROR("is_numeric\n"); 
        res->column_prec = inp->get_byte(); 
        res->column_scale = inp->get_byte(); 
      } 
      if (is_collate_type(res->column_type)) { 
        TDS_WERROR("is_collate\n"); 
        inp->get_raw(4);        // Collation 
        inp->get_byte();        // charset 
      } 
      if (is_blob_type(res->cardinal_type)) { 
        TDS_WERROR("is_blob\n"); 
        res->table = inp->get_string(inp->get_smallint()); 
        TDS_WERROR("Table name: %O\n", res->table); 
      } 
      res->name = inp->get_string(inp->get_byte()); 
      TDS_WERROR("Column info: %O\n", res); 
      return res; 
    } 
 
    static void tds7_process_result(InPacket inp) 
    { 
      int num_cols = inp->get_smallint(); 
      if (num_cols == 0xffff) { 
        TDS_WERROR("No meta data.\n"); 
        column_info = 0; 
        return; 
      } 
      TDS_WERROR("%d columns in result.\n", num_cols); 
      column_info = allocate(num_cols); 
      //busy = 1; 
      int offset = 0; 
      foreach(column_info; int col; ) { 
        column_info[col] = tds7_get_data_info(inp); 
        column_info[col]->column_offset = offset; 
        TDS_WERROR("Offset: 0x%04x\n", offset); 
        if (is_numeric_type(column_info[col]->cardinal_type)) { 
          offset += 35;         // sizeof(TDS_NUMERIC) 
        } else if (is_blob_type(column_info[col]->cardinal_type)) { 
          offset += 32;         // sizeof(TDSBLOB) 
        } else { 
          offset += column_info[col]->column_size; 
        } 
        offset += (~(offset - 1) & 0x3);        // Align. 
        TDS_WERROR("Next offset: 0x%04x\n", offset); 
      } 
       
    } 
 
    static void process_default_tokens(InPacket inp, int token_type) 
    { 
      if (token_type == TDS_DONE_TOKEN) return; 
      switch(token_type) { 
      case TDS_DONE_TOKEN: 
        return; 
      case TDS_ERROR_TOKEN: 
      case TDS_INFO_TOKEN: 
      case TDS_EED_TOKEN: 
        process_msg(inp, token_type); 
        return; 
      case TDS_ENVCHANGE_TOKEN: 
        process_env_chg(inp); 
        return; 
      case TDS7_RESULT_TOKEN: 
        tds7_process_result(inp); 
        break; 
      case TDS5_DYNAMIC_TOKEN: 
      case TDS_LOGINACK_TOKEN: 
      case TDS_ORDERBY_TOKEN: 
      case TDS_CONTROL_TOKEN: 
      case TDS_TABNAME_TOKEN: 
        TDS_WERROR("Skipping token: %d\n", token_type); 
        inp->get_raw(inp->get_smallint()); 
        break; 
      default: 
        TDS_WERROR("FIXME: Got unknown token in process_default_tokens: %d\n", 
                   token_type); 
        break; 
      } 
    } 
 
    int process_result_tokens(InPacket inp) 
    { 
      if (!busy) { 
        //return TDS_NO_MORE_RESULTS; 
      } 
      while (1) { 
        int token_type = inp->peek_byte(); 
        TDS_WERROR("Got result token %d\n", token_type); 
        switch(token_type) { 
        case TDS7_RESULT_TOKEN: 
          TDS_WERROR("TDS7_RESULT_TOKEN\n"); 
          inp->get_byte(); 
          tds7_process_result(inp); 
          if (inp->peek_byte() == TDS_TABNAME_TOKEN) { 
            TDS_WERROR("TDS_TABNAME_TOKEN\n"); 
            process_default_tokens(inp, inp->get_byte()); 
            if (inp->peek_byte() == TDS_COLINFO_TOKEN) { 
              TDS_WERROR("TDS_COLINFO_TOKEN\n"); 
              inp->get_byte(); 
              //process_colinfo();    // FIXME! 
            } 
          } 
          TDS_WERROR("==> TDS_ROWFMT_RESULT\n"); 
          return TDS_ROWFMT_RESULT; 
        case TDS_DONE_TOKEN: 
        case TDS_ROW_TOKEN: 
          TDS_WERROR("==> TDS_ROW_RESULT\n"); 
          return TDS_ROW_RESULT; 
        default: 
          TDS_WERROR("==> FIXME: process_result_tokens\n"); 
          // FALL_THROUGH 
        case TDS_ORDERBY_TOKEN: 
          inp->get_byte(); 
          process_default_tokens(inp, token_type); 
          return 0;         /***** FIXME:::::: *****/ 
          break; 
        } 
      } 
    } 
 
    static string|int get_data(InPacket inp, 
                               mapping(string:mixed) info, int col) 
    { 
      TDS_WERROR("get_data for column %d info:%O\n", col, info); 
      mapping blobinfo; 
      int colsize = 0; 
 
    outer: 
      switch(info->varint_size) { 
      case 4: 
        switch(info->cardinal_type) { 
        case SYBVARIANT: 
          int sz = inp->get_int(); 
          if (!sz) return 0;    // NULL; 
          return inp->get_raw(sz); 
        case SYBLONGBINARY: 
          colsize = inp->get_int(); 
          break outer; 
        } 
        int len = inp->get_byte(); 
        if (len == 16) { 
          blobinfo = ([ 
            "textptr":inp->get_raw(16), 
            "timestamp":inp->get_raw(8), 
          ]); 
          colsize = inp->get_int(); 
          TDS_WERROR("BLOB: size:%d info:%O\n", colsize, blobinfo); 
        } 
        break; 
      case 2: 
        colsize = inp->get_smallint(); 
        if (!colsize) { 
          // Empty string. 
          return ""; 
        } 
        if (colsize == 0xffff) colsize = 0; 
        break; 
      case 1: 
        colsize = inp->get_byte(); 
        break; 
      case 0: 
        colsize = get_size_by_type(info->cardinal_type); 
        break; 
      } 
      TDS_WERROR("Column size is %d\n", colsize); 
      if (!colsize) return 0;   // NULL. 
      if (is_numeric_type(info->cardinal_type)) { 
        // NUMERIC 
        string arr = inp->get_raw(colsize); 
        TDS_WERROR("NUMERIC: %O\n", arr); 
        return arr; // FIXME 
      } else if (is_blob_type(info->cardinal_type)) { 
        // BLOB 
        string raw = inp->get_raw(colsize); 
        TDS_WERROR("BLOB. colsize:%d, raw: %O\n", colsize, raw); 
        if (is_char_type(info->cardinal_type)) { 
          return utf16_to_string(raw); 
        } 
        return raw; 
      } else { 
        string raw = inp->get_raw(colsize); 
        TDS_WERROR("Got raw data: %O\ncolumn_size:%d colsize:%d\n%s\n", 
                   raw, info->column_size, colsize, hex_dump(raw)); 
        if (is_unicode_type(info->column_type)) { 
          raw = utf16_to_string(raw); 
        } 
        if (colsize < info->column_size) { 
          // Handle padding. 
          switch(info->cardinal_type) { 
          case SYBLONGBINARY: 
            // FIXME: ?? 
            break; 
          case SYBCHAR: 
          case XSYBCHAR: 
            raw += " "*(info->column_size - colsize); 
            break; 
          case SYBBINARY: 
          case XSYBBINARY: 
            raw += "\0"*(info->column_size - colsize); 
            break; 
          } 
        } 
        if (info->cardinal_type == SYBDATETIME4) { 
          TDS_WERROR("Datetime4:%{ %d}\n", (array)raw); 
        } 
        TDS_WERROR("ROW: %O\n", raw); 
        return raw; 
      } 
    } 
 
    static string|int convert(string|int raw, mapping(string:mixed) info) 
    { 
      if (!raw) return raw; /* NULL */ 
      int cardinal_type; 
      switch(cardinal_type = info->cardinal_type) { 
      case SYBCHAR: 
      case SYBVARCHAR: 
      case SYBTEXT: 
      case XSYBCHAR: 
      case XSYBVARCHAR: 
      case SYBNVARCHAR: 
      case SYBNTEXT: 
      case SYBLONGBINARY: 
      case SYBBINARY: 
      case SYBVARBINARY: 
      case SYBIMAGE: 
      case XSYBBINARY: 
      case XSYBVARBINARY: 
        return raw; 
      case SYBMONEY4: 
        { 
          int val; 
          sscanf(raw, "%-4c", val); 
          if (val < 0) { 
            int cents = -(val/50 - 1)/2; 
            return sprintf("-%d.%02d", cents/100, cents%100); 
          } else { 
            int cents = (val/50 + 1)/2; 
            return sprintf("%d.%02d", cents/100, cents%100); 
          } 
        } 
      case SYBMONEY: 
        { 
          int val; 
          string sgn = ""; 
          sscanf(raw, "%-8c", val); 
          if (val < 0) { 
            sgn = "-"; 
            val = -val; 
          } 
          int cents = (val + 50)/100; 
          return sprintf("%s%d.%02d", sgn, cents/100, cents%100); 
        } 
      case SYBNUMERIC: 
      case SYBDECIMAL: 
      case SYBREAL: 
      case SYBFLT8: 
      case SYBUNIQUE: 
      default: 
        // FIXME: 
        TDS_WERROR("Not yet supported: %d\n", info->cardinal_type); 
        return raw; 
      case SYBBIT: 
      case SYBBITN: 
        return raw[0]?"1":"0"; 
      case SYBINT1: 
      case SYBINT2: 
      case SYBINT4: 
      case SYBINT8: 
      case SYBINTN: 
        { 
          int val; 
          if (raw[-1]& 0x80) { 
            // Negative. 
            val = ~array_sscanf((~raw) + ("\x00"*8), "%-8c")[0]; 
          } else { 
            val = array_sscanf(raw + ("\x00"*8), "%-8c")[0]; 
          } 
          TDS_WERROR("Converted %O ==> %d\n", raw, val); 
          return sprintf("%d", val); 
        } 
      case SYBDATETIMN: 
        if (sizeof(raw) == 8) cardinal_type = SYBDATETIME; 
        // FALL_THROUGH 
      case SYBDATETIME: 
      case SYBDATETIME4: 
        { 
          int day, min, sec, sec_300; 
          if (cardinal_type == SYBDATETIME) { 
            sscanf(raw, "%-4c%-4c", day, sec_300); 
            sec = sec_300/300; 
            sec_300 %= 300; 
            min = sec/60; 
            sec %= 60; 
          } else { 
            sscanf(raw, "%-2c%-2c", day, min); 
          } 
          int hour = min/60; 
          min %= 60; 
 
          int l = day + 146038; 
          int century = (l * 4)/146097; 
          l -= (century*146097 + 3)/4; 
          int year = ((l + 1)*4000)/1461001; 
          l -= (year * 1461)/4; 
          int yday = (l > 306)?(l - 305):(l + 60); 
          l += 31; 
          int j = (l * 80)/2447; 
          int mday = l - (j * 2447)/80; 
          l = j/11; 
          int mon = j + 1 - l*12; 
          year += (century + 15)*100 + l; 
          if (!l && !(year & 3) && ((year % 100) || !(year % 400))) 
            yday++; 
          return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", 
                         year, mon, mday, hour, min, sec); 
        } 
      } 
      return raw; 
    } 
 
    static array(string|int) process_row(InPacket inp) 
    { 
      if (!column_info) return 0; 
      array(string|int) res = allocate(sizeof(column_info)); 
      foreach(column_info; int i; mapping(string:mixed) info) { 
        res[i] = convert(get_data(inp, info, i), info); 
      } 
      return res; 
    } 
 
    array(string|int) process_row_tokens(InPacket inp, 
                                         int|void leave_end_token) 
    { 
      //if (!busy) return 0;  // No more rows. 
      while (1) { 
        int token_type = inp->peek_byte(); 
        TDS_WERROR("Process row tokens: Token type: %d\n", token_type); 
        switch(token_type) { 
        case TDS_RESULT_TOKEN: 
        case TDS_ROWFMT2_TOKEN: 
        case TDS7_RESULT_TOKEN: 
          // Some other result starts here. 
          //busy = 0; 
          return 0; 
        case TDS_ROW_TOKEN: 
          inp->get_byte(); 
          return process_row(inp); 
          break; 
        case TDS_DONE_TOKEN: 
        case TDS_DONEPROC_TOKEN: 
        case TDS_DONEINPROC_TOKEN: 
          if (!leave_end_token) inp->get_byte(); 
          //busy = 0; 
          return 0; 
        default: 
          inp->get_byte(); 
          process_default_tokens(inp, token_type); 
          break; 
        } 
      } 
    } 
 
    static void process_login_tokens(InPacket inp) 
    { 
      int ok = 0; 
      int token_type; 
      do { 
        token_type = inp->get_byte(); 
        TDS_WERROR("Got token: %d\n", token_type); 
        switch(token_type) { 
        case TDS_DONE_TOKEN: 
          TDS_WERROR("TDS_DONE_TOKEN\n"); 
          break; 
        case TDS_AUTH_TOKEN: 
          TDS_WERROR("TDS_AUTH_TOKEN\n"); 
          process_auth(inp); 
          break; 
        case TDS_LOGINACK_TOKEN: 
          TDS_WERROR("TDS_LOGINACK_TOKEN\n"); 
          int len = inp->get_smallint(); 
          int ack = inp->get_byte(); 
          int major = inp->get_byte(); 
          int minor = inp->get_byte(); 
          inp->get_smallint();  /* Ignored. */ 
          inp->get_byte();              /* Product name len */ 
          server_product_name = inp->get_string((len-10)/2); 
          int product_version = inp->get_int_be(); 
          if ((major == 4) && (minor == 2) && 
              ((product_version & 0xff0000ff) == 0x5f0000ff)) { 
            // Workaround for bugs in MSSQL 6.5 & 7.0 for TDS 4.2. 
            product_version = (product_version & 0xffff00) | 0x800000000; 
          } 
           
          // TDS 5.0 reports 5 on success 6 on fail. 
          // TDS 4.2 reports 1 on success and N/A on failure. 
          if ((ack == 5) || (ack == 1)) ok = 1; 
          TDS_WERROR("  ok:%d ack:%d %s major:%d minor:%d version:%08x\n", 
                     ok, ack, 
                     server_product_name, major, minor, product_version); 
          server_product_name = (server_product_name/"\0")[0]; 
          server_data = sprintf("%s %d.%d.%d.%d", 
                                server_product_name, 
                                product_version>>24, 
                                product_version & 0xffffff, 
                                major, minor); 
          break; 
        default: 
          process_default_tokens(inp, token_type); 
          break; 
        } 
      } while (token_type != TDS_DONE_TOKEN); 
      //if (!(spid = rows_affected)) set_spid(); 
      if (!ok) tds_error("Login failed.\n"); 
      TDS_WERROR("Login ok!\n"); 
    } 
 
    InPacket submit_query(compile_query query, void|array(mixed) params) 
    { 
      Packet p = Packet(); 
      if (!query->n_param || !params || !sizeof(params)) { 
        string raw = query->splitted_query*"?"; 
        p->put_raw(raw, sizeof(raw)); 
        send_packet(p, 0x01, 1); 
        return InPacket(); 
      } else { 
        tds_error("parametrized queries not supported yet.\n"); 
      } 
    } 
 
    static void disconnect() 
    { 
      socket->close(); 
      destruct(); 
    } 
 
    static void create(string server, int port, string database, 
                       string username, string auth) 
    { 
      string domain; 
      array(string) tmp = username/"\\"; 
      if (sizeof(tmp) > 1) { 
        // Domain login. 
        domain = tmp[0]; 
        username = tmp[1..]*"\\"; 
      } 
 
      this_program::server = server; 
      this_program::port = port; 
      this_program::database = database; 
      this_program::username = username; 
      this_program::password = auth; 
      this_program::domain = domain; 
 
      TDS_WERROR("Connecting to %s:%d version %d.%d\n", 
                 server, port, major_version, minor_version); 
 
      socket = Stdio.File(); 
      if (!socket->connect(server, port)) { 
        socket = 0; 
        tds_error("Failed to connect to %s:%d\n", server, port); 
      } 
      process_login_tokens(send_login()); 
    } 
  } 
 
  object(Connection) con; 
 
  int affected_rows; 
  string server_product_name = ""; 
 
  int busy; 
 
  void Disconnect() 
  { 
    con->disconnect(); 
    con = 0; 
  } 
 
  void Connect(string server, int port, string database, 
               string uid, string password) 
  { 
    con = Connection(server, port, database, uid, password); 
  } 
 
  Connection.InPacket Execute(compile_query query) 
  { 
    query->parse_prepared_query(); 
    if (busy) { 
      tds_error("Connection not idle.\n"); 
    } 
    Connection.InPacket res; 
    if (!query->params) { 
      res = con->submit_query(query); 
    } else { 
      res = con->submit_execdirect(query, query->params); 
    } 
    int done = 0; 
    int res_type; 
    while (!done) { 
      res_type = con->process_result_tokens(res); 
      switch(res_type) { 
      case TDS_COMPUTE_RESULT: 
      case TDS_ROW_RESULT: 
        done = 1; 
        break; 
      default: 
        TDS_WERROR("res_type: %d\n", res_type); 
        break; 
      } 
    } 
    // populate_ird 
    switch(res_type) { 
      //case TDS_NO_MORE_RESULTS: 
    } 
    return res; 
  } 
#if (__REAL_MAJOR__ > 7) || ((__REAL_MAJOR__ == 7) && (__REAL_MINOR__ >= 6)) 
}; 
#endif /* Pike 7.6 or later */ 
 
class compile_query 
{ 
  int n_param; 
  array(string) splitted_query; 
 
  array(mixed) params; 
 
  void parse_prepared_query() 
  { 
    // FIXME: 
  } 
 
  static array(string) split_query_on_placeholders(string query) 
  { 
    array(string) res = ({}); 
    int i; 
    int j = 0; 
    for (i = 0; i < sizeof(query); i++) { 
      switch(query[i]) { 
      case '\'': 
        i = search(query, "\'", i+1); 
        if (i == -1) { 
          TDS_WERROR("Bad quoting!\n"); 
          i = sizeof(query); 
        } 
        break; 
      case '\"': 
        i = search(query, "\"", i+1); 
        if (i == -1) { 
          TDS_WERROR("Bad quoting!\n"); 
          i = sizeof(query); 
        } 
        break; 
      case '[': 
        i = search(query, "]", i+1); 
        if (i == -1) { 
          TDS_WERROR("Bad quoting!\n"); 
          i = sizeof(query); 
        } 
        break; 
      case '-': 
        if (query[i..i+1] == "--") { 
          i = search(query, "\n", i+1); 
          if (i == -1) { 
            TDS_WERROR("Unterminated comment.\n"); 
            i = sizeof(query); 
          } 
        } 
        break; 
      case '/': 
        if (query[i..i+1] == "/*") { 
          i = search(query, "*/", i+2); 
          if (i == -1) { 
            TDS_WERROR("Unterminated comment.\n"); 
            i = sizeof(query); 
          } 
        } 
        break; 
      case '?': 
        res += ({ query[j..i-1] }); 
        j = i+1; 
        break; 
      } 
    } 
    res += ({ query[j..] }); 
    return map(res, string_to_utf16); 
  } 
 
  static void create(string query) 
  { 
    TDS_WERROR("Compiling query: %O\n", query); 
    splitted_query = split_query_on_placeholders(query); 
    n_param = sizeof(splitted_query)-1; 
  } 
} 
 
class big_query 
{ 
  static int row_no; 
  static int eot; 
 
  static Connection.InPacket result_packet; 
 
  int|array(string|int) fetch_row() 
  { 
    if (eot) return 0; 
    TDS_WERROR("fetch_row()::::::::::::::::::::::::::::::::::::::::::\n"); 
    int|array(string|int) res = con->process_row_tokens(result_packet); 
    eot = !res; 
    row_no++; 
    return res; 
  } 
 
  array(mapping(string:mixed)) fetch_fields() 
  { 
    TDS_WERROR("fetch_fields()::::::::::::::::::::::::::::::::::::::::::\n"); 
    return copy_value(column_info || ({})); 
  } 
 
  static void create(string|compile_query query) 
  { 
    if (stringp(query)) { 
      query = compile_query(query); 
    } 
    result_packet = Execute(query); 
  } 
} 
 
string server_info() 
{ 
  return server_data; 
} 
 
string error() 
{ 
  return last_error; 
} 
 
static void create(string|void server, string|void database, 
                   string|void user, string|void pwd) 
{ 
  if (con) { 
    Disconnect(); 
  } 
  int port = DEF_PORT; 
  if (server) { 
    array(string) tmp = server/":"; 
    if (sizeof(tmp) > 1) { 
      port = (int)tmp[-1]; 
      server = tmp[..sizeof(tmp)-2]*":"; 
    } 
  } else { 
    server = "127.0.0.1"; 
  } 
  Connect(server, port, database || "default", 
          user || "", pwd || ""); 
}