Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rtosc.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <stdbool.h>
  6. #include <ctype.h>
  7. #include <assert.h>
  8. #include <rtosc/rtosc.h>
  9. const char *rtosc_argument_string(const char *msg)
  10. {
  11. assert(msg && *msg);
  12. while(*++msg); //skip pattern
  13. while(!*++msg);//skip null
  14. return msg+1; //skip comma
  15. }
  16. unsigned rtosc_narguments(const char *msg)
  17. {
  18. const char *args = rtosc_argument_string(msg);
  19. int nargs = 0;
  20. while(*args++)
  21. nargs += (*args == ']' || *args == '[') ? 0 : 1;
  22. return nargs;
  23. }
  24. static int has_reserved(char type)
  25. {
  26. switch(type)
  27. {
  28. case 'i'://official types
  29. case 's':
  30. case 'b':
  31. case 'f':
  32. case 'h'://unofficial
  33. case 't':
  34. case 'd':
  35. case 'S':
  36. case 'r':
  37. case 'm':
  38. case 'c':
  39. return 1;
  40. case 'T':
  41. case 'F':
  42. case 'N':
  43. case 'I':
  44. case '[':
  45. case ']':
  46. return 0;
  47. }
  48. //Should not happen
  49. return 0;
  50. }
  51. static unsigned nreserved(const char *args)
  52. {
  53. unsigned res = 0;
  54. for(;*args;++args)
  55. res += has_reserved(*args);
  56. return res;
  57. }
  58. char rtosc_type(const char *msg, unsigned nargument)
  59. {
  60. assert(nargument < rtosc_narguments(msg));
  61. const char *arg = rtosc_argument_string(msg);
  62. while(1) {
  63. if(*arg == '[' || *arg == ']')
  64. ++arg;
  65. else if(!nargument || !*arg)
  66. return *arg;
  67. else
  68. ++arg, --nargument;
  69. }
  70. }
  71. static unsigned arg_start(const char *msg_)
  72. {
  73. const uint8_t *msg = (const uint8_t*)msg_;
  74. //Iterate to the right position
  75. const uint8_t *args = (const uint8_t*) rtosc_argument_string(msg_);
  76. const uint8_t *aligned_ptr = args-1;
  77. const uint8_t *arg_pos = args;
  78. while(*++arg_pos);
  79. //Alignment
  80. arg_pos += 4-(arg_pos-aligned_ptr)%4;
  81. return arg_pos-msg;
  82. }
  83. static unsigned arg_size(const uint8_t *arg_mem, char type)
  84. {
  85. if(!has_reserved(type))
  86. return 0;
  87. const uint8_t *arg_pos=arg_mem;
  88. uint32_t blob_length = 0;
  89. switch(type)
  90. {
  91. case 'h':
  92. case 't':
  93. case 'd':
  94. return 8;
  95. case 'm':
  96. case 'r':
  97. case 'f':
  98. case 'c':
  99. case 'i':
  100. return 4;
  101. case 'S':
  102. case 's':
  103. while(*++arg_pos);
  104. arg_pos += 4-(arg_pos-arg_mem)%4;
  105. return arg_pos-arg_mem;
  106. case 'b':
  107. blob_length |= (*arg_pos++ << 24);
  108. blob_length |= (*arg_pos++ << 16);
  109. blob_length |= (*arg_pos++ << 8);
  110. blob_length |= (*arg_pos++);
  111. if(blob_length%4)
  112. blob_length += 4-blob_length%4;
  113. arg_pos += blob_length;
  114. return arg_pos-arg_mem;
  115. default:
  116. assert("Invalid Type");
  117. }
  118. return -1;
  119. }
  120. static unsigned arg_off(const char *msg, unsigned idx)
  121. {
  122. if(!has_reserved(rtosc_type(msg,idx)))
  123. return 0;
  124. //Iterate to the right position
  125. const uint8_t *args = (const uint8_t*) rtosc_argument_string(msg);
  126. const uint8_t *aligned_ptr = args-1;
  127. const uint8_t *arg_pos = args;
  128. while(*++arg_pos);
  129. //Alignment
  130. arg_pos += 4-(arg_pos-((uint8_t*)aligned_ptr))%4;
  131. //ignore any leading '[' or ']'
  132. while(*args == '[' || *args == ']')
  133. ++args;
  134. while(idx--) {
  135. char type = *args++;
  136. if(type == '[' || type == ']')
  137. idx++;//not a valid arg idx
  138. else
  139. arg_pos += arg_size(arg_pos, type);
  140. }
  141. return arg_pos-(uint8_t*)msg;
  142. }
  143. size_t rtosc_message(char *buffer,
  144. size_t len,
  145. const char *address,
  146. const char *arguments,
  147. ...)
  148. {
  149. va_list va;
  150. va_start(va, arguments);
  151. size_t result = rtosc_vmessage(buffer, len, address, arguments, va);
  152. va_end(va);
  153. return result;
  154. }
  155. //Calculate the size of the message without writing to a buffer
  156. static size_t vsosc_null(const char *address,
  157. const char *arguments,
  158. const rtosc_arg_t *args)
  159. {
  160. unsigned pos = 0;
  161. pos += strlen(address);
  162. pos += 4-pos%4;//get 32 bit alignment
  163. pos += 1+strlen(arguments);
  164. pos += 4-pos%4;
  165. unsigned toparse = nreserved(arguments);
  166. unsigned arg_pos = 0;
  167. //Take care of varargs
  168. while(toparse)
  169. {
  170. char arg = *arguments++;
  171. assert(arg);
  172. int i;
  173. const char *s;
  174. switch(arg) {
  175. case 'h':
  176. case 't':
  177. case 'd':
  178. ++arg_pos;
  179. pos += 8;
  180. --toparse;
  181. break;
  182. case 'm':
  183. case 'r':
  184. case 'c':
  185. case 'f':
  186. case 'i':
  187. ++arg_pos;
  188. pos += 4;
  189. --toparse;
  190. break;
  191. case 's':
  192. case 'S':
  193. s = args[arg_pos++].s;
  194. assert(s && "Input strings CANNOT be NULL");
  195. pos += strlen(s);
  196. pos += 4-pos%4;
  197. --toparse;
  198. break;
  199. case 'b':
  200. i = args[arg_pos++].b.len;
  201. pos += 4 + i;
  202. if(pos%4)
  203. pos += 4-pos%4;
  204. --toparse;
  205. break;
  206. default:
  207. ;
  208. }
  209. }
  210. return pos;
  211. }
  212. size_t rtosc_vmessage(char *buffer,
  213. size_t len,
  214. const char *address,
  215. const char *arguments,
  216. va_list ap)
  217. {
  218. const unsigned nargs = nreserved(arguments);
  219. if(!nargs)
  220. return rtosc_amessage(buffer,len,address,arguments,NULL);
  221. rtosc_arg_t args[nargs];
  222. unsigned arg_pos = 0;
  223. const char *arg_str = arguments;
  224. uint8_t *midi_tmp;
  225. while(arg_pos < nargs)
  226. {
  227. switch(*arg_str++) {
  228. case 'h':
  229. case 't':
  230. args[arg_pos++].h = va_arg(ap, int64_t);
  231. break;
  232. case 'd':
  233. args[arg_pos++].d = va_arg(ap, double);
  234. break;
  235. case 'c':
  236. case 'i':
  237. case 'r':
  238. args[arg_pos++].i = va_arg(ap, int);
  239. break;
  240. case 'm':
  241. midi_tmp = va_arg(ap, uint8_t *);
  242. args[arg_pos].m[0] = midi_tmp[0];
  243. args[arg_pos].m[1] = midi_tmp[1];
  244. args[arg_pos].m[2] = midi_tmp[2];
  245. args[arg_pos++].m[3] = midi_tmp[3];
  246. break;
  247. case 'S':
  248. case 's':
  249. args[arg_pos++].s = va_arg(ap, const char *);
  250. break;
  251. case 'b':
  252. args[arg_pos].b.len = va_arg(ap, int);
  253. args[arg_pos].b.data = va_arg(ap, unsigned char *);
  254. arg_pos++;
  255. break;
  256. case 'f':
  257. args[arg_pos++].f = va_arg(ap, double);
  258. break;
  259. default:
  260. ;
  261. }
  262. }
  263. return rtosc_amessage(buffer,len,address,arguments,args);
  264. }
  265. size_t rtosc_amessage(char *buffer,
  266. size_t len,
  267. const char *address,
  268. const char *arguments,
  269. const rtosc_arg_t *args)
  270. {
  271. const size_t total_len = vsosc_null(address, arguments, args);
  272. if(!buffer)
  273. return total_len;
  274. //Abort if the message cannot fit
  275. if(total_len>len) {
  276. memset(buffer, 0, len);
  277. return 0;
  278. }
  279. memset(buffer, 0, total_len);
  280. unsigned pos = 0;
  281. while(*address)
  282. buffer[pos++] = *address++;
  283. //get 32 bit alignment
  284. pos += 4-pos%4;
  285. buffer[pos++] = ',';
  286. const char *arg_str = arguments;
  287. while(*arg_str)
  288. buffer[pos++] = *arg_str++;
  289. pos += 4-pos%4;
  290. unsigned toparse = nreserved(arguments);
  291. unsigned arg_pos = 0;
  292. while(toparse)
  293. {
  294. char arg = *arguments++;
  295. assert(arg);
  296. int32_t i;
  297. int64_t d;
  298. const uint8_t *m;
  299. const char *s;
  300. const unsigned char *u;
  301. rtosc_blob_t b;
  302. switch(arg) {
  303. case 'h':
  304. case 't':
  305. case 'd':
  306. d = args[arg_pos++].t;
  307. buffer[pos++] = ((d>>56) & 0xff);
  308. buffer[pos++] = ((d>>48) & 0xff);
  309. buffer[pos++] = ((d>>40) & 0xff);
  310. buffer[pos++] = ((d>>32) & 0xff);
  311. buffer[pos++] = ((d>>24) & 0xff);
  312. buffer[pos++] = ((d>>16) & 0xff);
  313. buffer[pos++] = ((d>>8) & 0xff);
  314. buffer[pos++] = (d & 0xff);
  315. --toparse;
  316. break;
  317. case 'r':
  318. case 'f':
  319. case 'c':
  320. case 'i':
  321. i = args[arg_pos++].i;
  322. buffer[pos++] = ((i>>24) & 0xff);
  323. buffer[pos++] = ((i>>16) & 0xff);
  324. buffer[pos++] = ((i>>8) & 0xff);
  325. buffer[pos++] = (i & 0xff);
  326. --toparse;
  327. break;
  328. case 'm':
  329. //TODO verify ordering of spec
  330. m = args[arg_pos++].m;
  331. buffer[pos++] = m[0];
  332. buffer[pos++] = m[1];
  333. buffer[pos++] = m[2];
  334. buffer[pos++] = m[3];
  335. --toparse;
  336. break;
  337. case 'S':
  338. case 's':
  339. s = args[arg_pos++].s;
  340. while(*s)
  341. buffer[pos++] = *s++;
  342. pos += 4-pos%4;
  343. --toparse;
  344. break;
  345. case 'b':
  346. b = args[arg_pos++].b;
  347. i = b.len;
  348. buffer[pos++] = ((i>>24) & 0xff);
  349. buffer[pos++] = ((i>>16) & 0xff);
  350. buffer[pos++] = ((i>>8) & 0xff);
  351. buffer[pos++] = (i & 0xff);
  352. u = b.data;
  353. if(u) {
  354. while(i--)
  355. buffer[pos++] = *u++;
  356. }
  357. else
  358. pos += i;
  359. if(pos%4)
  360. pos += 4-pos%4;
  361. --toparse;
  362. break;
  363. default:
  364. ;
  365. }
  366. }
  367. return pos;
  368. }
  369. static rtosc_arg_t extract_arg(const uint8_t *arg_pos, char type)
  370. {
  371. rtosc_arg_t result = {0};
  372. //trivial case
  373. if(!has_reserved(type)) {
  374. switch(type)
  375. {
  376. case 'T':
  377. result.T = true;
  378. break;
  379. case 'F':
  380. result.T = false;
  381. break;
  382. default:
  383. ;
  384. }
  385. } else {
  386. switch(type)
  387. {
  388. case 'h':
  389. case 't':
  390. case 'd':
  391. result.t |= (((uint64_t)*arg_pos++) << 56);
  392. result.t |= (((uint64_t)*arg_pos++) << 48);
  393. result.t |= (((uint64_t)*arg_pos++) << 40);
  394. result.t |= (((uint64_t)*arg_pos++) << 32);
  395. result.t |= (((uint64_t)*arg_pos++) << 24);
  396. result.t |= (((uint64_t)*arg_pos++) << 16);
  397. result.t |= (((uint64_t)*arg_pos++) << 8);
  398. result.t |= (((uint64_t)*arg_pos++));
  399. break;
  400. case 'r':
  401. case 'f':
  402. case 'c':
  403. case 'i':
  404. result.i |= (*arg_pos++ << 24);
  405. result.i |= (*arg_pos++ << 16);
  406. result.i |= (*arg_pos++ << 8);
  407. result.i |= (*arg_pos++);
  408. break;
  409. case 'm':
  410. result.m[0] = *arg_pos++;
  411. result.m[1] = *arg_pos++;
  412. result.m[2] = *arg_pos++;
  413. result.m[3] = *arg_pos++;
  414. break;
  415. case 'b':
  416. result.b.len |= (*arg_pos++ << 24);
  417. result.b.len |= (*arg_pos++ << 16);
  418. result.b.len |= (*arg_pos++ << 8);
  419. result.b.len |= (*arg_pos++);
  420. result.b.data = (unsigned char *)arg_pos;
  421. break;
  422. case 'S':
  423. case 's':
  424. result.s = (char *)arg_pos;
  425. break;
  426. }
  427. }
  428. return result;
  429. }
  430. static const char *advance_past_dummy_args(const char *args)
  431. {
  432. while(*args == '[' || *args == ']')
  433. args++;
  434. return args;
  435. }
  436. rtosc_arg_itr_t rtosc_itr_begin(const char *msg)
  437. {
  438. rtosc_arg_itr_t itr;
  439. itr.type_pos = advance_past_dummy_args(rtosc_argument_string(msg));
  440. itr.value_pos = (uint8_t*)(msg+arg_start(msg));
  441. return itr;
  442. }
  443. rtosc_arg_val_t rtosc_itr_next(rtosc_arg_itr_t *itr)
  444. {
  445. //current position provides the value
  446. rtosc_arg_val_t result = {0,{0}};
  447. result.type = *itr->type_pos;
  448. if(result.type)
  449. result.val = extract_arg(itr->value_pos, result.type);
  450. //advance
  451. itr->type_pos = advance_past_dummy_args(itr->type_pos+1);
  452. char type = result.type;
  453. int size = arg_size(itr->value_pos, type);
  454. itr->value_pos += size;
  455. return result;
  456. }
  457. int rtosc_itr_end(rtosc_arg_itr_t itr)
  458. {
  459. return !itr.type_pos || !*itr.type_pos;
  460. }
  461. rtosc_arg_t rtosc_argument(const char *msg, unsigned idx)
  462. {
  463. char type = rtosc_type(msg, idx);
  464. uint8_t *arg_mem = (uint8_t*)msg + arg_off(msg, idx);
  465. return extract_arg(arg_mem, type);
  466. }
  467. static unsigned char deref(unsigned pos, ring_t *ring)
  468. {
  469. return pos<ring[0].len ? ring[0].data[pos] :
  470. ((pos-ring[0].len)<ring[1].len ? ring[1].data[pos-ring[0].len] : 0x00);
  471. }
  472. static size_t bundle_ring_length(ring_t *ring)
  473. {
  474. unsigned pos = 8+8;//goto first length field
  475. uint32_t advance = 0;
  476. do {
  477. advance = deref(pos+0, ring) << (8*3) |
  478. deref(pos+1, ring) << (8*2) |
  479. deref(pos+2, ring) << (8*1) |
  480. deref(pos+3, ring) << (8*0);
  481. if(advance)
  482. pos += 4+advance;
  483. } while(advance);
  484. return pos <= (ring[0].len+ring[1].len) ? pos : 0;
  485. }
  486. //Zero means no full message present
  487. size_t rtosc_message_ring_length(ring_t *ring)
  488. {
  489. //Check if the message is a bundle
  490. if(deref(0,ring) == '#' &&
  491. deref(1,ring) == 'b' &&
  492. deref(2,ring) == 'u' &&
  493. deref(3,ring) == 'n' &&
  494. deref(4,ring) == 'd' &&
  495. deref(5,ring) == 'l' &&
  496. deref(6,ring) == 'e' &&
  497. deref(7,ring) == '\0')
  498. return bundle_ring_length(ring);
  499. //Proceed for normal messages
  500. //Consume path
  501. unsigned pos = 0;
  502. while(deref(pos++,ring));
  503. pos--;
  504. //Travel through the null word end [1..4] bytes
  505. for(int i=0; i<4; ++i)
  506. if(deref(++pos, ring))
  507. break;
  508. if(deref(pos, ring) != ',')
  509. return 0;
  510. unsigned aligned_pos = pos;
  511. int arguments = pos+1;
  512. while(deref(++pos,ring));
  513. pos += 4-(pos-aligned_pos)%4;
  514. unsigned toparse = 0;
  515. {
  516. int arg = arguments-1;
  517. while(deref(++arg,ring))
  518. toparse += has_reserved(deref(arg,ring));
  519. }
  520. //Take care of varargs
  521. while(toparse)
  522. {
  523. char arg = deref(arguments++,ring);
  524. assert(arg);
  525. uint32_t i;
  526. switch(arg) {
  527. case 'h':
  528. case 't':
  529. case 'd':
  530. pos += 8;
  531. --toparse;
  532. break;
  533. case 'm':
  534. case 'r':
  535. case 'c':
  536. case 'f':
  537. case 'i':
  538. pos += 4;
  539. --toparse;
  540. break;
  541. case 'S':
  542. case 's':
  543. while(deref(++pos,ring));
  544. pos += 4-(pos-aligned_pos)%4;
  545. --toparse;
  546. break;
  547. case 'b':
  548. i = 0;
  549. i |= (deref(pos++,ring) << 24);
  550. i |= (deref(pos++,ring) << 16);
  551. i |= (deref(pos++,ring) << 8);
  552. i |= (deref(pos++,ring));
  553. pos += i;
  554. if((pos-aligned_pos)%4)
  555. pos += 4-(pos-aligned_pos)%4;
  556. --toparse;
  557. break;
  558. default:
  559. ;
  560. }
  561. }
  562. return pos <= (ring[0].len+ring[1].len) ? pos : 0;
  563. }
  564. size_t rtosc_message_length(const char *msg, size_t len)
  565. {
  566. ring_t ring[2] = {{(char*)msg,len},{NULL,0}};
  567. return rtosc_message_ring_length(ring);
  568. }
  569. bool rtosc_valid_message_p(const char *msg, size_t len)
  570. {
  571. //Validate Path Characters (assumes printable characters are sufficient)
  572. if(*msg != '/')
  573. return false;
  574. const char *tmp = msg;
  575. for(unsigned i=0; i<len; ++i) {
  576. if(*tmp == 0)
  577. break;
  578. if(!isprint(*tmp))
  579. return false;
  580. tmp++;
  581. }
  582. //tmp is now either pointing to a null or the end of the string
  583. const size_t offset1 = tmp-msg;
  584. size_t offset2 = tmp-msg;
  585. for(; offset2<len; offset2++) {
  586. if(*tmp == ',')
  587. break;
  588. tmp++;
  589. }
  590. //Too many NULL bytes
  591. if(offset2-offset1 > 4)
  592. return false;
  593. if((offset2 % 4) != 0)
  594. return false;
  595. size_t observed_length = rtosc_message_length(msg, len);
  596. return observed_length == len;
  597. }
  598. static uint64_t extract_uint64(const uint8_t *arg_pos)
  599. {
  600. uint64_t arg = 0;
  601. arg |= (((uint64_t)*arg_pos++) << 56);
  602. arg |= (((uint64_t)*arg_pos++) << 48);
  603. arg |= (((uint64_t)*arg_pos++) << 40);
  604. arg |= (((uint64_t)*arg_pos++) << 32);
  605. arg |= (((uint64_t)*arg_pos++) << 24);
  606. arg |= (((uint64_t)*arg_pos++) << 16);
  607. arg |= (((uint64_t)*arg_pos++) << 8);
  608. arg |= (((uint64_t)*arg_pos++));
  609. return arg;
  610. }
  611. static uint32_t extract_uint32(const uint8_t *arg_pos)
  612. {
  613. uint32_t arg = 0;
  614. arg |= (((uint32_t)*arg_pos++) << 24);
  615. arg |= (((uint32_t)*arg_pos++) << 16);
  616. arg |= (((uint32_t)*arg_pos++) << 8);
  617. arg |= (((uint32_t)*arg_pos++));
  618. return arg;
  619. }
  620. static void emplace_uint64(uint8_t *buffer, uint64_t d)
  621. {
  622. buffer[0] = ((d>>56) & 0xff);
  623. buffer[1] = ((d>>48) & 0xff);
  624. buffer[2] = ((d>>40) & 0xff);
  625. buffer[3] = ((d>>32) & 0xff);
  626. buffer[4] = ((d>>24) & 0xff);
  627. buffer[5] = ((d>>16) & 0xff);
  628. buffer[6] = ((d>>8) & 0xff);
  629. buffer[7] = ((d>>0) & 0xff);
  630. }
  631. static void emplace_uint32(uint8_t *buffer, uint32_t d)
  632. {
  633. buffer[0] = ((d>>24) & 0xff);
  634. buffer[1] = ((d>>16) & 0xff);
  635. buffer[2] = ((d>>8) & 0xff);
  636. buffer[3] = ((d>>0) & 0xff);
  637. }
  638. size_t rtosc_bundle(char *buffer, size_t len, uint64_t tt, int elms, ...)
  639. {
  640. char *_buffer = buffer;
  641. memset(buffer, 0, len);
  642. strcpy(buffer, "#bundle");
  643. buffer += 8;
  644. emplace_uint64((uint8_t*)buffer, tt);
  645. buffer += 8;
  646. va_list va;
  647. va_start(va, elms);
  648. for(int i=0; i<elms; ++i) {
  649. const char *msg = va_arg(va, const char*);
  650. //It is assumed that any passed message/bundle is valid
  651. size_t size = rtosc_message_length(msg, -1);
  652. emplace_uint32((uint8_t*)buffer, size);
  653. buffer += 4;
  654. memcpy(buffer, msg, size);
  655. buffer+=size;
  656. }
  657. va_end(va);
  658. return buffer-_buffer;
  659. }
  660. #define POS ((size_t)(((const char *)lengths) - buffer))
  661. size_t rtosc_bundle_elements(const char *buffer, size_t len)
  662. {
  663. const uint32_t *lengths = (const uint32_t*) (buffer+16);
  664. size_t elms = 0;
  665. while(POS < len && extract_uint32((const uint8_t*)lengths)) {
  666. lengths += extract_uint32((const uint8_t*)lengths)/4+1;
  667. if(POS > len)
  668. break;
  669. ++elms;
  670. }
  671. return elms;
  672. }
  673. #undef POS
  674. const char *rtosc_bundle_fetch(const char *buffer, unsigned elm)
  675. {
  676. const uint32_t *lengths = (const uint32_t*) (buffer+16);
  677. size_t elm_pos = 0;
  678. while(elm_pos!=elm && extract_uint32((const uint8_t*)lengths)) {
  679. ++elm_pos;
  680. lengths += extract_uint32((const uint8_t*)lengths)/4+1;
  681. }
  682. return (const char*) (elm==elm_pos?lengths+1:NULL);
  683. }
  684. size_t rtosc_bundle_size(const char *buffer, unsigned elm)
  685. {
  686. const uint32_t *lengths = (const uint32_t*) (buffer+16);
  687. size_t elm_pos = 0;
  688. size_t last_len = 0;
  689. while(elm_pos!=elm && extract_uint32((const uint8_t*)lengths)) {
  690. last_len = extract_uint32((const uint8_t*)lengths);
  691. ++elm_pos, lengths+=extract_uint32((const uint8_t*)lengths)/4+1;
  692. }
  693. return last_len;
  694. }
  695. int rtosc_bundle_p(const char *msg)
  696. {
  697. return !strcmp(msg,"#bundle");
  698. }
  699. uint64_t rtosc_bundle_timetag(const char *msg)
  700. {
  701. return extract_uint64((const uint8_t*)msg+8);
  702. }