Skip to content

Commit

Permalink
lib-mail: Make message_header_strdup() public
Browse files Browse the repository at this point in the history
Also move it to a better file.
  • Loading branch information
sirainen committed Aug 30, 2018
1 parent 4e2d0ee commit ce57fab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
23 changes: 23 additions & 0 deletions src/lib-mail/message-header-parser.c
Expand Up @@ -4,6 +4,7 @@
#include "buffer.h"
#include "istream.h"
#include "str.h"
#include "unichar.h"
#include "message-size.h"
#include "message-header-parser.h"

Expand Down Expand Up @@ -411,3 +412,25 @@ void message_header_line_write(buffer_t *output,
buffer_append_c(output, '\n');
}
}

const char *
message_header_strdup(pool_t pool, const unsigned char *data, size_t size)
{
if (memchr(data, '\0', size) == NULL) {
/* fast path */
char *dest = p_malloc(pool, size+1);
memcpy(dest, data, size);
return dest;
}

/* slow path - this could be made faster, but it should be
rare so keep it simple */
string_t *str = str_new(pool, size+2);
for (size_t i = 0; i < size; i++) {
if (data[i] != '\0')
str_append_c(str, data[i]);
else
str_append(str, UNICODE_REPLACEMENT_CHAR_UTF8);
}
return str_c(str);
}
5 changes: 5 additions & 0 deletions src/lib-mail/message-header-parser.h
Expand Up @@ -74,4 +74,9 @@ void message_parse_header(struct istream *input, struct message_size *hdr_size,
void message_header_line_write(buffer_t *output,
const struct message_header_line *hdr);

/* Duplicate the given header value data and return it. Replaces any NULs with
UNICODE_REPLACEMENT_CHAR_UTF8. */
const char *
message_header_strdup(pool_t pool, const unsigned char *data, size_t size);

#endif
22 changes: 0 additions & 22 deletions src/lib-mail/message-part-data.c
Expand Up @@ -170,28 +170,6 @@ envelope_get_field(const char *name)
return ENVELOPE_FIELD_UNKNOWN;
}

static const char *
message_header_strdup(pool_t pool, const unsigned char *data, size_t size)
{
if (memchr(data, '\0', size) == NULL) {
/* fast path */
char *dest = p_malloc(pool, size+1);
memcpy(dest, data, size);
return dest;
}

/* slow path - this could be made faster, but it should be
rare so keep it simple */
string_t *str = str_new(pool, size+2);
for (size_t i = 0; i < size; i++) {
if (data[i] != '\0')
str_append_c(str, data[i]);
else
str_append(str, UNICODE_REPLACEMENT_CHAR_UTF8);
}
return str_c(str);
}

void message_part_envelope_parse_from_header(pool_t pool,
struct message_part_envelope **data,
struct message_header_line *hdr)
Expand Down

0 comments on commit ce57fab

Please sign in to comment.