1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/streams.h"
#include "gate/results.h"
#include "gate/strings.h"
#include "gate/atomics.h"
#include "gate/debugging.h"
#include "gate/times.h"
#include "gate/arrays.h"

#include <stdarg.h>

#if defined(GATE_SYS_WIN)
#	define GATE_CORE_STREAMS_WINAPI 1
#elif defined(GATE_SYS_DOS)
#	define GATE_CORE_STREAMS_POSIX 1
#elif defined(GATE_SYS_POSIX)
#	define GATE_CORE_STREAMS_POSIX 1
#endif



/********************************
 * memory stream implementation *
 ********************************/

static void				gate_memstream_release_impl(void* self);
static int				gate_memstream_retain_impl(void* self);
static char const* gate_memstream_interface_name_impl(void* self);
static gate_result_t	gate_memstream_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
static gate_result_t	gate_memstream_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
static gate_result_t	gate_memstream_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
static gate_result_t	gate_memstream_flush_impl(void* self);
static gate_result_t	gate_memstream_reserve_impl(void* self, gate_size_t space);
static gate_size_t		gate_memstream_size_impl(void* self);
static gate_result_t	gate_memstream_fill_impl(void* self, char fillitem, gate_size_t fillsize);
static gate_size_t		gate_memstream_discard_impl(void* self, gate_size_t bytestodiscard);
static gate_size_t		gate_memstream_discard_back_impl(void* self, gate_size_t bytestodiscard);
static char const* gate_memstream_get_data_impl(void* self);
static gate_result_t	gate_memstream_reset_impl(void* self);


static void				gate_memstream_release_static_impl(void* self);
static int				gate_memstream_retain_static_impl(void* self);
static gate_result_t	gate_memstream_write_static_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
static gate_result_t	gate_memstream_reserve_static_impl(void* self, gate_size_t space);
static gate_result_t	gate_memstream_fill_static_impl(void* self, char fillitem, gate_size_t fillsize);
static gate_result_t	gate_memstream_write_disabled_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
static gate_result_t	gate_memstream_reserve_disabled_impl(void* self, gate_size_t space);
static gate_result_t	gate_memstream_fill_disabled_impl(void* self, char fillitem, gate_size_t fillsize);

static void				gate_memstream_release_static_unmanaged_impl(void* self);

static GATE_INTERFACE_VTBL(gate_memstream) gate_memstream_static_unmanaged_readonly_vtbl;

static void gate_init_memstream_static_unmanaged_readonly_vtbl()
{
    if (!gate_memstream_static_unmanaged_readonly_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_memstream) const local_vtbl =
        {
            &gate_memstream_interface_name_impl,
            &gate_memstream_release_static_unmanaged_impl,
            &gate_memstream_retain_static_impl,

            &gate_memstream_read_impl,
            &gate_memstream_peek_impl,
            &gate_memstream_write_disabled_impl,
            &gate_memstream_flush_impl,

            &gate_memstream_get_data_impl,
            &gate_memstream_size_impl,
            &gate_memstream_reserve_disabled_impl,
            &gate_memstream_fill_disabled_impl,
            &gate_memstream_discard_impl,
            &gate_memstream_discard_back_impl,
            &gate_memstream_reset_impl
        };
        gate_memstream_static_unmanaged_readonly_vtbl = local_vtbl;
    }
}



static void gate_memstream_release_impl(void* self)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    if (gate_atomic_int_dec(&strm->ref_counter) == 0)
    {
        /* release memstream */
        if (strm->priv_membuffer != NULL)
        {
            gate_mem_dealloc(strm->priv_membuffer);
            strm->priv_membuffer = NULL;
        }
        gate_mem_dealloc(strm);
    }
}
static int gate_memstream_retain_impl(void* self)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    return gate_atomic_int_inc(&strm->ref_counter);
}
static char const* gate_memstream_interface_name_impl(void* self)
{
    (void)self;
    return GATE_INTERFACE_NAME_MEMSTREAM;
}

gate_result_t gate_memstream_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    if (bufferlength > strm->priv_memlength)
    {
        bufferlength = strm->priv_memlength;
    }
    if (bufferlength != 0)
    {
        gate_mem_copy(buffer, &strm->priv_membuffer[strm->priv_memoffset], bufferlength);
        strm->priv_memlength -= bufferlength;
        if (strm->priv_memlength == 0)
        {
            strm->priv_memoffset = 0;
        }
        else
        {
            strm->priv_memoffset += bufferlength;
        }
    }
    if (returned != NULL)
    {
        *returned = bufferlength;
    }
    return GATE_RESULT_OK;
}
gate_result_t gate_memstream_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    if (bufferlength > strm->priv_memlength)
    {
        bufferlength = strm->priv_memlength;
    }
    if (bufferlength != 0)
    {
        gate_mem_copy(buffer, strm->priv_membuffer, bufferlength);
    }
    if (returned != NULL)
    {
        *returned = bufferlength;
    }
    return GATE_RESULT_OK;
}
gate_result_t gate_memstream_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    gate_result_t result = strm->vtbl->reserve(strm, bufferlength);
    if (GATE_FAILED(result))
    {
        return result;
    }

    gate_mem_copy(&strm->priv_membuffer[strm->priv_memoffset + strm->priv_memlength], buffer, bufferlength);
    strm->priv_memlength += bufferlength;

    if (written != NULL)
    {
        *written = bufferlength;
    }
    return GATE_RESULT_OK;
}
gate_result_t gate_memstream_flush_impl(void* self)
{
    (void)self;
    return GATE_RESULT_OK;
}
static gate_result_t gate_memstream_reserve_impl(void* self, gate_size_t space)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    gate_size_t unusedspace = strm->priv_memcapacity - strm->priv_memlength;
    gate_size_t freespace = unusedspace - strm->priv_memoffset;
    if (space > freespace)
    {
        if (space > unusedspace)
        {
            /* reallocation is required */
            gate_size_t newcapacity = (strm->priv_memcapacity + space) + (strm->priv_memcapacity / 2);
            char* newbuffer = (char*)gate_mem_alloc(newcapacity);
            if (newbuffer == NULL)
            {
                return GATE_RESULT_OUTOFMEMORY;
            }
            if (strm->priv_memlength != 0)
            {
                gate_mem_copy(&newbuffer[0], &strm->priv_membuffer[strm->priv_memoffset], strm->priv_memlength);
            }
            if (strm->priv_membuffer != NULL)
            {
                gate_mem_dealloc(strm->priv_membuffer);
            }
            strm->priv_membuffer = newbuffer;
            strm->priv_memoffset = 0;
            strm->priv_memcapacity = newcapacity;
        }
        else
        {
            /* moving bytes to origin of buffer */
            gate_mem_copy(&strm->priv_membuffer[0], &strm->priv_membuffer[strm->priv_memoffset], strm->priv_memlength);
            strm->priv_memoffset = 0;
        }
    }
    return GATE_RESULT_OK;
}
static gate_size_t gate_memstream_size_impl(void* self)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    return strm->priv_memlength;
}
static gate_result_t gate_memstream_fill_impl(void* self, char fillitem, gate_size_t fillsize)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    gate_size_t ndx;
    char* ptr;
    gate_result_t result = strm->vtbl->reserve(strm, fillsize);

    if (GATE_FAILED(result))
    {
        return result;
    }

    ptr = &strm->priv_membuffer[strm->priv_memoffset + strm->priv_memlength];
    for (ndx = 0; ndx < fillsize; ++ndx, ++ptr)
    {
        *ptr = fillitem;
    }
    strm->priv_memlength += fillsize;
    return GATE_RESULT_OK;
}
static gate_size_t gate_memstream_discard_impl(void* self, gate_size_t bytestodiscard)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    gate_size_t ret;
    if (bytestodiscard >= strm->priv_memlength)
    {
        ret = strm->priv_memlength;
        strm->priv_memoffset = 0;
        strm->priv_memlength = 0;
    }
    else
    {
        ret = bytestodiscard;
        strm->priv_memoffset += bytestodiscard;
        strm->priv_memlength -= bytestodiscard;
    }
    return ret;
}
static gate_size_t gate_memstream_discard_back_impl(void* self, gate_size_t bytestodiscard)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    gate_size_t ret;
    if (bytestodiscard >= strm->priv_memlength)
    {
        ret = strm->priv_memlength;
        strm->priv_memoffset = 0;
        strm->priv_memlength = 0;
    }
    else
    {
        ret = bytestodiscard;
        strm->priv_memlength -= bytestodiscard;
    }
    return ret;
}
static char const* gate_memstream_get_data_impl(void* self)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    return &strm->priv_membuffer[strm->priv_memoffset];
}

static gate_result_t gate_memstream_reset_impl(void* self)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    strm->priv_memoffset = 0;
    strm->priv_memlength = 0;
    return GATE_RESULT_OK;
}

static GATE_INTERFACE_VTBL(gate_memstream) gate_memstream_vtbl;

void gate_init_memstream_vtbl()
{
    if (!gate_memstream_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_memstream) const local_vtbl =
        {
            &gate_memstream_interface_name_impl,
            &gate_memstream_release_impl,
            &gate_memstream_retain_impl,

            &gate_memstream_read_impl,
            &gate_memstream_peek_impl,
            &gate_memstream_write_impl,
            &gate_memstream_flush_impl,

            &gate_memstream_get_data_impl,
            &gate_memstream_size_impl,
            &gate_memstream_reserve_impl,
            &gate_memstream_fill_impl,
            &gate_memstream_discard_impl,
            &gate_memstream_discard_back_impl,
            &gate_memstream_reset_impl
        };
        gate_memstream_vtbl = local_vtbl;
    }
}

gate_memstream_t* gate_memstream_create(gate_size_t capacity)
{
    gate_result_t result;
    gate_memstream_impl_t* ret = (gate_memstream_impl_t*)gate_mem_alloc(sizeof(gate_memstream_impl_t));
    if (ret != NULL)
    {
        gate_init_memstream_vtbl();
        ret->vtbl = &gate_memstream_vtbl;
        gate_atomic_int_set(&ret->ref_counter, 1);
        ret->priv_membuffer = NULL;
        ret->priv_memcapacity = 0;
        ret->priv_memoffset = 0;
        ret->priv_memlength = 0;
        if (capacity > 0)
        {
            result = ret->vtbl->reserve(ret, capacity);
            (void)result;
        }

    }
    return (gate_memstream_t*)ret;
}


static void	gate_memstream_release_static_impl(void* self)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    if (0 == gate_atomic_int_dec(&strm->ref_counter))
    {
        gate_mem_dealloc(self);
    }
}
static int gate_memstream_retain_static_impl(void* self)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    return (int)gate_atomic_int_inc(&strm->ref_counter);
}
static gate_result_t gate_memstream_write_static_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    gate_result_t ret = GATE_RESULT_OK;
    gate_size_t avail;
    if (bufferlength > 0)
    {
        avail = strm->priv_memcapacity - strm->priv_memlength;
        if (bufferlength > avail)
        {
            if (bufferlength < strm->priv_memcapacity)
            {
                avail = strm->priv_memcapacity - bufferlength;
                if (avail > strm->priv_memlength) avail = strm->priv_memlength;
                gate_mem_copy(
                    &strm->priv_membuffer[0],
                    &strm->priv_membuffer[strm->priv_memoffset + strm->priv_memlength - avail],
                    avail);
                strm->priv_memoffset = 0;
                strm->priv_memlength = avail;
                gate_mem_copy(&strm->priv_membuffer[strm->priv_memlength], buffer, bufferlength);
                strm->priv_memlength += bufferlength;
            }
            else
            {
                gate_mem_copy(
                    &strm->priv_membuffer[0],
                    &buffer[bufferlength - strm->priv_memcapacity],
                    strm->priv_memcapacity);
                strm->priv_memoffset = 0;
                strm->priv_memlength = strm->priv_memcapacity;
            }
        }
        else
        {
            if (strm->priv_memoffset != 0)
            {
                gate_mem_copy(&strm->priv_membuffer[0], &strm->priv_membuffer[strm->priv_memoffset], strm->priv_memlength);
                strm->priv_memoffset = 0;
            }
            gate_mem_copy(&strm->priv_membuffer[strm->priv_memlength], buffer, bufferlength);
            strm->priv_memlength += bufferlength;
        }
    }
    if (written != NULL) *written = bufferlength;
    return ret;
}
static gate_result_t gate_memstream_write_disabled_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
    (void)self;
    (void)buffer;
    (void)bufferlength;
    (void)written;
    return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t gate_memstream_reserve_disabled_impl(void* self, gate_size_t space)
{
    (void)self;
    (void)space;
    return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t gate_memstream_fill_disabled_impl(void* self, char fillitem, gate_size_t fillsize)
{
    (void)self;
    (void)fillitem;
    (void)fillsize;
    return GATE_RESULT_NOTSUPPORTED;
}

static gate_result_t gate_memstream_reserve_static_impl(void* self, gate_size_t space)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    if (strm->priv_memoffset != 0)
    {
        gate_mem_copy(&strm->priv_membuffer[0], &strm->priv_membuffer[strm->priv_memoffset], strm->priv_memlength);
        strm->priv_memoffset = 0;
    }
    return (space > strm->priv_memcapacity) ? GATE_RESULT_OUTOFMEMORY : GATE_RESULT_OK;
}
static gate_result_t gate_memstream_fill_static_impl(void* self, char fillitem, gate_size_t fillsize)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    gate_result_t ret = GATE_RESULT_OK;
    gate_size_t avail;
    char* ptr;
    gate_size_t index;
    gate_size_t preserve;
    if (fillsize != 0)
    {
        if (strm->priv_memoffset != 0)
        {
            gate_mem_copy(&strm->priv_membuffer[0], &strm->priv_membuffer[strm->priv_memoffset], strm->priv_memlength);
            strm->priv_memoffset = 0;
        }
        avail = strm->priv_memcapacity - strm->priv_memlength;
        if (fillsize > avail)
        {
            if (fillsize >= strm->priv_memcapacity)
            {
                ptr = &strm->priv_membuffer[0];
                fillsize = strm->priv_memcapacity;
                strm->priv_memoffset = 0;
                strm->priv_memlength = 0;
            }
            else
            {
                preserve = strm->priv_memcapacity - fillsize;
                gate_mem_copy(&strm->priv_membuffer[0], &strm->priv_membuffer[strm->priv_memoffset + strm->priv_memlength - preserve], preserve);
                strm->priv_memoffset = 0;
                strm->priv_memlength = preserve;
                ptr = &strm->priv_membuffer[preserve];
            }
        }
        else
        {
            ptr = &strm->priv_membuffer[strm->priv_memoffset + strm->priv_memlength];
        }
        for (index = 0; index != fillsize; ++index)
        {
            *ptr = fillitem;
            ++ptr;
        }
        strm->priv_memlength += fillsize;
    }
    return ret;
}


static void gate_memstream_release_static_unmanaged_impl(void* self)
{
    gate_memstream_impl_t* strm = (gate_memstream_impl_t*)self;
    if (0 == gate_atomic_int_dec(&strm->ref_counter))
    {
        /* do nothing */
    }
}

static GATE_INTERFACE_VTBL(gate_memstream) gate_memstream_static_unmanaged_vtbl;

void gate_init_memstream_static_unmanaged_vtbl()
{
    if (!gate_memstream_static_unmanaged_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_memstream) const local_vtbl =
        {
            &gate_memstream_interface_name_impl,
            &gate_memstream_release_static_unmanaged_impl,
            &gate_memstream_retain_static_impl,

            &gate_memstream_read_impl,
            &gate_memstream_peek_impl,
            &gate_memstream_write_static_impl,
            &gate_memstream_flush_impl,

            &gate_memstream_get_data_impl,
            &gate_memstream_size_impl,
            &gate_memstream_reserve_static_impl,
            &gate_memstream_fill_static_impl,
            &gate_memstream_discard_impl,
            &gate_memstream_discard_back_impl,
            &gate_memstream_reset_impl
        };
        gate_memstream_static_unmanaged_vtbl = local_vtbl;
    }
}

gate_memstream_t* gate_memstream_create_static_unmanaged(gate_memstream_impl_t* impl, void* buffer, gate_size_t capacity, gate_size_t used)
{
    gate_init_memstream_static_unmanaged_vtbl();
    impl->vtbl = &gate_memstream_static_unmanaged_vtbl;
    gate_atomic_int_init(&impl->ref_counter, 1);
    impl->priv_memoffset = 0;
    impl->priv_membuffer = (char*)buffer;
    impl->priv_memcapacity = capacity;
    impl->priv_memlength = used;
    return (gate_memstream_t*)impl;
}

