| 1 |
|
| /* |
| 2 |
|
| * This Program is free software; you can redistribute it and/or modify it under |
| 3 |
|
| * the terms of the GNU General Public License as published by the Free Software |
| 4 |
|
| * Foundation; version 2 of the License. |
| 5 |
|
| * |
| 6 |
|
| * This Program is distributed in the hope that it will be useful, but WITHOUT |
| 7 |
|
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 8 |
|
| * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 9 |
|
| * |
| 10 |
|
| * You should have received a copy of the GNU General Public License along with |
| 11 |
|
| * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 12 |
|
| * Place, Suite 330, Boston, MA 02111-1307 USA. |
| 13 |
|
| * |
| 14 |
|
| * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission. |
| 15 |
|
| * Copyright (C) 2005 Red Hat, Inc. |
| 16 |
|
| * Copyright (C) 2009 Qindel Formacion y Servicios S.L. |
| 17 |
|
| * All rights reserved. |
| 18 |
|
| * |
| 19 |
|
| * This code is derived from 389 Directory Server (aka Fedora |
| 20 |
|
| * Directory Server) "389-ds-base-1.2.1/ldap/servers/slapd/dn.c". |
| 21 |
|
| */ |
| 22 |
|
| |
| 23 |
|
| #include "common.h" |
| 24 |
|
| |
| 25 |
|
| static char hex_digit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 26 |
|
| 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 27 |
|
| |
| 28 |
|
| static void |
| 29 |
0 |
| escape_char(char c, char **to) { |
| 30 |
0 |
| *(*to)++ = '\\'; |
| 31 |
0 |
| *(*to)++ = hex_digit[(c >> 4) & 0xf]; |
| 32 |
0 |
| *(*to)++ = hex_digit[c & 0xf]; |
| 33 |
0 |
| } |
| 34 |
|
| |
| 35 |
|
| static int |
| 36 |
0 |
| hex_digit2int(char c) { |
| 37 |
0 |
▶ | if (c >= '0' && c <= '9') return c - '0'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= '0' && c <= '9') return c - '0'; |
0 |
0 |
0 |
| if (c >= '0' && c <= '9') return c - '0'; |
0 |
0 |
0 |
|
| 38 |
0 |
▶ | if (c >= 'A' && c <= 'F') return c - ('A' - 10); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'F') return c - ('A' - 10); |
0 |
0 |
0 |
| if (c >= 'A' && c <= 'F') return c - ('A' - 10); |
0 |
0 |
0 |
|
| 39 |
0 |
▶ | if (c >= 'a' && c <= 'f') return c - ('a' - 10); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'a' && c <= 'f') return c - ('a' - 10); |
0 |
0 |
0 |
| if (c >= 'a' && c <= 'f') return c - ('a' - 10); |
0 |
0 |
0 |
|
| 40 |
0 |
| return -1; |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| static int |
| 44 |
0 |
| unescape_char(const char **from, const char *top) { |
| 45 |
0 |
▶ | if (*from >= top) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*from >= top) |
0 |
0 |
0 |
|
| 46 |
0 |
| return -1; |
| 47 |
0 |
| char c = *(*from)++; |
| 48 |
0 |
| int hn = hex_digit2int(c); |
| 49 |
0 |
▶ | if (hn >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hn >= 0) { |
0 |
0 |
0 |
|
| 50 |
0 |
▶ | if (*from < top) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*from < top) { |
0 |
0 |
0 |
|
| 51 |
0 |
| int ln = hex_digit2int(**from); |
| 52 |
0 |
▶ | if (ln >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ln >= 0) { |
0 |
0 |
0 |
|
| 53 |
0 |
| (*from)++; |
| 54 |
0 |
| return ((hn << 4) + ln); |
| 55 |
|
| } |
| 56 |
|
| } |
| 57 |
0 |
| return hn; |
| 58 |
|
| } |
| 59 |
0 |
| return c; |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
|
| #define SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '+') |
| 63 |
|
| |
| 64 |
|
| #define B4TYPE 0 |
| 65 |
|
| #define INTYPE 1 |
| 66 |
|
| #define B4EQUAL 2 |
| 67 |
|
| #define B4VALUE 3 |
| 68 |
|
| #define INVALUE 4 |
| 69 |
|
| #define INQUOTEDVALUE 5 |
| 70 |
|
| #define INVALUEHEX 6 |
| 71 |
|
| |
| 72 |
|
| SV * |
| 73 |
0 |
| dn_normalize(SV *ssv) { |
| 74 |
|
| STRLEN slen, space_ix; |
| 75 |
0 |
| const char *s = SvPVutf8(ssv, slen); |
| 76 |
0 |
| const char *stop = s + slen; |
| 77 |
0 |
| char *dstart = NULL, *d = NULL, *dtop = NULL; |
| 78 |
0 |
| SV *dsv = sv_2mortal(newSV(sv_len(ssv) + 4 * (UTF8_MAXBYTES_CASE))); |
| 79 |
0 |
| int state = B4TYPE; |
| 80 |
|
| |
| 81 |
0 |
| SvPOK_on(dsv); |
| 82 |
|
| |
| 83 |
0 |
▶ | while (s < stop) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < stop) { |
0 |
0 |
0 |
|
| 84 |
0 |
| int c = (unsigned char)(*s++); |
| 85 |
0 |
▶ | int is_space = isSPACE(c); |
Branch: 2 branches
| Branch | True | False | Total |
| int is_space = isSPACE(c); |
0 |
0 |
0 |
| int is_space = isSPACE(c); |
0 |
0 |
0 |
|
| 86 |
|
| |
| 87 |
0 |
▶ | if (dtop - d < UTF8_MAXBYTES_CASE + 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dtop - d < UTF8_MAXBYTES_CASE + 4) { |
0 |
0 |
0 |
|
| 88 |
0 |
| STRLEN dlen = d - dstart; |
| 89 |
0 |
▶ | if (dlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dlen) |
0 |
0 |
0 |
|
| 90 |
0 |
| SvCUR_set(dsv, dlen); |
| 91 |
0 |
| dstart = sv_grow(dsv, dlen + (stop - s) + 4 * (UTF8_MAXBYTES_CASE + 4)); |
| 92 |
0 |
| dtop = dstart + SvLEN(dsv); |
| 93 |
0 |
| d = dstart + SvCUR(dsv); |
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
0 |
| switch ( state ) { |
| 97 |
0 |
| case B4TYPE: |
| 98 |
0 |
▶ | if (is_space) |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_space) |
0 |
0 |
0 |
|
| 99 |
0 |
| break; |
| 100 |
0 |
| state = INTYPE; /* fall through */ |
| 101 |
|
| |
| 102 |
0 |
| case INTYPE: |
| 103 |
0 |
▶ | if (is_space) |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_space) |
0 |
0 |
0 |
|
| 104 |
0 |
| state = B4EQUAL; |
| 105 |
|
| else { |
| 106 |
0 |
▶ | if ( c == '=' ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( c == '=' ) |
0 |
0 |
0 |
|
| 107 |
0 |
| state = B4VALUE; |
| 108 |
|
| |
| 109 |
0 |
▶ | else if (!isALNUM(c) && c != '-') |
Branch: 3 branches
| Branch | True | False | Total |
| else if (!isALNUM(c) && c != '-') |
0 |
0 |
0 |
| else if (!isALNUM(c) && c != '-') |
0 |
0 |
0 |
| else if (!isALNUM(c) && c != '-') |
0 |
0 |
0 |
|
| 110 |
0 |
| croak("invalid character '%c' for dn description", c); |
| 111 |
|
| |
| 112 |
0 |
| *d++ = c; |
| 113 |
|
| } |
| 114 |
0 |
| break; |
| 115 |
|
| |
| 116 |
0 |
| case B4EQUAL: |
| 117 |
0 |
▶ | if (!is_space) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_space) { |
0 |
0 |
0 |
|
| 118 |
0 |
▶ | if ( c == '=' ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( c == '=' ) { |
0 |
0 |
0 |
|
| 119 |
0 |
| state = B4VALUE; |
| 120 |
0 |
| *d++ = c; |
| 121 |
|
| } |
| 122 |
|
| else |
| 123 |
0 |
| croak ("invalid character '%c' for dn description", *s); |
| 124 |
|
| } |
| 125 |
0 |
| break; |
| 126 |
|
| |
| 127 |
0 |
| case B4VALUE: |
| 128 |
0 |
▶ | if (is_space) |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_space) |
0 |
0 |
0 |
|
| 129 |
0 |
| break; |
| 130 |
0 |
▶ | if (c == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '#') { |
0 |
0 |
0 |
|
| 131 |
0 |
| state = INVALUEHEX; |
| 132 |
0 |
| break; |
| 133 |
|
| } |
| 134 |
0 |
| state = INVALUE; /* fall through */ |
| 135 |
0 |
| space_ix = d - dstart; |
| 136 |
|
| |
| 137 |
0 |
| case INVALUE: |
| 138 |
0 |
▶ | if (c == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') |
0 |
0 |
0 |
|
| 139 |
0 |
| state = INQUOTEDVALUE; |
| 140 |
|
| else { |
| 141 |
0 |
▶ | if (SEPARATOR(c)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SEPARATOR(c)) { |
0 |
0 |
0 |
| if (SEPARATOR(c)) { |
0 |
0 |
0 |
| if (SEPARATOR(c)) { |
0 |
0 |
0 |
|
| 142 |
0 |
| d = dstart + space_ix; |
| 143 |
0 |
| *d++ = c; |
| 144 |
0 |
| state = B4TYPE; |
| 145 |
0 |
| break; |
| 146 |
|
| } |
| 147 |
0 |
▶ | if (c == '\\') |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\\') |
0 |
0 |
0 |
|
| 148 |
0 |
| c = unescape_char(&s, stop); |
| 149 |
|
| |
| 150 |
0 |
▶ | if (c >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c >= 0) { |
0 |
0 |
0 |
|
| 151 |
0 |
▶ | if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
|
| 152 |
0 |
| *d++ = c; |
| 153 |
|
| else |
| 154 |
0 |
| escape_char(c, &d); |
| 155 |
|
| } |
| 156 |
0 |
▶ | if (!is_space) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_space) |
0 |
0 |
0 |
|
| 157 |
0 |
| space_ix = d - dstart; |
| 158 |
|
| } |
| 159 |
0 |
| break; |
| 160 |
|
| |
| 161 |
0 |
| case INQUOTEDVALUE: |
| 162 |
0 |
▶ | if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { |
0 |
0 |
0 |
|
| 163 |
0 |
| state = INVALUE; |
| 164 |
0 |
| space_ix = d - dstart; |
| 165 |
|
| } |
| 166 |
|
| else { |
| 167 |
0 |
▶ | if (c == '\\') |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\\') |
0 |
0 |
0 |
|
| 168 |
0 |
| c = unescape_char(&s, stop); |
| 169 |
0 |
▶ | if (c >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c >= 0) { |
0 |
0 |
0 |
|
| 170 |
0 |
▶ | if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
|
| 171 |
0 |
| *d++ = c; |
| 172 |
|
| else |
| 173 |
0 |
| escape_char(c, &d); |
| 174 |
|
| } |
| 175 |
|
| } |
| 176 |
0 |
| break; |
| 177 |
|
| |
| 178 |
0 |
| case INVALUEHEX: |
| 179 |
|
| { |
| 180 |
0 |
▶ | if (SEPARATOR(c)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SEPARATOR(c)) { |
0 |
0 |
0 |
| if (SEPARATOR(c)) { |
0 |
0 |
0 |
| if (SEPARATOR(c)) { |
0 |
0 |
0 |
|
| 181 |
0 |
| *d++ = c; |
| 182 |
0 |
| state = B4TYPE; |
| 183 |
|
| } |
| 184 |
0 |
▶ | else if (!is_space) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!is_space) { |
0 |
0 |
0 |
|
| 185 |
0 |
| int hn = hex_digit2int(c); |
| 186 |
0 |
▶ | if (hn >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hn >= 0) { |
0 |
0 |
0 |
|
| 187 |
0 |
▶ | int ln = (s < stop ? hex_digit2int(*s) : -1); |
Branch: 1 branch
| Branch | True | False | Total |
| int ln = (s < stop ? hex_digit2int(*s) : -1); |
0 |
0 |
0 |
|
| 188 |
0 |
▶ | c = (ln >= 0 ? ((hn << 4) + ln) : hn); |
Branch: 1 branch
| Branch | True | False | Total |
| c = (ln >= 0 ? ((hn << 4) + ln) : hn); |
0 |
0 |
0 |
|
| 189 |
|
| |
| 190 |
0 |
▶ | if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
| if ((c < 0x80) && (isALNUM(c) || (c == '-'))) |
0 |
0 |
0 |
|
| 191 |
0 |
| *d++ = c; |
| 192 |
|
| else |
| 193 |
0 |
| escape_char(c, &d); |
| 194 |
|
| } |
| 195 |
|
| else |
| 196 |
0 |
| croak("invalid character '%c' inside hexadecimal string", c); |
| 197 |
|
| } |
| 198 |
0 |
| break; |
| 199 |
|
| } |
| 200 |
0 |
| default: |
| 201 |
0 |
| croak ("internal error: bad state %d", state); |
| 202 |
|
| } |
| 203 |
|
| } |
| 204 |
0 |
| SvCUR_set(dsv, d - dstart); |
| 205 |
0 |
| SvREFCNT_inc(dsv); |
| 206 |
0 |
| return dsv; |
| 207 |
|
| } |