File: Dksesstr.c

package info (click to toggle)
virtuoso-opensource 6.1.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 260,060 kB
  • ctags: 123,765
  • sloc: ansic: 652,532; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (1717 lines) | stat: -rw-r--r-- 46,138 bytes parent folder | download | duplicates (2)
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
/*
 *  Dksesstr.c
 *
 *  $Id$
 *
 *  String sessions
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "Dk.h"
#include "libutil.h"
#include "Dksesstr.h"

char *ses_tmp_dir;

/* The string session

  The string session is a session offering the session_buffered_read/writer
  API. A string session has separate in and out buffers as well as a
  linked list of buffer elements. Each of these contains a buffer, a
  fill count and a read count.

  The string session has two modes of operation. Write and read. It needs to be
  full written before anything can be read from it.

  If the output in a string session is shorter than the out buffer no buffer
  elements are allocated. If there is more output, it is dumped into successive
  buffers linked together by the buffer_elt_t structure.

  Before a string session can be red it needs to be 'rewound' with
  the strses_rewind operation. After this no writes may be attempted.

  The following operations are available:

  strses_write - append a string to the session.
  strses_read - read a string from the session.
  strses_rewind - Initialize reading from the string session.
  strses_flush - Free all buffers and set an existing string session to be
		 reused for writing.
  strses_allocate - Make a new string session and make it  ready for writing.

  strses_free - deallocate all memory associated with a string session.

*/

long strses_file_reads = 0;
long strses_file_seeks = 0;
long strses_file_writes = 0;
long strses_file_wait_msec = 0;

long read_wides_from_utf8_file (dk_session_t * ses, long nchars, unsigned char *dest, int copy_as_utf8, unsigned char **dest_ptr_out);

OFF_T 
strf_lseek (strsestmpfile_t * sesfile, OFF_T offset, int whence)
{
  OFF_T ret;
  long start_time = get_msec_real_time ();
  strses_file_seeks ++;
  if (NULL != sesfile->ses_lseek_func)
    ret = sesfile->ses_lseek_func (sesfile, offset, whence);
  else
    ret = LSEEK (sesfile->ses_file_descriptor, offset, whence);
  strses_file_wait_msec += (get_msec_real_time () - start_time);
  return ret;
}

size_t 
strf_read (strsestmpfile_t * sesfile, void *buf, size_t nbyte)
{
  size_t ret;
  long start_time = get_msec_real_time ();
  strses_file_reads ++;
  if (NULL != sesfile->ses_read_func)
    ret = sesfile->ses_read_func (sesfile, buf, nbyte);
  else
    ret = read (sesfile->ses_file_descriptor, buf, nbyte);
  strses_file_wait_msec += (get_msec_real_time () - start_time);
  return ret;
}

static size_t 
strf_write (strsestmpfile_t * sesfile, const void *buf, size_t nbyte)
{
  size_t ret;
  strses_file_writes ++;
  if (NULL != sesfile->ses_wrt_func)
    ret = sesfile->ses_wrt_func (sesfile, buf, nbyte);
  else
    ret = write (sesfile->ses_file_descriptor, buf, nbyte);
  return ret;
}

/*
 *  Returns a pointer to a non-full buffer
 */
static buffer_elt_t *
DBG_NAME (strdev_get_buf) (DBG_PARAMS dk_session_t * ses)
{
  buffer_elt_t *buf = ses->dks_buffer_chain_tail;
  buffer_elt_t **last_ref = &ses->dks_buffer_chain_tail;
  buffer_elt_t *new_buf;
  strdevice_t *strdev = (strdevice_t *) ses->dks_session->ses_device;

  while (buf)
    {
      if (buf->fill < DKSES_OUT_BUFFER_LENGTH && !buf->space_exausted)
	return (buf);
      last_ref = &buf->next;
      buf = buf->next;
    }

  new_buf = (buffer_elt_t *) dk_alloc (sizeof (buffer_elt_t));
  new_buf->fill = 0;
  new_buf->read = 0;
  new_buf->fill_chars = 0;
  new_buf->space_exausted = 0;
  new_buf->data = (char *) DK_ALLOC (DKSES_OUT_BUFFER_LENGTH);
  new_buf->next = NULL;
  last_ref[0] = new_buf;
  if (NULL == ses->dks_buffer_chain)
    strdev->strdev_buffer_ptr = ses->dks_buffer_chain = ses->dks_buffer_chain_tail;
  else
    ses->dks_buffer_chain_tail = new_buf;
  return (new_buf);
}


#ifdef MALLOC_DEBUG
#define strdev_get_buf(ses) dbg_strdev_get_buf (__FILE__, __LINE__, ses)
#endif

static void
strdev_free_buf (buffer_elt_t * b, caddr_t arg)
{
  dk_free (b->data, DKSES_OUT_BUFFER_LENGTH);
  dk_free (b, sizeof (buffer_elt_t));
}


/* outputs the input buffer up to the last full utf8 char boundary */
/* utf8_in : the incoming (possibly not correct) utf8 string */
/* max_utf8_chars : the max length of the utf8_in */
/* out_buf : the buffer to hold the (correctly ending) utf8 */
/* max_out_buf : the max length of the out_buf */
/* nwc : if supplied, the out param ; number of wide chars stored in the wide buf */
/* space_exausted : if supplied, the out param ; marks exit due out of output space */
/* return value : the number of utf8 bytes actually stored into the output buf */
static size_t
strdev_round_utf8_partial_string (
    const unsigned char *utf8_in,
    size_t max_utf8_chars,
    unsigned char *out_buf,
    size_t max_out_buf,
    size_t * pnwc,
    int *space_exausted)
{
  size_t written = 0;
  size_t nwc = 0;
  virt_mbstate_t ps;
  memset (&ps, 0, sizeof (ps));
  while (written < max_out_buf && max_utf8_chars)
    {
      size_t utf_char_len = virt_mbrtowc (NULL, utf8_in, max_utf8_chars, &ps);
      if (utf_char_len == (size_t) - 1)
	return (size_t) - 1;

      if (utf_char_len <= max_out_buf - written)
	{
	  memcpy (out_buf, utf8_in, utf_char_len);
	  out_buf += utf_char_len;
	  written += utf_char_len;
	  nwc++;
	}
      else
	{
	  if (space_exausted)
	    *space_exausted = 1;
	  break;
	}
      max_utf8_chars -= utf_char_len;
      utf8_in += utf_char_len;
    }
  if (written == max_out_buf && space_exausted)
    *space_exausted = 1;
  if (pnwc)
    *pnwc = nwc;
  return written;
}


int
utf8_align_memcpy (void *dst, const void *src, size_t len, size_t * pnwc, int *space_exausted)
{
  size_t ret = strdev_round_utf8_partial_string ((const unsigned char *) src, len,
      (unsigned char *) dst, len,
      pnwc,
      space_exausted);
  if (ret == (size_t) - 1)
    return -1;
  else
    return (int) ret;
}


