static char * mailstream_read_len_append(mailstream * stream, MMAPString * line, size_t i) { size_t cur_size; cur_size = line->len; if (mmap_string_set_size(line, line->len + i) == NULL) return NULL; if (mailstream_read(stream, line->str + cur_size, i) < 0) return NULL; return line->str; }
int collect(struct mem_message *message, int msg_stream) { #ifndef MMAP_UNAVAILABLE struct stat sb; #endif int len; memset(message, 0, sizeof(struct mem_message)); #ifndef MMAP_UNAVAILABLE /* if stream is a file whose size is known, try to mmap it */ if (!fstat(0, &sb) && S_ISREG(sb.st_mode) && sb.st_size >= 0) { message->len = sb.st_size; if ((message->data = mmap(NULL, message->len, PROT_READ, MAP_SHARED, msg_stream, 0)) != MAP_FAILED) return 0; } #endif /* read the buffer from stdin by blocks, until EOF or error. save the message in a mmap_string */ if ((message->mstring = mmap_string_sized_new(BLOCKSIZE)) == NULL) { perror("mmap_string_new"); goto error; } message->len = 0; while ((len = _read(msg_stream, message->mstring->str + message->len, BLOCKSIZE)) > 0) { message->len += len; /* reserve room for next block */ if ((mmap_string_set_size(message->mstring, message->len + BLOCKSIZE)) == NULL) { perror("mmap_string_set_size"); goto error; } } if (len == 0) { message->data = message->mstring->str; return 0; /* OK */ } perror("read"); error: if (message->mstring != NULL) mmap_string_free(message->mstring); return -1; }
oxws_result oxws_write_response_to_buffer(oxws* oxws, size_t buffer_size_hint) { if(oxws == NULL) return OXWS_ERROR_INVALID_PARAMETER; oxws_internal* internal = OXWS_INTERNAL(oxws); if(internal == NULL) return OXWS_ERROR_INTERNAL; curl_easy_setopt(internal->curl, CURLOPT_WRITEFUNCTION, oxws_write_response_to_buffer_callback); curl_easy_setopt(internal->curl, CURLOPT_WRITEDATA, internal); /* (re)allocate and clear response buffer */ if(internal->response_buffer) { mmap_string_set_size(internal->response_buffer, buffer_size_hint); mmap_string_truncate(internal->response_buffer, 0); } else { internal->response_buffer = mmap_string_sized_new(buffer_size_hint); } return OXWS_NO_ERROR; }
static int db_get_next_msg_number(struct mail_cache_db * maildb, uint32_t * p_num) { int r; char key_value[PATH_MAX]; uint32_t num; void * serialized; size_t serialized_len; int res; MMAPString * mmapstr; size_t cur_token; mmapstr = mmap_string_new(""); if (mmapstr == NULL) { res = MAIL_ERROR_MEMORY; goto err; } snprintf(key_value, sizeof(key_value), "next-msg"); r = mail_cache_db_get(maildb, key_value, strlen(key_value), &serialized, &serialized_len); if (r >= 0) { if (mmap_string_append_len(mmapstr, serialized, serialized_len) == NULL) { res = MAIL_ERROR_MEMORY; goto err; } cur_token = 0; r = mailimf_cache_int_read(mmapstr, &cur_token, &num); if (r < 0) num = 1; } else { num = 1; } mmap_string_set_size(mmapstr, 0); cur_token = 0; r = mailimf_cache_int_write(mmapstr, &cur_token, num + 1); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_mmapstr; } r = mail_cache_db_put(maildb, key_value, strlen(key_value), mmapstr->str, mmapstr->len); if (r < 0) { res = MAIL_ERROR_FILE; goto free_mmapstr; } mmap_string_free(mmapstr); * p_num = num; return MAIL_NO_ERROR; free_mmapstr: mmap_string_free(mmapstr); err: return res; }
LIBETPAN_EXPORT int charconv_buffer(const char * tocode, const char * fromcode, const char * str, size_t length, char ** result, size_t * result_len) { #ifdef HAVE_ICONV iconv_t conv; size_t iconv_r; int r; char * out; char * pout; size_t out_size; size_t old_out_size; size_t count; #endif int res; MMAPString * mmapstr; if (extended_charconv != NULL) { size_t result_length; result_length = length * 6; mmapstr = mmap_string_sized_new( result_length + 1); *result_len = 0; if (mmapstr == NULL) { res = MAIL_CHARCONV_ERROR_MEMORY; } else { res = (*extended_charconv)( tocode, fromcode, str, length, mmapstr->str, &result_length); if (res != MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET) { if (res == MAIL_CHARCONV_NO_ERROR) { *result = mmapstr->str; res = mmap_string_ref(mmapstr); if (res < 0) { res = MAIL_CHARCONV_ERROR_MEMORY; mmap_string_free(mmapstr); } else { mmap_string_set_size( mmapstr, result_length); /* can't fail */ *result_len = result_length; } } free( *result); } return res; } /* else, let's try with iconv, if available */ } #ifndef HAVE_ICONV return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; #else conv = iconv_open(tocode, fromcode); if (conv == (iconv_t) -1) { res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; goto err; } out_size = 6 * length; /* UTF-8 can be encoded up to 6 bytes */ mmapstr = mmap_string_sized_new(out_size + 1); if (mmapstr == NULL) { res = MAIL_CHARCONV_ERROR_MEMORY; goto err; } out = mmapstr->str; pout = out; old_out_size = out_size; iconv_r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?"); if (iconv_r == (size_t) -1) { res = MAIL_CHARCONV_ERROR_CONV; goto free; } iconv_close(conv); * pout = '\0'; count = old_out_size - out_size; r = mmap_string_ref(mmapstr); if (r < 0) { res = MAIL_CHARCONV_ERROR_MEMORY; goto free; } * result = out; * result_len = count; return MAIL_CHARCONV_NO_ERROR; free: mmap_string_free(mmapstr); err: return res; #endif }