00001
00002 00003 00004 00005 00006 00007 00008 00009 00010 00011
00012 00013 00014 00015 00016
00017 #include "ldapconf_defs.h"
00018 #include <string.h>
00019 #include <ctype.h>
00020
00021 00022 00023 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 00051 00052
00053 int dc2dns(char *buf,int size)
00054 {
00055 SSTRING base;
00056 SSTRINGS dc;
00057 SSTRINGS tb;
00058
00059
00060 str_splitline(buf,',',dc);
00061
00062 for (int i=0; i<dc.getnb(); i++){
00063 tb.remove_all();
00064
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 00080 00081
00082 int dc2list(const char *dc_str, SSTRINGS &dc_lst)
00083 {
00084 SSTRINGS dc;
00085 SSTRINGS tb;
00086
00087
00088 str_splitline(dc_str,',',dc);
00089
00090 for (int i=0; i<dc.getnb(); i++){
00091 tb.remove_all();
00092
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 00104 00105 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 00121 00122 00123 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)){
00136 D(debugf(3,"Non-ascii character found %c",*p));
00137
00138
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 00150 00151 00152 00153 00154
00155
00156 00157 00158 00159 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 00184 00185 00186
00187
00188 static char UTF8len[64]
00189 00190 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,
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:
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 {
00217 ungetc (c, stdin);
00218 break;
00219 }
00220 }
00221 if (u <= 0xFF) {
00222 putchar (u);
00223 } else {
00224 putchar ('?');
00225 }
00226 if (c == EOF) break;
00227 }
00228 if ( ! feof (stdin)) {
00229 errno = ferror (stdin);
00230 perror (argv[0]);
00231 }
00232 return 0;
00233 }