Revision: 10712 https://osdn.net/projects/ttssh2/scm/svn/commits/10712 Author: zmatsuo Date: 2023-05-18 22:21:38 +0900 (Thu, 18 May 2023) Log Message: ----------- xmodem.c コメント追加、コンパイラ警告対策 Modified Paths: -------------- trunk/teraterm/teraterm/filesys_proto.h trunk/teraterm/ttpfile/xmodem.c -------------- next part -------------- Modified: trunk/teraterm/teraterm/filesys_proto.h =================================================================== --- trunk/teraterm/teraterm/filesys_proto.h 2023-05-18 13:21:29 UTC (rev 10711) +++ trunk/teraterm/teraterm/filesys_proto.h 2023-05-18 13:21:38 UTC (rev 10712) @@ -50,7 +50,8 @@ // \x81\xAB\x8Ae\x83v\x83\x8D\x83g\x83R\x83\x8B\x82Ŏg\x97p\x82\xB7\x82郏\x81[\x83N BOOL FileOpen; - LONG FileSize, ByteCount; + LONG FileSize; + LONG ByteCount; BOOL OverWrite; BOOL Success; Modified: trunk/teraterm/ttpfile/xmodem.c =================================================================== --- trunk/teraterm/ttpfile/xmodem.c 2023-05-18 13:21:29 UTC (rev 10711) +++ trunk/teraterm/ttpfile/xmodem.c 2023-05-18 13:21:38 UTC (rev 10712) @@ -27,7 +27,7 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* TTFILE.DLL, XMODEM protocol */ +/* XMODEM protocol */ #include "teraterm.h" #include "tttypes.h" #include <stdio.h> @@ -38,15 +38,28 @@ #include "xmodem.h" -/* XMODEM */ +typedef enum { + XpktSOH = 1, + XpktBLK = 2, + XpktBLK2 = 3, + XpktDATA = 4, +} PKT_READ_MODE; + +typedef enum { + XnakNAK = 1, + XnakC = 2 +} NAK_MODE; + typedef struct { BYTE PktIn[1030], PktOut[1030]; int PktBufCount, PktBufPtr; BYTE PktNum, PktNumSent; int PktNumOffset; - int PktReadMode; - WORD XMode, XOpt, TextFlag; - WORD NAKMode; + PKT_READ_MODE PktReadMode; + WORD XMode, XOpt; + BOOL TextFlagConvertCRLF; + BOOL TextFlagTrim1A; + NAK_MODE NAKMode; int NAKCount; WORD DataLen, CheckLen; BOOL CRRecv; @@ -62,17 +75,6 @@ } TXVar; typedef TXVar far *PXVar; -/* prototypes */ - -/* XMODEM states */ -#define XpktSOH 1 -#define XpktBLK 2 -#define XpktBLK2 3 -#define XpktDATA 4 - -#define XnakNAK 1 -#define XnakC 2 - static void XCancel(PFileVarProto fv, PComVar cv); static int XRead1Byte(PFileVarProto fv, PXVar xv, PComVar cv, LPBYTE b) @@ -95,9 +97,10 @@ return 1; } -static int XWrite(PFileVarProto fv, PXVar xv, PComVar cv, PCHAR B, int C) +static int XWrite(PFileVarProto fv, PXVar xv, PComVar cv, const void *_B, size_t C) { int i, j; + const char *B = (char *)_B; i = CommBinaryOut(cv, B, C); if (xv->log != NULL && (i > 0)) { @@ -183,7 +186,7 @@ fv->FTSetTimeOut(fv, t); } -static WORD XCalcCheck(PXVar xv, PCHAR PktBuf) +static WORD XCalcCheck(PXVar xv, const BYTE *PktBuf) { int i; WORD Check; @@ -229,11 +232,18 @@ xv->FullName = fv->GetNextFname(fv); if (xv->XMode == IdXSend) { + size_t size; fv->FileOpen = file->OpenRead(file, xv->FullName); if (fv->FileOpen == FALSE) { return FALSE; } - fv->FileSize = file->GetFSize(file, xv->FullName); + size = file->GetFSize(file, xv->FullName); + if (size > 0x7FFFFFFF) { // LONG_MAX + // TODO + // FileSize \x82͊e\x83v\x83\x8D\x83g\x83R\x83\x8B\x82\xB2\x82ƂɎ\x9D\x82ق\xA4\x82\xAA\x82悳\x82\xBB\x82\xA4 + return FALSE; + } + fv->FileSize = (LONG)size; fv->InitDlgProgress(fv, &fv->ProgStat); } else { fv->FileOpen = file->OpenWrite(file, xv->FullName); @@ -279,8 +289,6 @@ switch (xv->XMode) { case IdXSend: - xv->TextFlag = 0; - // \x83t\x83@\x83C\x83\x8B\x91\x97\x90M\x8AJ\x8En\x91O\x82ɁA"rx \x83t\x83@\x83C\x83\x8B\x96\xBC"\x82\xF0\x8E\xA9\x93\xAE\x93I\x82ɌĂяo\x82\xB7\x81B(2007.12.20 yutaka) if (ts->XModemRcvCommand[0] != '\0') { TFileIO *file = fv->file; @@ -446,21 +454,35 @@ xv->PktNumOffset = xv->PktNumOffset + 256; c = xv->DataLen; - if (xv->TextFlag > 0) + if (xv->TextFlagTrim1A) { + // \x83p\x83P\x83b\x83g\x96\x96\x82\xCC 0x1A \x82\xF0\x8D폜 while ((c > 0) && (xv->PktIn[2 + c] == 0x1A)) c--; + } - if (xv->TextFlag > 0) + if (xv->TextFlagConvertCRLF) { + // \x83p\x83P\x83b\x83g\x93\xE0\x82\xCCCR\x82̂\xDDLF\x82݂̂\xF0CR+LF\x82ɕϊ\xB7\x82\xB5\x82Ȃ\xAA\x82\xE7\x83t\x83@\x83C\x83\x8B\x82֏o\x97\xCD for (i = 0; i <= c - 1; i++) { b = xv->PktIn[3 + i]; - if ((b == LF) && (!xv->CRRecv)) + if ((b == LF) && (!xv->CRRecv)) { + // LF(0x0A)\x8E\xF3\x90M && \x91O\x82̃f\x81[\x83^\x82\xAACR(0x0D)\x82ł͂Ȃ\xA2 + // CR(0x0D)\x8Fo\x97\xCD + // \x81\xA8 (CR\x88ȊO)+(LF) \x82́A(CR\x88ȊO)+(CR)+(LF)\x82ŏo\x97͂\xB3\x82\xEA\x82\xE9 file->WriteFile(file, "\015", 1); - if (xv->CRRecv && (b != LF)) + } + if (xv->CRRecv && (b != LF)) { + // LF(0x0A)\x88ȊO\x8E\xF3\x90M && \x91O\x82̃f\x81[\x83^\x82\xAACR(0xOD) + // LF(0x0A)\x8Fo\x97\xCD + // \x81\xA8 (CR)+(LF\x88ȊO)\x82́A(CR)+(LF)+(LF\x88ȊO)\x82ŏo\x97͂\xB3\x82\xEA\x82\xE9 file->WriteFile(file, "\012", 1); + } xv->CRRecv = b == CR; file->WriteFile(file, &b, 1); - } else + } + } else { + // \x82\xBB\x82̂܂܃t\x83@\x83C\x83\x8B\x82֏o\x97\xCD file->WriteFile(file, &(xv->PktIn[3]), c); + } fv->ByteCount = fv->ByteCount + c; @@ -527,10 +549,10 @@ break; case 0x43: if ((xv->PktNum == 0) && (xv->PktNumOffset == 0) && (xv->PktNumSent == 0)) { - if ((xv->XOpt == XoptCheck)) { + if (xv->XOpt == XoptCheck) { XSetOpt(fv, xv, XoptCRC); } - else if ((xv->XOpt == Xopt1kCksum)) { + else if (xv->XOpt == Xopt1kCksum) { XSetOpt(fv, xv, Xopt1kCRC); } SendFlag = TRUE; @@ -651,18 +673,20 @@ PXVar xv = fv->data; switch(request) { case XMODEM_MODE: { - int Mode = va_arg(ap, int); + const int Mode = va_arg(ap, int); xv->XMode = Mode; return 0; } case XMODEM_OPT: { - WORD Opt1 = va_arg(ap, int); + const WORD Opt1 = va_arg(ap, int); xv->XOpt = Opt1; return 0; } case XMODEM_TEXT_FLAG: { - WORD Opt2 = va_arg(ap, int); - xv->TextFlag = Opt2; + const WORD Opt2 = va_arg(ap, int); + const BOOL TextFlag = Opt2 == 0 ? FALSE : TRUE; + xv->TextFlagConvertCRLF = TextFlag; + xv->TextFlagTrim1A = TextFlag; return 0; } }