nonascii.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 #ifdef HAVE_CONFIG_H
00024 # include "config.h"
00025 #endif
00026
00027
00028 #include "nonascii.h"
00029
00030 #include <stdlib.h>
00031 #include <string.h>
00032
00033
00034 static inline unsigned char
00035 to_uchar (char ch)
00036 {
00037 return ch;
00038 }
00039
00040 char *
00041 latin1toutf8 (const char *str)
00042 {
00043 char *p = malloc (2 * strlen (str) + 1);
00044 if (p)
00045 {
00046 size_t i, j = 0;
00047 for (i = 0; str[i]; i++)
00048 {
00049 if (to_uchar (str[i]) < 0x80)
00050 p[j++] = str[i];
00051 else if (to_uchar (str[i]) < 0xC0)
00052 {
00053 p[j++] = (unsigned char) 0xC2;
00054 p[j++] = str[i];
00055 }
00056 else
00057 {
00058 p[j++] = (unsigned char) 0xC3;
00059 p[j++] = str[i] - 64;
00060 }
00061 }
00062 p[j] = 0x00;
00063 }
00064
00065 return p;
00066 }
00067
00068 char *
00069 utf8tolatin1ifpossible (const char *passwd)
00070 {
00071 char *p;
00072 size_t i;
00073
00074 for (i = 0; passwd[i]; i++)
00075 {
00076 if (to_uchar (passwd[i]) > 0x7F)
00077 {
00078 if (to_uchar (passwd[i]) < 0xC0 || to_uchar (passwd[i]) > 0xC3)
00079 return strdup (passwd);
00080 i++;
00081 if (to_uchar (passwd[i]) < 0x80 || to_uchar (passwd[i]) > 0xBF)
00082 return strdup (passwd);
00083 }
00084 }
00085
00086 p = malloc (strlen (passwd) + 1);
00087 if (p)
00088 {
00089 size_t j = 0;
00090 for (i = 0; passwd[i]; i++)
00091 {
00092 if (to_uchar (passwd[i]) > 0x7F)
00093 {
00094
00095 p[j++] =
00096 ((to_uchar (passwd[i]) & 0x3) << 6)
00097 | (to_uchar (passwd[i + 1]) & 0x3F);
00098 i++;
00099 }
00100 else
00101 p[j++] = passwd[i];
00102 }
00103 p[j] = 0x00;
00104 }
00105 return p;
00106 }