}
new->filename = memory_pool_alloc (pool, len + sizeof (BINLOG_SUFFIX));
- g_strlcpy (new->filename, path, len + 1);
- g_strlcpy (new->filename + len, BINLOG_SUFFIX, sizeof (BINLOG_SUFFIX));
+ rspamd_strlcpy (new->filename, path, len + 1);
+ rspamd_strlcpy (new->filename + len, BINLOG_SUFFIX, sizeof (BINLOG_SUFFIX));
if (stat (new->filename, &st) == -1) {
/* Check errno to check whether we should create this file */
gint r;
va_start (aq, fmt);
- g_strlcpy (readbuf, yytext, sizeof (readbuf));
+ rspamd_strlcpy (readbuf, yytext, sizeof (readbuf));
r = snprintf (logbuf, sizeof (logbuf), "config file parse error! line: %d, text: %s, reason: ", yylineno, readbuf);
r += vsnprintf (logbuf + r, sizeof (logbuf) - r, fmt, aq);
gint r;
va_start (aq, fmt);
- g_strlcpy (readbuf, yytext, sizeof (readbuf));
+ rspamd_strlcpy (readbuf, yytext, sizeof (readbuf));
r = snprintf (logbuf, sizeof (logbuf), "config file parse warning! line: %d, text: %s, reason: ", yylineno, readbuf);
r += vsnprintf (logbuf + r, sizeof (logbuf) - r, fmt, aq);
while (*p) {
if (*p == ',' && *c != *p) {
str = memory_pool_alloc (pool, p - c + 1);
- g_strlcpy (str, c, p - c + 1);
+ rspamd_strlcpy (str, c, p - c + 1);
res = g_list_prepend (res, str);
/* Skip spaces */
while (g_ascii_isspace (*(++p)));
/* Put function name */
len = code_begin - line;
code_begin = memory_pool_alloc (cfg->cfg_pool, len + 1);
- g_strlcpy (code_begin, line, len + 1);
+ rspamd_strlcpy (code_begin, line, len + 1);
params = g_list_prepend (params, code_begin);
}
memory_pool_add_destructor (cfg->cfg_pool, (pool_destruct_func)g_list_free, params);
gchar *val;
val = memory_pool_alloc (pool, len + 1);
- g_strlcpy (val, text, len + 1);
+ rspamd_strlcpy (val, text, len + 1);
return val;
}
ud->section_pointer = check_worker_conf (ud->cfg, NULL);
}
else if (g_ascii_strcasecmp (element_name, "lua") == 0) {
- g_strlcpy (ud->section_name, element_name, sizeof (ud->section_name));
+ rspamd_strlcpy (ud->section_name, element_name, sizeof (ud->section_name));
ud->cur_attrs = process_attrs (ud->cfg, attribute_names, attribute_values);
if (! handle_lua (ud->cfg, ud, ud->cur_attrs, NULL, NULL, ud->cfg, 0)) {
*error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "cannot parse tag '%s'", ud->section_name);
}
else {
/* Extract other tags */
- g_strlcpy (ud->section_name, element_name, sizeof (ud->section_name));
+ rspamd_strlcpy (ud->section_name, element_name, sizeof (ud->section_name));
ud->cur_attrs = process_attrs (ud->cfg, attribute_names, attribute_values);
ud->state = XML_READ_VALUE;
}
ud->section_pointer = memory_pool_alloc0 (ud->cfg->cfg_pool, sizeof (struct statfile));
}
else {
- g_strlcpy (ud->section_name, element_name, sizeof (ud->section_name));
+ rspamd_strlcpy (ud->section_name, element_name, sizeof (ud->section_name));
/* Save attributes */
ud->cur_attrs = process_attrs (ud->cfg, attribute_names, attribute_values);
}
case XML_READ_WORKER:
case XML_READ_LOGGING:
case XML_READ_VIEW:
- g_strlcpy (ud->section_name, element_name, sizeof (ud->section_name));
+ rspamd_strlcpy (ud->section_name, element_name, sizeof (ud->section_name));
/* Save attributes */
ud->cur_attrs = process_attrs (ud->cfg, attribute_names, attribute_values);
break;
if (base->global_randomize_case) {
unsigned i;
char randbits[(sizeof(namebuf)+7)/8];
- g_strlcpy(namebuf, name, sizeof(namebuf));
+ rspamd_strlcpy(namebuf, name, sizeof(namebuf));
rand_bytes_function(randbits, (name_len+7)/8);
for (i = 0; i < name_len; ++i) {
if (g_ascii_isalpha(namebuf[i])) {
p++;
}
str = memory_pool_alloc (pool, p - c + 2);
- g_strlcpy (str, c - 1, (p - c + 2));
+ rspamd_strlcpy (str, c - 1, (p - c + 2));
g_strstrip (str);
msg_debug ("found regexp: %s", str);
if (strlen (str) > 0) {
func = memory_pool_alloc (pool, sizeof (struct expression_function));
func->name = memory_pool_alloc (pool, p - c + 1);
func->args = NULL;
- g_strlcpy (func->name, c, (p - c + 1));
+ rspamd_strlcpy (func->name, c, (p - c + 1));
g_strstrip (func->name);
state = READ_FUNCTION_ARGUMENT;
g_queue_push_tail (function_stack, func);
/* In fact it is not function, but symbol */
if (c != p) {
str = memory_pool_alloc (pool, p - c + 1);
- g_strlcpy (str, c, (p - c + 1));
+ rspamd_strlcpy (str, c, (p - c + 1));
g_strstrip (str);
if (strlen (str) > 0) {
insert_expression (pool, &expr, EXPR_STR, 0, str, copy);
p++;
if (c != p) {
str = memory_pool_alloc (pool, p - c + 1);
- g_strlcpy (str, c, (p - c + 1));
+ rspamd_strlcpy (str, c, (p - c + 1));
g_strstrip (str);
if (strlen (str) > 0) {
insert_expression (pool, &expr, EXPR_STR, 0, str, copy);
if (*p == ',' || (*p == ')' && brackets == 0)) {
arg = NULL;
str = memory_pool_alloc (pool, p - c + 1);
- g_strlcpy (str, c, (p - c + 1));
+ rspamd_strlcpy (str, c, (p - c + 1));
g_strstrip (str);
/* Recursive call */
arg = maybe_parse_expression (pool, str);
node = *((struct rspamd_fuzzy_node **)pvalue);
if (now - node->time > expire) {
/* Remove expired item */
- g_strlcpy (tmpindex, indexbuf, sizeof (tmpindex));
+ rspamd_strlcpy (tmpindex, indexbuf, sizeof (tmpindex));
pvalue = JudySLNext (jtree, tmpindex, PJE0);
JudySLDel (&jtree, indexbuf, PJE0);
- g_strlcpy (indexbuf, tmpindex, sizeof (indexbuf));
+ rspamd_strlcpy (indexbuf, tmpindex, sizeof (indexbuf));
bloom_del (bf, node->h.hash_pipe);
server_stat->fuzzy_hashes_expired ++;
server_stat->fuzzy_hashes --;
}
url_text = memory_pool_alloc (task->task_pool, len + 1);
- g_strlcpy (url_text, c, len + 1);
+ rspamd_strlcpy (url_text, c, len + 1);
decode_entitles (url_text, NULL);
if (g_ascii_strncasecmp (url_text, "http://", sizeof ("http://") - 1) != 0 &&
}
lmtp->task->helo = memory_pool_alloc (lmtp->task->task_pool, line->len - i + 1);
/* Strlcpy makes string null terminated by design */
- g_strlcpy (lmtp->task->helo, c, line->len - i + 1);
+ rspamd_strlcpy (lmtp->task->helo, c, line->len - i + 1);
lmtp->state = LMTP_READ_FROM;
if (! out_lmtp_reply (lmtp->task, LMTP_OK, "", "Ok")) {
return -1;
}
rcpt = memory_pool_alloc (lmtp->task->task_pool, line->len - i + 1);
/* Strlcpy makes string null terminated by design */
- g_strlcpy (rcpt, c, line->len - i + 1);
+ rspamd_strlcpy (rcpt, c, line->len - i + 1);
lmtp->task->rcpt = g_list_prepend (lmtp->task->rcpt, rcpt);
lmtp->state = LMTP_READ_MESSAGE;
if (! out_lmtp_reply (lmtp->task, LMTP_DATA, "", "Enter message, ending with \".\" on a line by itself")) {
void
rspamd_common_log_function (GLogLevelFlags log_level, const gchar *function, const gchar *fmt, ...)
{
- static gchar logbuf[BUFSIZ];
+ static gchar logbuf[BUFSIZ], escaped_logbuf[BUFSIZ];
va_list vp;
u_char *end;
va_start (vp, fmt);
end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
*end = '\0';
+ (void)rspamd_escape_string (escaped_logbuf, logbuf, sizeof (escaped_logbuf));
va_end (vp);
- rspamd_log->log_func (NULL, function, log_level, logbuf, FALSE, rspamd_log->cfg);
+ rspamd_log->log_func (NULL, function, log_level, escaped_logbuf, FALSE, rspamd_log->cfg);
}
}
void
rspamd_conditional_debug (guint32 addr, const gchar *function, const gchar *fmt, ...)
{
- static gchar logbuf[BUFSIZ];
+ static gchar logbuf[BUFSIZ], escaped_logbuf[BUFSIZ];
va_list vp;
u_char *end;
va_start (vp, fmt);
end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
*end = '\0';
+ (void)rspamd_escape_string (escaped_logbuf, logbuf, sizeof (escaped_logbuf));
va_end (vp);
- rspamd_log->log_func (NULL, function, G_LOG_LEVEL_DEBUG, logbuf, TRUE, rspamd_log->cfg);
+ rspamd_log->log_func (NULL, function, G_LOG_LEVEL_DEBUG, escaped_logbuf, TRUE, rspamd_log->cfg);
}
}
void
rspamd_glib_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg)
{
+ gchar escaped_logbuf[BUFSIZ];
+
if (rspamd_log->enabled) {
- rspamd_log->log_func (log_domain, NULL, log_level, message, FALSE, rspamd_log->cfg);
+ (void)rspamd_escape_string (escaped_logbuf, message, sizeof (escaped_logbuf));
+ rspamd_log->log_func (log_domain, NULL, log_level, escaped_logbuf, FALSE, rspamd_log->cfg);
}
}
case 2:
if (*p == ':') {
reply->cur_header = g_malloc (p - s + 1);
- g_strlcpy (reply->cur_header, s, p - s + 1);
+ rspamd_strlcpy (reply->cur_header, s, p - s + 1);
reply->parser_state = 3;
}
else if (*p == '\r' && *(p + 1) == '\n') {
if (*p == '\r') {
if (reply->cur_header != NULL) {
tmp = g_malloc (p - s + 1);
- g_strlcpy (tmp, s, p - s + 1);
+ rspamd_strlcpy (tmp, s, p - s + 1);
g_hash_table_insert (reply->headers, reply->cur_header, tmp);
reply->cur_header = NULL;
}
hostend = p;
}
hdata->host = memory_pool_alloc (map_pool, hostend - def + 1);
- g_strlcpy (hdata->host, def, hostend - def + 1);
+ rspamd_strlcpy (hdata->host, def, hostend - def + 1);
hdata->path = memory_pool_strdup (map_pool, p);
hdata->rlen = 0;
/* Now try to resolve */
else {
/* Create new tree */
prefix = g_malloc (cur_level * sizeof (char) + 1);
- g_strlcpy (prefix, orig, cur_level + 1);
+ rspamd_strlcpy (prefix, orig, cur_level + 1);
cur->leafs[num].data = g_tree_new_full (compare_prefixes, NULL, g_free, NULL);
g_tree_insert (cur->leafs[num].data, prefix, GUINT_TO_POINTER (1));
return 1;
}
else {
/* Got some node, so check it */
- g_strlcpy (tmp, orig, MIN (sizeof (tmp), cur_level + 1));
+ rspamd_strlcpy (tmp, orig, MIN (sizeof (tmp), cur_level + 1));
if ((res = (uintptr_t)g_tree_lookup (cur->leafs[num].data, tmp)) != 0) {
if (! read_only) {
g_tree_insert (cur->leafs[num].data, g_strdup (tmp), GUINT_TO_POINTER (res + 1));
while (p - line <= len) {
if (g_ascii_isspace (*p) || p - line == len) {
name = g_malloc (p - line + 1);
- g_strlcpy (name, line, p - line + 1);
+ rspamd_strlcpy (name, line, p - line + 1);
if (metaphone (name, 0, &metaname)) {
/* Skip spaces */
while (p - line <= len && g_ascii_isspace (*p)) {
port = DEFAULT_PORT;
}
name = memory_pool_alloc (fuzzy_module_ctx->fuzzy_pool, p - strvec[i]);
- g_strlcpy (name, strvec[i], p - strvec[i]);
+ rspamd_strlcpy (name, strvec[i], p - strvec[i]);
if (!inet_aton (name, &addr)) {
/* Resolve using dns */
hent = gethostbyname (name);
checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5, image->data->data, image->data->len);
/* Construct fake fuzzy hash */
fake_fuzzy = memory_pool_alloc0 (task->task_pool, sizeof (fuzzy_hash_t));
- g_strlcpy (fake_fuzzy->hash_pipe, checksum, sizeof (fake_fuzzy->hash_pipe));
+ rspamd_strlcpy (fake_fuzzy->hash_pipe, checksum, sizeof (fake_fuzzy->hash_pipe));
register_fuzzy_call (task, fake_fuzzy);
g_free (checksum);
}
checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5, mime_part->content->data, mime_part->content->len);
/* Construct fake fuzzy hash */
fake_fuzzy = memory_pool_alloc0 (task->task_pool, sizeof (fuzzy_hash_t));
- g_strlcpy (fake_fuzzy->hash_pipe, checksum, sizeof (fake_fuzzy->hash_pipe));
+ rspamd_strlcpy (fake_fuzzy->hash_pipe, checksum, sizeof (fake_fuzzy->hash_pipe));
register_fuzzy_call (task, fake_fuzzy);
g_free (checksum);
}
/* Construct fake fuzzy hash */
fake_fuzzy.block_size = 0;
bzero (fake_fuzzy.hash_pipe, sizeof (fake_fuzzy.hash_pipe));
- g_strlcpy (fake_fuzzy.hash_pipe, checksum, sizeof (fake_fuzzy.hash_pipe));
+ rspamd_strlcpy (fake_fuzzy.hash_pipe, checksum, sizeof (fake_fuzzy.hash_pipe));
if (! register_fuzzy_controller_call (session, task, &fake_fuzzy, cmd, value, flag, saved)) {
/* Cannot write hash */
session->state = STATE_REPLY;
/* Construct fake fuzzy hash */
fake_fuzzy.block_size = 0;
bzero (fake_fuzzy.hash_pipe, sizeof (fake_fuzzy.hash_pipe));
- g_strlcpy (fake_fuzzy.hash_pipe, checksum, sizeof (fake_fuzzy.hash_pipe));
+ rspamd_strlcpy (fake_fuzzy.hash_pipe, checksum, sizeof (fake_fuzzy.hash_pipe));
if (! register_fuzzy_controller_call (session, task, &fake_fuzzy, cmd, value, flag, saved)) {
/* Cannot write hash */
session->state = STATE_REPLY;
}
else if (is_numeric && dots_num == 0) {
/* This is number */
- g_strlcpy (num_buf, hostname->begin, MIN (hostname->len + 1, sizeof (num_buf)));
+ rspamd_strlcpy (num_buf, hostname->begin, MIN (hostname->len + 1, sizeof (num_buf)));
errno = 0;
ip_num = strtoull (num_buf, NULL, 10);
if (errno != 0) {
cur_param->bufsize = sizeof (gint);
sum_str = g_compute_checksum_for_string (G_CHECKSUM_MD5, struri (url), -1);
- g_strlcpy (cur_param->key, sum_str, sizeof (cur_param->key));
+ rspamd_strlcpy (cur_param->key, sum_str, sizeof (cur_param->key));
g_free (sum_str);
selected = (struct memcached_server *)get_upstream_by_hash ((void *)task->cfg->memcached_servers,
if (cur) {
f = cur->data;
s = memory_pool_alloc (session->pool, f->len + 1);
- g_strlcpy (s, f->begin, f->len + 1);
+ rspamd_strlcpy (s, f->begin, f->len + 1);
session->task->from = s;
}
/* Save recipients */
if (cur) {
f = cur->data;
s = memory_pool_alloc (session->pool, f->len + 1);
- g_strlcpy (s, f->begin, f->len + 1);
+ rspamd_strlcpy (s, f->begin, f->len + 1);
session->task->rcpt = g_list_prepend (session->task->rcpt, s);
}
t = g_list_next (t);
}
arg = cmd->args->data;
session->helo = memory_pool_alloc (session->pool, arg->len + 1);
- g_strlcpy (session->helo, arg->begin, arg->len + 1);
+ rspamd_strlcpy (session->helo, arg->begin, arg->len + 1);
/* Now try to write reply */
if (cmd->command == SMTP_COMMAND_HELO) {
/* No ESMTP */
r = check_smtp_ustream_reply (in, '2');
if (r == -1) {
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
/* XXX: assume upstream errors as critical errors */
session->state = SMTP_STATE_CRITICAL_ERROR;
rspamd_dispatcher_restore (session->dispatcher);
r = check_smtp_ustream_reply (in, '2');
if (r == -1) {
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
/* XXX: assume upstream errors as critical errors */
session->state = SMTP_STATE_CRITICAL_ERROR;
rspamd_dispatcher_restore (session->dispatcher);
r = check_smtp_ustream_reply (in, '2');
if (r == -1) {
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
/* XXX: assume upstream errors as critical errors */
session->state = SMTP_STATE_CRITICAL_ERROR;
rspamd_dispatcher_restore (session->dispatcher);
r = check_smtp_ustream_reply (in, '2');
if (r == -1) {
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
/* XXX: assume upstream errors as critical errors */
session->state = SMTP_STATE_CRITICAL_ERROR;
rspamd_dispatcher_restore (session->dispatcher);
r = check_smtp_ustream_reply (in, '2');
if (r == -1) {
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
rspamd_dispatcher_restore (session->dispatcher);
if (! rspamd_dispatcher_write (session->dispatcher, session->error, in->len, FALSE, TRUE)) {
goto err;
rspamd_dispatcher_pause (session->upstream_dispatcher);
}
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
/* Write to client */
if (! rspamd_dispatcher_write (session->dispatcher, session->error, in->len, FALSE, TRUE)) {
goto err;
r = check_smtp_ustream_reply (in, '3');
if (r == -1) {
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
/* XXX: assume upstream errors as critical errors */
session->state = SMTP_STATE_CRITICAL_ERROR;
rspamd_dispatcher_restore (session->dispatcher);
break;
case SMTP_STATE_AFTER_DATA:
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
session->state = SMTP_STATE_DATA;
rspamd_dispatcher_restore (session->dispatcher);
if (! rspamd_dispatcher_write (session->dispatcher, session->error, 0, FALSE, TRUE)) {
r = check_smtp_ustream_reply (in, '5');
if (r == -1) {
session->error = memory_pool_alloc (session->pool, in->len + 1);
- g_strlcpy (session->error, in->begin, in->len + 1);
+ rspamd_strlcpy (session->error, in->begin, in->len + 1);
/* XXX: assume upstream errors as critical errors */
session->state = SMTP_STATE_CRITICAL_ERROR;
rspamd_dispatcher_restore (session->dispatcher);
p = strchr (begin, '/');
if (p != NULL) {
/* Extract mask */
- g_strlcpy (mask_buf, p + 1, sizeof (mask_buf));
+ rspamd_strlcpy (mask_buf, p + 1, sizeof (mask_buf));
addr->mask = mask_buf[0] * 10 + mask_buf[1];
if (addr->mask > 32) {
return FALSE;
if (host == NULL) {
hostlen = p - begin;
host = memory_pool_alloc (task->task_pool, hostlen);
- g_strlcpy (host, begin, hostlen);
+ rspamd_strlcpy (host, begin, hostlen);
}
}
else {
}
- g_strlcpy (new_file->filename, filename, sizeof (new_file->filename));
+ rspamd_strlcpy (new_file->filename, filename, sizeof (new_file->filename));
new_file->len = st.st_size;
/* Aqquire lock for this operation */
lock_file (new_file->fd, FALSE);
statfile_pool_is_open (statfile_pool_t * pool, gchar *filename)
{
static stat_file_t f, *ret;
- g_strlcpy (f.filename, filename, sizeof (f.filename));
+ rspamd_strlcpy (f.filename, filename, sizeof (f.filename));
ret = lfind (&f, pool->files, (size_t *)&pool->opened, sizeof (stat_file_t), cmpstatfile);
return ret;
}
/* One more character */
p ++;
}
- g_strlcpy (numbuf, c, MIN (p - c + 1, sizeof (numbuf)));
+ rspamd_strlcpy (numbuf, c, MIN (p - c + 1, sizeof (numbuf)));
errno = 0;
*val = strtoull (numbuf, NULL, 10);
if (errno != 0) {
item = memory_pool_alloc0 (pcache->static_pool, sizeof (struct cache_item));
item->s = memory_pool_alloc0 (pcache->static_pool, sizeof (struct saved_cache_item));
- g_strlcpy (item->s->symbol, name, sizeof (item->s->symbol));
+ rspamd_strlcpy (item->s->symbol, name, sizeof (item->s->symbol));
item->func = func;
item->user_data = user_data;
item = memory_pool_alloc0 (dynamic_pool, sizeof (struct cache_item));
item->s = memory_pool_alloc (dynamic_pool, sizeof (struct saved_cache_item));
- g_strlcpy (item->s->symbol, name, sizeof (item->s->symbol));
+ rspamd_strlcpy (item->s->symbol, name, sizeof (item->s->symbol));
item->func = func;
item->user_data = user_data;
/* Handle weight using default metric */
addr->sun_family = AF_UNIX;
- g_strlcpy (addr->sun_path, path, sizeof (addr->sun_path));
+ rspamd_strlcpy (addr->sun_path, path, sizeof (addr->sun_path));
#ifdef FREEBSD
addr->sun_len = SUN_LEN (addr);
#endif
return p - buf;
}
+gchar *
+rspamd_escape_string (gchar *dst, const gchar *src, gsize len)
+{
+ gchar *buf = dst, *last = dst + len;
+ guint8 c;
+ const gchar *p = src;
+
+ while (*p && buf < last) {
+ c = *p++;
+ if (G_UNLIKELY ((c & 0x80))) {
+ c &= 0x7F;
+ if (last - buf >= 3) {
+ *buf++ = 'M';
+ *buf++ = '-';
+ }
+ }
+
+ if (G_UNLIKELY ( g_ascii_iscntrl (c))) {
+ if (c == '\n') {
+ *buf++ = ' ';
+ }
+ else if (c == '\t') {
+ *buf++ = '\t';
+ }
+ else {
+ *buf++ = '^';
+ if (buf != last) {
+ *buf++ = c ^ 0100;
+ }
+ }
+ }
+ else {
+ *buf++ = c;
+ }
+ }
+
+ *buf = '\0';
+
+ return buf;
+}
gchar *
rspamd_vsnprintf (gchar *buf, size_t max, const gchar *fmt, va_list args)
{
- gchar *p, zero, *last;
- gint d;
+ gchar *p, zero, *last;
+ gint d;
long double f, scale;
size_t len, slen;
- gint64 i64;
- guint64 ui64;
- guint width, sign, hex, max_width, frac_width, i;
+ gint64 i64;
+ guint64 ui64;
+ guint width, sign, hex, max_width, frac_width, i;
f_str_t *v;
if (max == 0) {
continue;
+ case 'S':
+ p = va_arg(args, gchar *);
+ if (p == NULL) {
+ p = "(NULL)";
+ }
+
+ if (slen == (size_t) -1) {
+ buf = rspamd_escape_string (buf, p, last - buf);
+
+ } else {
+ len = (buf + slen < last) ? slen : (size_t) (last - buf);
+
+ buf = rspamd_escape_string (buf, p, len);
+ }
+
+ fmt++;
+
+ continue;
+
case 'O':
i64 = (gint64) va_arg (args, off_t);
sign = 1;
}
#endif
+gsize
+rspamd_strlcpy (gchar *dst, const gchar *src, gsize siz)
+{
+ gchar *d = dst;
+ const gchar *s = src;
+ gsize n = siz;
+
+ /* Copy as many bytes as will fit */
+ if (n != 0) {
+ while (--n != 0) {
+ if ((*d++ = *s++) == '\0') {
+ break;
+ }
+ }
+ }
+
+ if (n == 0 && siz != 0) {
+ *d = '\0';
+ }
+
+ return (s - src - 1); /* count does not include NUL */
+}
+
/*
* vi:ts=4
*/
* %p void *
* %V f_str_t *
* %s null-terminated string
+ * %S ascii null-terminated string
* %*s length and string
* %Z '\0'
* %N '\n'
gint rspamd_snprintf (gchar *buf, size_t max, const gchar *fmt, ...);
gchar *rspamd_vsnprintf (gchar *buf, size_t max, const gchar *fmt, va_list args);
+/*
+ * Copy src to dest limited to len, in compare with standart strlcpy(3) rspamd strlcpy does not
+ * traverse the whole string and it is possible to use it for non NULL terminated strings. This is
+ * more like memccpy(dst, src, size, '\0')
+ *
+ * @param dst destination string
+ * @param src source string
+ * @param siz length of destination buffer
+ * @return bytes copied
+ */
+gsize rspamd_strlcpy (gchar *dst, const gchar *src, gsize siz);
+
+/*
+ * Escape rspamd string to write it to log file or other 7 bit prefferable places
+ *
+ * @param dst destination string
+ * @param src source string
+ * @param len length of destination buffer
+ * @return pointer to end of buffer
+ */
+gchar * rspamd_escape_string (gchar *dst, const gchar *src, gsize len);
+
#endif
ctx->timeout.tv_usec = 0;
ctx->sock = -1;
ctx->options = MEMC_OPT_DEBUG;
- g_strlcpy (param->key, buf, sizeof (param->key));
+ rspamd_strlcpy (param->key, buf, sizeof (param->key));
param->buf = buf;
param->bufsize = strlen (buf);
ctx->param = param;