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