Subversion Repositories Kolibri OS

Rev

Rev 7079 | Rev 7085 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7079 Rev 7084
1
#include 
1
#include 
2
#include 
2
#include 
3
#include 
3
#include 
4
 
4
 
5
typedef int conv_t;
5
typedef int conv_t;
6
typedef unsigned int ucs4_t;
6
typedef unsigned int ucs4_t;
7
typedef int iconv_t;
7
typedef int iconv_t;
8
 
8
 
9
/* Return code if invalid input after a shift sequence of n bytes was read.
9
/* Return code if invalid input after a shift sequence of n bytes was read.
10
   (xxx_mbtowc) */
10
   (xxx_mbtowc) */
11
#define RET_SHIFT_ILSEQ(n)  (-1-2*(n))
11
#define RET_SHIFT_ILSEQ(n)  (-1-2*(n))
12
/* Return code if invalid. (xxx_mbtowc) */
12
/* Return code if invalid. (xxx_mbtowc) */
13
#define RET_ILSEQ           RET_SHIFT_ILSEQ(0)
13
#define RET_ILSEQ           RET_SHIFT_ILSEQ(0)
14
/* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
14
/* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
15
#define RET_TOOFEW(n)       (-2-2*(n))
15
#define RET_TOOFEW(n)       (-2-2*(n))
16
 
16
 
17
/* Return code if invalid. (xxx_wctomb) */
17
/* Return code if invalid. (xxx_wctomb) */
18
#define RET_ILUNI      -1
18
#define RET_ILUNI      -1
19
/* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
19
/* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
20
#define RET_TOOSMALL   -2
20
#define RET_TOOSMALL   -2
21
 
21
 
22
#define CP866 0
22
#define CP866 0
23
#define CP1251 1
23
#define CP1251 1
24
#define CP1252 2
24
#define CP1252 2
25
#define KOI8_RU 3
25
#define KOI8_RU 3
26
#define ISO8859_5 4
26
#define ISO8859_5 4
27
#define UTF_8 5
27
#define UTF_8 5
-
 
28
#define KOI8_R 6
28
 
29
 
29
#include "cp866.h"
30
#include "cp866.h"
30
#include "cp1251.h"
31
#include "cp1251.h"
31
#include "cp1252.h"
32
#include "cp1252.h"
-
 
33
#include "koi8_r.h"
32
#include "koi8_ru.h"
34
#include "koi8_ru.h"
33
#include "iso8859_5.h"
35
#include "iso8859_5.h"
34
#include "utf8.h"
36
#include "utf8.h"
35
 
37
 
36
int encoding(const char *someencoding) {
38
int encoding(const char *someencoding) {
37
 
39
 
38
	char *what = strdup(someencoding);
40
	char *what = strdup(someencoding);
39
	/* Ignore //TRANSLIT or //IGNORE for now. */
41
	/* Ignore //TRANSLIT or //IGNORE for now. */
40
	int i;
42
	int i;
41
	for(i = 0; i < strlen(what); i++) {
43
	for(i = 0; i < strlen(what); i++) {
42
	  if(what[i] == '/') {
44
	  if(what[i] == '/') {
43
		what[i] = '\0';
45
		what[i] = '\0';
44
		break;
46
		break;
45
	  }
47
	  }
46
	}
48
	}
47
 
49
 
48
	if (!strcasecmp(what,"CP866")) return CP866;
50
	if (!strcasecmp(what,"CP866")) return CP866;
49
	if (!strcasecmp(what,"CP1251")) return CP1251;
51
	if (!strcasecmp(what,"CP1251")) return CP1251;
50
	if (!strcasecmp(what,"windows-1251")) return CP1251;
52
	if (!strcasecmp(what,"windows-1251")) return CP1251;
51
	if (!strcasecmp(what,"windows-1252")) return CP1252;
-
 
52
	if (!strcasecmp(what,"CP1252")) return CP1252;
53
	if (!strcasecmp(what,"CP1252")) return CP1252;
-
 