/*##**********************************************************************
 *
 *		strdev_write
 *
 * Append a string to the string session
 * This allocates a buffer of a preset length and fills it uo to its
 * size. Higher levels will call this several times to append long strings.
 *
 * Input params :	  - The string session, the string to append
 &		       - the number of bytes to append.
 *
 * Output params:    - none
 *
 * Return value :    the number of bytes effectively added to the string session.
 *
 * Limitations  :   - The algorithm is poor, the session needs to be in write mode.
 *
 * Globals used :
 */
static int
strdev_write (session_t * ses2, char *buffer, int bytes)
{
  dk_session_t *ses = SESSION_DK_SESSION (ses2);
  int filled;
  buffer_elt_t *buf = NULL;
  long space = 0;
  strdevice_t *strdev = (strdevice_t *) ses2->ses_device;

  if (ses2->ses_file->ses_file_descriptor)
    {
      int written = 0;
      OFF_T fill;

      fill = strf_lseek (ses2->ses_file, 0, SEEK_END);
      if (fill == -1)
	{
	  SESSTAT_SET (ses2, SST_DISK_ERROR);
	  log_error ("Can't seek in file %s", ses2->ses_file->ses_temp_file_name);
	  return 0;
	}
      else
	{
	  written = strf_write (ses2->ses_file, buffer, bytes);
	  if (bytes != written)
	    {
	    report_error:
	      SESSTAT_SET (ses2, SST_DISK_ERROR);
	      log_error ("Can't write to file %s", ses2->ses_file->ses_temp_file_name);
	      return 0;
	    }
	  ses2->ses_file->ses_fd_fill = fill + written;
	  if (strdev->strdev_is_utf8)
	    {
	      virt_mbstate_t mb;
	      size_t len;
	      unsigned char *buf = (unsigned char *) buffer;
	      memset (&mb, 0, sizeof (mb));
	      len = virt_mbsnrtowcs (NULL, &buf, written, 0, &mb);
	      if (len == -1)
		goto report_error;
	      ses2->ses_file->ses_fd_fill_chars += len;
	    }
	  else
	    ses2->ses_file->ses_fd_fill_chars = ses2->ses_file->ses_fd_fill;
	}
      return written;
    }

  buf = strdev_get_buf (ses);
  space = DKSES_OUT_BUFFER_LENGTH - buf->fill;

  if (ses2->ses_file->ses_max_blocks_in_mem && buf->fill == 0 && buf->read == 0)
    {
      ses2->ses_file->ses_max_blocks_in_mem--;
      if (!ses2->ses_file->ses_max_blocks_in_mem)
	{
	  char fname[PATH_MAX + 1];
	  snprintf (fname, sizeof (fname), "%s/sesXXXXXX", ses_tmp_dir);
	  mktemp (fname);

#if defined (WIN32)
# define OPEN_FLAGS  	  O_CREAT | O_RDWR | O_BINARY | O_EXCL | O_TEMPORARY
#elif defined (FILE64)
# define OPEN_FLAGS       O_RDWR | O_CREAT | O_BINARY | O_EXCL | O_LARGEFILE
#else
# define OPEN_FLAGS       O_RDWR | O_CREAT | O_BINARY | O_EXCL
#endif

#ifdef WIN32
	  ses2->ses_file->ses_file_descriptor = _open (fname, OPEN_FLAGS, 0600);
#else
	  ses2->ses_file->ses_file_descriptor = open (fname, OPEN_FLAGS, 0600);
	  unlink (fname);
#endif
	  if (ses2->ses_file->ses_file_descriptor < 0)
	    {
	      SESSTAT_SET (ses2, SST_DISK_ERROR);
	      log_error ("Can't open file %s, error %d", fname, errno);
	      ses2->ses_file->ses_file_descriptor = 0;
	    }
	  else
	    ses2->ses_file->ses_temp_file_name = box_dv_short_string (fname);
	  ses2->ses_file->ses_fd_fill = ses2->ses_file->ses_fd_read = 0;
	}
    }

  SESSTAT_SET (ses->dks_session, SST_OK);
  if (strdev->strdev_in_read && !buf->fill && bytes >= ses->dks_out_length && !buf->read && ses2->ses_device)
    {
      buf->read = strdev->strdev_in_read;
      strdev->strdev_in_read = 0;
    }
  if (strdev->strdev_is_utf8)
    {
      size_t nwc = 0;
      int space_exausted = 0;

      filled = strdev_round_utf8_partial_string (
	    (unsigned char *) buffer,
	    bytes,
	    (unsigned char *) &buf->data[buf->fill],
	    space,
	    &nwc,
	    &space_exausted);
      if (filled == (size_t) - 1)
	{
	  SESSTAT_SET (ses2, SST_DISK_ERROR);
	  SESSTAT_CLR (ses2, SST_OK);
	  log_error ("Invalid UTF-8 data in writing utf8 into a session");
#ifndef NDEBUG
	  GPF_T;
#endif
	  return -1;
	}
      buf->space_exausted = space_exausted;
      buf->fill_chars += (int) nwc;
    }
  else
    {
      memcpy (&buf->data[buf->fill], buffer, filled = MIN (bytes, space));
      buf->fill_chars += filled;
    }
  buf->fill += filled;
  return (filled);
}


/*##**********************************************************************
 *
 *		strdev_read
 *
 * Reads a string from a string session. This will read only as many bytes
 ( as are contiguously available in the string session. Higher levels will
 * call this several times to read long strings.
 *
 * Input params :	  - the string session, the target buffer, the count
 *		       - of bytes desired.
 *
 * Output params:    -
 *
 * Return value :    The number of bytes copied nto the target buffer.
 *
 * Limitations  :  The session needs to in read mode (c.f. strses_rewind)
 *
 * Globals used :
 */
static int
strdev_read (session_t * ses2, char *buffer, int bytes)
{
  dk_session_t *ses = SESSION_DK_SESSION (ses2);
  strdevice_t *strdev = (strdevice_t *) ses->dks_session->ses_device;
  /* the first data to read will be in the first element of the buffer
     chain. When the buffer chain is empty the data will be in the
     regular output buffer. */
  if (strdev->strdev_buffer_ptr)
    {
      /* take as much as needed from the first buffer */
      buffer_elt_t *buf = strdev->strdev_buffer_ptr;
      int count = MIN (buf->fill - buf->read, bytes);
      memcpy (buffer, buf->data + buf->read, count);
      buf->read += count;
      if (buf->read == buf->fill)
	{
	  strdev->strdev_buffer_ptr = buf->next;
	}
      return (count);
    }
  else if (ses2->ses_file->ses_file_descriptor &&
  	   ses2->ses_file->ses_fd_read < ses2->ses_file->ses_fd_fill)
    {
      if (-1 == strf_lseek (ses2->ses_file, ses2->ses_file->ses_fd_read, SEEK_SET))
	{
	  SESSTAT_SET (ses2, SST_DISK_ERROR);
	  log_error ("Can't seek in file %s", ses2->ses_file->ses_temp_file_name);
	  return 0;
	}
      else
	{
	  int to_read = MIN (bytes, ses2->ses_file->ses_fd_fill - ses2->ses_file->ses_fd_read);
	  int readed;

	  readed = strf_read (ses2->ses_file, buffer, to_read);
	  if (readed > 0)
	    ses2->ses_file->ses_fd_read += readed;
	  else if (readed < 0)
	    {
	      log_error ("Can't read from file %s", ses2->ses_file->ses_temp_file_name);
	      SESSTAT_SET (ses2, SST_DISK_ERROR);
	    }
	  return readed;
	}
    }
  else
    {
      /* The data to be read is in the out buffer. Set the in buffer to
         be the out buffer, the read count to zero and the fill to the
         out buffer fill. Return the fill. */
      int count = MIN (ses->dks_out_fill - strdev->strdev_in_read, bytes);
      memcpy (buffer, ses->dks_out_buffer + strdev->strdev_in_read, count);
      strdev->strdev_in_read += count;
      return (count);
    }
}