static GATE_INTERFACE_VTBL(gate_memstream) gate_memstream_static_vtbl;
void gate_init_memstream_static_vtbl()
{
    if (!gate_memstream_static_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_memstream) const local_vtbl =
        {
            &gate_memstream_interface_name_impl,
            &gate_memstream_release_static_impl,
            &gate_memstream_retain_static_impl,

            &gate_memstream_read_impl,
            &gate_memstream_peek_impl,
            &gate_memstream_write_static_impl,
            &gate_memstream_flush_impl,

            &gate_memstream_get_data_impl,
            &gate_memstream_size_impl,
            &gate_memstream_reserve_static_impl,
            &gate_memstream_fill_static_impl,
            &gate_memstream_discard_impl,
            &gate_memstream_discard_back_impl,
            &gate_memstream_reset_impl
        };
        gate_memstream_static_vtbl = local_vtbl;
    }
}

gate_memstream_t* gate_memstream_create_static(void* buffer, gate_size_t capacity, gate_size_t used)
{
    gate_memstream_impl_t* impl = (gate_memstream_impl_t*)gate_mem_alloc(sizeof(gate_memstream_impl_t));
    if (impl != NULL)
    {
        gate_init_memstream_static_vtbl();
        impl->vtbl = &gate_memstream_static_vtbl;
        gate_atomic_int_init(&impl->ref_counter, 1);
        impl->priv_memoffset = 0;
        impl->priv_membuffer = (char*)buffer;
        impl->priv_memcapacity = capacity;
        impl->priv_memlength = used;
    }
    return (gate_memstream_t*)impl;
}

gate_memstream_t* gate_memstream_create_static_unmanaged_readonly(gate_memstream_impl_t* impl, void const* buffer, gate_size_t capacity, gate_size_t used)
{
    gate_init_memstream_static_unmanaged_readonly_vtbl();
    impl->vtbl = &gate_memstream_static_unmanaged_readonly_vtbl;
    gate_atomic_int_init(&impl->ref_counter, 1);
    impl->priv_memoffset = 0;
    impl->priv_membuffer = (char*)buffer;
    impl->priv_memcapacity = capacity;
    impl->priv_memlength = used;
    return (gate_memstream_t*)impl;
}



/*****************************************
 * control-memstream-view implementation *
 *****************************************/

typedef struct memview_class
{
    GATE_INTERFACE_VTBL(gate_controlstream)* vtbl;

    gate_atomic_int_t	ref_counter;
    char const* ptr_buffer;
    gate_size_t			buffer_len;
    gate_size_t			buffer_offset;
    gate_bool_t			closed;			/* true, when stream is closed */
    gate_object_t* ptr_container;	/* container holding mem-allocation, released on close*/
} memview_t;

static void memview_destroy(memview_t* self)
{
    if (self->ptr_container)
    {
        gate_object_release(self->ptr_container);
    }
    gate_mem_dealloc(self);
}

static char const* memview_get_interface_name(void* obj)
{
    GATE_UNUSED_ARG(obj);
    return GATE_INTERFACE_NAME_CONTROLSTREAM;
}
static void memview_release(void* obj)
{
    memview_t* const self = (memview_t*)obj;
    if (0 == gate_atomic_int_dec(&self->ref_counter))
    {
        memview_destroy(self);
    }
}
static int memview_retain(void* obj)
{
    memview_t* const self = (memview_t*)obj;
    return (int)gate_atomic_int_inc(&self->ref_counter);
}

static gate_result_t memview_peek(void* obj, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    memview_t* const self = (memview_t*)obj;
    gate_result_t result = GATE_RESULT_FAILED;
    gate_size_t copied = 0;
    gate_size_t available;

    do
    {
        if (!buffer || !obj)
        {
            result = GATE_RESULT_NULLPOINTER;
            break;
        }

        available = self->buffer_len - self->buffer_offset;

        if (self->closed || (available == 0))
        {
            /* react as end of stream */
            result = GATE_RESULT_OK;
            break;
        }

        copied = (bufferlength > available) ? available : bufferlength;
        gate_mem_copy(buffer, &self->ptr_buffer[self->buffer_offset], copied);
        result = GATE_RESULT_OK;
    } while (0);

    if (returned)
    {
        *returned = copied;
    }

    return result;
}

static gate_result_t memview_read(void* obj, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    memview_t* const self = (memview_t*)obj;
    gate_result_t result = GATE_RESULT_FAILED;
    gate_size_t copied = 0;

    result = memview_peek(obj, buffer, bufferlength, &copied);
    if (GATE_SUCCEEDED(result))
    {
        self->buffer_offset += copied;
        if (self->buffer_offset > self->buffer_len)
        {
            self->buffer_offset = self->buffer_len;
        }
    }

    if (returned)
    {
        *returned = copied;
    }

    return result;
}

static gate_result_t memview_write(void* obj, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
    GATE_UNUSED_ARG(obj);
    GATE_UNUSED_ARG(buffer);
    GATE_UNUSED_ARG(bufferlength);
    GATE_UNUSED_ARG(written);
    return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t memview_flush(void* obj)
{
    GATE_UNUSED_ARG(obj);
    return GATE_RESULT_NOTSUPPORTED;
}

static gate_result_t memview_get_resource(void* obj, gate_enumint_t resource_type, gate_uintptr_t* resource)
{
    memview_t* const self = (memview_t*)obj;
    switch (resource_type)
    {
    case GATE_STREAM_RESOURCE_INPUT:
    case GATE_STREAM_RESOURCE_DEFAULT:
        if (resource)
        {
            *resource = (gate_uintptr_t)self->ptr_buffer;
        }
        return GATE_RESULT_OK;
    default:
    {
        return GATE_RESULT_NOTAVAILABLE;
    }
    }
}

static gate_result_t memview_can_read(void* obj, gate_bool_t* return_value)
{
    memview_t* const self = (memview_t*)obj;
    if (return_value)
    {
        *return_value = !self->closed;
    }
    return GATE_RESULT_OK;
}
static gate_result_t memview_can_write(void* obj, gate_bool_t* return_value)
{
    GATE_UNUSED_ARG(obj);
    if (return_value)
    {
        *return_value = false;
    }
    return GATE_RESULT_OK;
}
static gate_result_t memview_get_size(void* obj, gate_int64_t* return_value)
{
    memview_t* const self = (memview_t*)obj;
    if (return_value)
    {
        *return_value = self->buffer_len;
    }
    return GATE_RESULT_OK;
}
static gate_result_t memview_get_available(void* obj, gate_int64_t* return_value)
{
    memview_t* const self = (memview_t*)obj;
    if (return_value)
    {
        *return_value = self->closed ? 0 : (gate_int64_t)(self->buffer_len - self->buffer_offset);
    }
    return GATE_RESULT_OK;
}
static gate_result_t memview_seek(void* obj, gate_int64_t position, gate_enumint_t origin, gate_int64_t* final_position)
{
    memview_t* const self = (memview_t*)obj;
    gate_result_t result = GATE_RESULT_FAILED;
    gate_size_t shift;

    if (self->closed)
    {
        return GATE_RESULT_NOTAVAILABLE;
    }

    switch (origin)
    {
    case GATE_STREAM_SEEK_BEGIN:
    {
        if (position < 0)<--- Assuming condition 'position<0' is false
        {
            result = GATE_RESULT_INVALIDARG;
            break;
        }
        if (position > (gate_int64_t)self->buffer_len)
        {
            result = GATE_RESULT_NOTSUPPORTED;
            break;
        }
        self->buffer_offset = (gate_size_t)(gate_uint64_t)position;
        result = GATE_RESULT_OK;
        break;
    }
    case GATE_STREAM_SEEK_CURRENT:
    {
        if (position < 0)<--- Condition 'position<0' is always false
        {
            shift = (gate_size_t)(gate_uint64_t)(-position);
            if (shift > self->buffer_offset)
            {
                result = GATE_RESULT_NOTSUPPORTED;
                break;
            }
            self->buffer_offset -= shift;
        }
        else
        {
            shift = (gate_size_t)(gate_uint64_t)position;
            if (self->buffer_offset + shift > self->buffer_len)
            {
                result = GATE_RESULT_NOTSUPPORTED;
                break;
            }
            self->buffer_offset += shift;
        }
        result = GATE_RESULT_OK;
        break;
    }
    case GATE_STREAM_SEEK_END:
    {
        if (position > 0)
        {
            result = GATE_RESULT_NOTSUPPORTED;
            break;
        }
        shift = (gate_size_t)(-position);
        if (shift > self->buffer_len)
        {
            result = GATE_RESULT_INVALIDARG;
            break;
        }
        self->buffer_offset = self->buffer_len - shift;
        result = GATE_RESULT_OK;
        break;
    }
    default:
    {
        result = GATE_RESULT_INVALIDARG;
        break;
    }
    }

    if (final_position)
    {
        *final_position = (gate_int64_t)(gate_uint64_t)self->buffer_offset;
    }
    return result;
}
static gate_result_t memview_reset(void* obj)
{
    memview_t* const self = (memview_t*)obj;
    self->buffer_offset = 0;
    return GATE_RESULT_OK;
}
static gate_result_t memview_close(void* obj, gate_enumint_t close_type)
{
    memview_t* const self = (memview_t*)obj;
    switch (close_type)
    {
    case GATE_STREAM_CLOSE_DEFAULT:
    case GATE_STREAM_CLOSE_INPUT:
        self->closed = true;
        return GATE_RESULT_OK;
    default:
        return GATE_RESULT_NOTSUPPORTED;
    }
}

static GATE_INTERFACE_VTBL(gate_controlstream) memview_vtbl;

static void init_memview_vtbl()
{
    if (memview_vtbl.get_interface_name == NULL)
    {
        GATE_INTERFACE_VTBL(gate_controlstream) local_vtbl =
        {
            &memview_get_interface_name,
            &memview_release,
            &memview_retain,

            &memview_read,
            &memview_peek,
            &memview_write,
            &memview_flush,

            &memview_get_resource,

            &memview_can_read,
            &memview_can_write,
            &memview_get_size,
            &memview_get_available,
            &memview_seek,
            &memview_reset,
            &memview_close
        };
        memview_vtbl = local_vtbl;
    }
}

gate_controlstream_t* gate_memstream_create_view(void const* buffer, gate_size_t length, gate_size_t offset, gate_object_t* ptr_container)
{
    gate_controlstream_t* ret = NULL;
    memview_t* newstrm = NULL;

    do
    {
        newstrm = (memview_t*)gate_mem_alloc(sizeof(memview_t));
        if (!newstrm)
        {
            break;
        }
        gate_mem_clear(newstrm, sizeof(memview_t));
        init_memview_vtbl();
        newstrm->vtbl = &memview_vtbl;
        gate_atomic_int_init(&newstrm->ref_counter, 1);
        newstrm->closed = false;
        newstrm->ptr_buffer = (char const*)buffer;
        newstrm->buffer_len = length;
        newstrm->buffer_offset = (offset > length) ? length : offset;
        newstrm->ptr_container = ptr_container;
        if (newstrm->ptr_container)
        {
            gate_object_retain(newstrm->ptr_container);
        }
        /* success case */
        ret = (gate_controlstream_t*)newstrm;
        newstrm = NULL;
    } while (0);

    if (newstrm)
    {
        memview_destroy(newstrm);
    }
    return ret;
}



/*****************************************
 * control-memstream-file implementation *
 *****************************************/

typedef struct memfile_class
{
    GATE_INTERFACE_VTBL(gate_controlstream)* vtbl;

    gate_atomic_int_t	ref_counter;
    gate_arraylist_t	storage;
    gate_size_t			storage_offset;	/* current read/write position */
    gate_bool_t			closed;			/* true, when stream is closed */
} memfile_t;

static void memfile_destroy(memfile_t* self)
{
    gate_arraylist_release(self->storage);
    gate_mem_dealloc(self);
}

static char const* memfile_get_interface_name(void* obj)
{
    GATE_UNUSED_ARG(obj);
    return GATE_INTERFACE_NAME_MEMFILE;
}
static void memfile_release(void* obj)
{
    memfile_t* const self = (memfile_t*)obj;
    if (0 == gate_atomic_int_dec(&self->ref_counter))
    {
        memfile_destroy(self);
    }
}
static int memfile_retain(void* obj)
{
    memfile_t* const self = (memfile_t*)obj;
    return (int)gate_atomic_int_inc(&self->ref_counter);
}

static gate_result_t memfile_peek(void* obj, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    memfile_t* const self = (memfile_t*)obj;
    gate_result_t result = GATE_RESULT_FAILED;
    gate_size_t copied = 0;
    gate_size_t available;
    gate_size_t storage_length;
    char const* ptr_storage_data = NULL;

    do
    {
        if (!buffer || !obj)
        {
            result = GATE_RESULT_NULLPOINTER;
            break;
        }

        storage_length = gate_arraylist_length(self->storage);
        GATE_DEBUG_ASSERT(self->storage_offset <= storage_length);
        available = storage_length - self->storage_offset;

        if (self->closed || (available == 0))
        {
            /* react as end of stream */
            result = GATE_RESULT_OK;
            break;
        }

        ptr_storage_data = (char const*)gate_arraylist_get(self->storage, self->storage_offset);
        if (!ptr_storage_data)
        {
            result = GATE_RESULT_NOTAVAILABLE;
            break;
        }
        copied = (bufferlength > available) ? available : bufferlength;
        gate_mem_copy(buffer, ptr_storage_data, copied);
        result = GATE_RESULT_OK;
    } while (0);

    if (returned)
    {
        *returned = copied;
    }

    return result;
}

static gate_result_t memfile_read(void* obj, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    memfile_t* const self = (memfile_t*)obj;
    gate_result_t result = GATE_RESULT_FAILED;
    gate_size_t copied = 0;

    result = memfile_peek(obj, buffer, bufferlength, &copied);
    if (GATE_SUCCEEDED(result))
    {
        self->storage_offset += copied;
    }

    if (returned)
    {
        *returned = copied;
    }

    return result;
}

static gate_result_t memfile_write(void* obj, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
    memfile_t* const self = (memfile_t*)obj;
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_size_t stored_bytes = 0;
    gate_size_t storage_length;
    gate_size_t available;
    char* ptr_storage_data;

    do
    {
        storage_length = gate_arraylist_length(self->storage);
        GATE_DEBUG_ASSERT(self->storage_offset <= storage_length);
        available = storage_length - self->storage_offset;

        if (available > 0)
        {
            ptr_storage_data = (char*)gate_arraylist_get(self->storage, self->storage_offset);
            if (!ptr_storage_data)
            {
                ret = GATE_RESULT_NOTAVAILABLE;
                break;
            }
            stored_bytes = (bufferlength <= available) ? bufferlength : available;
            gate_mem_copy(ptr_storage_data, buffer, stored_bytes);
            self->storage_offset += stored_bytes;
            bufferlength -= stored_bytes;
            buffer += stored_bytes;
        }
        if (bufferlength > 0)
        {
            if (NULL == gate_arraylist_insert(self->storage, storage_length, buffer, bufferlength))
            {
                ret = (stored_bytes > 0) ? GATE_RESULT_OK : GATE_RESULT_OUTOFMEMORY;
                break;
            }
            self->storage_offset += bufferlength;
            stored_bytes += bufferlength;
        }
        ret = GATE_RESULT_OK;
    } while (0);

    if (written)
    {
        *written = stored_bytes;
    }
    return ret;
}
static gate_result_t memfile_flush(void* obj)
{
    GATE_UNUSED_ARG(obj);
    return GATE_RESULT_OK;
}

static gate_result_t memfile_get_resource(void* obj, gate_enumint_t resource_type, gate_uintptr_t* resource)
{
    memfile_t* const self = (memfile_t*)obj;
    switch (resource_type)
    {
    case GATE_STREAM_RESOURCE_INPUT:
    case GATE_STREAM_RESOURCE_OUTPUT:
    case GATE_STREAM_RESOURCE_DEFAULT:
        if (resource)
        {
            *resource = (gate_uintptr_t)gate_arraylist_get(self->storage, 0);
        }
        return GATE_RESULT_OK;
    default:
    {
        return GATE_RESULT_NOTAVAILABLE;
    }
    }
}
static gate_result_t memfile_can_read(void* obj, gate_bool_t* return_value)
{
    memfile_t* const self = (memfile_t*)obj;
    if (return_value)
    {
        *return_value = !self->closed;
    }
    return GATE_RESULT_OK;
}
static gate_result_t memfile_can_write(void* obj, gate_bool_t* return_value)
{
    memfile_t* const self = (memfile_t*)obj;
    if (return_value)
    {
        *return_value = !self->closed;
    }
    return GATE_RESULT_OK;
}
static gate_result_t memfile_get_size(void* obj, gate_int64_t* return_value)
{
    memfile_t* const self = (memfile_t*)obj;
    if (return_value)
    {
        *return_value = (gate_int64_t)gate_arraylist_length(self->storage);
    }
    return GATE_RESULT_OK;
}
static gate_result_t memfile_get_available(void* obj, gate_int64_t* return_value)
{
    memfile_t* const self = (memfile_t*)obj;

    if (self->closed)
    {
        return GATE_RESULT_INVALIDSTATE;
    }
    else if (return_value)
    {
        gate_size_t storage_length = gate_arraylist_length(self->storage);
        GATE_DEBUG_ASSERT(self->storage_offset <= storage_length);
        *return_value = (gate_int64_t)(storage_length - self->storage_offset);
    }
    return GATE_RESULT_OK;
}
static gate_result_t memfile_seek(void* obj, gate_int64_t position, gate_enumint_t origin, gate_int64_t* final_position)
{
    memfile_t* const self = (memfile_t*)obj;
    gate_result_t result = GATE_RESULT_FAILED;
    gate_size_t shift;
    gate_size_t storage_length;

    if (self->closed)
    {
        return GATE_RESULT_NOTAVAILABLE;
    }

    storage_length = gate_arraylist_length(self->storage);

    switch (origin)
    {
    case GATE_STREAM_SEEK_BEGIN:
    {
        if (position < 0)<--- Assuming condition 'position<0' is false
        {
            result = GATE_RESULT_INVALIDARG;
            break;
        }
        if (position > (gate_int64_t)storage_length)
        {
            result = GATE_RESULT_NOTSUPPORTED;
            break;
        }
        self->storage_offset = (gate_size_t)(gate_uint64_t)position;
        result = GATE_RESULT_OK;
        break;
    }
    case GATE_STREAM_SEEK_CURRENT:
    {
        if (position < 0)<--- Condition 'position<0' is always false
        {
            shift = (gate_size_t)(gate_uint64_t)(-position);
            if (shift > self->storage_offset)
            {
                result = GATE_RESULT_NOTSUPPORTED;
                break;
            }
            self->storage_offset -= shift;
        }
        else
        {
            shift = (gate_size_t)(gate_uint64_t)position;
            if (self->storage_offset + shift > storage_length)
            {
                result = GATE_RESULT_NOTSUPPORTED;
                break;
            }
            self->storage_offset += shift;
        }
        result = GATE_RESULT_OK;
        break;
    }
    case GATE_STREAM_SEEK_END:
    {
        if (position > 0)
        {
            result = GATE_RESULT_NOTSUPPORTED;
            break;
        }
        shift = (gate_size_t)(-position);
        if (shift > storage_length)
        {
            result = GATE_RESULT_INVALIDARG;
            break;
        }
        self->storage_offset = storage_length - shift;
        result = GATE_RESULT_OK;
        break;
    }
    default:
    {
        result = GATE_RESULT_INVALIDARG;
        break;
    }
    }

    if (final_position)
    {
        *final_position = (gate_int64_t)(gate_uint64_t)self->storage_offset;
    }
    return result;
}
static gate_result_t memfile_reset(void* obj)
{
    memfile_t* const self = (memfile_t*)obj;
    self->storage_offset = 0;
    return GATE_RESULT_OK;
}
static gate_result_t memfile_close(void* obj, gate_enumint_t close_type)
{
    memfile_t* const self = (memfile_t*)obj;
    switch (close_type)
    {
    case GATE_STREAM_CLOSE_DEFAULT:
    case GATE_STREAM_CLOSE_INPUT:
    case GATE_STREAM_CLOSE_OUTPUT:
        self->closed = true;
        return GATE_RESULT_OK;
    default:
        return GATE_RESULT_NOTSUPPORTED;
    }
}

static GATE_INTERFACE_VTBL(gate_controlstream) memfile_vtbl;

static void init_memfile_vtbl()
{
    if (memfile_vtbl.get_interface_name == NULL)
    {
        GATE_INTERFACE_VTBL(gate_controlstream) local_vtbl =
        {
            &memfile_get_interface_name,
            &memfile_release,
            &memfile_retain,

            &memfile_read,
            &memfile_peek,
            &memfile_write,
            &memfile_flush,

            &memfile_get_resource,

            &memfile_can_read,
            &memfile_can_write,
            &memfile_get_size,
            &memfile_get_available,
            &memfile_seek,
            &memfile_reset,
            &memfile_close
        };
        memfile_vtbl = local_vtbl;
    }
}

gate_controlstream_t* gate_memfilestream_create(gate_size_t prealloc)
{
    gate_controlstream_t* ret = NULL;
    memfile_t* newstrm = NULL;

    do
    {
        newstrm = (memfile_t*)gate_mem_alloc(sizeof(memfile_t));
        if (!newstrm)
        {
            break;
        }
        gate_mem_clear(newstrm, sizeof(memfile_t));
        init_memfile_vtbl();
        newstrm->vtbl = &memfile_vtbl;
        gate_atomic_int_init(&newstrm->ref_counter, 1);
        newstrm->storage = gate_arraylist_create(sizeof(char), NULL, prealloc, NULL, NULL);
        if (!newstrm->storage)
        {
            break;
        }
        newstrm->closed = false;
        newstrm->storage_offset = 0;

        /* success case */
        ret = (gate_controlstream_t*)newstrm;
        newstrm = NULL;
    } while (0);

    if (newstrm)
    {
        memfile_destroy(newstrm);
    }
    return ret;
}

gate_controlstream_t* gate_memfilestream_share(gate_controlstream_t* ptr_memfile)
{
    gate_controlstream_t* ret = NULL;
    memfile_t* other_memfile = NULL;
    memfile_t* newstrm = NULL;

    do
    {
        if (!gate_object_implements_interface((gate_object_t*)ptr_memfile, GATE_INTERFACE_NAME_MEMFILE))
        {
            /* given stream is not a mem-file */
            break;
        }
        other_memfile = (memfile_t*)ptr_memfile;

        newstrm = (memfile_t*)gate_mem_alloc(sizeof(memfile_t));
        if (!newstrm)
        {
            break;
        }
        gate_mem_clear(newstrm, sizeof(memfile_t));
        init_memfile_vtbl();
        newstrm->vtbl = &memfile_vtbl;
        gate_atomic_int_init(&newstrm->ref_counter, 1);
        newstrm->storage = other_memfile->storage;
        if (!newstrm->storage)
        {
            break;
        }
        gate_arraylist_retain(newstrm->storage);
        newstrm->closed = false;
        newstrm->storage_offset = 0;

        /* success case */
        ret = (gate_controlstream_t*)newstrm;
        newstrm = NULL;
    } while (0);

    if (newstrm)
    {
        memfile_destroy(newstrm);
    }
    return ret;
}



/********************************
 * string stream implementation *
 ********************************/

static void gate_stringstream_release_impl(void* thisptr);
static int gate_stringstream_retain_impl(void* thisptr);
static char const* gate_stringstream_get_interface_name_impl(void* thisptr);
static gate_result_t gate_stringstream_read_impl(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
static gate_result_t gate_stringstream_peek_impl(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
static gate_result_t gate_stringstream_write_impl(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
static gate_result_t gate_stringstream_flush_impl(void* thisptr);

static char const* gate_stringstream_get_data_impl(void* thisptr);
static gate_size_t gate_stringstream_size_impl(void* thisptr);

static gate_result_t gate_stringstream_get_current_view_impl(void*, gate_string_t* ptr_output);
static gate_result_t gate_stringstream_to_string_impl(void*, gate_string_t* ptr_output);


static GATE_INTERFACE_VTBL(gate_stringstream) gate_stringstream_vtbl;
static void gate_init_stringstream_vtbl()
{
    if (!gate_stringstream_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_stringstream) local_vtbl =
        {
            &gate_stringstream_get_interface_name_impl,
            &gate_stringstream_release_impl,
            &gate_stringstream_retain_impl,

            &gate_stringstream_read_impl,
            &gate_stringstream_peek_impl,
            &gate_stringstream_write_impl,
            &gate_stringstream_flush_impl,

            &gate_stringstream_get_data_impl,
            &gate_stringstream_size_impl,
            &gate_stringstream_get_current_view_impl,
            &gate_stringstream_to_string_impl
        };
        gate_stringstream_vtbl = local_vtbl;
    }
}

typedef struct gate_stringstream_impl
{
    GATE_INTERFACE_VTBL(gate_stringstream)* vtbl;

    gate_atomic_int_t	ref_counter;
    gate_strbuilder_t* ptr_builder;
    gate_strbuilder_t	str_builder;
} gate_stringstream_impl_t;


gate_stringstream_t* gate_stringstream_create(gate_size_t capacity)
{
    gate_stringstream_impl_t* ret = gate_mem_alloc(sizeof(gate_stringstream_impl_t));
    if (ret != NULL)
    {
        gate_mem_clear(ret, sizeof(gate_stringstream_impl_t));
        gate_init_stringstream_vtbl();
        ret->vtbl = &gate_stringstream_vtbl;
        gate_atomic_int_init(&ret->ref_counter, 1);
        gate_strbuilder_create(&ret->str_builder, capacity);
        ret->ptr_builder = &ret->str_builder;
    }
    return (gate_stringstream_t*)ret;
}

gate_stringstream_t* gate_stringstream_create_builder(gate_strbuilder_t* external_strbuilder)
{
    gate_stringstream_impl_t* ret = gate_mem_alloc(sizeof(gate_stringstream_impl_t));
    if (ret != NULL)
    {
        gate_mem_clear(ret, sizeof(gate_stringstream_impl_t));
        gate_init_stringstream_vtbl();
        ret->vtbl = &gate_stringstream_vtbl;
        gate_atomic_int_init(&ret->ref_counter, 1);
        ret->ptr_builder = external_strbuilder;
    }
    return (gate_stringstream_t*)ret;
}


static void gate_stringstream_release_impl(void* ptr)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    if (gate_atomic_int_dec(&thisptr->ref_counter) == 0)
    {
        if (&thisptr->str_builder == thisptr->ptr_builder)
        {
            gate_strbuilder_release(thisptr->ptr_builder);
        }
        gate_mem_dealloc(thisptr);
    }
}
static int gate_stringstream_retain_impl(void* ptr)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    return (int)gate_atomic_int_inc(&thisptr->ref_counter);
}
static char const* gate_stringstream_get_interface_name_impl(void* ptr)
{
    (void)ptr;
    return GATE_INTERFACE_NAME_STRINGSTREAM;
}
static gate_result_t gate_stringstream_read_impl(void* ptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    gate_size_t bytesread = 0;
    gate_result_t result = gate_stringstream_peek_impl(ptr, buffer, bufferlength, &bytesread);

    if (GATE_SUCCEEDED(result))
    {
        gate_strbuilder_discard(thisptr->ptr_builder, bytesread);
        if (returned != NULL)
        {
            *returned = bytesread;
        }
    }
    return result;
}
static gate_result_t gate_stringstream_peek_impl(void* ptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    char const* dataptr = gate_strbuilder_ptr(thisptr->ptr_builder, 0);
    gate_size_t datalen = gate_strbuilder_length(thisptr->ptr_builder);
    if ((bufferlength > 0) && (buffer == NULL))
    {
        return GATE_RESULT_INVALIDARG;
    }
    if (bufferlength > datalen)
    {
        bufferlength = datalen;
    }
    if (bufferlength > 0)
    {
        if (NULL == gate_mem_copy(buffer, dataptr, bufferlength))
        {
            return GATE_RESULT_FAILED;
        }
    }
    if (returned != NULL)
    {
        *returned = bufferlength;
    }
    return GATE_RESULT_OK;
}
static gate_result_t gate_stringstream_write_impl(void* ptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    gate_size_t lenwritten;
    gate_result_t ret;
    lenwritten = gate_strbuilder_append_text(thisptr->ptr_builder, buffer, bufferlength);
    ret = (lenwritten == 0) ? GATE_RESULT_FAILED : GATE_RESULT_OK;

    if (written != NULL)
    {
        *written = lenwritten;
    }
    return ret;
}
static gate_result_t gate_stringstream_flush_impl(void* ptr)
{
    (void)ptr;
    return GATE_RESULT_OK;
}

static char const* gate_stringstream_get_data_impl(void* ptr)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    return gate_strbuilder_ptr(thisptr->ptr_builder, 0);
}
static gate_size_t gate_stringstream_size_impl(void* ptr)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    return gate_strbuilder_length(thisptr->ptr_builder);
}
static gate_result_t gate_stringstream_get_current_view_impl(void* ptr, gate_string_t* ptr_output)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    if (NULL != gate_string_create_static_len(ptr_output,
        gate_strbuilder_ptr(thisptr->ptr_builder, 0), gate_strbuilder_length(thisptr->ptr_builder)))
    {
        return GATE_RESULT_OK;
    }
    else
    {
        return GATE_RESULT_OUTOFMEMORY;
    }
}
static gate_result_t gate_stringstream_to_string_impl(void* ptr, gate_string_t* ptr_output)
{
    gate_stringstream_impl_t* thisptr = (gate_stringstream_impl_t*)ptr;
    if (NULL == gate_strbuilder_to_string(thisptr->ptr_builder, ptr_output))
    {
        return GATE_RESULT_FAILED;
    }
    else
    {
        return GATE_RESULT_OK;
    }
}



/********************************
 *  null stream implementation  *
 ********************************/

static void	gate_nullstream_release_impl(void* self)
{
    (void)self;
}
static int gate_nullstream_retain_impl(void* self)
{
    (void)self;
    return 1;
}
static char const* gate_nullstream_get_interface_name_impl(void* self)
{
    (void)self;
    return GATE_INTERFACE_NAME_STREAM;
}
static gate_result_t gate_nullstream_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    (void)self;
    (void)buffer;
    (void)bufferlength;
    if (returned != NULL)
    {
        *returned = 0;
    }
    return GATE_RESULT_OK;
}
static gate_result_t gate_nullstream_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    (void)self;
    (void)buffer;
    (void)bufferlength;
    if (returned != NULL)
    {
        *returned = 0;
    }
    return GATE_RESULT_OK;
}
static gate_result_t gate_nullstream_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
    (void)self;
    (void)buffer;
    if (written != NULL)
    {
        *written = bufferlength;
    }
    return GATE_RESULT_OK;
}
static gate_result_t gate_nullstream_flush_impl(void* self)
{
    (void)self;
    return GATE_RESULT_OK;
}

static GATE_INTERFACE_VTBL(gate_stream) gate_nullstream_vtbl;
static gate_stream_t global_null_stream =
{
    &gate_nullstream_vtbl
};
static void gate_nullstream_vtbl_init()
{
    if (!gate_nullstream_vtbl.get_interface_name)
    {

        GATE_INTERFACE_VTBL(gate_stream) local_vtbl =
        {
            &gate_nullstream_get_interface_name_impl,
            &gate_nullstream_release_impl,
            &gate_nullstream_retain_impl,
            &gate_nullstream_read_impl,
            &gate_nullstream_peek_impl,
            &gate_nullstream_write_impl,
            &gate_nullstream_flush_impl
        };
        gate_nullstream_vtbl = local_vtbl;
    }
}

