Imported Debian patch 0.63.0-6
[hcoop/debian/courier-authlib.git] / authldapescape.c
CommitLineData
8d138742
CE
1/*
2** Copyright 2009 Double Precision, Inc. See COPYING for
3** distribution information.
4*/
5
6#if HAVE_CONFIG_H
7#include "courier_auth_config.h"
8#endif
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <errno.h>
13#if HAVE_UNISTD_H
14#include <unistd.h>
15#endif
16
17#include "courierauth.h"
18
19static const char rcsid[]="$Id: authldapescape.c,v 1.2 2009/12/18 04:33:45 mrsam Exp $";
20
21static void escape_specials(const char *str,
22 char *bufptr,
23 size_t *sizeptr)
24{
25 static const char specials[]="*()\\";
26
27 while (*str)
28 {
29 char buf[10];
30 char *p;
31
32 if (strchr(specials, *str))
33 {
34 sprintf(buf, "\\%02x", (int)(unsigned char)*str);
35 }
36 else
37 {
38 buf[0]=*str;
39 buf[1]=0;
40 }
41
42 for (p=buf; *p; p++)
43 {
44 if (bufptr)
45 *bufptr++=*p;
46 if (sizeptr)
47 ++*sizeptr;
48 }
49 ++str;
50 }
51
52 if (bufptr)
53 *bufptr=0;
54}
55
56char *courier_auth_ldap_escape(const char *str)
57{
58 char *escaped;
59 size_t escaped_cnt=1;
60 int rc;
61
62 escape_specials(str, NULL, &escaped_cnt);
63 escaped=malloc(escaped_cnt);
64 if (!escaped)
65 return NULL;
66
67 escape_specials(str, escaped, NULL);
68
69 return escaped;
70}