54
	if (!strcasecmp(what,"windows-1252")) return CP1252;	
-
 
55
	if (!strcasecmp(what,"KOI8-R")) return KOI8_R;	
53
	if (!strcasecmp(what,"KOI8-RU")) return KOI8_RU;
56
	if (!strcasecmp(what,"KOI8-RU")) return KOI8_RU;
54
	if (!strcasecmp(what,"KOI8-R")) return KOI8_RU;
-
 
55
	if (!strcasecmp(what,"ISO8859-5")) return ISO8859_5;
57
	if (!strcasecmp(what,"ISO8859-5")) return ISO8859_5;
56
	if (!strcasecmp(what,"UTF-8")) return UTF_8;
58
	if (!strcasecmp(what,"UTF-8")) return UTF_8;
57
	return -1;
59
	return -1;
58
}
60
}
59
 
61
 
60
iconv_t iconv_open(const char *tocode, const char *fromcode) {
62
iconv_t iconv_open(const char *tocode, const char *fromcode) {
61
	int to, from;
63
	int to, from;
62
 
64
 
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
 
67
 
66
	to=to<<16&0xFFFF0000;
68
	to=to<<16&0xFFFF0000;
67
	from=from&0xFFFF;
69
	from=from&0xFFFF;
68
 
70
 
69
	return to+from;
71
	return to+from;
70
}
72
}
71
 
73
 
72
int iconv_close(iconv_t icd)
74
int iconv_close(iconv_t icd)
73
{
75
{
74
  return 0;
76
  return 0;
75
}
77
}
76
 
78
 
77
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
79
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
78
	char **outbuf, size_t *outbytesleft)
80
	char **outbuf, size_t *outbytesleft)
