int32_t osc_message_s_renameCopy(char *dest, t_osc_msg_s *src, int32_t new_address_len, char *new_address) { if(!dest){ return 0; } if(osc_error_validateAddress(new_address)){ return 0; } int32_t oldlen = osc_message_s_getSize(src); //int32_t old_address_len = strlen(osc_message_s_getAddress(src)); //int32_t newlen = oldlen - (old_address_len - new_address_len); //while(newlen % 4){ //newlen++; //} int32_t newlen = oldlen - osc_util_getPaddedStringLen(osc_message_s_getAddress(src)) + osc_util_getPaddingForNBytes(new_address_len); *((int32_t *)dest) = hton32(newlen); char *ptr = dest + 4; if(new_address_len > 0){ memcpy(ptr, new_address, new_address_len); } ptr += new_address_len; *ptr++ = '\0'; while((ptr - dest) % 4){ *ptr++ = '\0'; } memcpy(ptr, src->typetags, oldlen - (src->typetags - src->address)); return newlen; }
//void omiterate_fullPacket(t_omiterate *x, long len, long ptr) void omiterate_fullPacket(t_omiterate *x, t_symbol *msg, int argc, t_atom *argv) { OMAX_UTIL_GET_LEN_AND_PTR; t_osc_bndl_it_s *it = osc_bndl_it_s_get(len, ptr); while(osc_bndl_it_s_hasNext(it)){ t_osc_msg_s *m = osc_bndl_it_s_next(it); long l = osc_message_s_getSize(m); char *p = osc_message_s_getAddress(m); omax_util_outletOSC(x->outlet, l, p); } osc_bndl_it_s_destroy(it); }
t_osc_msg_u *osc_message_s_deserialize(t_osc_msg_s *msg) { t_osc_msg_u *m = osc_message_u_alloc(); osc_message_u_setAddress(m, osc_message_s_getAddress(msg)); t_osc_msg_it_s *it = osc_msg_it_s_get(msg); while(osc_msg_it_s_hasNext(it)){ t_osc_atom_s *a = osc_msg_it_s_next(it); t_osc_atom_u *ua = osc_atom_s_deserialize(a); osc_message_u_appendAtom(m, ua); } osc_msg_it_s_destroy(it); return m; }
void ocoll_fullPacket_impl(t_ocoll *x, long len, char *ptr) { osc_bundle_s_wrap_naked_message(len, ptr); if(len == OSC_HEADER_SIZE){ // empty bundle return; } critical_enter(x->lock); if(x->buffer_pos + len > x->buffer_len){ char *tmp = (char *)osc_mem_resize(x->buffer, x->buffer_pos + len); if(!tmp){ object_error((t_object *)x, "Out of memory...sayonara max..."); critical_exit(x->lock); return; } x->buffer = tmp; memset(x->buffer + x->buffer_pos, '\0', len); x->buffer_len = x->buffer_pos + len; } t_osc_bndl_it_s *it = osc_bndl_it_s_get(len, ptr); while(osc_bndl_it_s_hasNext(it)){ t_osc_msg_s *m = osc_bndl_it_s_next(it); t_osc_msg_ar_s *match = osc_bundle_s_lookupAddress(x->buffer_pos, x->buffer, osc_message_s_getAddress(m), 1); if(!match){ long l = osc_message_s_getSize(m) + 4; memcpy(x->buffer + x->buffer_pos, osc_message_s_getPtr(m), l); x->buffer_pos += l; }else{ // this function can resize its buffer, but we don't have to worry about that // since we already resized it above to accommidate the entire bundle int i; for(i = 0; i < osc_message_array_s_getLen(match); i++){ t_osc_msg_s *mm = osc_message_array_s_get(match, i); osc_bundle_s_replaceMessage(&(x->buffer_len), &(x->buffer_pos), &(x->buffer), mm, m); } osc_message_array_s_free(match); } } osc_bndl_it_s_destroy(it); critical_exit(x->lock); }
int main(int argc, char **argv) { // create a bundle and add messages to it t_osc_bndl_u *bndl_u = osc_bundle_u_alloc(); t_osc_msg_u *m1 = osc_message_u_alloc(); osc_message_u_setAddress(m1, "/foo"); osc_message_u_appendFloat(m1, 3.14); osc_bundle_u_addMsg(bndl_u, m1); t_osc_msg_u *m2 = osc_message_u_allocWithString("/bar", "whatevs"); osc_bundle_u_addMsg(bndl_u, m2); t_osc_msg_u *m3 = osc_message_u_allocWithAddress("/bloo"); t_osc_atom_u *a = osc_atom_u_allocWithInt32(12); osc_message_u_appendAtom(m3, a); osc_bundle_u_addMsg(bndl_u, m3); // serialize the bundle long len = osc_bundle_u_nserialize(NULL, 0, bndl_u); char bndl_s[len]; osc_bundle_u_nserialize(bndl_s, len, bndl_u); // free the original unserialized bundle osc_bundle_u_free(bndl_u); bndl_u = NULL; // deserialize the serialized bundle osc_bundle_s_deserialize(len, bndl_s, &bndl_u); // iterate over messages in a serialized bundle t_osc_bndl_it_s *b_it_s = osc_bndl_it_s_get(len, bndl_s); while(osc_bndl_it_s_hasNext(b_it_s)){ t_osc_msg_s *m = osc_bndl_it_s_next(b_it_s); printf("%s\n", osc_message_s_getAddress(m)); } osc_bndl_it_s_destroy(b_it_s); // turn a serialized bundle into printable text long tlen = osc_bundle_s_nformat(NULL, 0, len, bndl_s, 0); char text[tlen + 1]; osc_bundle_s_nformat(text, tlen, len, bndl_s, 0); printf("\nBUNDLE:\n"); printf("%s\n", text); printf("\n"); // turn text into an unserialized bundle t_osc_bndl_u *bndl_u_2 = NULL; char *text2 = "/jean : [1, 2, 3], /john : 6.66, /jeremy : \"is cool\""; osc_parser_parseString(strlen(text2), text2, &bndl_u_2); // iterate over messages in an unserialized bundle t_osc_bndl_it_u *b_it_u = osc_bndl_it_u_get(bndl_u_2); while(osc_bndl_it_u_hasNext(b_it_u)){ t_osc_msg_u *m = osc_bndl_it_u_next(b_it_u); printf("%s has typetags ", osc_message_u_getAddress(m)); // iterate over atoms in list t_osc_msg_it_u *m_it_u = osc_msg_it_u_get(m); while(osc_msg_it_u_hasNext(m_it_u)){ t_osc_atom_u *a = osc_msg_it_u_next(m_it_u); printf("%c", osc_atom_u_getTypetag(a)); } osc_msg_it_u_destroy(m_it_u); printf("\n"); } osc_bndl_it_u_destroy(b_it_u); }
void omax_FullPacketCallback(t_osc_msg_s *msg, void *context) { t_context *c = (t_context *)context; int ao, po; char *msg_address = osc_message_s_getAddress(msg); int msg_argc = osc_message_s_getArgCount(msg); char *msg_ptr = osc_message_s_getPtr(msg); int msg_size = osc_message_s_getSize(msg); //if(osc_match(msg.address, c->osc_classname->s_name, &po, &ao)){ if(1){ // the first part of the address matches our class name, so this message is meant for us // now iterate through all of our message names and see if we get a match t_hashtab *ht = omax_class_getHashtab(c->classname->s_name); if(!ht){ // oops... return; } long nkeys; t_symbol **keys = NULL; hashtab_getkeys(ht, &nkeys, &keys); int i; int matched = 0; for(i = 0; i < nkeys; i++){ int r = osc_match(msg_address, keys[i]->s_name, &po, &ao); if(r == (OSC_MATCH_ADDRESS_COMPLETE | OSC_MATCH_PATTERN_COMPLETE)){ matched++; // match // t_max_err object_method_typed(void *x, t_symbol *s, long ac, t_atom *av, t_atom *rv); t_atom rv; t_omax_method *m = NULL; long argc = 0; hashtab_lookup(ht, keys[i], (t_object **)(&m)); if(m){ switch(m->type){ case OMAX_METHOD: { t_atom argv[msg_argc + 1]; omax_util_oscMsg2MaxAtoms(msg, argv); object_method_typedfun(c->ob, (t_messlist *)(m->thing), ((t_messlist *)(m->thing))->m_sym, argc - 1, argv + 1, &rv); } break; case OMAX_PARAMETER: { t_object *attr = NULL; long get; method f = object_attr_method(c->ob, m->sym, (void **)(&attr), &get); t_atom argv[msg_argc + 1]; omax_util_oscMsg2MaxAtoms(msg, argv); f(c->ob, attr, msg_argc, argv + 1); c->should_output_state = 1; } break; } } } } if(!matched){ memcpy(c->buf + c->bufpos, msg_ptr, msg_size + 4); c->bufpos += (msg_size + 4); } }else{ memcpy(c->buf + c->bufpos, msg_ptr, msg_size + 4); c->bufpos += (msg_size + 4); } }
long osc_message_s_nformat(char *buf, long n, t_osc_msg_s *m, int nindent) { if(!m){ return 0; } long numargs = osc_message_s_getArgCount(m); long offset = 0; t_osc_msg_it_s *it = osc_msg_it_s_get(m); char tabs[nindent + 1]; for(int i = 0; i < nindent; i++){ tabs[i] = '\t'; } tabs[nindent] = '\0'; if(!buf){ if(numargs){ if(numargs > 1){ offset += snprintf(NULL, 0, "%s%s : [", tabs, osc_message_s_getAddress(m)); }else{ offset += snprintf(NULL, 0, "%s%s : ", tabs, osc_message_s_getAddress(m)); } }else{ offset += snprintf(NULL, 0, "%s%s", tabs, osc_message_s_getAddress(m)); } while(osc_msg_it_s_hasNext(it)){ //offset += snprintf(NULL, 0, " "); t_osc_atom_s *a = osc_msg_it_s_next(it); offset += osc_atom_s_nformat(NULL, 0, a, nindent); if(osc_msg_it_s_hasNext(it)){ //if(osc_atom_s_getTypetag(a) == OSC_BUNDLE_TYPETAG){ //offset += snprintf(NULL, 0, ",\n"); //}else{ offset += snprintf(NULL, 0, ", "); //} } } if(numargs > 1){ offset += snprintf(NULL, 0, "]"); } //offset += snprintf(NULL, 0, "\n"); }else{ if(numargs){ if(numargs > 1){ offset += snprintf(buf + offset, n - offset, "%s%s : [", tabs, osc_message_s_getAddress(m)); }else{ offset += snprintf(buf + offset, n - offset, "%s%s : ", tabs, osc_message_s_getAddress(m)); } }else{ offset += snprintf(buf + offset, n - offset, "%s%s", tabs, osc_message_s_getAddress(m)); } while(osc_msg_it_s_hasNext(it)){ //offset += snprintf(NULL, 0, " "); t_osc_atom_s *a = osc_msg_it_s_next(it); offset += osc_atom_s_nformat(buf + offset, n - offset, a, nindent); if(osc_msg_it_s_hasNext(it)){ //if(osc_atom_s_getTypetag(a) == OSC_BUNDLE_TYPETAG){ //offset += snprintf(buf + offset, n - offset, ",\n"); //}else{ offset += snprintf(buf + offset, n - offset, ", "); //} } } if(numargs > 1){ offset += snprintf(buf + offset, n - offset, "]"); } //offset += snprintf(buf + offset, n - offset, "\n"); } osc_msg_it_s_destroy(it); return offset; }
//void oO_fullPacket(t_oO *x, long len, long ptr) void oO_fullPacket(t_oO *x, t_symbol *msg, int argc, t_atom *argv) { char bracket[] = {'[', ']', '{', '}'}; char wild[] = {'*', '?'}; char alphanum[62]; for(int i = 0; i < 10; i++){ alphanum[i] = i + 48; } for(int i = 0; i < 26; i++){ alphanum[i + 10] = i + 65; } for(int i = 0; i < 26; i++){ alphanum[i + 36] = i + 97; } OMAX_UTIL_GET_LEN_AND_PTR; char copy[len]; memcpy(copy, ptr, len); int n = 0; osc_bundle_s_getMsgCount(len, copy, &n); long r = floor(((double)random() / 2147483647.) * n); t_osc_bndl_it_s *it = osc_bndl_it_s_get(len, copy); int i = 0; t_osc_msg_s *m = NULL; while(i <= r && osc_bndl_it_s_hasNext(it)){ i++; m = osc_bndl_it_s_next(it); } osc_bndl_it_s_destroy(it); if(m){ char *p = osc_message_s_getAddress(m); if(p){ int n = osc_message_s_getSize(m); long r = floor(((double)random() / 2147483647.) * n); if(r <= strlen(p)){ // inside address section---can add a random NULL byte or f**k with wildcard chars switch(random() & 0x1){ case 0: object_post((t_object *)x, "replaced '%c' at position %d with NULL\n", p[r], r); p[r] = '\0'; break; case 1: { int isbracket = 0; for(int i = 0; i < sizeof(bracket); i++){ if(bracket[i] == p[r]){ isbracket = 1; break; } } if(isbracket){ // we have a bracket, replace it with an alphanum char or NULL switch(random() & 0x1){ case 0: { long rrr = (long)floor(((double)random() / 2147483647.) * sizeof(alphanum)); object_post((t_object *)x, "replaced '%c' at position %d with '%c'\n", p[r], r, alphanum[rrr]); p[r] = alphanum[rrr]; } break; case 1: object_post((t_object *)x, "replaced '%c' at position %d with NULL\n", p[r], r); p[r] = NULL; break; } }else{ // not a bracket, replace it with one or NULL switch(random() & 0x1){ case 0: { long rrr = (long)floor(((double)random() / 2147483647.) * sizeof(bracket)); object_post((t_object *)x, "replaced '%c' at position %d with '%c'\n", p[r], r, bracket[rrr]); p[r] = bracket[rrr]; } break; case 1: object_post((t_object *)x, "replaced '%c' at position %d with NULL\n", p[r], r); p[r] = NULL; break; } } } break; } }else if(r < (osc_message_s_getData(m) - p)){ // typetag section---add a NULL or char if(p[r] == NULL){ long rrr = (long)floor(((double)random() / 2147483647.) * sizeof(alphanum)); object_post((t_object *)x, "replaced NULL at position %d with '%c'\n", r, alphanum[rrr]); p[r] = alphanum[rrr]; }else{ object_post((t_object *)x, "replaced '%c' at position %d with NULL\n", p[r], r); p[r] = NULL; } }else{ // not much we can do here unles we know it's a string or something else with NULL padding, // so just try again oO_fullPacket(x, msg, argc, argv); return; } } } omax_util_outletOSC(x->outlet, len, copy); }