10
|
1 /* nkf32.dll nfk32dll.c */
|
|
2 // e-mail:tkaneto@nifty.com
|
|
3 // URL: http://www1.ttcn.ne.jp/~kaneto
|
|
4
|
|
5 /*WIN32DLL*/
|
|
6 /* こちらのバージョンも更新してください。 */
|
|
7 #define NKF_VERSIONW L"2.0.5"
|
|
8 /* NKF_VERSION のワイド文字 */
|
|
9 #define DLL_VERSION "2.0.5.0 2"
|
|
10 /* DLLが返す */
|
|
11 #define DLL_VERSIONW L"2.0.5.0 2"
|
|
12 /* DLLが返す DLL_VERSION のワイド文字 */
|
|
13
|
|
14 /* nkf32.dll main */
|
|
15 #include <windows.h>
|
|
16 #include <tchar.h>
|
|
17 #include <stdarg.h>
|
|
18
|
|
19 #ifdef DLLDBG /* DLLDBG @@*/
|
|
20 #include "nkf.h"
|
|
21
|
|
22 void dumpn(unsigned char *buff,unsigned n)
|
|
23 {
|
|
24 int i;
|
|
25
|
|
26 i = 0;
|
|
27 while ( n ) {
|
|
28 if ( i == 0 ) {
|
|
29 printf(":%x ",buff);
|
|
30 }
|
|
31 printf("%02x ",*buff++);
|
|
32 i++;
|
|
33 if ( i == 16 ) {
|
|
34 printf("\n");
|
|
35 i = 0;
|
|
36 }
|
|
37 n--;
|
|
38 }
|
|
39 printf("\n");
|
|
40 }
|
|
41
|
|
42 void dumpf(char *f);
|
|
43 void mkfile(char *f,char *p);
|
|
44 #endif /* DLLDBG @@*/
|
|
45
|
|
46 #ifndef GUESS
|
|
47 #define GUESS 64
|
|
48 #endif /*GUESS*/
|
|
49
|
|
50 char *guessbuffA = NULL;
|
|
51 #ifdef UNICODESUPPORT
|
|
52 wchar_t *guessbuffW = NULL;
|
|
53 UINT guessCodePage = CP_OEMCP;
|
|
54 DWORD guessdwFlags = MB_PRECOMPOSED;
|
|
55
|
|
56 wchar_t *tounicode(char *p)
|
|
57 {
|
|
58 static wchar_t buff[GUESS];
|
|
59 int sts;
|
|
60
|
|
61 sts = MultiByteToWideChar(guessCodePage,guessdwFlags,p,-1,buff,sizeof(buff) / sizeof(wchar_t));
|
|
62 if ( sts ) {
|
|
63 return buff;
|
|
64 } else {
|
|
65 return L"(NULL)";
|
|
66 }
|
|
67 }
|
|
68 #endif /*UNICODESUPPORT*/
|
|
69
|
|
70 char *ubuff;
|
|
71 int ulen;
|
|
72 int uret;
|
|
73
|
|
74 int dllprintf(FILE *fp,char *fmt,...)
|
|
75 {
|
|
76 va_list argp;
|
|
77 int sts;
|
|
78
|
|
79 if ( uret != FALSE && ulen >= 1 && fmt != NULL && *fmt != 0 ) {
|
|
80 va_start(argp, fmt);
|
|
81 sts = _vsnprintf(ubuff,ulen - 1,fmt,argp);
|
|
82 va_end(argp);
|
|
83 if ( sts >= 0 ) {
|
|
84 ubuff += sts;
|
|
85 ulen -= sts;
|
|
86 } else {
|
|
87 uret = FALSE;
|
|
88 }
|
|
89 return sts;
|
|
90 } else return 0;
|
|
91 }
|
|
92
|
|
93 /** Network Kanji Filter. (PDS Version)
|
|
94 ************************************************************************
|
|
95 ** Copyright (C) 1987, Fujitsu LTD. (Itaru ICHIKAWA)
|
|
96 ** 連絡先: (株)富士通研究所 ソフト3研 市川 至
|
|
97 ** (E-Mail Address: ichikawa@flab.fujitsu.co.jp)
|
|
98 ** Copyright (C) 1996,1998
|
|
99 ** Copyright (C) 2002
|
|
100 ** 連絡先: 琉球大学情報工学科 河野 真治 mime/X0208 support
|
|
101 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
|
|
102 ** 連絡先: COW for DOS & Win16 & Win32 & OS/2
|
|
103 ** (E-Mail Address: GHG00637@niftyserve.or.p)
|
|
104 **
|
|
105 ** このソースのいかなる複写,改変,修正も許諾します。ただし、
|
|
106 ** その際には、誰が貢献したを示すこの部分を残すこと。
|
|
107 ** 再配布や雑誌の付録などの問い合わせも必要ありません。
|
|
108 ** 営利利用も上記に反しない範囲で許可します。
|
|
109 ** バイナリの配布の際にはversion messageを保存することを条件とします。
|
|
110 ** このプログラムについては特に何の保証もしない、悪しからず。
|
|
111 **
|
|
112 ** Everyone is permitted to do anything on this program
|
|
113 ** including copying, modifying, improving,
|
|
114 ** as long as you don't try to pretend that you wrote it.
|
|
115 ** i.e., the above copyright notice has to appear in all copies.
|
|
116 ** Binary distribution requires original version messages.
|
|
117 ** You don't have to ask before copying, redistribution or publishing.
|
|
118 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
|
|
119 ***********************************************************************/
|
|
120
|
|
121 static const unsigned char *cin = NULL;
|
|
122 static int nin = -1;
|
|
123 static int ninmax = -1;
|
|
124 static int std_getc_mode = 1;
|
|
125
|
|
126 int
|
|
127 std_getc(f)
|
|
128 FILE *f;
|
|
129 {
|
|
130 if (std_gc_ndx){
|
|
131 return std_gc_buf[--std_gc_ndx];
|
|
132 } else {
|
|
133 if ( std_getc_mode == 1 ) {
|
|
134 return getc(f);
|
|
135 }
|
|
136 if ( std_getc_mode == 2 && cin != NULL ) {
|
|
137 if ( ninmax >= 0 ) {
|
|
138 if ( nin >= ninmax ) {
|
|
139 return EOF;
|
|
140 } else {
|
|
141 nin++;
|
|
142 return *cin++;
|
|
143 }
|
|
144 } else {
|
|
145 if ( *cin ) {
|
|
146 return *cin++;
|
|
147 } else {
|
|
148 return EOF;
|
|
149 }
|
|
150 }
|
|
151 }
|
|
152 }
|
|
153 return EOF;
|
|
154 }
|
|
155
|
|
156 static FILE *fout = stdout;
|
|
157 static unsigned char *cout = NULL;
|
|
158 static int nout = -1;
|
|
159 static int noutmax = -1;
|
|
160 static int std_putc_mode = 1;
|
|
161
|
|
162 void
|
|
163 std_putc(c)
|
|
164 int c;
|
|
165 {
|
|
166 if(c!=EOF)
|
|
167 {
|
|
168 if ( (std_putc_mode & 1) && fout != NULL ) {
|
|
169 putc(c,fout);
|
|
170 }
|
|
171 if ( (std_putc_mode & 4) && nout != -1 ) {
|
|
172 if ( noutmax >= 0 && nout >= noutmax ) std_putc_mode &= ~2;
|
|
173 nout++;
|
|
174 }
|
|
175 if ( (std_putc_mode & 2) && cout != NULL ) {
|
|
176 *cout++ = c;
|
|
177 }
|
|
178 }
|
|
179 }
|
|
180
|
|
181 void
|
|
182 print_guessed_code (filename)
|
|
183 char *filename;
|
|
184 {
|
|
185 char *codename = "BINARY";
|
|
186 if (!is_inputcode_mixed) {
|
|
187 if (strcmp(input_codename, "") == 0) {
|
|
188 codename = "ASCII";
|
|
189 } else {
|
|
190 codename = input_codename;
|
|
191 }
|
|
192 }
|
|
193 if (filename != NULL) {
|
|
194 guessbuffA = realloc(guessbuffA,(strlen(filename) + GUESS + 1) * sizeof (char) );
|
|
195 sprintf(guessbuffA,"%s:%s", filename,codename);
|
|
196 } else {
|
|
197 guessbuffA = realloc(guessbuffA,(GUESS + 1) * sizeof (char) );
|
|
198 sprintf(guessbuffA,"%s", codename);
|
|
199 }
|
|
200 }
|
|
201
|
|
202 #ifdef UNICODESUPPORT
|
|
203 void
|
|
204 print_guessed_codeW (filename)
|
|
205 wchar_t *filename;
|
|
206 {
|
|
207 char *codename = "BINARY";
|
|
208 if (!is_inputcode_mixed) {
|
|
209 if (strcmp(input_codename, "") == 0) {
|
|
210 codename = "ASCII";
|
|
211 } else {
|
|
212 codename = input_codename;
|
|
213 }
|
|
214 }
|
|
215 if (filename != NULL) {
|
|
216 guessbuffW = realloc(guessbuffW,(wcslen(filename) + GUESS + 1) * sizeof (wchar_t) );
|
|
217 swprintf(guessbuffW,L"%s:%s",filename,tounicode(codename));
|
|
218 } else {
|
|
219 guessbuffW = realloc(guessbuffW,(GUESS + 1) * sizeof (wchar_t));
|
|
220 swprintf(guessbuffW,L"%s",tounicode(codename));
|
|
221 }
|
|
222 }
|
|
223 #endif /*UNICODESUPPORT*/
|
|
224
|
|
225 /**
|
|
226 ** パッチ制作者
|
|
227 ** void@merope.pleiades.or.jp (Kusakabe Youichi)
|
|
228 ** NIDE Naoyuki <nide@ics.nara-wu.ac.jp>
|
|
229 ** ohta@src.ricoh.co.jp (Junn Ohta)
|
|
230 ** inouet@strl.nhk.or.jp (Tomoyuki Inoue)
|
|
231 ** kiri@pulser.win.or.jp (Tetsuaki Kiriyama)
|
|
232 ** Kimihiko Sato <sato@sail.t.u-tokyo.ac.jp>
|
|
233 ** a_kuroe@kuroe.aoba.yokohama.jp (Akihiko Kuroe)
|
|
234 ** kono@ie.u-ryukyu.ac.jp (Shinji Kono)
|
|
235 ** GHG00637@nifty-serve.or.jp (COW)
|
|
236 **
|
|
237 **/
|
|
238
|
|
239 void
|
|
240 reinitdll()
|
|
241 {
|
|
242 cin = NULL;
|
|
243 nin = -1;
|
|
244 ninmax = -1;
|
|
245 std_getc_mode = 1;
|
|
246 fout = stdout;
|
|
247 cout = NULL;
|
|
248 nout = -1;
|
|
249 noutmax = -1;
|
|
250 std_putc_mode = 1;
|
|
251 if ( guessbuffA ) {
|
|
252 free(guessbuffA);
|
|
253 guessbuffA = NULL;
|
|
254 }
|
|
255 #ifdef UNICODESUPPORT
|
|
256 if ( guessbuffW ) {
|
|
257 free(guessbuffW);
|
|
258 guessbuffW = NULL;
|
|
259 }
|
|
260 #endif /*UNICODESUPPORT*/
|
|
261 }
|
|
262
|
|
263 #ifndef DLLDBG /* DLLDBG @@*/
|
|
264 int WINAPI DllEntryPoint(HINSTANCE hinst,unsigned long reason,void* lpReserved)
|
|
265 {
|
|
266 return 1;
|
|
267 }
|
|
268 #endif /* DLLDBG @@*/
|
|
269
|
|
270 static LPSTR nkfverA = NKF_VERSION;
|
|
271 static LPSTR dllverA = DLL_VERSION;
|
|
272 #ifdef UNICODESUPPORT
|
|
273 static LPWSTR nkfverW = NKF_VERSIONW;
|
|
274 static LPWSTR dllverW = DLL_VERSIONW;
|
|
275 #endif /*UNICODESUPPORT*/
|
|
276
|
|
277 BOOL scp(LPSTR s,LPSTR t,DWORD n)
|
|
278 {
|
|
279 while ( n ) {
|
|
280 if ( (*s = *t) == 0 ) return TRUE;
|
|
281 if ( --n == 0 ) {
|
|
282 *s = 0;
|
|
283 break;
|
|
284 }
|
|
285 s++;
|
|
286 t++;
|
|
287 }
|
|
288 return FALSE;
|
|
289 }
|
|
290
|
|
291 #ifdef UNICODESUPPORT
|
|
292 BOOL wscp(LPWSTR s,LPWSTR t,DWORD n)
|
|
293 {
|
|
294 while ( n ) {
|
|
295 if ( (*s = *t) == 0 ) return TRUE;
|
|
296 if ( --n == 0 ) {
|
|
297 *s = 0;
|
|
298 break;
|
|
299 }
|
|
300 s++;
|
|
301 t++;
|
|
302 }
|
|
303 return FALSE;
|
|
304 }
|
|
305 #endif /*UNICODESUPPORT*/
|
|
306
|
|
307 void CALLBACK GetNkfVersion(LPSTR verStr){
|
|
308 strcpy(verStr,dllverA);
|
|
309 }
|
|
310
|
|
311 BOOL WINAPI GetNkfVersionSafeA(LPSTR verStr,DWORD nBufferLength /*in TCHARs*/,LPDWORD lpTCHARsReturned /*in TCHARs*/)
|
|
312 {
|
|
313 *lpTCHARsReturned = strlen(dllverA) + 1;
|
|
314 if ( verStr == NULL || nBufferLength == 0 ) return FALSE;
|
|
315 return scp(verStr,dllverA,nBufferLength);
|
|
316 }
|
|
317
|
|
318 BOOL WINAPI GetNkfVersionSafeW(LPWSTR verStr,DWORD nBufferLength /*in TCHARs*/,LPDWORD lpTCHARsReturned /*in TCHARs*/)
|
|
319 {
|
|
320 #ifdef UNICODESUPPORT
|
|
321 *lpTCHARsReturned = wcslen(dllverW) + 1;
|
|
322 if ( verStr == NULL || nBufferLength == 0 ) return FALSE;
|
|
323 wcsncpy(verStr,dllverW,nBufferLength);
|
|
324 if ( wcslen(dllverW) >= nBufferLength ) {
|
|
325 *(verStr + nBufferLength - 1) = 0;
|
|
326 return FALSE;
|
|
327 }
|
|
328 return TRUE;
|
|
329 #else /*UNICODESUPPORT*/
|
|
330 return FALSE;
|
|
331 #endif /*UNICODESUPPORT*/
|
|
332 }
|
|
333
|
|
334 int CALLBACK SetNkfOption(LPCSTR optStr)
|
|
335 {
|
|
336 LPSTR p;
|
|
337
|
|
338 if ( *optStr == '-' ) {
|
|
339 reinit();
|
|
340 options(optStr);
|
|
341 } else {
|
|
342 p = malloc(strlen(optStr) + 2);
|
|
343 if ( p == NULL ) return -1;
|
|
344 *p = '-';
|
|
345 strcpy(p + 1,optStr);
|
|
346 reinit();
|
|
347 options(p);
|
|
348 free(p);
|
|
349 }
|
|
350 return 0;
|
|
351 }
|
|
352
|
|
353 void CALLBACK NkfConvert(LPSTR outStr, LPCSTR inStr)
|
|
354 {
|
|
355 std_putc_mode = 2;
|
|
356 cout = outStr;
|
|
357 noutmax = -1;
|
|
358 nout = -1;
|
|
359 std_getc_mode = 2;
|
|
360 cin = inStr;
|
|
361 ninmax = -1;
|
|
362 nin = -1;
|
|
363 kanji_convert(NULL);
|
|
364 *cout = 0;
|
|
365 }
|
|
366
|
|
367 BOOL WINAPI NkfConvertSafe(LPSTR outStr,DWORD nOutBufferLength /*in Bytes*/,LPDWORD lpBytesReturned /*in Bytes*/, LPCSTR inStr,DWORD nInBufferLength /*in Bytes*/){
|
|
368 if ( inStr == NULL ) return FALSE;
|
|
369 std_putc_mode = 6;
|
|
370 cout = outStr;
|
|
371 noutmax = nOutBufferLength;
|
|
372 nout = 0;
|
|
373 std_getc_mode = 2;
|
|
374 cin = inStr;
|
|
375 ninmax = nInBufferLength;
|
|
376 nin = 0;
|
|
377 kanji_convert(NULL);
|
|
378 *lpBytesReturned = nout;
|
|
379 if ( nout < noutmax ) *cout = 0;
|
|
380 return TRUE;
|
|
381 }
|
|
382
|
|
383 void CALLBACK ToHankaku(LPSTR inStr)
|
|
384 {
|
|
385 unsigned char *p;
|
|
386 int len;
|
|
387
|
|
388 len = strlen(inStr) + 1;
|
|
389 p = malloc(len);
|
|
390 if ( p == NULL ) return;
|
|
391 memcpy(p,inStr,len);
|
|
392 reinit();
|
|
393 options("-ZSs");
|
|
394 NkfConvert(inStr,p);
|
|
395 free(p);
|
|
396 }
|
|
397
|
|
398 BOOL WINAPI ToHankakuSafe(LPSTR outStr,DWORD nOutBufferLength /*in Bytes*/,LPDWORD lpBytesReturned /*in Bytes*/,LPCSTR inStr,DWORD nInBufferLength /*in Bytes*/)
|
|
399 {
|
|
400 reinit();
|
|
401 options("-ZSs");
|
|
402 return NkfConvertSafe(outStr,nOutBufferLength,lpBytesReturned,inStr,nInBufferLength);
|
|
403 }
|
|
404
|
|
405 void CALLBACK ToZenkakuKana(LPSTR outStr, LPCSTR inStr)
|
|
406 {
|
|
407 reinit();
|
|
408 options("-Ss");
|
|
409 NkfConvert(outStr, inStr);
|
|
410 }
|
|
411
|
|
412 BOOL WINAPI ToZenkakuKanaSafe(LPSTR outStr,DWORD nOutBufferLength /*in Bytes*/,LPDWORD lpBytesReturned /*in Bytes*/,LPCSTR inStr,DWORD nInBufferLength /*in Bytes*/)
|
|
413 {
|
|
414 reinit();
|
|
415 options("-Ss");
|
|
416 return NkfConvertSafe(outStr,nOutBufferLength,lpBytesReturned,inStr,nInBufferLength);
|
|
417 }
|
|
418
|
|
419 void CALLBACK EncodeSubject(LPSTR outStr ,LPCSTR inStr){
|
|
420 reinit();
|
|
421 options("-jM");
|
|
422 NkfConvert(outStr, inStr);
|
|
423 }
|
|
424
|
|
425 BOOL WINAPI EncodeSubjectSafe(LPSTR outStr,DWORD nOutBufferLength /*in Bytes*/,LPDWORD lpBytesReturned /*in Bytes*/,LPCSTR inStr,DWORD nInBufferLength /*in Bytes*/)
|
|
426 {
|
|
427 reinit();
|
|
428 options("-jM");
|
|
429 return NkfConvertSafe(outStr,nOutBufferLength,lpBytesReturned,inStr,nInBufferLength);
|
|
430 }
|
|
431
|
|
432 #ifdef TOMIME
|
|
433 void CALLBACK ToMime(LPSTR outStr ,LPCSTR inStr)
|
|
434 {
|
|
435 EncodeSubject(outStr,inStr);
|
|
436 }
|
|
437 #endif /*TOMIME*/
|
|
438
|
|
439 #ifdef GETKANJICODE
|
|
440 int CALLBACK NkfGetKanjiCode(VOID)
|
|
441 {
|
|
442 int iCode=0;
|
|
443 //if(iconv == s_iconv)iCode=0; /* 0:シフトJIS */
|
|
444 if(iconv == w_iconv)iCode=3; /* UTF-8 */
|
|
445 else if(iconv == w_iconv16){
|
|
446 if(utf16_mode == UTF16BE_INPUT)iCode=5; /* 5:UTF-16BE */
|
|
447 else iCode=4; /* 4:UTF-16LE */
|
|
448 }else if(iconv == e_iconv){
|
|
449 if(estab_f == FALSE)iCode=2; /* 2:ISO-2022-JP */
|
|
450 else iCode=1; /* 1:EUC */
|
|
451 }
|
|
452 return iCode;
|
|
453 }
|
|
454 #endif /*GETKANJICODE*/
|
|
455
|
|
456 #ifdef FILECONVERT1
|
|
457 void CALLBACK NkfFileConvert1(LPCSTR fName)
|
|
458 {
|
|
459 FILE *fin;
|
|
460 char *tempdname;
|
|
461 char tempfname[MAX_PATH];
|
|
462 char d[4];
|
|
463 DWORD len;
|
|
464 BOOL sts;
|
|
465
|
|
466 len = GetTempPath(sizeof d,d);
|
|
467 tempdname = malloc(len + 1);
|
|
468 if ( tempdname == NULL ) return;
|
|
469 len = GetTempPath(len + 1,tempdname);
|
|
470 sts = GetTempFileName(tempdname,"NKF",0,tempfname);
|
|
471 if ( sts != 0 ) {
|
|
472 sts = CopyFileA(fName,tempfname,FALSE);
|
|
473 if ( sts ) {
|
|
474 if ((fin = fopen(tempfname, "rb")) != NULL) {
|
|
475 if ((fout = fopen(fName, "wb")) != NULL) {
|
|
476 cin = NULL;
|
|
477 nin = -1;
|
|
478 ninmax = -1;
|
|
479 std_getc_mode = 1;
|
|
480 cout = NULL;
|
|
481 nout = -1;
|
|
482 noutmax = -1;
|
|
483 std_putc_mode = 1;
|
|
484 kanji_convert(fin);
|
|
485 fclose(fin);
|
|
486 }
|
|
487 fclose(fout);
|
|
488 }
|
|
489 DeleteFile(tempfname);
|
|
490 }
|
|
491 }
|
|
492 free(tempdname);
|
|
493 }
|
|
494 #endif /*FILECONVERT1*/
|
|
495
|
|
496 BOOL WINAPI NkfFileConvert1SafeA(LPCSTR fName,DWORD nBufferLength /*in TCHARs*/)
|
|
497 {
|
|
498 FILE *fin;
|
|
499 char *tempdname;
|
|
500 char tempfname[MAX_PATH];
|
|
501 char d[4];
|
|
502 DWORD len;
|
|
503 BOOL sts;
|
|
504 BOOL ret;
|
|
505 LPCSTR p;
|
|
506
|
|
507 ret = FALSE;
|
|
508 p = fName;
|
|
509 for ( ;; ) {
|
|
510 if ( nBufferLength == 0 ) return ret;
|
|
511 if ( *p == 0 ) break;
|
|
512 p++;
|
|
513 --nBufferLength;
|
|
514 }
|
|
515 if ( chmod(fName,_S_IREAD | _S_IWRITE) == -1 ) return ret;
|
|
516 len = GetTempPath(sizeof d,d);
|
|
517 tempdname = malloc(len + 1);
|
|
518 if ( tempdname == NULL ) return FALSE;
|
|
519 len = GetTempPath(len + 1,tempdname);
|
|
520 sts = GetTempFileName(tempdname,"NKF",0,tempfname);
|
|
521 if ( sts != 0 ) {
|
|
522 sts = CopyFileA(fName,tempfname,FALSE);
|
|
523 if ( sts ) {
|
|
524 if ((fin = fopen(tempfname, "rb")) != NULL) {
|
|
525 if ((fout = fopen(fName, "wb")) != NULL) {
|
|
526 cin = NULL;
|
|
527 nin = -1;
|
|
528 ninmax = -1;
|
|
529 std_getc_mode = 1;
|
|
530 cout = NULL;
|
|
531 nout = -1;
|
|
532 noutmax = -1;
|
|
533 std_putc_mode = 1;
|
|
534 kanji_convert(fin);
|
|
535 fclose(fin);
|
|
536 ret = TRUE;
|
|
537 }
|
|
538 fclose(fout);
|
|
539 }
|
|
540 DeleteFileA(tempfname);
|
|
541 }
|
|
542 }
|
|
543 free(tempdname);
|
|
544 return ret;
|
|
545 }
|
|
546
|
|
547 BOOL WINAPI NkfFileConvert1SafeW(LPCWSTR fName,DWORD nBufferLength /*in TCHARs*/)
|
|
548 {
|
|
549 #ifdef UNICODESUPPORT
|
|
550 FILE *fin;
|
|
551 wchar_t *tempdname;
|
|
552 wchar_t tempfname[MAX_PATH];
|
|
553 wchar_t d[2];
|
|
554 DWORD len;
|
|
555 BOOL sts;
|
|
556 BOOL ret;
|
|
557 LPCWSTR p;
|
|
558
|
|
559 ret = FALSE;
|
|
560 p = fName;
|
|
561 for ( ;; ) {
|
|
562 if ( nBufferLength == 0 ) return ret;
|
|
563 if ( *p == 0 ) break;
|
|
564 p++;
|
|
565 --nBufferLength;
|
|
566 }
|
|
567 if ( _wchmod(fName,_S_IREAD | _S_IWRITE) == -1 ) return ret;
|
|
568 len = GetTempPathW(sizeof d / sizeof(WCHAR),d);
|
|
569 tempdname = malloc((len + 1) * sizeof(WCHAR));
|
|
570 if ( tempdname == NULL ) return FALSE;
|
|
571 len = GetTempPathW(len + 1,tempdname);
|
|
572 sts = GetTempFileNameW(tempdname,L"NKF",0,tempfname);
|
|
573 if ( sts != 0 ) {
|
|
574 sts = CopyFileW(fName,tempfname,FALSE);
|
|
575 if ( sts ) {
|
|
576 if ((fin = _wfopen(tempfname,L"rb")) != NULL) {
|
|
577 if ((fout = _wfopen(fName,L"wb")) != NULL) {
|
|
578 cin = NULL;
|
|
579 nin = -1;
|
|
580 ninmax = -1;
|
|
581 std_getc_mode = 1;
|
|
582 cout = NULL;
|
|
583 nout = -1;
|
|
584 noutmax = -1;
|
|
585 std_putc_mode = 1;
|
|
586 kanji_convert(fin);
|
|
587 fclose(fin);
|
|
588 ret = TRUE;
|
|
589 }
|
|
590 fclose(fout);
|
|
591 }
|
|
592 DeleteFileW(tempfname);
|
|
593 }
|
|
594 }
|
|
595 free(tempdname);
|
|
596 return ret;
|
|
597 #else /*UNICODESUPPORT*/
|
|
598 return FALSE;
|
|
599 #endif /*UNICODESUPPORT*/
|
|
600 }
|
|
601
|
|
602 #ifdef FILECONVERT2
|
|
603 void CALLBACK NkfFileConvert2(LPCSTR fInName,LPCSTR fOutName)
|
|
604 {
|
|
605 FILE *fin;
|
|
606
|
|
607 if ((fin = fopen(fInName, "rb")) == NULL) return;
|
|
608 if((fout=fopen(fOutName, "wb")) == NULL) {
|
|
609 fclose(fin);
|
|
610 return;
|
|
611 }
|
|
612 cin = NULL;
|
|
613 nin = -1;
|
|
614 ninmax = -1;
|
|
615 std_getc_mode = 1;
|
|
616 cout = NULL;
|
|
617 nout = -1;
|
|
618 noutmax = -1;
|
|
619 std_putc_mode = 1;
|
|
620 kanji_convert(fin);
|
|
621 fclose(fin);
|
|
622 fclose(fout);
|
|
623 }
|
|
624 #endif /*FILECONVERT2*/
|
|
625
|
|
626 BOOL WINAPI NkfFileConvert2SafeA(LPCSTR fInName,DWORD fInBufferLength /*in TCHARs*/,LPCSTR fOutName,DWORD fOutBufferLength /*in TCHARs*/)
|
|
627 {
|
|
628 FILE *fin;
|
|
629 BOOL sts;
|
|
630 BOOL ret;
|
|
631 LPCSTR p;
|
|
632
|
|
633 ret = FALSE;
|
|
634 p = fInName;
|
|
635 for ( ;; ) {
|
|
636 if ( fInBufferLength == 0 ) return ret;
|
|
637 if ( *p == 0 ) break;
|
|
638 p++;
|
|
639 --fInBufferLength;
|
|
640 }
|
|
641 p = fOutName;
|
|
642 for ( ;; ) {
|
|
643 if ( fOutBufferLength == 0 ) return ret;
|
|
644 if ( *p == 0 ) break;
|
|
645 p++;
|
|
646 --fOutBufferLength;
|
|
647 }
|
|
648 if ((fin = fopen(fInName, "rb")) != NULL) {
|
|
649 if((fout=fopen(fOutName, "wb")) != NULL) {
|
|
650 cin = NULL;
|
|
651 nin = -1;
|
|
652 ninmax = -1;
|
|
653 std_getc_mode = 1;
|
|
654 cout = NULL;
|
|
655 nout = -1;
|
|
656 noutmax = -1;
|
|
657 std_putc_mode = 1;
|
|
658 kanji_convert(fin);
|
|
659 fclose(fin);
|
|
660 ret = TRUE;
|
|
661 }
|
|
662 fclose(fout);
|
|
663 }
|
|
664 return ret;
|
|
665 }
|
|
666
|
|
667 BOOL WINAPI NkfFileConvert2SafeW(LPCWSTR fInName,DWORD fInBufferLength /*in TCHARs*/,LPCWSTR fOutName,DWORD fOutBufferLength /*in TCHARs*/)
|
|
668 {
|
|
669 #ifdef UNICODESUPPORT
|
|
670 FILE *fin;
|
|
671 BOOL sts;
|
|
672 BOOL ret;
|
|
673 LPCWSTR p;
|
|
674
|
|
675 ret = FALSE;
|
|
676 p = fInName;
|
|
677 for ( ;; ) {
|
|
678 if ( fInBufferLength == 0 ) return ret;
|
|
679 if ( *p == 0 ) break;
|
|
680 p++;
|
|
681 --fInBufferLength;
|
|
682 }
|
|
683 p = fOutName;
|
|
684 for ( ;; ) {
|
|
685 if ( fOutBufferLength == 0 ) return ret;
|
|
686 if ( *p == 0 ) break;
|
|
687 p++;
|
|
688 --fOutBufferLength;
|
|
689 }
|
|
690 if ( (fin = _wfopen(fInName,L"rb")) != NULL) {
|
|
691 if( (fout = _wfopen(fOutName,L"wb")) != NULL) {
|
|
692 cin = NULL;
|
|
693 nin = -1;
|
|
694 ninmax = -1;
|
|
695 std_getc_mode = 1;
|
|
696 cout = NULL;
|
|
697 nout = -1;
|
|
698 noutmax = -1;
|
|
699 std_putc_mode = 1;
|
|
700 kanji_convert(fin);
|
|
701 fclose(fin);
|
|
702 ret = TRUE;
|
|
703 }
|
|
704 fclose(fout);
|
|
705 }
|
|
706 return ret;
|
|
707 #else /*UNICODESUPPORT*/
|
|
708 return FALSE;
|
|
709 #endif /*UNICODESUPPORT*/
|
|
710 }
|
|
711
|
|
712 BOOL WINAPI GetNkfGuessA(LPSTR outStr,DWORD nBufferLength /*in TCHARs*/,LPDWORD lpTCHARsReturned /*in TCHARs*/)
|
|
713 {
|
|
714 if ( outStr == NULL || nBufferLength == 0 ) return FALSE;
|
|
715 print_guessed_code(NULL);
|
|
716 *lpTCHARsReturned = strlen(guessbuffA) + 1;
|
|
717 return scp(outStr,guessbuffA,nBufferLength);
|
|
718 }
|
|
719
|
|
720 BOOL WINAPI GetNkfGuessW(LPWSTR outStr,DWORD nBufferLength /*in TCHARs*/,LPDWORD lpTCHARsReturned /*in TCHARs*/)
|
|
721 {
|
|
722 #ifdef UNICODESUPPORT
|
|
723 if ( outStr == NULL || nBufferLength == 0 ) return FALSE;
|
|
724 print_guessed_codeW(NULL);
|
|
725 *lpTCHARsReturned = wcslen(guessbuffW) + 1;
|
|
726 return wscp(outStr,guessbuffW,nBufferLength);
|
|
727 #else /*UNICODESUPPORT*/
|
|
728 return FALSE;
|
|
729 #endif /*UNICODESUPPORT*/
|
|
730 }
|
|
731
|
|
732 static struct {
|
|
733 DWORD size;
|
|
734 LPCSTR copyrightA;
|
|
735 LPCSTR versionA;
|
|
736 LPCSTR dateA;
|
|
737 DWORD functions;
|
|
738 } NkfSupportFunctions = {
|
|
739 sizeof(NkfSupportFunctions),
|
|
740 NULL,
|
|
741 NKF_VERSION,
|
|
742 NKF_RELEASE_DATE,
|
|
743 1 /* nkf32103a.lzh uminchu 1.03 */
|
|
744 /* | 2 */ /* nkf32dll.zip 0.91 */
|
|
745 #if defined(TOMIME) && defined(GETKANJICODE) && defined(FILECONVERT1) && defined(FILECONVERT2)
|
|
746 | 4 /* nkf32204.zip Kaneto 2.0.4.0 */
|
|
747 #endif
|
|
748 | 8 /* this */
|
|
749 #ifdef UNICODESUPPORT
|
|
750 | 0x80000000
|
|
751 #endif /*UNICODESUPPORT*/
|
|
752 ,
|
|
753 };
|
|
754
|
|
755 BOOL WINAPI GetNkfSupportFunctions(void *outStr,DWORD nBufferLength /*in Bytes*/,LPDWORD lpBytesReturned /*in Bytes*/)
|
|
756 {
|
|
757 *lpBytesReturned = sizeof NkfSupportFunctions;
|
|
758 if ( outStr == NULL || nBufferLength == 0 ) return FALSE;
|
|
759 NkfSupportFunctions.copyrightA = CopyRight;
|
|
760 memcpy(outStr,&NkfSupportFunctions,sizeof NkfSupportFunctions > nBufferLength ? nBufferLength : sizeof NkfSupportFunctions);
|
|
761 return TRUE;
|
|
762 }
|
|
763
|
|
764 BOOL WINAPI NkfUsage(LPSTR outStr,DWORD nBufferLength /*in Bytes*/,LPDWORD lpBytesReturned /*in Bytes*/)
|
|
765 {
|
|
766 ubuff = outStr;
|
|
767 ulen = nBufferLength;
|
|
768 uret = TRUE;
|
|
769 usage();
|
|
770 if ( uret == TRUE ) {
|
|
771 *lpBytesReturned = nBufferLength - ulen;
|
|
772 }
|
|
773 return uret;
|
|
774 }
|
|
775
|
|
776 /* nkf32.dll main end */
|
|
777
|
|
778 #ifdef DLLDBG /* DLLDBG @@*/
|
|
779 /* dbg.exe */
|
|
780 unsigned char buff[65536];
|
|
781 unsigned char buff2[65536];
|
|
782 unsigned char buff3[65536];
|
|
783 unsigned char buff4[65536];
|
|
784 char *code[] = {"シフトJIS","EUC","ISO-2022-JP","UTF-8","UTF-16LE","UTF-16BE"};
|
|
785
|
|
786 int n;
|
|
787 BOOL sts;
|
|
788 DWORD len;
|
|
789
|
|
790 void mimeencode(unsigned char *buff2)
|
|
791 {
|
|
792 memset(buff,0,sizeof buff);
|
|
793 EncodeSubject(buff,buff2);
|
|
794 printf("EncodeSubject(%s)=%s\n",buff2,buff);
|
|
795 memset(buff,0,sizeof buff);
|
|
796 ToMime(buff,buff2);
|
|
797 printf("ToMime(%s)=%s\n",buff2,buff);
|
|
798 memset(buff,0,sizeof buff);
|
|
799 sts = EncodeSubjectSafe(buff,sizeof buff,&len,buff2,strlen(buff2));
|
|
800 printf("EncodeSubjectSafe(%s)=%d len=%d '%s'\n",buff,sts,len,buff);
|
|
801 dumpn(buff2,strlen(buff2));
|
|
802 dumpn(buff,len);
|
|
803 }
|
|
804
|
|
805 void convert(char *arg,unsigned char *buff2)
|
|
806 {
|
|
807 sts = SetNkfOption(arg);
|
|
808 printf("SetNkfOption(%s)=%d\n",arg,sts);
|
|
809 memset(buff,0,sizeof buff);
|
|
810 NkfConvert(buff,buff2);
|
|
811 printf("NkfConvert(%s)=%s\n",buff2,buff);
|
|
812 n = NkfGetKanjiCode();
|
|
813 printf("NkfGetKanjiCode()=%d\n",n);
|
|
814 sts = SetNkfOption(arg);
|
|
815 printf("SetNkfOption(%s)=%d\n",arg,sts);
|
|
816 memset(buff,0,sizeof buff);
|
|
817 sts = NkfConvertSafe(buff,sizeof buff,&len,buff2,strlen(buff2));
|
|
818 printf("NkfConvertSafe(%s)=%d len=%d '%s'\n",buff2,sts,len,buff);
|
|
819 dumpn(buff2,strlen(buff2));
|
|
820 dumpn(buff,len);
|
|
821 n = NkfGetKanjiCode();
|
|
822 printf("NkfGetKanjiCode()=%d\n",n);
|
|
823 }
|
|
824
|
|
825 void guess(unsigned char *buff2)
|
|
826 {
|
|
827 char *g = "--guess";
|
|
828
|
|
829 sts = SetNkfOption(g);
|
|
830 printf("SetNkfOption(%s)=%d\n",g,sts);
|
|
831 memset(buff,0,sizeof buff);
|
|
832 NkfConvert(buff,buff2);
|
|
833 printf("NkfConvert(%s)=%s\n",buff2,buff);
|
|
834 dumpn(buff2,strlen(buff2));
|
|
835 n = NkfGetKanjiCode();
|
|
836 printf("NkfGetKanjiCode()=%d %s\n",n,code[n]);
|
|
837 memset(buff,0,sizeof buff);
|
|
838 sts = GetNkfGuessA(buff,sizeof buff,&len);
|
|
839 printf("GetNkfGuessA()=%d len=%d '%s'\n",sts,len,buff);
|
|
840 dumpn(buff,len);
|
|
841 memset(buff,0,sizeof buff);
|
|
842 sts = GetNkfGuessW((LPWSTR)buff,sizeof buff / sizeof(WCHAR),&len);
|
|
843 printf("GetNkfGuessW()=%d len=%d\n",sts,len);
|
|
844 dumpn(buff,len * sizeof(WCHAR));
|
|
845 }
|
|
846
|
|
847 void dumpf(char *f)
|
|
848 {
|
|
849 FILE *fp;
|
|
850 unsigned int n;
|
|
851
|
|
852 fp = fopen(f,"rb");
|
|
853 if ( fp == NULL ) return;
|
|
854 n = fread(buff,1,sizeof buff,fp);
|
|
855 fclose(fp);
|
|
856 printf("dumpf(%s,%d)\n",f,n);
|
|
857 dumpn(buff,n);
|
|
858 }
|
|
859
|
|
860 void mkfile(char *f,char *p)
|
|
861 {
|
|
862 FILE *fp;
|
|
863
|
|
864 fp = fopen(f,"w");
|
|
865 if ( fp == NULL ) return;
|
|
866 fputs(p,fp);
|
|
867 fclose(fp);
|
|
868 dumpf(f);
|
|
869 }
|
|
870
|
|
871 void file(char *arg2,char *arg3,unsigned char *buf)
|
|
872 {
|
|
873 sts = SetNkfOption(arg2);
|
|
874 printf("SetNkfOption(%s)=%d\n",arg2,sts);
|
|
875 mkfile(arg3,buf);
|
|
876 NkfFileConvert1(arg3);
|
|
877 printf("NkfFileConvert1(%s)\n",arg3);
|
|
878 dumpf(arg3);
|
|
879 sts = SetNkfOption(arg2);
|
|
880 printf("SetNkfOption(%s)=%d\n",arg2,sts);
|
|
881 mkfile(arg3,buf);
|
|
882 sts = NkfFileConvert1SafeA(arg3,strlen(arg3) + 1);
|
|
883 printf("NkfFileConvert1SafeA(%s)=%d\n",arg3,sts);
|
|
884 dumpf(arg3);
|
|
885 sts = SetNkfOption(arg2);
|
|
886 printf("SetNkfOption(%s)=%d\n",arg2,sts);
|
|
887 mkfile(arg3,buf);
|
|
888 sts = MultiByteToWideChar(CP_OEMCP,MB_PRECOMPOSED,arg3,-1,(wchar_t *)buff,sizeof(buff) / sizeof(wchar_t));
|
|
889 printf("MultiByteToWideChar(%s)=%d\n",arg3,sts);
|
|
890 dumpn(buff,(wcslen((wchar_t *)buff) + 1) * sizeof(wchar_t));
|
|
891 sts = NkfFileConvert1SafeW((wchar_t *)buff,sizeof buff / sizeof(wchar_t) /*wcslen((wchar_t *)buff) + 1*/);
|
|
892 printf("NkfFileConvert1SafeW()=%d\n",sts);
|
|
893 dumpf(arg3);
|
|
894 }
|
|
895
|
|
896 void file2(char *arg2,char *arg3,char *arg4,unsigned char *buf)
|
|
897 {
|
|
898 sts = SetNkfOption(arg2);
|
|
899 printf("SetNkfOption(%s)=%d\n",arg2,sts);
|
|
900 mkfile(arg3,buf);
|
|
901 NkfFileConvert2(arg3,arg4);
|
|
902 printf("NkfFileConvert1(%s,%s)\n",arg3,arg4);
|
|
903 dumpf(arg3);
|
|
904 dumpf(arg4);
|
|
905 sts = SetNkfOption(arg2);
|
|
906 printf("SetNkfOption(%s)=%d\n",arg2,sts);
|
|
907 mkfile(arg3,buf);
|
|
908 sts = NkfFileConvert2SafeA(arg3,strlen(arg3) + 1,arg4,strlen(arg4) + 1);
|
|
909 printf("NkfFileConvert2SafeA(%s,%s)=%d\n",arg3,arg4,sts);
|
|
910 dumpf(arg3);
|
|
911 dumpf(arg4);
|
|
912 sts = SetNkfOption(arg2);
|
|
913 printf("SetNkfOption(%s)=%d\n",arg2,sts);
|
|
914 mkfile(arg3,buf);
|
|
915 sts = MultiByteToWideChar(CP_OEMCP,MB_PRECOMPOSED,arg3,-1,(wchar_t *)buff,sizeof(buff) / sizeof(wchar_t));
|
|
916 printf("MultiByteToWideChar(%s)=%d\n",arg3,sts);
|
|
917 dumpn(buff,(wcslen((wchar_t *)buff) + 1) * sizeof(wchar_t));
|
|
918 sts = MultiByteToWideChar(CP_OEMCP,MB_PRECOMPOSED,arg4,-1,(wchar_t *)buff4,sizeof(buff4) / sizeof(wchar_t));
|
|
919 printf("MultiByteToWideChar(%s)=%d\n",arg4,sts);
|
|
920 dumpn(buff4,(wcslen((wchar_t *)buff4) + 1) * sizeof(wchar_t));
|
|
921 sts = NkfFileConvert2SafeW((wchar_t *)buff,sizeof buff / sizeof(wchar_t) ,(wchar_t *)buff4,sizeof buff4 / sizeof(wchar_t));
|
|
922 printf("NkfFileConvert2SafeW()=%d\n",sts);
|
|
923 dumpf(arg3);
|
|
924 dumpf(arg4);
|
|
925 }
|
|
926
|
|
927 int main(int argc,char **argv)
|
|
928 {
|
|
929 struct NKFSUPPORTFUNCTIONS fnc;
|
|
930
|
|
931 if ( argc < 2 ) return 0;
|
|
932 switch ( *argv[1] ) {
|
|
933 case 'v':
|
|
934 memset(buff,0,sizeof buff);
|
|
935 GetNkfVersion(buff);
|
|
936 printf("GetNkfVersion() '%s'\n",buff);
|
|
937 sts = GetNkfVersionSafeA(buff,sizeof buff,&len);
|
|
938 printf("GetNkfVersionSafeA()=%d len=%d '%s'\n",sts,len,buff);
|
|
939 sts = GetNkfVersionSafeW((LPWSTR)buff,sizeof buff / sizeof(WCHAR),&len);
|
|
940 printf("GetNkfVersionSafeW()=%d len=%d\n",sts,len);
|
|
941 dumpn(buff,len * sizeof(WCHAR));
|
|
942 sts = GetNkfSupportFunctions(&fnc,sizeof fnc,&len);
|
|
943 printf("GetNkfSupportFunctions()=%d len=%d\n",sts,len);
|
|
944 printf("size=%d\n",fnc.size);
|
|
945 printf("copyrightA='%s'\n",fnc.copyrightA);
|
|
946 printf("versionA='%s'\n",fnc.versionA);
|
|
947 printf("dateA='%s'\n",fnc.dateA);
|
|
948 printf("functions=%d %x\n",fnc.functions,fnc.functions);
|
|
949 break;
|
|
950 case 'm':
|
|
951 if ( argc < 3 ) return 0;
|
|
952 mimeencode(argv[2]);
|
|
953 break;
|
|
954 case 'M':
|
|
955 if ( argc < 2 ) return 0;
|
|
956 gets(buff2);
|
|
957 mimeencode(buff2);
|
|
958 break;
|
|
959 case 'c':
|
|
960 if ( argc < 4 ) return 0;
|
|
961 convert(argv[2],argv[3]);
|
|
962 break;
|
|
963 case 'C':
|
|
964 if ( argc < 3 ) return 0;
|
|
965 gets(buff2);
|
|
966 convert(argv[2],buff2);
|
|
967 break;
|
|
968 case 'g':
|
|
969 if ( argc < 3 ) return 0;
|
|
970 guess(argv[2]);
|
|
971 break;
|
|
972 case 'G':
|
|
973 if ( argc < 2 ) return 0;
|
|
974 gets(buff2);
|
|
975 guess(buff2);
|
|
976 break;
|
|
977 case 'f':
|
|
978 if ( argc < 5 ) return 0;
|
|
979 file(argv[2],argv[3],argv[4]);
|
|
980 break;
|
|
981 case 'F':
|
|
982 if ( argc < 4 ) return 0;
|
|
983 gets(buff3);
|
|
984 file(argv[2],argv[3],buff3);
|
|
985 break;
|
|
986 case '2':
|
|
987 if ( argc < 6 ) return 0;
|
|
988 file2(argv[2],argv[3],argv[4],argv[5]);
|
|
989 break;
|
|
990 case '#':
|
|
991 if ( argc < 5 ) return 0;
|
|
992 gets(buff3);
|
|
993 file2(argv[2],argv[3],argv[4],buff3);
|
|
994 break;
|
|
995 case 'u':
|
|
996 sts = NkfUsage(buff,sizeof buff,&len);
|
|
997 printf("strlen(buff)=%d\n",strlen(buff));
|
|
998 printf("NkfUsage()=%d len=%d \n%s",sts,len,buff);
|
|
999 break;
|
|
1000 }
|
|
1001 return 0;
|
|
1002 }
|
|
1003 /* dbg.exe end */
|
|
1004 #endif /* DLLDBG @@*/
|
|
1005 /*WIN32DLL*/
|