/*
 * zmodem.h
 *
 *  Created on: Sep 3, 2024
 *      Author: tm
 */

#ifndef ZMODEM_H_
#define ZMODEM_H_



// cf2_zmod.h
/************************************************************************/
/* Copyright 2013 MBARI                         */
/************************************************************************/
/* Summary  : Include file for porting Embedded ZModem to BEDS on Persistor*/
/* Filename : bedszm.h                          */
/* Author   : Robert Herlien (rah)                  */
/* Project  : Benthic Event Detection System (BEDS)         */
/* Revision : 1.0                           */
/* Created  : 10/2/2013                         */
/*                                      */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*                                      */
/************************************************************************/
/* Modification History:                        */
/* 2oct2013 rah - created                       */
/************************************************************************/
/* This file was created for porting Embedded ZModem from Omen to BEDS  */
/* on the CF2.  It's needed primarily because the ^*%&^%* Metrowerks    */
/* compiler does not support -Ddef, as is standard for every other  */
/* C compiler in the modern world.                  */
/************************************************************************/

#ifndef ZMODEM_INC
#define ZMODEM_INC          1

//#include <cfxpico.h>

/* Defines that are normally in Makefile as -Ddef   */

#define DIAGS
#define DEBUGZ
#define ZSTAB
#define MANYREGS
#define NOCOPYTX
#define UNSL unsigned

/* Added this one, 7oct2013, rah, MBARI         */
#define HAVE_STAT

//#define flushmo() CIOdrain()
#define zbitch(s)   zperr(s)

/* Declarations for externally called functions     */
int zmSendFiles(char *path, char *name, int verbose, unsigned window, unsigned framelen);

/* From isz.c       */
void    startz(void);
int     wcs(char *name);
void    saybibi(void);

/* From bedszm.c - these are the (mostly) MBARI-written functions for   */
/* porting Industrial ZModem to PicoDOS on the Persistor CF2        */
void    sendline(int c);
int     readline(int timeout);
int     checkline(void);
void    purgeline(void);
void    vfile(char *f, ...);
void    zperr(char *s, ...);
void    zclose(int);
int     zseek(long n);
int     zfilbuf(void);

/* Add'l functions for Persistor port   */
int     insertStat(char *q);

/* Forward declarations, because compiler pedantically spits out warnings*/
/* if function has no prototype and is not declared static, even if */
/* function appears ahead of any use of it              */

/* From izm.c       */
void    zsbh32(char *hdr, int type);

/* From izmr.c      */
void    zsdar32(char *buf, int length, int frameend);
int     zrdatr32(char *buf, int length);

#endif




// izm.h
//#include "cf2_zmod.h"   /* Added 2013/10/2, rah, MBARI, for BEDS port*/

#define ADDRLE
/*
 *   Z M O D E M . H     Manifest constants for ZMODEM
 *    application to application file transfer protocol
 *  Copyright 2004 2011 Omen Technology INC All Rights Reserved
 *     This is UNPUBLISHED PROPRIETARY SOURCE CODE of Omen Technology INC.
 *     The Copyright notice above does not evidence any actual or intended
 *     publication such source code.
 */
#define IZOK 0
#define FALSE 0
#define TRUE 1
#define IZERROR (-1)
#define TIMEOUT (-2)
/* Ward Christensen / CP/M parameters - Don't change these! */
#define CAN ('X'&037)
#define XOFF ('s'&037)
#define XON ('q'&037)


#define XON ('q'&037)
#define XOFF ('s'&037)
#define ZPAD '*'    /* 052 Padding character begins frames */
#define ZDLE 030    /* Ctrl-X Zmodem escape - `ala BISYNC DLE */
#define ZDLEE (ZDLE^0100)   /* Escaped ZDLE as transmitted */
#define ZBIN 'A'    /* Binary frame indicator */
#define ZHEX 'B'    /* HEX frame indicator */
#define ZBIN32 'C'  /* Binary frame with 32 bit FCS */
#define ZBINR32 'D' /* RLE packed Binary frame with 32 bit FCS */
#define ZRESC   0176    /* RLE flag/escape character */

