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>
37 typedef struct s_wStreamPool wStreamPool;
48 BOOL isAllocatedStream;
52 static INLINE
size_t Stream_Capacity(
const wStream* _s);
53 WINPR_API
size_t Stream_GetRemainingCapacity(
const wStream* _s);
54 WINPR_API
size_t Stream_GetRemainingLength(
const wStream* _s);
56 WINPR_API BOOL Stream_EnsureCapacity(
wStream* s,
size_t size);
57 WINPR_API BOOL Stream_EnsureRemainingCapacity(
wStream* s,
size_t size);
60 #define WINPR_STREAM_CAST(t, val) static_cast<t>(val)
62 #define WINPR_STREAM_CAST(t, val) (t)(val)
65 #define Stream_CheckAndLogRequiredCapacityOfSize(tag, s, nmemb, size) \
66 Stream_CheckAndLogRequiredCapacityEx(tag, WLOG_WARN, s, nmemb, size, "%s(%s:%" PRIuz ")", \
67 __func__, __FILE__, (size_t)__LINE__)
68 #define Stream_CheckAndLogRequiredCapacity(tag, s, len) \
69 Stream_CheckAndLogRequiredCapacityOfSize((tag), (s), (len), 1)
71 WINPR_API BOOL Stream_CheckAndLogRequiredCapacityEx(
const char* tag, DWORD level,
wStream* s,
72 size_t nmemb,
size_t size,
const char* fmt,
74 WINPR_API BOOL Stream_CheckAndLogRequiredCapacityExVa(
const char* tag, DWORD level,
wStream* s,
75 size_t nmemb,
size_t size,
76 const char* fmt, va_list args);
78 #define Stream_CheckAndLogRequiredCapacityOfSizeWLog(log, s, nmemb, size) \
79 Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, nmemb, size, "%s(%s:%" PRIuz ")", \
80 __func__, __FILE__, (size_t)__LINE__)
82 #define Stream_CheckAndLogRequiredCapacityWLog(log, s, len) \
83 Stream_CheckAndLogRequiredCapacityOfSizeWLog((log), (s), (len), 1)
85 WINPR_API BOOL Stream_CheckAndLogRequiredCapacityWLogEx(wLog* log, DWORD level,
wStream* s,
86 size_t nmemb,
size_t size,
87 const char* fmt, ...);
88 WINPR_API BOOL Stream_CheckAndLogRequiredCapacityWLogExVa(wLog* log, DWORD level,
wStream* s,
89 size_t nmemb,
size_t size,
90 const char* fmt, va_list args);
92 WINPR_API
void Stream_Free(
wStream* s, BOOL bFreeBuffer);
94 WINPR_ATTR_MALLOC(Stream_Free, 1)
95 WINPR_API
wStream* Stream_New(BYTE* buffer,
size_t size);
96 WINPR_API
wStream* Stream_StaticConstInit(
wStream* s, const BYTE* buffer,
size_t size);
97 WINPR_API
wStream* Stream_StaticInit(
wStream* s, BYTE* buffer,
size_t size);
99 #define Stream_CheckAndLogRequiredLengthOfSize(tag, s, nmemb, size) \
100 Stream_CheckAndLogRequiredLengthEx(tag, WLOG_WARN, s, nmemb, size, "%s(%s:%" PRIuz ")", \
101 __func__, __FILE__, (size_t)__LINE__)
102 #define Stream_CheckAndLogRequiredLength(tag, s, len) \
103 Stream_CheckAndLogRequiredLengthOfSize(tag, s, len, 1)
105 WINPR_API BOOL Stream_CheckAndLogRequiredLengthEx(
const char* tag, DWORD level,
wStream* s,
106 size_t nmemb,
size_t size,
const char* fmt,
108 WINPR_API BOOL Stream_CheckAndLogRequiredLengthExVa(
const char* tag, DWORD level,
wStream* s,
109 size_t nmemb,
size_t size,
const char* fmt,
112 #define Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, nmemb, size) \
113 Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, nmemb, size, "%s(%s:%" PRIuz ")", \
114 __func__, __FILE__, (size_t)__LINE__)
115 #define Stream_CheckAndLogRequiredLengthWLog(log, s, len) \
116 Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, len, 1)
118 WINPR_API BOOL Stream_CheckAndLogRequiredLengthWLogEx(wLog* log, DWORD level,
wStream* s,
119 size_t nmemb,
size_t size,
120 const char* fmt, ...);
121 WINPR_API BOOL Stream_CheckAndLogRequiredLengthWLogExVa(wLog* log, DWORD level,
wStream* s,
122 size_t nmemb,
size_t size,
123 const char* fmt, va_list args);
125 static INLINE
void Stream_Seek(
wStream* s,
size_t _offset)
128 WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= _offset);
129 s->pointer += (_offset);
132 static INLINE
void Stream_Rewind(
wStream* s,
size_t _offset)
136 WINPR_ASSERT(s->buffer <= s->pointer);
137 cur = WINPR_STREAM_CAST(
size_t, s->pointer - s->buffer);
138 WINPR_ASSERT(cur >= _offset);
140 s->pointer -= (_offset);
142 s->pointer = s->buffer;
145 static INLINE UINT8 stream_read_u8(
wStream* _s, BOOL seek)
148 WINPR_ASSERT(Stream_GetRemainingLength(_s) >=
sizeof(UINT8));
150 const UINT8 v = *(_s)->pointer;
152 Stream_Seek(_s,
sizeof(UINT8));
156 static INLINE INT8 stream_read_i8(
wStream* _s, BOOL seek)
158 const UINT8 v = stream_read_u8(_s, seek);
159 return WINPR_STREAM_CAST(INT8, v);
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);
169 for (
size_t x = 0; x < typesize; x++)
172 v |= (_s)->pointer[typesize - x - 1];
175 Stream_Seek(_s, typesize);
179 static INLINE UINT16 stream_read_u16_be(
wStream* _s, BOOL seek)
181 const size_t typesize =
sizeof(UINT16);
183 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
186 for (
size_t x = 0; x < typesize; x++)
189 v |= (_s)->pointer[x];
192 Stream_Seek(_s, typesize);
196 static INLINE INT16 stream_read_i16_le(
wStream* _s, BOOL seek)
198 const UINT16 v = stream_read_u16_le(_s, seek);
199 return WINPR_STREAM_CAST(INT16, v);
202 static INLINE INT16 stream_read_i16_be(
wStream* _s, BOOL seek)
204 const UINT16 v = stream_read_u16_be(_s, seek);
205 return WINPR_STREAM_CAST(INT16, v);
208 static INLINE UINT32 stream_read_u32_le(
wStream* _s, BOOL seek)
210 const size_t typesize =
sizeof(UINT32);
212 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
215 for (
size_t x = 0; x < typesize; x++)
218 v |= (_s)->pointer[typesize - x - 1];
221 Stream_Seek(_s, typesize);
225 static INLINE UINT32 stream_read_u32_be(
wStream* _s, BOOL seek)
227 const size_t typesize =
sizeof(UINT32);
229 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
232 for (
size_t x = 0; x < typesize; x++)
235 v |= (_s)->pointer[x];
238 Stream_Seek(_s, typesize);
242 static INLINE INT32 stream_read_i32_le(
wStream* _s, BOOL seek)
244 const UINT32 v = stream_read_u32_le(_s, seek);
245 return WINPR_STREAM_CAST(INT32, v);
248 static INLINE INT32 stream_read_i32_be(
wStream* _s, BOOL seek)
250 const UINT32 v = stream_read_u32_be(_s, seek);
251 return WINPR_STREAM_CAST(INT32, v);
254 static INLINE UINT64 stream_read_u64_le(
wStream* _s, BOOL seek)
256 const size_t typesize =
sizeof(UINT64);
258 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
261 for (
size_t x = 0; x < typesize; x++)
264 v |= (_s)->pointer[typesize - x - 1];
267 Stream_Seek(_s, typesize);
271 static INLINE UINT64 stream_read_u64_be(
wStream* _s, BOOL seek)
273 const size_t typesize =
sizeof(UINT64);
275 WINPR_ASSERT(Stream_GetRemainingLength(_s) >= typesize);
278 for (
size_t x = 0; x < typesize; x++)
281 v |= (_s)->pointer[x];
284 Stream_Seek(_s, typesize);
288 static INLINE INT64 stream_read_i64_le(
wStream* _s, BOOL seek)
290 const UINT64 v = stream_read_u64_le(_s, seek);
291 return WINPR_STREAM_CAST(INT64, v);
294 static INLINE INT64 stream_read_i64_be(
wStream* _s, BOOL seek)
296 const UINT64 v = stream_read_u64_be(_s, seek);
297 return WINPR_STREAM_CAST(INT64, v);
306 static INLINE UINT8 Stream_Get_UINT8(
wStream* _s)
308 return stream_read_u8(_s, TRUE);
317 static INLINE INT8 Stream_Get_INT8(
wStream* _s)
319 return stream_read_i8(_s, TRUE);
328 static INLINE UINT16 Stream_Get_UINT16(
wStream* _s)
330 return stream_read_u16_le(_s, TRUE);
339 static INLINE INT16 Stream_Get_INT16(
wStream* _s)
341 return stream_read_i16_le(_s, TRUE);
350 static INLINE UINT16 Stream_Get_UINT16_BE(
wStream* _s)
352 return stream_read_u16_be(_s, TRUE);
361 static INLINE INT16 Stream_Get_INT16_BE(
wStream* _s)
363 return stream_read_i16_be(_s, TRUE);
372 static INLINE UINT32 Stream_Get_UINT32(
wStream* _s)
374 return stream_read_u32_le(_s, TRUE);
383 static INLINE INT32 Stream_Get_INT32(
wStream* _s)
385 return stream_read_i32_le(_s, TRUE);
394 static INLINE UINT32 Stream_Get_UINT32_BE(
wStream* _s)
396 return stream_read_u32_be(_s, TRUE);
405 static INLINE INT32 Stream_Get_INT32_BE(
wStream* _s)
407 return stream_read_i32_be(_s, TRUE);
416 static INLINE UINT64 Stream_Get_UINT64(
wStream* _s)
418 return stream_read_u64_le(_s, TRUE);
427 static INLINE INT64 Stream_Get_INT64(
wStream* _s)
429 return stream_read_i64_le(_s, TRUE);
438 static INLINE UINT64 Stream_Get_UINT64_BE(
wStream* _s)
440 return stream_read_u64_be(_s, TRUE);
449 static INLINE INT64 Stream_Get_INT64_BE(
wStream* _s)
451 return stream_read_i64_be(_s, TRUE);
460 static INLINE UINT8 Stream_Peek_Get_UINT8(
wStream* _s)
462 return stream_read_u8(_s, FALSE);
471 static INLINE INT8 Stream_Peek_Get_INT8(
wStream* _s)
473 return stream_read_i8(_s, FALSE);
482 static INLINE UINT16 Stream_Peek_Get_UINT16(
wStream* _s)
484 return stream_read_u16_le(_s, FALSE);
493 static INLINE INT16 Stream_Peek_Get_INT16(
wStream* _s)
495 return stream_read_i16_le(_s, FALSE);
504 static INLINE UINT16 Stream_Peek_Get_UINT16_BE(
wStream* _s)
506 return stream_read_u16_be(_s, FALSE);
515 static INLINE INT16 Stream_Peek_Get_INT16_BE(
wStream* _s)
517 return stream_read_i16_be(_s, FALSE);
526 static INLINE UINT32 Stream_Peek_Get_UINT32(
wStream* _s)
528 return stream_read_u32_le(_s, FALSE);
537 static INLINE INT32 Stream_Peek_Get_INT32(
wStream* _s)
539 return stream_read_i32_le(_s, FALSE);
548 static INLINE UINT32 Stream_Peek_Get_UINT32_BE(
wStream* _s)
550 return stream_read_u32_be(_s, FALSE);
559 static INLINE INT32 Stream_Peek_Get_INT32_BE(
wStream* _s)
561 return stream_read_i32_be(_s, FALSE);
570 static INLINE UINT64 Stream_Peek_Get_UINT64(
wStream* _s)
572 return stream_read_u64_le(_s, FALSE);
581 static INLINE INT64 Stream_Peek_Get_INT64(
wStream* _s)
583 return stream_read_i64_le(_s, FALSE);
592 static INLINE UINT64 Stream_Peek_Get_UINT64_BE(
wStream* _s)
594 return stream_read_u64_be(_s, FALSE);
603 static INLINE INT64 Stream_Peek_Get_INT64_BE(
wStream* _s)
605 return stream_read_i64_be(_s, FALSE);
608 #define Stream_Read_UINT8(_s, _v) \
611 _v = stream_read_u8(_s, TRUE); \
614 #define Stream_Read_INT8(_s, _v) \
617 _v = stream_read_i8(_s, TRUE); \
620 #define Stream_Read_UINT16(_s, _v) \
623 _v = stream_read_u16_le(_s, TRUE); \
626 #define Stream_Read_INT16(_s, _v) \
629 _v = stream_read_i16_le(_s, TRUE); \
632 #define Stream_Read_UINT16_BE(_s, _v) \
635 _v = stream_read_u16_be(_s, TRUE); \
638 #define Stream_Read_INT16_BE(_s, _v) \
641 _v = stream_read_i16_be(_s, TRUE); \
644 #define Stream_Read_UINT32(_s, _v) \
647 _v = stream_read_u32_le(_s, TRUE); \
650 #define Stream_Read_INT32(_s, _v) \
653 _v = stream_read_i32_le(_s, TRUE); \
656 #define Stream_Read_UINT32_BE(_s, _v) \
659 _v = stream_read_u32_be(_s, TRUE); \
662 #define Stream_Read_INT32_BE(_s, _v) \
665 _v = stream_read_i32_be(_s, TRUE); \
668 #define Stream_Read_UINT64(_s, _v) \
671 _v = stream_read_u64_le(_s, TRUE); \
674 #define Stream_Read_INT64(_s, _v) \
677 _v = stream_read_i64_le(_s, TRUE); \
680 #define Stream_Read_UINT64_BE(_s, _v) \
683 _v = stream_read_u64_be(_s, TRUE); \
686 #define Stream_Read_INT64_BE(_s, _v) \
689 _v = stream_read_i64_be(_s, TRUE); \
692 static INLINE
void Stream_Read(
wStream* _s,
void* _b,
size_t _n)
695 WINPR_ASSERT(_b || (_n == 0));
696 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= _n);
697 memcpy(_b, (_s->pointer), (_n));
701 #define Stream_Peek_UINT8(_s, _v) \
704 _v = stream_read_u8(_s, FALSE); \
707 #define Stream_Peek_INT8(_s, _v) \
710 _v = stream_read_i8(_s, FALSE); \
713 #define Stream_Peek_UINT16(_s, _v) \
716 _v = stream_read_u16_le(_s, FALSE); \
719 #define Stream_Peek_INT16(_s, _v) \
722 _v = stream_read_i16_le(_s, FALSE); \
725 #define Stream_Peek_UINT16_BE(_s, _v) \
728 _v = stream_read_u16_be(_s, FALSE); \
731 #define Stream_Peek_INT16_BE(_s, _v) \
734 _v = stream_read_i16_be(_s, FALSE); \
737 #define Stream_Peek_UINT32(_s, _v) \
740 _v = stream_read_u32_le(_s, FALSE); \
743 #define Stream_Peek_INT32(_s, _v) \
746 _v = stream_read_i32_le(_s, FALSE); \
749 #define Stream_Peek_UINT32_BE(_s, _v) \
752 _v = stream_read_u32_be(_s, FALSE); \
755 #define Stream_Peek_INT32_BE(_s, _v) \
758 _v = stream_read_i32_be(_s, FALSE); \
761 #define Stream_Peek_UINT64(_s, _v) \
764 _v = stream_read_u64_le(_s, FALSE); \
767 #define Stream_Peek_INT64(_s, _v) \
770 _v = stream_read_i64_le(_s, FALSE); \
773 #define Stream_Peek_UINT64_BE(_s, _v) \
776 _v = stream_read_u64_be(_s, FALSE); \
779 #define Stream_Peek_INT64_BE(_s, _v) \
782 _v = stream_read_i64_be(_s, FALSE); \
785 static INLINE
void Stream_Peek(
const wStream* _s,
void* _b,
size_t _n)
788 WINPR_ASSERT(_b || (_n == 0));
789 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= _n);
790 memcpy(_b, (_s->pointer), (_n));
793 #define Stream_Write_INT8(s, v) \
796 WINPR_ASSERT((v) <= INT8_MAX); \
797 WINPR_ASSERT((v) >= INT8_MIN); \
798 Stream_Write_INT8_unchecked((s), (v)); \
808 static INLINE
void Stream_Write_INT8_unchecked(
wStream* _s, INT8 _v)
811 WINPR_ASSERT(_s->pointer);
812 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 1);
814 *_s->pointer++ = WINPR_STREAM_CAST(BYTE, _v);
817 #define Stream_Write_UINT8(s, v) \
820 WINPR_ASSERT((v) <= UINT8_MAX); \
821 WINPR_ASSERT((v) >= 0); \
822 Stream_Write_UINT8_unchecked((s), (v)); \
832 static INLINE
void Stream_Write_UINT8_unchecked(
wStream* _s, UINT8 _v)
835 WINPR_ASSERT(_s->pointer);
836 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 1);
838 *_s->pointer++ = WINPR_STREAM_CAST(BYTE, _v);
841 #define Stream_Write_INT16(s, v) \
844 WINPR_ASSERT((v) >= INT16_MIN); \
845 WINPR_ASSERT((v) <= INT16_MAX); \
846 Stream_Write_INT16_unchecked((s), (v)); \
857 static INLINE
void Stream_Write_INT16_unchecked(
wStream* _s, INT16 _v)
860 WINPR_ASSERT(_s->pointer);
861 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 2);
863 *_s->pointer++ = (_v) & 0xFF;
864 *_s->pointer++ = ((_v) >> 8) & 0xFF;
867 #define Stream_Write_UINT16(s, v) \
870 WINPR_ASSERT((v) <= UINT16_MAX); \
871 WINPR_ASSERT((v) >= 0); \
872 Stream_Write_UINT16_unchecked((s), (v)); \
883 static INLINE
void Stream_Write_UINT16_unchecked(
wStream* _s, UINT16 _v)
886 WINPR_ASSERT(_s->pointer);
887 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 2);
889 *_s->pointer++ = (_v) & 0xFF;
890 *_s->pointer++ = ((_v) >> 8) & 0xFF;
893 #define Stream_Write_UINT16_BE(s, v) \
896 WINPR_ASSERT((v) <= UINT16_MAX); \
897 WINPR_ASSERT((v) >= 0); \
898 Stream_Write_UINT16_BE_unchecked((s), (v)); \
909 static INLINE
void Stream_Write_UINT16_BE_unchecked(
wStream* _s, UINT16 _v)
912 WINPR_ASSERT(_s->pointer);
913 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 2);
915 *_s->pointer++ = ((_v) >> 8) & 0xFF;
916 *_s->pointer++ = (_v) & 0xFF;
919 #define Stream_Write_INT16_BE(s, v) \
922 WINPR_ASSERT((v) <= INT16_MAX); \
923 WINPR_ASSERT((v) >= INT16_MIN); \
924 Stream_Write_INT16_BE_unchecked((s), (v)); \
937 static INLINE
void Stream_Write_INT16_BE_unchecked(
wStream* _s, INT16 _v)
940 WINPR_ASSERT(_s->pointer);
941 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 2);
943 *_s->pointer++ = ((_v) >> 8) & 0xFF;
944 *_s->pointer++ = (_v) & 0xFF;
947 #define Stream_Write_UINT24_BE(s, v) \
950 WINPR_ASSERT((v) <= 0xFFFFFF); \
951 WINPR_ASSERT((v) >= 0); \
952 Stream_Write_UINT24_BE_unchecked((s), (v)); \
963 static INLINE
void Stream_Write_UINT24_BE_unchecked(
wStream* _s, UINT32 _v)
966 WINPR_ASSERT(_s->pointer);
967 WINPR_ASSERT(_v <= 0x00FFFFFF);
968 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 3);
970 *_s->pointer++ = ((_v) >> 16) & 0xFF;
971 *_s->pointer++ = ((_v) >> 8) & 0xFF;
972 *_s->pointer++ = (_v) & 0xFF;
975 #define Stream_Write_INT32(s, v) \
978 WINPR_ASSERT((v) <= INT32_MAX); \
979 WINPR_ASSERT((v) >= INT32_MIN); \
980 Stream_Write_INT32_unchecked((s), (v)); \
991 static INLINE
void Stream_Write_INT32_unchecked(
wStream* _s, INT32 _v)
994 WINPR_ASSERT(_s->pointer);
995 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 4);
997 *_s->pointer++ = (_v) & 0xFF;
998 *_s->pointer++ = ((_v) >> 8) & 0xFF;
999 *_s->pointer++ = ((_v) >> 16) & 0xFF;
1000 *_s->pointer++ = ((_v) >> 24) & 0xFF;
1003 #define Stream_Write_INT32_BE(s, v) \
1006 WINPR_ASSERT((v) <= INT32_MAX); \
1007 WINPR_ASSERT((v) >= INT32_MIN); \
1008 Stream_Write_INT32_BE_unchecked((s), (v)); \
1021 static INLINE
void Stream_Write_INT32_BE_unchecked(
wStream* _s, INT32 _v)
1024 WINPR_ASSERT(_s->pointer);
1025 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 4);
1027 *_s->pointer++ = (_v) & 0xFF;
1028 *_s->pointer++ = ((_v) >> 8) & 0xFF;
1029 *_s->pointer++ = ((_v) >> 16) & 0xFF;
1030 *_s->pointer++ = ((_v) >> 24) & 0xFF;
1031 *_s->pointer++ = (_v) & 0xFF;
1032 *_s->pointer++ = ((_v) >> 8) & 0xFF;
1033 *_s->pointer++ = ((_v) >> 16) & 0xFF;
1034 *_s->pointer++ = ((_v) >> 24) & 0xFF;
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 *_s->pointer++ = (_v) & 0xFF;
1060 *_s->pointer++ = ((_v) >> 8) & 0xFF;
1061 *_s->pointer++ = ((_v) >> 16) & 0xFF;
1062 *_s->pointer++ = ((_v) >> 24) & 0xFF;
1065 #define Stream_Write_UINT32_BE(s, v) \
1068 WINPR_ASSERT((v) <= UINT32_MAX); \
1069 WINPR_ASSERT((v) >= 0); \
1070 Stream_Write_UINT32_BE_unchecked((s), (v)); \
1081 static INLINE
void Stream_Write_UINT32_BE_unchecked(
wStream* _s, UINT32 _v)
1083 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 4);
1085 Stream_Write_UINT16_BE(_s, ((_v) >> 16 & 0xFFFF));
1086 Stream_Write_UINT16_BE(_s, ((_v) & 0xFFFF));
1095 static INLINE
void Stream_Write_UINT64(
wStream* _s, UINT64 _v)
1098 WINPR_ASSERT(_s->pointer);
1099 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 8);
1101 Stream_Write_UINT32(_s, ((_v) & 0xFFFFFFFFUL));
1102 Stream_Write_UINT32(_s, ((_v) >> 16 & 0xFFFFFFFFUL));
1111 static INLINE
void Stream_Write_UINT64_BE(
wStream* _s, UINT64 _v)
1114 WINPR_ASSERT(_s->pointer);
1115 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= 8);
1117 Stream_Write_UINT32_BE(_s, ((_v) >> 16 & 0xFFFFFFFFUL));
1118 Stream_Write_UINT32_BE(_s, ((_v) & 0xFFFFFFFFUL));
1121 static INLINE
void Stream_Write(
wStream* _s,
const void* _b,
size_t _n)
1127 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= _n);
1128 memcpy(_s->pointer, (_b), (_n));
1129 Stream_Seek(_s, _n);
1133 static INLINE
void Stream_Seek_UINT8(
wStream* _s)
1135 Stream_Seek(_s,
sizeof(UINT8));
1137 static INLINE
void Stream_Seek_UINT16(
wStream* _s)
1139 Stream_Seek(_s,
sizeof(UINT16));
1141 static INLINE
void Stream_Seek_UINT32(
wStream* _s)
1143 Stream_Seek(_s,
sizeof(UINT32));
1145 static INLINE
void Stream_Seek_UINT64(
wStream* _s)
1147 Stream_Seek(_s,
sizeof(UINT64));
1150 static INLINE
void Stream_Rewind_UINT8(
wStream* _s)
1152 Stream_Rewind(_s,
sizeof(UINT8));
1154 static INLINE
void Stream_Rewind_UINT16(
wStream* _s)
1156 Stream_Rewind(_s,
sizeof(UINT16));
1158 static INLINE
void Stream_Rewind_UINT32(
wStream* _s)
1160 Stream_Rewind(_s,
sizeof(UINT32));
1162 static INLINE
void Stream_Rewind_UINT64(
wStream* _s)
1164 Stream_Rewind(_s,
sizeof(UINT64));
1167 static INLINE
void Stream_Fill(
wStream* _s,
int _v,
size_t _n)
1170 WINPR_ASSERT(Stream_GetRemainingCapacity(_s) >= (_n));
1171 memset(_s->pointer, _v, (_n));
1172 Stream_Seek(_s, _n);
1175 static INLINE
void Stream_Zero(
wStream* _s,
size_t _n)
1177 Stream_Fill(_s,
'\0', _n);
1180 static INLINE
void Stream_Copy(
wStream* _src,
wStream* _dst,
size_t _n)
1184 WINPR_ASSERT(Stream_GetRemainingCapacity(_src) >= (_n));
1185 WINPR_ASSERT(Stream_GetRemainingCapacity(_dst) >= (_n));
1187 memcpy(_dst->pointer, _src->pointer, _n);
1188 Stream_Seek(_dst, _n);
1189 Stream_Seek(_src, _n);
1196 #define Stream_BufferAs(s, type) WINPR_STREAM_CAST(type*, Stream_Buffer(s))
1198 static INLINE BYTE* Stream_Buffer(
wStream* _s)
1208 #define Stream_ConstBufferAs(s, type) WINPR_STREAM_CAST(type*, Stream_ConstBuffer(s))
1209 static INLINE
const BYTE* Stream_ConstBuffer(
const wStream* _s)
1215 #define Stream_GetBuffer(_s, _b) _b = Stream_Buffer(_s)
1221 #define Stream_GetBufferAs(_s, _b) _b = Stream_BufferAs(_s, typeof(_b))
1223 #define Stream_PointerAs(s, type) WINPR_STREAM_CAST(type*, Stream_Pointer(s))
1225 static INLINE
void* Stream_Pointer(
wStream* _s)
1231 static INLINE
const void* Stream_ConstPointer(
const wStream* _s)
1237 #define Stream_GetPointer(_s, _p) _p = Stream_Pointer(_s)
1243 #define Stream_GetPointerAs(_s, _p) _p = Stream_PointerAs(_s, typeof(_p))
1245 #if defined(WITH_WINPR_DEPRECATED)
1246 WINPR_API WINPR_DEPRECATED_VAR(
"Use Stream_SetPosition instead",
1247 BOOL Stream_SetPointer(
wStream* _s, BYTE* _p));
1248 WINPR_API WINPR_DEPRECATED_VAR(
"Use Stream_New(buffer, capacity) instead",
1249 BOOL Stream_SetBuffer(
wStream* _s, BYTE* _b));
1250 WINPR_API WINPR_DEPRECATED_VAR(
"Use Stream_New(buffer, capacity) instead",
1251 void Stream_SetCapacity(
wStream* _s,
size_t capacity));
1254 static INLINE
size_t Stream_Length(
const wStream* _s)
1260 #define Stream_GetLength(_s, _l) _l = Stream_Length(_s)
1261 WINPR_API BOOL Stream_SetLength(
wStream* _s,
size_t _l);
1263 static INLINE
size_t Stream_Capacity(
const wStream* _s)
1266 return _s->capacity;
1269 #define Stream_GetCapacity(_s, _c) _c = Stream_Capacity(_s);
1271 static INLINE
size_t Stream_GetPosition(
const wStream* _s)
1274 WINPR_ASSERT(_s->buffer <= _s->pointer);
1275 return WINPR_STREAM_CAST(
size_t, (_s->pointer - _s->buffer));
1278 WINPR_API BOOL Stream_SetPosition(
wStream* _s,
size_t _p);
1280 WINPR_API
void Stream_SealLength(
wStream* _s);
1282 static INLINE
void Stream_Clear(
wStream* _s)
1285 memset(_s->buffer, 0, _s->capacity);
1288 #define Stream_SafeSeek(s, size) Stream_SafeSeekEx(s, size, __FILE__, __LINE__, __func__)
1289 WINPR_API BOOL Stream_SafeSeekEx(
wStream* s,
size_t size,
const char* file,
size_t line,
1292 WINPR_API BOOL Stream_Read_UTF16_String(
wStream* s, WCHAR* dst,
size_t charLength);
1293 WINPR_API BOOL Stream_Write_UTF16_String(
wStream* s,
const WCHAR* src,
size_t charLength);
1304 WINPR_API
char* Stream_Read_UTF16_String_As_UTF8(
wStream* s,
size_t wcharLength,
1305 size_t* pUtfCharLength);
1316 WINPR_API SSIZE_T Stream_Read_UTF16_String_As_UTF8_Buffer(
wStream* s,
size_t wcharLength,
1318 size_t utfBufferCharLength);
1331 WINPR_API SSIZE_T Stream_Write_UTF16_String_From_UTF8(
wStream* s,
size_t wcharLength,
1332 const char* src,
size_t length,
1337 WINPR_API
void StreamPool_Return(wStreamPool* pool,
wStream* s);
1339 WINPR_API
wStream* StreamPool_Take(wStreamPool* pool,
size_t size);
1341 WINPR_API
void Stream_AddRef(
wStream* s);
1342 WINPR_API
void Stream_Release(
wStream* s);
1344 WINPR_API
wStream* StreamPool_Find(wStreamPool* pool,
const BYTE* ptr);
1346 WINPR_API
void StreamPool_Clear(wStreamPool* pool);
1348 WINPR_API
void StreamPool_Free(wStreamPool* pool);
1350 WINPR_ATTR_MALLOC(StreamPool_Free, 1)
1351 WINPR_API wStreamPool* StreamPool_New(BOOL synchronized,
size_t defaultSize);
1353 WINPR_API
char* StreamPool_GetStatistics(wStreamPool* pool,
char* buffer,
size_t size);