static int write_to_ast_str(const char *buffer, size_t size, void *data) { struct ast_str **dst = data; size_t str_size = ast_str_size(*dst); size_t remaining = str_size - ast_str_strlen(*dst); int ret; /* While ast_str_append will grow the ast_str, it won't report * allocation errors. Fortunately, it's not that hard. */ /* Remaining needs to be big enough for buffer, plus null char */ while (remaining < size + 1) { /* doubling the size of the buffer gives us 'amortized * constant' time. * See http://stackoverflow.com/a/249695/115478 for info. */ str_size *= 2; remaining = str_size - ast_str_strlen(*dst); } ret = ast_str_make_space(dst, str_size); if (ret == -1) { /* Could not alloc; fail */ return -1; } ast_str_append_substr(dst, -1, buffer, size); return 0; }
int __ast_str_helper(struct ast_str **buf, ssize_t max_len, int append, const char *fmt, va_list ap) #endif { int res, need; int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0; va_list aq; do { if (max_len < 0) { max_len = (*buf)->__AST_STR_LEN; /* don't exceed the allocated space */ } /* * Ask vsnprintf how much space we need. Remember that vsnprintf * does not count the final <code>'\\0'</code> so we must add 1. */ va_copy(aq, ap); res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq); need = res + offset + 1; /* * If there is not enough space and we are below the max length, * reallocate the buffer and return a message telling to retry. */ if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) { int len = (int)(*buf)->__AST_STR_LEN; if (max_len && max_len < need) { /* truncate as needed */ need = max_len; } else if (max_len == 0) { /* if unbounded, give more room for next time */ need += 16 + need / 4; } if (0) { /* debugging */ ast_verbose("extend from %d to %d\n", len, need); } if ( #if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) _ast_str_make_space(buf, need, file, lineno, function) #else ast_str_make_space(buf, need) #endif ) { ast_verbose("failed to extend from %d to %d\n", len, need); va_end(aq); return AST_DYNSTR_BUILD_FAILED; } (*buf)->__AST_STR_STR[offset] = '\0'; /* Truncate the partial write. */ /* Restart va_copy before calling vsnprintf() again. */ va_end(aq); continue; } va_end(aq); break; } while (1); /* update space used, keep in mind the truncation */ (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN - 1: res + offset; return res; }
static int base64_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len) { if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd); return -1; } if (cmd[7] == 'E') { if (buf) { ast_base64encode(buf, (unsigned char *) data, strlen(data), len); } else { if (len >= 0) { ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2); } ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str)); ast_str_update(*str); } } else { int decoded_len; if (buf) { decoded_len = ast_base64decode((unsigned char *) buf, data, len); /* add a terminating null at the end of buf, or at the * end of our decoded string, which ever is less */ buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0'; } else { if (len >= 0) { ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2); } decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str)); if (len) /* add a terminating null at the end of our * buffer, or at the end of our decoded string, * which ever is less */ ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0'; else /* space for the null is allocated above */ ast_str_buffer(*str)[decoded_len] = '\0'; ast_str_update(*str); } } return 0; }
static int blacklist_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len) { /* 2 bytes is a single integer, plus terminating null */ if (ast_str_size(*str) - ast_str_strlen(*str) < 2) { if (len > ast_str_size(*str) || len == 0) { ast_str_make_space(str, len ? len : ast_str_strlen(*str) + 2); } } if (ast_str_size(*str) - ast_str_strlen(*str) >= 2) { int res = blacklist_read(chan, cmd, data, ast_str_buffer(*str) + ast_str_strlen(*str), 2); ast_str_update(*str); return res; } return -1; }
char *__ast_str_helper2(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas) { int dynamic = 0; char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR; if (maxlen < 1) { if (maxlen == 0) { dynamic = 1; } maxlen = (*buf)->__AST_STR_LEN; } while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) { if (escapecommas && (*src == '\\' || *src == ',')) { *ptr++ = '\\'; maxlen--; (*buf)->__AST_STR_USED++; } *ptr++ = *src++; maxsrc--; maxlen--; (*buf)->__AST_STR_USED++; if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) || (dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) { char *oldbase = (*buf)->__AST_STR_STR; size_t old = (*buf)->__AST_STR_LEN; if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) { /* If the buffer can't be extended, end it. */ break; } /* What we extended the buffer by */ maxlen = old; ptr += (*buf)->__AST_STR_STR - oldbase; } } if (__builtin_expect(!maxlen, 0)) { ptr--; } *ptr = '\0'; return (*buf)->__AST_STR_STR; }
static void xpidf_to_string(void *body, struct ast_str **str) { pjxpidf_pres *pres = body; int growths = 0; int size; do { size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1); if (size <= AST_PJSIP_XML_PROLOG_LEN) { ast_str_make_space(str, ast_str_size(*str) * 2); ++growths; } } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS); if (size <= AST_PJSIP_XML_PROLOG_LEN) { ast_log(LOG_WARNING, "XPIDF body text too large\n"); return; } *(ast_str_buffer(*str) + size) = '\0'; ast_str_update(*str); }
static void dialog_info_to_string(void *body, struct ast_str **str) { pj_xml_node *dialog_info = body; int growths = 0; int size; do { size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str) - 1, PJ_TRUE); if (size <= AST_PJSIP_XML_PROLOG_LEN) { ast_str_make_space(str, ast_str_size(*str) * 2); ++growths; } } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS); if (size <= AST_PJSIP_XML_PROLOG_LEN) { ast_log(LOG_WARNING, "dialog-info+xml body text too large\n"); return; } *(ast_str_buffer(*str) + size) = '\0'; ast_str_update(*str); }
/*! * core handler for dynamic strings. * This is not meant to be called directly, but rather through the * various wrapper macros * ast_str_set(...) * ast_str_append(...) * ast_str_set_va(...) * ast_str_append_va(...) */ int __attribute__((format (printf, 4, 0))) __ast_str_helper(struct ast_str **buf, size_t max_len, int append, const char *fmt, va_list ap) { int res, need; int offset = (append && (*buf)->len) ? (*buf)->used : 0; if (max_len < 0) max_len = (*buf)->len; /* don't exceed the allocated space */ /* * Ask vsnprintf how much space we need. Remember that vsnprintf * does not count the final '\0' so we must add 1. */ res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap); need = res + offset + 1; /* * If there is not enough space and we are below the max length, * reallocate the buffer and return a message telling to retry. */ if (need > (*buf)->len && (max_len == 0 || (*buf)->len < max_len) ) { if (max_len && max_len < need) /* truncate as needed */ need = max_len; else if (max_len == 0) /* if unbounded, give more room for next time */ need += 16 + need/4; if (ast_str_make_space(buf, need)) { return AST_DYNSTR_BUILD_FAILED; } (*buf)->str[offset] = '\0'; /* Truncate the partial write. */ /* va_end() and va_start() must be done before calling * vsnprintf() again. */ return AST_DYNSTR_BUILD_RETRY; } /* update space used, keep in mind the truncation */ (*buf)->used = (res + offset > (*buf)->len) ? (*buf)->len : res + offset; return res; }
int __ast_str_helper(struct ast_str **buf, ssize_t max_len, int append, const char *fmt, va_list ap) #endif { int res; int added; int need; int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0; va_list aq; if (max_len < 0) { max_len = (*buf)->__AST_STR_LEN; /* don't exceed the allocated space */ } do { va_copy(aq, ap); res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq); va_end(aq); if (res < 0) { /* * vsnprintf write to string failed. * I don't think this is possible with a memory buffer. */ res = AST_DYNSTR_BUILD_FAILED; added = 0; break; } /* * vsnprintf returns how much space we used or would need. * Remember that vsnprintf does not count the nil terminator * so we must add 1. */ added = res; need = offset + added + 1; if (need <= (*buf)->__AST_STR_LEN || (max_len && max_len <= (*buf)->__AST_STR_LEN)) { /* * There was enough room for the string or we are not * allowed to try growing the string buffer. */ break; } /* Reallocate the buffer and try again. */ if (max_len == 0) { /* unbounded, give more room for next time */ need += 16 + need / 4; } else if (max_len < need) { /* truncate as needed */ need = max_len; } if ( #if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) _ast_str_make_space(buf, need, file, lineno, function) #else ast_str_make_space(buf, need) #endif ) { ast_log_safe(LOG_VERBOSE, "failed to extend from %d to %d\n", (int) (*buf)->__AST_STR_LEN, need); res = AST_DYNSTR_BUILD_FAILED; break; } } while (1); /* Update space used, keep in mind truncation may be necessary. */ (*buf)->__AST_STR_USED = ((*buf)->__AST_STR_LEN <= offset + added) ? (*buf)->__AST_STR_LEN - 1 : offset + added; /* Ensure that the string is terminated. */ (*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED] = '\0'; return res; }