Subversion Repositories Kolibri OS

Rev

Rev 3479 | Rev 6892 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3479 Rev 5098
1
#include 
1
//#include 
2
#include 
2
//#include 
-
 
3
typedef unsigned int size_t;
-
 
4
#define NULL ((void*)0)
3
 
5
 
4
typedef int conv_t;
6
typedef int conv_t;
5
typedef unsigned int ucs4_t;
7
typedef unsigned int ucs4_t;
6
 
8
 
7
typedef int iconv_t;
9
typedef int iconv_t;
8
 
10
 
9
 
11
 
10
/* Return code if invalid input after a shift sequence of n bytes was read.
12
/* Return code if invalid input after a shift sequence of n bytes was read.
11
   (xxx_mbtowc) */
13
   (xxx_mbtowc) */
12
#define RET_SHIFT_ILSEQ(n)  (-1-2*(n))
14
#define RET_SHIFT_ILSEQ(n)  (-1-2*(n))
13
/* Return code if invalid. (xxx_mbtowc) */
15
/* Return code if invalid. (xxx_mbtowc) */
14
#define RET_ILSEQ           RET_SHIFT_ILSEQ(0)
16
#define RET_ILSEQ           RET_SHIFT_ILSEQ(0)
15
/* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
17
/* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
16
#define RET_TOOFEW(n)       (-2-2*(n))
18
#define RET_TOOFEW(n)       (-2-2*(n))
17
 
19
 
18
/* Return code if invalid. (xxx_wctomb) */
20
/* Return code if invalid. (xxx_wctomb) */
19
#define RET_ILUNI      -1
21
#define RET_ILUNI      -1
20
/* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
22
/* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
21
#define RET_TOOSMALL   -2
23
#define RET_TOOSMALL   -2
22
 
24
 
23
 
25
 
24
#define CP866 0
26
#define CP866 0
25
#define CP1251 1
27
#define CP1251 1
26
#define CP1252 2
28
#define CP1252 2
27
#define KOI8_RU 3
29
#define KOI8_RU 3
28
#define ISO8859_5 4
30
#define ISO8859_5 4
29
#define UTF_8 5
31
#define UTF_8 5
30
 
32
 
31
 
33
 
32
int strcmp (const char* a, const char* b)
34
int strcmp (const char* a, const char* b)
33
{
35
{
34
	return (*a && *b && (*a == *b)) ? ((*(a+1) || *(b+1)) ? (strcmp(a+1, b+1)) : (0)) : ((*a > *b) ? (1) : (-1));
36
	return (*a && *b && (*a == *b)) ? ((*(a+1) || *(b+1)) ? (strcmp(a+1, b+1)) : (0)) : ((*a > *b) ? (1) : (-1));
35
}
37
}
36
 
38
 
37
 
39
 
38
 
40
 
39
 
41
 
40
#include "cp866.h"
42
#include "cp866.h"
41
#include "cp1251.h"
43
#include "cp1251.h"
42
#include "cp1252.h"
44
#include "cp1252.h"
43
#include "koi8_ru.h"
45
#include "koi8_ru.h"
44
#include "iso8859_5.h"
46
#include "iso8859_5.h"
45
#include "utf8.h"
47
#include "utf8.h"
46
 
48
 
47
 
49
 
48
int encoding(const char *what) {
50
int encoding(const char *what) {
49
	
51
	
50
	if (!strcmp(what,"CP866")) return CP866;
52
	if (!strcmp(what,"CP866")) return CP866;
51
	if (!strcmp(what,"CP1251")) return CP1251;
53
	if (!strcmp(what,"CP1251")) return CP1251;
52
	if (!strcmp(what,"CP1252")) return CP1252;
54
	if (!strcmp(what,"CP1252")) return CP1252;
53
	if (!strcmp(what,"KOI8-RU")) return KOI8_RU;
55
	if (!strcmp(what,"KOI8-RU")) return KOI8_RU;
54
	if (!strcmp(what,"ISO8859-5")) return ISO8859_5;
56
	if (!strcmp(what,"ISO8859-5")) return ISO8859_5;
55
	if (!strcmp(what,"UTF-8")) return UTF_8;
57
	if (!strcmp(what,"UTF-8")) return UTF_8;
56
	return -1;
58
	return -1;
57
	
59
	
58
}
60
}
59
 
61
 
60
 
62
 
61
iconv_t iconv_open(const char *tocode, const char *fromcode) {
63
iconv_t iconv_open(const char *tocode, const char *fromcode) {
62
	int to, from;
64
	int to, from;
63
	if ((to=encoding(tocode))==-1) return -1;
65
	if ((to=encoding(tocode))==-1) return -1;
64
	if ((from=encoding(fromcode))==-1) return -1;
66
	if ((from=encoding(fromcode))==-1) return -1;
65
	to=to<<16&0xFFFF0000;
67
	to=to<<16&0xFFFF0000;
66
	from=from&0xFFFF;
68
	from=from&0xFFFF;
67
	return to+from;
69
	return to+from;
68
		
70
		
69
}
71
}
70
 
72
 
71
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
73
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
72
	char **outbuf, size_t *outbytesleft)
74
	char **outbuf, size_t *outbytesleft)
73
{
75
{
74
	int n, to, from, count1,count2;
76
	int n, to, from, count1,count2;
75
	int pwc, converted,written;
77
	int pwc, converted,written;
76
	int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
78
	int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
77
	int (*wctomb)(conv_t, ucs4_t *, const unsigned char *, int);
79
	int (*wctomb)(conv_t, ucs4_t *, const unsigned char *, int);
78
	
80
	
79
	char *str;
81
	char *str;
80
	str=*outbuf;
82
	str=*outbuf;
81
	
83
	
82
	from=cd>>16;
84
	from=cd>>16;
83
	to=cd&0xFFFF;
85
	to=cd&0xFFFF;
84
			
86
			
85
	switch (from)
87
	switch (from)
86
	{
88
	{
87
		case CP866: mbtowc=cp866_mbtowc; break;
89
		case CP866: mbtowc=cp866_mbtowc; break;
88
		case CP1251: mbtowc=cp1251_mbtowc; break;
90
		case CP1251: mbtowc=cp1251_mbtowc; break;
89
		case CP1252: mbtowc=cp1252_mbtowc; break;
91
		case CP1252: mbtowc=cp1252_mbtowc; break;
90
		case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
92
		case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
91
		case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
93
		case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
92
		case UTF_8: mbtowc=utf8_mbtowc; break;
94
		case UTF_8: mbtowc=utf8_mbtowc; break;
93
		default: return -2;
95
		default: return -2;
94
	}
96
	}
95
		
97
		
96
	switch (to)
98
	switch (to)
97
	{
99
	{
98
		case CP866: wctomb=cp866_wctomb; break;
100
		case CP866: wctomb=cp866_wctomb; break;
99
		case CP1251: wctomb=cp1251_wctomb; break;
101
		case CP1251: wctomb=cp1251_wctomb; break;
100
		case CP1252: wctomb=cp1252_wctomb; break;
102
		case CP1252: wctomb=cp1252_wctomb; break;
101
		case ISO8859_5: wctomb=iso8859_5_wctomb; break;
103
		case ISO8859_5: wctomb=iso8859_5_wctomb; break;
102
		case KOI8_RU: wctomb=koi8_ru_wctomb; break;
104
		case KOI8_RU: wctomb=koi8_ru_wctomb; break;
103
		case UTF_8: wctomb=utf8_wctomb; break;
105
		case UTF_8: wctomb=utf8_wctomb; break;
104
		default: return -3;
106
		default: return -3;
105
	}
107
	}
106
	
108
	
107
	count1=0;
109
	count1=0;
108
	count2=0;
110
	count2=0;
109
		
111
		
110
	while ( *inbytesleft>0 && *outbytesleft>1)
112
	while ( *inbytesleft>0 && *outbytesleft>1)
111
	{
113
	{
112
		n=1;
114
		n=1;
113
		
115
		
114
		do {
116
		do {
115
		//converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
117
		//converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
116
		//	printf("%d\n",n);
118
		//	printf("%d\n",n);
117
		converted= (mbtowc)(0,&pwc,((*inbuf)+count1),n);
119
		converted= (mbtowc)(0,&pwc,((*inbuf)+count1),n);
118
		
120
		
119
		n++;
121
		n++;
120
		}	while (converted==RET_TOOFEW(0));
122
		}	while (converted==RET_TOOFEW(0));
121
		
123
		
122
		if (converted<0) return -10; 
124
		if (converted<0) return -10; 
123
		//written=  (cp866_wctomb)(0,str+count2,pwc,1);
125
		//written=  (cp866_wctomb)(0,str+count2,pwc,1);
124
		written=  (wctomb)(0,str+count2,pwc,1);
126
		written=  (wctomb)(0,str+count2,pwc,1);
125
		if (written<0) written=0;//return -11; 
127
		if (written<0) written=0;//return -11; 
126
				
128
				
127
		//printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
129
		//printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
128
		
130
		
129
		(*inbytesleft)-=converted;
131
		(*inbytesleft)-=converted;
130
		(*outbytesleft)-=written;
132
		(*outbytesleft)-=written;
131
		count1+=converted;
133
		count1+=converted;
132
		count2+=written;
134
		count2+=written;
133
	}
135
	}
134
	*(str+count2)='\0';
136
	*(str+count2)='\0';
135
			
137
			
136
	if (*inbytesleft>0 && *outbytesleft==0) return -12;
138
	if (*inbytesleft>0 && *outbytesleft==0) return -12;
137
	return 0;
139
	return 0;
138
}
140
}
139
 
141
 
140
 
142
 
141
/*
143
/*
142
int main()
144
int main()
143
{
145
{
144
	char *s;// ="вертолет";
146
	char *s;// ="вертолет";
145
	char *z;
147
	char *z;
146
	//unsigned int pwc;
148
	//unsigned int pwc;
147
	iconv_t cd;
149
	iconv_t cd;
148
	int in, out;
150
	int in, out;
149
	
151
	
150
		FILE *infile; 
152
		FILE *infile; 
151
		char *fname = "file.txt";
153
		char *fname = "file.txt";
152
	   
154
	   
153
		infile = fopen(fname,"r");
155
		infile = fopen(fname,"r");
154
	
156
	
155
	fseek(infile, 0, SEEK_END);
157
	fseek(infile, 0, SEEK_END);
156
	size_t file_size = ftell(infile);
158
	size_t file_size = ftell(infile);
157
	rewind(infile);
159
	rewind(infile);
158
 
160
 
159
	//printf ("LOL\n");
161
	//printf ("LOL\n");
160
 
162
 
161
	char *buffer = (char*)malloc(file_size * sizeof(char));
163
	char *buffer = (char*)malloc(file_size * sizeof(char));
162
	if (buffer == NULL)
164
	if (buffer == NULL)
163
	{
165
	{
164
		fclose(infile);
166
		fclose(infile);
165
		printf("Error allocating %d bytes.\n", file_size * sizeof(char));
167
		printf("Error allocating %d bytes.\n", file_size * sizeof(char));
166
		return -1;
168
		return -1;
167
	}
169
	}
168
	size_t bytes_read = fread(buffer, sizeof(char), file_size, infile);
170
	size_t bytes_read = fread(buffer, sizeof(char), file_size, infile);
169
	if (bytes_read != file_size)
171
	if (bytes_read != file_size)
170
	{
172
	{
171
		printf("Have read only %d bytes of %d.\n", bytes_read, file_size);
173
		printf("Have read only %d bytes of %d.\n", bytes_read, file_size);
172
		free(buffer);
174
		free(buffer);
173
		fclose(infile);
175
		fclose(infile);
174
		return -1;
176
		return -1;
175
	}
177
	}
176
	
178
	
177
	in=strlen(buffer);
179
	in=strlen(buffer);
178
	z=malloc(in+1);
180
	z=malloc(in+1);
179
	
181
	
180
	out=in+1;
182
	out=in+1;
181
	cd=iconv_open("CP1251","CP866");
183
	cd=iconv_open("CP1251","CP866");
182
//	printf("%x\n",cd);
184
//	printf("%x\n",cd);
183
	int t;
185
	int t;
184
	t=iconv(cd, &buffer, &in, &z, &out);
186
	t=iconv(cd, &buffer, &in, &z, &out);
185
	printf("\nResult: %d", t);
187
	printf("\nResult: %d", t);
186
	puts(z);
188
	puts(z);
187
	//for (;s
189
	//for (;s
188
}
190
}
189
*/
191
*/
190
 
192
 
191
 
193
 
192
typedef struct
194
typedef struct
193
{
195
{
194
	char *name;
196
	char *name;
195
	void *f;
197
	void *f;
196
} export_t;
198
} export_t;
197
 
199
 
198
char szStart[]           = "START";
200
char szStart[]           = "START";
199
char szVersion[]         = "version";
201
char szVersion[]         = "version";
200
char sziconv_open[]    = "iconv_open";
202
char sziconv_open[]    = "iconv_open";
201
char sziconv[]   = "iconv";
203
char sziconv[]   = "iconv";
202
 
204
 
203
export_t EXPORTS[] __asm__("EXPORTS") =
205
export_t EXPORTS[] __asm__("EXPORTS") =
204
{
206
{
205
	{ szStart,       (void*)0x0 },
207
	{ szStart,       (void*)0x0 },
206
	{ szVersion,     (void*)0x00010001 },
208
	{ szVersion,     (void*)0x00010001 },
207
	{ sziconv_open,  iconv_open    },
209
	{ sziconv_open,  iconv_open    },
208
	{ sziconv,       iconv   },
210
	{ sziconv,       iconv   },
209
	{ NULL,          NULL },
211
	{ NULL,          NULL },
210
};
212
};
211
>
213
>
212
>
214
>