[Ttssh2-commit] [5176] チェックサムアルゴリズム (CRC16, CHECKSUM8/16/32) のマクロコマンドを

svnno****@sourc***** svnno****@sourc*****
2013年 3月 27日 (水) 00:18:46 JST


Revision: 5176
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/5176
Author:   yutakapon
Date:     2013-03-27 00:18:45 +0900 (Wed, 27 Mar 2013)
Log Message:
-----------
チェックサムアルゴリズム(CRC16, CHECKSUM8/16/32)のマクロコマンドを
サポートした。

Modified Paths:
--------------
    trunk/teraterm/common/helpid.h
    trunk/teraterm/ttpmacro/ttl.c
    trunk/teraterm/ttpmacro/ttmparse.c
    trunk/teraterm/ttpmacro/ttmparse.h

-------------- next part --------------
Modified: trunk/teraterm/common/helpid.h
===================================================================
--- trunk/teraterm/common/helpid.h	2013-03-26 12:25:07 UTC (rev 5175)
+++ trunk/teraterm/common/helpid.h	2013-03-26 15:18:45 UTC (rev 5176)
@@ -168,6 +168,13 @@
 #define HlpMacroCommandCall             92004
 #define HlpMacroCommandCallmenu         92125
 #define HlpMacroCommandChangedir        92005
+#define HlpMacroCommandChecksum8        92204
+#define HlpMacroCommandChecksum8file    92205
+#define HlpMacroCommandChecksum16       92206
+#define HlpMacroCommandChecksum16file   92207
+#define HlpMacroCommandChecksum32       92208
+#define HlpMacroCommandChecksum32file   92209
+
 #define HlpMacroCommandClearscreen      92006
 #define HlpMacroCommandClipb2var        92113
 #define HlpMacroCommandClosesbox        92007
@@ -175,6 +182,8 @@
 #define HlpMacroCommandCode2str         92009
 #define HlpMacroCommandConnect          92010
 #define HlpMacroCommandContinue         92155
+#define HlpMacroCommandCrc16            92202
+#define HlpMacroCommandCrc16File        92203
 #define HlpMacroCommandCrc32            92138
 #define HlpMacroCommandCrc32File        92139
 #define HlpMacroCommandCygConnect       92130

Modified: trunk/teraterm/ttpmacro/ttl.c
===================================================================
--- trunk/teraterm/ttpmacro/ttl.c	2013-03-26 12:25:07 UTC (rev 5175)
+++ trunk/teraterm/ttpmacro/ttl.c	2013-03-26 15:18:45 UTC (rev 5176)
@@ -634,31 +634,77 @@
 	return Err;
 }
 
+
 /*
  * cf. http://oku.edu.mie-u.ac.jp/~okumura/algo/
  */
-#define CRCPOLY1 0x04C11DB7UL
-	/* x^{32}+x^{26}+x^{23}+x^{22}+x^{16}+x^{12}+x^{11]+
-	   x^{10}+x^8+x^7+x^5+x^4+x^2+x^1+1 */
-#define CRCPOLY2 0xEDB88320UL  /* \x8D\xB6\x89E\x8Bt\x93] */
 
-static unsigned long crc1(int n, unsigned char c[])
+enum checksum_type {
+	CHECKSUM8,
+	CHECKSUM16,
+	CHECKSUM32,
+	CRC16,
+	CRC32
+};
+
+static unsigned int checksum32(int n, unsigned char c[])
 {
+	unsigned long value = 0;
+	int i;
+
+	for (i = 0; i < n; i++) {
+		value += (c[i] & 0xFF);
+	}
+	return (value & 0xFFFFFFFF);
+}
+
+static unsigned int checksum16(int n, unsigned char c[])
+{
+	unsigned long value = 0;
+	int i;
+
+	for (i = 0; i < n; i++) {
+		value += (c[i] & 0xFF);
+	}
+	return (value & 0xFFFF);
+}
+
+static unsigned int checksum8(int n, unsigned char c[])
+{
+	unsigned long value = 0;
+	int i;
+
+	for (i = 0; i < n; i++) {
+		value += (c[i] & 0xFF);
+	}
+	return (value & 0xFF);
+}
+
+// CRC-16-CCITT
+static unsigned int crc16(int n, unsigned char c[])
+{
+#define CRC16POLY1  0x1021U  /* x^{16}+x^{12}+x^5+1 */
+#define CRC16POLY2  0x8408U  /* \x8D\xB6\x89E\x8Bt\x93] */
+
 	int i, j;
 	unsigned long r;
 
-	r = 0xFFFFFFFFUL;
+	r = 0xFFFFU;
 	for (i = 0; i < n; i++) {
-		r ^= (unsigned long)c[i] << (32 - CHAR_BIT);
+		r ^= c[i];
 		for (j = 0; j < CHAR_BIT; j++)
-			if (r & 0x80000000UL) r = (r << 1) ^ CRCPOLY1;
-			else                  r <<= 1;
+			if (r & 1) r = (r >> 1) ^ CRC16POLY2;
+			else       r >>= 1;
 	}
-	return ~r & 0xFFFFFFFFUL;
+	return r ^ 0xFFFFU;
 }
 