static int
strdev_free (device_t * dev)
{
  dk_free (dev->dev_funs, sizeof (devfuns_t));
  dk_free (dev, sizeof (strdevice_t));
  return (0);
}


#ifdef never
void
not_applicable ()
{
  printf ("Attempt to call a session function not available on string sessions");
#ifndef DLL
  fflush (stdout);
#endif
  call_exit (1);
}
#endif


/*##**********************************************************************
 *
 *		strdev_allocate
 *
 * Function used for allocating and initializing a new string output
 * session.
 *
 * Input params :	  - none
 *
 * Output params:    - none
 *
 * Return value :    pointer to new session instance
 *
 * Limitations  :
 *
 * Globals used :
 */
device_t *
DBG_NAME (strdev_allocate) (DBG_PARAMS_0)
{
  strdevice_t *strdev = (strdevice_t *) dk_alloc (sizeof (strdevice_t));
  device_t *dev = (device_t *) strdev;
  devfuns_t *devfuns = (devfuns_t *) dk_alloc (sizeof (devfuns_t));

  /* Initialize pointers */
  dev->dev_funs = devfuns;

  /* Set string session methods */
  dev->dev_funs->dfp_free = strdev_free;
#ifdef never
  dev->dev_funs->dfp_allocate = not_applicable;
  dev->dev_funs->dfp_set_address = not_applicable;
  dev->dev_funs->dfp_listen = not_applicable;
  dev->dev_funs->dfp_accept = not_applicable;
  dev->dev_funs->dfp_connect = not_applicable;
  dev->dev_funs->dfp_disconnect = not_applicable;
  dev->dev_funs->dfp_set_control = not_applicable;
#endif
  dev->dev_funs->dfp_read = strdev_read;
  dev->dev_funs->dfp_write = strdev_write;

  strdev->strdev_in_read = 0;
  strdev->strdev_buffer_ptr = NULL;
  strdev->strdev_is_utf8 = 0;
  return (dev);
}


#if 0
void
strses_rewind (dk_session_t * ses)
{
  /* No place you wanna be */
  GPF_T;

  /* In read mode the out buffer length serves as a count of bytes
     read from the out buffer. The out buffer fill is the length of the buffer
   */
  if (!ses->dks_in_buffer)
    {
      ses->dks_in_buffer = (char *) dk_alloc (DKSES_IN_BUFFER_LENGTH);
    }
  ses->dks_out_length = 0;
}
#endif


void
strses_map (dk_session_t * ses, void (*func) (buffer_elt_t * e, caddr_t arg), caddr_t arg)
{
  buffer_elt_t *buf = ses->dks_buffer_chain;
  buffer_elt_t *next;
  while (buf)
    {
      next = buf->next;
      func (buf, arg);
      buf = next;
    }
}


void
strses_file_map (dk_session_t * ses, void (*func) (buffer_elt_t * e, caddr_t arg), caddr_t arg)
{
  buffer_elt_t elt;
  unsigned char buffer[DKSES_IN_BUFFER_LENGTH];
  OFF_T offset;
  strsestmpfile_t *sesfile = (strsestmpfile_t *) ses->dks_session->ses_file;

  if (sesfile->ses_file_descriptor)
    {
      if (-1 == strf_lseek (sesfile, 0, SEEK_SET))
	{
	  log_error ("Can't seek in file %s", sesfile->ses_temp_file_name);
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  return;
	}
      offset = 0;
      do
	{
	  int readbytes;

	  memset (&elt, 0, sizeof (elt));
	  elt.data = (char *) buffer;
	  readbytes = strf_read (sesfile, buffer,
		MIN (DKSES_IN_BUFFER_LENGTH, sesfile->ses_fd_fill - offset));
	  if (readbytes == -1)
	    {
	      log_error ("Can't read from file %s", sesfile->ses_temp_file_name);
	      SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	      return;
	    }
	  elt.fill = readbytes;
	  func (&elt, arg);
	  offset += readbytes;
	}
      while (offset < sesfile->ses_fd_fill);
    }
}


void
strses_flush (dk_session_t * ses)
{
  strdevice_t *strdev = (strdevice_t *) ses->dks_session->ses_device;
  strsestmpfile_t *sesfile = (strsestmpfile_t *) ses->dks_session->ses_file;
  strses_map (ses, strdev_free_buf, NULL);
  ses->dks_buffer_chain = ses->dks_buffer_chain_tail = strdev->strdev_buffer_ptr = NULL;
  ses->dks_out_fill = strdev->strdev_in_read = 0;
  ses->dks_out_length = DKSES_OUT_BUFFER_LENGTH;
  ses->dks_bytes_sent = 0;
  if (ses->dks_in_buffer)
    {
      ses->dks_in_length = DKSES_OUT_BUFFER_LENGTH;
      ses->dks_in_fill = ses->dks_in_read = 0;
    }
  if (sesfile->ses_file_descriptor)
    {
      if (sesfile->ses_close_func)
	{
	  if (sesfile->ses_close_func (sesfile))
	    {
	      SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	      log_error ("Can't close session tmp file");
	    }
	}
      else if (close (sesfile->ses_file_descriptor))
	{
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  log_error ("Can't close session tmp file");
	}
      sesfile->ses_file_descriptor = 0;
      sesfile->ses_file_ctx = NULL;
      sesfile->ses_fd_read = 0;
      sesfile->ses_fd_fill = 0;
      sesfile->ses_fd_fill_chars = 0;
      dk_free_box (sesfile->ses_temp_file_name);
      sesfile->ses_max_blocks_in_mem = sesfile->ses_max_blocks_init;
    }
}


/*##**********************************************************************
 *
 *		strses_length
 *
 * Returns the count of bytes written this far into the string session.
 *
 * Input params :	  - The string session.
 *
 * Output params:    - none
 *
 * Return value :    The byte count.
 *
 * Limitations  :
 *
 * Globals used :
 */