/* Frame types (see array "frametypes" in izm.c) */
#define ZRQINIT 0   /* Request receive init */
#define ZRINIT  1   /* Receive init */
#define ZSINIT 2    /* Send init sequence (optional) */
#define ZACK 3      /* ACK to above */
#define ZFILE 4     /* File name from sender */
#define ZSKIP 5     /* To sender: skip this file */
#define ZNAK 6      /* Last packet was garbled */
#define ZABORT 7    /* Abort batch transfers */
#define ZFIN 8      /* Finish session */
#define ZRPOS 9     /* Resume data trans at this position */
#define ZDATA 10    /* Data packet(s) follow */
#define ZEOF 11     /* End of file */
#define ZFERR 12    /* Fatal Read or Write error Detected */
#define ZCRC 13     /* Request for file CRC and response */
#define ZCHALLENGE 14   /* Receiver's Challenge */
#define ZCOMPL 15   /* Request is complete */
#define ZCAN 16     /* Other end canned session with CAN*5 */
#define ZFREECNT 17 /* Request for free bytes on filesystem */
#define ZCOMMAND 18 /* Command from sending program */

/* ZDLE sequences */
#define ZCRCE 'h'   /* CRC next, frame ends, header packet follows */
#define ZCRCG 'i'   /* CRC next, frame continues nonstop */
#define ZCRCQ 'j'   /* CRC next, frame continues, ZACK expected */
#define ZCRCW 'k'   /* CRC next, ZACK expected, end of frame */
#define ZRUB0 'l'   /* Translate to rubout 0177 */
#define ZRUB1 'm'   /* Translate to rubout 0377 */

/* zdlread return values (internal) */
/* -1 is general error, -2 is timeout */
#define GOTOR 0400
#define GOTCRCE (ZCRCE|GOTOR)   /* ZDLE-ZCRCE received */
#define GOTCRCG (ZCRCG|GOTOR)   /* ZDLE-ZCRCG received */
#define GOTCRCQ (ZCRCQ|GOTOR)   /* ZDLE-ZCRCQ received */
#define GOTCRCW (ZCRCW|GOTOR)   /* ZDLE-ZCRCW received */
#define GOTCAN  (GOTOR|030) /* CAN*5 seen */

/* Byte positions within header array */
#define ZF0 3   /* First flags byte */
#define ZF1 2
#define ZF2 1
#define ZF3 0
#define ZP0 0   /* Low order 8 bits of position */
#define ZP1 1
#define ZP2 2
#define ZP3 3   /* High order 8 bits of file position */

/* Bit Masks for ZRINIT flags byte ZF0 */
#define CANFDX  01  /* Rx can send and receive true FDX */
#define CANOVIO 02  /* Rx can receive data during disk I/O */
#define CANBRK  04  /* Rx can send a break signal */
#define CANRLE  010 /* Receiver can decrypt */
#define CANLZW  020 /* Receiver can uncompress */
#define CANFC32 040 /* Receiver can use 32 bit Frame Check */
#define ESCCTL 0100 /* Receiver expects ctl chars to be escaped */
#define ESC8   0200 /* Receiver expects 8th bit to be escaped */

/* Bit Masks for ZRINIT flags byte ZF1 */
#define CANLDP  128 /* Receiver understands LDP protection */

/* Bit Masks for ZRINIT flags byte ZF2 */
#define ZGOTFILEOK 1    /* Last file received and closed */

/* Extended options for ZF3, bit encoded */
#define ZSRWKK  8   /* Sender will use long subpackets */


/* Parameters for ZSINIT frame */
#define ZATTNLEN 32 /* Max length of attention string */
/* Bit Masks for ZSINIT flags byte ZF0 */
#define TESCCTL 0100    /* Transmitter expects ctl chars to be escaped */
#define TESC8   0200    /* Transmitter expects 8th bit to be escaped */

