static const char * parse_windows_proxy_setting (const char *str, struct auto_proxy_info_entry *e, struct gc_arena *gc) { char buf[128]; const char *ret = NULL; struct buffer in; CLEAR (*e); buf_set_read (&in, (const uint8_t *)str, strlen (str)); if (strchr (str, '=') != NULL) { if (buf_parse (&in, '=', buf, sizeof (buf))) ret = string_alloc (buf, gc); } if (buf_parse (&in, ':', buf, sizeof (buf))) e->server = string_alloc (buf, gc); if (e->server && buf_parse (&in, '\0', buf, sizeof (buf))) e->port = atoi (buf); return ret; }
/* * See management/management-notes.txt for more info on the * the dynamic challenge/response protocol implemented here. */ struct auth_challenge_info * get_auth_challenge (const char *auth_challenge, struct gc_arena *gc) { if (auth_challenge) { struct auth_challenge_info *ac; const int len = strlen (auth_challenge); char *work = (char *) gc_malloc (len+1, false, gc); char *cp; struct buffer b; buf_set_read (&b, (const uint8_t *)auth_challenge, len); ALLOC_OBJ_CLEAR_GC (ac, struct auth_challenge_info, gc); /* parse prefix */ if (!buf_parse(&b, ':', work, len)) return NULL; if (strcmp(work, "CRV1")) return NULL; /* parse flags */ if (!buf_parse(&b, ':', work, len)) return NULL; for (cp = work; *cp != '\0'; ++cp) { const char c = *cp; if (c == 'E') ac->flags |= CR_ECHO; else if (c == 'R') ac->flags |= CR_RESPONSE; } /* parse state ID */ if (!buf_parse(&b, ':', work, len)) return NULL; ac->state_id = string_alloc(work, gc); /* parse user name */ if (!buf_parse(&b, ':', work, len)) return NULL; ac->user = (char *) gc_malloc (strlen(work)+1, true, gc); openvpn_base64_decode(work, (void*)ac->user, -1); /* parse challenge text */ ac->challenge_text = string_alloc(BSTR(&b), gc); return ac; } else return NULL; }
void argv_printf_arglist (struct argv *a, const char *format, const unsigned int flags, va_list arglist) { struct gc_arena gc = gc_new (); char *term; const char *f = format; if (!(flags & APA_CAT)) argv_reset (a); argv_extend (a, 1); /* ensure trailing NULL */ while ((term = argv_term (&f)) != NULL) { if (term[0] == '%') { if (!strcmp (term, "%s")) { char *s = va_arg (arglist, char *); if (!s) s = ""; argv_append (a, string_alloc (s, NULL)); argv_system_str_append (a, s, true); } else if (!strcmp (term, "%sc")) { char *s = va_arg (arglist, char *); if (s) { int nparms; char *parms[MAX_PARMS+1]; int i; nparms = parse_line (s, parms, MAX_PARMS, "SCRIPT-ARGV", 0, D_ARGV_PARSE_CMD, &gc); if (nparms) { for (i = 0; i < nparms; ++i) argv_append (a, string_alloc (parms[i], NULL)); } else argv_append (a, string_alloc (s, NULL)); argv_system_str_append (a, s, false); } else { argv_append (a, string_alloc ("", NULL)); argv_system_str_append (a, "echo", false); } }
char *get_host(UriUriA *uri) { char *ret_str = NULL; int i, len = 0; if (uri->hostData.ip4) return *string_printf(&ret_str, "%d.%d.%d.%d", uri->hostData.ip4->data[0], uri->hostData.ip4->data[1], uri->hostData.ip4->data[2], uri->hostData.ip4->data[3]); if (uri->hostData.ip6) { ret_str = string_alloc(40); for (i = 0; i < 16; i++) { len += sprintf(ret_str + len, "%2.2x", uri->hostData.ip6->data[i]); if (i % 2 == 1 && i < 15) ret_str[len++] = ':'; } return ret_str; } if (uri->hostData.ipFuture.first) return string_nnew(uri->hostData.ipFuture.first, uri->hostData.ipFuture.afterLast - uri->hostData.ipFuture.first); if (uri->hostText.first) return string_nnew(uri->hostText.first, uri->hostText.afterLast - uri->hostText.first); return NULL; }
static char *ms_error_text(DWORD ms_err) { LPVOID lpMsgBuf = NULL; char *rv = NULL; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, ms_err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ (LPTSTR) &lpMsgBuf, 0, NULL); if (lpMsgBuf) { char *p; rv = string_alloc(lpMsgBuf, NULL); LocalFree(lpMsgBuf); /* trim to the left */ if (rv) for (p = rv + strlen(rv) - 1; p >= rv; p--) { if (isspace(*p)) *p = '\0'; else break; } } return rv; }
static void _doc_process_var(doc_ptr doc, cptr name) { if (strcmp(name, "version") == 0) { string_ptr s = string_alloc_format("%d.%d.%d", VER_MAJOR, VER_MINOR, VER_PATCH); doc_insert(doc, string_buffer(s)); string_free(s); } else if (strlen(name) > 3 && strncmp(name, "FF_", 3) == 0) { char buf[100]; int f_idx; sprintf(buf, "%s", name + 3); f_idx = f_tag_to_index(buf); if (0 <= f_idx && f_idx < max_f_idx) { string_ptr s = string_alloc(); feature_type *feat = &f_info[f_idx]; string_printf(s, "<color:%c>%c</color>", attr_to_attr_char(feat->d_attr[F_LIT_STANDARD]), feat->d_char[F_LIT_STANDARD]); doc_insert(doc, string_buffer(s)); string_free(s); } } }
/* * GuizmOVPN_get_auto_proxy (struct gc_arena *gc) : * Detect proxy settings */ struct auto_proxy_info *GuizmOVPN_get_auto_proxy (struct gc_arena *gc) { guizmovpn_ctx.bAutoproxy=true; GuizmOVPN_tools("handle_proxy"); struct auto_proxy_info *pi; char szHTTPProxyIP[32]; char szHTTPProxyPort[32]; int32_t HTTPProxyPort=0; szHTTPProxyIP[0]='\0'; GuizmOVPN_ReadPrefs("HTTPProxy",szHTTPProxyIP); szHTTPProxyPort[0]='\0'; GuizmOVPN_ReadPrefs("HTTPPort",szHTTPProxyPort); HTTPProxyPort=atoi(szHTTPProxyPort); if(strlen(szHTTPProxyIP)>7 && HTTPProxyPort>=0) { ALLOC_OBJ_CLEAR_GC (pi, struct auto_proxy_info, gc); pi->http.server=string_alloc(szHTTPProxyIP,gc); pi->http.port = HTTPProxyPort; msg (M_INFO,"Proxy detected : %s:%d",szHTTPProxyIP,HTTPProxyPort); return pi; } else { return NULL; } }
static obj_t make_string_fn(obj_t args, Reporter rep) { size_t nargs, i, k; char ch; string str; nargs = list_length(args); if (nargs == 0 || nargs > 2) { reportf(rep, "make-string: " "length and optional fill char expected"); return unspecific; } if (!is_num(list_ref(args, 0))) { reportf(rep, "make-string: " "first argument must be a non-negative integer"); return unspecific; } if (nargs == 2 && !is_char(list_ref(args, 1))) { reportf(rep, "make-string: " "second argument must be a character"); return unspecific; } k = fetch_num(list_ref(args, 0)); ch = (nargs == 1) ? 0 : fetch_char(list_ref(args, 1)); str = string_alloc(k); for (i = 0; i < str->len; i++) str->data[i] = ch; return make_string(str); }
char * pkcs11_certificate_dn (pkcs11h_certificate_t cert, struct gc_arena *gc) { char *ret = NULL; char dn[1024] = {0}; mbedtls_x509_crt mbed_crt = {0}; if (mbedtls_pkcs11_x509_cert_bind(&mbed_crt, cert)) { msg (M_FATAL, "PKCS#11: Cannot retrieve mbed TLS certificate object"); goto cleanup; } if (-1 == mbedtls_x509_dn_gets (dn, sizeof(dn), &mbed_crt.subject)) { msg (M_FATAL, "PKCS#11: mbed TLS cannot parse subject"); goto cleanup; } ret = string_alloc(dn, gc); cleanup: mbedtls_x509_crt_free(&mbed_crt); return ret; }
static char * get_pa_var (const char *key, const char *pa, struct gc_arena *gc) { char k[64]; char v[256]; const char *content = pa; while (true) { const int status = get_key_value(content, k, v, sizeof(k), sizeof(v), &content); if (status) { if (!strcmp(key, k)) return string_alloc(v, gc); } else return NULL; /* advance to start of next key */ if (*content == ',') ++content; while (*content && isspace(*content)) ++content; } }
/** * Prepare push options, based on local options and available peer info. * * @param options Connection options * @param tls_multi TLS state structure for the current tunnel * * @return true on success, false on failure. */ static bool prepare_push_reply (struct options *o, struct tls_multi *tls_multi) { const char *optstr = NULL; const char * const peer_info = tls_multi->peer_info; /* Send peer-id if client supports it */ optstr = peer_info ? strstr(peer_info, "IV_PROTO=") : NULL; if (optstr) { int proto = 0; int r = sscanf(optstr, "IV_PROTO=%d", &proto); if ((r == 1) && (proto >= 2)) { push_option_fmt(o, M_USAGE, "peer-id %d", tls_multi->peer_id); } } /* Push cipher if client supports Negotiable Crypto Parameters */ if (tls_peer_info_ncp_ver (peer_info) >= 2 && o->ncp_enabled) { /* Push the first cipher from --ncp-ciphers to the client. * TODO: actual negotiation, instead of server dictatorship. */ char *push_cipher = string_alloc(o->ncp_ciphers, &o->gc); o->ciphername = strtok (push_cipher, ":"); push_option_fmt(o, M_USAGE, "cipher %s", o->ciphername); } return true; }
Object obj_mul(Object a, Object b) { if (a.type == b.type && a.type == TYPE_NUM) { GET_NUM(a) *= GET_NUM(b); return a; } if (a.type == TYPE_NUM && b.type == TYPE_STR) { Object temp = a; a = b; b = temp; } if (a.type == TYPE_STR && b.type == TYPE_NUM) { int len = GET_STR_LEN(a); Object des; if (len == 0) return a; int times = (int) GET_NUM(b); if (times <= 0) return sz_to_string(""); if (times == 1) return a; des = string_alloc(NULL, len * times); char* s = GET_STR(des); int i; for (i = 0; i < times; i++) { strncpy(s, GET_STR(a), len); s += len; } return des; } tm_raise("obj_mul: can not multiply %o and %o", a, b); return NONE_OBJECT; }
Object obj_add(Object a, Object b) { if (TM_TYPE(a) == TM_TYPE(b)) { switch (TM_TYPE(a)) { case TYPE_NUM: GET_NUM(a) += GET_NUM(b); return a; case TYPE_STR: { char* sa = GET_STR(a); char* sb = GET_STR(b); int la = GET_STR_LEN(a); int lb = GET_STR_LEN(b); if (la == 0) {return b; } if (lb == 0) {return a; } int len = la + lb; Object des = string_alloc(NULL, len); char*s = GET_STR(des); memcpy(s, sa, la); memcpy(s + la, sb, lb); return des; } case TYPE_LIST: { return list_add(GET_LIST(a), GET_LIST(b)); } } } tm_raise("obj_add: can not add %o and %o", (a), (b)); return NONE_OBJECT; }
int string_new( const char * text ) { int len = strlen( text ) + 1 ; int i; /* Reuse strings */ for ( i = 0; i < string_count; i++ ) if ( !strcmp( text, string_mem + string_offset[ i ] ) ) return i; if ( string_count == string_max ) { string_max += 1024 ; string_offset = ( int * ) realloc( string_offset, string_max * sizeof( int ) ) ; if ( string_offset == 0 ) { fprintf( stdout, "Too many strings\n" ) ; exit( 1 ) ; } } while ( string_used + len >= string_allocated ) string_alloc( 1024 ) ; string_offset[ string_count ] = string_used ; strcpy( string_mem + string_used, text ) ; string_used += len ; return string_count++ ; }
string_t *string_copy(string_t *str) { string_t *res = string_alloc(); if (str->buf) string_set(res, str->buf); return res; }
void set_win_sys_path (const char *newpath, struct env_set *es) { free (win_sys_path); win_sys_path = string_alloc (newpath, NULL); setenv_str (es, SYS_PATH_ENV_VAR_NAME, win_sys_path); /* route.exe needs this */ }
/* EN: Enter Newline */ void cmd_en (int argc, char *argv[]) { pad_t *pad = e->cpad; int xpos; int ypos; line_t *cline; line_t *newline; char *extratext; int intab; if (!(e->cpad->flags & FILE_WRITE)) { output_message_c ("en", "Pad is read-only."); return; } ypos = pad->curs_y + pad->offset_y; if (ypos > pad->line_count) pad_grow (pad, ypos); LINE_insert (pad, ypos, ""); cline = LINE_get_line_at (pad, ypos); if (cline->str != NULL) { xpos = get_string_pos (pad->curs_x + pad->offset_x, cline->str->data, &intab); if ((size_t)xpos < strlen (cline->str->data)) { /* We're 'inside' the string. Get the text on the * right, and truncate the current string. Then put * the 'extra' text on the line we've inserted or * allocated. */ extratext = cline->str->data + xpos; newline = LINE_get_line_at (pad, ypos + 1); newline->str = string_alloc ("%s", extratext); string_truncate (cline->str, xpos); } } pad->curs_x = 1; pad->offset_x = 0; if (pad->curs_y == pad->height) { pad->offset_y++; } else { pad->curs_y++; } if (e->occupied_window == COMMAND_WINDOW) e->occupied_window = COMMAND_WINDOW_EXECUTE; else pad_modified (pad); }
struct argv argv_clone (const struct argv *a, const size_t headroom) { struct argv r; size_t i; argv_init (&r); for (i = 0; i < headroom; ++i) argv_append (&r, NULL); if (a) { for (i = 0; i < a->argc; ++i) argv_append (&r, string_alloc (a->argv[i], NULL)); r.system_str = string_alloc (a->system_str, NULL); } return r; }
/* * Extract the Proxy-Authenticate header from the stream. * Consumes all headers. */ static int get_proxy_authenticate (socket_descriptor_t sd, int timeout, char **data, struct gc_arena *gc, volatile int *signal_received) { char buf[256]; int ret = HTTP_AUTH_NONE; while (true) { if (!recv_line (sd, buf, sizeof (buf), timeout, true, NULL, signal_received)) { *data = NULL; return HTTP_AUTH_NONE; } chomp (buf); if (!strlen(buf)) return ret; if (ret == HTTP_AUTH_NONE && !strncmp(buf, "Proxy-Authenticate: ", 20)) { if (!strncmp(buf+20, "Basic ", 6)) { msg (D_PROXY, "PROXY AUTH BASIC: '%s'", buf); *data = string_alloc(buf+26, gc); ret = HTTP_AUTH_BASIC; } #if PROXY_DIGEST_AUTH else if (!strncmp(buf+20, "Digest ", 7)) { msg (D_PROXY, "PROXY AUTH DIGEST: '%s'", buf); *data = string_alloc(buf+27, gc); ret = HTTP_AUTH_DIGEST; } #endif #if NTLM else if (!strncmp(buf+20, "NTLM", 4)) { msg (D_PROXY, "PROXY AUTH HTLM: '%s'", buf); *data = NULL; ret = HTTP_AUTH_NTLM; } #endif } } }
struct argv argv_insert_head (const struct argv *a, const char *head) { struct argv r; char *s; r = argv_clone (a, 1); r.argv[0] = string_alloc (head, NULL); s = r.system_str; r.system_str = string_alloc (head, NULL); if (s) { argv_system_str_append (&r, s, false); free (s); } return r; }
Object bf_time_asctime() { time_t rawtime; struct tm* timeinfo; time(&rawtime); timeinfo = localtime ( &rawtime ); char* ct = asctime(timeinfo); // ct ends with \n return string_alloc(ct, strlen(ct)-1); }
void alloc_string_array(string_array_t *dest, int ndims, ...) { size_t elements = 0; va_list ap; va_start(ap, ndims); elements = alloc_base_array(dest, ndims, ap); va_end(ap); dest->data = string_alloc(elements); }
Char* string_dup(const Char* str) { if (!str) return 0; if (!*str) return &s_empty; const size_t len = std::strlen(str); Char* const cpy = string_alloc(static_cast<UnsignedLong>(len)); if (cpy) std::strncpy(cpy, str, len + 1); return cpy; }
static void tokeniser_advance(Tokeniser* t) { ASSERT(!tokeniser_eof(t), "next() on empty tokeniser"); SET(t->tokenIndex, nil_make()); skipSpace(t); if (*t->cursor == 0) { t->eof = 1; return; } const char* punctChars = "()'"; for (int i = 0; punctChars[i] != 0; i++) { if (*t->cursor == punctChars[i]) { t->cursor++; SET(t->tokenIndex, makeToken(&punctChars[i], 1)); return; } } if (*t->cursor == '"') { // Find out how long the escaped string is, and check it was terminated // properly. int length = 0; t->cursor++; // advance past the initial " const char* end = stringEscape(t->cursor, countChars, &length); if (*end != '"') { THROW("Expected \", got EOF"); } // Now allocate a string and copy it in. Pointer ptr = string_alloc(length); char* dest = (char*) string_get(ptr); stringEscape(t->cursor, copyChars, &dest); *dest = 0; // Advance the cursor past the string. t->cursor = end + 1; SET(t->tokenIndex, ptr); } else { // Scan out a token const char* start = t->cursor; while (1) { char c = *t->cursor; if ((c == 0) || isspace(c) || isdelim(c)) { int length = t->cursor - start; SET(t->tokenIndex, makeToken(start, length)); break; } // Advance the cursor now, not earlier. t->cursor++; } } }
ArgvWrapper(const Argv& argv) { size_t i; argc = argv.size(); cargv = new char *[argc+1]; for (i = 0; i < argc; ++i) cargv[i] = string_alloc(argv[i]); cargv[i] = NULL; }
uint8_t * make_base64_string2 (const uint8_t *str, int src_len, struct gc_arena *gc) { uint8_t *ret = NULL; char *b64out = NULL; ASSERT (base64_encode ((const void *)str, src_len, &b64out) >= 0); ret = (uint8_t *) string_alloc (b64out, gc); free (b64out); return ret; }
/* ED: Edit Delete ('delete') */ void cmd_ed (int argc, char *argv[]) { pad_t *pad = e->cpad; int xpos; int ypos; line_t *cline; line_t *cline2; int intab; int len; if (!(e->cpad->flags & FILE_WRITE)) { output_message_c ("ed", "Pad is read-only."); return; } ypos = pad->curs_y + pad->offset_y; if (ypos > pad->line_count) { pad_grow (pad, ypos); pad_modified (pad); return; } cline = LINE_get_line_at (pad, ypos); len = xpos = 0; if (cline->str) { xpos = get_string_pos (pad->curs_x + pad->offset_x, cline->str->data, &intab); len = strlen (cline->str->data); } if (xpos < len) { string_remove (cline->str, xpos, 1); } else { cline2 = LINE_get_line_at (pad, ypos + 1); if (cline2 != NULL) { if (cline2->str != NULL) { if (cline->str == NULL) cline->str = string_alloc (""); if (cline->str) string_insert (cline->str, xpos, "%s", cline2->str->data); } LINE_remove (pad, cline2); } } pad_modified (pad); }
static bool add_client(const char *line, const char *prefix, const int line_num, struct pf_cn_elem ***next, const bool exclude) { struct pf_cn_elem *e; ALLOC_OBJ_CLEAR(e, struct pf_cn_elem); e->rule.exclude = exclude; e->rule.cn = string_alloc(line, NULL); **next = e; *next = &e->next; return true; }
static obj_t string_fn(obj_t args, Reporter rep) { string str; size_t i; if (!args_plus_match(rep, args, 0, is_char)) return unspecific; str = string_alloc(list_length(args)); for (i = 0; !is_null(args); i++, args = cdr(args)) str->data[i] = fetch_char(car(args)); return make_string(str); }
static void add_env_item(char *str, const bool do_alloc, struct env_item **list, struct gc_arena *gc) { struct env_item *item; ASSERT(str); ASSERT(list); ALLOC_OBJ_GC(item, struct env_item, gc); item->string = do_alloc ? string_alloc(str, gc) : str; item->next = *list; *list = item; }