📄 nkflib.c
字号:
/* TiMidity++ -- MIDI to WAVE converter and player Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp> Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#ifdef HAVE_CONFIG_H#include "config.h"#endif /* HAVE_CONFIG_H */#include "timidity.h"#ifdef JAPANESE/** Network Kanji Filter. (PDS Version)************************************************************************** Copyright (C) 1987, Fujitsu LTD. (Itaru ICHIKAWA)** 息晚黎¨ ∈臭∷少晃奶甫垫疥 ソフト3甫 辉李 魂 ** ∈E-Mail Address: ichikawa@flab.fujitsu.co.jp∷** Copyright (C) 1996,1998** 息晚黎¨ 伟靛络池攫鼠供池彩 蚕填 靠迹 mine/X0208 support** ∈E-Mail Address: kono@ie.u-ryukyu.ac.jp∷** 息晚黎¨ COW for DOS & Win16 & Win32 & OS/2** ∈E-Mail Address: GHG00637@niftyserve.or.p∷** このソ〖スのいかなる剩继·猖恃·饯赖も钓满します。ただし、** その狠には、茂が棺弗したを绩すこの婶尸を荒すこと。** 浩芹邵や花伙の烧峡などの啼い圭わせも涩妥ありません。** このプログラムについては泼に部の瘦沮もしない、碍しからず。** Everyone is permitted to do anything on this program ** including copying, modifying, improving.** as long as you don't try to pretend that you wrote it.** i.e., the above copyright notice has to appear in all copies.** You don't have to ask before copying or publishing.** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.***********************************************************************//* 笆布のソ〖スは、nkf を矢机误拎侯できるよう猖陇したライブラリである。 nkf_conv(傅矢机误,叫蜗矢机误、out モ〖ド) 叫蜗矢机误を NULL としたときは、傅矢机误を拎侯する。 バグ : 恃垂され叫蜗される矢机误のための挝拌はある镍刨とっておくこと。 さもないと、バグを栏じる。 nkf_convert(傅矢机误、叫蜗矢机误、叫蜗矢机误の呵络の络きさ、 in モ〖ド、out モ〖ド) kanji_conv に洁じる。叫蜗矢机误の呵络の络きさが回年できる。 その络きさ笆惧になったときはそれ笆惧の矢机の叫蜗は虑ち磊られる。 モ〖ド nkf の convert に涂えるオプションを涂える矢机误。鄂球で惰磊って回年する。 称オプション: このプログラムに簇しての螟侯涪がらみのことは nkf に洁じるものとする。 痰瘦沮であるので、蝗脱の眷圭は极らの勒扦をもってすること。 猖恃荚 滥腾络叔 1997.02*//* 痰绿なところを猴近したˉ 戮で脱いられないインタ〖フェ〖スは static にしたˉ コンパイラの Warning メッセ〖ジを娃扩するように ANSI C の妨及にしたˉ 矢机を unsigned char * で SFILE に眠えるようにしたˉ SFILE を词帽步ˉ input_f == FALSE で convert すると·染逞カタカナ SJIS が EUC と冉们されてしまうバグ(慌屯だった々)を木したˉ しかしながら·SJIS の染逞カタカナ 2 矢机と EUC は惰侍できない 眷圭があるˉこのときは SJIS として恃垂することにしたˉ EUC_STRICT_CHECK を年盗すると EUC-Japan の年盗コ〖ドを窗链にチェックする ようにしたˉ 粕み哈み矢机を回年できるようにしたˉ 猖恃荚 叫崩赖景 1997*//* 1.7ベ〖スに恃构。 猖恃荚 滥腾络叔 2000.10*//* もし·EUC-Japan の窗链なチェックをする眷圭は EUC_STRICT_CHECK を年盗 * してくださいˉただし·1 バイトでも EUC-Japan の踏年盗矢机が崔まれていると * EUC とみなされなくなってしまいますˉ戮のプログラムで戳机コ〖ドを EUC に恃垂 * した眷圭·EUC の踏年盗拌へマップされる材墙拉がありますˉ *//* #define EUC_STRICT_CHECK */#if 0static char *CopyRight = "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),1998 S. Kono, COW";static char *Version = "1.7";static char *Patchlevel = "0/9711/Shinji Kono";#endif/********* USAGE: nkf [flags] [file] **** Flags:** b Output is bufferred (DEFAULT)** u Output is unbufferred**** t no operation**** j Outout code is JIS 7 bit (DEFAULT SELECT) ** s Output code is MS Kanji (DEFAULT SELECT) ** e Output code is AT&T JIS (DEFAULT SELECT) ** l Output code is JIS 7bit and ISO8859-1 Latin-1**** m MIME conversion for ISO-2022-JP** i_ Output sequence to designate JIS-kanji (DEFAULT_J)** o_ Output sequence to designate single-byte roman characters (DEFAULT_R)**** r {de/en}crypt ROT13/47**** v display Version**** T Text mode output (for MS-DOS)**** x Do not convert X0201 kana into X0208** Z Convert X0208 alphabet to ASCII**** f60 fold option**** m MIME decode** B try to fix broken JIS, missing Escape** B[1-9] broken level**** O Output to 'nkf.out' file ** d Delete \r in line feed ** c Add \r in line feed **//******************************//* デフォルトの叫蜗コ〖ド联买 *//* Select DEFAULT_CODE */#define DEFAULT_CODE_JIS/* #define DEFAULT_CODE_SJIS *//* #define DEFAULT_CODE_EUC *//******************************/#if (defined(__TURBOC__) || defined(LSI_C)) && !defined(MSDOS)#define MSDOS#endif#include <stdio.h>#include <stdlib.h>#ifndef NO_STRING_H#include <string.h>#else#include <strings.h>#endif#ifdef MSDOS#ifdef LSI_C#define setbinmode(fp) fsetbin(fp)#else /* Microsoft C, Turbo C */#define setbinmode(fp) setmode(fileno(fp), O_BINARY)#endif#else /* UNIX,OS/2 */#define setbinmode(fp)#endif#ifdef _IOFBF /* SysV and MSDOS */#define setvbuffer(fp, buf, size) setvbuf(fp, buf, _IOFBF, size)#else /* BSD */#define setvbuffer(fp, buf, size) setbuffer(fp, buf, size)#endif#include "common.h"#include "nkflib.h"#define VOIDVOID 0#ifndef FALSE#define FALSE 0#endif /* FALSE */#ifndef TRUE#define TRUE 1#endif /* TRUE *//* state of output_mode and input_mode */#define ASCII 0#define X0208 1#define X0201 2#define NO_X0201 3#define JIS_INPUT 4#define SJIS_INPUT 5#define LATIN1_INPUT 6#define FIXED_MIME 7#define DOUBLE_SPACE -2#define EUC_INPUT 8#define NL 0x0a#define ESC 0x1b#define SPACE 0x20#define AT 0x40#define SSP 0xa0#define DEL 0x7f#define SI 0x0f#define SO 0x0e#define SSO 0x8e#define HOLD_SIZE 32#define IOBUF_SIZE 16384#define DEFAULT_J 'B'#define DEFAULT_R 'B'#define SJ0162 0x00e1 /* 01 - 62 ku offset */#define SJ6394 0x0161 /* 63 - 94 ku offset *//* SFILE begin *//* 矢机误 を FILE みたいに胺う井嘿供 *//* これは nkf の戳机コ〖ド恃垂がファイルに滦してのみ滦炳しているのでそれを 矢机误拎侯で蝗えるようにするためのインタ〖フェ〖スである。ただし、 滦炳している怠墙は警ないし、涩妥なものしか侯っていない。したがって、 これらは nkf の面でしか罢蹋のないものであろう。 SFILE は FILE みたいなもので矢机误をファイルみたいに胺えるようにする。 SFILE を蝗うためには涩ずオ〖プンすること。ssopen で mode=="new" または "auto" 回年していなければクロ〖ズする涩妥はない。SFILE の面を木儡拎侯 した眷圭はいろいろ啼玛が叫てくるであろう。 SEOF は EOF みたいなもの。 ssopen は open みたいな簇眶で、 sf : SFILE 房の恃眶 st : 矢机误 maxsize : 矢机误が钓推できる呵络の络きさ。sputc 箕に扩嘎を掐れるもの。 maxsize に -1 を回年するとこの借妄を痰浑するようになる。 そのときは、涩妥笆惧の矢机を sputc しないように丹をつけなけれ ばならない。 mode : newstr、stdout、stdin の矢机误を回年できる。 毋えば mode="new stdout" newstr は极瓢弄に矢机误のメモリを maxsize だけ惩评する。 ただし、maxsize < 1 のときはディフォルトの猛を惩评する。 stdout は SFILE の筛洁叫蜗 stdout となる矢机误を回年する。 stdin は SFILE の筛洁掐蜗 stdin となる矢机误を回年する。 sclose は close みたいな簇眶で、newstr でオ〖プンされていたときは、 矢机误も free で久殿する。 sgetc、sungetc、sputc、sputchar はそれぞれ getc、ungetc、putc、putchar に陵碰する。苞眶の sf が NULL の箕は SEOF を手す。*/typedef struct __SFILE { unsigned char *pointer; /* 矢机误附哼のポインタ */ unsigned char *head; /* 矢机误の呵介の疤弥 */ unsigned char *tail; /* 矢机误の钓推の呵稿の疤弥 */ char mode[20]; /* 矢机误オ〖プンモ〖ド newstr,stdout,stdin */ /* "newstr stdin" の寥圭わせはない */} SFILE;#define SEOF -1static SFILE *sstdout=NULL;static SFILE *sstdin=NULL; /* Never used ? */#ifndef BUFSIZ#define BUFSIZ 1024#endif /* BUFSIZ */static char sfile_buffer[BUFSIZ];#ifndef SAFE_CONVERT_LENGTH#define SAFE_CONVERT_LENGTH(len) (2 * (len) + 7)#endif /* SAFE_CONVERT_LENGTH *//* Functions */static SFILE *ssopen(SFILE *, char *string,signed int maxsize,char *md);static void sclose(SFILE *sf);static int sgetc(SFILE *sf);static int sungetc(int c,SFILE *sf);static int sputc(int c,SFILE *sf);#define sputchar(c) sputc(c,sstdout)/* nkf 戳机コンバ〖ト */char *nkf_convert(char *si,char *so,int maxsize,char *in_mode,char *out_mode);char *nkf_conv(char *si,char *so,char *out_mode);static int check_kanji_code(unsigned char *p);/* MIME preprocessor */#undef STRICT_MIME /* do stupid strict mime integrity check */#define GETC(p) ((!mime_mode)?sgetc(p):mime_getc(p))#define UNGETC(c,p) ((!mime_mode)?sungetc(c,p):mime_ungetc(c))#ifdef EASYWIN /*Easy Win */extern POINT _BufferSize;#endif/* function prototype */static int noconvert(SFILE *f);static int kanji_convert(SFILE *f);static int h_conv(SFILE *f,int c2,int c1);static int push_hold_buf(int c2,int c1);static int s_iconv(int c2,int c1);static int e_oconv(int c2,int c1);static int s_oconv(int c2,int c1);static int j_oconv(int c2,int c1);static int line_fold(int c2,int c1);static int pre_convert(int c1,int c2);static int mime_begin(SFILE *f);static int mime_getc(SFILE *f);static int mime_ungetc(unsigned int c);static int mime_integrity(SFILE *f,unsigned char *p);static int base64decode(int c);static int usage(void);static void arguments(char *c);static void reinit();/* buffers */static char stdibuf[IOBUF_SIZE];static char stdobuf[IOBUF_SIZE];static unsigned char hold_buf[HOLD_SIZE*2];static int hold_count;/* MIME preprocessor fifo */#define MIME_BUF_SIZE (1024) /* 2^n ring buffer */#define MIME_BUF_MASK (MIME_BUF_SIZE-1) #define Fifo(n) mime_buf[(n)&MIME_BUF_MASK]static unsigned char mime_buf[MIME_BUF_SIZE];static unsigned int mime_top = 0;static unsigned int mime_last = 0; /* decoded */static unsigned int mime_input = 0; /* undecoded *//* flags */static int unbuf_f = FALSE;static int estab_f = FALSE;static int nop_f = FALSE;static int binmode_f = TRUE; /* binary mode */static int rot_f = FALSE; /* rot14/43 mode */static int input_f = FALSE; /* non fixed input code */static int alpha_f = FALSE; /* convert JIx0208 alphbet to ASCII */static int mime_f = TRUE; /* convert MIME B base64 or Q */static int mimebuf_f = FALSE; /* MIME buffered input */static int broken_f = FALSE; /* convert ESC-less broken JIS */static int iso8859_f = FALSE; /* ISO8859 through */#if defined(MSDOS) || defined(__OS2__) static int x0201_f = TRUE; /* Assume JISX0201 kana */#elsestatic int x0201_f = NO_X0201; /* Assume NO JISX0201 */#endif/* X0208 -> ASCII converter */static int c1_return;/* fold parameter */static int line = 0; /* chars in line */static int prev = 0;static int fold_f = FALSE;static int fold_len = 0;/* options */static char kanji_intro = DEFAULT_J, ascii_intro = DEFAULT_R;/* Folding */int line_fold();#define FOLD_MARGIN 10#define DEFAULT_FOLD 60/* converters */#ifdef DEFAULT_CODE_JIS# define DEFAULT_CONV j_oconv#endif#ifdef DEFAULT_CODE_SJIS# define DEFAULT_CONV s_oconv#endif#ifdef DEFAULT_CODE_EUC# define DEFAULT_CONV e_oconv#endifstatic int (*iconv)(int c2,int c1); /* s_iconv or oconv */static int (*oconv)(int c2,int c1) = DEFAULT_CONV; /* [ejs]_oconv *//* Global states */static int output_mode = ASCII, /* output kanji mode */ input_mode = ASCII, /* input kanji mode */ shift_mode = FALSE; /* TRUE shift out, or X0201 */static int mime_mode = FALSE; /* MIME mode B base64, Q hex *//* X0201 / X0208 conversion tables *//* X0201 kana conversion table *//* 90-9F A0-DF */unsigned char cv[]= {0x21,0x21,0x21,0x23,0x21,0x56,0x21,0x57,0x21,0x22,0x21,0x26,0x25,0x72,0x25,0x21,0x25,0x23,0x25,0x25,0x25,0x27,0x25,0x29,0x25,0x63,0x25,0x65,0x25,0x67,0x25,0x43,0x21,0x3c,0x25,0x22,0x25,0x24,0x25,0x26,0x25,0x28,0x25,0x2a,0x25,0x2b,0x25,0x2d,0x25,0x2f,0x25,0x31,0x25,0x33,0x25,0x35,0x25,0x37,0x25,0x39,0x25,0x3b,0x25,0x3d,0x25,0x3f,0x25,0x41,0x25,0x44,0x25,0x46,0x25,0x48,0x25,0x4a,0x25,0x4b,0x25,0x4c,0x25,0x4d,0x25,0x4e,0x25,0x4f,0x25,0x52,0x25,0x55,0x25,0x58,0x25,0x5b,0x25,0x5e,0x25,0x5f,0x25,0x60,0x25,0x61,0x25,0x62,0x25,0x64,0x25,0x66,0x25,0x68,0x25,0x69,0x25,0x6a,0x25,0x6b,0x25,0x6c,0x25,0x6d,0x25,0x6f,0x25,0x73,0x21,0x2b,0x21,0x2c,0x00,0x00};/* X0201 kana conversion table for daguten *//* 90-9F A0-DF */unsigned char dv[]= { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x2c,0x25,0x2e,0x25,0x30,0x25,0x32,0x25,0x34,0x25,0x36,0x25,0x38,0x25,0x3a,0x25,0x3c,0x25,0x3e,0x25,0x40,0x25,0x42,0x25,0x45,0x25,0x47,0x25,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -