ldap_escape.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdio.h>
00034 #include <ctype.h>
00035 #include "ldap_escape.h"
00036
00037 static const char hex[] = "0123456789ABCDEF";
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 int ldap_rfc4515_escape(str *sin, str *sout, int url_encode)
00054 {
00055 char *src, *dst;
00056
00057 if (sin == NULL || sout == NULL || sin->s == NULL || sout->s == NULL
00058 || sin->len <= 0 || sout->len < 3*sin->len+1)
00059 {
00060 return -1;
00061 }
00062
00063 src = sin->s;
00064 dst = sout->s;
00065
00066 while (src < (sin->s + sin->len))
00067 {
00068 switch (*src)
00069 {
00070 case '*':
00071 *dst++ = '\\';
00072 *dst++ = '2';
00073 *dst = 'a';
00074 break;
00075 case '(':
00076 *dst++ = '\\';
00077 *dst++ = '2';
00078 *dst = '8';
00079 break;
00080 case ')':
00081 *dst++ = '\\';
00082 *dst++ = '2';
00083 *dst = '9';
00084 break;
00085 case '\\':
00086 *dst++ = '\\';
00087 *dst++ = '5';
00088 *dst = 'c';
00089 break;
00090 case '?':
00091 if (url_encode)
00092 {
00093 *dst++ = '%';
00094 *dst++ = '3';
00095 *dst = 'F';
00096 } else
00097 {
00098 *dst = *src;
00099 }
00100 break;
00101 default:
00102 *dst = *src;
00103 }
00104
00105 src++;
00106 dst++;
00107 }
00108
00109 *dst = 0;
00110 sout->len = dst - sout->s;
00111 return 0;
00112 }