-static unsigned long crc2(int n, unsigned char c[])
+static unsigned long crc32(int n, unsigned char c[])
 {
+#define CRC32POLY1 0x04C11DB7UL
+	/* x^{32}+x^{26}+x^{23}+x^{22}+x^{16}+x^{12}+x^{11]+
+	   x^{10}+x^8+x^7+x^5+x^4+x^2+x^1+1 */
+#define CRC32POLY2 0xEDB88320UL  /* \x8D\xB6\x89E\x8Bt\x93] */
 	int i, j;
 	unsigned long r;
 
@@ -666,21 +712,19 @@
 	for (i = 0; i < n; i++) {
 		r ^= c[i];
 		for (j = 0; j < CHAR_BIT; j++)
-			if (r & 1) r = (r >> 1) ^ CRCPOLY2;
+			if (r & 1) r = (r >> 1) ^ CRC32POLY2;
 			else       r >>= 1;
 	}
 	return r ^ 0xFFFFFFFFUL;
 }
 
-// CRC32\x82̌v\x8EZ\x82\xF0\x8Ds\x82\xA4\x81B
-//
-// \x8F\x91\x8E\xAE: crc32 intvar str
-//
-WORD TTLCrc32()
+// \x83`\x83F\x83b\x83N\x83T\x83\x80\x83A\x83\x8B\x83S\x83\x8A\x83Y\x83\x80\x81E\x8B\xA4\x92ʃ\x8B\x81[\x83`\x83\x93
+WORD TTLDoChecksum(enum checksum_type type)
 {
 	TStrVal Str;
 	WORD Err;
 	TVarId VarId;
+	DWORD cksum;
 
 	Err = 0;
 	GetIntVar(&VarId, &Err);
@@ -690,16 +734,33 @@
 	if (Err!=0) return Err;
 	if (Str[0]==0) return Err;
 
-	SetIntVal(VarId, crc2(strlen(Str), Str));
+	switch (type) {
+		case CHECKSUM8:
+			cksum = checksum8(strlen(Str), Str);
+			break;
+		case CHECKSUM16:
+			cksum = checksum16(strlen(Str), Str);
+			break;
+		case CHECKSUM32:
+			cksum = checksum32(strlen(Str), Str);
+			break;
+		case CRC16:
+			cksum = crc16(strlen(Str), Str);
+			break;
+		case CRC32:
+			cksum = crc32(strlen(Str), Str);
+			break;
+		default:
+			cksum = 0;
+			break;
+	}
 
+	SetIntVal(VarId, cksum);
+
 	return Err;
 }
 
