Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

ldap_utils.cc

Go to the documentation of this file.
00001 /*************************************************************************/
00002 /*  LDAPCONF - Linuxconf module for LDAP operation.
00003     
00004     Copyright (C) 1999,2000,2001 Stein Vråle <stein@terminator.net>
00005 
00006     This program is distributed in the hope that it will be useful,
00007     but WITHOUT ANY WARRANTY; without even the implied warranty of
00008     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00009     GNU General Public License for more details.
00010     
00011 **************************************************************************/
00012 /*  LDAP_UTILS.cc
00013 
00014     LDAP specific utility functions.
00015 
00016 **************************************************************************/
00017 #include "ldapconf_defs.h"
00018 #include <string.h>
00019 #include <ctype.h>
00020 
00021 /*
00022 
00023   Convert from DNS name to dc components
00024 
00025 */
00026 int dns2dc(char *buf,int size,int strip_hostname)
00027 {
00028     SSTRING base;
00029     SSTRINGS dc;
00030     
00031     str_splitline(buf,'.',dc);
00032     if (strip_hostname){
00033         dc.remove_del(dc.getitem(0));
00034     }
00035 
00036     for (int i=0; i<dc.getnb(); i++){
00037         if (i>0) {
00038             base.append(",");
00039         }
00040         sprintf(buf,"dc=%s",dc.getitem(i)->get());
00041         base.append(buf);
00042     }
00043     strcpy(buf,base.get());
00044 
00045     return 0;
00046 }
00047 
00048 /*
00049 
00050   Convert from dc components to DNS domain
00051 
00052 */
00053 int dc2dns(char *buf,int size)
00054 {
00055     SSTRING base;
00056     SSTRINGS dc;
00057     SSTRINGS tb;
00058     
00059     // Split componenents
00060     str_splitline(buf,',',dc);
00061 
00062     for (int i=0; i<dc.getnb(); i++){
00063         tb.remove_all();
00064         // Remove assignement
00065         str_splitline(dc.getitem(i)->get(),'=',tb);
00066         if (i>0) {
00067             base.append(".");
00068         }
00069         sprintf(buf,"%s",tb.getitem(1)->get());
00070         base.append(buf);
00071     }
00072     strcpy(buf,base.get());
00073 
00074     return 0;
00075 }
00076 
00077 /*
00078 
00079   Convert from dc component string to list
00080 
00081 */
00082 int dc2list(const char *dc_str, SSTRINGS &dc_lst)
00083 {
00084     SSTRINGS dc;
00085     SSTRINGS tb;
00086 
00087     // Split componenents
00088     str_splitline(dc_str,',',dc);
00089 
00090     for (int i=0; i<dc.getnb(); i++){
00091         tb.remove_all();
00092         // Remove assignement
00093         D(debugf(4,"dc2list: comp= %s\n",dc.getitem(i)->get()));
00094         str_splitline(dc.getitem(i)->get(),'=',tb);
00095         dc_lst.add(new SSTRING(tb.getitem(1)->get()));
00096     }
00097 
00098     return 0;
00099 }
00100 
00101 /*
00102   
00103   Chop the number at the end of a string. 
00104   Used for multivalue attributes, where the number is used to index the current attribute value.
00105   Returns the value of the (index) number.
00106   
00107 */
00108 int str_chop_num(const char *str){
00109 
00110     int n = strlen(str);
00111 
00112 
00113     for (int i=n; i >= 0; i--){
00114     }
00115 
00116     return n;
00117 }
00118 
00119 /*
00120   Convert characters to 7 bit ascii.
00121   Temporarly solution until UTF is supported.
00122   Any non-ascii char is converted to '?'.
00123   Return number of characters changed.
00124 */
00125 int str_conv_ascii(const char *str)
00126 {
00127     D(debugf(4,"-->LDAP_UTILS::str_conv_ascii: (%s)",str));
00128 
00129     const char *p = NULL; 
00130     int c = 0;
00131 
00132     p = str;
00133     
00134     while (*p){
00135         if (!isascii(*p)){ // Non-ascii - convert it to '?' and increase counter.
00136             D(debugf(3,"Non-ascii character found %c",*p));
00137             // *p = '?';
00138             // Have to change a bit in ldapobject to get rid of the const
00139             c++;
00140         }
00141         p++;
00142     }
00143 
00144     D(debugf(4,"<--LDAP_UTILS::str_conv_ascii: (%s) %i",str,c));
00145     return c;
00146 }
00147 
00148 /*
00149   The following code for handling UTF-8 <-> ISO-8859-1 encoding was posted 
00150   by John Kristian (Netscape), found it in the OpenLDAP mailinglist archive 
00151   (http://www.openldap.org/lists/openldap-general/199811/msg00031.html)
00152 
00153   (The code is not tested yet, maybe we find a better way to do it but at least we have something to start with)
00154 */
00155 
00156 /* 
00157    Read Latin-1 (ISO-8859-1) characters from stdin, convert them
00158    to UTF-8, and write the converted characters to stdout.
00159    UTF-8 is defined by RFC 2279.
00160 */
00161 
00162 #include <errno.h>
00163 #include <stdio.h>
00164 
00165 int latin1_to_UTF8(int argc, char** argv)
00166 {
00167     register int c;
00168     while ((c = getchar()) != EOF) {
00169         if ((c & 0x80) == 0) {
00170             putchar (c);
00171         } else {
00172             putchar (0xC0 | (0x03 & (c << 6)));
00173             putchar (0x80 | (0x3F & c));
00174         }
00175     }
00176     if ( ! feof (stdin)) {
00177         errno = ferror (stdin);
00178         perror (argv[0]);
00179     }
00180     return 0;
00181 }
00182 
00183 /* Read UTF-8 characters from stdin, convert them to Latin-1
00184    (ISO-8859-1), and write the converted characters to stdout.
00185    UTF-8 is defined by RFC 2279.
00186 */
00187 
00188 static char UTF8len[64]
00189 /* A map from the most-significant 6 bits of the first byte
00190    to the total number of bytes in a UTF-8 character.
00191 */
00192 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00193    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00194    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* erroneous */
00195    2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6};
00196 
00197 int UTF8_to_latin1 (int argc, char** argv)
00198 {
00199     register int c;
00200     while ((c = getchar()) != EOF) {
00201         auto int len = UTF8len [(c >> 2) & 0x3F];
00202         register unsigned long u = 0;
00203         switch (len) {
00204         case 6: u = c & 0x01; break;
00205         case 5: u = c & 0x03; break;
00206         case 4: u = c & 0x07; break;
00207         case 3: u = c & 0x0F; break;
00208         case 2: u = c & 0x1F; break;
00209         case 1: u = c & 0x7F; break;
00210         case 0: /* erroneous: c is the middle of a character. */
00211             u = c & 0x3F; len = 5; break;
00212         }
00213         while (--len && (c = getchar()) != EOF) {
00214             if ((c & 0xC0) == 0x80) {
00215                 u = (u << 6) | (c & 0x3F);
00216             } else { /* unexpected start of a new character */
00217                 ungetc (c, stdin);
00218                 break;
00219             }
00220         }
00221         if (u <= 0xFF) {
00222             putchar (u);
00223         } else { /* this character can't be represented in Latin-1 */
00224             putchar ('?'); /* a reasonable alternative is 0x1A (SUB) */
00225         }
00226         if (c == EOF) break;
00227     }
00228     if ( ! feof (stdin)) {
00229         errno = ferror (stdin);
00230         perror (argv[0]);
00231     }
00232     return 0;
00233 }

Generated at Mon Jan 22 08:35:11 2001 for ldapconf by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000