/* Parameters for ZFILE frame */
/* Conversion options one of these in ZF0 */
#define ZCBIN   1   /* Binary transfer - inhibit conversion */
#define ZCNL    2   /* Convert NL to local end of line convention */
#define ZCRESUM 3   /* Resume interrupted file transfer */
/* Management include options, one of these ored in ZF1 */
#define ZMSKNOLOC   0200    /* Skip file if not present at rx */
/* Management options, one of these ored in ZF1 */
#define ZMMASK  037 /* Mask for the choices below */
#define ZMNEWL  1   /* Transfer if source newer or longer */
#define ZMCRC   2   /* Transfer if different file CRC or length */
#define ZMAPND  3   /* Append contents to existing file (if any) */
#define ZMCLOB  4   /* Replace existing file */
#define ZMNEW   5   /* Transfer if source newer */
    /* Number 5 is alive ... */
#define ZMDIFF  6   /* Transfer if dates or lengths different */
#define ZMPROT  7   /* Protect destination file */
/* Transport options, one of these in ZF2 */
#define ZTLZW   1   /* Lempel-Ziv compression */
#define ZTCRYPT 2   /* Encryption */
#define ZTRLE   3   /* Run Length encoding */
/* Extended options for ZF3, bit encoded */
#define ZSLDP   16  /* CRCs will be biased for LDP protection */
#define ZXSPARS 64  /* Encoding for sparse file operations */

/* Parameters for ZCOMMAND frame ZF0 (otherwise 0) */
#define ZCACK1  1   /* Acknowledge, then do command */

/* Globals used by ZMODEM functions */
extern int Rxframeind;  /* ZBIN ZBIN32, or ZHEX type of frame received */
extern int Rxtype;      /* Type of header received */
extern int Rxcount;     /* Count of data bytes received */
extern long bytcnt;
extern int Zrwindow;    /* RX window size (controls garbage count) */
extern int Rxtimeout;   /* Tenths of seconds to wait for something */
extern char Rxhdr[4];   /* Received header */
extern char Txhdr[8];   /* Transmitted header */
extern long Rxpos;  /* Received file position */
extern long Txpos;  /* Transmitted file position */

extern int Txfcs32;     /* TURE means send binary frames with 32 bit FCS */
extern int Crc32t;      /* Display flag indicating 32 bit CRC being sent */

#ifdef LDP
extern int Zldpactive;  /* LDP protection active */
extern char Zaddr8[8];  /* Array for address CRC bias */
extern unsigned long Zcrcpreset;    /* CRC preset from filename header */
extern unsigned long Zcrcinit;  /* CRC init for zrdata zsdata */
extern char zcpyrldp[];
int Zfileclosedok;  /* last file was received and closed ok */
#endif

int readline(), zgethdr(), zrdata();
void mputs();
void zsendline(), purgeline();
void saybibi(), startz();


void canit(), stohdr();
long rclhdr();
void zputhex();
void zsbhdr(), zshhdr(), zsdata();
int openit(), rz(), wcs();

void  zsda32();

void flushline(), flushmo(), flushmoc();
void sendline();

#ifdef DIAGS
//void vfile(), zperr();    //Moved to bedszm.h
#endif

/* End of IZM.H */




// izmr.h
//#include "cf2_zmod.h"   /* Added 2013/10/2, rah, MBARI, for BEDS port*/

#define ADDRLE
/*
 *   Z M O D E M . H     Manifest constants for ZMODEM
 *    application to application file transfer protocol
 *  Copyright 2004 2011 Omen Technology INC All Rights Reserved
 *     This is UNPUBLISHED PROPRIETARY SOURCE CODE of Omen Technology INC.
 *     The Copyright notice above does not evidence any actual or intended
 *     publication such source code.
 */
#define IZOK 0
#define FALSE 0
#define TRUE 1
#define IZERROR (-1)
#define TIMEOUT (-2)
/* Ward Christensen / CP/M parameters - Don't change these! */
#define CAN ('X'&037)
#define XOFF ('s'&037)
#define XON ('q'&037)