#define GATE_INTERFACE_VTBL_INIT(ifname, vtbl_instance, vtbl_block) \
	if(! ( vtbl_instance . get_interface_name ) ) { \
		GATE_INTERFACE_VTBL(gate_stream) local_vtbl = { GATE_STRIP_PARENTHESES vtbl_block }; \
		vtbl_instance = local_vtbl; \
	}


gate_stream_t* gate_nullstream()
{
    gate_nullstream_vtbl_init();

    return &global_null_stream;
}



gate_result_t gate_stream_read_block(gate_stream_t* stream, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    gate_result_t result = GATE_RESULT_OK;
    gate_size_t totalread = 0;
    gate_size_t blockread;
    while (totalread < bufferlength)
    {
        blockread = 0;
        result = gate_stream_read(stream, &buffer[totalread], bufferlength - totalread, &blockread);
        if (GATE_FAILED(result))
        {
            break;
        }
        GATE_DEBUG_ASSERT(blockread <= (bufferlength - totalread));
        if (blockread == 0)
        {
            break;
        }
        totalread += blockread;
    }
    if (returned != NULL)
    {
        *returned = totalread;
    }
    return result;
}

gate_result_t gate_stream_read_line(gate_stream_t* stream, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
    gate_result_t ret = GATE_RESULT_OK;
    gate_size_t pos = 0;
    gate_size_t lenread;

    while (pos != bufferlength)
    {
        ret = gate_stream_read(stream, &buffer[pos], 1, &lenread);
        GATE_BREAK_IF_FAILED(ret);
        if (lenread == 0)
        {
            /* end of stream */
            break;
        }
        if (buffer[pos] == '\b')
        {
            if (pos > 0)
            {
                --pos;
            }
            continue;
        }

        if (buffer[pos] == '\n')
        {
            /* end of line */
            ++pos;
            break;
        }
        ++pos;
    }
    if (returned != NULL)
    {
        *returned = pos;
    }
    return ret;
}


