/*! * Remove hexencoded dots and slashes (":2e" and ":2f") */ static int ad_conv_dehex(const char *path, const struct stat *sp, const struct vol *vol, const char **newpathp) { EC_INIT; static char buf[MAXPATHLEN]; const char *adpath, *p; int adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0; bstring newpath = NULL; LOG(log_debug, logtype_default,"ad_conv_dehex(\"%s\"): BEGIN", fullpathname(path)); *newpathp = NULL; if ((p = strchr(path, ':')) == NULL) goto EC_CLEANUP; EC_NULL( newpath = bfromcstr(path) ); EC_ZERO( bfindreplace(newpath, bfromcstr(":2e"), bfromcstr("."), 0) ); EC_ZERO( bfindreplace(newpath, bfromcstr(":2f"), bfromcstr(":"), 0) ); become_root(); if (adflags != ADFLAGS_DIR) rename(vol->ad_path(path, 0), vol->ad_path(bdata(newpath), 0)); rename(path, bdata(newpath)); unbecome_root(); strlcpy(buf, bdata(newpath), sizeof(buf)); *newpathp = buf; EC_CLEANUP: if (newpath) bdestroy(newpath); EC_EXIT; }
char *test_bfindreplace(void) { bstring b = bfromcstr(test); bstring find = bfromcstr(test_find); bstring replace = bfromcstr(test_replace); mu_assert(bfindreplace(b, find, replace, 0) == BSTR_OK, "Failed to find replace on bfindreplace()."); mu_assert(strcmp((const char *)b->data, test_bfindreplace_result) == 0, "Wrong bfindreplace() result."); mu_assert(bdestroy(b) == BSTR_OK, "Failed to bdestroy() afetr bfindreplace()."); b = bfromcstr(test); mu_assert(bfindreplace(b, find, replace, 10) == BSTR_OK, "Failed to find replace on bfindreplace()."); mu_assert(strcmp((const char *)b->data, test_bfindreplace_result) == 0, "Wrong bfindreplace() result."); mu_assert(bdestroy(b) == BSTR_OK, "Failed to bdestroy() afetr bfindreplace()."); mu_assert(bdestroy(find) == BSTR_OK, "Failed to bdestroy() afetr bfindreplace()."); mu_assert(bdestroy(replace) == BSTR_OK, "Failed to bdestroy() afetr bfindreplace()."); return NULL; }
static void unescape_opt_value(bstring str) { const struct tagbstring esc_comma = bsStatic("\\,"); const struct tagbstring comma = bsStatic(","); const struct tagbstring esc_slash = bsStatic("\\\\"); const struct tagbstring slash = bsStatic("\\"); const struct tagbstring esc_colon = bsStatic("\\:"); const struct tagbstring colon = bsStatic(":"); const struct tagbstring esc_equals = bsStatic("\\="); const struct tagbstring equals = bsStatic("="); assert(str); /* Unescape: \, \\ \: \; \= */ bfindreplace(str, &esc_comma, &comma, 0); bfindreplace(str, &esc_slash, &slash, 0); bfindreplace(str, &esc_colon, &colon, 0); bfindreplace(str, &esc_equals, &equals, 0); }
/* Expands trigraphs in the read-in source file. */ int bircc_sourcefile_expand_trigraphs(bircc_sourcefile * me) { bircc_trigraph_row * row; if (!me) return -1; if (!me->text) return -2; /* use bfindreplace for now, a bit brute force, but hey... :p */ for(row = bircc_trigraph_table; row->trigraph; row++) { bstring tofind = bfromcstr(row->trigraph); bstring torepl = bfromcstr(row->replacement); while(bfindreplace(me->text, tofind, torepl, 0) > 0); bdestroy(tofind); bdestroy(torepl); } return 0; }
char *test_bfindreplace() { bstring test_str = bfromcstr("hello world! hello world!"); bstring find = bfromcstr("hello"); bstring replace = bfromcstr("goodbye"); int rc = bfindreplace(test_str, find, replace, 0); mu_assert(rc == BSTR_OK, "bfindreplace should return BSTR_OK"); mu_assert(biseqStatic(test_str, "goodbye world! goodbye world!") == 1, "bfindreplace return content error"); bdestroy(test_str); bdestroy(find); bdestroy(replace); return NULL; }
char *test_bfindreplace() { bstr = bfromcstr(test2); bstring pattern = bfromcstr(data); bstring replace = bfromcstr(vata); int rc = bfindreplace(bstr, pattern, replace, 0); mu_assert(rc == 0, "Replace did not go well."); int pos = binstr(bstr, 0, replace); mu_assert(pos == 6, "Find returned incorrect result."); bdestroy(pattern); bdestroy(replace); bdestroy(bstr); return NULL; }
int write_some(RingBuffer *buffer, int fd, int is_socket) { int rc = 0; bstring data = RingBuffer_get_all(buffer); check(data != NULL, "Failed to get data from the buffer."); check(bfindreplace(data, &NL, &CRLF, 0) == BSTR_OK, "Failed to replace NL."); if(is_socket) { rc = send(fd, bdata(data), blength(data), 0); } else { rc = write(fd, bdata(data), blength(data)); } check(rc == blength(data), "failed to write everything to fd: %d", fd); bdestroy(data); return rc; error: return -1; }
static int Server_load_ciphers(Server *srv, bstring ssl_ciphers_val) { const struct tagbstring bstr_underscore = bsStatic("_"); const struct tagbstring bstr_dash = bsStatic("-"); struct bstrList *ssl_cipher_list = bsplit(ssl_ciphers_val, ' '); int i = 0, n = 0; int *ciphers = NULL; check(ssl_cipher_list != NULL && ssl_cipher_list->qty > 0, "Invalid cipher list, it must be separated by space ' ' characters " "and you need at least one. Or, just leave it out for defaults."); ciphers = h_calloc(ssl_cipher_list->qty + 1, sizeof(int)); check_mem(ciphers); for(i = 0; i < ssl_cipher_list->qty; i++) { bstring cipher = ssl_cipher_list->entry[i]; int id = -1; // Replace underscores (used in old ciphers) with dashes bfindreplace(cipher, &bstr_underscore, &bstr_dash, 0); // Search legacy cipher list for(n = 0; legacy_cipher_table[n].name != NULL; ++n) { if(biseqcstr(cipher, legacy_cipher_table[n].name)) { id = legacy_cipher_table[n].id; break; } } if(id != -1 && mbedtls_ssl_ciphersuite_from_id(id) != NULL) { ciphers[i] = id; } else { // Search polarssl cipher list const mbedtls_ssl_ciphersuite_t * suite = mbedtls_ssl_ciphersuite_from_string(bdata(cipher)); if (suite != NULL) ciphers[i] = suite->id; else sentinel("Unrecognized cipher: %s", bdata(cipher)); } } bstrListDestroy(ssl_cipher_list); ciphers[i] = 0; srv->ciphers = ciphers; hattach(ciphers, srv); return 0; error: if(ssl_cipher_list) bstrListDestroy(ssl_cipher_list); if(ciphers != NULL) h_free(ciphers); return -1; }
void createLine(int side, bstring base, bstring content, lineData lineMap, int * highlightMask) { if (lineMap.type == INFO) { content = bfromcstr(""); lineMap.lineNo = 0; } int position = 0; int needToCloseLastHighlightBeforeEscapingHTML = FALSE; if (highlightMask != NULL) { int lastState = MASK_SAME; int advanceBy; int i; int contentLen = content->slen; // Copy this because it will change as we work. for (i = 0; i < contentLen; i++) { advanceBy = 1; // Normally advance by one char. // Escape HTML as we go. if (content->data[position] == '&') { breplace(content, position, 1, bfromcstr("&"), ' '); advanceBy += 4; } else if (content->data[position] == '<') { breplace(content, position, 1, bfromcstr("<"), ' '); advanceBy += 3; } else if (content->data[position] == '>') { breplace(content, position, 1, bfromcstr(">"), ' '); advanceBy += 3; } if (highlightMask[i] != lastState) { if (highlightMask[i] == MASK_DIFFERENT) { binsert(content, position, bfromcstr("<em>"), ' '); advanceBy += 4; } else { binsert(content, position, bfromcstr("</em>"), ' '); advanceBy += 5; } } position += advanceBy; lastState = highlightMask[i]; } } // Escape HTML. // TODO: This can't possibly be good enough. bfindreplace(content, bfromcstr("&"), bfromcstr("&"), position); bfindreplace(content, bfromcstr("<"), bfromcstr("<"), position); bfindreplace(content, bfromcstr(">"), bfromcstr(">"), position); // Put something in blank lines. if (content->slen == 0) bcatcstr(content, " "); if (needToCloseLastHighlightBeforeEscapingHTML) { bcatcstr(content, "</em>"); } // TODO: there's a lot of string manipulation going on here. It might be // good for performance to call ballocmin and boost the base string size by // a big chunk. if (lineMap.lineNo >= 0 && lineMap.type != INFO) { char * lineNo = lineNumberString(lineMap.lineNo); bcatcstr(base, "<td class='line_number "); bcatcstr(base, typeString(lineMap.type)); bcatcstr(base, " "); bcatcstr(base, (side == LEFT) ? "left" : "right"); bcatcstr(base, "' width='*'>"); bcatcstr(base, lineNo); bcatcstr(base, "</td>\n"); bcatcstr(base, "<td class='line "); free(lineNo); } else { bcatcstr(base, "<td colspan='2' class='line "); } bstring whitespace; bcatcstr(base, typeString(lineMap.type)); bcatcstr(base, " "); bcatcstr(base, (side == LEFT) ? "left" : "right"); bcatcstr(base, "' width='49%'>"); bconcat(base, whitespace = getWhitespace(lineMap.leadingSpaces)); bconcat(base, content); bcatcstr(base, "</td>\n"); bdestroy(whitespace); }
int bescape(bstring str) { // TODO: Handle all other non-printable characters by using \0xXXXX format. if (bfindreplace(str, bfromcstr("\\"), bfromcstr("\\\\"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\0"), bfromcstr("\\0"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\a"), bfromcstr("\\a"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\b"), bfromcstr("\\b"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\f"), bfromcstr("\\f"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\n"), bfromcstr("\\n"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\r"), bfromcstr("\\r"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\t"), bfromcstr("\\t"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\v"), bfromcstr("\\v"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\'"), bfromcstr("\\'"), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\""), bfromcstr("\\\""), 0) == BSTR_ERR) return BSTR_ERR; if (bfindreplace(str, bfromcstr("\?"), bfromcstr("\?"), 0) == BSTR_ERR) return BSTR_ERR; return BSTR_OK; }