27 #include <netinet/in.h>
34 #define LO_DEF_TYPE_SIZE 8
35 #define LO_DEF_DATA_SIZE 8
59 #define lo_pow2_over(a,b) \
60 a = ((b > a) ? (a << ((int)((log(((double)b/(double)a))/0.69315)+1))) : a);
62 #define lo_pow2_over(a,b) \
63 while (b > a) {a *= 2;}
103 va_list ap,
const char *file,
int line)
116 while (types && *types) {
128 i = va_arg(ap, int32_t);
133 f = (float)va_arg(ap,
double);
138 s = va_arg(ap,
char *);
141 fprintf(stderr,
"liblo error: lo_send or lo_message_add called with "
142 "invalid string pointer for arg %d, probably arg mismatch\n"
143 "at %s:%d, exiting.\n", count, file, line);
155 i64 = va_arg(ap, int64_t);
165 d = va_arg(ap,
double);
170 s = va_arg(ap,
char *);
173 fprintf(stderr,
"liblo error: lo_send or lo_message_add called with "
174 "invalid symbol pointer for arg %d, probably arg mismatch\n"
175 "at %s:%d, exiting.\n", count, file, line);
189 m = va_arg(ap, uint8_t *);
211 fprintf(stderr,
"liblo warning: unknown type '%c' at %s:%d\n",
212 *(types-1), file, line);
217 i = va_arg(ap, uint32_t);
220 fprintf(stderr,
"liblo error: lo_send, lo_message_add, or lo_message_add_varargs called with "
221 "mismatching types and data at\n%s:%d, exiting.\n", file, line);
225 i = va_arg(ap, uint32_t);
228 fprintf(stderr,
"liblo error: lo_send, lo_message_add, or lo_message_add_varargs called with "
229 "mismatching types and data at\n%s:%d, exiting.\n", file, line);
242 const char *types, ...)
251 const char *file =
"";
265 if (!nptr)
return -1;
278 if (!nptr)
return -1;
291 if (!nptr)
return -1;
295 strncpy(nptr, a, size);
304 if (!nptr)
return -1;
308 memset(nptr + size - 4, 0, 4);
310 memcpy(nptr, &dsize,
sizeof(dsize));
319 if (!nptr)
return -1;
332 if (!nptr)
return -1;
345 if (!nptr)
return -1;
358 if (!nptr)
return -1;
362 strncpy(nptr, a, size);
370 if (!nptr)
return -1;
383 if (!nptr)
return -1;
388 memcpy(nptr, a,
sizeof(a));
414 if (m->typelen + 1 >= m->typesize) {
415 int new_typesize = m->typesize * 2;
419 new_types = realloc(m->types, new_typesize);
420 if (!new_types)
return -1;
421 m->types = new_types;
422 m->typesize = new_typesize;
424 m->types[m->typelen] = t;
426 m->types[m->typelen] =
'\0';
436 uint32_t old_dlen = m->datalen;
437 int new_datasize = m->datasize;
438 int new_datalen = m->datalen + s;
445 new_data = realloc(m->data, new_datasize);
449 m->datalen = new_datalen;
450 m->datasize = new_datasize;
458 return (
void*)((
char*)m->data + old_dlen);
463 return 4 * (strlen(s) / 4 + 1);
494 fprintf(stderr,
"liblo warning: unhandled OSC type '%c' at %s:%d\n", type, __FILE__, __LINE__);
504 return (result >= 4) ? (
char *)data : NULL;
509 ssize_t i = 0, len = 0;
515 for (i = 0; i < size; ++i) {
516 if (pos[i] ==
'\0') {
517 len = 4 * (i / 4 + 1);
527 for (; i < len; ++i) {
528 if (pos[i] !=
'\0') {
540 char *pos = (
char *)data;
549 end =
sizeof(uint32_t) + dsize;
550 len = 4 * (end / 4 + 1);
554 for (i = end; i < len; ++i) {
555 if (pos[i] !=
'\0') {
565 ssize_t len = 0, remain = size;
573 if (0 != strcmp(data,
"#bundle")) {
586 while (remain >= 4) {
587 elem_len =
lo_otoh32(*((uint32_t *)pos));
590 if (elem_len > remain) {
667 fprintf(stderr,
"liblo warning: unhandled OSC type '%c' at %s:%d\n",
668 type, __FILE__, __LINE__);
701 fprintf(stderr,
"liblo warning: unhandled OSC type '%c' at %s:%d\n",
702 type, __FILE__, __LINE__);
724 return m->typelen - 1;
733 if (NULL != m->argv) {
return m->argv; }
736 argc = m->typelen - 1;
737 types = m->types + 1;
740 argv = calloc(argc,
sizeof(
lo_arg *));
741 for (i = 0; i < argc; ++i) {
743 argv[i] = len ? (
lo_arg*)ptr : NULL;
769 memset((
char*)to +
lo_strsize(path) - 4, 0, 4);
772 strcpy((
char*)to +
lo_strsize(path), m->types);
774 types = m->types + 1;
776 memcpy(ptr, m->data, m->datalen);
779 argc = m->typelen - 1;
780 for (i = 0; i < argc; ++i) {
792 char *types = NULL, *ptr = NULL;
793 int i = 0, argc = 0, remain = size, res = 0, len;
795 if (remain <= 0) { res =
LO_ESIZE;
goto fail; }
798 if (!msg) { res =
LO_EALLOC;
goto fail; }
823 types = (
char*)data + len;
829 if (types[0] !=
',') {
835 msg->typelen = strlen(types);
837 msg->types = malloc(msg->typesize);
838 if (NULL == msg->types) { res =
LO_EALLOC;
goto fail; }
839 memcpy(msg->types, types, msg->typesize);
842 msg->data = malloc(remain);
843 if (NULL == msg->data) { res =
LO_EALLOC;
goto fail; }
844 memcpy(msg->data, types + len, remain);
845 msg->datalen = msg->datasize = remain;
849 argc = msg->typelen - 1;
851 msg->argv = calloc(argc,
sizeof(
lo_arg *));
852 if (NULL == msg->argv) { res =
LO_EALLOC;
goto fail; }
855 for (i = 0; remain >= 0 && i < argc; ++i) {
862 msg->argv[i] = len ? (
lo_arg*)ptr : NULL;
866 if (0 != remain || i != argc) {
871 if (result) { *result = res; }
876 if (result) { *result = res; }
883 void *end = (
char*)m->data + m->datalen;
886 printf(
"%s ", m->types);
887 for (i = 1; m->types[i]; i++) {
897 fprintf(stderr,
"liblo warning: type and data do not match (off by %d) in message %p\n",
898 abs((
char*)d - (
char*)end), m);
915 if (size == 4 || type ==
LO_BLOB) {
919 val32.
nl = *(int32_t *)data;
921 }
else if (size == 8) {
925 val64.
nl = *(int64_t *)data;
931 printf(
"%d", val32.
i);
935 printf(
"%f", val32.
f);
939 printf(
"\"%s\"", (
char *)data);
945 printf(
"%d byte blob", val32.
i);
947 printf(
"%db ", val32.
i);
948 for (i=0; i<val32.
i; i++) {
949 printf(
"0x%02x", *((
char *)(data) + 4 + i));
950 if (i+1 < val32.
i) printf(
" ");
957 printf(
"%lld", (
long long int)val64.
i);
965 printf(
"%f", val64.
f);
969 printf(
"'%s", (
char *)data);
973 printf(
"'%c'", (
char)val32.
c);
978 for (i=0; i<4; i++) {
979 printf(
"0x%02x", *((uint8_t *)(data) + i));
980 if (i+1 < 4) printf(
" ");
1002 fprintf(stderr,
"liblo warning: unhandled type: %c\n", type);
1019 if (type_to == type_from) {
1026 strcpy((
char *)to, (
char *)from);
1050 fprintf(stderr,
"liblo: bad coercion: %c -> %c\n", type_from,
1072 fprintf(stderr,
"liblo: hires val requested of non numerical type '%c' at %s:%d\n", type, __FILE__, __LINE__);