void document (char *xml) { enum ikserror err; iksparser *p; nr_tests++; if (my_x) iks_delete (my_x); p = iks_dom_new (&my_x); err = iks_parse (p, xml, 0, 1); switch (err) { case IKS_OK: break; case IKS_NOMEM: PR_TEST; puts ("Not enough memory."); exit (1); case IKS_BADXML: PR_TEST; printf ("Invalid xml at byte %ld in\n[%s]\n", iks_nr_bytes (p), xml); exit (1); case IKS_HOOK: PR_TEST; puts ("Hook."); } iks_parser_delete (p); }
void serialize_test (char *buf, int len) { unsigned long time; iks *x; iksparser *prs; char *xml; int err; prs = iks_dom_new (&x); err = iks_parse (prs, buf, len, 1); switch (err) { case IKS_OK: break; case IKS_NOMEM: exit (2); case IKS_BADXML: fprintf (stderr, "Invalid xml at byte %ld, line %ld\n", iks_nr_bytes (prs), iks_nr_lines (prs)); exit (1); case IKS_HOOK: exit (1); } iks_parser_delete (prs); t_reset (); xml = iks_string (iks_stack (x), x); time = t_elapsed (); printf ("Serialize: serializing the tree took %ld milliseconds.\n", time); iks_delete (x); }
/** * Transforms SSML into file_string format and opens file_string. * @param handle * @param path the inline SSML * @return SWITCH_STATUS_SUCCESS if opened */ static switch_status_t ssml_file_open(switch_file_handle_t *handle, const char *path) { switch_status_t status = SWITCH_STATUS_FALSE; struct ssml_context *context = switch_core_alloc(handle->memory_pool, sizeof(*context)); struct ssml_parser *parsed_data = switch_core_alloc(handle->memory_pool, sizeof(*parsed_data)); iksparser *parser = iks_sax_new(parsed_data, tag_hook, cdata_hook); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Open: %s\n", path); parsed_data->cur_node = NULL; parsed_data->files = switch_core_alloc(handle->memory_pool, sizeof(struct ssml_file) * MAX_VOICE_FILES); parsed_data->max_files = MAX_VOICE_FILES; parsed_data->num_files = 0; parsed_data->pool = handle->memory_pool; parsed_data->sample_rate = handle->samplerate; if (iks_parse(parser, path, 0, 1) == IKS_OK) { if (parsed_data->num_files) { context->files = parsed_data->files; context->num_files = parsed_data->num_files; context->index = -1; handle->private_info = context; status = next_file(handle); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "No files to play: %s\n", path); } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Parse error: %s, num_files = %i\n", path, parsed_data->num_files); } iks_parser_delete(parser); return status; }
/** * Parse the result, looking for noinput/nomatch/match * @param result the NLSML result to parse * @param uuid optional UUID for logging * @return true if successful */ enum nlsml_match_type nlsml_parse(const char *result, const char *uuid) { struct nlsml_parser parser = { 0 }; parser.uuid = uuid; if (!zstr(result)) { iksparser *p = iks_sax_new(&parser, tag_hook, cdata_hook); if (iks_parse(p, result, 0, 1) == IKS_OK) { /* check result */ if (parser.match) { return NMT_MATCH; } if (parser.nomatch) { return NMT_NOMATCH; } if (parser.noinput) { return NMT_NOINPUT; } switch_log_printf(SWITCH_CHANNEL_UUID_LOG(parser.uuid), SWITCH_LOG_INFO, "NLSML result does not have match/noinput/nomatch!\n"); } else { switch_log_printf(SWITCH_CHANNEL_UUID_LOG(parser.uuid), SWITCH_LOG_INFO, "Failed to parse NLSML!\n"); } iks_parser_delete(p); } else { switch_log_printf(SWITCH_CHANNEL_UUID_LOG(parser.uuid), SWITCH_LOG_INFO, "Missing NLSML result\n"); } return NMT_BAD_XML; }
static void test_rayo_test_srgs(void) { iks *grammar = NULL; iksparser *p = iks_dom_new(&grammar); ASSERT_EQUALS(IKS_OK, iks_parse(p, rayo_test_srgs, 0, 1)); iks_parser_delete(p); iks_delete(grammar); }
// xxx Make sure that the stream generates no callbacks during or // after destruction. void ikss_Stream_destroy(ikss_Stream *self) { if (!self) return; ikss_Stream_disconnect (self); // This is enough if all our memory is managed by the stack. We must // have the stack, as otherwise would not have "self". And if we // have "self" we also have "prs", which owns "self->s". iks_parser_delete(self->prs); }
static void test_empty_cdata(void) { iks *iq = NULL; iks *input = NULL; iksparser *p = iks_dom_new(&iq); const char *cdata; ASSERT_EQUALS(IKS_OK, iks_parse(p, empty_cdata, 0, 1)); iks_parser_delete(p); ASSERT_NOT_NULL((input = iks_find(iq, "input"))); ASSERT_NULL((cdata = iks_find_cdata(input, "grammar"))); iks_delete(iq); }
static void test_repeating_bracket(void) { iks *iq = NULL; iks *input = NULL; iksparser *p = iks_dom_new(&iq); const char *cdata; ASSERT_EQUALS(IKS_OK, iks_parse(p, repeating_bracket, 0, 1)); iks_parser_delete(p); ASSERT_NOT_NULL((input = iks_find(iq, "input"))); ASSERT_NOT_NULL((cdata = iks_find_cdata(input, "grammar"))); ASSERT_STRING_EQUALS("[1 DIGITS]>]]]]]]]]] ]] ", cdata); iks_delete(iq); }
void dom_test (char *buf, int len) { int bs, i, err; iksparser *prs; unsigned long time; iks *x; size_t allocated, used; bs = block_size; if (0 == bs) bs = len; t_reset (); prs = iks_dom_new (&x); iks_set_size_hint (prs, len); i = 0; while (i < len) { if (i + bs > len) bs = len - i; err = iks_parse (prs, buf + i, bs, 0); switch (err) { case IKS_OK: break; case IKS_NOMEM: exit (2); case IKS_BADXML: fprintf (stderr, "Invalid xml at byte %ld, line %ld\n", iks_nr_bytes (prs), iks_nr_lines (prs)); exit (1); case IKS_HOOK: exit (1); } i += bs; } time = t_elapsed (); iks_stack_stat (iks_stack (x), &allocated, &used); printf ("DOM: parsing and building the tree took %ld milliseconds.\n", time); printf ("DOM: ikstack: %d bytes allocated, %d bytes used.\n", allocated, used); t_reset (); iks_delete (x); time = t_elapsed (); printf ("DOM: deleting the tree took %ld milliseconds.\n", time); iks_parser_delete (prs); }
iks * zip_load_xml (zip *z, const char *name, int *err) { iksparser *prs; char *real_buf; iks *x; struct zipfile *zfile; *err = ZIP_OK; zfile = find_file (z, name); if (!zfile) { *err = ZIP_NOFILE; return NULL; } seek_file (z, zfile); real_buf = malloc (zfile->real_size + 1); if (zfile->zip_size < zfile->real_size) { char *zip_buf; z_stream zs; zs.zalloc = NULL; zs.zfree = NULL; zs.opaque = NULL; zip_buf = malloc (zfile->zip_size); fread (zip_buf, zfile->zip_size, 1, z->f); zs.next_in = zip_buf; zs.avail_in = zfile->zip_size; zs.next_out = real_buf; zs.avail_out = zfile->real_size; inflateInit2 (&zs, -MAX_WBITS); inflate (&zs, Z_FINISH); inflateEnd (&zs); free (zip_buf); } else { fread (real_buf, zfile->real_size, 1, z->f); } real_buf[zfile->real_size] = '\0'; prs = iks_dom_new (&x); iks_parse (prs, real_buf, zfile->real_size, 1); iks_parser_delete (prs); free (real_buf); return x; }
/** * Makes NLSML result to conform to mrcpv2 * @param result the potentially non-conforming result * @return the conforming result */ iks *nlsml_normalize(const char *result) { iks *result_xml = NULL; iksparser *p = iks_dom_new(&result_xml); if (iks_parse(p, result, 0, 1) == IKS_OK && result_xml) { /* for now, all that is needed is to set the proper namespace */ iks_insert_attrib(result_xml, "xmlns", NLSML_NS); } else { /* unexpected ... */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Failed to normalize NLSML result: %s\n", result); if (result_xml) { iks_delete(result_xml); } } iks_parser_delete(p); return result_xml; }
/** * Send rayo complete */ void rayo_component_send_complete_with_metadata_string(struct rayo_component *component, const char *reason, const char *reason_namespace, const char *meta, int child_of_complete) { iks *meta_xml = NULL; iksparser *p = iks_dom_new(&meta_xml); if (iks_parse(p, meta, 0, 1) != IKS_OK) { /* unexpected ... */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "%s Failed to parse metadata for complete event: %s\n", RAYO_JID(component), meta); /* send without... */ rayo_component_send_complete(component, reason, reason_namespace); } else { rayo_component_send_complete_with_metadata(component, reason, reason_namespace, meta_xml, child_of_complete); } if (meta_xml) { iks_delete(meta_xml); } iks_parser_delete(p); }
CFStouXmppGwConn* CFStouXmppGwNewConn(CFStouXmppGw* gw, CFStouSvrConn* stouConn) { CFStouXmppGwConn *gwConn; LCF_DBG_IN_FUNC(); gwConn = malloc(sizeof(CFStouXmppGwConn)); if (!gwConn) { LCF_ERR_OUT(ERR_OUT, "out of memory\n"); } memset(gwConn, 0, sizeof(CFStouXmppGwConn)); if (XCInit(&gwConn->xc, NULL, NULL, NULL, gw->xmppSvrIp, NULL, gw->xmppSvrPort, gw->evts, 256, _XCCb, gwConn)) { LCF_ERR_OUT(ERR_FREE_CONN, "XCInit() failed\n"); } LCF_DBG_N(); gwConn->gw = gw; gwConn->stouConn = stouConn; CFStouSvrConnSetUserData(stouConn, gwConn); LCF_DBG_N(); CFStouSvrConnSetOnData(gwConn->stouConn, _StouSvrConnOnData); CFStouSvrConnSetOnDisconnected(gwConn->stouConn, __StouSvrConnOnDisconnected); if (!(gwConn->sscParser = iks_stream_new("StouXmppStouSvrConnParser", gwConn, _StouSvrConnStreamHook))) { LCF_ERR_OUT(ERR_EXIT_XC, "\n"); } CFListInit(&gwConn->list); CFListAppend(&gw->conns, &gwConn->list); CFStouSvrConnMonitorReadable(gwConn->stouConn, TRUE); gwConn->stat = CF_STOU_XMPP_GW_CONN_ST_SCC_CONNECTED; return gwConn; ERR_FREE_SSC_PARSER: iks_parser_delete(gwConn->sscParser); ERR_EXIT_XC: XCExit(&gwConn->xc); ERR_FREE_CONN: free(gwConn); ERR_OUT: return NULL; }
void sax_test (char *buf, int len) { unsigned long time; iksparser *prs; int bs, i, err; bs = block_size; if (0 == bs) bs = len; sax_tag = 0; sax_cdata = 0; t_reset (); prs = iks_sax_new (NULL, tagHook, cdataHook); i = 0; while (i < len) { if (i + bs > len) bs = len - i; err = iks_parse (prs, buf + i, bs, 0); switch (err) { case IKS_OK: break; case IKS_NOMEM: exit (2); case IKS_BADXML: fprintf (stderr, "Invalid xml at byte %ld, line %ld\n", iks_nr_bytes (prs), iks_nr_lines (prs)); exit (1); case IKS_HOOK: exit (1); } i += bs; } time = t_elapsed (); printf ("SAX: parsing took %ld milliseconds.\n", time); printf ("SAX: tag hook called %d, cdata hook called %d times.\n", sax_tag, sax_cdata); iks_parser_delete (prs); }
int CFStouXmppGwDestroyConn(CFStouXmppGw* gw, CFStouXmppGwConn* gwConn) { LCF_DBG_IN_FUNC(); switch(gwConn->stat) { case CF_STOU_XMPP_GW_CONN_ST_TUNNEL_CREATED: iks_filter_remove_hook(XCGetFilter(&gwConn->xc), _XcFilterIqCtlHook); case CF_STOU_XMPP_GW_CONN_ST_XC_STARTING: XCStop(&gwConn->xc); case CF_STOU_XMPP_GW_CONN_ST_AUTHING: if (gwConn->sid) { free(gwConn->sid); } if (gwConn->xmppClientJid) { CFJidFree(gwConn->xmppClientJid); } if (gwConn->xmppSvrJid) { CFJidFree(gwConn->xmppSvrJid); } case CF_STOU_XMPP_GW_CONN_ST_SCC_CONNECTED: CFListDel(&gwConn->list); CFListExit(&gwConn->list); iks_parser_delete(gwConn->sscParser); CFStouSvrConnSend(gwConn->stouConn, (uint8_t*)CF_CONST_STR_LEN("</stream:stream>")); XCExit(&gwConn->xc); /* todo: should be called at XCCb(XC_CB_DISCONN) */ if (!gwConn->disconned) { LCF_DBG_N(); CFStouSvrConnDisconnect(gwConn->stouConn); gwConn->stat = CF_STOU_XMPP_GW_CONN_ST_NONE; break; } case CF_STOU_XMPP_GW_CONN_ST_NONE: LCF_DBG_N(); free(gwConn); break; default: LCF_ERR("unkown stat, bug!!\n"); } return 0; }
int main(int argc, char *argv[]) { iksid *myjabberid = NULL; char *jabberid = NULL; char *resource = NULL; int state,args; int port = 0; if(argc > 2) { for (args=1; args < argc; args++) { if(!strncmp(argv[args], "-u", sizeof(argv[args]))) { jabberid = argv[args+1]; #ifdef DEBUG puts((char *) jabberid); #endif } else { if(!strncmp(argv[args], "-p", sizeof(argv[args]))) { port = atoi(argv[args+1]); if(port == 0) if(error("can't bind to this port")) return 1; #ifdef DEBUG printf("port: %i\n", port); #endif } if(!strncmp(argv[args],"-r",sizeof(argv[args]))){ resource = argv[args+1]; #ifdef DEBUG printf("resource: %s\n",resource); #endif } } } } /* * initialising the struct net of the type netdata */ struct netdata net; /* * create a new SAX parser */ net.parser = iks_sax_new(NULL, tagHook, cdataHook); /* * check if the parser is valid */ if(!net.parser) error("f**k off damn bastard."); /* * create a new stream on the parser */ net.parser = iks_stream_new(IKS_NS_CLIENT, &net, (iksStreamHook *) tagHook); #ifdef DEBUG if(net.parser) puts("net.parser initiated."); #endif /* * copy the new jabberid to the net struct */ net.id = create_id(jabberid, &net); /* * just a boring message.. ;-) */ printf("Connecting to '%s'...", net.id->server); /* * try to connect to the remote server */ if(port == 0) state = iks_connect_tcp(net.parser, net.id->server, IKS_JABBER_PORT); else state = iks_connect_tcp(net.parser, net.id->server, port); /* * check wether the connection is established or not. */ switch (state) { case IKS_OK: /* * everything is OK! ;-) */ puts("OK"); break; case IKS_NET_NODNS: /* * hostname could not be resolved */ if(error("hostname lookup failed") == 1) return 1; case IKS_NET_NOSOCK: /* * socket descriptor cannot be created */ if(error("socket cannot be created") == 1) return 1; case IKS_NET_NOCONN: /* * the connection cannot be established.. * this could have several sources. */ if(error("connection failed") == 1) return 1; default: /* * everything else. */ if(error("io error") == 1) return 1; } /* * setting the resource */ if(!net.id->resource) net.id->resource = RESOURCE; #ifdef DEBUG printf ("username: %s\nserver: %s\nresource: %s\npartial: %s\nfull: %s\n", net.id->user, net.id->server, net.id->resource, net.id->partial, net.id->full); #endif /* * disconnect the parser. */ iks_disconnect(net.parser); /* * delete the parser.. */ iks_parser_delete(net.parser); return 0; }
void jabber_disconnect (void) { iks_parser_delete (j_parser); }