coverity fixes

This commit is contained in:
iceman1001 2020-09-17 23:08:49 +02:00
commit f7e6327fdf

View file

@ -364,31 +364,36 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
if (pbmpheader->bpp != 24) { if (pbmpheader->bpp != 24) {
return PM3_ESOFT; return PM3_ESOFT;
} }
if (pbmpheader->B == 'M' || pbmpheader->M == 'B') { //0x4d42 if (pbmpheader->B == 'M' || pbmpheader->M == 'B') { //0x4d42
PrintAndLogEx(WARNING, "The file is not a BMP!"); PrintAndLogEx(WARNING, "The file is not a BMP!");
return PM3_ESOFT; return PM3_ESOFT;
} }
PrintAndLogEx(DEBUG, "file size = %d", pbmpheader->fsize); PrintAndLogEx(DEBUG, "file size = %d", pbmpheader->fsize);
PrintAndLogEx(DEBUG, "file offset = %d", pbmpheader->offset); PrintAndLogEx(DEBUG, "file offset = %d", pbmpheader->offset);
if (pbmpheader->fsize > bmpsize) { if (pbmpheader->fsize > bmpsize) {
PrintAndLogEx(WARNING, "The file is truncated!"); PrintAndLogEx(WARNING, "The file is truncated!");
return PM3_ESOFT; return PM3_ESOFT;
} }
// Get BMP file data pointer // Get BMP file data pointer
uint32_t offset = pbmpheader->offset; uint32_t offset = pbmpheader->offset;
uint16_t width = pbmpheader->BMP_Width; uint16_t width = pbmpheader->BMP_Width;
uint16_t height = pbmpheader->BMP_Height; uint16_t height = pbmpheader->BMP_Height;
int16_t *chanR = calloc(width*height, sizeof(int16_t)); int16_t *chanR = calloc(width * height, sizeof(int16_t));
if (chanR == NULL) { if (chanR == NULL) {
return PM3_EMALLOC; return PM3_EMALLOC;
} }
int16_t *chanG = calloc(width*height, sizeof(int16_t));
int16_t *chanG = calloc(width * height, sizeof(int16_t));
if (chanG == NULL) { if (chanG == NULL) {
free(chanR); free(chanR);
return PM3_EMALLOC; return PM3_EMALLOC;
} }
int16_t *chanB = calloc(width*height, sizeof(int16_t));
int16_t *chanB = calloc(width * height, sizeof(int16_t));
if (chanB == NULL) { if (chanB == NULL) {
free(chanR); free(chanR);
free(chanG); free(chanG);
@ -396,26 +401,26 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
} }
// Extracting BMP chans // Extracting BMP chans
for (uint16_t Y=0; Y<height; Y++) { for (uint16_t Y = 0; Y < height; Y++) {
for (uint16_t X=0; X<width; X++) { for (uint16_t X = 0; X < width; X++) {
chanB[X + (height - Y - 1) * width] = bmp[offset++]; chanB[X + (height - Y - 1) * width] = bmp[offset++];
chanG[X + (height - Y - 1) * width] = bmp[offset++]; chanG[X + (height - Y - 1) * width] = bmp[offset++];
chanR[X + (height - Y - 1) * width] = bmp[offset++]; chanR[X + (height - Y - 1) * width] = bmp[offset++];
} }
// Skip line padding // Skip line padding
offset+=width%4; offset += width % 4;
} }
if ((model_nr == M1in54B) || (model_nr == M2in13B)) { if ((model_nr == M1in54B) || (model_nr == M2in13B)) {
// for BW+Red screens: // for BW+Red screens:
uint8_t *mapBlack = calloc(width*height, sizeof(uint8_t)); uint8_t *mapBlack = calloc(width * height, sizeof(uint8_t));
if (mapBlack == NULL) { if (mapBlack == NULL) {
free(chanR); free(chanR);
free(chanG); free(chanG);
free(chanB); free(chanB);
return PM3_EMALLOC; return PM3_EMALLOC;
} }
uint8_t *mapRed = calloc(width*height, sizeof(uint8_t)); uint8_t *mapRed = calloc(width * height, sizeof(uint8_t));
if (mapRed == NULL) { if (mapRed == NULL) {
free(chanR); free(chanR);
free(chanG); free(chanG);
@ -432,14 +437,14 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
if (save_conversions) { if (save_conversions) {
// fill BMP chans // fill BMP chans
offset = pbmpheader->offset; offset = pbmpheader->offset;
for (uint16_t Y=0; Y<height; Y++) { for (uint16_t Y = 0; Y < height; Y++) {
for (uint16_t X=0; X<width; X++) { for (uint16_t X = 0; X < width; X++) {
bmp[offset++] = chanB[X + (height - Y - 1) * width] & 0xFF; bmp[offset++] = chanB[X + (height - Y - 1) * width] & 0xFF;
bmp[offset++] = chanG[X + (height - Y - 1) * width] & 0xFF; bmp[offset++] = chanG[X + (height - Y - 1) * width] & 0xFF;
bmp[offset++] = chanR[X + (height - Y - 1) * width] & 0xFF; bmp[offset++] = chanR[X + (height - Y - 1) * width] & 0xFF;
} }
// Skip line padding // Skip line padding
offset+=width%4; offset += width % 4;
} }
PrintAndLogEx(INFO, "Saving red+black dithered version..."); PrintAndLogEx(INFO, "Saving red+black dithered version...");
if (saveFile(filename, ".bmp", bmp, offset) != PM3_SUCCESS) { if (saveFile(filename, ".bmp", bmp, offset) != PM3_SUCCESS) {
@ -473,7 +478,7 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
free(mapRed); free(mapRed);
} else { } else {
// for BW-only screens: // for BW-only screens:
int16_t *chanGrey = calloc(width*height, sizeof(int16_t)); int16_t *chanGrey = calloc(width * height, sizeof(int16_t));
if (chanGrey == NULL) { if (chanGrey == NULL) {
free(chanR); free(chanR);
free(chanG); free(chanG);
@ -483,7 +488,7 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
rgb_to_gray(chanR, chanG, chanB, width, height, chanGrey); rgb_to_gray(chanR, chanG, chanB, width, height, chanGrey);
dither_chan_inplace(chanGrey, width, height); dither_chan_inplace(chanGrey, width, height);
uint8_t *mapBlack = calloc(width*height, sizeof(uint8_t)); uint8_t *mapBlack = calloc(width * height, sizeof(uint8_t));
if (mapBlack == NULL) { if (mapBlack == NULL) {
free(chanR); free(chanR);
free(chanG); free(chanG);
@ -496,14 +501,14 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
if (save_conversions) { if (save_conversions) {
// fill BMP chans // fill BMP chans
offset = pbmpheader->offset; offset = pbmpheader->offset;
for (uint16_t Y=0; Y<height; Y++) { for (uint16_t Y = 0; Y < height; Y++) {
for (uint16_t X=0; X<width; X++) { for (uint16_t X = 0; X < width; X++) {
bmp[offset++] = chanGrey[X + (height - Y - 1) * width] & 0xFF; bmp[offset++] = chanGrey[X + (height - Y - 1) * width] & 0xFF;
bmp[offset++] = chanGrey[X + (height - Y - 1) * width] & 0xFF; bmp[offset++] = chanGrey[X + (height - Y - 1) * width] & 0xFF;
bmp[offset++] = chanGrey[X + (height - Y - 1) * width] & 0xFF; bmp[offset++] = chanGrey[X + (height - Y - 1) * width] & 0xFF;
} }
// Skip line padding // Skip line padding
offset+=width%4; offset += width % 4;
} }
PrintAndLogEx(INFO, "Saving black dithered version..."); PrintAndLogEx(INFO, "Saving black dithered version...");
if (saveFile(filename, ".bmp", bmp, offset) != PM3_SUCCESS) { if (saveFile(filename, ".bmp", bmp, offset) != PM3_SUCCESS) {
@ -549,15 +554,18 @@ static void read_red(uint32_t i, uint8_t *l, uint8_t model_nr, uint8_t *red) {
static int transceive_blocking( uint8_t* txBuf, uint16_t txBufLen, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* actLen, bool retransmit ){ static int transceive_blocking( uint8_t* txBuf, uint16_t txBufLen, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* actLen, bool retransmit ){
uint8_t fail_num = 0; uint8_t fail_num = 0;
if (rxBufLen < 2) if (rxBufLen < 2) {
return PM3_EINVARG; return PM3_EINVARG;
}
while (1) { while (1) {
PacketResponseNG resp; PacketResponseNG resp;
SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, txBufLen, 0, txBuf, txBufLen); SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, txBufLen, 0, txBuf, txBufLen);
rxBuf[0] = 1; rxBuf[0] = 1;
if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
if (resp.oldarg[0] > rxBufLen) { if (resp.oldarg[0] > rxBufLen) {
PrintAndLogEx(WARNING, "Received % bytes, rxBuf too small (%)", resp.oldarg[0], rxBufLen); PrintAndLogEx(WARNING, "Received %u bytes, rxBuf too small (%u)", resp.oldarg[0], rxBufLen);
memcpy(rxBuf, resp.data.asBytes, rxBufLen); memcpy(rxBuf, resp.data.asBytes, rxBufLen);
*actLen = rxBufLen; *actLen = rxBufLen;
return PM3_ESOFT; return PM3_ESOFT;
@ -565,6 +573,7 @@ static int transceive_blocking( uint8_t* txBuf, uint16_t txBufLen, uint8_t* rxBu
memcpy(rxBuf, resp.data.asBytes, resp.oldarg[0]); memcpy(rxBuf, resp.data.asBytes, resp.oldarg[0]);
*actLen = resp.oldarg[0]; *actLen = resp.oldarg[0];
} }
if ((retransmit) && (rxBuf[0] != 0 || rxBuf[1] != 0)) { if ((retransmit) && (rxBuf[0] != 0 || rxBuf[1] != 0)) {
fail_num++; fail_num++;
if (fail_num > 10) { if (fail_num > 10) {