#define XON ('q'&037)
#define XOFF ('s'&037)
#define ZPAD '*'    /* 052 Padding character begins frames */
#define ZDLE 030    /* Ctrl-X Zmodem escape - `ala BISYNC DLE */
#define ZDLEE (ZDLE^0100)   /* Escaped ZDLE as transmitted */
#define ZBIN 'A'    /* Binary frame indicator */
#define ZHEX 'B'    /* HEX frame indicator */
#define ZBIN32 'C'  /* Binary frame with 32 bit FCS */
#define ZBINR32 'D' /* RLE packed Binary frame with 32 bit FCS */
#define ZRESC   0176    /* RLE flag/escape character */

/* Frame types (see array "frametypes" in izm.c) */
#define ZRQINIT 0   /* Request receive init */
#define ZRINIT  1   /* Receive init */
#define ZSINIT 2    /* Send init sequence (optional) */
#define ZACK 3      /* ACK to above */
#define ZFILE 4     /* File name from sender */
#define ZSKIP 5     /* To sender: skip this file */
#define ZNAK 6      /* Last packet was garbled */
#define ZABORT 7    /* Abort batch transfers */
#define ZFIN 8      /* Finish session */
#define ZRPOS 9     /* Resume data trans at this position */
#define ZDATA 10    /* Data packet(s) follow */
#define ZEOF 11     /* End of file */
#define ZFERR 12    /* Fatal Read or Write error Detected */
#define ZCRC 13     /* Request for file CRC and response */
#define ZCHALLENGE 14   /* Receiver's Challenge */
#define ZCOMPL 15   /* Request is complete */
#define ZCAN 16     /* Other end canned session with CAN*5 */
#define ZFREECNT 17 /* Request for free bytes on filesystem */
#define ZCOMMAND 18 /* Command from sending program */

/* ZDLE sequences */
#define ZCRCE 'h'   /* CRC next, frame ends, header packet follows */
#define ZCRCG 'i'   /* CRC next, frame continues nonstop */
#define ZCRCQ 'j'   /* CRC next, frame continues, ZACK expected */
#define ZCRCW 'k'   /* CRC next, ZACK expected, end of frame */
#define ZRUB0 'l'   /* Translate to rubout 0177 */
#define ZRUB1 'm'   /* Translate to rubout 0377 */

/* zdlread return values (internal) */
/* -1 is general error, -2 is timeout */
#define GOTOR 0400
#define GOTCRCE (ZCRCE|GOTOR)   /* ZDLE-ZCRCE received */
#define GOTCRCG (ZCRCG|GOTOR)   /* ZDLE-ZCRCG received */
#define GOTCRCQ (ZCRCQ|GOTOR)   /* ZDLE-ZCRCQ received */
#define GOTCRCW (ZCRCW|GOTOR)   /* ZDLE-ZCRCW received */
#define GOTCAN  (GOTOR|030) /* CAN*5 seen */

/* Byte positions within header array */
#define ZF0 3   /* First flags byte */
#define ZF1 2
#define ZF2 1
#define ZF3 0
#define ZP0 0   /* Low order 8 bits of position */
#define ZP1 1
#define ZP2 2
#define ZP3 3   /* High order 8 bits of file position */

/* Bit Masks for ZRINIT flags byte ZF0 */
#define CANFDX  01  /* Rx can send and receive true FDX */
#define CANOVIO 02  /* Rx can receive data during disk I/O */
#define CANBRK  04  /* Rx can send a break signal */
#define CANRLE  010 /* Receiver can decrypt */
#define CANLZW  020 /* Receiver can uncompress */
#define CANFC32 040 /* Receiver can use 32 bit Frame Check */
#define ESCCTL 0100 /* Receiver expects ctl chars to be escaped */
#define ESC8   0200 /* Receiver expects 8th bit to be escaped */

