-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmessage.c
1069 lines (904 loc) · 22.9 KB
/
message.c
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
/*
* Copyright (C) 2004 Steve Harris
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* $Id$
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include "lop_types_internal.h"
#include "lop_internal.h"
#include "lop/lop_lowlevel.h"
#include "lop/lop_macros.h"
#include "lop/lop_endian.h"
#include "lop/lop_osc_types.h"
#define LOP_DEF_TYPE_SIZE 8
#define LOP_DEF_DATA_SIZE 8
static char lop_numerical_types[] = {
LOP_INT32,
LOP_FLOAT,
LOP_INT64,
LOP_DOUBLE,
'\0'
};
static char lop_string_types[] = {
LOP_STRING,
LOP_SYMBOL,
'\0'
};
static int lop_message_add_typechar(lop_message m, char t);
static void *lop_message_add_data(lop_message m, size_t s);
void lop_arg_pp_internal(lop_type type, void *data, int bigendian);
// Used for calculating new sizes when expanding message data buffers.
// Note that log(x)/0.69315 = log2(x): this simply finds the next
// highest power of 2.
#if 1
#define lop_pow2_over(a,b) \
a = ((b > a) ? (a << ((int)((log(((double)b/(double)a))/0.69315)+1))) : a);
#else
#define lop_pow2_over(a,b) \
while (b > a) {a *= 2;}
#endif
lop_message lop_message_new(void)
{
lop_message m = malloc(sizeof(struct _lop_message));
if (!m) {
return m;
}
m->types = calloc(LOP_DEF_TYPE_SIZE, sizeof(char));
m->types[0] = ',';
m->types[1] = '\0';
m->typelen = 1;
m->typesize = LOP_DEF_TYPE_SIZE;
m->data = NULL;
m->datalen = 0;
m->datasize = 0;
m->argv = NULL;
m->ts = LOP_TT_IMMEDIATE;
return m;
}
void lop_message_free(lop_message m)
{
if (m) {
free(m->types);
free(m->data);
free(m->argv);
}
free(m);
}
/* Don't call lop_message_add_varargs_internal directly, use
* lop_message_add_varargs, a macro wrapping this function with
* appropriate values for file and line */
int lop_message_add_varargs_internal(lop_message msg, const char *types,
va_list ap, const char *file, int line)
{
int count = 0;
int i;
int64_t i64;
float f;
char *s;
lop_blob b;
uint8_t *m;
lop_timetag tt;
double d;
int ret = 0;
while (types && *types) {
count++;
i=0;
i64=0;
f=0;
s=0;
b=0;
m=0;
d=0;
switch (*types++) {
case LOP_INT32:
i = va_arg(ap, int32_t);
lop_message_add_int32(msg, i);
break;
case LOP_FLOAT:
f = (float)va_arg(ap, double);
lop_message_add_float(msg, f);
break;
case LOP_STRING:
s = va_arg(ap, char *);
#ifdef __GNUC__
if (s == (char *)LOP_MARKER_A) {
fprintf(stderr, "lop error: lop_message_add called with "
"invalid string pointer for arg %d, probably arg mismatch\n"
"at %s:%d, exiting.\n", count, file, line);
}
#endif
lop_message_add_string(msg, s);
break;
case LOP_BLOB:
b = va_arg(ap, lop_blob);
lop_message_add_blob(msg, b);
break;
case LOP_INT64:
i64 = va_arg(ap, int64_t);
lop_message_add_int64(msg, i64);
break;
case LOP_TIMETAG:
tt = va_arg(ap, lop_timetag);
lop_message_add_timetag(msg, tt);
break;
case LOP_DOUBLE:
d = va_arg(ap, double);
lop_message_add_double(msg, d);
break;
case LOP_SYMBOL:
s = va_arg(ap, char *);
#ifdef __GNUC__
if (s == (char *)LOP_MARKER_A) {
fprintf(stderr, "lop error: lop_message_add called with "
"invalid symbol pointer for arg %d, probably arg mismatch\n"
"at %s:%d, exiting.\n", count, file, line);
va_end(ap);
return -2;
}
#endif
lop_message_add_symbol(msg, s);
break;
case LOP_CHAR:
i = va_arg(ap, int);
lop_message_add_char(msg, i);
break;
case LOP_MIDI:
m = va_arg(ap, uint8_t *);
lop_message_add_midi(msg, m);
break;
case LOP_TRUE:
lop_message_add_true(msg);
break;
case LOP_FALSE:
lop_message_add_false(msg);
break;
case LOP_NIL:
lop_message_add_nil(msg);
break;
case LOP_INFINITUM:
lop_message_add_infinitum(msg);
break;
default:
ret = -1; // unknown type
fprintf(stderr, "lop warning: unknown type '%c' at %s:%d\n",
*(types-1), file, line);
break;
}
}
#ifdef __GNUC__
i = va_arg(ap, uint32_t);
if (i != LOP_MARKER_A) {
ret = -2; // bad format/args
fprintf(stderr, "lop error: lop_message_add or lop_message_add_varargs called with "
"mismatching types and data at\n%s:%d, exiting.\n", file, line);
va_end(ap);
return ret;
}
i = va_arg(ap, uint32_t);
if (i != LOP_MARKER_B) {
ret = -2; // bad format/args
fprintf(stderr, "lop error: lop_message_add or lop_message_add_varargs called with "
"mismatching types and data at\n%s:%d, exiting.\n", file, line);
}
#endif
va_end(ap);
return ret;
}
/* Don't call lop_message_add_internal directly, use lop_message_add,
* a macro wrapping this function with appropriate values for file and line */
#ifdef __GNUC__
int lop_message_add_internal(lop_message msg, const char *file, const int line,
const char *types, ...)
#else
int lop_message_add(lop_message msg, const char *types, ...)
#endif
{
va_list ap;
int ret = 0;
#ifndef __GNUC__
const char *file = "";
const int line = 0;
#endif
va_start(ap, types);
ret = lop_message_add_varargs_internal(msg, types, ap, file, line);
return ret;
}
int lop_message_add_int32(lop_message m, int32_t a)
{
lop_pcast32 b;
int32_t *nptr = lop_message_add_data(m, sizeof(a));
if (!nptr) return -1;
b.i = a;
if (lop_message_add_typechar(m, LOP_INT32))
return -1;
*nptr = b.nl;
return 0;
}
int lop_message_add_float(lop_message m, float a)
{
lop_pcast32 b;
int32_t *nptr = lop_message_add_data(m, sizeof(a));
if (!nptr) return -1;
b.f = a;
if (lop_message_add_typechar(m, LOP_FLOAT))
return -1;
*nptr = b.nl;
return 0;
}
int lop_message_add_string(lop_message m, const char *a)
{
const int size = lop_strsize(a);
char *nptr = lop_message_add_data(m, size);
if (!nptr) return -1;
if (lop_message_add_typechar(m, LOP_STRING))
return -1;
strncpy(nptr, a, size);
return 0;
}
int lop_message_add_blob(lop_message m, lop_blob a)
{
const uint32_t size = lop_blobsize(a);
const uint32_t dsize = lop_blob_datasize(a);
char *nptr = lop_message_add_data(m, size);
if (!nptr) return -1;
if (lop_message_add_typechar(m, LOP_BLOB))
return -1;
memset(nptr + size - 4, 0, 4);
memcpy(nptr, &dsize, sizeof(dsize));
memcpy(nptr + sizeof(int32_t), lop_blob_dataptr(a), lop_blob_datasize(a));
return 0;
}
int lop_message_add_int64(lop_message m, int64_t a)
{
lop_pcast64 b;
uint64_t *nptr = lop_message_add_data(m, sizeof(a));
if (!nptr) return -1;
b.i = a;
if (lop_message_add_typechar(m, LOP_INT64))
return -1;
*nptr = b.nl;
return 0;
}
int lop_message_add_timetag(lop_message m, lop_timetag a)
{
lop_pcast64 b;
uint64_t *nptr = lop_message_add_data(m, sizeof(a));
if (!nptr) return -1;
b.tt = a;
if (lop_message_add_typechar(m, LOP_TIMETAG))
return -1;
*nptr = b.nl;
return 0;
}
int lop_message_add_double(lop_message m, double a)
{
lop_pcast64 b;
uint64_t *nptr = lop_message_add_data(m, sizeof(a));
if (!nptr) return -1;
b.f = a;
if (lop_message_add_typechar(m, LOP_DOUBLE))
return -1;
*nptr = b.nl;
return 0;
}
int lop_message_add_symbol(lop_message m, const char *a)
{
const int size = lop_strsize(a);
char *nptr = lop_message_add_data(m, size);
if (!nptr) return -1;
if (lop_message_add_typechar(m, LOP_SYMBOL))
return -1;
strncpy(nptr, a, size);
return 0;
}
int lop_message_add_char(lop_message m, char a)
{
lop_pcast32 b;
int32_t *nptr = lop_message_add_data(m, sizeof(int32_t));
if (!nptr) return -1;
b.c = a;
if (lop_message_add_typechar(m, LOP_CHAR))
return -1;
*nptr = b.nl;
return 0;
}
int lop_message_add_midi(lop_message m, uint8_t a[4])
{
char *nptr = lop_message_add_data(m, 4);
if (!nptr) return -1;
if (lop_message_add_typechar(m, LOP_MIDI))
return -1;
memcpy(nptr, a, sizeof(a));
return 0;
}
int lop_message_add_true(lop_message m)
{
return lop_message_add_typechar(m, LOP_TRUE);
}
int lop_message_add_false(lop_message m)
{
return lop_message_add_typechar(m, LOP_FALSE);
}
int lop_message_add_nil(lop_message m)
{
return lop_message_add_typechar(m, LOP_NIL);
}
int lop_message_add_infinitum(lop_message m)
{
return lop_message_add_typechar(m, LOP_INFINITUM);
}
static int lop_message_add_typechar(lop_message m, char t)
{
if (m->typelen + 1 >= m->typesize) {
int new_typesize = m->typesize * 2;
char *new_types = 0;
if (!new_typesize)
new_typesize = LOP_DEF_TYPE_SIZE;
new_types = realloc(m->types, new_typesize);
if (!new_types) return -1;
m->types = new_types;
m->typesize = new_typesize;
}
m->types[m->typelen] = t;
m->typelen++;
m->types[m->typelen] = '\0';
if (m->argv) {
free(m->argv);
m->argv = NULL;
}
return 0;
}
static void *lop_message_add_data(lop_message m, size_t s)
{
uint32_t old_dlen = m->datalen;
int new_datasize = m->datasize;
int new_datalen = m->datalen + s;
void *new_data = 0;
if (!new_datasize)
new_datasize = LOP_DEF_DATA_SIZE;
lop_pow2_over(new_datasize, new_datalen);
new_data = realloc(m->data, new_datasize);
if (!new_data)
return 0;
m->datalen = new_datalen;
m->datasize = new_datasize;
m->data = new_data;
if (m->argv) {
free(m->argv);
m->argv = NULL;
}
return (void*)((char*)m->data + old_dlen);
}
int lop_strsize(const char *s)
{
return 4 * (strlen(s) / 4 + 1);
}
size_t lop_arg_size(lop_type type, void *data)
{
switch (type) {
case LOP_TRUE:
case LOP_FALSE:
case LOP_NIL:
case LOP_INFINITUM:
return 0;
case LOP_INT32:
case LOP_FLOAT:
case LOP_MIDI:
case LOP_CHAR:
return 4;
case LOP_INT64:
case LOP_TIMETAG:
case LOP_DOUBLE:
return 8;
case LOP_STRING:
case LOP_SYMBOL:
return lop_strsize((char *)data);
case LOP_BLOB:
return lop_blobsize((lop_blob)data);
default:
fprintf(stderr, "lop warning: unhandled OSC type '%c' at %s:%d\n", type, __FILE__, __LINE__);
return 0;
}
return 0;
}
char *lop_get_path(void *data, ssize_t size)
{
ssize_t result = lop_validate_string(data, size);
return (result >= 4) ? (char *)data : NULL;
}
ssize_t lop_validate_string(void *data, ssize_t size)
{
ssize_t i = 0, len = 0;
char *pos = data;
if (size < 0) {
return -LOP_ESIZE; // invalid size
}
for (i = 0; i < size; ++i) {
if (pos[i] == '\0') {
len = 4 * (i / 4 + 1);
break;
}
}
if (0 == len) {
return -LOP_ETERM; // string not terminated
}
if (len > size) {
return -LOP_ESIZE; // would overflow buffer
}
for (; i < len; ++i) {
if (pos[i] != '\0') {
return -LOP_EPAD; // non-zero char found in pad area
}
}
return len;
}
ssize_t lop_validate_blob(void *data, ssize_t size)
{
ssize_t i, end, len;
uint32_t dsize;
char *pos = (char *)data;
if (size < 0) {
return -LOP_ESIZE; // invalid size
}
dsize = lop_otoh32(*(uint32_t*)data);
if (dsize > LOP_MAX_MSG_SIZE) { // avoid int overflow in next step
return -LOP_ESIZE;
}
end = sizeof(uint32_t) + dsize; // end of data
len = 4 * (end / 4 + 1); // full padded size
if (len > size) {
return -LOP_ESIZE; // would overflow buffer
}
for (i = end; i < len; ++i) {
if (pos[i] != '\0') {
return -LOP_EPAD; // non-zero char found in pad area
}
}
return len;
}
ssize_t lop_validate_bundle(void *data, ssize_t size)
{
ssize_t len = 0, remain = size;
char *pos = data;
uint32_t elem_len;
len = lop_validate_string(data, size);
if (len < 0) {
return -LOP_ESIZE; // invalid size
}
if (0 != strcmp(data, "#bundle")) {
return -LOP_EINVALIDBUND; // not a bundle
}
pos += len;
remain -= len;
// time tag
if (remain < 8) {
return -LOP_ESIZE;
}
pos += 8;
remain -= 8;
while (remain >= 4) {
elem_len = lop_otoh32(*((uint32_t *)pos));
pos += 4;
remain -= 4;
if (elem_len > remain) {
return -LOP_ESIZE;
}
pos += elem_len;
remain -= elem_len;
}
if (0 != remain) {
return -LOP_ESIZE;
}
return size;
}
ssize_t lop_validate_arg(lop_type type, void *data, ssize_t size)
{
if (size < 0) {
return -1;
}
switch (type) {
case LOP_TRUE:
case LOP_FALSE:
case LOP_NIL:
case LOP_INFINITUM:
return 0;
case LOP_INT32:
case LOP_FLOAT:
case LOP_MIDI:
case LOP_CHAR:
return size >= 4 ? 4 : -LOP_ESIZE;
case LOP_INT64:
case LOP_TIMETAG:
case LOP_DOUBLE:
return size >= 8 ? 8 : -LOP_ESIZE;
case LOP_STRING:
case LOP_SYMBOL:
return lop_validate_string((char *)data, size);
case LOP_BLOB:
return lop_validate_blob((lop_blob)data, size);
default:
return -LOP_EINVALIDTYPE;
}
return -LOP_INT_ERR;
}
/* convert endianness of arg pointed to by data from network to host */
void lop_arg_host_endian(lop_type type, void *data)
{
switch (type) {
case LOP_INT32:
case LOP_FLOAT:
case LOP_BLOB:
case LOP_CHAR:
*(int32_t *)data = lop_otoh32(*(int32_t *)data);
break;
case LOP_INT64:
case LOP_TIMETAG:
case LOP_DOUBLE:
*(int64_t *)data = lop_otoh64(*(int64_t *)data);
break;
case LOP_STRING:
case LOP_SYMBOL:
case LOP_MIDI:
case LOP_TRUE:
case LOP_FALSE:
case LOP_NIL:
case LOP_INFINITUM:
/* these are fine */
break;
default:
fprintf(stderr, "lop warning: unhandled OSC type '%c' at %s:%d\n",
type, __FILE__, __LINE__);
break;
}
}
/* convert endianness of arg pointed to by data from host to network */
void lop_arg_network_endian(lop_type type, void *data)
{
switch (type) {
case LOP_INT32:
case LOP_FLOAT:
case LOP_BLOB:
case LOP_CHAR:
*(int32_t *)data = lop_htoo32(*(int32_t *)data);
break;
case LOP_INT64:
case LOP_TIMETAG:
case LOP_DOUBLE:
*(int64_t *)data = lop_htoo64(*(int64_t *)data);
break;
case LOP_STRING:
case LOP_SYMBOL:
case LOP_MIDI:
case LOP_TRUE:
case LOP_FALSE:
case LOP_NIL:
case LOP_INFINITUM:
/* these are fine */
break;
default:
fprintf(stderr, "lop warning: unhandled OSC type '%c' at %s:%d\n",
type, __FILE__, __LINE__);
break;
}
}
lop_timetag lop_message_get_timestamp(lop_message m)
{
return m->ts;
}
size_t lop_message_length(lop_message m, const char *path)
{
return lop_strsize(path) + lop_strsize(m->types) + m->datalen;
}
int lop_message_get_argc(lop_message m)
{
return m->typelen - 1;
}
lop_arg **lop_message_get_argv(lop_message m)
{
int i, argc;
char *types, *ptr;
lop_arg **argv;
if (NULL != m->argv) { return m->argv; }
i = 0;
argc = m->typelen - 1;
types = m->types + 1;
ptr = m->data;
argv = calloc(argc, sizeof(lop_arg *));
for (i = 0; i < argc; ++i) {
size_t len = lop_arg_size(types[i], ptr);
argv[i] = len ? (lop_arg*)ptr : NULL;
ptr += len;
}
m->argv = argv;
return argv;
}
char *lop_message_get_types(lop_message m)
{
return m->types + 1;
}
void *lop_message_serialise(lop_message m, const char *path, void *to,
size_t *size)
{
int i, argc;
char *types, *ptr;
size_t s = lop_message_length(m, path);
if (size) {
*size = s;
}
if (!to) {
to = calloc(1, s);
}
memset((char*)to + lop_strsize(path) - 4, 0, 4); // ensure zero-padding
strcpy(to, path);
memset((char*)to + lop_strsize(path) + lop_strsize(m->types) - 4, 0, 4);
strcpy((char*)to + lop_strsize(path), m->types);
types = m->types + 1;
ptr = (char*)to + lop_strsize(path) + lop_strsize(m->types);
memcpy(ptr, m->data, m->datalen);
i = 0;
argc = m->typelen - 1;
for (i = 0; i < argc; ++i) {
size_t len = lop_arg_size(types[i], ptr);
lop_arg_network_endian(types[i], ptr);
ptr += len;
}
return to;
}
lop_message lop_message_deserialise(void *data, size_t size, int *result)
{
lop_message msg = NULL;
char *types = NULL, *ptr = NULL;
int i = 0, argc = 0, remain = size, res = 0, len;
if (remain <= 0) { res = LOP_ESIZE; goto fail; }
msg = malloc(sizeof(struct _lop_message));
if (!msg) { res = LOP_EALLOC; goto fail; }
msg->types = NULL;
msg->typelen = 0;
msg->typesize = 0;
msg->data = NULL;
msg->datalen = 0;
msg->datasize = 0;
msg->argv = NULL;
msg->ts = LOP_TT_IMMEDIATE;
// path
len = lop_validate_string(data, remain);
if (len < 0) {
res = LOP_EINVALIDPATH; // invalid path string
goto fail;
}
remain -= len;
// types
if (remain <= 0) {
res = LOP_ENOTYPE; // no type tag string
goto fail;
}
types = (char*)data + len;
len = lop_validate_string(types, remain);
if (len < 0) {
res = LOP_EINVALIDTYPE; // invalid type tag string
goto fail;
}
if (types[0] != ',') {
res = LOP_EBADTYPE; // type tag string missing initial comma
goto fail;
}
remain -= len;
msg->typelen = strlen(types);
msg->typesize = len;
msg->types = malloc(msg->typesize);
if (NULL == msg->types) { res = LOP_EALLOC; goto fail; }
memcpy(msg->types, types, msg->typesize);
// args
msg->data = malloc(remain);
if (NULL == msg->data) { res = LOP_EALLOC; goto fail; }
memcpy(msg->data, types + len, remain);
msg->datalen = msg->datasize = remain;
ptr = msg->data;
++types;
argc = msg->typelen - 1;
if (argc) {
msg->argv = calloc(argc, sizeof(lop_arg *));
if (NULL == msg->argv) { res = LOP_EALLOC; goto fail; }
}
for (i = 0; remain >= 0 && i < argc; ++i) {
len = lop_validate_arg((lop_type)types[i], ptr, remain);
if (len < 0) {
res = LOP_EINVALIDARG; // invalid argument
goto fail;
}
lop_arg_host_endian((lop_type)types[i], ptr);
msg->argv[i] = len ? (lop_arg*)ptr : NULL;
remain -= len;
ptr += len;
}
if (0 != remain || i != argc) {
res = LOP_ESIZE; // size/argument mismatch
goto fail;
}
if (result) { *result = res; }
return msg;
fail:
if (msg) { lop_message_free(msg); }
if (result) { *result = res; }
return NULL;
}
void lop_message_pp(lop_message m)
{
void *d = m->data;
void *end = (char*)m->data + m->datalen;
int i;
printf("%s ", m->types);
for (i = 1; m->types[i]; i++) {
if (i > 1) {
printf(" ");
}
lop_arg_pp_internal(m->types[i], d, 1);
d = (char*)d + lop_arg_size(m->types[i], d);
}
putchar('\n');
if (d != end) {
fprintf(stderr, "lop warning: type and data do not match (off by %d) in message %p\n",
abs((char*)d - (char*)end), m);
}
}
void lop_arg_pp(lop_type type, void *data)
{
lop_arg_pp_internal(type, data, 0);
}
void lop_arg_pp_internal(lop_type type, void *data, int bigendian)
{
lop_pcast32 val32;
lop_pcast64 val64;
int size;
int i;
size = lop_arg_size(type, data);
if (size == 4 || type == LOP_BLOB) {
if (bigendian) {
val32.nl = lop_otoh32(*(int32_t *)data);
} else {
val32.nl = *(int32_t *)data;
}
} else if (size == 8) {
if (bigendian) {
val64.nl = lop_otoh64(*(int64_t *)data);
} else {
val64.nl = *(int64_t *)data;
}
}
switch (type) {
case LOP_INT32:
printf("%"PRId32, val32.i);
break;
case LOP_FLOAT:
printf("%f", val32.f);
break;
case LOP_STRING:
printf("\"%s\"", (char *)data);
break;
case LOP_BLOB:
printf("[");
if (val32.i > 12) {
printf("%"PRId32" byte blob", val32.i);
} else {
printf("%"PRId32"b ", val32.i);
for (i=0; i<val32.i; i++) {
printf("0x%02x", *((char *)(data) + 4 + i));
if (i+1 < val32.i) printf(" ");
}
}
printf("]");
break;
case LOP_INT64:
printf("%lld", (long long int)val64.i);
break;
case LOP_TIMETAG:
printf("%08"PRIx32".%08"PRIx32, val64.tt.sec, val64.tt.frac);
break;
case LOP_DOUBLE:
printf("%f", val64.f);
break;
case LOP_SYMBOL:
printf("'%s", (char *)data);
break;
case LOP_CHAR:
printf("'%c'", (char)val32.c);
break;
case LOP_MIDI:
printf("MIDI [");
for (i=0; i<4; i++) {
printf("0x%02x", *((uint8_t *)(data) + i));
if (i+1 < 4) printf(" ");
}
printf("]");
break;
case LOP_TRUE:
printf("#T");
break;
case LOP_FALSE:
printf("#F");
break;
case LOP_NIL:
printf("Nil");
break;
case LOP_INFINITUM:
printf("Infinitum");
break;
default:
fprintf(stderr, "lop warning: unhandled type: %c\n", type);
break;
}
}
int lop_is_numerical_type(lop_type a)
{
return strchr(lop_numerical_types, a) != 0;
}
int lop_is_string_type(lop_type a)