79
{
81
{
80
	int n, to, from;
82
	int n, to, from;
81
	size_t count1,count2;
83
	size_t count1,count2;
82
	unsigned int pwc;
84
	unsigned int pwc;
83
	int converted,written;
85
	int converted,written;
84
	int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
86
	int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
85
	int (*wctomb)(conv_t, unsigned char *, ucs4_t, int);
87
	int (*wctomb)(conv_t, unsigned char *, ucs4_t, int);
86
 
88
 
87
	to=cd>>16;
89
	to=cd>>16;
88
	from=cd&0xFFFF;
90
	from=cd&0xFFFF;
89
 
91
 
90
	switch (from)
92
	switch (from)
91
	{
93
	{
92
		case CP866: mbtowc=cp866_mbtowc; break;
94
		case CP866: mbtowc=cp866_mbtowc; break;
93
		case CP1251: mbtowc=cp1251_mbtowc; break;
95
		case CP1251: mbtowc=cp1251_mbtowc; break;
94
		case CP1252: mbtowc=cp1252_mbtowc; break;
96
		case CP1252: mbtowc=cp1252_mbtowc; break;
95
		case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
97
		case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
-
 
98
		case KOI8_R: mbtowc=koi8_r_mbtowc; break;		
96
		case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
99
		case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
97
		case UTF_8: mbtowc=utf8_mbtowc; break;
100
		case UTF_8: mbtowc=utf8_mbtowc; break;
98
		default: return (size_t)-1;
101
		default: return (size_t)-1;
99
	}
102
	}
100
 
103
 
101
	switch (to)
104
	switch (to)
102
	{
105
	{
103
		case CP866: wctomb=cp866_wctomb; break;
106
		case CP866: wctomb=cp866_wctomb; break;
104
		case CP1251: wctomb=cp1251_wctomb; break;
107
		case CP1251: wctomb=cp1251_wctomb; break;
105
		case CP1252: wctomb=cp1252_wctomb; break;
108
		case CP1252: wctomb=cp1252_wctomb; break;
106
		case ISO8859_5: wctomb=iso8859_5_wctomb; break;
109
		case ISO8859_5: wctomb=iso8859_5_wctomb; break;
-
 
110
		case KOI8_R: wctomb=koi8_r_wctomb; break;		
107
		case KOI8_RU: wctomb=koi8_ru_wctomb; break;
111
		case KOI8_RU: wctomb=koi8_ru_wctomb; break;
108
		case UTF_8: wctomb=utf8_wctomb; break;
112
		case UTF_8: wctomb=utf8_wctomb; break;
109
		default: return (size_t)-1;
113
		default: return (size_t)-1;
110
	}
114
	}
111
 
115
 
112
	count1=0;
116
	count1=0;
113
	count2=0;
117
	count2=0;
114
 
118
 
115
    /* Convert input multibyte char to wide character by using calls to mbtowc */
119
    /* Convert input multibyte char to wide character by using calls to mbtowc */
116
    /* Convert wide character to multibyte by calls to wctomb */
120
    /* Convert wide character to multibyte by calls to wctomb */
117
    /* Handle errors as we go on converting to be as standard compliant as possible */
121
    /* Handle errors as we go on converting to be as standard compliant as possible */
118
    while(count1 < *inbytesleft) {
122
    while(count1 < *inbytesleft) {
119
      unsigned char mbholder[] = { 0,0,0,0,0,0 };
123
      unsigned char mbholder[] = { 0,0,0,0,0,0 };
120
 
124
 
121
      int numbytes = (mbtowc)(0, &pwc,((*inbuf)+count1), *inbytesleft - count1);
125
      int numbytes = (mbtowc)(0, &pwc,((*inbuf)+count1), *inbytesleft - count1);
122
      if(numbytes < 0) {
126
      if(numbytes < 0) {
123
        /* errno = EILSEQ if invalid multibyte sequence encountered in input */
127
        /* errno = EILSEQ if invalid multibyte sequence encountered in input */
124
        /* errno = EINVAL if input ends in the middle of a multibyte sequence */
128
        /* errno = EINVAL if input ends in the middle of a multibyte sequence */
125
 
129
 
126
        switch(numbytes) {
130
        switch(numbytes) {
127
          case RET_TOOFEW(0):
131
          case RET_TOOFEW(0):
128
            errno = EINVAL;
132
            errno = EINVAL;
129
            break;
133
            break;
130
 
134
 
131
          case RET_ILSEQ:
135
          case RET_ILSEQ:
132
            errno = EILSEQ;
136
            errno = EILSEQ;
133
            break;
137
            break;
134
        }
138
        }
135
 
139
 
136
        *inbytesleft -= count1;
140
        *inbytesleft -= count1;
137
        *outbytesleft -= count2;
141
        *outbytesleft -= count2;
138
        *inbuf += count1;
142
        *inbuf += count1;
139
        *outbuf += count2;
143
        *outbuf += count2;
140
        return (size_t) -1;
144
        return (size_t) -1;
141
      }
145
      }
142
 
146
 
143
      /* Convert from wide to multibyte storing result in mbholder and num converted in numbytes2 */
147
      /* Convert from wide to multibyte storing result in mbholder and num converted in numbytes2 */
144
      /* Pass the minimum amount of space we have, one from mbholder and one from remaining in outbuf */
148
      /* Pass the minimum amount of space we have, one from mbholder and one from remaining in outbuf */
145
      int minspace = sizeof(mbholder) <= (*outbytesleft - count2) ? sizeof(mbholder) : (*outbytesleft - count2);
149
      int minspace = sizeof(mbholder) <= (*outbytesleft - count2) ? sizeof(mbholder) : (*outbytesleft - count2);
146
 
150
 
147
      int numbytes2 = (wctomb)(0, &mbholder[0], pwc, minspace);
151
      int numbytes2 = (wctomb)(0, &mbholder[0], pwc, minspace);
148
      if(numbytes2 < 0) {
152
      if(numbytes2 < 0) {
149
        switch(numbytes2) {
153
        switch(numbytes2) {
150
          case RET_ILUNI:
154
          case RET_ILUNI:
151
            errno = EILSEQ;
155
            errno = EILSEQ;
152
            break;
156
            break;
153
          case RET_TOOSMALL:
157
          case RET_TOOSMALL:
154
            errno = E2BIG;
158
            errno = E2BIG;
155
            break;
159
            break;
156
        }
160
        }
157
 
161
 
158
        *inbytesleft -= count1;
162
        *inbytesleft -= count1;
159
        *outbytesleft -= count2;
163
        *outbytesleft -= count2;
160
        *inbuf += count1;
164
        *inbuf += count1;
161
        *outbuf += count2;
165
        *outbuf += count2;
162
 
166
 
163
        return (size_t) -1;
167
        return (size_t) -1;
164
      }
168
      }
165
 
169
 
166
      int i;
170
      int i;
167
      for(i = 0; i < numbytes2; i++) {
171
      for(i = 0; i < numbytes2; i++) {
168
        *(*outbuf + count2 + i) = mbholder[i];
172
        *(*outbuf + count2 + i) = mbholder[i];
169
      }
173
      }
170
 
174
 
171
      count1+=numbytes;
175
      count1+=numbytes;
172
      count2+=numbytes2;
176
      count2+=numbytes2;
173
    }
177
    }
174
 
178
 
175
    /* Successfully converted everything, update the variables and return number of bytes converted */
179
    /* Successfully converted everything, update the variables and return number of bytes converted */
176
    *inbytesleft -= count1;
180
    *inbytesleft -= count1;
177
    *outbytesleft -= count2;
181
    *outbytesleft -= count2;
178
    *inbuf += count1;
182
    *inbuf += count1;
179
    *outbuf += count2;
183
    *outbuf += count2;
180
 
184
 
181
    return count1;
185
    return count1;
182
}
186
}
183
 