-// CRC32\x82̌v\x8EZ\x82\xF0\x8Ds\x82\xA4\x81B
-//
-// \x8F\x91\x8E\xAE: crc32file intvar filename
-//
-WORD TTLCrc32File()
+WORD TTLDoChecksumFile(enum checksum_type type)
 {
 	TStrVal Str;
 	int result = 0;
@@ -708,6 +769,7 @@
 	HANDLE fh = INVALID_HANDLE_VALUE, hMap = NULL;
 	LPBYTE lpBuf = NULL;
 	DWORD fsize;
+	DWORD cksum;
 
 	Err = 0;
 	GetIntVar(&VarId, &Err);
@@ -739,8 +801,29 @@
 
 	fsize = GetFileSize(fh,NULL);
 
-	SetIntVal(VarId, crc2(fsize, lpBuf));
+	switch (type) {
+		case CHECKSUM8:
+			cksum = checksum8(fsize, lpBuf);
+			break;
+		case CHECKSUM16:
+			cksum = checksum16(fsize, lpBuf);
+			break;
+		case CHECKSUM32:
+			cksum = checksum32(fsize, lpBuf);
+			break;
+		case CRC16:
+			cksum = crc16(fsize, lpBuf);
+			break;
+		case CRC32:
+			cksum = crc32(fsize, lpBuf);
+			break;
+		default:
+			cksum = 0;
+			break;
+	}
 
+	SetIntVal(VarId, cksum);
+
 error:
 	if (lpBuf != NULL) {
 		UnmapViewOfFile(lpBuf);
@@ -5304,6 +5387,18 @@
 			Err = TTLCommCmdInt(CmdCallMenu,0); break;
 		case RsvChangeDir:
 			Err = TTLCommCmdFile(CmdChangeDir,0); break;
+		case RsvChecksum8:
+			Err = TTLDoChecksum(CHECKSUM8); break;
+		case RsvChecksum8File:
+			Err = TTLDoChecksumFile(CHECKSUM8); break;
+		case RsvChecksum16:
+			Err = TTLDoChecksum(CHECKSUM16); break;
+		case RsvChecksum16File:
+			Err = TTLDoChecksumFile(CHECKSUM16); break;
+		case RsvChecksum32:
+			Err = TTLDoChecksum(CHECKSUM32); break;
+		case RsvChecksum32File:
+			Err = TTLDoChecksumFile(CHECKSUM32); break;
 		case RsvClearScreen:
 			Err = TTLCommCmdInt(CmdClearScreen,0); break;
 		case RsvClipb2Var:
@@ -5317,10 +5412,14 @@
 		case RsvConnect:
 		case RsvCygConnect:
 			Err = TTLConnect(WId); break;
+		case RsvCrc16:
+			Err = TTLDoChecksum(CRC16); break;
+		case RsvCrc16File:
+			Err = TTLDoChecksumFile(CRC16); break;
 		case RsvCrc32:
-			Err = TTLCrc32(); break;
+			Err = TTLDoChecksum(CRC32); break;
 		case RsvCrc32File:
-			Err = TTLCrc32File(); break;
+			Err = TTLDoChecksumFile(CRC32); break;
 		case RsvDelPassword:
 			Err = TTLDelPassword(); break;
 		case RsvDirname:

Modified: trunk/teraterm/ttpmacro/ttmparse.c
===================================================================
--- trunk/teraterm/ttpmacro/ttmparse.c	2013-03-26 12:25:07 UTC (rev 5175)
+++ trunk/teraterm/ttpmacro/ttmparse.c	2013-03-26 15:18:45 UTC (rev 5176)
@@ -156,6 +156,12 @@
 		if (_stricmp(Str,"call")==0) *WordId = RsvCall;
 		else if (_stricmp(Str,"callmenu")==0) *WordId = RsvCallMenu;
 		else if (_stricmp(Str,"changedir")==0) *WordId = RsvChangeDir;
+		else if (_stricmp(Str,"checksum8")==0) *WordId = RsvChecksum8;
+		else if (_stricmp(Str,"checksum8file")==0) *WordId = RsvChecksum8File;
+		else if (_stricmp(Str,"checksum16")==0) *WordId = RsvChecksum16;
+		else if (_stricmp(Str,"checksum16file")==0) *WordId = RsvChecksum16File;
+		else if (_stricmp(Str,"checksum32")==0) *WordId = RsvChecksum32;
+		else if (_stricmp(Str,"checksum32file")==0) *WordId = RsvChecksum32File;
 		else if (_stricmp(Str,"clearscreen")==0) *WordId = RsvClearScreen;
 		else if (_stricmp(Str,"clipb2var")==0) *WordId = RsvClipb2Var;  // add 'clipb2var' (2006.9.17 maya)
 		else if (_stricmp(Str,"closesbox")==0) *WordId = RsvCloseSBox;
@@ -163,6 +169,8 @@
 		else if (_stricmp(Str,"code2str")==0) *WordId = RsvCode2Str;
 		else if (_stricmp(Str,"connect")==0) *WordId = RsvConnect;
 		else if (_stricmp(Str,"continue")==0) *WordId = RsvContinue;
+		else if (_stricmp(Str,"crc16")==0) *WordId = RsvCrc16;
+		else if (_stricmp(Str,"crc16file")==0) *WordId = RsvCrc16File;
 		else if (_stricmp(Str,"crc32")==0) *WordId = RsvCrc32;
 		else if (_stricmp(Str,"crc32file")==0) *WordId = RsvCrc32File;
 		else if (_stricmp(Str,"cygconnect")==0) *WordId = RsvCygConnect;

Modified: trunk/teraterm/ttpmacro/ttmparse.h
===================================================================
--- trunk/teraterm/ttpmacro/ttmparse.h	2013-03-26 12:25:07 UTC (rev 5175)
+++ trunk/teraterm/ttpmacro/ttmparse.h	2013-03-26 15:18:45 UTC (rev 5176)
@@ -228,6 +228,14 @@
 #define RsvGetIPv4Addr  199
 #define RsvGetIPv6Addr  200
 #define RsvLogRotate    201
+#define RsvCrc16        202
+#define RsvCrc16File    203
+#define RsvChecksum8    204
+#define RsvChecksum8File    205
+#define RsvChecksum16   206
+#define RsvChecksum16File   207
+#define RsvChecksum32   208
+#define RsvChecksum32File   209
 
 #define RsvOperator     1000
 #define RsvBNot         1001



Ttssh2-commit メーリングリストの案内