int64
strses_length (dk_session_t * ses)
{
  int64 len = 0;
  buffer_elt_t *elt = ses->dks_buffer_chain;
  int fd = ses->dks_session->ses_file->ses_file_descriptor;
  while (elt)
    {
      len += elt->fill;
      elt = elt->next;
    }

  if (fd)
    {
      len += ses->dks_session->ses_file->ses_fd_fill;
    }

  return (len + ses->dks_out_fill);
}


int64
strses_chars_length (dk_session_t * ses)
{
  int64 len = 0;
  buffer_elt_t *elt = ses->dks_buffer_chain;
  int fd = ses->dks_session->ses_file->ses_file_descriptor;

  if (!strses_is_utf8 (ses))
    return strses_length (ses);

  while (elt)
    {
      len += elt->fill_chars;
      elt = elt->next;
    }

  if (fd)
    {
      len += ses->dks_session->ses_file->ses_fd_fill_chars;
    }


  if (ses->dks_out_fill)
    {
      size_t last_len;
      virt_mbstate_t mb;
      unsigned char *ptr = (unsigned char *) ses->dks_out_buffer;
      memset (&mb, 0, sizeof (mb));
      last_len = virt_mbsnrtowcs (NULL, &ptr, ses->dks_out_fill, 0, &mb);
      if (last_len != (size_t) - 1)
	len += (long) last_len;
    }
  return (len);
}


/*##**********************************************************************
 *
 *		strses_write_out
 *
 * Writes all data written into a string session into another session.
 *
 * Input params :
 *		ses    - The string session.
 *		out    - The session into which the data in the string
 *			 session is to be written.
 *
 * Output params:    - none
 *
 * Return value :
 *
 * Limitations  :
 *
 * Globals used :
 */
void
strses_write_out (dk_session_t * ses, dk_session_t * out)
{
  buffer_elt_t *elt = ses->dks_buffer_chain;
  strsestmpfile_t * ses_file = ses->dks_session->ses_file;

  while (elt)
    {
      session_flush_1 (out);
      session_buffered_write (out, elt->data, elt->fill);	/* was: service_write, there was error when we have smth in buffer */
      elt = elt->next;
    }
  if (ses_file->ses_file_descriptor)
    {
      char buffer[DKSES_IN_BUFFER_LENGTH];
      size_t readed, to_read;
      OFF_T end = strf_lseek (ses_file, 0, SEEK_END);
      if (end == -1)
	{
	  log_error ("Can't seek in file %s", ses_file->ses_temp_file_name);
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  return;
	}
      if (-1 == strf_lseek (ses_file, 0, SEEK_SET))
	{
	  log_error ("Can't seek in file %s", ses_file->ses_temp_file_name);
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  return;
	}
      while (end)
	{
	  to_read = end < DKSES_IN_BUFFER_LENGTH ? (size_t) end : DKSES_IN_BUFFER_LENGTH;
	  readed = strf_read (ses_file, buffer, to_read);
	  if (readed != to_read)
	    log_error ("Can't read from file %s", ses_file->ses_temp_file_name);
	  if (-1 == readed)
	    {
	      SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	    }
	  session_flush_1 (out);
	  session_buffered_write (out, buffer, to_read);
	  end = end - to_read;
	}
    }
  if (ses->dks_out_fill)
    session_buffered_write (out, ses->dks_out_buffer, ses->dks_out_fill);
}


static unsigned char *
strses_skip_wchars (unsigned char *data, long nbytes, long ofs)
{
  virt_mbstate_t mb;
  unsigned char *data_ptr = data;

  memset (&mb, 0, sizeof (mb));
  while (ofs)
    {
      size_t sz = virt_mbrtowc (NULL, data_ptr,
	  VIRT_MB_CUR_MAX, &mb);
      if (sz == (size_t) - 1)
	return NULL;
      data_ptr += sz;
      ofs--;
    }
  return data_ptr;
}


long
strses_cp_utf8_to_utf8 (unsigned char *dest_ptr, unsigned char *src_ptr, long src_ofs, long copy_chars, void *state_data)
{
  unsigned char *src_ptr_in;
  long copied_bytes;
  virt_mbstate_t mb;

  src_ptr = strses_skip_wchars (src_ptr, copy_chars * VIRT_MB_CUR_MAX, src_ofs);
  if (!src_ptr)
    GPF_T;


  /* copy copy_chars utf8 blocks into the dest */
  src_ptr_in = src_ptr;
  memset (&mb, 0, sizeof (mb));
  while (copy_chars)
    {
      size_t sz = virt_mbrtowc (NULL, src_ptr,
	  VIRT_MB_CUR_MAX, &mb);
      if (sz == (size_t) - 1)
	GPF_T;
      memcpy (dest_ptr, src_ptr, sz);
      dest_ptr += sz;
      src_ptr += sz;
      copy_chars--;
    }

  copied_bytes = (src_ptr - src_ptr_in);
  if (state_data)
    *((long *) state_data) += copied_bytes;
  return copied_bytes;
}


void
strses_serialize (caddr_t strses_box, dk_session_t * ses)
{
  dk_session_t *strses = (dk_session_t *) strses_box;
  long len = strses_length (strses);
  long char_len = strses_chars_length (strses);
  int is_utf8 = strses_is_utf8 (strses);

  if (len < 255)
    {
      session_buffered_write_char (is_utf8 ? DV_WIDE : DV_SHORT_STRING_SERIAL, ses);
      session_buffered_write_char (len & 0xff, ses);
      strses_write_out (strses, ses);
    }
  else if (len < (long) (MAX_READ_STRING / (is_utf8 ? (2 + sizeof (wchar_t)) : 1)))
    {
      session_buffered_write_char (is_utf8 ? DV_LONG_WIDE : DV_LONG_STRING, ses);
      print_long (len, ses);
      strses_write_out (strses, ses);
    }
  else
    {
      long ofs = 0;
      char buffer[64000];
      buffer_elt_t *elt = strses->dks_buffer_chain;
      long ver = cdef_param (ses->dks_caller_id_opts, "__SQL_CLIENT_VERSION", 0);
      if (ver && ver < 2724)
	{
	  if (ses->dks_session)
	    {
	    report_read_error:
	      SESSTAT_CLR (ses->dks_session, SST_OK);
	      SESSTAT_SET (ses->dks_session, SST_BROKEN_CONNECTION);
	      ses->dks_to_close = 1;
	      call_disconnect_callback_func (ses);
	      if (ses->dks_session->ses_class != SESCLASS_STRING &&
	      	  SESSION_SCH_DATA (ses) &&
		  SESSION_SCH_DATA (ses)->sio_write_fail_on)
		longjmp_splice (&SESSION_SCH_DATA (ses)->sio_write_broken_context, 1);
	    }
	  return;
	}
      /* WIRE PROTO: the tag */
      session_buffered_write_char (DV_STRING_SESSION, ses);
      /* WIRE PROTO: flags : b1 = is utf8 */
      session_buffered_write_char (is_utf8 ? 1 : 0, ses);
      while (elt)
	{
	  session_buffered_write_char (DV_LONG_STRING, ses);
	  print_long (elt->fill, ses);
	  session_buffered_write (ses, elt->data, elt->fill);
	  ofs += elt->fill_chars;
	  elt = elt->next;
	}
      while (ofs < char_len)
	{
	  int to_read_chars = MIN (((int) sizeof (buffer)) / (is_utf8 ? VIRT_MB_CUR_MAX : 1), char_len - ofs);
	  long copied_bytes;
	  if (is_utf8)
	    {
	      long copied_utf8_bytes = 0;
	      if (0 != strses_get_part_1 (strses, buffer, ofs, to_read_chars,
		    (copy_func_ptr_t) strses_cp_utf8_to_utf8, &copied_utf8_bytes))
		goto report_read_error;
	      copied_bytes = copied_utf8_bytes;
	    }
	  else
	    {
	      if (0 != strses_get_part (strses, buffer, ofs, to_read_chars))
		goto report_read_error;
	      copied_bytes = to_read_chars;
	    }
	  /* WIRE PROTO: i-th segment */
	  session_buffered_write_char (DV_LONG_STRING, ses);
	  print_long (copied_bytes, ses);
	  session_buffered_write (ses, buffer, copied_bytes);
	  ofs += to_read_chars;
	}

      /* WIRE PROTO: zero len terminating string */
      session_buffered_write_char (DV_SHORT_STRING_SERIAL, ses);
      session_buffered_write_char (0, ses);
    }
}


caddr_t
strses_deserialize (dk_session_t * session, dtp_t macro)
{
  unsigned char flags;
  dk_session_t *strses;

  MARSH_CHECK_BOX (strses = strses_allocate ());

#if 0
  /* 10M in mem */
  strses_enable_paging (strses, 1024 * 1024 * 10);
#endif

  /* WIRE PROTO: flags */
  flags = session_buffered_read_char (session);
  strses_set_utf8 (strses, flags & 0x1);

  while (1)
    {
      caddr_t str;

      /* WIRE PROTO: i-th segment */
      str = (caddr_t) scan_session_boxing (session);

      if (str && DV_TYPE_OF (str) != DV_STRING)
	{					 /* being paranoid is a good thing */
	  dk_free_tree (str);
	  sr_report_future_error (session, "", "Invalid data type of the incoming session segment");
	  str = NULL;
	}

      if (!str)
	dk_free_tree ((box_t) strses);

      MARSH_CHECK_BOX (str);

      /* WIRE PROTO: zero len terminating string */
      if (box_length (str) == 1)
	{
	  dk_free_box (str);
	  break;
	}

      session_buffered_write (strses, str, box_length (str) - 1);

      dk_free_box (str);
    }
  return (caddr_t) strses;
}

/* a bit dangerous : buffer must have space for whole session length */
void
strses_to_array (dk_session_t * ses, char *buffer)
{
  strsestmpfile_t * ses_file = ses->dks_session->ses_file;

  buffer_elt_t *elt = ses->dks_buffer_chain;
  while (elt)
    {
      memcpy (buffer, elt->data, elt->fill);
      buffer += elt->fill;
      elt = elt->next;
    }

  if (ses_file->ses_file_descriptor)
    {
      OFF_T rc, end = strf_lseek (ses_file, 0, SEEK_END);
      if (end == -1)
	{
	  log_error ("Can't seek in file %s", ses_file->ses_temp_file_name);
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  return;
	}
      if (-1 == strf_lseek (ses_file, 0, SEEK_SET))
	{
	  log_error ("Can't seek in file %s", ses_file->ses_temp_file_name);
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  return;
	}
      rc = strf_read (ses_file, buffer, end);
      if (rc != end)
	log_error ("Can't read from file %s", ses_file->ses_temp_file_name);
      if (rc == -1)
	SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
      buffer += end;
    }
  memcpy (buffer, ses->dks_out_buffer, ses->dks_out_fill);
}


size_t
strses_fragment_to_array (dk_session_t * ses, char *buffer, size_t fragment_offset, size_t fragment_size)
{
  strsestmpfile_t * ses_file = ses->dks_session->ses_file;
  buffer_elt_t *elt;
  size_t tail_size = fragment_size;
  for (elt = ses->dks_buffer_chain; elt && tail_size; elt = elt->next)
    {
      size_t cut_sz = elt->fill;
      char *data = elt->data;
      if (fragment_offset)
	{
	  if (cut_sz <= fragment_offset)
	    {
	      fragment_offset -= cut_sz;
	      continue;
	    }
	  data += fragment_offset;
	  cut_sz -= fragment_offset;
	  fragment_offset = 0;
	}
      if (cut_sz > tail_size)
	cut_sz = tail_size;
      memcpy (buffer, data, cut_sz);
      tail_size -= cut_sz;
      buffer += cut_sz;
    }
  if (ses_file->ses_file_descriptor && tail_size)
    {
      OFF_T rc, cut_sz = tail_size; /* if file is stream e.g. gzip, then we try to uncompress bytes number which is asked */
      if (!ses_file->ses_fd_is_stream) /* if it is regular file, we seek at the end */
	{
	  cut_sz = strf_lseek (ses_file, 0, SEEK_END);
	  if (cut_sz < 0 && !ses_file->ses_fd_is_stream)
	    {
	      log_error ("Can't seek in file %s", ses_file->ses_temp_file_name);
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  return 0;
	}
	}
      if ((unsigned) cut_sz <= fragment_offset)
	{
	  fragment_offset -= cut_sz;
	  goto end_of_file_read;
	}
      if (-1 == strf_lseek (ses_file, fragment_offset, SEEK_SET))
	{
	  log_error ("Can't seek in file %s", ses_file->ses_temp_file_name);
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  return 0;
	}
      cut_sz -= fragment_offset;
      fragment_offset = 0;
      if ((unsigned) cut_sz > tail_size)
	cut_sz = tail_size;
      rc = strf_read (ses_file, buffer, cut_sz);
      if (rc != cut_sz)
	log_error ("Can't read from file %s", ses_file->ses_temp_file_name);
      if (rc == -1)
	SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
      tail_size -= cut_sz;
      buffer += cut_sz;
    }

end_of_file_read:
  if (tail_size)
    {
      size_t cut_sz = ses->dks_out_fill;
      char *data = ses->dks_out_buffer;
      if (cut_sz <= fragment_offset)
	return 0;
      data += fragment_offset;
      cut_sz -= fragment_offset;
      if (cut_sz > tail_size)
	cut_sz = tail_size;
      memcpy (buffer, data, cut_sz);
      tail_size -= cut_sz;
    }
  return fragment_size - tail_size;
}


void
strses_enable_paging (dk_session_t * ses, int max_bytes_in_mem)
{
  int parts = max_bytes_in_mem / DKSES_IN_BUFFER_LENGTH;
  ses->dks_session->ses_file->ses_max_blocks_in_mem = parts ? parts : 1;
  ses->dks_session->ses_file->ses_max_blocks_init = parts ? parts : 1;
  if (!ses->dks_in_buffer)
    {
      ses->dks_in_buffer = (char *) dk_alloc (DKSES_OUT_BUFFER_LENGTH);
      ses->dks_in_length = DKSES_OUT_BUFFER_LENGTH;
    }
}


caddr_t
DBG_NAME (strses_string) (DBG_PARAMS dk_session_t * ses)
{
  int64 len = strses_length (ses);
  caddr_t box;
  if (NULL == (box = DBG_NAME (dk_alloc_box) (DBG_ARGS len + 1, DV_LONG_STRING)))
    return NULL;
  strses_to_array (ses, box);
  box[len] = 0;
  return box;
}


caddr_t
DBG_NAME (strses_wide_string) (DBG_PARAMS dk_session_t * ses)
{
  int64 len = strses_length (ses);
  caddr_t box;
  if (NULL == (box = DBG_NAME (dk_alloc_box) (DBG_ARGS len + sizeof (wchar_t), DV_WIDE)))
    return NULL;
  strses_to_array (ses, box);
  ((wchar_t *)(box+len))[0] = 0;
  return box;
}


caddr_t
t_strses_string (dk_session_t * ses)
{
  int64 len = strses_length (ses);
  caddr_t box;
  box = t_alloc_box (len + 1, DV_LONG_STRING);
  strses_to_array (ses, box);
  box[len] = 0;
  return box;
}


void
strses_free (dk_session_t * ses)
{
  dk_free_box ((box_t) ses);
}


int
strses_destroy (dk_session_t * ses)
{
#ifndef NDEBUG
  if (0 >= ses->dks_refcount)
    GPF_T1 ("Invalid dks_refcount in strses_destroy()");
#endif
  ses->dks_refcount--;
  if (ses->dks_refcount)
    return 1;
  strses_flush (ses);
  dk_free (ses->dks_out_buffer, ses->dks_out_length);
  if (ses->dks_in_buffer)
    dk_free (ses->dks_in_buffer, ses->dks_in_length);
  dk_free (SESSION_SCH_DATA (ses), sizeof (scheduler_io_data_t));
  session_free (ses->dks_session);
  return 0;
}


long
strses_get_part_1 (dk_session_t * ses, void *buf2, int64 starting_ofs, long nbytes, copy_func_ptr_t cpf, void *state_data)
{
  unsigned char *buffer = (unsigned char *) buf2;
  long copybytes;
  buffer_elt_t *elt = ses->dks_buffer_chain;
  strsestmpfile_t * ses_file = ses->dks_session->ses_file;

  while (elt && nbytes)
    {
      if (elt->fill_chars > starting_ofs)
	{
	  long dest_copybytes;
	  dest_copybytes = copybytes = MIN (elt->fill_chars - starting_ofs, nbytes);

	  if (cpf)
	    dest_copybytes = cpf (buffer, elt->data, starting_ofs, copybytes, state_data);
	  else
	    memcpy (buffer, elt->data + starting_ofs, copybytes);
	  buffer += dest_copybytes;
	  nbytes -= copybytes;
	  starting_ofs = 0;
	}
      else
	starting_ofs -= elt->fill_chars;
      elt = elt->next;
    }

  if (ses_file->ses_file_descriptor && nbytes)
    {
      if (ses_file->ses_fd_fill_chars > starting_ofs)
	{
	  if (strses_is_utf8 (ses))
	    {
	      int readed;
	      OFF_T skipchars;
	      unsigned char *buf2_out = buffer;

	      if (ses_file->ses_fd_curr_char_pos > starting_ofs ||
	          ses_file->ses_fd_curr_char_pos == 0)
		{				 /* if the file ptr is behind the requested one start from the beginning */
		  if (-1 == strf_lseek (ses_file, 0, SEEK_SET))
		    {
		      log_error ("Can't seek in file %s", ses_file->ses_temp_file_name);
		      SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
		      return 0;
		    }
		  skipchars = starting_ofs;
		  ses_file->ses_fd_curr_char_pos = 0;
		}
	      else
		skipchars = starting_ofs - ses_file->ses_fd_curr_char_pos;

	      /* now skip the skipchars */
	      if (-1 == read_wides_from_utf8_file (ses, skipchars, NULL, 0, NULL))
		{
		  ses->dks_session->ses_file->ses_fd_curr_char_pos = 0;
		  return 0;
		}
	      ses->dks_session->ses_file->ses_fd_curr_char_pos += skipchars;

	      /* it's time to read the actual data */
	      readed = read_wides_from_utf8_file (ses, nbytes, buffer, 1, &buf2_out);
	      if (-1 == readed)
		{
		  ses->dks_session->ses_file->ses_fd_curr_char_pos = 0;
		  return 0;
		}
	      ses->dks_session->ses_file->ses_fd_curr_char_pos += nbytes;

	      if (state_data)
		*((long *) state_data) += buf2_out - buffer;
	      buffer = buf2_out;
	      nbytes = readed;
	      starting_ofs = 0;
	    }
	  else
	    {
	      size_t readed;
	      size_t dest_readed;
	      if (-1 == strf_lseek (ses_file, starting_ofs, SEEK_SET))
		{
		  log_error ("Can't seek in file %s", ses_file->ses_temp_file_name);
		  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
		  return 0;
		}
	      if (cpf)
		{
		  long src_n_bytes = nbytes;
		  unsigned char src_buffer[64000];
		  long dest_copybytes;
		  dest_readed = 0;
		  do
		    {
		      long to_read = MIN (sizeof (buffer), src_n_bytes);
		      readed = strf_read (ses_file, src_buffer, to_read);
		      if (readed == -1)
			break;
		      dest_copybytes = cpf (buffer + dest_readed, src_buffer, 0, readed, state_data);
		      dest_readed += dest_copybytes;
		      src_n_bytes -= readed;
		    }
		  while (src_n_bytes);
		}
	      else
		readed = strf_read (ses_file, buffer, nbytes);
	      if (readed == -1)
		{
		  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
		  log_error ("Can't read from file %s", ses_file->ses_temp_file_name);
		  return 0;
		}
	      buffer += readed;
	      nbytes -= readed;
	      starting_ofs = 0;
	    }
	}
      else
	starting_ofs -= ses->dks_session->ses_file->ses_fd_fill_chars;
    }

  if (nbytes && ses->dks_out_fill)
    {
      long dest_copybytes;
      long last_len_chars;

      if (strses_is_utf8 (ses))
	{
	  virt_mbstate_t mb;
	  unsigned char *ptr = (unsigned char *) ses->dks_out_buffer;
	  memset (&mb, 0, sizeof (mb));
	  last_len_chars = virt_mbsnrtowcs (NULL, &ptr, ses->dks_out_fill, 0, &mb);
	  if (last_len_chars == (size_t) - 1)
	    GPF_T;
	}
      else
	last_len_chars = ses->dks_out_fill;

      if (last_len_chars > starting_ofs)
	{
	  dest_copybytes = copybytes = MIN (last_len_chars - starting_ofs, nbytes);
	  if (cpf)
	    dest_copybytes = cpf (buffer, ses->dks_out_buffer, starting_ofs, copybytes, state_data);
	  else
	    memcpy (buffer, ses->dks_out_buffer + starting_ofs, copybytes);
	  buffer += dest_copybytes;
	  nbytes -= copybytes;
	}
    }


  return nbytes;
}


long
strses_get_part (dk_session_t * ses, void *buf2, int64 starting_ofs, long nbytes)
{
  return strses_get_part_1 (ses, buf2, starting_ofs, nbytes, NULL, NULL);
}


long
read_wides_from_utf8_file (
    dk_session_t * ses,
    long nchars,
    unsigned char *dest,
    int copy_as_utf8,
    unsigned char **dest_ptr_out)
{
  unsigned char src_buffer[64000];
  virt_mbstate_t mb;

  memset (&mb, 0, sizeof (mb));
  while (nchars)
    {
      long to_read_bytes = MIN (sizeof (src_buffer), nchars * VIRT_MB_CUR_MAX);
      long readed;
      unsigned char *data_ptr = &src_buffer[0];
      size_t converted;

      /* read a buffer full */
      readed = strf_read (ses->dks_session->ses_file, src_buffer, to_read_bytes);
      if (-1 == readed)
	{
	  log_error ("Can't read in file %s", ses->dks_session->ses_file->ses_temp_file_name);
	  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	  return -1;
	}
      else if (0 == readed)
	break;

      if (copy_as_utf8)
	{
	  unsigned char *dest_ptr = dest;
	  virt_mbstate_t mb;

	  memset (&mb, 0, sizeof (mb));

	  while (nchars && (dest_ptr - dest) < readed)
	    {
	      size_t sz = virt_mbrtowc (NULL, data_ptr,
		  VIRT_MB_CUR_MAX, &mb);
	      if (sz == (size_t) - 1)
		{
		  log_error ("Invalid utf-8 data in file %s", ses->dks_session->ses_file->ses_temp_file_name);
		  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
		  return -1;
		}
	      memcpy (dest_ptr, data_ptr, sz);
	      dest_ptr += sz;
	      data_ptr += sz;
	      nchars--;
	    }
	  if (dest_ptr_out)
	    *dest_ptr_out = dest_ptr;
	}
      else
	{
	  converted = virt_mbsnrtowcs ((wchar_t *) dest, &data_ptr, readed, nchars, &mb);
	  if (converted == (size_t) - 1)
	    {
	      log_error ("Invalid utf-8 data in file %s", ses->dks_session->ses_file->ses_temp_file_name);
	      SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	      return -1;
	    }
	  nchars = (long) converted;
	}

      if (data_ptr - &src_buffer[0] < readed)
	{					 /* there are some bytes left unconverted, unwind them for the next go */
	  if (-1 == strf_lseek (ses->dks_session->ses_file, -(readed - (data_ptr - &src_buffer[0])), SEEK_CUR))
	    {
	      log_error ("Can't seek in file %s", ses->dks_session->ses_file->ses_temp_file_name);
	      SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
	      return -1;
	    }
	}
    }
  return nchars;
}


long
strses_get_wide_part (dk_session_t * ses, wchar_t * buf, long starting_ofs, long nchars)
{
  buffer_elt_t *elt = ses->dks_buffer_chain;
  int fd = ses->dks_session->ses_file->ses_file_descriptor;
  virt_mbstate_t mb;

  while (elt && nchars)
    {
      if (elt->fill_chars > starting_ofs)
	{
	  long copychars = MIN (elt->fill_chars - starting_ofs, nchars);
	  unsigned char *data_ptr;

	  /* skip starting_ofs wide chars */
	  data_ptr = strses_skip_wchars ((unsigned char *) elt->data, elt->fill, starting_ofs);
	  if (!data_ptr)
	    return 0;

	  /* get the copychars worth of wides into the buffer */
	  if (virt_mbsnrtowcs (buf, &data_ptr,
		elt->fill - (data_ptr - (unsigned char *) elt->data), copychars, &mb) == (size_t) -1)
	    return 0;

	  buf += copychars;
	  nchars -= copychars;
	  starting_ofs = 0;
	}
      else
	starting_ofs -= elt->fill_chars;
      elt = elt->next;
    }

  if (fd && nchars)
    {
      if (ses->dks_session->ses_file->ses_fd_fill_chars > starting_ofs)
	{
	  size_t readed;
	  OFF_T skipchars;
	  if (ses->dks_session->ses_file->ses_fd_curr_char_pos > starting_ofs ||
	      ses->dks_session->ses_file->ses_fd_curr_char_pos == 0)
	    {					 /* if the file ptr is behind the requested one start from the begining */
	      if (-1 == strf_lseek (ses->dks_session->ses_file, 0, SEEK_SET))
		{
		  log_error ("Can't seek in file %s", ses->dks_session->ses_file->ses_temp_file_name);
		  SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
		  return 0;
		}
	      skipchars = starting_ofs;
	      ses->dks_session->ses_file->ses_fd_curr_char_pos = 0;
	    }
	  else
	    skipchars = starting_ofs - ses->dks_session->ses_file->ses_fd_curr_char_pos;

	  /* now skip the skipchars */
	  if (-1 == read_wides_from_utf8_file (ses, skipchars, NULL, 0, NULL))
	    {
	      ses->dks_session->ses_file->ses_fd_curr_char_pos = 0;
	      return 0;
	    }
	  ses->dks_session->ses_file->ses_fd_curr_char_pos += skipchars;

	  /* it's time to read the actual data */
	  readed = read_wides_from_utf8_file (ses, nchars, (unsigned char *) buf, 0, NULL);
	  if (-1 == readed)
	    {
	      ses->dks_session->ses_file->ses_fd_curr_char_pos = 0;
	      return 0;
	    }
	  ses->dks_session->ses_file->ses_fd_curr_char_pos += nchars;

	  buf += (nchars - readed);
	  nchars = readed;
	  starting_ofs = 0;
	}
      else
	starting_ofs -= ses->dks_session->ses_file->ses_fd_fill_chars;
    }

  if (nchars)
    {
      unsigned char *data_ptr = (unsigned char *) ses->dks_out_buffer, *data_ptr_start;
      long converted = 0;

      memset (&mb, 0, sizeof (mb));
      /* skip starting_ofs wide chars */
      data_ptr_start = strses_skip_wchars (data_ptr, ses->dks_out_fill, starting_ofs);
      if (!data_ptr_start)
	return 0;

      if ((data_ptr - data_ptr_start) < ses->dks_out_fill)
	{
	  /* get the copychars worth of wides into the buffer */
	  if ((converted = virt_mbsnrtowcs (buf, &data_ptr_start,
	  	ses->dks_out_fill - (data_ptr - data_ptr_start), nchars, &mb) == (size_t) - 1))
	    return 0;
	}

      buf += converted;
      nchars -= converted;
    }

  return nchars;
}


