void sort_str(const char *str) { char ar[100][100]; int index = 0; int word = 0; int start = 0; int end = 0; while (str[end] != '\0') { if (my_isspace(str[end])) { if (word == 1) { word = 0; str_ncpy(ar[index], str, start, end - 1); index++; } } else if (word == 0){ word = 1; start = end; } end++; } if (!my_isspace(--end)) { str_ncpy(ar[index++], str, start, end); } sort_word(ar, index); print_word(ar, index); }
void flow_mediaflow_estab(struct flow *flow, const char *crypto, const char *codec, const char *ltype, const char *rtype, const struct sa *sa) { uint64_t now = tmr_jiffies(); struct call *call = flow->call; info("flowmgr(%p): call(%p): flow(%p -- %s): mediaflow established " "est_st=%d crypto=%s codec=%s ice=%s-%s.%J\n", call ? call->fm : NULL, call, flow, flow->flowid, flow->est_st, crypto, codec, ltype, rtype, sa); flow->estab = true; flow->stats.sa = *sa; flow->stats.estab_time = (int)(now - flow->startts); str_ncpy(flow->stats.ltype, ltype, sizeof(flow->stats.ltype)); str_ncpy(flow->stats.rtype, rtype, sizeof(flow->stats.rtype)); str_ncpy(flow->stats.crypto, crypto, sizeof(flow->stats.crypto)); str_ncpy(flow->stats.codec, codec, sizeof(flow->stats.codec)); flow->estabts = tmr_jiffies(); flow->est_st |= FLOWMGR_ESTAB_ICE; flow_estabh(flow); }
static int cand_add(struct flow *flow, struct json_object *jcand) { const char *mid = jzon_str(jcand, "sdp_mid"); const char *rcand = jzon_str(jcand, "sdp"); int idx; int err; err = jzon_int(&idx, jcand, "sdp_mline_index"); if (err) return err; debug("cand_add: mid=%s idx=%d sdp=%s\n", mid, idx, rcand); if (flow->got_sdp) { struct mediaflow *mf = userflow_mediaflow(flow->userflow); return mediaflow_add_rcand(mf, rcand, mid, idx); } else { struct cand *cand = mem_zalloc(sizeof(*cand), cand_destructor); if (!cand) return ENOMEM; str_ncpy(cand->sdp, rcand, sizeof(cand->sdp)); str_ncpy(cand->mid, mid, sizeof(cand->mid)); cand->idx = idx; list_append(&flow->pendingl, &cand->le, cand); return 0; } }
void audio_set_devicename(struct audio *a, const char *src, const char *play) { if (!a) return; str_ncpy(a->tx.device, src, sizeof(a->tx.device)); str_ncpy(a->rx.device, play, sizeof(a->rx.device)); }
void audio_set_tx_device(struct audio *a, const char *mod, const char *dev) { if (!a) return; str_ncpy(a->tx.mod, mod, sizeof(a->tx.mod)); str_ncpy(a->tx.device, dev, sizeof(a->tx.device)); }
static bool ini_parseKey(Ini* ini, char* line) { int i = 0; char key[256]; char value[1024]; int strLen = str_len(line); bool keyFound = false; Section* section; key[0] = '\0'; value[0] = '\0'; for (i = 0; i < strLen; i++) { if (keyFound) { str_ncpy(value, (line + i), strLen - i); value[strLen - i] = '\0'; break; } if ((line[i] == '=') && keyFound == false) { keyFound = true; str_ncpy(key, line, i); key[i] = '\0'; continue; } } str_trim(key); str_trim(value); if (str_len(key) == 0) { logWarn("No key in line '%s'", line); return false; } if (str_len(value) == 0) { logWarn("No value in line '%s'", line); return false; } if (ini->sections->size == 0) { logWarn("There is no section to add the key"); return false; } // Get the last section added section = (Section*)list_get(ini->sections, (ini->sections->size - 1)); return ini_addKey(ini, section, key, value); }
/** * Set the name of the ICE Media object, used for debugging * * @param icem ICE Media object * @param name Media name */ void icem_set_name(struct icem *icem, const char *name) { if (!icem) return; str_ncpy(icem->name, name, sizeof(icem->name)); }
bool ini_addSection(Ini* ini, const char* sectionName) { if (ini == NULL) { logWarn("Invalid ini object"); return false; } if (sectionName == NULL) { logWarn("Section name is NULL"); return false; } if (ini_get(ini, sectionName) != NULL) { logWarn("Section '%s' already exist", sectionName); return false; } Section* section = memory_new(Section, true); str_ncpy(section->name, sectionName, sizeof(section->name)); section->keys = list_new(true); section->values = list_new(true); list_add(ini->sections, (Any)section); return true; }
void flow_delete(struct call *call, const char *flowid, struct rr_resp *rr, int err) { char path[256]; char reason[64] = "released"; if (!call || !flowid) return; info("flowmgr(%p): call(%p): flow(%s): delete: err=%d\n", call->fm, call, flowid, err); switch(err) { case ETIMEDOUT: str_ncpy(reason, "timeout", sizeof(reason)); break; default: break; } snprintf(path, sizeof(path), CREQ_FLOWS"/%s?reason=%s", call->convid, flowid, reason); flowmgr_send_request(call->fm, call, rr, path, HTTP_DELETE, NULL, NULL); }
/** * Get the path to configuration files * * @param path Buffer to write path * @param sz Size of path buffer * * @return 0 if success, otherwise errorcode */ int conf_path_get(char *path, size_t sz) { char buf[FS_PATH_MAX]; int err; /* Use explicit conf path */ if (conf_path) { if (re_snprintf(path, sz, "%s", conf_path) < 0) return ENOMEM; return 0; } #ifdef CONFIG_PATH str_ncpy(buf, CONFIG_PATH, sizeof(buf)); (void)err; #else err = fs_gethome(buf, sizeof(buf)); if (err) return err; #endif if (re_snprintf(path, sz, "%s" DIR_SEP ".baresip", buf) < 0) return ENOMEM; return 0; }
void expression(ecc_context *ctx, const char *token) { char previous_token[TOKEN_MAX]; if (token[0] == '-') { /* okay, we have a "-(foo)" situation */ /* let's just slip-stream a zero. */ ctx->output_term(ctx, 0); } else { term(ctx, token); } while (1) { token = ctx->lex_look_ahead(ctx); if (!is_add_precedence_op(token)) { break; } str_ncpy(previous_token, token, TOKEN_MAX); ctx->lex_advance(ctx, 1); token = ctx->lex_look_ahead(ctx); term(ctx, token); switch (previous_token[0]) { case '+': ctx->output_add(ctx); break; case '-': ctx->output_subtract(ctx); } } }
void term(ecc_context *ctx, const char *token) { char previous_token[TOKEN_MAX]; factor(ctx, token); while (1) { token = ctx->lex_look_ahead(ctx); if (!is_multiply_precedence_op(token)) { break; } str_ncpy(previous_token, token, TOKEN_MAX); ctx->lex_advance(ctx, 1); token = ctx->lex_look_ahead(ctx); factor(ctx, token); switch (previous_token[0]) { case '*': ctx->output_multiply(ctx); break; case '/': ctx->output_divide(ctx); } } }
int pf_create(struct pf **pfp, struct udp_sock *us, const char *name) { struct pf *pf; int err; if (!pfp || !us) return EINVAL; pf = mem_zalloc(sizeof(*pf), pf_destructor); if (!pf) return ENOMEM; pf->us = mem_ref(us); str_ncpy(pf->name, name, sizeof(pf->name)); err = udp_register_helper(&pf->uh, us, -1000, /* very low layer */ pf_send_handler, pf_recv_handler, pf); if (err) mem_deref(pf); else *pfp = pf; return err; }
void installer_init(struct installer *inst, char *appname) { assert(inst != NULL && appname != NULL); DEBUG(LOG_VERBOSE, "Try to install %s\n", appname); str_ncpy(inst->pname, appname, sizeof(inst->pname)); }
int dns_dname_decode(struct mbuf *mb, char **name, size_t start) { uint32_t i = 0, loopc = 0; bool comp = false; size_t pos = 0; char buf[256]; if (!mb || !name) return EINVAL; while (mb->pos < mb->end) { uint8_t len = mb->buf[mb->pos++]; if (!len) { if (comp) mb->pos = pos; buf[i++] = '\0'; *name = mem_alloc(i, NULL); if (!*name) return ENOMEM; str_ncpy(*name, buf, i); return 0; } else if ((len & COMP_MASK) == COMP_MASK) { uint16_t offset; if (loopc++ > COMP_LOOP) break; --mb->pos; offset = ntohs(mbuf_read_u16(mb)) & OFFSET_MASK; if (!comp) { pos = mb->pos; comp = true; } mb->pos = offset + start; continue; } else if (len > mbuf_get_left(mb)) break; else if (len > sizeof(buf) - i - 2) break; if (i > 0) buf[i++] = '.'; while (len--) buf[i++] = mb->buf[mb->pos++]; } return EINVAL; }
void opus_mirror_params(const char *x) { if (!opus_mirror) return; info("opus: mirror parameters: \"%s\"\n", x); str_ncpy(fmtp_mirror, x, sizeof(fmtp_mirror)); }
int callc get_initialization_info(unsigned long inforreq, void* outinfo, unsigned long* outsize) { if(inforreq == fennec_information_basic) { str_ncpy(outinfo, info_base, min(*outsize + 1, str_size(info_base) + sizeof(letter))); *outsize = (unsigned long)str_size(info_base); return 1; } return 0; }
static int device_alloc(struct device **devicep, struct list *lst, const char *name) { struct device *device; CBoxResult rc; char tmp[256], *dir; int err = 0; device = (struct device *)mem_zalloc(sizeof(*device), device_destructor); if (!device) return ENOMEM; str_ncpy(device->name, name, sizeof(device->name)); re_snprintf(tmp, sizeof(tmp), "/tmp/ztest_cbox_%s_XXXXXX", name); dir = mkdtemp(tmp); assert(dir != NULL); str_ncpy(device->path, dir, sizeof(device->path)); rc = cbox_file_open(dir, &device->box); assert(rc == CBOX_SUCCESS); assert(device->box != NULL); rc = cbox_new_prekey(device->box, 1, &device->prekey); assert(rc == CBOX_SUCCESS); list_append(lst, &device->le, device); #if 0 re_printf("[ %s ] new device with prekey %zu bytes (%s)\n", name, cbox_vec_len(device->prekey), device->path); #endif out: if (err) mem_deref(device); else if (devicep) *devicep = device; return err; }
/* * Append module extension, if not exist * * input: foobar * output: foobar.so * */ static void append_extension(char *buf, size_t sz, const char *name) { if (0 == re_regex(name, str_len(name), "[^.]+"MOD_EXT, NULL)) { str_ncpy(buf, name, sz); } else { re_snprintf(buf, sz, "%s"MOD_EXT, name); } }
int callc encoder_get_initialization_info(unsigned long inforreq, void* outinfo, unsigned long* outsize) { if(inforreq == fennec_information_basic) { str_ncpy(outinfo, encoder_info_base, min(*outsize + 1, str_len(encoder_info_base) + 1)); *outsize = (unsigned long)str_len(encoder_info_base); return 1; } return 0; }
int trice_add_lcandidate(struct ice_lcand **candp, struct trice *icem, struct list *lst, unsigned compid, char *foundation, int proto, uint32_t prio, const struct sa *addr, const struct sa *base_addr, enum ice_cand_type type, const struct sa *rel_addr, enum ice_tcptype tcptype) { struct ice_lcand *cand; int err = 0; if (!lst || !compid || !proto || !addr) return EINVAL; cand = mem_zalloc(sizeof(*cand), lcand_destructor); if (!cand) return ENOMEM; cand->attr.compid = compid; if (foundation) str_ncpy(cand->attr.foundation, foundation, sizeof(cand->attr.foundation)); else err = compute_foundation(cand, addr, type); cand->attr.proto = proto; cand->attr.prio = prio; cand->attr.addr = *addr; cand->attr.type = type; cand->attr.tcptype = tcptype; if (rel_addr) cand->attr.rel_addr = *rel_addr; if (err) goto out; cand->icem = icem; cand->recvh = trice_lcand_recv_handler; cand->arg = icem; if (base_addr) cand->base_addr = *base_addr; list_append(lst, &cand->le, cand); out: if (err) mem_deref(cand); else if (candp) *candp = cand; return err; }
void _cpu_info(void) { unsigned int *istr; int oldbrand; struct cpuid_result cpuid_r; mem_zero(&cpu_i, sizeof(struct cpu_info)); _cpuid(CPUID_BASIC ,&cpuid_r); istr = (unsigned int *) cpu_i.vendor_string; istr[0] = cpuid_r.r_ebx; istr[1] = cpuid_r.r_edx; istr[2] = cpuid_r.r_ecx; _cpuid(CPUID_FEATURE ,&cpuid_r); cpu_i.version_information = cpuid_r.r_eax; oldbrand = cpuid_r.r_ebx & 0x8; cpu_i.feature_ecx = cpuid_r.r_ecx; cpu_i.feature_edx = cpuid_r.r_edx; _cpuid(CPUID_EXT_MAX, &cpuid_r); if (cpuid_r.r_eax >= CPUID_EXT_BRAND3) { int i = 0; istr = (unsigned int *) cpu_i.brand_string; _cpuid(CPUID_EXT_BRAND1, &cpuid_r); istr[i++] = cpuid_r.r_eax; istr[i++] = cpuid_r.r_ebx; istr[i++] = cpuid_r.r_ecx; istr[i++] = cpuid_r.r_edx; _cpuid(CPUID_EXT_BRAND2, &cpuid_r); istr[i++] = cpuid_r.r_eax; istr[i++] = cpuid_r.r_ebx; istr[i++] = cpuid_r.r_ecx; istr[i++] = cpuid_r.r_edx; _cpuid(CPUID_EXT_BRAND3, &cpuid_r); istr[i++] = cpuid_r.r_eax; istr[i++] = cpuid_r.r_ebx; istr[i++] = cpuid_r.r_ecx; istr[i++] = cpuid_r.r_edx; } else { if (oldbrand == 0 || oldbrand > 0x18) { snprintf(cpu_i.brand_string, 54, "brand id %u", oldbrand); } else { str_ncpy(cpu_i.brand_string, cpu_old_brands[oldbrand], 54); } } }
static int winwave_get_dev_name(unsigned int i, char name[32]) { WAVEINCAPS wic; int err = 0; if (waveInGetDevCaps(i, &wic, sizeof(WAVEINCAPS)) == MMSYSERR_NOERROR) { str_ncpy(name, wic.szPname, 32); } else { err = ENODEV; } return err; }
static int add_candidate(struct agent *ag, const struct sa *addr, int proto, enum ice_tcptype tcptype, const char *ifname) { struct ice_lcand *lcand; uint32_t prio; int err; prio = calc_prio(ICE_CAND_TYPE_HOST, proto, tcptype, sa_af(addr), proto); err = trice_lcand_add(&lcand, ag->icem, COMPID, proto, prio, addr, NULL, ICE_CAND_TYPE_HOST, NULL, tcptype, NULL, LAYER_ICE); if (err) { re_fprintf(stderr, "failed to add local candidate (%m)\n", err); return err; } str_ncpy(lcand->ifname, ifname, sizeof(lcand->ifname)); err = control_send_message(ag->cli, "a=candidate:%H\r\n", ice_cand_attr_encode, &lcand->attr); if (err) return err; /* gather SRFLX candidates */ if (ag->cli->param.stun_server) { if (proto == IPPROTO_UDP || (proto == IPPROTO_TCP && tcptype != ICE_TCP_ACTIVE)) { gather_srflx(ag, lcand, proto); } } /* gather RELAY candidates, NOTE IPv4 only */ if (ag->cli->param.turn_server) { if (proto == IPPROTO_UDP && sa_af(addr)==AF_INET) { gather_relay(ag, lcand, IPPROTO_UDP); gather_relay(ag, lcand, IPPROTO_TCP); } } return err; }
static bool if_getname_handler(const char *ifname, const struct sa *sa, void *arg) { struct ifentry *ife = arg; if (ife->af != sa_af(sa)) return false; if (0 == sa_cmp(sa, ife->ip, SA_ADDR)) { str_ncpy(ife->ifname, ifname, ife->sz); ife->found = true; return true; } return false; }
int device_connect(struct device **devp, const char *device, struct auplay_st *auplay, struct ausrc_st *ausrc) { struct device *dev; int err = 0; if (!devp) return EINVAL; if (!str_isset(device)) return ENODEV; dev = find_device(device); if (dev) { *devp = mem_ref(dev); } else { dev = mem_zalloc(sizeof(*dev), destructor); if (!dev) return ENOMEM; str_ncpy(dev->name, device, sizeof(dev->name)); hash_append(ht_device, hash_joaat_str(device), &dev->le, dev); *devp = dev; debug("aubridge: created device '%s'\n", device); } if (auplay) dev->auplay = auplay; if (ausrc) dev->ausrc = ausrc; /* wait until we have both SRC+PLAY */ if (dev->ausrc && dev->auplay && !dev->run) { dev->run = true; err = pthread_create(&dev->thread, NULL, device_thread, dev); if (err) { dev->run = false; } } return err; }
static int get_srtp_key_info(const struct dtls_flow *tc, char *name, size_t sz, struct key *client_key, struct key *server_key) { SRTP_PROTECTION_PROFILE *sel; const char *keymatexportlabel = "EXTRACTOR-dtls_srtp"; uint8_t exportedkeymat[1024], *p; int keymatexportlen; size_t kl = 128, sl = 112; sel = SSL_get_selected_srtp_profile(tc->ssl); if (!sel) return ENOENT; str_ncpy(name, sel->name, sz); kl /= 8; sl /= 8; keymatexportlen = (int)(kl + sl)*2; if (keymatexportlen != 60) { warning("dtls: expected 60 bits, but keying material is %d\n", keymatexportlen); return EINVAL; } if (!SSL_export_keying_material(tc->ssl, exportedkeymat, keymatexportlen, keymatexportlabel, strlen(keymatexportlabel), NULL, 0, 0)) { return ENOENT; } p = exportedkeymat; memcpy(client_key->key, p, kl); p += kl; memcpy(server_key->key, p, kl); p += kl; memcpy(client_key->salt, p, sl); p += sl; memcpy(server_key->salt, p, sl); p += sl; client_key->key_len = server_key->key_len = kl; client_key->salt_len = server_key->salt_len = sl; return 0; }
void test_str_ncpy() { const char *src = "foo"; char dest[80]; char *result, *expected_result; dest[60] = 'X'; check_unsigned_int(sizeof(dest), 80); check_char(dest[60], 'X'); result = str_ncpy(dest, src, sizeof(dest)); check_strs(dest, src, "1"); check_ptrs(result, dest, "2"); check_chars(dest[60], '\0', "should null-out rest of buffer"); expected_result = strncpy(dest, src, sizeof(dest)); check_strs(dest, src, "3"); check_ptrs(result, expected_result, "4"); }
void stack_name_add(names_stack *stack, const char *name) { names_list *names_lists; const char **old_names; const char **names; char *new_buffer; unsigned int size; unsigned int i; names_lists = stack->names_lists[stack->names_lists_c - 1]; old_names = names_lists->names; size = sizeof(const char *) * (names_lists->names_c + 1); names = (const char **)heap_malloc(size); if (!names) { err_msg("malloc char**\n"); die(); } for (i = 0; i < names_lists->names_c; i++) { names[i] = old_names[i]; } if (old_names) { heap_free(old_names); } size = sizeof(char) * str_nlen(name, 100) + 1; new_buffer = (char *)heap_malloc(size); if (!new_buffer) { err_msg("malloc char*\n"); die(); } names[names_lists->names_c] = new_buffer; str_ncpy(new_buffer, name, size); names_lists->names = names; names_lists->names_c++; }
static int src_alloc(struct vidsrc_st **stp, const struct vidsrc *vs, struct media_ctx **ctx, struct vidsrc_prm *prm, const struct vidsz *size, const char *fmt, const char *dev, vidsrc_frame_h *frameh, vidsrc_error_h *errorh, void *arg) { struct vidsrc_st *st; int err = 0; (void)ctx; (void)prm; (void)fmt; (void)errorh; (void)arg; if (!stp || !size || !frameh) return EINVAL; st = mem_zalloc(sizeof(*st), src_destructor); if (!st) return ENOMEM; st->vs = vs; /* NOTE: copy instance data into global space */ if (str_isset(dev)) str_ncpy(v4l2.device, dev, sizeof(v4l2.device)); v4l2.width = size->w; v4l2.height = size->h; if (err) mem_deref(st); else *stp = st; return err; }