Imported Upstream version 0.66.1
[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
8d138742
CE
19
20static void escape_specials(const char *str,
21 char *bufptr,
22 size_t *sizeptr)
23{
24 static const char specials[]="*()\\";
25
26 while (*str)
27 {
28 char buf[10];
29 char *p;
30
31 if (strchr(specials, *str))
32 {
33 sprintf(buf, "\\%02x", (int)(unsigned char)*str);
34 }
35 else
36 {
37 buf[0]=*str;
38 buf[1]=0;
39 }
40
41 for (p=buf; *p; p++)
42 {
43 if (bufptr)
44 *bufptr++=*p;
45 if (sizeptr)
46 ++*sizeptr;
47 }
48 ++str;
49 }
50
51 if (bufptr)
52 *bufptr=0;
53}
54
55char *courier_auth_ldap_escape(const char *str)
56{
57 char *escaped;
58 size_t escaped_cnt=1;
8d138742
CE
59
60 escape_specials(str, NULL, &escaped_cnt);
61 escaped=malloc(escaped_cnt);
62 if (!escaped)
63 return NULL;
64
65 escape_specials(str, escaped, NULL);
66
67 return escaped;
68}