mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-21 13:53:55 -07:00
zlib, uart: fix mix of spaces & tabs
This commit is contained in:
parent
46af10d54d
commit
1fe75f2481
6 changed files with 426 additions and 426 deletions
|
@ -44,11 +44,11 @@
|
|||
|
||||
|
||||
#if defined (_WIN32)
|
||||
#define SERIAL_PORT_H "com3"
|
||||
#define SERIAL_PORT_H "com3"
|
||||
#elif defined(__APPLE__)
|
||||
#define SERIAL_PORT_H "/dev/cu.usbmodem"
|
||||
#else
|
||||
#define SERIAL_PORT_H "/dev/ttyACM0"
|
||||
#define SERIAL_PORT_H "/dev/ttyACM0"
|
||||
#endif
|
||||
|
||||
/* serial_port is declared as a void*, which you should cast to whatever type
|
||||
|
|
|
@ -62,363 +62,363 @@
|
|||
|
||||
typedef struct termios term_info;
|
||||
typedef struct {
|
||||
int fd; // Serial port file descriptor
|
||||
term_info tiOld; // Terminal info before using the port
|
||||
term_info tiNew; // Terminal info during the transaction
|
||||
int fd; // Serial port file descriptor
|
||||
term_info tiOld; // Terminal info before using the port
|
||||
term_info tiNew; // Terminal info during the transaction
|
||||
} serial_port_unix;
|
||||
|
||||
// Set time-out on 30 miliseconds
|
||||
struct timeval timeout = {
|
||||
.tv_sec = 0, // 0 second
|
||||
.tv_usec = 30000 // 30 000 micro seconds
|
||||
.tv_sec = 0, // 0 second
|
||||
.tv_usec = 30000 // 30 000 micro seconds
|
||||
};
|
||||
|
||||
serial_port uart_open(const char* pcPortName) {
|
||||
serial_port_unix* sp = calloc(sizeof(serial_port_unix), sizeof(uint8_t));
|
||||
if (sp == 0) return INVALID_SERIAL_PORT;
|
||||
serial_port_unix* sp = calloc(sizeof(serial_port_unix), sizeof(uint8_t));
|
||||
if (sp == 0) return INVALID_SERIAL_PORT;
|
||||
|
||||
if (memcmp(pcPortName, "tcp:", 4) == 0) {
|
||||
struct addrinfo *addr, *rp;
|
||||
char *addrstr = strdup(pcPortName + 4);
|
||||
if (memcmp(pcPortName, "tcp:", 4) == 0) {
|
||||
struct addrinfo *addr, *rp;
|
||||
char *addrstr = strdup(pcPortName + 4);
|
||||
|
||||
if (addrstr == NULL) {
|
||||
printf("Error: strdup\n");
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
if (addrstr == NULL) {
|
||||
printf("Error: strdup\n");
|
||||
free(sp);
|
||||
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 *portstr;
|
||||
if (colon) {
|
||||
portstr = colon + 1;
|
||||
*colon = '\0';
|
||||
} else {
|
||||
portstr = "7901";
|
||||
}
|
||||
char *colon = strrchr(addrstr, ':');
|
||||
char *portstr;
|
||||
if (colon) {
|
||||
portstr = colon + 1;
|
||||
*colon = '\0';
|
||||
} else {
|
||||
portstr = "7901";
|
||||
}
|
||||
|
||||
int s = getaddrinfo(addrstr, portstr, NULL, &addr);
|
||||
if (s != 0) {
|
||||
printf("Error: getaddrinfo: %s\n", gai_strerror(s));
|
||||
freeaddrinfo(addr);
|
||||
free(addrstr);
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
int s = getaddrinfo(addrstr, portstr, NULL, &addr);
|
||||
if (s != 0) {
|
||||
printf("Error: getaddrinfo: %s\n", gai_strerror(s));
|
||||
freeaddrinfo(addr);
|
||||
free(addrstr);
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
int sfd;
|
||||
for (rp = addr; rp != NULL; rp = rp->ai_next) {
|
||||
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
int sfd;
|
||||
for (rp = addr; rp != NULL; rp = rp->ai_next) {
|
||||
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
|
||||
if (sfd == -1)
|
||||
continue;
|
||||
if (sfd == -1)
|
||||
continue;
|
||||
|
||||
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
||||
break;
|
||||
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
||||
break;
|
||||
|
||||
close(sfd);
|
||||
}
|
||||
close(sfd);
|
||||
}
|
||||
|
||||
if (rp == NULL) { /* No address succeeded */
|
||||
printf("Error: Could not connect\n");
|
||||
freeaddrinfo(addr);
|
||||
free(addrstr);
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
if (rp == NULL) { /* No address succeeded */
|
||||
printf("Error: Could not connect\n");
|
||||
freeaddrinfo(addr);
|
||||
free(addrstr);
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
freeaddrinfo(addr);
|
||||
free(addrstr);
|
||||
freeaddrinfo(addr);
|
||||
free(addrstr);
|
||||
|
||||
sp->fd = sfd;
|
||||
sp->fd = sfd;
|
||||
|
||||
int one = 1;
|
||||
int res = setsockopt(sp->fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
|
||||
if ( res != 0) {
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
return sp;
|
||||
}
|
||||
int one = 1;
|
||||
int res = setsockopt(sp->fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
|
||||
if ( res != 0) {
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
return sp;
|
||||
}
|
||||
|
||||
|
||||
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
|
||||
if (sp->fd == -1) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
|
||||
if (sp->fd == -1) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// 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
|
||||
struct flock fl;
|
||||
fl.l_type = F_WRLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
fl.l_start = 0;
|
||||
fl.l_len = 0;
|
||||
fl.l_pid = getpid();
|
||||
// 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
|
||||
struct flock fl;
|
||||
fl.l_type = F_WRLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
fl.l_start = 0;
|
||||
fl.l_len = 0;
|
||||
fl.l_pid = getpid();
|
||||
|
||||
// Does the system allows us to place a lock on this file descriptor
|
||||
if (fcntl(sp->fd, F_SETLK, &fl) == -1) {
|
||||
// A conflicting lock is held by another process
|
||||
free(sp);
|
||||
return CLAIMED_SERIAL_PORT;
|
||||
}
|
||||
// Does the system allows us to place a lock on this file descriptor
|
||||
if (fcntl(sp->fd, F_SETLK, &fl) == -1) {
|
||||
// A conflicting lock is held by another process
|
||||
free(sp);
|
||||
return CLAIMED_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Try to retrieve the old (current) terminal info struct
|
||||
if (tcgetattr(sp->fd,&sp->tiOld) == -1) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Try to retrieve the old (current) terminal info struct
|
||||
if (tcgetattr(sp->fd,&sp->tiOld) == -1) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Duplicate the (old) terminal info struct
|
||||
sp->tiNew = sp->tiOld;
|
||||
// Duplicate the (old) terminal info struct
|
||||
sp->tiNew = sp->tiOld;
|
||||
|
||||
// Configure the serial port
|
||||
sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD;
|
||||
sp->tiNew.c_iflag = IGNPAR;
|
||||
sp->tiNew.c_oflag = 0;
|
||||
sp->tiNew.c_lflag = 0;
|
||||
// Configure the serial port
|
||||
sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD;
|
||||
sp->tiNew.c_iflag = IGNPAR;
|
||||
sp->tiNew.c_oflag = 0;
|
||||
sp->tiNew.c_lflag = 0;
|
||||
|
||||
// Block until n bytes are received
|
||||
sp->tiNew.c_cc[VMIN] = 0;
|
||||
// Block until a timer expires (n * 100 mSec.)
|
||||
sp->tiNew.c_cc[VTIME] = 0;
|
||||
// Block until n bytes are received
|
||||
sp->tiNew.c_cc[VMIN] = 0;
|
||||
// Block until a timer expires (n * 100 mSec.)
|
||||
sp->tiNew.c_cc[VTIME] = 0;
|
||||
|
||||
// Try to set the new terminal info struct
|
||||
if (tcsetattr(sp->fd, TCSANOW, &sp->tiNew) == -1) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Try to set the new terminal info struct
|
||||
if (tcsetattr(sp->fd, TCSANOW, &sp->tiNew) == -1) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Flush all lingering data that may exist
|
||||
tcflush(sp->fd, TCIOFLUSH);
|
||||
|
||||
#ifdef WITH_FPC
|
||||
if ( uart_set_speed(sp, 115200) ) {
|
||||
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
|
||||
} else {
|
||||
uart_set_speed(sp, 9600);
|
||||
printf("[=] UART Setting serial baudrate 9600 [FPC enabled]\n");
|
||||
}
|
||||
if ( uart_set_speed(sp, 115200) ) {
|
||||
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
|
||||
} else {
|
||||
uart_set_speed(sp, 9600);
|
||||
printf("[=] UART Setting serial baudrate 9600 [FPC enabled]\n");
|
||||
}
|
||||
#else
|
||||
// set speed, works for UBUNTU 14.04
|
||||
bool success = uart_set_speed(sp, 460800);
|
||||
if (success) {
|
||||
printf("[=] UART Setting serial baudrate 460800\n");
|
||||
} else {
|
||||
uart_set_speed(sp, 115200);
|
||||
printf("[=] UART Setting serial baudrate 115200\n");
|
||||
}
|
||||
// set speed, works for UBUNTU 14.04
|
||||
bool success = uart_set_speed(sp, 460800);
|
||||
if (success) {
|
||||
printf("[=] UART Setting serial baudrate 460800\n");
|
||||
} else {
|
||||
uart_set_speed(sp, 115200);
|
||||
printf("[=] UART Setting serial baudrate 115200\n");
|
||||
}
|
||||
#endif
|
||||
return sp;
|
||||
return sp;
|
||||
}
|
||||
|
||||
void uart_close(const serial_port sp) {
|
||||
serial_port_unix* spu = (serial_port_unix*)sp;
|
||||
tcflush(spu->fd, TCIOFLUSH);
|
||||
tcsetattr(spu->fd, TCSANOW, &(spu->tiOld));
|
||||
struct flock fl;
|
||||
fl.l_type = F_UNLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
fl.l_start = 0;
|
||||
fl.l_len = 0;
|
||||
fl.l_pid = getpid();
|
||||
serial_port_unix* spu = (serial_port_unix*)sp;
|
||||
tcflush(spu->fd, TCIOFLUSH);
|
||||
tcsetattr(spu->fd, TCSANOW, &(spu->tiOld));
|
||||
struct flock fl;
|
||||
fl.l_type = F_UNLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
fl.l_start = 0;
|
||||
fl.l_len = 0;
|
||||
fl.l_pid = getpid();
|
||||
|
||||
// Does the system allows us to place a lock on this file descriptor
|
||||
int err = fcntl(spu->fd, F_SETLK, &fl);
|
||||
if ( err == -1) {
|
||||
//perror("fcntl");
|
||||
}
|
||||
close(spu->fd);
|
||||
free(sp);
|
||||
// Does the system allows us to place a lock on this file descriptor
|
||||
int err = fcntl(spu->fd, F_SETLK, &fl);
|
||||
if ( err == -1) {
|
||||
//perror("fcntl");
|
||||
}
|
||||
close(spu->fd);
|
||||
free(sp);
|
||||
}
|
||||
|
||||
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
|
||||
int res;
|
||||
int byteCount;
|
||||
fd_set rfds;
|
||||
struct timeval tv;
|
||||
int res;
|
||||
int byteCount;
|
||||
fd_set rfds;
|
||||
struct timeval tv;
|
||||
|
||||
// Reset the output count
|
||||
*pszRxLen = 0;
|
||||
// Reset the output count
|
||||
*pszRxLen = 0;
|
||||
|
||||
do {
|
||||
// Reset file descriptor
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(((serial_port_unix*)sp)->fd, &rfds);
|
||||
tv = timeout;
|
||||
res = select(((serial_port_unix*)sp)->fd + 1, &rfds, NULL, NULL, &tv);
|
||||
do {
|
||||
// Reset file descriptor
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(((serial_port_unix*)sp)->fd, &rfds);
|
||||
tv = timeout;
|
||||
res = select(((serial_port_unix*)sp)->fd + 1, &rfds, NULL, NULL, &tv);
|
||||
|
||||
// Read error
|
||||
if (res < 0) {
|
||||
return false;
|
||||
}
|
||||
// Read error
|
||||
if (res < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read time-out
|
||||
if (res == 0) {
|
||||
if (*pszRxLen == 0) {
|
||||
// Error, we received no data
|
||||
return false;
|
||||
} else {
|
||||
// We received some data, but nothing more is available
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Read time-out
|
||||
if (res == 0) {
|
||||
if (*pszRxLen == 0) {
|
||||
// Error, we received no data
|
||||
return false;
|
||||
} else {
|
||||
// We received some data, but nothing more is available
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve the count of the incoming bytes
|
||||
res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
|
||||
if (res < 0) return false;
|
||||
// Retrieve the count of the incoming bytes
|
||||
res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
|
||||
if (res < 0) return false;
|
||||
|
||||
// Cap the number of bytes, so we don't overrun the buffer
|
||||
if (pszMaxRxLen - (*pszRxLen) < byteCount) {
|
||||
byteCount = pszMaxRxLen - (*pszRxLen);
|
||||
}
|
||||
// Cap the number of bytes, so we don't overrun the buffer
|
||||
if (pszMaxRxLen - (*pszRxLen) < byteCount) {
|
||||
byteCount = pszMaxRxLen - (*pszRxLen);
|
||||
}
|
||||
|
||||
// There is something available, read the data
|
||||
res = read(((serial_port_unix*)sp)->fd, pbtRx + (*pszRxLen), byteCount);
|
||||
// There is something available, read the data
|
||||
res = read(((serial_port_unix*)sp)->fd, pbtRx + (*pszRxLen), byteCount);
|
||||
|
||||
// Stop if the OS has some troubles reading the data
|
||||
if (res <= 0) return false;
|
||||
// Stop if the OS has some troubles reading the data
|
||||
if (res <= 0) return false;
|
||||
|
||||
*pszRxLen += res;
|
||||
*pszRxLen += res;
|
||||
|
||||
if (*pszRxLen == pszMaxRxLen) {
|
||||
// We have all the data we wanted.
|
||||
return true;
|
||||
}
|
||||
if (*pszRxLen == pszMaxRxLen) {
|
||||
// We have all the data we wanted.
|
||||
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) {
|
||||
int32_t res;
|
||||
size_t pos = 0;
|
||||
fd_set rfds;
|
||||
struct timeval tv;
|
||||
int32_t res;
|
||||
size_t pos = 0;
|
||||
fd_set rfds;
|
||||
struct timeval tv;
|
||||
|
||||
while (pos < len) {
|
||||
// Reset file descriptor
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(((serial_port_unix*)sp)->fd, &rfds);
|
||||
tv = timeout;
|
||||
res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);
|
||||
while (pos < len) {
|
||||
// Reset file descriptor
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(((serial_port_unix*)sp)->fd, &rfds);
|
||||
tv = timeout;
|
||||
res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);
|
||||
|
||||
// Write error
|
||||
if (res < 0) {
|
||||
printf("UART:: write error (%d)\n", res);
|
||||
return false;
|
||||
}
|
||||
// Write error
|
||||
if (res < 0) {
|
||||
printf("UART:: write error (%d)\n", res);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write time-out
|
||||
if (res == 0) {
|
||||
printf("UART:: write time-out\n");
|
||||
return false;
|
||||
}
|
||||
// Write time-out
|
||||
if (res == 0) {
|
||||
printf("UART:: write time-out\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Send away the bytes
|
||||
res = write(((serial_port_unix*)sp)->fd, pbtTx + pos, len - pos);
|
||||
// Send away the bytes
|
||||
res = write(((serial_port_unix*)sp)->fd, pbtTx + pos, len - pos);
|
||||
|
||||
// Stop if the OS has some troubles sending the data
|
||||
if (res <= 0) return false;
|
||||
// Stop if the OS has some troubles sending the data
|
||||
if (res <= 0) return false;
|
||||
|
||||
pos += res;
|
||||
}
|
||||
return true;
|
||||
pos += res;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
|
||||
const serial_port_unix* spu = (serial_port_unix*)sp;
|
||||
speed_t stPortSpeed;
|
||||
switch (uiPortSpeed) {
|
||||
case 0: stPortSpeed = B0; break;
|
||||
case 50: stPortSpeed = B50; break;
|
||||
case 75: stPortSpeed = B75; break;
|
||||
case 110: stPortSpeed = B110; break;
|
||||
case 134: stPortSpeed = B134; break;
|
||||
case 150: stPortSpeed = B150; break;
|
||||
case 300: stPortSpeed = B300; break;
|
||||
case 600: stPortSpeed = B600; break;
|
||||
case 1200: stPortSpeed = B1200; break;
|
||||
case 1800: stPortSpeed = B1800; break;
|
||||
case 2400: stPortSpeed = B2400; break;
|
||||
case 4800: stPortSpeed = B4800; break;
|
||||
case 9600: stPortSpeed = B9600; break;
|
||||
case 19200: stPortSpeed = B19200; break;
|
||||
case 38400: stPortSpeed = B38400; break;
|
||||
const serial_port_unix* spu = (serial_port_unix*)sp;
|
||||
speed_t stPortSpeed;
|
||||
switch (uiPortSpeed) {
|
||||
case 0: stPortSpeed = B0; break;
|
||||
case 50: stPortSpeed = B50; break;
|
||||
case 75: stPortSpeed = B75; break;
|
||||
case 110: stPortSpeed = B110; break;
|
||||
case 134: stPortSpeed = B134; break;
|
||||
case 150: stPortSpeed = B150; break;
|
||||
case 300: stPortSpeed = B300; break;
|
||||
case 600: stPortSpeed = B600; break;
|
||||
case 1200: stPortSpeed = B1200; break;
|
||||
case 1800: stPortSpeed = B1800; break;
|
||||
case 2400: stPortSpeed = B2400; break;
|
||||
case 4800: stPortSpeed = B4800; break;
|
||||
case 9600: stPortSpeed = B9600; break;
|
||||
case 19200: stPortSpeed = B19200; break;
|
||||
case 38400: stPortSpeed = B38400; break;
|
||||
# ifdef B57600
|
||||
case 57600: stPortSpeed = B57600; break;
|
||||
case 57600: stPortSpeed = B57600; break;
|
||||
# endif
|
||||
# ifdef B115200
|
||||
case 115200: stPortSpeed = B115200; break;
|
||||
case 115200: stPortSpeed = B115200; break;
|
||||
# endif
|
||||
# ifdef B230400
|
||||
case 230400: stPortSpeed = B230400; break;
|
||||
case 230400: stPortSpeed = B230400; break;
|
||||
# endif
|
||||
# ifdef B460800
|
||||
case 460800: stPortSpeed = B460800; break;
|
||||
case 460800: stPortSpeed = B460800; break;
|
||||
# endif
|
||||
# ifdef B921600
|
||||
case 921600: stPortSpeed = B921600; break;
|
||||
case 921600: stPortSpeed = B921600; break;
|
||||
# endif
|
||||
default: return false;
|
||||
};
|
||||
default: return false;
|
||||
};
|
||||
|
||||
struct termios ti;
|
||||
if (tcgetattr(spu->fd,&ti) == -1)
|
||||
return false;
|
||||
struct termios ti;
|
||||
if (tcgetattr(spu->fd,&ti) == -1)
|
||||
return false;
|
||||
|
||||
// Set port speed (Input and Output)
|
||||
cfsetispeed(&ti, stPortSpeed);
|
||||
cfsetospeed(&ti, stPortSpeed);
|
||||
return (tcsetattr(spu->fd, TCSANOW, &ti) != -1);
|
||||
// Set port speed (Input and Output)
|
||||
cfsetispeed(&ti, stPortSpeed);
|
||||
cfsetospeed(&ti, stPortSpeed);
|
||||
return (tcsetattr(spu->fd, TCSANOW, &ti) != -1);
|
||||
}
|
||||
|
||||
uint32_t uart_get_speed(const serial_port sp) {
|
||||
struct termios ti;
|
||||
uint32_t uiPortSpeed;
|
||||
const serial_port_unix* spu = (serial_port_unix*)sp;
|
||||
struct termios ti;
|
||||
uint32_t uiPortSpeed;
|
||||
const serial_port_unix* spu = (serial_port_unix*)sp;
|
||||
|
||||
if (tcgetattr(spu->fd, &ti) == -1)
|
||||
return 0;
|
||||
if (tcgetattr(spu->fd, &ti) == -1)
|
||||
return 0;
|
||||
|
||||
// Set port speed (Input)
|
||||
speed_t stPortSpeed = cfgetispeed(&ti);
|
||||
switch (stPortSpeed) {
|
||||
case B0: uiPortSpeed = 0; break;
|
||||
case B50: uiPortSpeed = 50; break;
|
||||
case B75: uiPortSpeed = 75; break;
|
||||
case B110: uiPortSpeed = 110; break;
|
||||
case B134: uiPortSpeed = 134; break;
|
||||
case B150: uiPortSpeed = 150; break;
|
||||
case B300: uiPortSpeed = 300; break;
|
||||
case B600: uiPortSpeed = 600; break;
|
||||
case B1200: uiPortSpeed = 1200; break;
|
||||
case B1800: uiPortSpeed = 1800; break;
|
||||
case B2400: uiPortSpeed = 2400; break;
|
||||
case B4800: uiPortSpeed = 4800; break;
|
||||
case B9600: uiPortSpeed = 9600; break;
|
||||
case B19200: uiPortSpeed = 19200; break;
|
||||
case B38400: uiPortSpeed = 38400; break;
|
||||
// Set port speed (Input)
|
||||
speed_t stPortSpeed = cfgetispeed(&ti);
|
||||
switch (stPortSpeed) {
|
||||
case B0: uiPortSpeed = 0; break;
|
||||
case B50: uiPortSpeed = 50; break;
|
||||
case B75: uiPortSpeed = 75; break;
|
||||
case B110: uiPortSpeed = 110; break;
|
||||
case B134: uiPortSpeed = 134; break;
|
||||
case B150: uiPortSpeed = 150; break;
|
||||
case B300: uiPortSpeed = 300; break;
|
||||
case B600: uiPortSpeed = 600; break;
|
||||
case B1200: uiPortSpeed = 1200; break;
|
||||
case B1800: uiPortSpeed = 1800; break;
|
||||
case B2400: uiPortSpeed = 2400; break;
|
||||
case B4800: uiPortSpeed = 4800; break;
|
||||
case B9600: uiPortSpeed = 9600; break;
|
||||
case B19200: uiPortSpeed = 19200; break;
|
||||
case B38400: uiPortSpeed = 38400; break;
|
||||
# ifdef B57600
|
||||
case B57600: uiPortSpeed = 57600; break;
|
||||
case B57600: uiPortSpeed = 57600; break;
|
||||
# endif
|
||||
# ifdef B115200
|
||||
case B115200: uiPortSpeed = 115200; break;
|
||||
case B115200: uiPortSpeed = 115200; break;
|
||||
# endif
|
||||
# ifdef B230400
|
||||
case B230400: uiPortSpeed = 230400; break;
|
||||
case B230400: uiPortSpeed = 230400; break;
|
||||
# endif
|
||||
# ifdef B460800
|
||||
case B460800: uiPortSpeed = 460800; break;
|
||||
case B460800: uiPortSpeed = 460800; break;
|
||||
# endif
|
||||
# ifdef B921600
|
||||
case B921600: uiPortSpeed = 921600; break;
|
||||
case B921600: uiPortSpeed = 921600; break;
|
||||
# endif
|
||||
default: return 0;
|
||||
};
|
||||
return uiPortSpeed;
|
||||
default: return 0;
|
||||
};
|
||||
return uiPortSpeed;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -49,129 +49,129 @@ typedef struct {
|
|||
} serial_port_windows;
|
||||
|
||||
serial_port uart_open(const char* pcPortName) {
|
||||
char acPortName[255];
|
||||
serial_port_windows* sp = calloc(sizeof(serial_port_windows), sizeof(uint8_t));
|
||||
char acPortName[255];
|
||||
serial_port_windows* sp = calloc(sizeof(serial_port_windows), sizeof(uint8_t));
|
||||
|
||||
if (sp == 0) {
|
||||
printf("[!] UART failed to allocate memory\n");
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Copy the input "com?" to "\\.\COM?" format
|
||||
sprintf(acPortName,"\\\\.\\%s", pcPortName);
|
||||
_strupr(acPortName);
|
||||
if (sp == 0) {
|
||||
printf("[!] UART failed to allocate memory\n");
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Copy the input "com?" to "\\.\COM?" format
|
||||
sprintf(acPortName,"\\\\.\\%s", pcPortName);
|
||||
_strupr(acPortName);
|
||||
|
||||
// Try to open the serial port
|
||||
// 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);
|
||||
if (sp->hPort == INVALID_HANDLE_VALUE) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Try to open the serial port
|
||||
// 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);
|
||||
if (sp->hPort == INVALID_HANDLE_VALUE) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Prepare the device control
|
||||
// doesn't matter since PM3 device ignors this CDC command: set_line_coding in usb_cdc.c
|
||||
memset(&sp->dcb, 0, sizeof(DCB));
|
||||
sp->dcb.DCBlength = sizeof(DCB);
|
||||
if (!BuildCommDCBA("baud=115200 parity=N data=8 stop=1", &sp->dcb)) {
|
||||
uart_close(sp);
|
||||
printf("[!] UART error cdc setup\n");
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Prepare the device control
|
||||
// doesn't matter since PM3 device ignors this CDC command: set_line_coding in usb_cdc.c
|
||||
memset(&sp->dcb, 0, sizeof(DCB));
|
||||
sp->dcb.DCBlength = sizeof(DCB);
|
||||
if (!BuildCommDCBA("baud=115200 parity=N data=8 stop=1", &sp->dcb)) {
|
||||
uart_close(sp);
|
||||
printf("[!] UART error cdc setup\n");
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Update the active serial port
|
||||
if (!SetCommState(sp->hPort, &sp->dcb)) {
|
||||
uart_close(sp);
|
||||
printf("[!] UART error while setting com state\n");
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// all zero's configure: no timeout for read/write used.
|
||||
// took settings from libnfc/buses/uart.c
|
||||
// Update the active serial port
|
||||
if (!SetCommState(sp->hPort, &sp->dcb)) {
|
||||
uart_close(sp);
|
||||
printf("[!] UART error while setting com state\n");
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// all zero's configure: no timeout for read/write used.
|
||||
// took settings from libnfc/buses/uart.c
|
||||
#ifdef WITH_FPC
|
||||
sp->ct.ReadIntervalTimeout = 1000;
|
||||
sp->ct.ReadTotalTimeoutMultiplier = 0;
|
||||
sp->ct.ReadTotalTimeoutConstant = 1500;
|
||||
sp->ct.WriteTotalTimeoutMultiplier = 1000;
|
||||
sp->ct.WriteTotalTimeoutConstant = 0;
|
||||
sp->ct.ReadIntervalTimeout = 1000;
|
||||
sp->ct.ReadTotalTimeoutMultiplier = 0;
|
||||
sp->ct.ReadTotalTimeoutConstant = 1500;
|
||||
sp->ct.WriteTotalTimeoutMultiplier = 1000;
|
||||
sp->ct.WriteTotalTimeoutConstant = 0;
|
||||
#else
|
||||
sp->ct.ReadIntervalTimeout = 30;
|
||||
sp->ct.ReadTotalTimeoutMultiplier = 0;
|
||||
sp->ct.ReadTotalTimeoutConstant = 30;
|
||||
sp->ct.WriteTotalTimeoutMultiplier = 30;
|
||||
sp->ct.WriteTotalTimeoutConstant = 0;
|
||||
sp->ct.ReadIntervalTimeout = 30;
|
||||
sp->ct.ReadTotalTimeoutMultiplier = 0;
|
||||
sp->ct.ReadTotalTimeoutConstant = 30;
|
||||
sp->ct.WriteTotalTimeoutMultiplier = 30;
|
||||
sp->ct.WriteTotalTimeoutConstant = 0;
|
||||
#endif
|
||||
|
||||
if (!SetCommTimeouts(sp->hPort, &sp->ct)) {
|
||||
uart_close(sp);
|
||||
printf("[!] UART error while setting comm time outs\n");
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
if (!SetCommTimeouts(sp->hPort, &sp->ct)) {
|
||||
uart_close(sp);
|
||||
printf("[!] UART error while setting comm time outs\n");
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
|
||||
#ifdef WITH_FPC
|
||||
if ( uart_set_speed(sp, 115200) ) {
|
||||
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
|
||||
} else {
|
||||
uart_set_speed(sp, 9600);
|
||||
printf("[=] UART Setting serial baudrate 9600 [FPC enabled]\n");
|
||||
}
|
||||
if ( uart_set_speed(sp, 115200) ) {
|
||||
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
|
||||
} else {
|
||||
uart_set_speed(sp, 9600);
|
||||
printf("[=] UART Setting serial baudrate 9600 [FPC enabled]\n");
|
||||
}
|
||||
#else
|
||||
bool success = uart_set_speed(sp, 460800);
|
||||
if (success) {
|
||||
printf("[=] UART Setting serial baudrate 460800\n");
|
||||
} else {
|
||||
uart_set_speed(sp, 115200);
|
||||
printf("[=] UART Setting serial baudrate 115200\n");
|
||||
}
|
||||
bool success = uart_set_speed(sp, 460800);
|
||||
if (success) {
|
||||
printf("[=] UART Setting serial baudrate 460800\n");
|
||||
} else {
|
||||
uart_set_speed(sp, 115200);
|
||||
printf("[=] UART Setting serial baudrate 115200\n");
|
||||
}
|
||||
#endif
|
||||
return sp;
|
||||
return sp;
|
||||
}
|
||||
|
||||
void uart_close(const serial_port sp) {
|
||||
if (((serial_port_windows*)sp)->hPort != INVALID_HANDLE_VALUE )
|
||||
CloseHandle(((serial_port_windows*)sp)->hPort);
|
||||
free(sp);
|
||||
if (((serial_port_windows*)sp)->hPort != INVALID_HANDLE_VALUE )
|
||||
CloseHandle(((serial_port_windows*)sp)->hPort);
|
||||
free(sp);
|
||||
}
|
||||
|
||||
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)
|
||||
switch (uiPortSpeed) {
|
||||
case 9600:
|
||||
case 19200:
|
||||
case 38400:
|
||||
case 57600:
|
||||
case 115200:
|
||||
case 230400:
|
||||
case 460800:
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
};
|
||||
// Set port speed (Input and Output)
|
||||
switch (uiPortSpeed) {
|
||||
case 9600:
|
||||
case 19200:
|
||||
case 38400:
|
||||
case 57600:
|
||||
case 115200:
|
||||
case 230400:
|
||||
case 460800:
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
};
|
||||
|
||||
spw = (serial_port_windows*)sp;
|
||||
spw->dcb.BaudRate = uiPortSpeed;
|
||||
bool result = SetCommState(spw->hPort, &spw->dcb);
|
||||
PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
return result;
|
||||
spw = (serial_port_windows*)sp;
|
||||
spw->dcb.BaudRate = uiPortSpeed;
|
||||
bool result = SetCommState(spw->hPort, &spw->dcb);
|
||||
PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t uart_get_speed(const serial_port sp) {
|
||||
const serial_port_windows* spw = (serial_port_windows*)sp;
|
||||
if (!GetCommState(spw->hPort, (serial_port) & spw->dcb))
|
||||
return spw->dcb.BaudRate;
|
||||
const serial_port_windows* spw = (serial_port_windows*)sp;
|
||||
if (!GetCommState(spw->hPort, (serial_port) & spw->dcb))
|
||||
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) {
|
||||
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) {
|
||||
DWORD txlen = 0;
|
||||
return WriteFile(((serial_port_windows*)sp)->hPort, p_tx, len, &txlen, NULL);
|
||||
DWORD txlen = 0;
|
||||
return WriteFile(((serial_port_windows*)sp)->hPort, p_tx, len, &txlen, NULL);
|
||||
}
|
||||
|
||||
#endif
|
132
zlib/deflate.c
132
zlib/deflate.c
|
@ -1160,10 +1160,10 @@ local uInt longest_match(s, cur_match)
|
|||
{
|
||||
unsigned chain_length = s->max_chain_length;/* max hash chain length */
|
||||
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 */
|
||||
#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
|
||||
int best_len = s->prev_length; /* best match length so far */
|
||||
#endif
|
||||
|
@ -1737,73 +1737,73 @@ local block_state deflate_fast(s, flush)
|
|||
|
||||
#ifdef ZLIB_PM3_TUNED
|
||||
local uInt try_harder(s, strstart, lookahead, hash_head)
|
||||
deflate_state *s;
|
||||
uInt strstart;
|
||||
uInt lookahead;
|
||||
IPos hash_head;
|
||||
deflate_state *s;
|
||||
uInt strstart;
|
||||
uInt lookahead;
|
||||
IPos hash_head;
|
||||
{
|
||||
uInt strstart_save = s->strstart;
|
||||
s->strstart = strstart;
|
||||
uInt lookahead_save = s->lookahead;
|
||||
s->lookahead = lookahead;
|
||||
uInt ins_h_save = s->ins_h;
|
||||
uInt combined_gain;
|
||||
uInt best_combined_gain = 0;
|
||||
uInt match_length;
|
||||
uInt prev_length = s->prev_length < MIN_MATCH ? 1 : s->prev_length;
|
||||
uInt best_prev_length = prev_length;
|
||||
uInt current_match_start = s->match_start;
|
||||
uInt current_match_length = s->match_length;
|
||||
uInt strstart_save = s->strstart;
|
||||
s->strstart = strstart;
|
||||
uInt lookahead_save = s->lookahead;
|
||||
s->lookahead = lookahead;
|
||||
uInt ins_h_save = s->ins_h;
|
||||
uInt combined_gain;
|
||||
uInt best_combined_gain = 0;
|
||||
uInt match_length;
|
||||
uInt prev_length = s->prev_length < MIN_MATCH ? 1 : s->prev_length;
|
||||
uInt best_prev_length = prev_length;
|
||||
uInt current_match_start = s->match_start;
|
||||
uInt current_match_length = s->match_length;
|
||||
|
||||
do {
|
||||
do {
|
||||
if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
|
||||
match_length = longest_match (s, hash_head);
|
||||
/* longest_match() sets match_start */
|
||||
} else {
|
||||
match_length = MIN_MATCH - 1;
|
||||
}
|
||||
match_length = longest_match (s, hash_head);
|
||||
/* longest_match() sets match_start */
|
||||
} else {
|
||||
match_length = MIN_MATCH - 1;
|
||||
}
|
||||
#if TOO_FAR <= 32767
|
||||
if (match_length == MIN_MATCH && s->strstart - s->match_start > TOO_FAR) {
|
||||
match_length = MIN_MATCH-1;
|
||||
}
|
||||
if (match_length == MIN_MATCH && s->strstart - s->match_start > TOO_FAR) {
|
||||
match_length = MIN_MATCH-1;
|
||||
}
|
||||
#endif
|
||||
if (s->strstart == strstart) { // store match at current position
|
||||
current_match_length = match_length;
|
||||
current_match_start = s->match_start;
|
||||
}
|
||||
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)
|
||||
} else {
|
||||
combined_gain = s->strstart - strstart + 1 - MIN_MATCH; // (possibly truncated) previous_length - 3 literals
|
||||
}
|
||||
if (match_length < MIN_MATCH) {
|
||||
combined_gain += 0; // no gain
|
||||
} else {
|
||||
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
|
||||
best_combined_gain = combined_gain;
|
||||
best_prev_length = s->strstart - strstart + 1;
|
||||
}
|
||||
s->strstart++;
|
||||
s->lookahead--;
|
||||
UPDATE_HASH(s, s->ins_h, s->window[(s->strstart) + (MIN_MATCH-1)]);
|
||||
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
|
||||
&& s->strstart <= s->window_size - MIN_LOOKAHEAD); // watch out for the end of the input
|
||||
if (s->strstart == strstart) { // store match at current position
|
||||
current_match_length = match_length;
|
||||
current_match_start = s->match_start;
|
||||
}
|
||||
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)
|
||||
} else {
|
||||
combined_gain = s->strstart - strstart + 1 - MIN_MATCH; // (possibly truncated) previous_length - 3 literals
|
||||
}
|
||||
if (match_length < MIN_MATCH) {
|
||||
combined_gain += 0; // no gain
|
||||
} else {
|
||||
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
|
||||
best_combined_gain = combined_gain;
|
||||
best_prev_length = s->strstart - strstart + 1;
|
||||
}
|
||||
s->strstart++;
|
||||
s->lookahead--;
|
||||
UPDATE_HASH(s, s->ins_h, s->window[(s->strstart) + (MIN_MATCH-1)]);
|
||||
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
|
||||
&& s->strstart <= s->window_size - MIN_LOOKAHEAD); // watch out for the end of the input
|
||||
|
||||
s->strstart = strstart_save;
|
||||
s->lookahead = lookahead_save;
|
||||
s->ins_h = ins_h_save;
|
||||
s->match_length = current_match_length;
|
||||
s->match_start = current_match_start;
|
||||
if (best_prev_length >= MIN_MATCH) {
|
||||
s->prev_length = best_prev_length;
|
||||
s->match_length = MIN_MATCH - 1;
|
||||
} else {
|
||||
s->prev_length = MIN_MATCH - 1;
|
||||
}
|
||||
return best_combined_gain;
|
||||
s->strstart = strstart_save;
|
||||
s->lookahead = lookahead_save;
|
||||
s->ins_h = ins_h_save;
|
||||
s->match_length = current_match_length;
|
||||
s->match_start = current_match_start;
|
||||
if (best_prev_length >= MIN_MATCH) {
|
||||
s->prev_length = best_prev_length;
|
||||
s->match_length = MIN_MATCH - 1;
|
||||
} else {
|
||||
s->prev_length = MIN_MATCH - 1;
|
||||
}
|
||||
return best_combined_gain;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -1850,9 +1850,9 @@ local block_state deflate_slow(s, flush)
|
|||
s->match_length = MIN_MATCH-1;
|
||||
|
||||
#ifdef ZLIB_PM3_TUNED
|
||||
if (s->prev_length < s->max_lazy_match) {
|
||||
try_harder(s, s->strstart, s->lookahead, hash_head);
|
||||
}
|
||||
if (s->prev_length < s->max_lazy_match) {
|
||||
try_harder(s, s->strstart, s->lookahead, hash_head);
|
||||
}
|
||||
|
||||
#else
|
||||
if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
|
||||
|
|
|
@ -857,7 +857,7 @@ int flush;
|
|||
case 1: /* fixed block */
|
||||
#ifdef ZLIB_PM3_TUNED
|
||||
strm->msg = (char *)"fixed block coding not supported";
|
||||
state->mode = BAD;
|
||||
state->mode = BAD;
|
||||
#else
|
||||
fixedtables(state);
|
||||
Tracev((stderr, "inflate: fixed codes block%s\n",
|
||||
|
|
|
@ -989,7 +989,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
|
|||
#endif
|
||||
} else {
|
||||
#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,
|
||||
max_blindex+1);
|
||||
compress_block(s, (const ct_data *)s->dyn_ltree,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue