21 #include <freerdp/config.h>
23 #include <freerdp/channels/log.h>
24 #include <freerdp/utils/encoded_types.h>
27 #define TAG CHANNELS_TAG("encoded_types")
39 } EncodedTypeByteCount;
49 EncodedTypeByteCount c;
55 } FOUR_BYTE_SIGNED_INTEGER;
59 EncodedTypeByteCount c;
68 BOOL freerdp_read_four_byte_signed_integer(
wStream* s, INT32* value)
70 FOUR_BYTE_SIGNED_INTEGER si = { 0 };
78 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
81 Stream_Read_UINT8(s,
byte);
83 si.c = (
byte & 0xC0) >> 6;
84 si.s = (
byte & 0x20) >> 5;
85 si.val1 = (
byte & 0x1F);
87 if (!Stream_CheckAndLogRequiredLength(TAG, s, si.c))
96 Stream_Read_UINT8(s, si.val2);
97 *value = (((INT32)si.val1) << 8) | ((INT32)si.val2);
100 Stream_Read_UINT8(s, si.val2);
101 Stream_Read_UINT8(s, si.val3);
102 *value = (((INT32)si.val1) << 16) | (((INT32)si.val2) << 8) | ((INT32)si.val3);
105 Stream_Read_UINT8(s, si.val2);
106 Stream_Read_UINT8(s, si.val3);
107 Stream_Read_UINT8(s, si.val4);
108 *value = (((INT32)si.val1) << 24) | (((INT32)si.val2) << 16) | (((INT32)si.val3) << 8) |
116 WLog_ERR(TAG,
"Invalid byte count value in si.c: %u", si.c);
120 if (si.s == NEGATIVE_VAL)
126 BOOL freerdp_write_four_byte_signed_integer(
wStream* s, INT32 value)
128 FOUR_BYTE_SIGNED_INTEGER si = { 0 };
131 if (value > FREERDP_FOUR_BYTE_SIGNED_INT_MAX)
133 if (value < FREERDP_FOUR_BYTE_SIGNED_INT_MIN)
145 si.val1 = value & 0x1f;
147 else if (value <= 0x1FFF)
150 si.val1 = (value >> 8) & 0x1f;
151 si.val2 = value & 0xff;
153 else if (value <= 0x1FFFFF)
155 si.c = THREE_BYTE_VAL;
156 si.val1 = (value >> 16) & 0x1f;
157 si.val2 = (value >> 8) & 0xff;
158 si.val3 = value & 0xff;
160 else if (value <= 0x1FFFFFFF)
162 si.c = FOUR_BYTE_VAL;
163 si.val1 = (value >> 24) & 0x1f;
164 si.val2 = (value >> 16) & 0xff;
165 si.val3 = (value >> 8) & 0xff;
166 si.val4 = value & 0xff;
170 WLog_ERR(TAG,
"Invalid byte count for value %" PRId32, value);
174 if (!Stream_EnsureRemainingCapacity(s, si.c + 1))
177 const BYTE
byte = ((si.c << 6) & 0xC0) | ((si.s << 5) & 0x20) | (si.val1 & 0x1F);
178 Stream_Write_UINT8(s,
byte);
185 Stream_Write_UINT8(s, si.val2);
188 Stream_Write_UINT8(s, si.val2);
189 Stream_Write_UINT8(s, si.val3);
192 Stream_Write_UINT8(s, si.val2);
193 Stream_Write_UINT8(s, si.val3);
194 Stream_Write_UINT8(s, si.val4);
201 WLog_ERR(TAG,
"Invalid byte count value in si.c: %u", si.c);
208 BOOL freerdp_read_four_byte_float(
wStream* s,
double* value)
210 return freerdp_read_four_byte_float_exp(s, value, NULL);
213 BOOL freerdp_read_four_byte_float_exp(
wStream* s,
double* value, BYTE* exp)
215 FOUR_BYTE_FLOAT f = { 0 };
224 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
227 Stream_Read_UINT8(s,
byte);
229 f.c = (
byte & 0xC0) >> 6;
230 f.s = (
byte & 0x20) >> 5;
231 f.e = (
byte & 0x1C) >> 2;
232 f.val1 = (
byte & 0x03);
234 if (!Stream_CheckAndLogRequiredLength(TAG, s, f.c))
243 Stream_Read_UINT8(s, f.val2);
244 base = (((UINT32)f.val1) << 8) | ((UINT32)f.val2);
247 Stream_Read_UINT8(s, f.val2);
248 Stream_Read_UINT8(s, f.val3);
249 base = (((UINT32)f.val1) << 16) | (((UINT32)f.val2) << 8) | ((UINT32)f.val3);
252 Stream_Read_UINT8(s, f.val2);
253 Stream_Read_UINT8(s, f.val3);
254 Stream_Read_UINT8(s, f.val4);
255 base = (((UINT32)f.val1) << 24) | (((UINT32)f.val2) << 16) | (((UINT32)f.val3) << 8) |
263 WLog_ERR(TAG,
"Invalid byte count value in f.c: %u", f.c);
268 *value /= pow(10, f.e);
270 if (f.s == NEGATIVE_VAL)
279 BOOL freerdp_write_four_byte_float(
wStream* s,
double value)
281 FOUR_BYTE_FLOAT si = { 0 };
285 if (value > FREERDP_FOUR_BYTE_FLOAT_MAX)
287 if (value < FREERDP_FOUR_BYTE_FLOAT_MIN)
297 double ival = FP_NAN;
298 const double aval = fabs(value);
299 const double frac = modf(aval, &ival);
302 const double maxfrac = frac * 10000000.0;
305 else if (maxfrac <= 10.0)
307 else if (maxfrac <= 100.0)
309 else if (maxfrac <= 1000.0)
311 else if (maxfrac <= 10000.0)
313 else if (maxfrac <= 100000.0)
315 else if (maxfrac <= 1000000.0)
321 UINT64 base = (UINT64)llround(aval);
324 const double div = pow(10.0, exp);
325 const double dval = (aval * div);
327 if (base <= 0x03ffffff)
339 si.val1 = base & 0x03;
341 else if (base <= 0x03ff)
344 si.val1 = (base >> 8) & 0x03;
345 si.val2 = base & 0xff;
347 else if (base <= 0x03ffff)
349 si.c = THREE_BYTE_VAL;
350 si.val1 = (base >> 16) & 0x03;
351 si.val2 = (base >> 8) & 0xff;
352 si.val3 = base & 0xff;
354 else if (base <= 0x03ffffff)
356 si.c = FOUR_BYTE_VAL;
357 si.val1 = (base >> 24) & 0x03;
358 si.val2 = (base >> 16) & 0xff;
359 si.val3 = (base >> 8) & 0xff;
360 si.val4 = base & 0xff;
364 WLog_ERR(TAG,
"Invalid byte count for value %ld", value);
368 if (!Stream_EnsureRemainingCapacity(s, si.c + 1))
372 ((si.c << 6) & 0xC0) | ((si.s << 5) & 0x20) | ((si.e << 2) & 0x1c) | (si.val1 & 0x03);
373 Stream_Write_UINT8(s,
byte);
380 Stream_Write_UINT8(s, si.val2);
383 Stream_Write_UINT8(s, si.val2);
384 Stream_Write_UINT8(s, si.val3);
387 Stream_Write_UINT8(s, si.val2);
388 Stream_Write_UINT8(s, si.val3);
389 Stream_Write_UINT8(s, si.val4);
396 WLog_ERR(TAG,
"Invalid byte count value in si.c: %u", si.c);