187
 
184
/* int main() */
188
/* int main() */
185
/* { */
189
/* { */
186
/* 	char *s;// ="вертолет"; */
190
/* 	char *s;// ="вертолет"; */
187
/* 	char *z; */
191
/* 	char *z; */
188
/* 	//unsigned int pwc; */
192
/* 	//unsigned int pwc; */
189
/* 	iconv_t cd; */
193
/* 	iconv_t cd; */
190
/* 	size_t in, out; */
194
/* 	size_t in, out; */
191
 
195
 
192
/*     FILE *infile; */
196
/*     FILE *infile; */
193
/*     char *fname = "file3.txt"; */
197
/*     char *fname = "file3.txt"; */
194
 
198
 
195
/*     size_t testmax = 100; */
199
/*     size_t testmax = 100; */
196
/*     size_t test = 0; */
200
/*     size_t test = 0; */
197
 
201
 
198
/*     infile = fopen(fname,"r"); */
202
/*     infile = fopen(fname,"r"); */
199
 
203
 
200
/* 	fseek(infile, 0, SEEK_END); */
204
/* 	fseek(infile, 0, SEEK_END); */
201
/* 	size_t file_size = ftell(infile); */
205
/* 	size_t file_size = ftell(infile); */
202
/* 	rewind(infile); */
206
/* 	rewind(infile); */
203
 
207
 
204
/* 	char *buffer = (char*)malloc(file_size * sizeof(char)); */
208
/* 	char *buffer = (char*)malloc(file_size * sizeof(char)); */
205
/* 	if (buffer == NULL) */
209
/* 	if (buffer == NULL) */
206
/* 	{ */
210
/* 	{ */
207
/* 		fclose(infile); */
211
/* 		fclose(infile); */
208
/* 		printf("Error allocating %d bytes.\n", file_size * sizeof(char)); */
212
/* 		printf("Error allocating %d bytes.\n", file_size * sizeof(char)); */
209
/* 		return -1; */
213
/* 		return -1; */
210
/* 	} */
214
/* 	} */
211
/* 	size_t bytes_read = fread(buffer, sizeof(char), file_size, infile); */
215
/* 	size_t bytes_read = fread(buffer, sizeof(char), file_size, infile); */
212
/* 	if (bytes_read != file_size) */
216
/* 	if (bytes_read != file_size) */
213
/* 	{ */
217
/* 	{ */
214
/* 		/\* printf("Have read only %d bytes of %d.\n", bytes_read, file_size); *\/ */
218
/* 		/\* printf("Have read only %d bytes of %d.\n", bytes_read, file_size); *\/ */
215
/* 		free(buffer); */
219
/* 		free(buffer); */
216
/* 		fclose(infile); */
220
/* 		fclose(infile); */
217
/* 		return -1; */
221
/* 		return -1; */
218
/* 	} */
222
/* 	} */
219
 