void
strses_set_utf8 (dk_session_t * ses, int is_utf8)
{
  if (ses->dks_session->ses_class == SESCLASS_STRING)
    ((strdevice_t *) (ses->dks_session->ses_device))->strdev_is_utf8 = is_utf8 ? 1 : 0;
}


int
strses_is_utf8 (dk_session_t * ses)
{
  return ses->dks_session->ses_class == SESCLASS_STRING &&
  	 ((strdevice_t *) (ses->dks_session->ses_device))->strdev_is_utf8 ? 1 : 0;
}


dk_session_t *
DBG_NAME (strses_allocate) (DBG_PARAMS_0)
{
  dk_session_t *dk_ses = (dk_session_t *) DBG_NAME (dk_alloc_box_zero) (DBG_ARGS sizeof (dk_session_t),
      DV_STRING_SESSION);
  session_t *ses = session_allocate (SESCLASS_STRING);

  /*memset (dk_ses, 0, sizeof (dk_session_t)); */

  SESSION_SCH_DATA (dk_ses) = (scheduler_io_data_t *) DBG_NAME (dk_alloc) (DBG_ARGS sizeof (scheduler_io_data_t));
  memset (SESSION_SCH_DATA (dk_ses), 0, sizeof (scheduler_io_data_t));

  ses->ses_client_data = (void *) dk_ses;
  if (!ses->ses_device)
    ses->ses_device = DBG_NAME (strdev_allocate) (DBG_ARGS_0);

  dk_ses->dks_session = ses;
  SESSION_DK_SESSION (ses) = dk_ses;		 /* two way link. */

  dk_ses->dks_out_buffer = (char *) DBG_NAME (dk_alloc) (DBG_ARGS DKSES_OUT_BUFFER_LENGTH);
  dk_ses->dks_out_length = DKSES_OUT_BUFFER_LENGTH;
/* No need, we have dk_alloc_box_zero above
  dk_ses->dks_out_fill = 0;

  dk_ses->dks_in_buffer = NULL;
  dk_ses->dks_in_fill = 0;
  dk_ses->dks_in_read = 0;

  dk_ses->dks_buffer_chain = NULL;
*/
  dk_ses->dks_refcount = 1;
  return (dk_ses);
}


/*##**********************************************************************
 *
 *		strdev_ws_chunked_write
 *
 * Implementation of the HTTP chunked protocol
 *
 * Input params :	  - The string session, the string to append
 &		       - the number of bytes to append.
 *
 * Output params:    - none
 *
 * Return value :    the number of bytes effectively added to the string session.
 *
 * Limitations  :   - The algorithm is poor, the session needs to be in write mode.
 *
 * Globals used :
 */
static int
strdev_ws_chunked_write (session_t * ses2, char *buffer, int bytes)
{
  dk_session_t *ses = SESSION_DK_SESSION (ses2);
  strdevice_t *strdev = (strdevice_t *) ses->dks_session->ses_device;
  dk_session_t *http_ses = (dk_session_t *) ses->dks_fixed_thread;
  int filled;
  buffer_elt_t *buf = ses->dks_buffer_chain_tail;
  long space;


  if (!buf)
    buf = strdev_get_buf (ses);

  space = DKSES_OUT_BUFFER_LENGTH - buf->fill;

  SESSTAT_SET (ses->dks_session, SST_OK);
  if (strdev->strdev_in_read && !buf->fill && bytes >= ses->dks_out_length && !buf->read && ses2->ses_device)
    {
      buf->read = strdev->strdev_in_read;
      strdev->strdev_in_read = 0;
    }
  memcpy (&buf->data[buf->fill], buffer, filled = MIN (bytes, space));
  buf->fill += filled;

  if (buf->fill == DKSES_OUT_BUFFER_LENGTH)
    {
#ifndef NDEBUG
      if (SESSION_SCH_DATA (http_ses)->sio_write_fail_on)
	GPF_T;
#endif
      CATCH_WRITE_FAIL (http_ses)
      {
	char tmp[20];
	snprintf (tmp, sizeof (tmp), "%x\r\n", DKSES_OUT_BUFFER_LENGTH);
	SES_PRINT (http_ses, tmp);
	session_buffered_write (http_ses, buf->data, DKSES_OUT_BUFFER_LENGTH);
	SES_PRINT (http_ses, "\r\n");
	buf->fill = 0;
	session_flush_1 (http_ses);

      }
      FAILED
      {
	filled = bytes;
      }
      END_WRITE_FAIL (http_ses);
    }

  return (filled);
}


int
strses_is_ws_chunked_output (dk_session_t * ses)
{
  return (ses->dks_session->ses_device->dev_funs->dfp_write == strdev_ws_chunked_write);
}


void
strses_ws_chunked_state_set (dk_session_t * ses, dk_session_t * http_ses)
{
  ses->dks_session->ses_device->dev_funs->dfp_write = strdev_ws_chunked_write;
  ses->dks_fixed_thread = (du_thread_t *) http_ses;
}


void
strses_ws_chunked_state_reset (dk_session_t * ses)
{
  ses->dks_session->ses_device->dev_funs->dfp_write = strdev_write;
  ses->dks_fixed_thread = NULL;
}


#ifdef MALLOC_DEBUG
#undef strses_allocate
dk_session_t *
strses_allocate (void)
{
  return dbg_strses_allocate (__FILE__, __LINE__);
}


#undef strses_string
caddr_t
strses_string (dk_session_t * ses)
{
  return dbg_strses_string (__FILE__, __LINE__, ses);
}


#undef strses_wide_string
caddr_t
strses_wide_string (dk_session_t * ses)
{
  return dbg_strses_wide_string (__FILE__, __LINE__, ses);
}
#endif

caddr_t
strses_fake_copy (caddr_t orig)
{
  dk_session_t *orig_ses = (dk_session_t *) orig;
#ifndef NDEBUG
  if (0 >= orig_ses->dks_refcount)
    GPF_T1 ("Invalid dks_refcount in strses_fake_copy()");
#endif
  orig_ses->dks_refcount += 1;
  return orig;
}


caddr_t
strses_mp_copy (mem_pool_t * mp, caddr_t box)
{
  strses_fake_copy (box);
  dk_set_push (&mp->mp_trash, (void *) box);
  return box;
}


void
strses_mem_initalize (void)
{
  dk_mem_hooks_2 (DV_STRING_SESSION, strses_fake_copy, (box_destr_f) strses_destroy, 1, strses_mp_copy);
}


void
strses_readtable_initialize (void)
{
  macro_char_func *rt = get_readtable ();
  PrpcSetWriter (DV_STRING_SESSION, (ses_write_func) strses_serialize);
  rt[DV_STRING_SESSION] = (macro_char_func) strses_deserialize;
}