23 #ifndef WINPR_UTILS_STREAM_H
24 #define WINPR_UTILS_STREAM_H
26 #include <winpr/winpr.h>
27 #include <winpr/wtypes.h>
28 #include <winpr/endian.h>
29 #include <winpr/synch.h>
30 #include <winpr/assert.h>
31 #include <winpr/cast.h>
32 #include <winpr/wlog.h>
39 typedef struct s_wStreamPool wStreamPool;
50 BOOL isAllocatedStream;
54 static INLINE
size_t Stream_Capacity(
const wStream* _s);
55 WINPR_API
size_t Stream_GetRemainingCapacity(
const wStream* _s);
56 WINPR_API
size_t Stream_GetRemainingLength(
const wStream* _s);
58 WINPR_API BOOL Stream_EnsureCapacity(
wStream* s,
size_t size);
59 WINPR_API BOOL Stream_EnsureRemainingCapacity(
wStream* s,
size_t size);
61 #define WINPR_STREAM_CAST(t, val) WINPR_CXX_COMPAT_CAST(t, val)
63 #define Stream_CheckAndLogRequiredCapacityOfSize(tag, s, nmemb, size) \
64 Stream_CheckAndLogRequiredCapacityEx(tag, WLOG_WARN, s, nmemb, size, "%s(%s:%" PRIuz ")", \
65 __func__, __FILE__, (size_t)__LINE__)
66 #define Stream_CheckAndLogRequiredCapacity(tag, s, len) \
67 Stream_CheckAndLogRequiredCapacityOfSize((tag), (s), (len), 1)
69 WINPR_API BOOL Stream_CheckAndLogRequiredCapacityEx(
const char* tag, DWORD level,
wStream* s,
70 size_t nmemb,
size_t size,
const char* fmt,
72 WINPR_API BOOL Stream_CheckAndLogRequiredCapacityExVa(
const char* tag, DWORD level,
wStream* s,
73 size_t nmemb,
size_t size,
74 const char* fmt, va_list args);
76 #define Stream_CheckAndLogRequiredCapacityOfSizeWLog(log, s, nmemb, size) \
77 Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, nmemb, size, "%s(%s:%" PRIuz ")", \
78 __func__, __FILE__, (size_t)__LINE__)
80 #define Stream_CheckAndLogRequiredCapacityWLog(log, s, len) \
81 Stream_CheckAndLogRequiredCapacityOfSizeWLog((log), (s), (len), 1)
83 WINPR_API BOOL Stream_CheckAndLogRequiredCapacityWLogEx(wLog* log, DWORD level,
wStream* s,
84 size_t nmemb,
size_t size,
85 const char* fmt, ...);
86 WINPR_API BOOL Stream_CheckAndLogRequiredCapacityWLogExVa(wLog* log, DWORD level,
wStream* s,
87 size_t nmemb,
size_t size,
88 const char* fmt, va_list args);
90 WINPR_API
void Stream_Free(
wStream* s, BOOL bFreeBuffer);
92 WINPR_ATTR_MALLOC(Stream_Free, 1)
93 WINPR_API
wStream* Stream_New(BYTE* buffer,
size_t size);
94 WINPR_API
wStream* Stream_StaticConstInit(
wStream* s, const BYTE* buffer,
size_t size);
95 WINPR_API
wStream* Stream_StaticInit(
wStream* s, BYTE* buffer,
size_t size);
97 #define Stream_CheckAndLogRequiredLengthOfSize(tag, s, nmemb, size) \
98 Stream_CheckAndLogRequiredLengthEx(tag, WLOG_WARN, s, nmemb, size, "%s(%s:%" PRIuz ")", \
99 __func__, __FILE__, (size_t)__LINE__)
100 #define Stream_CheckAndLogRequiredLength(tag, s, len) \
101 Stream_CheckAndLogRequiredLengthOfSize(tag, s, len, 1)
103 WINPR_API BOOL Stream_CheckAndLogRequiredLengthEx(
const char* tag, DWORD level,
wStream* s,
104 size_t nmemb,
size_t size,
const char* fmt,
106 WINPR_API BOOL Stream_CheckAndLogRequiredLengthExVa(
const char* tag, DWORD level,
wStream* s,
107 size_t nmemb,
size_t size,
const char* fmt,
110 #define Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, nmemb, size) \
111 Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, nmemb, size, "%s(%s:%" PRIuz ")", \
112 __func__, __FILE__, (size_t)__LINE__)
113 #define Stream_CheckAndLogRequiredLengthWLog(log, s, len) \
114 Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, len, 1)
116 WINPR_API BOOL Stream_CheckAndLogRequiredLengthWLogEx(wLog* log, DWORD level,
wStream* s,
117 size_t nmemb,
size_t size,
118 const char* fmt, ...);
119 WINPR_API BOOL Stream_CheckAndLogRequiredLengthWLogExVa(wLog* log, DWORD level,
wStream* s,
120 size_t nmemb,
size_t size,
121 const char* fmt, va_list args);
123 static INLINE
void Stream_Seek(
wStream* s,
size_t _offset)
126 WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= _offset);
127 s->pointer += (_offset);
130 static INLINE
void Stream_Rewind(
wStream* s,
size_t _offset)
134 WINPR_ASSERT(s->buffer <= s->pointer);
135 cur = WINPR_STREAM_CAST(
size_t, s->pointer - s->buffer);
136 WINPR_ASSERT(cur >= _offset);
138 s->pointer -= (_offset);
140 s->pointer = s->buffer;
143 static INLINE UINT8 stream_read_u8(
wStream* _s, BOOL seek)
146 WINPR_ASSERT(Stream_GetRemainingLength(_s) >=
sizeof(UINT8));
148 const UINT8 v = winpr_Data_Get_UINT8(_s->pointer);
150 Stream_Seek(_s,
sizeof(UINT8));
154 static INLINE INT8 stream_read_i8(
wStream* _s, BOOL seek)
156 const INT8 v = winpr_Data_Get_INT8(_s->pointer);
158 Stream_Seek(_s,
sizeof(INT8));
162 static INLINE UINT16 stream_read_u16_le(
wStream* _s, BOOL seek)
164 const size_t typesize =
sizeof(UINT16);
166 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
168 const UINT16 v = winpr_Data_Get_UINT16(_s->pointer);
170 Stream_Seek(_s, typesize);
174 static INLINE UINT16 stream_read_u16_be(
wStream* _s, BOOL seek)
176 const size_t typesize =
sizeof(UINT16);
178 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
180 const UINT16 v = winpr_Data_Get_UINT16_BE(_s->pointer);
182 Stream_Seek(_s, typesize);
186 static INLINE INT16 stream_read_i16_le(
wStream* _s, BOOL seek)
188 const size_t typesize =
sizeof(INT16);
190 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
192 const INT16 v = winpr_Data_Get_INT16(_s->pointer);
194 Stream_Seek(_s, typesize);
198 static INLINE INT16 stream_read_i16_be(
wStream* _s, BOOL seek)
200 const size_t typesize =
sizeof(INT16);
202 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
204 const INT16 v = winpr_Data_Get_INT16_BE(_s->pointer);
206 Stream_Seek(_s, typesize);
210 static INLINE UINT32 stream_read_u32_le(
wStream* _s, BOOL seek)
212 const size_t typesize =
sizeof(UINT32);
214 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
216 const UINT32 v = winpr_Data_Get_UINT32(_s->pointer);
218 Stream_Seek(_s, typesize);
222 static INLINE UINT32 stream_read_u32_be(
wStream* _s, BOOL seek)
224 const size_t typesize =
sizeof(UINT32);
226 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
228 const UINT32 v = winpr_Data_Get_UINT32_BE(_s->pointer);
230 Stream_Seek(_s, typesize);
234 static INLINE INT32 stream_read_i32_le(
wStream* _s, BOOL seek)
236 const size_t typesize =
sizeof(INT32);
238 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
240 const INT32 v = winpr_Data_Get_INT32(_s->pointer);
242 Stream_Seek(_s, typesize);
246 static INLINE INT32 stream_read_i32_be(
wStream* _s, BOOL seek)
248 const size_t typesize =
sizeof(INT32);
250 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
252 const INT32 v = winpr_Data_Get_INT32_BE(_s->pointer);
254 Stream_Seek(_s, typesize);
258 static INLINE UINT64 stream_read_u64_le(
wStream* _s, BOOL seek)
260 const size_t typesize =
sizeof(UINT64);
262 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
264 const UINT64 v = winpr_Data_Get_UINT64(_s->pointer);
266 Stream_Seek(_s, typesize);
270 static INLINE UINT64 stream_read_u64_be(
wStream* _s, BOOL seek)
272 const size_t typesize =
sizeof(UINT64);
274 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
276 const UINT64 v = winpr_Data_Get_UINT64_BE(_s->pointer);
278 Stream_Seek(_s, typesize);
282 static INLINE INT64 stream_read_i64_le(
wStream* _s, BOOL seek)
284 const size_t typesize =
sizeof(INT64);
286 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
288 const INT64 v = winpr_Data_Get_INT64(_s->pointer);
290 Stream_Seek(_s, typesize);
294 static INLINE INT64 stream_read_i64_be(
wStream* _s, BOOL seek)
296 const size_t typesize =
sizeof(INT64);
298 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
300 const INT64 v = winpr_Data_Get_INT64_BE(_s->pointer);
302 Stream_Seek(_s, typesize);
312 static INLINE UINT8 Stream_Get_UINT8(
wStream* _s)
314 return stream_read_u8(_s, TRUE);
323 static INLINE INT8 Stream_Get_INT8(
wStream* _s)
325 return stream_read_i8(_s, TRUE);
334 static INLINE UINT16 Stream_Get_UINT16(
wStream* _s)
336 return stream_read_u16_le(_s, TRUE);
345 static INLINE INT16 Stream_Get_INT16(
wStream* _s)
347 return stream_read_i16_le(_s, TRUE);
356 static INLINE UINT16 Stream_Get_UINT16_BE(
wStream* _s)
358 return stream_read_u16_be(_s, TRUE);
367 static INLINE INT16 Stream_Get_INT16_BE(
wStream* _s)
369 return stream_read_i16_be(_s, TRUE);
378 static INLINE UINT32 Stream_Get_UINT32(
wStream* _s)
380 return stream_read_u32_le(_s, TRUE);
389 static INLINE INT32 Stream_Get_INT32(
wStream* _s)
391 return stream_read_i32_le(_s, TRUE);
400 static INLINE UINT32 Stream_Get_UINT32_BE(
wStream* _s)
402 return stream_read_u32_be(_s, TRUE);
411 static INLINE INT32 Stream_Get_INT32_BE(
wStream* _s)
413 return stream_read_i32_be(_s, TRUE);
422 static INLINE UINT64 Stream_Get_UINT64(
wStream* _s)
424 return stream_read_u64_le(_s, TRUE);
433 static INLINE INT64 Stream_Get_INT64(
wStream* _s)
435 return stream_read_i64_le(_s, TRUE);
444 static INLINE UINT64 Stream_Get_UINT64_BE(
wStream* _s)
446 return stream_read_u64_be(_s, TRUE);
455 static INLINE INT64 Stream_Get_INT64_BE(
wStream* _s)
457 return stream_read_i64_be(_s, TRUE);
466 static INLINE UINT8 Stream_Peek_Get_UINT8(
wStream* _s)
468 return stream_read_u8(_s, FALSE);
477 static INLINE INT8 Stream_Peek_Get_INT8(
wStream* _s)
479 return stream_read_i8(_s, FALSE);
488 static INLINE UINT16 Stream_Peek_Get_UINT16(
wStream* _s)
490 return stream_read_u16_le(_s, FALSE);
499 static INLINE INT16 Stream_Peek_Get_INT16(
wStream* _s)
501 return stream_read_i16_le(_s, FALSE);
510 static INLINE UINT16 Stream_Peek_Get_UINT16_BE(
wStream* _s)
512 return stream_read_u16_be(_s, FALSE);
521 static INLINE INT16 Stream_Peek_Get_INT16_BE(
wStream* _s)
523 return stream_read_i16_be(_s, FALSE);
532 static INLINE UINT32 Stream_Peek_Get_UINT32(
wStream* _s)
534 return stream_read_u32_le(_s, FALSE);
543 static INLINE INT32 Stream_Peek_Get_INT32(
wStream* _s)
545 return stream_read_i32_le(_s, FALSE);
554 static INLINE UINT32 Stream_Peek_Get_UINT32_BE(
wStream* _s)
556 return stream_read_u32_be(_s, FALSE);
565 static INLINE INT32 Stream_Peek_Get_INT32_BE(
wStream* _s)
567 return stream_read_i32_be(_s, FALSE);
576 static INLINE UINT64 Stream_Peek_Get_UINT64(
wStream* _s)
578 return stream_read_u64_le(_s, FALSE);
587 static INLINE INT64 Stream_Peek_Get_INT64(
wStream* _s)
589 return stream_read_i64_le(_s, FALSE);
598 static INLINE UINT64 Stream_Peek_Get_UINT64_BE(
wStream* _s)
600 return stream_read_u64_be(_s, FALSE);
609 static INLINE INT64 Stream_Peek_Get_INT64_BE(
wStream* _s)
611 return stream_read_i64_be(_s, FALSE);
614 #define Stream_Read_UINT8(_s, _v) \
617 _v = stream_read_u8(_s, TRUE); \
620 #define Stream_Read_INT8(_s, _v) \
623 _v = stream_read_i8(_s, TRUE); \
626 #define Stream_Read_UINT16(_s, _v) \
629 _v = stream_read_u16_le(_s, TRUE); \
632 #define Stream_Read_INT16(_s, _v) \
635 _v = stream_read_i16_le(_s, TRUE); \
638 #define Stream_Read_UINT16_BE(_s, _v) \
641 _v = stream_read_u16_be(_s, TRUE); \
644 #define Stream_Read_INT16_BE(_s, _v) \
647 _v = stream_read_i16_be(_s, TRUE); \
650 #define Stream_Read_UINT32(_s, _v) \
653 _v = stream_read_u32_le(_s, TRUE); \
656 #define Stream_Read_INT32(_s, _v) \
659 _v = stream_read_i32_le(_s, TRUE); \
662 #define Stream_Read_UINT32_BE(_s, _v) \
665 _v = stream_read_u32_be(_s, TRUE); \
668 #define Stream_Read_INT32_BE(_s, _v) \
671 _v = stream_read_i32_be(_s, TRUE); \
674 #define Stream_Read_UINT64(_s, _v) \
677 _v = stream_read_u64_le(_s, TRUE); \
680 #define Stream_Read_INT64(_s, _v) \
683 _v = stream_read_i64_le(_s, TRUE); \
686 #define Stream_Read_UINT64_BE(_s, _v) \
689 _v = stream_read_u64_be(_s, TRUE); \
692 #define Stream_Read_INT64_BE(_s, _v) \
695 _v = stream_read_i64_be(_s, TRUE); \
698 static INLINE
void Stream_Read(
wStream* _s,
void* _b,
size_t _n)
701 WINPR_ASSERT(_b || (_n == 0));
702 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= _n);
703 memcpy(_b, (_s->pointer), (_n));
707 #define Stream_Peek_UINT8(_s, _v) \
710 _v = stream_read_u8(_s, FALSE); \
713 #define Stream_Peek_INT8(_s, _v) \
716 _v = stream_read_i8(_s, FALSE); \
719 #define Stream_Peek_UINT16(_s, _v) \
722 _v = stream_read_u16_le(_s, FALSE); \
725 #define Stream_Peek_INT16(_s, _v) \
728 _v = stream_read_i16_le(_s, FALSE); \
731 #define Stream_Peek_UINT16_BE(_s, _v) \
734 _v = stream_read_u16_be(_s, FALSE); \
737 #define Stream_Peek_INT16_BE(_s, _v) \
740 _v = stream_read_i16_be(_s, FALSE); \
743 #define Stream_Peek_UINT32(_s, _v) \
746 _v = stream_read_u32_le(_s, FALSE); \
749 #define Stream_Peek_INT32(_s, _v) \
752 _v = stream_read_i32_le(_s, FALSE); \
755 #define Stream_Peek_UINT32_BE(_s, _v) \
758 _v = stream_read_u32_be(_s, FALSE); \
761 #define Stream_Peek_INT32_BE(_s, _v) \
764 _v = stream_read_i32_be(_s, FALSE); \
767 #define Stream_Peek_UINT64(_s, _v) \
770 _v = stream_read_u64_le(_s, FALSE); \
773 #define Stream_Peek_INT64(_s, _v) \
776 _v = stream_read_i64_le(_s, FALSE); \
779 #define Stream_Peek_UINT64_BE(_s, _v) \
782 _v = stream_read_u64_be(_s, FALSE); \
785 #define Stream_Peek_INT64_BE(_s, _v) \
788 _v = stream_read_i64_be(_s, FALSE); \
791 static INLINE
void Stream_Peek(
const wStream* _s,
void* _b,
size_t _n)
794 WINPR_ASSERT(_b || (_n == 0));
795 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= _n);
796 memcpy(_b, (_s->pointer), (_n));
799 #define Stream_Write_INT8(s, v) \
802 WINPR_ASSERT((v) <= INT8_MAX); \
803 WINPR_ASSERT((v) >= INT8_MIN); \
804 Stream_Write_INT8_unchecked((s), (v)); \
814 static INLINE
void Stream_Write_INT8_unchecked(
wStream* _s, INT8 _v)
817 WINPR_ASSERT(_s->pointer);
818 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 1);
820 winpr_Data_Write_INT8(_s->pointer, _v);
824 #define Stream_Write_UINT8(s, v) \
827 WINPR_ASSERT((v) <= UINT8_MAX); \
828 WINPR_ASSERT((v) >= 0); \
829 Stream_Write_UINT8_unchecked((s), (v)); \
839 static INLINE
void Stream_Write_UINT8_unchecked(
wStream* _s, UINT8 _v)
842 WINPR_ASSERT(_s->pointer);
843 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 1);
845 winpr_Data_Write_UINT8(_s->pointer, _v);
849 #define Stream_Write_INT16(s, v) \
852 WINPR_ASSERT((v) >= INT16_MIN); \
853 WINPR_ASSERT((v) <= INT16_MAX); \
854 Stream_Write_INT16_unchecked((s), (v)); \
865 static INLINE
void Stream_Write_INT16_unchecked(
wStream* _s, INT16 _v)
868 WINPR_ASSERT(_s->pointer);
869 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 2);
871 winpr_Data_Write_INT16(_s->pointer, _v);
875 #define Stream_Write_UINT16(s, v) \
878 WINPR_ASSERT((v) <= UINT16_MAX); \
879 WINPR_ASSERT((v) >= 0); \
880 Stream_Write_UINT16_unchecked((s), (v)); \
891 static INLINE
void Stream_Write_UINT16_unchecked(
wStream* _s, UINT16 _v)
894 WINPR_ASSERT(_s->pointer);
895 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 2);
897 winpr_Data_Write_UINT16(_s->pointer, _v);
901 #define Stream_Write_UINT16_BE(s, v) \
904 WINPR_ASSERT((v) <= UINT16_MAX); \
905 WINPR_ASSERT((v) >= 0); \
906 Stream_Write_UINT16_BE_unchecked((s), (v)); \
917 static INLINE
void Stream_Write_UINT16_BE_unchecked(
wStream* _s, UINT16 _v)
920 WINPR_ASSERT(_s->pointer);
921 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 2);
923 winpr_Data_Write_UINT16_BE(_s->pointer, _v);
927 #define Stream_Write_INT16_BE(s, v) \
930 WINPR_ASSERT((v) <= INT16_MAX); \
931 WINPR_ASSERT((v) >= INT16_MIN); \
932 Stream_Write_INT16_BE_unchecked((s), (v)); \
945 static INLINE
void Stream_Write_INT16_BE_unchecked(
wStream* _s, INT16 _v)
948 WINPR_ASSERT(_s->pointer);
949 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 2);
951 winpr_Data_Write_INT16_BE(_s->pointer, _v);
955 #define Stream_Write_UINT24_BE(s, v) \
958 WINPR_ASSERT((v) <= 0xFFFFFF); \
959 WINPR_ASSERT((v) >= 0); \
960 Stream_Write_UINT24_BE_unchecked((s), (v)); \
971 static INLINE
void Stream_Write_UINT24_BE_unchecked(
wStream* _s, UINT32 _v)
974 WINPR_ASSERT(_s->pointer);
975 WINPR_ASSERT(_v <= 0x00FFFFFF);
976 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 3);
978 *_s->pointer++ = ((_v) >> 16) & 0xFF;
979 *_s->pointer++ = ((_v) >> 8) & 0xFF;
980 *_s->pointer++ = (_v) & 0xFF;
983 #define Stream_Write_INT32(s, v) \
986 WINPR_ASSERT((v) <= INT32_MAX); \
987 WINPR_ASSERT((v) >= INT32_MIN); \
988 Stream_Write_INT32_unchecked((s), (v)); \
999 static INLINE
void Stream_Write_INT32_unchecked(
wStream* _s, INT32 _v)
1002 WINPR_ASSERT(_s->pointer);
1003 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 4);
1005 winpr_Data_Write_INT32(_s->pointer, _v);
1009 #define Stream_Write_INT32_BE(s, v) \
1012 WINPR_ASSERT((v) <= INT32_MAX); \
1013 WINPR_ASSERT((v) >= INT32_MIN); \
1014 Stream_Write_INT32_BE_unchecked((s), (v)); \
1027 static INLINE
void Stream_Write_INT32_BE_unchecked(
wStream* _s, INT32 _v)
1030 WINPR_ASSERT(_s->pointer);
1031 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 4);
1033 winpr_Data_Write_INT32_BE(_s->pointer, _v);
1037 #define Stream_Write_UINT32(s, v) \
1040 WINPR_ASSERT((v) <= UINT32_MAX); \
1041 WINPR_ASSERT((v) >= 0); \
1042 Stream_Write_UINT32_unchecked((s), (v)); \
1053 static INLINE
void Stream_Write_UINT32_unchecked(
wStream* _s, UINT32 _v)
1056 WINPR_ASSERT(_s->pointer);
1057 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 4);
1059 winpr_Data_Write_UINT32(_s->pointer, _v);
1063 #define Stream_Write_UINT32_BE(s, v) \
1066 WINPR_ASSERT((v) <= UINT32_MAX); \
1067 WINPR_ASSERT((v) >= 0); \
1068 Stream_Write_UINT32_BE_unchecked((s), (v)); \
1079 static INLINE
void Stream_Write_UINT32_BE_unchecked(
wStream* _s, UINT32 _v)
1081 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 4);
1083 winpr_Data_Write_UINT32_BE(_s->pointer, _v);
1093 static INLINE
void Stream_Write_UINT64(
wStream* _s, UINT64 _v)
1096 WINPR_ASSERT(_s->pointer);
1097 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 8);
1099 winpr_Data_Write_UINT64(_s->pointer, _v);
1109 static INLINE
void Stream_Write_UINT64_BE(
wStream* _s, UINT64 _v)
1112 WINPR_ASSERT(_s->pointer);
1113 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 8);
1115 winpr_Data_Write_UINT64_BE(_s->pointer, _v);
1126 static INLINE
void Stream_Write_INT64(
wStream* _s, INT64 _v)
1129 WINPR_ASSERT(_s->pointer);
1130 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 8);
1132 winpr_Data_Write_INT64(_s->pointer, _v);
1143 static INLINE
void Stream_Write_INT64_BE(
wStream* _s, INT64 _v)
1146 WINPR_ASSERT(_s->pointer);
1147 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 8);
1149 winpr_Data_Write_INT64_BE(_s->pointer, _v);
1153 static INLINE
void Stream_Write(
wStream* _s,
const void* _b,
size_t _n)
1159 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= _n);
1160 memcpy(_s->pointer, (_b), (_n));
1161 Stream_Seek(_s, _n);
1165 static INLINE
void Stream_Seek_UINT8(
wStream* _s)
1167 Stream_Seek(_s,
sizeof(UINT8));
1169 static INLINE
void Stream_Seek_UINT16(
wStream* _s)
1171 Stream_Seek(_s,
sizeof(UINT16));
1173 static INLINE
void Stream_Seek_UINT32(
wStream* _s)
1175 Stream_Seek(_s,
sizeof(UINT32));
1177 static INLINE
void Stream_Seek_UINT64(
wStream* _s)
1179 Stream_Seek(_s,
sizeof(UINT64));
1182 static INLINE
void Stream_Rewind_UINT8(
wStream* _s)
1184 Stream_Rewind(_s,
sizeof(UINT8));
1186 static INLINE
void Stream_Rewind_UINT16(
wStream* _s)
1188 Stream_Rewind(_s,
sizeof(UINT16));
1190 static INLINE
void Stream_Rewind_UINT32(
wStream* _s)
1192 Stream_Rewind(_s,
sizeof(UINT32));
1194 static INLINE
void Stream_Rewind_UINT64(
wStream* _s)
1196 Stream_Rewind(_s,
sizeof(UINT64));
1199 static INLINE
void Stream_Fill(
wStream* _s,
int _v,
size_t _n)
1202 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= (_n));
1203 memset(_s->pointer, _v, (_n));
1204 Stream_Seek(_s, _n);
1207 static INLINE
void Stream_Zero(
wStream* _s,
size_t _n)
1209 Stream_Fill(_s,
'\0', _n);
1212 static INLINE
void Stream_Copy(
wStream* _src,
wStream* _dst,
size_t _n)
1216 WINPR_ASSERT(Stream_GetRemainingCapacity(_src) >= (_n));
1217 WINPR_ASSERT(Stream_GetRemainingCapacity(_dst) >= (_n));
1219 memcpy(_dst->pointer, _src->pointer, _n);
1220 Stream_Seek(_dst, _n);
1221 Stream_Seek(_src, _n);
1228 #define Stream_BufferAs(s, type) WINPR_STREAM_CAST(type*, Stream_Buffer(s))
1230 static INLINE BYTE* Stream_Buffer(
wStream* _s)
1240 #define Stream_ConstBufferAs(s, type) WINPR_STREAM_CAST(type*, Stream_ConstBuffer(s))
1241 static INLINE
const BYTE* Stream_ConstBuffer(
const wStream* _s)
1247 #define Stream_GetBuffer(_s, _b) _b = Stream_Buffer(_s)
1253 #define Stream_GetBufferAs(_s, _b) _b = Stream_BufferAs(_s, __typeof(_b))
1255 #define Stream_PointerAs(s, type) WINPR_STREAM_CAST(type*, Stream_Pointer(s))
1257 static INLINE
void* Stream_Pointer(
wStream* _s)
1263 static INLINE
const void* Stream_ConstPointer(
const wStream* _s)
1269 #define Stream_GetPointer(_s, _p) _p = Stream_Pointer(_s)
1275 #define Stream_GetPointerAs(_s, _p) _p = Stream_PointerAs(_s, __typeof(_p))
1277 #if defined(WITH_WINPR_DEPRECATED)
1278 WINPR_API WINPR_DEPRECATED_VAR(
"Use Stream_SetPosition instead",
1279 BOOL Stream_SetPointer(
wStream* _s, BYTE* _p));
1280 WINPR_API WINPR_DEPRECATED_VAR(
"Use Stream_New(buffer, capacity) instead",
1281 BOOL Stream_SetBuffer(
wStream* _s, BYTE* _b));
1282 WINPR_API WINPR_DEPRECATED_VAR(
"Use Stream_New(buffer, capacity) instead",
1283 void Stream_SetCapacity(
wStream* _s,
size_t capacity));
1286 static INLINE
size_t Stream_Length(
const wStream* _s)
1292 #define Stream_GetLength(_s, _l) _l = Stream_Length(_s)
1293 WINPR_API BOOL Stream_SetLength(
wStream* _s,
size_t _l);
1295 static INLINE
size_t Stream_Capacity(
const wStream* _s)
1298 return _s->capacity;
1301 #define Stream_GetCapacity(_s, _c) _c = Stream_Capacity(_s);
1303 static INLINE
size_t Stream_GetPosition(
const wStream* _s)
1306 WINPR_ASSERT(_s->buffer <= _s->pointer);
1307 return WINPR_STREAM_CAST(
size_t, (_s->pointer - _s->buffer));
1310 WINPR_API BOOL Stream_SetPosition(
wStream* _s,
size_t _p);
1312 WINPR_API
void Stream_SealLength(
wStream* _s);
1314 static INLINE
void Stream_Clear(
wStream* _s)
1317 memset(_s->buffer, 0, _s->capacity);
1320 #define Stream_SafeSeek(s, size) Stream_SafeSeekEx(s, size, __FILE__, __LINE__, __func__)
1321 WINPR_API BOOL Stream_SafeSeekEx(
wStream* s,
size_t size,
const char* file,
size_t line,
1324 WINPR_API BOOL Stream_Read_UTF16_String(
wStream* s, WCHAR* dst,
size_t charLength);
1325 WINPR_API BOOL Stream_Write_UTF16_String(
wStream* s,
const WCHAR* src,
size_t charLength);
1336 WINPR_API
char* Stream_Read_UTF16_String_As_UTF8(
wStream* s,
size_t wcharLength,
1337 size_t* pUtfCharLength);
1348 WINPR_API SSIZE_T Stream_Read_UTF16_String_As_UTF8_Buffer(
wStream* s,
size_t wcharLength,
1350 size_t utfBufferCharLength);
1363 WINPR_API SSIZE_T Stream_Write_UTF16_String_From_UTF8(
wStream* s,
size_t wcharLength,
1364 const char* src,
size_t length,
1369 WINPR_API
void StreamPool_Return(wStreamPool* pool,
wStream* s);
1371 WINPR_API
void Stream_AddRef(
wStream* s);
1372 WINPR_API
void Stream_Release(
wStream* s);
1374 WINPR_ATTR_MALLOC(Stream_Release, 1)
1375 WINPR_API
wStream* StreamPool_Take(wStreamPool* pool,
size_t size);
1377 WINPR_API
wStream* StreamPool_Find(wStreamPool* pool, const BYTE* ptr);
1387 WINPR_API
size_t StreamPool_UsedCount(wStreamPool* pool);
1399 WINPR_API BOOL StreamPool_WaitForReturn(wStreamPool* pool, UINT32 timeoutMS);
1401 WINPR_API
void StreamPool_Clear(wStreamPool* pool);
1403 WINPR_API
void StreamPool_Free(wStreamPool* pool);
1405 WINPR_ATTR_MALLOC(StreamPool_Free, 1)
1406 WINPR_API wStreamPool* StreamPool_New(BOOL synchronized,
size_t defaultSize);
1408 WINPR_API
char* StreamPool_GetStatistics(wStreamPool* pool,
char* buffer,
size_t size);