gate_result_t gate_stream_write_block(gate_stream_t* stream, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
    gate_result_t result = GATE_RESULT_OK;
    gate_size_t totalwritten = 0;
    gate_size_t blockwritten;
    while (totalwritten < bufferlength)
    {
        blockwritten = 0;
        result = gate_stream_write(stream, &buffer[totalwritten], bufferlength - totalwritten, &blockwritten);
        /*result = stream->vtbl->write(stream, &buffer[totalwritten], bufferlength - totalwritten, &blockwritten);*/
        if (GATE_FAILED(result))
        {
            break;
        }
        GATE_DEBUG_ASSERT(blockwritten <= (bufferlength - totalwritten));
        if (blockwritten == 0)
        {
            result = GATE_RESULT_CANCELED;
            break;
        }
        totalwritten += blockwritten;
    }
    if (GATE_SUCCEEDED(result))
    {
        GATE_DEBUG_ASSERT(bufferlength == totalwritten);
    }
    if (written != NULL)
    {
        *written = totalwritten;
    }
    return result;
}


gate_result_t gate_stream_print_cstr(gate_stream_t* stream, gate_cstr_t text)
{
    gate_size_t textlength = gate_str_length(text);
    gate_size_t written;
    return gate_stream_write_block(stream, text, textlength, &written);
}
gate_result_t gate_stream_print_wstr(gate_stream_t* stream, gate_wstr_t wstr, gate_size_t wstr_len)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    char buffer[8192 + 4096];
    char* ptr_buffer = &buffer[0];
    gate_size_t max_utf8_length = wstr_len * 3 + 1;
    gate_size_t buffer_used;
    gate_string_t tmp = GATE_STRING_INIT_EMPTY;

    do
    {
        if (max_utf8_length >= sizeof(buffer))
        {
            ptr_buffer = (char*)gate_mem_alloc(max_utf8_length);
            if (ptr_buffer == NULL)
            {
                ret = GATE_RESULT_OUTOFMEMORY;
                break;
            }
        }

        buffer_used = gate_wstr_to_cstr(wstr, wstr_len, ptr_buffer, max_utf8_length);
        gate_string_create_static_len(&tmp, ptr_buffer, buffer_used);
        ret = gate_stream_print_string(stream, &tmp);
    } while (0);

    if ((ptr_buffer != NULL) && (ptr_buffer != &buffer[0]))
    {
        gate_mem_dealloc(ptr_buffer);
    }
    return ret;
}

gate_result_t gate_stream_println_cstr(gate_stream_t* stream, char const* text)
{
    gate_size_t textlength = gate_str_length(text);
    gate_result_t result = GATE_RESULT_OK;
    if (textlength > 0)
    {
        result = gate_stream_write_block(stream, text, textlength, NULL);
    }
    if (GATE_SUCCEEDED(result))
    {
        result = gate_stream_write_block(stream, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH, NULL);
    }
    return result;
}
gate_result_t gate_stream_print_int(gate_stream_t* stream, gate_int32_t num)
{
    gate_char8_t buffer[24];
    gate_size_t bufferlength = gate_str_print_int32(buffer, sizeof(buffer), num);
    return gate_stream_write_block(stream, buffer, bufferlength, NULL);
}
gate_result_t gate_stream_print_uint(gate_stream_t* stream, gate_uint32_t num)
{
    gate_char8_t buffer[24];
    gate_size_t bufferlength = gate_str_print_uint32(buffer, sizeof(buffer), num);
    return gate_stream_write_block(stream, buffer, bufferlength, NULL);
}
gate_result_t gate_stream_print_int64(gate_stream_t* stream, gate_int64_t num)
{
    gate_char8_t buffer[48];
    gate_size_t bufferlength = gate_str_print_int64(buffer, sizeof(buffer), num);
    return gate_stream_write_block(stream, buffer, bufferlength, NULL);
}
gate_result_t gate_stream_print_uint64(gate_stream_t* stream, gate_uint64_t num)
{
    gate_char8_t buffer[48];
    gate_size_t bufferlength = gate_str_print_uint64(buffer, sizeof(buffer), num);
    return gate_stream_write_block(stream, buffer, bufferlength, NULL);
}
gate_result_t gate_stream_print_real(gate_stream_t* stream, gate_real64_t num, unsigned intlen, unsigned decimallen, unsigned grouplen)
{
    gate_char8_t buffer[64];
    gate_size_t bufferlength = gate_str_print_real(buffer, sizeof(buffer), num, intlen, decimallen, grouplen);
    return gate_stream_write_block(stream, buffer, bufferlength, NULL);
}
gate_result_t gate_stream_print_string(gate_stream_t* stream, gate_string_t const* text)
{
    if (text != NULL)
    {
        return gate_stream_write_block(stream, text->str, text->length, NULL);
    }
    else
    {
        return GATE_RESULT_OK;
    }
}


