zlib, uart: fix mix of spaces & tabs

This commit is contained in:
Philippe Teuwen 2019-03-09 11:15:18 +01:00
commit 1fe75f2481
6 changed files with 426 additions and 426 deletions

View file

@ -44,11 +44,11 @@
#if defined (_WIN32) #if defined (_WIN32)
#define SERIAL_PORT_H "com3" #define SERIAL_PORT_H "com3"
#elif defined(__APPLE__) #elif defined(__APPLE__)
#define SERIAL_PORT_H "/dev/cu.usbmodem" #define SERIAL_PORT_H "/dev/cu.usbmodem"
#else #else
#define SERIAL_PORT_H "/dev/ttyACM0" #define SERIAL_PORT_H "/dev/ttyACM0"
#endif #endif
/* serial_port is declared as a void*, which you should cast to whatever type /* serial_port is declared as a void*, which you should cast to whatever type

View file

@ -62,363 +62,363 @@
typedef struct termios term_info; typedef struct termios term_info;
typedef struct { typedef struct {
int fd; // Serial port file descriptor int fd; // Serial port file descriptor
term_info tiOld; // Terminal info before using the port term_info tiOld; // Terminal info before using the port
term_info tiNew; // Terminal info during the transaction term_info tiNew; // Terminal info during the transaction
} serial_port_unix; } serial_port_unix;
// Set time-out on 30 miliseconds // Set time-out on 30 miliseconds
struct timeval timeout = { struct timeval timeout = {
.tv_sec = 0, // 0 second .tv_sec = 0, // 0 second
.tv_usec = 30000 // 30 000 micro seconds .tv_usec = 30000 // 30 000 micro seconds
}; };
serial_port uart_open(const char* pcPortName) { serial_port uart_open(const char* pcPortName) {
serial_port_unix* sp = calloc(sizeof(serial_port_unix), sizeof(uint8_t)); serial_port_unix* sp = calloc(sizeof(serial_port_unix), sizeof(uint8_t));
if (sp == 0) return INVALID_SERIAL_PORT; if (sp == 0) return INVALID_SERIAL_PORT;
if (memcmp(pcPortName, "tcp:", 4) == 0) { if (memcmp(pcPortName, "tcp:", 4) == 0) {
struct addrinfo *addr, *rp; struct addrinfo *addr, *rp;
char *addrstr = strdup(pcPortName + 4); char *addrstr = strdup(pcPortName + 4);
if (addrstr == NULL) { if (addrstr == NULL) {
printf("Error: strdup\n"); printf("Error: strdup\n");
free(sp); free(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
timeout.tv_usec = 300000; // 300 000 micro seconds timeout.tv_usec = 300000; // 300 000 micro seconds
char *colon = strrchr(addrstr, ':'); char *colon = strrchr(addrstr, ':');
char *portstr; char *portstr;
if (colon) { if (colon) {
portstr = colon + 1; portstr = colon + 1;
*colon = '\0'; *colon = '\0';
} else { } else {
portstr = "7901"; portstr = "7901";
} }
int s = getaddrinfo(addrstr, portstr, NULL, &addr); int s = getaddrinfo(addrstr, portstr, NULL, &addr);
if (s != 0) { if (s != 0) {
printf("Error: getaddrinfo: %s\n", gai_strerror(s)); printf("Error: getaddrinfo: %s\n", gai_strerror(s));
freeaddrinfo(addr); freeaddrinfo(addr);
free(addrstr); free(addrstr);
free(sp); free(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
int sfd; int sfd;
for (rp = addr; rp != NULL; rp = rp->ai_next) { for (rp = addr; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1) if (sfd == -1)
continue; continue;
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break; break;
close(sfd); close(sfd);
} }
if (rp == NULL) { /* No address succeeded */ if (rp == NULL) { /* No address succeeded */
printf("Error: Could not connect\n"); printf("Error: Could not connect\n");
freeaddrinfo(addr); freeaddrinfo(addr);
free(addrstr); free(addrstr);
free(sp); free(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
freeaddrinfo(addr); freeaddrinfo(addr);
free(addrstr); free(addrstr);
sp->fd = sfd; sp->fd = sfd;
int one = 1; int one = 1;
int res = setsockopt(sp->fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one)); int res = setsockopt(sp->fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
if ( res != 0) { if ( res != 0) {
free(sp); free(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
return sp; return sp;
} }
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (sp->fd == -1) { if (sp->fd == -1) {
uart_close(sp); uart_close(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
// Finally figured out a way to claim a serial port interface under unix // Finally figured out a way to claim a serial port interface under unix
// We just try to set a (advisory) lock on the file descriptor // We just try to set a (advisory) lock on the file descriptor
struct flock fl; struct flock fl;
fl.l_type = F_WRLCK; fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET; fl.l_whence = SEEK_SET;
fl.l_start = 0; fl.l_start = 0;
fl.l_len = 0; fl.l_len = 0;
fl.l_pid = getpid(); fl.l_pid = getpid();
// Does the system allows us to place a lock on this file descriptor // Does the system allows us to place a lock on this file descriptor
if (fcntl(sp->fd, F_SETLK, &fl) == -1) { if (fcntl(sp->fd, F_SETLK, &fl) == -1) {
// A conflicting lock is held by another process // A conflicting lock is held by another process
free(sp); free(sp);
return CLAIMED_SERIAL_PORT; return CLAIMED_SERIAL_PORT;
} }
// Try to retrieve the old (current) terminal info struct // Try to retrieve the old (current) terminal info struct
if (tcgetattr(sp->fd,&sp->tiOld) == -1) { if (tcgetattr(sp->fd,&sp->tiOld) == -1) {
uart_close(sp); uart_close(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
// Duplicate the (old) terminal info struct // Duplicate the (old) terminal info struct
sp->tiNew = sp->tiOld; sp->tiNew = sp->tiOld;
// Configure the serial port // Configure the serial port
sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD; sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD;
sp->tiNew.c_iflag = IGNPAR; sp->tiNew.c_iflag = IGNPAR;
sp->tiNew.c_oflag = 0; sp->tiNew.c_oflag = 0;
sp->tiNew.c_lflag = 0; sp->tiNew.c_lflag = 0;
// Block until n bytes are received // Block until n bytes are received
sp->tiNew.c_cc[VMIN] = 0; sp->tiNew.c_cc[VMIN] = 0;
// Block until a timer expires (n * 100 mSec.) // Block until a timer expires (n * 100 mSec.)
sp->tiNew.c_cc[VTIME] = 0; sp->tiNew.c_cc[VTIME] = 0;
// Try to set the new terminal info struct // Try to set the new terminal info struct
if (tcsetattr(sp->fd, TCSANOW, &sp->tiNew) == -1) { if (tcsetattr(sp->fd, TCSANOW, &sp->tiNew) == -1) {
uart_close(sp); uart_close(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
// Flush all lingering data that may exist // Flush all lingering data that may exist
tcflush(sp->fd, TCIOFLUSH); tcflush(sp->fd, TCIOFLUSH);
#ifdef WITH_FPC #ifdef WITH_FPC
if ( uart_set_speed(sp, 115200) ) { if ( uart_set_speed(sp, 115200) ) {
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n"); printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
} else { } else {
uart_set_speed(sp, 9600); uart_set_speed(sp, 9600);
printf("[=] UART Setting serial baudrate 9600 [FPC enabled]\n"); printf("[=] UART Setting serial baudrate 9600 [FPC enabled]\n");
} }
#else #else
// set speed, works for UBUNTU 14.04 // set speed, works for UBUNTU 14.04
bool success = uart_set_speed(sp, 460800); bool success = uart_set_speed(sp, 460800);
if (success) { if (success) {
printf("[=] UART Setting serial baudrate 460800\n"); printf("[=] UART Setting serial baudrate 460800\n");
} else { } else {
uart_set_speed(sp, 115200); uart_set_speed(sp, 115200);
printf("[=] UART Setting serial baudrate 115200\n"); printf("[=] UART Setting serial baudrate 115200\n");
} }
#endif #endif
return sp; return sp;
} }
void uart_close(const serial_port sp) { void uart_close(const serial_port sp) {
serial_port_unix* spu = (serial_port_unix*)sp; serial_port_unix* spu = (serial_port_unix*)sp;
tcflush(spu->fd, TCIOFLUSH); tcflush(spu->fd, TCIOFLUSH);
tcsetattr(spu->fd, TCSANOW, &(spu->tiOld)); tcsetattr(spu->fd, TCSANOW, &(spu->tiOld));
struct flock fl; struct flock fl;
fl.l_type = F_UNLCK; fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET; fl.l_whence = SEEK_SET;
fl.l_start = 0; fl.l_start = 0;
fl.l_len = 0; fl.l_len = 0;
fl.l_pid = getpid(); fl.l_pid = getpid();
// Does the system allows us to place a lock on this file descriptor // Does the system allows us to place a lock on this file descriptor
int err = fcntl(spu->fd, F_SETLK, &fl); int err = fcntl(spu->fd, F_SETLK, &fl);
if ( err == -1) { if ( err == -1) {
//perror("fcntl"); //perror("fcntl");
} }
close(spu->fd); close(spu->fd);
free(sp); free(sp);
} }
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) { bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
int res; int res;
int byteCount; int byteCount;
fd_set rfds; fd_set rfds;
struct timeval tv; struct timeval tv;
// Reset the output count // Reset the output count
*pszRxLen = 0; *pszRxLen = 0;
do { do {
// Reset file descriptor // Reset file descriptor
FD_ZERO(&rfds); FD_ZERO(&rfds);
FD_SET(((serial_port_unix*)sp)->fd, &rfds); FD_SET(((serial_port_unix*)sp)->fd, &rfds);
tv = timeout; tv = timeout;
res = select(((serial_port_unix*)sp)->fd + 1, &rfds, NULL, NULL, &tv); res = select(((serial_port_unix*)sp)->fd + 1, &rfds, NULL, NULL, &tv);
// Read error // Read error
if (res < 0) { if (res < 0) {
return false; return false;
} }
// Read time-out // Read time-out
if (res == 0) { if (res == 0) {
if (*pszRxLen == 0) { if (*pszRxLen == 0) {
// Error, we received no data // Error, we received no data
return false; return false;
} else { } else {
// We received some data, but nothing more is available // We received some data, but nothing more is available
return true; return true;
} }
} }
// Retrieve the count of the incoming bytes // Retrieve the count of the incoming bytes
res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount); res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
if (res < 0) return false; if (res < 0) return false;
// Cap the number of bytes, so we don't overrun the buffer // Cap the number of bytes, so we don't overrun the buffer
if (pszMaxRxLen - (*pszRxLen) < byteCount) { if (pszMaxRxLen - (*pszRxLen) < byteCount) {
byteCount = pszMaxRxLen - (*pszRxLen); byteCount = pszMaxRxLen - (*pszRxLen);
} }
// There is something available, read the data // There is something available, read the data
res = read(((serial_port_unix*)sp)->fd, pbtRx + (*pszRxLen), byteCount); res = read(((serial_port_unix*)sp)->fd, pbtRx + (*pszRxLen), byteCount);
// Stop if the OS has some troubles reading the data // Stop if the OS has some troubles reading the data
if (res <= 0) return false; if (res <= 0) return false;
*pszRxLen += res; *pszRxLen += res;
if (*pszRxLen == pszMaxRxLen) { if (*pszRxLen == pszMaxRxLen) {
// We have all the data we wanted. // We have all the data we wanted.
return true; return true;
} }
} while (byteCount); } while (byteCount);
return true; return true;
} }
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t len) { bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t len) {
int32_t res; int32_t res;
size_t pos = 0; size_t pos = 0;
fd_set rfds; fd_set rfds;
struct timeval tv; struct timeval tv;
while (pos < len) { while (pos < len) {
// Reset file descriptor // Reset file descriptor
FD_ZERO(&rfds); FD_ZERO(&rfds);
FD_SET(((serial_port_unix*)sp)->fd, &rfds); FD_SET(((serial_port_unix*)sp)->fd, &rfds);
tv = timeout; tv = timeout;
res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv); res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);
// Write error // Write error
if (res < 0) { if (res < 0) {
printf("UART:: write error (%d)\n", res); printf("UART:: write error (%d)\n", res);
return false; return false;
} }
// Write time-out // Write time-out
if (res == 0) { if (res == 0) {
printf("UART:: write time-out\n"); printf("UART:: write time-out\n");
return false; return false;
} }
// Send away the bytes // Send away the bytes
res = write(((serial_port_unix*)sp)->fd, pbtTx + pos, len - pos); res = write(((serial_port_unix*)sp)->fd, pbtTx + pos, len - pos);
// Stop if the OS has some troubles sending the data // Stop if the OS has some troubles sending the data
if (res <= 0) return false; if (res <= 0) return false;
pos += res; pos += res;
} }
return true; return true;
} }
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
const serial_port_unix* spu = (serial_port_unix*)sp; const serial_port_unix* spu = (serial_port_unix*)sp;
speed_t stPortSpeed; speed_t stPortSpeed;
switch (uiPortSpeed) { switch (uiPortSpeed) {
case 0: stPortSpeed = B0; break; case 0: stPortSpeed = B0; break;
case 50: stPortSpeed = B50; break; case 50: stPortSpeed = B50; break;
case 75: stPortSpeed = B75; break; case 75: stPortSpeed = B75; break;
case 110: stPortSpeed = B110; break; case 110: stPortSpeed = B110; break;
case 134: stPortSpeed = B134; break; case 134: stPortSpeed = B134; break;
case 150: stPortSpeed = B150; break; case 150: stPortSpeed = B150; break;
case 300: stPortSpeed = B300; break; case 300: stPortSpeed = B300; break;
case 600: stPortSpeed = B600; break; case 600: stPortSpeed = B600; break;
case 1200: stPortSpeed = B1200; break; case 1200: stPortSpeed = B1200; break;
case 1800: stPortSpeed = B1800; break; case 1800: stPortSpeed = B1800; break;
case 2400: stPortSpeed = B2400; break; case 2400: stPortSpeed = B2400; break;
case 4800: stPortSpeed = B4800; break; case 4800: stPortSpeed = B4800; break;
case 9600: stPortSpeed = B9600; break; case 9600: stPortSpeed = B9600; break;
case 19200: stPortSpeed = B19200; break; case 19200: stPortSpeed = B19200; break;
case 38400: stPortSpeed = B38400; break; case 38400: stPortSpeed = B38400; break;
# ifdef B57600 # ifdef B57600
case 57600: stPortSpeed = B57600; break; case 57600: stPortSpeed = B57600; break;
# endif # endif
# ifdef B115200 # ifdef B115200
case 115200: stPortSpeed = B115200; break; case 115200: stPortSpeed = B115200; break;
# endif # endif
# ifdef B230400 # ifdef B230400
case 230400: stPortSpeed = B230400; break; case 230400: stPortSpeed = B230400; break;
# endif # endif
# ifdef B460800 # ifdef B460800
case 460800: stPortSpeed = B460800; break; case 460800: stPortSpeed = B460800; break;
# endif # endif
# ifdef B921600 # ifdef B921600
case 921600: stPortSpeed = B921600; break; case 921600: stPortSpeed = B921600; break;
# endif # endif
default: return false; default: return false;
}; };
struct termios ti; struct termios ti;
if (tcgetattr(spu->fd,&ti) == -1) if (tcgetattr(spu->fd,&ti) == -1)
return false; return false;
// Set port speed (Input and Output) // Set port speed (Input and Output)
cfsetispeed(&ti, stPortSpeed); cfsetispeed(&ti, stPortSpeed);
cfsetospeed(&ti, stPortSpeed); cfsetospeed(&ti, stPortSpeed);
return (tcsetattr(spu->fd, TCSANOW, &ti) != -1); return (tcsetattr(spu->fd, TCSANOW, &ti) != -1);
} }
uint32_t uart_get_speed(const serial_port sp) { uint32_t uart_get_speed(const serial_port sp) {
struct termios ti; struct termios ti;
uint32_t uiPortSpeed; uint32_t uiPortSpeed;
const serial_port_unix* spu = (serial_port_unix*)sp; const serial_port_unix* spu = (serial_port_unix*)sp;
if (tcgetattr(spu->fd, &ti) == -1) if (tcgetattr(spu->fd, &ti) == -1)
return 0; return 0;
// Set port speed (Input) // Set port speed (Input)
speed_t stPortSpeed = cfgetispeed(&ti); speed_t stPortSpeed = cfgetispeed(&ti);
switch (stPortSpeed) { switch (stPortSpeed) {
case B0: uiPortSpeed = 0; break; case B0: uiPortSpeed = 0; break;
case B50: uiPortSpeed = 50; break; case B50: uiPortSpeed = 50; break;
case B75: uiPortSpeed = 75; break; case B75: uiPortSpeed = 75; break;
case B110: uiPortSpeed = 110; break; case B110: uiPortSpeed = 110; break;
case B134: uiPortSpeed = 134; break; case B134: uiPortSpeed = 134; break;
case B150: uiPortSpeed = 150; break; case B150: uiPortSpeed = 150; break;
case B300: uiPortSpeed = 300; break; case B300: uiPortSpeed = 300; break;
case B600: uiPortSpeed = 600; break; case B600: uiPortSpeed = 600; break;
case B1200: uiPortSpeed = 1200; break; case B1200: uiPortSpeed = 1200; break;
case B1800: uiPortSpeed = 1800; break; case B1800: uiPortSpeed = 1800; break;
case B2400: uiPortSpeed = 2400; break; case B2400: uiPortSpeed = 2400; break;
case B4800: uiPortSpeed = 4800; break; case B4800: uiPortSpeed = 4800; break;
case B9600: uiPortSpeed = 9600; break; case B9600: uiPortSpeed = 9600; break;
case B19200: uiPortSpeed = 19200; break; case B19200: uiPortSpeed = 19200; break;
case B38400: uiPortSpeed = 38400; break; case B38400: uiPortSpeed = 38400; break;
# ifdef B57600 # ifdef B57600
case B57600: uiPortSpeed = 57600; break; case B57600: uiPortSpeed = 57600; break;
# endif # endif
# ifdef B115200 # ifdef B115200
case B115200: uiPortSpeed = 115200; break; case B115200: uiPortSpeed = 115200; break;
# endif # endif
# ifdef B230400 # ifdef B230400
case B230400: uiPortSpeed = 230400; break; case B230400: uiPortSpeed = 230400; break;
# endif # endif
# ifdef B460800 # ifdef B460800
case B460800: uiPortSpeed = 460800; break; case B460800: uiPortSpeed = 460800; break;
# endif # endif
# ifdef B921600 # ifdef B921600
case B921600: uiPortSpeed = 921600; break; case B921600: uiPortSpeed = 921600; break;
# endif # endif
default: return 0; default: return 0;
}; };
return uiPortSpeed; return uiPortSpeed;
} }
#endif #endif

View file

@ -49,129 +49,129 @@ typedef struct {
} serial_port_windows; } serial_port_windows;
serial_port uart_open(const char* pcPortName) { serial_port uart_open(const char* pcPortName) {
char acPortName[255]; char acPortName[255];
serial_port_windows* sp = calloc(sizeof(serial_port_windows), sizeof(uint8_t)); serial_port_windows* sp = calloc(sizeof(serial_port_windows), sizeof(uint8_t));
if (sp == 0) { if (sp == 0) {
printf("[!] UART failed to allocate memory\n"); printf("[!] UART failed to allocate memory\n");
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
// Copy the input "com?" to "\\.\COM?" format // Copy the input "com?" to "\\.\COM?" format
sprintf(acPortName,"\\\\.\\%s", pcPortName); sprintf(acPortName,"\\\\.\\%s", pcPortName);
_strupr(acPortName); _strupr(acPortName);
// Try to open the serial port // Try to open the serial port
// r/w, none-share comport, no security, existing, no overlapping, no templates // r/w, none-share comport, no security, existing, no overlapping, no templates
sp->hPort = CreateFileA(acPortName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); sp->hPort = CreateFileA(acPortName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (sp->hPort == INVALID_HANDLE_VALUE) { if (sp->hPort == INVALID_HANDLE_VALUE) {
uart_close(sp); uart_close(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
// Prepare the device control // Prepare the device control
// doesn't matter since PM3 device ignors this CDC command: set_line_coding in usb_cdc.c // doesn't matter since PM3 device ignors this CDC command: set_line_coding in usb_cdc.c
memset(&sp->dcb, 0, sizeof(DCB)); memset(&sp->dcb, 0, sizeof(DCB));
sp->dcb.DCBlength = sizeof(DCB); sp->dcb.DCBlength = sizeof(DCB);
if (!BuildCommDCBA("baud=115200 parity=N data=8 stop=1", &sp->dcb)) { if (!BuildCommDCBA("baud=115200 parity=N data=8 stop=1", &sp->dcb)) {
uart_close(sp); uart_close(sp);
printf("[!] UART error cdc setup\n"); printf("[!] UART error cdc setup\n");
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
// Update the active serial port // Update the active serial port
if (!SetCommState(sp->hPort, &sp->dcb)) { if (!SetCommState(sp->hPort, &sp->dcb)) {
uart_close(sp); uart_close(sp);
printf("[!] UART error while setting com state\n"); printf("[!] UART error while setting com state\n");
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
// all zero's configure: no timeout for read/write used. // all zero's configure: no timeout for read/write used.
// took settings from libnfc/buses/uart.c // took settings from libnfc/buses/uart.c
#ifdef WITH_FPC #ifdef WITH_FPC
sp->ct.ReadIntervalTimeout = 1000; sp->ct.ReadIntervalTimeout = 1000;
sp->ct.ReadTotalTimeoutMultiplier = 0; sp->ct.ReadTotalTimeoutMultiplier = 0;
sp->ct.ReadTotalTimeoutConstant = 1500; sp->ct.ReadTotalTimeoutConstant = 1500;
sp->ct.WriteTotalTimeoutMultiplier = 1000; sp->ct.WriteTotalTimeoutMultiplier = 1000;
sp->ct.WriteTotalTimeoutConstant = 0; sp->ct.WriteTotalTimeoutConstant = 0;
#else #else
sp->ct.ReadIntervalTimeout = 30; sp->ct.ReadIntervalTimeout = 30;
sp->ct.ReadTotalTimeoutMultiplier = 0; sp->ct.ReadTotalTimeoutMultiplier = 0;
sp->ct.ReadTotalTimeoutConstant = 30; sp->ct.ReadTotalTimeoutConstant = 30;
sp->ct.WriteTotalTimeoutMultiplier = 30; sp->ct.WriteTotalTimeoutMultiplier = 30;
sp->ct.WriteTotalTimeoutConstant = 0; sp->ct.WriteTotalTimeoutConstant = 0;
#endif #endif
if (!SetCommTimeouts(sp->hPort, &sp->ct)) { if (!SetCommTimeouts(sp->hPort, &sp->ct)) {
uart_close(sp); uart_close(sp);
printf("[!] UART error while setting comm time outs\n"); printf("[!] UART error while setting comm time outs\n");
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR); PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
#ifdef WITH_FPC #ifdef WITH_FPC
if ( uart_set_speed(sp, 115200) ) { if ( uart_set_speed(sp, 115200) ) {
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n"); printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
} else { } else {
uart_set_speed(sp, 9600); uart_set_speed(sp, 9600);
printf("[=] UART Setting serial baudrate 9600 [FPC enabled]\n"); printf("[=] UART Setting serial baudrate 9600 [FPC enabled]\n");
} }
#else #else
bool success = uart_set_speed(sp, 460800); bool success = uart_set_speed(sp, 460800);
if (success) { if (success) {
printf("[=] UART Setting serial baudrate 460800\n"); printf("[=] UART Setting serial baudrate 460800\n");
} else { } else {
uart_set_speed(sp, 115200); uart_set_speed(sp, 115200);
printf("[=] UART Setting serial baudrate 115200\n"); printf("[=] UART Setting serial baudrate 115200\n");
} }
#endif #endif
return sp; return sp;
} }
void uart_close(const serial_port sp) { void uart_close(const serial_port sp) {
if (((serial_port_windows*)sp)->hPort != INVALID_HANDLE_VALUE ) if (((serial_port_windows*)sp)->hPort != INVALID_HANDLE_VALUE )
CloseHandle(((serial_port_windows*)sp)->hPort); CloseHandle(((serial_port_windows*)sp)->hPort);
free(sp); free(sp);
} }
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
serial_port_windows* spw; serial_port_windows* spw;
// Set port speed (Input and Output) // Set port speed (Input and Output)
switch (uiPortSpeed) { switch (uiPortSpeed) {
case 9600: case 9600:
case 19200: case 19200:
case 38400: case 38400:
case 57600: case 57600:
case 115200: case 115200:
case 230400: case 230400:
case 460800: case 460800:
break; break;
default: default:
return false; return false;
}; };
spw = (serial_port_windows*)sp; spw = (serial_port_windows*)sp;
spw->dcb.BaudRate = uiPortSpeed; spw->dcb.BaudRate = uiPortSpeed;
bool result = SetCommState(spw->hPort, &spw->dcb); bool result = SetCommState(spw->hPort, &spw->dcb);
PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR); PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
return result; return result;
} }
uint32_t uart_get_speed(const serial_port sp) { uint32_t uart_get_speed(const serial_port sp) {
const serial_port_windows* spw = (serial_port_windows*)sp; const serial_port_windows* spw = (serial_port_windows*)sp;
if (!GetCommState(spw->hPort, (serial_port) & spw->dcb)) if (!GetCommState(spw->hPort, (serial_port) & spw->dcb))
return spw->dcb.BaudRate; return spw->dcb.BaudRate;
return 0; return 0;
} }
bool uart_receive(const serial_port sp, uint8_t* p_rx, size_t pszMaxRxLen, size_t* len) { bool uart_receive(const serial_port sp, uint8_t* p_rx, size_t pszMaxRxLen, size_t* len) {
return ReadFile(((serial_port_windows*)sp)->hPort, p_rx, pszMaxRxLen, (LPDWORD)len, NULL); return ReadFile(((serial_port_windows*)sp)->hPort, p_rx, pszMaxRxLen, (LPDWORD)len, NULL);
} }
bool uart_send(const serial_port sp, const uint8_t* p_tx, const size_t len) { bool uart_send(const serial_port sp, const uint8_t* p_tx, const size_t len) {
DWORD txlen = 0; DWORD txlen = 0;
return WriteFile(((serial_port_windows*)sp)->hPort, p_tx, len, &txlen, NULL); return WriteFile(((serial_port_windows*)sp)->hPort, p_tx, len, &txlen, NULL);
} }
#endif #endif

View file

@ -1160,10 +1160,10 @@ local uInt longest_match(s, cur_match)
{ {
unsigned chain_length = s->max_chain_length;/* max hash chain length */ unsigned chain_length = s->max_chain_length;/* max hash chain length */
register Bytef *scan = s->window + s->strstart; /* current string */ register Bytef *scan = s->window + s->strstart; /* current string */
register Bytef *match; /* matched string */ register Bytef *match; /* matched string */
register int len; /* length of current match */ register int len; /* length of current match */
#ifdef ZLIB_PM3_TUNED #ifdef ZLIB_PM3_TUNED
int best_len = MIN_MATCH-1; // lift the restriction on prev-length int best_len = MIN_MATCH-1; /* lift the restriction on prev-length */
#else #else
int best_len = s->prev_length; /* best match length so far */ int best_len = s->prev_length; /* best match length so far */
#endif #endif
@ -1737,73 +1737,73 @@ local block_state deflate_fast(s, flush)
#ifdef ZLIB_PM3_TUNED #ifdef ZLIB_PM3_TUNED
local uInt try_harder(s, strstart, lookahead, hash_head) local uInt try_harder(s, strstart, lookahead, hash_head)
deflate_state *s; deflate_state *s;
uInt strstart; uInt strstart;
uInt lookahead; uInt lookahead;
IPos hash_head; IPos hash_head;
{ {
uInt strstart_save = s->strstart; uInt strstart_save = s->strstart;
s->strstart = strstart; s->strstart = strstart;
uInt lookahead_save = s->lookahead; uInt lookahead_save = s->lookahead;
s->lookahead = lookahead; s->lookahead = lookahead;
uInt ins_h_save = s->ins_h; uInt ins_h_save = s->ins_h;
uInt combined_gain; uInt combined_gain;
uInt best_combined_gain = 0; uInt best_combined_gain = 0;
uInt match_length; uInt match_length;
uInt prev_length = s->prev_length < MIN_MATCH ? 1 : s->prev_length; uInt prev_length = s->prev_length < MIN_MATCH ? 1 : s->prev_length;
uInt best_prev_length = prev_length; uInt best_prev_length = prev_length;
uInt current_match_start = s->match_start; uInt current_match_start = s->match_start;
uInt current_match_length = s->match_length; uInt current_match_length = s->match_length;
do { do {
if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
match_length = longest_match (s, hash_head); match_length = longest_match (s, hash_head);
/* longest_match() sets match_start */ /* longest_match() sets match_start */
} else { } else {
match_length = MIN_MATCH - 1; match_length = MIN_MATCH - 1;
} }
#if TOO_FAR <= 32767 #if TOO_FAR <= 32767
if (match_length == MIN_MATCH && s->strstart - s->match_start > TOO_FAR) { if (match_length == MIN_MATCH && s->strstart - s->match_start > TOO_FAR) {
match_length = MIN_MATCH-1; match_length = MIN_MATCH-1;
} }
#endif #endif
if (s->strstart == strstart) { // store match at current position if (s->strstart == strstart) { // store match at current position
current_match_length = match_length; current_match_length = match_length;
current_match_start = s->match_start; current_match_start = s->match_start;
} }
if (s->strstart - strstart + 1 < MIN_MATCH) { // previous match reduced to one or two literals if (s->strstart - strstart + 1 < MIN_MATCH) { // previous match reduced to one or two literals
combined_gain = 0; // need one literal per byte: no gain (assuming 8 bits per literal) combined_gain = 0; // need one literal per byte: no gain (assuming 8 bits per literal)
} else { } else {
combined_gain = s->strstart - strstart + 1 - MIN_MATCH; // (possibly truncated) previous_length - 3 literals combined_gain = s->strstart - strstart + 1 - MIN_MATCH; // (possibly truncated) previous_length - 3 literals
} }
if (match_length < MIN_MATCH) { if (match_length < MIN_MATCH) {
combined_gain += 0; // no gain combined_gain += 0; // no gain
} else { } else {
combined_gain += match_length - MIN_MATCH; // match_length bytes are coded as three literals combined_gain += match_length - MIN_MATCH; // match_length bytes are coded as three literals
} }
if (combined_gain >= best_combined_gain) { // in case of a tie we prefer the longer prev_length if (combined_gain >= best_combined_gain) { // in case of a tie we prefer the longer prev_length
best_combined_gain = combined_gain; best_combined_gain = combined_gain;
best_prev_length = s->strstart - strstart + 1; best_prev_length = s->strstart - strstart + 1;
} }
s->strstart++; s->strstart++;
s->lookahead--; s->lookahead--;
UPDATE_HASH(s, s->ins_h, s->window[(s->strstart) + (MIN_MATCH-1)]); UPDATE_HASH(s, s->ins_h, s->window[(s->strstart) + (MIN_MATCH-1)]);
hash_head = s->head[s->ins_h]; hash_head = s->head[s->ins_h];
} while (s->strstart <= strstart-1 + prev_length // try to truncate the previous match to 1, 3, ... prev_length } while (s->strstart <= strstart-1 + prev_length // try to truncate the previous match to 1, 3, ... prev_length
&& s->strstart <= s->window_size - MIN_LOOKAHEAD); // watch out for the end of the input && s->strstart <= s->window_size - MIN_LOOKAHEAD); // watch out for the end of the input
s->strstart = strstart_save; s->strstart = strstart_save;
s->lookahead = lookahead_save; s->lookahead = lookahead_save;
s->ins_h = ins_h_save; s->ins_h = ins_h_save;
s->match_length = current_match_length; s->match_length = current_match_length;
s->match_start = current_match_start; s->match_start = current_match_start;
if (best_prev_length >= MIN_MATCH) { if (best_prev_length >= MIN_MATCH) {
s->prev_length = best_prev_length; s->prev_length = best_prev_length;
s->match_length = MIN_MATCH - 1; s->match_length = MIN_MATCH - 1;
} else { } else {
s->prev_length = MIN_MATCH - 1; s->prev_length = MIN_MATCH - 1;
} }
return best_combined_gain; return best_combined_gain;
} }
#endif #endif
@ -1850,9 +1850,9 @@ local block_state deflate_slow(s, flush)
s->match_length = MIN_MATCH-1; s->match_length = MIN_MATCH-1;
#ifdef ZLIB_PM3_TUNED #ifdef ZLIB_PM3_TUNED
if (s->prev_length < s->max_lazy_match) { if (s->prev_length < s->max_lazy_match) {
try_harder(s, s->strstart, s->lookahead, hash_head); try_harder(s, s->strstart, s->lookahead, hash_head);
} }
#else #else
if (hash_head != NIL && s->prev_length < s->max_lazy_match && if (hash_head != NIL && s->prev_length < s->max_lazy_match &&

View file

@ -857,7 +857,7 @@ int flush;
case 1: /* fixed block */ case 1: /* fixed block */
#ifdef ZLIB_PM3_TUNED #ifdef ZLIB_PM3_TUNED
strm->msg = (char *)"fixed block coding not supported"; strm->msg = (char *)"fixed block coding not supported";
state->mode = BAD; state->mode = BAD;
#else #else
fixedtables(state); fixedtables(state);
Tracev((stderr, "inflate: fixed codes block%s\n", Tracev((stderr, "inflate: fixed codes block%s\n",

View file

@ -989,7 +989,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
#endif #endif
} else { } else {
#endif /* !ZLIB_PM3_TUNED */ #endif /* !ZLIB_PM3_TUNED */
send_bits(s, (DYN_TREES<<1)+last, 3); send_bits(s, (DYN_TREES<<1)+last, 3);
send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
max_blindex+1); max_blindex+1);
compress_block(s, (const ct_data *)s->dyn_ltree, compress_block(s, (const ct_data *)s->dyn_ltree,