Browse code

ctl: float/double values are stored over a long long int instead of int

- cope with larger values than MAX_INT/1000, supporint now
up to MAX_LLONG/1000

Daniel-Constantin Mierla authored on 12/01/2022 16:02:11
Showing 1 changed files
... ...
@@ -205,6 +205,31 @@ inline static int binrpc_add_tag(struct binrpc_pkt* pkt, int type, int end)
205 205
 
206 206
 
207 207
 
208
+/*  writes a minimal long long, returns the new offset and sets
209
+ * len to the number of bytes written (<=8)
210
+ * to check for oveflow use:  returned_value-p != *len
211
+ * (Note: if *len==0 using the test above succeeds even if p>=end)
212
+ */
213
+inline static unsigned char* binrpc_write_llong(	unsigned char* p,
214
+												unsigned char* end,
215
+												long long i, int *len)
216
+{
217
+	int size;
218
+	unsigned long long u;
219
+
220
+	u = (unsigned long long)i;
221
+
222
+	for (size=8; size && ((u & (0xffull<<56))==0); u<<=8, size--);
223
+	*len=size;
224
+	for(; (p<end) && (size); p++, size--){
225
+		*p=(unsigned char)(u>>56);
226
+		u<<=8;
227
+	}
228
+	return p;
229
+}
230
+
231
+
232
+
208 233
 /*  writes a minimal int, returns the new offset and sets
209 234
  * len to the number of bytes written (<=4)
210 235
  * to check for oveflow use:  returned_value-p != *len
... ...
@@ -330,6 +355,23 @@ inline static int binrpc_hdr_change_len(unsigned char* hdr, int hdr_len,
330 355
 }
331 356
 
332 357
 
358
+/* int format:     size TYPE <val>  */
359
+inline static int binrpc_add_llong_type(struct binrpc_pkt* pkt, long long i, int type)
360
+{
361
+
362
+	unsigned char* p;
363
+	int size;
364
+
365
+	p=binrpc_write_llong(pkt->crt+1, pkt->end, i, &size);
366
+	if ((pkt->crt>=pkt->end) || ((int)(p-pkt->crt-1)!=size))
367
+		goto error_len;
368
+	*(pkt->crt)=(size<<4) | type;
369
+	pkt->crt=p;
370
+	return 0;
371
+error_len:
372
+	return E_BINRPC_OVERFLOW;
373
+}
374
+
333 375
 
334 376
 /* int format:     size BINRPC_T_INT <val>  */
335 377
 inline static int binrpc_add_int_type(struct binrpc_pkt* pkt, int i, int type)
... ...
@@ -351,9 +393,9 @@ error_len:
351 393
 
352 394
 
353 395
 /* double format:  FIXME: for now a hack: fixed point represented in
354
- *  an int (=> max 3 decimals, < MAX_INT/1000) */
396
+ *  a long long (=> max 3 decimals, < MAX_LLONG/1000) */
355 397
 #define binrpc_add_double_type(pkt, f, type)\
356
-	binrpc_add_int_type((pkt), (int)((f)*1000), (type))
398
+	binrpc_add_llong_type((pkt), (long long)((f)*1000), (type))
357 399
 
358 400
 
359 401
 
... ...
@@ -516,6 +558,35 @@ static inline int binrpc_addfault(	struct binrpc_pkt* pkt,
516 558
 /* parsing incoming messages */
517 559
 
518 560
 
561
+static inline unsigned char* binrpc_read_llong(	long long* i,
562
+												int len,
563
+												unsigned char* s,
564
+												unsigned char* end,
565
+												int *err
566
+												)
567
+{
568
+	unsigned char* start;
569
+	unsigned long long u;
570
+
571
+	start=s;
572
+	*i=0;
573
+	u = 0;
574
+	*err=0;
575
+	for(;len>0; len--, s++){
576
+		if (s>=end){
577
+			*err=E_BINRPC_MORE_DATA;
578
+			*i = (long long)u;
579
+			return start;
580
+		}
581
+		u<<=8;
582
+		u|=*s;
583
+	};
584
+	*i = (long long)u;
585
+	return s;
586
+}
587
+
588
+
589
+
519 590
 static inline unsigned char* binrpc_read_int(	int* i,
520 591
 												int len,
521 592
 												unsigned char* s,
... ...
@@ -638,8 +709,8 @@ inline static unsigned char* binrpc_read_record(struct binrpc_parse_ctx* ctx,
638 709
 	int end_tag;
639 710
 	int tmp;
640 711
 	unsigned char* p;
641
-	int i;
642
-	
712
+	long long ll;
713
+
643 714
 	p=buf;
644 715
 	end_tag=0;
645 716
 	*err=0;
... ...
@@ -758,10 +829,10 @@ inline static unsigned char* binrpc_read_record(struct binrpc_parse_ctx* ctx,
758 829
 			}
759 830
 			break;
760 831
 		case BINRPC_T_DOUBLE: /* FIXME: hack: represented as fixed point
761
-		                                      inside an int */
832
+		                                      inside an long long */
762 833
 			if (ctx->in_struct && smode==0) goto error_record;
763
-			p=binrpc_read_int(&i, len, p, end, err);
764
-			v->u.fval=((double)i)/1000;
834
+			p=binrpc_read_llong(&ll, len, p, end, err);
835
+			v->u.fval=((double)ll)/1000;
765 836
 			break;
766 837
 		default:
767 838
 			if (ctx->in_struct){