gate_result_t gate_stream_print(gate_stream_t* stream, ...)
{
    gate_result_t ret = GATE_RESULT_OK;
    int print_type;
    gate_bool_t continue_loop = true;
    gate_bool_t val_bool;
    char val_chr[2] = GATE_INIT_EMPTY;
    char const* val_cstr;
    gate_string_t const* val_str;
    gate_int8_t val_i8;
    gate_uint8_t val_ui8;
    gate_int16_t val_i16;
    gate_uint16_t val_ui16;
    gate_int32_t val_i32;
    gate_uint32_t val_ui32;
    gate_int64_t val_i64;
    gate_uint64_t val_ui64;
    gate_real32_t val_r32;
    gate_real64_t val_r64;
    void* val_ptr;
    char const* ptr_text = NULL;
    char buffer[512];
    gate_size_t text_len;
    va_list vl;
    va_start(vl, stream);

    /* INFO: types small than [int] are converted to [int]
       https://stackoverflow.com/questions/23983471/char-is-promoted-to-int-when-passed-through-in-c
    */

    while (continue_loop)
    {
        print_type = va_arg(vl, int);
        ptr_text = NULL;
        text_len = 0;
        switch (print_type)
        {
        case GATE_PRINT_END:
        {
            text_len = 0;
            continue_loop = false;
            break;
        }
        case GATE_PRINT_CHAR:
        {
            val_chr[0] = (char)va_arg(vl, int);
            ptr_text = &val_chr[0];
            text_len = 1;
            break;
        }
        case GATE_PRINT_NEWLINE:
        {
            ptr_text = GATE_STR_NEWLINE;
            text_len = GATE_STR_NEWLINE_LENGTH;
            break;
        }
        case GATE_PRINT_CR:
        {
            ptr_text = GATE_STR_CR;
            text_len = 1;
            break;
        }
        case GATE_PRINT_LF:
        {
            ptr_text = GATE_STR_LF;
            text_len = 1;
            break;
        }
        case GATE_PRINT_CRLF:
        {
            ptr_text = GATE_STR_CRLF;
            text_len = 2;
            break;
        }
        case GATE_PRINT_CSTR:
        {
            val_cstr = va_arg(vl, char const*);
            ptr_text = val_cstr;
            text_len = gate_str_length(val_cstr);
            break;
        }
        case GATE_PRINT_STRING:
        {
            val_str = va_arg(vl, gate_string_t const*);
            ptr_text = val_str->str;
            text_len = val_str->length;
            break;
        }
        case GATE_PRINT_BOOL:
        {
            val_bool = (gate_bool_t)va_arg(vl, int);
            ptr_text = val_bool ? "true" : "false";
            text_len = val_bool ? 4 : 5;
            break;
        }
        case GATE_PRINT_I8:
        {
            val_i8 = (gate_int8_t)va_arg(vl, int);
            text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i8);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_UI8:
        {
            val_ui8 = (gate_uint8_t)va_arg(vl, int);
            text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui8);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_I16:
        {
            val_i16 = (gate_int16_t)va_arg(vl, int);
            text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i16);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_UI16:
        {
            val_ui16 = (gate_uint16_t)va_arg(vl, int);
            text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui16);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_I32:
        {
            val_i32 = va_arg(vl, gate_int32_t);
            text_len = gate_str_print_int32(buffer, sizeof(buffer), val_i32);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_UI32:
        {
            val_ui32 = va_arg(vl, gate_uint32_t);
            text_len = gate_str_print_uint32(buffer, sizeof(buffer), val_ui32);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_I64:
        {
            val_i64 = va_arg(vl, gate_int64_t);
            text_len = gate_str_print_int64(buffer, sizeof(buffer), val_i64);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_UI64:
        {
            val_ui64 = va_arg(vl, gate_uint64_t);
            text_len = gate_str_print_uint64(buffer, sizeof(buffer), val_ui64);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_H8:
        {
            val_ui8 = (gate_uint8_t)va_arg(vl, unsigned int);
            text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui8, 1, false);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_H16:
        {
            val_ui16 = (gate_uint16_t)va_arg(vl, unsigned int);
            text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui16, 2, false);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_H32:
        {
            val_ui32 = (gate_uint32_t)va_arg(vl, gate_uint32_t);
            text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui32, 4, false);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_H64:
        {
            val_ui64 = (gate_uint64_t)va_arg(vl, gate_uint64_t);
            text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui64, 8, false);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_R32:
        {
            val_r32 = (gate_real32_t)va_arg(vl, double);
            text_len = gate_str_print_real(buffer, sizeof(buffer), val_r32, 0, 6, 0);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_R64:
        {
            val_r64 = va_arg(vl, gate_real64_t);
            text_len = gate_str_print_real(buffer, sizeof(buffer), val_r64, 0, 6, 0);
            ptr_text = buffer;
            break;
        }
        case GATE_PRINT_ADDRESS:
        {
            val_ptr = va_arg(vl, void*);
            if (sizeof(void*) == 2)
            {
                text_len = gate_str_print_hex_uint16(buffer, sizeof(buffer), (gate_uint16_t)(gate_uintptr_t)val_ptr, true);
            }
            else if (sizeof(void*) == 4)
            {
                text_len = gate_str_print_hex_uint32(buffer, sizeof(buffer), (gate_uint32_t)(gate_uintptr_t)val_ptr, true);
            }
            else
            {
                text_len = gate_str_print_hex_uint64(buffer, sizeof(buffer), (gate_uint64_t)(gate_uintptr_t)val_ptr, true);
            }
            ptr_text = buffer;
            break;
        }
        default:
        {
            ret = GATE_RESULT_INVALIDARG;
            continue_loop = false;
            break;
        }
        }

        ret = gate_stream_write_block(stream, ptr_text, text_len, &text_len);
        GATE_BREAK_IF_FAILED(ret);
    }
    va_end(vl);
    return ret;
}

static gate_result_t gate_stream_peek_utf8_char(gate_stream_t* stream, gate_char32_t* chr, gate_size_t* decoded)
{
    char utf8_buffer[16];
    gate_size_t bytes_returned = 0;
    gate_size_t bytes_decoded = 0;
    gate_result_t ret = GATE_RESULT_FAILED;

    ret = gate_stream_peek(stream, utf8_buffer, sizeof(utf8_buffer), &bytes_returned);
    if (GATE_SUCCEEDED(ret))
    {
        if (bytes_returned == 0)
        {
            /* end of stream reached */
            *chr = 0;
            *decoded = 0;
        }
        else
        {
            bytes_decoded = gate_char_read_utf8(utf8_buffer, bytes_returned, chr);
            if (bytes_decoded == 0)
            {
                /* utf8 error, return byte value instead */
                *chr = (gate_char32_t)(gate_uint8_t)utf8_buffer[0];
                *decoded = 1;
            }
            else
            {
                *decoded = bytes_decoded;
            }
        }
    }
    return ret;
}

gate_size_t	gate_stream_scan_char32(gate_stream_t* stream, gate_char32_t* chr)
{
    /* TODO */
    return 0;
}
gate_size_t	gate_stream_scan_char16(gate_stream_t* stream, gate_char16_t* chr)
{
    /* TODO */
    return 0;
}
gate_size_t	gate_stream_scan_char8(gate_stream_t* stream, gate_char8_t* chr)
{
    /* TODO */
    return 0;
}


gate_size_t	gate_stream_scan_int(gate_stream_t* stream, gate_int32_t* num)
{
    /* TODO */
    return 0;
}
gate_size_t	gate_stream_scan_uint(gate_stream_t* stream, gate_uint32_t* num)
{
    /* TODO */
    return 0;
}
gate_size_t	gate_stream_scan_int64(gate_stream_t* stream, gate_int64_t* num)
{
    /* TODO */
    return 0;
}
gate_size_t	gate_stream_scan_uint64(gate_stream_t* stream, gate_uint64_t* num)
{
    /* TODO */
    return 0;
}
gate_size_t	gate_stream_scan_real(gate_stream_t* stream, gate_real64_t* num)
{
    /* TODO */
    return 0;
}
gate_size_t	gate_stream_scan_string(gate_stream_t* stream, gate_string_t* text)
{
    /* TODO */
    return 0;
}




gate_result_t gate_stream_transfer_buffer(gate_stream_t* src, gate_stream_t* dst, char* buffer, gate_size_t buffersize)
{
    gate_size_t bytes_read;
    gate_size_t bytes_written;
    gate_result_t result;
    for (;;)
    {
        result = gate_stream_read(src, buffer, buffersize, &bytes_read);
        if (GATE_FAILED(result))
        {
            break;
        }
        if (bytes_read == 0)
        {
            /* end of stream reached */
            break;
        }
        result = gate_stream_write_block(dst, buffer, bytes_read, &bytes_written);
        if (GATE_FAILED(result))
        {
            break;
        }
    }
    return result;
}

gate_result_t gate_stream_transfer_limit(gate_stream_t* src, gate_uint64_t limit, gate_stream_t* dst, gate_uint64_t* transferred)
{
    char buffer[GATE_MAX_COPYBUFFER_LENGTH];
    static gate_size_t const buffer_size = sizeof(buffer);
    gate_size_t next_size;
    gate_size_t bytes_read;
    gate_size_t bytes_written;
    gate_result_t result = GATE_RESULT_OK;
    gate_uint64_t counter = 0;
    while (limit > 0)
    {
        next_size = (limit > buffer_size) ? buffer_size : (gate_size_t)limit;
        result = gate_stream_read(src, buffer, next_size, &bytes_read);
        if (GATE_FAILED(result))
        {
            break;
        }
        if (bytes_read == 0)
        {
            /* end of stream reached */
            break;
        }
        GATE_DEBUG_ASSERT(limit >= bytes_read);
        limit -= bytes_read;
        result = gate_stream_write_block(dst, buffer, bytes_read, &bytes_written);
        if (GATE_FAILED(result))
        {
            break;
        }
        GATE_DEBUG_ASSERT(bytes_read == bytes_written);
        counter += bytes_written;
    }
    if (transferred)
    {
        *transferred = counter;
    }
    return result;
}


gate_result_t gate_stream_transfer(gate_stream_t* src, gate_stream_t* dst)
{
    char buffer[GATE_MAX_COPYBUFFER_LENGTH];
    return gate_stream_transfer_buffer(src, dst, buffer, sizeof(buffer));
}

gate_result_t gate_stream_skip(gate_stream_t* src, gate_uint64_t count, gate_uint64_t* bytes_skipped)
{
    char buffer[GATE_MAX_COPYBUFFER_LENGTH];
    gate_size_t nextlen;
    gate_size_t returned = 1;
    gate_result_t result = GATE_RESULT_OK;
    gate_uint64_t actual_skipped = 0;

    while ((count != 0) && (returned != 0))
    {
        nextlen = (count > GATE_MAX_COPYBUFFER_LENGTH) ? GATE_MAX_COPYBUFFER_LENGTH : (gate_size_t)count;
        result = gate_stream_read_block(src, buffer, nextlen, &returned);
        GATE_BREAK_IF_FAILED(result);
        GATE_DEBUG_ASSERT(returned <= nextlen);
        actual_skipped += returned;
        count -= returned;
    }
    if (bytes_skipped)
    {
        *bytes_skipped = actual_skipped;
    }
    return result;
}