2
0

OscReceivedElements.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. oscpack -- Open Sound Control packet manipulation library
  3. http://www.audiomulch.com/~rossb/oscpack
  4. Copyright (c) 2004-2005 Ross Bencina <[email protected]>
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. Any person wishing to distribute modifications to the Software is
  15. requested to send the modifications to the original developer so that
  16. they can be incorporated into the canonical version.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  21. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "OscReceivedElements.h"
  26. #include <cassert>
  27. #include "OscHostEndianness.h"
  28. namespace osc{
  29. // return the first 4 byte boundary after the end of a str4
  30. // be careful about calling this version if you don't know whether
  31. // the string is terminated correctly.
  32. static inline const char* FindStr4End( const char *p )
  33. {
  34. if( p[0] == '\0' ) // special case for SuperCollider integer address pattern
  35. return p + 4;
  36. p += 3;
  37. while( *p )
  38. p += 4;
  39. return p + 1;
  40. }
  41. // return the first 4 byte boundary after the end of a str4
  42. // returns 0 if p == end or if the string is unterminated
  43. static inline const char* FindStr4End( const char *p, const char *end )
  44. {
  45. if( p >= end )
  46. return 0;
  47. if( p[0] == '\0' ) // special case for SuperCollider integer address pattern
  48. return p + 4;
  49. p += 3;
  50. end -= 1;
  51. while( p < end && *p )
  52. p += 4;
  53. if( *p )
  54. return 0;
  55. else
  56. return p + 1;
  57. }
  58. static inline unsigned long RoundUp4( unsigned long x )
  59. {
  60. unsigned long remainder = x & 0x3UL;
  61. if( remainder )
  62. return x + (4 - remainder);
  63. else
  64. return x;
  65. }
  66. static inline int32 ToInt32( const char *p )
  67. {
  68. #ifdef OSC_HOST_LITTLE_ENDIAN
  69. union{
  70. osc::int32 i;
  71. char c[4];
  72. } u;
  73. u.c[0] = p[3];
  74. u.c[1] = p[2];
  75. u.c[2] = p[1];
  76. u.c[3] = p[0];
  77. return u.i;
  78. #else
  79. return *(int32*)p;
  80. #endif
  81. }
  82. static inline uint32 ToUInt32( const char *p )
  83. {
  84. #ifdef OSC_HOST_LITTLE_ENDIAN
  85. union{
  86. osc::uint32 i;
  87. char c[4];
  88. } u;
  89. u.c[0] = p[3];
  90. u.c[1] = p[2];
  91. u.c[2] = p[1];
  92. u.c[3] = p[0];
  93. return u.i;
  94. #else
  95. return *(uint32*)p;
  96. #endif
  97. }
  98. int64 ToInt64( const char *p )
  99. {
  100. #ifdef OSC_HOST_LITTLE_ENDIAN
  101. union{
  102. osc::int64 i;
  103. char c[8];
  104. } u;
  105. u.c[0] = p[7];
  106. u.c[1] = p[6];
  107. u.c[2] = p[5];
  108. u.c[3] = p[4];
  109. u.c[4] = p[3];
  110. u.c[5] = p[2];
  111. u.c[6] = p[1];
  112. u.c[7] = p[0];
  113. return u.i;
  114. #else
  115. return *(int64*)p;
  116. #endif
  117. }
  118. uint64 ToUInt64( const char *p )
  119. {
  120. #ifdef OSC_HOST_LITTLE_ENDIAN
  121. union{
  122. osc::uint64 i;
  123. char c[8];
  124. } u;
  125. u.c[0] = p[7];
  126. u.c[1] = p[6];
  127. u.c[2] = p[5];
  128. u.c[3] = p[4];
  129. u.c[4] = p[3];
  130. u.c[5] = p[2];
  131. u.c[6] = p[1];
  132. u.c[7] = p[0];
  133. return u.i;
  134. #else
  135. return *(uint64*)p;
  136. #endif
  137. }
  138. //------------------------------------------------------------------------------
  139. bool ReceivedPacket::IsBundle() const
  140. {
  141. return (Size() > 0 && Contents()[0] == '#');
  142. }
  143. //------------------------------------------------------------------------------
  144. bool ReceivedBundleElement::IsBundle() const
  145. {
  146. return (Size() > 0 && Contents()[0] == '#');
  147. }
  148. int32 ReceivedBundleElement::Size() const
  149. {
  150. return ToUInt32( size_ );
  151. }
  152. //------------------------------------------------------------------------------
  153. bool ReceivedMessageArgument::AsBool() const
  154. {
  155. if( !typeTag_ )
  156. throw MissingArgumentException();
  157. else if( *typeTag_ == TRUE_TYPE_TAG )
  158. return true;
  159. else if( *typeTag_ == FALSE_TYPE_TAG )
  160. return false;
  161. else
  162. throw WrongArgumentTypeException();
  163. }
  164. bool ReceivedMessageArgument::AsBoolUnchecked() const
  165. {
  166. if( !typeTag_ )
  167. throw MissingArgumentException();
  168. else if( *typeTag_ == TRUE_TYPE_TAG )
  169. return true;
  170. else
  171. return false;
  172. }
  173. int32 ReceivedMessageArgument::AsInt32() const
  174. {
  175. if( !typeTag_ )
  176. throw MissingArgumentException();
  177. else if( *typeTag_ == INT32_TYPE_TAG )
  178. return AsInt32Unchecked();
  179. else
  180. throw WrongArgumentTypeException();
  181. }
  182. int32 ReceivedMessageArgument::AsInt32Unchecked() const
  183. {
  184. #ifdef OSC_HOST_LITTLE_ENDIAN
  185. union{
  186. osc::int32 i;
  187. char c[4];
  188. } u;
  189. u.c[0] = argument_[3];
  190. u.c[1] = argument_[2];
  191. u.c[2] = argument_[1];
  192. u.c[3] = argument_[0];
  193. return u.i;
  194. #else
  195. return *(int32*)argument_;
  196. #endif
  197. }
  198. float ReceivedMessageArgument::AsFloat() const
  199. {
  200. if( !typeTag_ )
  201. throw MissingArgumentException();
  202. else if( *typeTag_ == FLOAT_TYPE_TAG )
  203. return AsFloatUnchecked();
  204. else
  205. throw WrongArgumentTypeException();
  206. }
  207. float ReceivedMessageArgument::AsFloatUnchecked() const
  208. {
  209. #ifdef OSC_HOST_LITTLE_ENDIAN
  210. union{
  211. float f;
  212. char c[4];
  213. } u;
  214. u.c[0] = argument_[3];
  215. u.c[1] = argument_[2];
  216. u.c[2] = argument_[1];
  217. u.c[3] = argument_[0];
  218. return u.f;
  219. #else
  220. return *(float*)argument_;
  221. #endif
  222. }
  223. char ReceivedMessageArgument::AsChar() const
  224. {
  225. if( !typeTag_ )
  226. throw MissingArgumentException();
  227. else if( *typeTag_ == CHAR_TYPE_TAG )
  228. return AsCharUnchecked();
  229. else
  230. throw WrongArgumentTypeException();
  231. }
  232. char ReceivedMessageArgument::AsCharUnchecked() const
  233. {
  234. return (char)ToInt32( argument_ );
  235. }
  236. uint32 ReceivedMessageArgument::AsRgbaColor() const
  237. {
  238. if( !typeTag_ )
  239. throw MissingArgumentException();
  240. else if( *typeTag_ == RGBA_COLOR_TYPE_TAG )
  241. return AsRgbaColorUnchecked();
  242. else
  243. throw WrongArgumentTypeException();
  244. }
  245. uint32 ReceivedMessageArgument::AsRgbaColorUnchecked() const
  246. {
  247. return ToUInt32( argument_ );
  248. }
  249. uint32 ReceivedMessageArgument::AsMidiMessage() const
  250. {
  251. if( !typeTag_ )
  252. throw MissingArgumentException();
  253. else if( *typeTag_ == MIDI_MESSAGE_TYPE_TAG )
  254. return AsMidiMessageUnchecked();
  255. else
  256. throw WrongArgumentTypeException();
  257. }
  258. uint32 ReceivedMessageArgument::AsMidiMessageUnchecked() const
  259. {
  260. return ToUInt32( argument_ );
  261. }
  262. int64 ReceivedMessageArgument::AsInt64() const
  263. {
  264. if( !typeTag_ )
  265. throw MissingArgumentException();
  266. else if( *typeTag_ == INT64_TYPE_TAG )
  267. return AsInt64Unchecked();
  268. else
  269. throw WrongArgumentTypeException();
  270. }
  271. int64 ReceivedMessageArgument::AsInt64Unchecked() const
  272. {
  273. return ToInt64( argument_ );
  274. }
  275. uint64 ReceivedMessageArgument::AsTimeTag() const
  276. {
  277. if( !typeTag_ )
  278. throw MissingArgumentException();
  279. else if( *typeTag_ == TIME_TAG_TYPE_TAG )
  280. return AsTimeTagUnchecked();
  281. else
  282. throw WrongArgumentTypeException();
  283. }
  284. uint64 ReceivedMessageArgument::AsTimeTagUnchecked() const
  285. {
  286. return ToUInt64( argument_ );
  287. }
  288. double ReceivedMessageArgument::AsDouble() const
  289. {
  290. if( !typeTag_ )
  291. throw MissingArgumentException();
  292. else if( *typeTag_ == DOUBLE_TYPE_TAG )
  293. return AsDoubleUnchecked();
  294. else
  295. throw WrongArgumentTypeException();
  296. }
  297. double ReceivedMessageArgument::AsDoubleUnchecked() const
  298. {
  299. #ifdef OSC_HOST_LITTLE_ENDIAN
  300. union{
  301. double d;
  302. char c[8];
  303. } u;
  304. u.c[0] = argument_[7];
  305. u.c[1] = argument_[6];
  306. u.c[2] = argument_[5];
  307. u.c[3] = argument_[4];
  308. u.c[4] = argument_[3];
  309. u.c[5] = argument_[2];
  310. u.c[6] = argument_[1];
  311. u.c[7] = argument_[0];
  312. return u.d;
  313. #else
  314. return *(double*)argument_;
  315. #endif
  316. }
  317. const char* ReceivedMessageArgument::AsString() const
  318. {
  319. if( !typeTag_ )
  320. throw MissingArgumentException();
  321. else if( *typeTag_ == STRING_TYPE_TAG )
  322. return argument_;
  323. else
  324. throw WrongArgumentTypeException();
  325. }
  326. const char* ReceivedMessageArgument::AsSymbol() const
  327. {
  328. if( !typeTag_ )
  329. throw MissingArgumentException();
  330. else if( *typeTag_ == SYMBOL_TYPE_TAG )
  331. return argument_;
  332. else
  333. throw WrongArgumentTypeException();
  334. }
  335. void ReceivedMessageArgument::AsBlob( const void*& data, unsigned long& size ) const
  336. {
  337. if( !typeTag_ )
  338. throw MissingArgumentException();
  339. else if( *typeTag_ == BLOB_TYPE_TAG )
  340. AsBlobUnchecked( data, size );
  341. else
  342. throw WrongArgumentTypeException();
  343. }
  344. void ReceivedMessageArgument::AsBlobUnchecked( const void*& data, unsigned long& size ) const
  345. {
  346. size = ToUInt32( argument_ );
  347. data = (void*)(argument_+4);
  348. }
  349. //------------------------------------------------------------------------------
  350. void ReceivedMessageArgumentIterator::Advance()
  351. {
  352. if( !value_.typeTag_ )
  353. return;
  354. switch( *value_.typeTag_++ ){
  355. case '\0':
  356. // don't advance past end
  357. --value_.typeTag_;
  358. break;
  359. case TRUE_TYPE_TAG:
  360. case FALSE_TYPE_TAG:
  361. case NIL_TYPE_TAG:
  362. case INFINITUM_TYPE_TAG:
  363. // zero length
  364. break;
  365. case INT32_TYPE_TAG:
  366. case FLOAT_TYPE_TAG:
  367. case CHAR_TYPE_TAG:
  368. case RGBA_COLOR_TYPE_TAG:
  369. case MIDI_MESSAGE_TYPE_TAG:
  370. value_.argument_ += 4;
  371. break;
  372. case INT64_TYPE_TAG:
  373. case TIME_TAG_TYPE_TAG:
  374. case DOUBLE_TYPE_TAG:
  375. value_.argument_ += 8;
  376. break;
  377. case STRING_TYPE_TAG:
  378. case SYMBOL_TYPE_TAG:
  379. // we use the unsafe function FindStr4End(char*) here because all of
  380. // the arguments have already been validated in
  381. // ReceivedMessage::Init() below.
  382. value_.argument_ = FindStr4End( value_.argument_ );
  383. break;
  384. case BLOB_TYPE_TAG:
  385. {
  386. uint32 blobSize = ToUInt32( value_.argument_ );
  387. value_.argument_ = value_.argument_ + 4 + RoundUp4( blobSize );
  388. }
  389. break;
  390. default: // unknown type tag
  391. // don't advance
  392. --value_.typeTag_;
  393. break;
  394. // not handled:
  395. // [ Indicates the beginning of an array. The tags following are for
  396. // data in the Array until a close brace tag is reached.
  397. // ] Indicates the end of an array.
  398. }
  399. }
  400. //------------------------------------------------------------------------------
  401. ReceivedMessage::ReceivedMessage( const ReceivedPacket& packet )
  402. : addressPattern_( packet.Contents() )
  403. {
  404. Init( packet.Contents(), packet.Size() );
  405. }
  406. ReceivedMessage::ReceivedMessage( const ReceivedBundleElement& bundleElement )
  407. : addressPattern_( bundleElement.Contents() )
  408. {
  409. Init( bundleElement.Contents(), bundleElement.Size() );
  410. }
  411. bool ReceivedMessage::AddressPatternIsUInt32() const
  412. {
  413. return (addressPattern_[0] == '\0');
  414. }
  415. uint32 ReceivedMessage::AddressPatternAsUInt32() const
  416. {
  417. return ToUInt32( addressPattern_ );
  418. }
  419. void ReceivedMessage::Init( const char *message, unsigned long size )
  420. {
  421. if( size == 0 )
  422. throw MalformedMessageException( "zero length messages not permitted" );
  423. if( (size & 0x03L) != 0 )
  424. throw MalformedMessageException( "message size must be multiple of four" );
  425. const char *end = message + size;
  426. typeTagsBegin_ = FindStr4End( addressPattern_, end );
  427. if( typeTagsBegin_ == 0 ){
  428. // address pattern was not terminated before end
  429. throw MalformedMessageException( "unterminated address pattern" );
  430. }
  431. if( typeTagsBegin_ == end ){
  432. // message consists of only the address pattern - no arguments or type tags.
  433. typeTagsBegin_ = 0;
  434. typeTagsEnd_ = 0;
  435. arguments_ = 0;
  436. }else{
  437. if( *typeTagsBegin_ != ',' )
  438. throw MalformedMessageException( "type tags not present" );
  439. if( *(typeTagsBegin_ + 1) == '\0' ){
  440. // zero length type tags
  441. typeTagsBegin_ = 0;
  442. typeTagsEnd_ = 0;
  443. arguments_ = 0;
  444. }else{
  445. // check that all arguments are present and well formed
  446. arguments_ = FindStr4End( typeTagsBegin_, end );
  447. if( arguments_ == 0 ){
  448. throw MalformedMessageException( "type tags were not terminated before end of message" );
  449. }
  450. ++typeTagsBegin_; // advance past initial ','
  451. const char *typeTag = typeTagsBegin_;
  452. const char *argument = arguments_;
  453. do{
  454. switch( *typeTag ){
  455. case TRUE_TYPE_TAG:
  456. case FALSE_TYPE_TAG:
  457. case NIL_TYPE_TAG:
  458. case INFINITUM_TYPE_TAG:
  459. // zero length
  460. break;
  461. case INT32_TYPE_TAG:
  462. case FLOAT_TYPE_TAG:
  463. case CHAR_TYPE_TAG:
  464. case RGBA_COLOR_TYPE_TAG:
  465. case MIDI_MESSAGE_TYPE_TAG:
  466. if( argument == end )
  467. throw MalformedMessageException( "arguments exceed message size" );
  468. argument += 4;
  469. if( argument > end )
  470. throw MalformedMessageException( "arguments exceed message size" );
  471. break;
  472. case INT64_TYPE_TAG:
  473. case TIME_TAG_TYPE_TAG:
  474. case DOUBLE_TYPE_TAG:
  475. if( argument == end )
  476. throw MalformedMessageException( "arguments exceed message size" );
  477. argument += 8;
  478. if( argument > end )
  479. throw MalformedMessageException( "arguments exceed message size" );
  480. break;
  481. case STRING_TYPE_TAG:
  482. case SYMBOL_TYPE_TAG:
  483. if( argument == end )
  484. throw MalformedMessageException( "arguments exceed message size" );
  485. argument = FindStr4End( argument, end );
  486. if( argument == 0 )
  487. throw MalformedMessageException( "unterminated string argument" );
  488. break;
  489. case BLOB_TYPE_TAG:
  490. {
  491. if( argument + 4 > end )
  492. MalformedMessageException( "arguments exceed message size" );
  493. uint32 blobSize = ToUInt32( argument );
  494. argument = argument + 4 + RoundUp4( blobSize );
  495. if( argument > end )
  496. MalformedMessageException( "arguments exceed message size" );
  497. }
  498. break;
  499. default:
  500. throw MalformedMessageException( "unknown type tag" );
  501. // not handled:
  502. // [ Indicates the beginning of an array. The tags following are for
  503. // data in the Array until a close brace tag is reached.
  504. // ] Indicates the end of an array.
  505. }
  506. }while( *++typeTag != '\0' );
  507. typeTagsEnd_ = typeTag;
  508. }
  509. }
  510. }
  511. //------------------------------------------------------------------------------
  512. ReceivedBundle::ReceivedBundle( const ReceivedPacket& packet )
  513. : elementCount_( 0 )
  514. {
  515. Init( packet.Contents(), packet.Size() );
  516. }
  517. ReceivedBundle::ReceivedBundle( const ReceivedBundleElement& bundleElement )
  518. : elementCount_( 0 )
  519. {
  520. Init( bundleElement.Contents(), bundleElement.Size() );
  521. }
  522. void ReceivedBundle::Init( const char *bundle, unsigned long size )
  523. {
  524. if( size < 16 )
  525. throw MalformedBundleException( "packet too short for bundle" );
  526. if( (size & 0x03L) != 0 )
  527. throw MalformedBundleException( "bundle size must be multiple of four" );
  528. if( bundle[0] != '#'
  529. || bundle[1] != 'b'
  530. || bundle[2] != 'u'
  531. || bundle[3] != 'n'
  532. || bundle[4] != 'd'
  533. || bundle[5] != 'l'
  534. || bundle[6] != 'e'
  535. || bundle[7] != '\0' )
  536. throw MalformedBundleException( "bad bundle address pattern" );
  537. end_ = bundle + size;
  538. timeTag_ = bundle + 8;
  539. const char *p = timeTag_ + 8;
  540. while( p < end_ ){
  541. if( p + 4 > end_ )
  542. throw MalformedBundleException( "packet too short for elementSize" );
  543. uint32 elementSize = ToUInt32( p );
  544. if( (elementSize & 0x03L) != 0 )
  545. throw MalformedBundleException( "bundle element size must be multiple of four" );
  546. p += 4 + elementSize;
  547. if( p > end_ )
  548. throw MalformedBundleException( "packet too short for bundle element" );
  549. ++elementCount_;
  550. }
  551. if( p != end_ )
  552. throw MalformedBundleException( "bundle contents " );
  553. }
  554. uint64 ReceivedBundle::TimeTag() const
  555. {
  556. return ToUInt64( timeTag_ );
  557. }
  558. } // namespace osc