223
 
220
/* 	/\* in=strlen(buffer); *\/ */
224
/* 	/\* in=strlen(buffer); *\/ */
221
/*     in = bytes_read; */
225
/*     in = bytes_read; */
222
/* 	z=malloc(in+12000); */
226
/* 	z=malloc(in+12000); */
223
 
227
 
224
/* 	out=in-1000; */
228
/* 	out=in-1000; */
225
/* 	cd=iconv_open("UTF-8","UTF-8"); */
229
/* 	cd=iconv_open("UTF-8","UTF-8"); */
226
/* //	printf("%x\n",cd); */
230
/* //	printf("%x\n",cd); */
227
/* 	int t; */
231
/* 	int t; */
228
/*     char *zor = z; */
232
/*     char *zor = z; */
229
 
233
 
230
/*     /\* for(t = 0; t < 27400; t++) *\/ */
234
/*     /\* for(t = 0; t < 27400; t++) *\/ */
231
/*     /\*    printf("0x%x,", buffer[t]); *\/ */
235
/*     /\*    printf("0x%x,", buffer[t]); *\/ */
232
 
236
 
233
/* 	t=iconv(cd, &buffer, &in, &z, &out); */
237
/* 	t=iconv(cd, &buffer, &in, &z, &out); */
234
/* 	/\* printf("\nResult after iconv(): %d", t); *\/ */
238
/* 	/\* printf("\nResult after iconv(): %d", t); *\/ */
235
 
239
 
236
/*     /\* for(t = 0; t < 24259; t++) *\/ */
240
/*     /\* for(t = 0; t < 24259; t++) *\/ */
237
/*     /\*   printf("%c", zor[t]); *\/ */
241
/*     /\*   printf("%c", zor[t]); *\/ */
238
 
242
 
239
/*  	//for (;s
243
/*  	//for (;s
240
/* } */
244
/* } */
241
 
245
 
242
/* typedef struct */
246
/* typedef struct */
243
/* { */
247
/* { */
244
/* 	char *name; */
248
/* 	char *name; */
245
/* 	void *f; */
249
/* 	void *f; */
246
/* } export_t; */
250
/* } export_t; */
247
 
251
 
248
/* char szStart[]           = "START"; */
252
/* char szStart[]           = "START"; */
249
/* char szVersion[]         = "version"; */
253
/* char szVersion[]         = "version"; */
250
/* char sziconv_open[]    = "iconv_open"; */
254
/* char sziconv_open[]    = "iconv_open"; */
251
/* char sziconv[]   = "iconv"; */
255
/* char sziconv[]   = "iconv"; */
252
 
256
 
253
/* export_t EXPORTS[] __asm__("EXPORTS") = */
257
/* export_t EXPORTS[] __asm__("EXPORTS") = */
254
/* { */
258
/* { */
255
/* 	{ szStart,       (void*)0x0 }, */
259
/* 	{ szStart,       (void*)0x0 }, */
256
/* 	{ szVersion,     (void*)0x00010001 }, */
260
/* 	{ szVersion,     (void*)0x00010001 }, */
257
/* 	{ sziconv_open,  iconv_open    }, */
261
/* 	{ sziconv_open,  iconv_open    }, */
258
/* 	{ sziconv,       iconv   }, */
262
/* 	{ sziconv,       iconv   }, */
259
/* 	{ NULL,          NULL }, */
263
/* 	{ NULL,          NULL }, */
260
/* }; */
264
/* }; */
261
>
265
>
262
>
266
>