[dovecot-cvs] dovecot/src/lib strescape.c,NONE,1.1 strescape.h,NONE,1.1 Makefile.am,1.25,1.26 strfuncs.c,1.25,1.26 strfuncs.h,1.13,1.14

cras at procontrol.fi cras at procontrol.fi
Fri Jan 3 17:57:15 EET 2003


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv6416/lib

Modified Files:
	Makefile.am strfuncs.c strfuncs.h 
Added Files:
	strescape.c strescape.h 
Log Message:
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
take memory, maybe also a bit faster. This caused pretty large changes all
around.

Also moved all string (un)escaping code to lib/strescape.c. 



--- NEW FILE: strescape.c ---
/*
    Copyright (c) 2003 Timo Sirainen

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "lib.h"
#include "str.h"
#include "strescape.h"

const char *str_escape(const char *str)
{
	char *ret, *p;
	size_t i, esc;

	/* get length of string and number of chars to escape */
	esc = 0;
	for (i = 0; str[i] != '\0'; i++) {
		if (IS_ESCAPED_CHAR(str[i]))
			esc++;
	}

	if (esc == 0)
		return str;

	/* @UNSAFE: escape them */
	p = ret = t_malloc(i + esc + 1);
	for (; *str != '\0'; str++) {
		if (IS_ESCAPED_CHAR(*str))
			*p++ = '\\';
		*p++ = *str;
	}
	*p = '\0';
	return ret;
}

void str_append_unescaped(String *dest, const char *src, size_t src_size)
{
	size_t start = 0, i = 0;

	while (i < src_size) {
		start = i;
		for (; i < src_size; i++) {
			if (src[i] == '\\')
				break;
		}

		str_append_n(dest, src + start, i-start);

		if (src[i] == '\\')
			i++;
		start = i;
	}
}

void str_unescape(char *str)
{
	/* @UNSAFE */
	char *dest;

	while (*str != '\\') {
		if (*str == '\0')
			return;
		str++;
	}

	for (dest = str; *str != '\0'; str++) {
		if (*str != '\\' || str[1] == '\0')
			*dest++ = *str;
	}

	*dest = '\0';
}

--- NEW FILE: strescape.h ---
#ifndef __STRESCAPE_H
#define __STRESCAPE_H

#define IS_ESCAPED_CHAR(c) ((c) == '"' || (c) == '\\')

/* escape all '\' and '"' characters */
const char *str_escape(const char *str);

/* remove all '\' characters, append to given string */
void str_append_unescaped(String *dest, const char *src, size_t src_size);

/* remove all '\' characters */
void str_unescape(char *str);

#endif

Index: Makefile.am
===================================================================
RCS file: /home/cvs/dovecot/src/lib/Makefile.am,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- Makefile.am	22 Dec 2002 07:06:16 -0000	1.25
+++ Makefile.am	3 Jan 2003 15:57:12 -0000	1.26
@@ -44,8 +44,9 @@
 	safe-memset.c \
 	safe-mkdir.c \
 	sendfile-util.c \
-	strfuncs.c \
 	str.c \
+	strescape.c \
+	strfuncs.c \
 	unlink-directory.c \
 	unlink-lockfiles.c \
 	utc-offset.c \
@@ -91,8 +92,9 @@
 	safe-memset.h \
 	safe-mkdir.h \
 	sendfile-util.h \
-	strfuncs.h \
 	str.h \
+	strescape.h \
+	strfuncs.h \
 	unlink-directory.h \
 	unlink-lockfiles.h \
 	utc-offset.h \

Index: strfuncs.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/strfuncs.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- strfuncs.c	27 Dec 2002 16:02:25 -0000	1.25
+++ strfuncs.c	3 Jan 2003 15:57:12 -0000	1.26
@@ -462,18 +462,6 @@
         return str;
 }
 
-void str_remove_escapes(char *str)
-{
-	char *dest;
-
-	for (dest = str; *str != '\0'; str++) {
-		if (*str != '\\' || str[1] == '\0')
-			*dest++ = *str;
-	}
-
-	*dest = '\0';
-}
-
 const char **t_strsplit(const char *data, const char *separators)
 {
         const char **array;

Index: strfuncs.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/strfuncs.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- strfuncs.h	22 Dec 2002 08:18:29 -0000	1.13
+++ strfuncs.h	3 Jan 2003 15:57:12 -0000	1.14
@@ -49,7 +49,6 @@
 
 char *str_ucase(char *str);
 char *str_lcase(char *str);
-void str_remove_escapes(char *str);
 
 /* seprators is an array of separator characters, not a separator string. */
 const char **t_strsplit(const char *data, const char *separators);




More information about the dovecot-cvs mailing list