/* Bit Masks for ZRINIT flags byte ZF1 */
#define CANLDP  128 /* Receiver understands LDP protection */

/* Bit Masks for ZRINIT flags byte ZF2 */
#define ZGOTFILEOK 1    /* Last file received and closed */

/* Extended options for ZF3, bit encoded */
#define ZSRWKK  8   /* Sender will use long subpackets */


/* Parameters for ZSINIT frame */
#define ZATTNLEN 32 /* Max length of attention string */
/* Bit Masks for ZSINIT flags byte ZF0 */
#define TESCCTL 0100    /* Transmitter expects ctl chars to be escaped */
#define TESC8   0200    /* Transmitter expects 8th bit to be escaped */

/* Parameters for ZFILE frame */
/* Conversion options one of these in ZF0 */
#define ZCBIN   1   /* Binary transfer - inhibit conversion */
#define ZCNL    2   /* Convert NL to local end of line convention */
#define ZCRESUM 3   /* Resume interrupted file transfer */
/* Management include options, one of these ored in ZF1 */
#define ZMSKNOLOC   0200    /* Skip file if not present at rx */
/* Management options, one of these ored in ZF1 */
#define ZMMASK  037 /* Mask for the choices below */
#define ZMNEWL  1   /* Transfer if source newer or longer */
#define ZMCRC   2   /* Transfer if different file CRC or length */
#define ZMAPND  3   /* Append contents to existing file (if any) */
#define ZMCLOB  4   /* Replace existing file */
#define ZMNEW   5   /* Transfer if source newer */
    /* Number 5 is alive ... */
#define ZMDIFF  6   /* Transfer if dates or lengths different */
#define ZMPROT  7   /* Protect destination file */
/* Transport options, one of these in ZF2 */
#define ZTLZW   1   /* Lempel-Ziv compression */
#define ZTCRYPT 2   /* Encryption */
#define ZTRLE   3   /* Run Length encoding */
/* Extended options for ZF3, bit encoded */
#define ZSLDP   16  /* CRCs will be biased for LDP protection */
#define ZXSPARS 64  /* Encoding for sparse file operations */

/* Parameters for ZCOMMAND frame ZF0 (otherwise 0) */
#define ZCACK1  1   /* Acknowledge, then do command */

/* Globals used by ZMODEM functions */
extern int Rxframeind;  /* ZBIN ZBIN32, or ZHEX type of frame received */
extern int Rxtype;      /* Type of header received */
extern int Rxcount;     /* Count of data bytes received */
extern long bytcnt;
extern int Zrwindow;    /* RX window size (controls garbage count) */
extern int Rxtimeout;   /* Tenths of seconds to wait for something */
extern char Rxhdr[4];   /* Received header */
extern char Txhdr[8];   /* Transmitted header */
extern long Rxpos;  /* Received file position */
extern long Txpos;  /* Transmitted file position */

extern int Txfcs32;     /* TURE means send binary frames with 32 bit FCS */
extern int Crc32t;      /* Display flag indicating 32 bit CRC being sent */

#ifdef LDP
extern int Zldpactive;  /* LDP protection active */
extern char Zaddr8[8];  /* Array for address CRC bias */
extern unsigned long Zcrcpreset;    /* CRC preset from filename header */
extern unsigned long Zcrcinit;  /* CRC init for zrdata zsdata */
extern char zcpyrldp[];
int Zfileclosedok;  /* last file was received and closed ok */
#endif

int readline(), zgethdr(), zrdata();
void mputs();
void zsendline(), purgeline();
void saybibi(), startz();


void canit(), stohdr();
long rclhdr();
void zputhex();
void zsbhdr(), zshhdr(), zsdata();
int openit(), rz(), wcs();

void  zsda32();

void flushline(), flushmo(), flushmoc();
void sendline();

int zmodem_send(int argc, char *argv[]);

#ifdef DIAGS
//void vfile(), zperr();    //Moved to bedszm.h
#endif

/* End of IZM.H */


#endif /* ZMODEM_H_ */
