/*************************************************************************/ /* LDAPCONF - Linuxconf module for LDAP operation. Copyright (C) 1999,2000,2001 Stein Vråle This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License for more details. **************************************************************************/ /* LDAP_UTILS.cc LDAP specific utility functions. **************************************************************************/ #include "ldapconf_defs.h" #include #include /* Convert from DNS name to dc components */ int dns2dc(char *buf,int size,int strip_hostname) { SSTRING base; SSTRINGS dc; str_splitline(buf,'.',dc); if (strip_hostname){ dc.remove_del(dc.getitem(0)); } for (int i=0; i0) { base.append(","); } sprintf(buf,"dc=%s",dc.getitem(i)->get()); base.append(buf); } strcpy(buf,base.get()); return 0; } /* Convert from dc components to DNS domain */ int dc2dns(char *buf,int size) { SSTRING base; SSTRINGS dc; SSTRINGS tb; // Split componenents str_splitline(buf,',',dc); for (int i=0; iget(),'=',tb); if (i>0) { base.append("."); } sprintf(buf,"%s",tb.getitem(1)->get()); base.append(buf); } strcpy(buf,base.get()); return 0; } /* Convert from dc component string to list */ int dc2list(const char *dc_str, SSTRINGS &dc_lst) { SSTRINGS dc; SSTRINGS tb; // Split componenents str_splitline(dc_str,',',dc); for (int i=0; iget())); str_splitline(dc.getitem(i)->get(),'=',tb); dc_lst.add(new SSTRING(tb.getitem(1)->get())); } return 0; } /* Chop the number at the end of a string. Used for multivalue attributes, where the number is used to index the current attribute value. Returns the value of the (index) number. */ int str_chop_num(const char *str){ int n = strlen(str); for (int i=n; i >= 0; i--){ } return n; } /* Convert characters to 7 bit ascii. Temporarly solution until UTF is supported. Any non-ascii char is converted to '?'. Return number of characters changed. */ int str_conv_ascii(char *str) { D(debugf(4,"-->LDAP_UTILS::str_conv_ascii: (%s)",str)); char *p = NULL; int c = 0; p = str; while (*p){ if (!isascii(*p)){ // Non-ascii - convert it to '?' and increase counter. D(debugf(3,"Non-ascii character found %c",*p)); *p = '?'; // Have to change a bit in ldapobject to get rid of the const c++; } p++; } D(debugf(4,"<--LDAP_UTILS::str_conv_ascii: (%s) %i",str,c)); return c; } /* The following code for handling UTF-8 <-> ISO-8859-1 encoding was posted by John Kristian (Netscape), found it in the OpenLDAP mailinglist archive (http://www.openldap.org/lists/openldap-general/199811/msg00031.html) (The code is not tested yet, maybe we find a better way to do it but at least we have something to start with) */ /* Read Latin-1 (ISO-8859-1) characters from stdin, convert them to UTF-8, and write the converted characters to stdout. UTF-8 is defined by RFC 2279. */ #include #include int latin1_to_UTF8(int argc, char** argv) { register int c; while ((c = getchar()) != EOF) { if ((c & 0x80) == 0) { putchar (c); } else { putchar (0xC0 | (0x03 & (c << 6))); putchar (0x80 | (0x3F & c)); } } if ( ! feof (stdin)) { errno = ferror (stdin); perror (argv[0]); } return 0; } /* Read UTF-8 characters from stdin, convert them to Latin-1 (ISO-8859-1), and write the converted characters to stdout. UTF-8 is defined by RFC 2279. */ static char UTF8len[64] /* A map from the most-significant 6 bits of the first byte to the total number of bytes in a UTF-8 character. */ = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* erroneous */ 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6}; int UTF8_to_latin1 (int argc, char** argv) { register int c; while ((c = getchar()) != EOF) { auto int len = UTF8len [(c >> 2) & 0x3F]; register unsigned long u = 0; switch (len) { case 6: u = c & 0x01; break; case 5: u = c & 0x03; break; case 4: u = c & 0x07; break; case 3: u = c & 0x0F; break; case 2: u = c & 0x1F; break; case 1: u = c & 0x7F; break; case 0: /* erroneous: c is the middle of a character. */ u = c & 0x3F; len = 5; break; } while (--len && (c = getchar()) != EOF) { if ((c & 0xC0) == 0x80) { u = (u << 6) | (c & 0x3F); } else { /* unexpected start of a new character */ ungetc (c, stdin); break; } } if (u <= 0xFF) { putchar (u); } else { /* this character can't be represented in Latin-1 */ putchar ('?'); /* a reasonable alternative is 0x1A (SUB) */ } if (c == EOF) break; } if ( ! feof (stdin)) { errno = ferror (stdin); perror (argv[0]); } return 0; }