Skip to content

Commit 8796326

Browse files
authored
Merge pull request #12 from tectrolabs/fixing_sonarqube_findings
Addressing C/C++ source code issues found with SonarQube scanner
2 parents 45ca58c + b796052 commit 8796326

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1361
-1063
lines changed

linux-and-macOS/swrng/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ endif
1616
CFLAGS = -O2 -I$(IDIR) $(IDIR_MACOS) -Wall -Wextra
1717
CFLAGS_THREAD = -lpthread
1818
CPPFLAGS = $(CFLAGS) -std=c++11
19-
#CLANGSTD = -std=c89
19+
#CLANGSTD = -std=c99
2020
LDFLAGS = -lusb-1.0 $(LDIR_MACOS)
2121
LDCPPFLAGS = $(LDFLAGS) -lstdc++
2222

linux-and-macOS/swrng/api-inc/ApiStructs.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
/**
1414
* @file ApiStructs.h
15-
* @date 7/8/2023
15+
* @date 9/17/2023
1616
* @Author: Andrian Belinski
17-
* @version 1.0
17+
* @version 1.1
1818
*
1919
* @brief Data structures used in the API implementation.
2020
*/
@@ -36,7 +36,9 @@ typedef struct {
3636
int64_t totalRetries;
3737

3838
/* Used for measuring performance */
39-
time_t beginTime, endTime, totalTime;
39+
time_t beginTime;
40+
time_t endTime;
41+
time_t totalTime;
4042

4143
/* Measured download speed in KB/sec */
4244
int downloadSpeedKBsec;

linux-and-macOS/swrng/api-inc/RandomSeqGenerator.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* RandomSeqGenerator.h
3-
* Ver 2.2
3+
* Ver 2.3
44
*/
55

66
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -23,15 +23,13 @@
2323
#include <cstring>
2424
#include <sstream>
2525

26-
using namespace std;
27-
2826
namespace swiftrng {
2927

3028
class RandomSeqGenerator {
3129
public:
3230
RandomSeqGenerator(int deviceNumber, uint32_t range);
3331
int generateSequence(uint32_t *dest, uint32_t size);
34-
string getLastErrorMessage() const {return m_error_log_oss.str();}
32+
std::string getLastErrorMessage() const {return m_error_log_oss.str();}
3533
virtual ~RandomSeqGenerator();
3634

3735
private:
@@ -42,7 +40,7 @@ class RandomSeqGenerator {
4240
int open_device();
4341

4442
private:
45-
ostringstream m_error_log_oss;
43+
std::ostringstream m_error_log_oss;
4644

4745
int32_t *m_number_buffer_1;
4846

linux-and-macOS/swrng/api-inc/SwiftRngApi.h

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
/**
1414
* @file SwiftRngApi.h
15-
* @date 7/8/2022
15+
* @date 9/17/2023
1616
* @Author: Andrian Belinski
17-
* @version 1.0
17+
* @version 1.1
1818
*
1919
* @brief Implements the API for interacting with the SwiftRNG device.
2020
*/
@@ -41,8 +41,6 @@
4141
#include <USBSerialDevice.h>
4242
#endif
4343

44-
using namespace std;
45-
4644
namespace swiftrng {
4745

4846
class SwiftRngApi {
@@ -51,7 +49,7 @@ class SwiftRngApi {
5149
SwiftRngApi();
5250
int open(int devNum);
5351
int close();
54-
int is_open();
52+
int is_open() const;
5553
int get_entropy(unsigned char *buffer, long length);
5654
int get_entropy_ex(unsigned char *buffer, long length);
5755
int get_raw_data_block(NoiseSourceRawData *noise_source_raw_data, int noise_source_num);
@@ -74,17 +72,17 @@ class SwiftRngApi {
7472
int enable_statistical_tests();
7573
DeviceStatistics* generate_device_statistics();
7674
const char* get_last_error_message();
77-
string get_last_error_log() {return m_last_error_log_oss.str();}
75+
std::string get_last_error_log() const {return m_last_error_log_oss.str();}
7876
void enable_printing_error_messages();
79-
int get_max_apt_failures_per_block(uint16_t *max_apt_failures_per_block);
80-
int get_max_rct_failures_per_block(uint16_t *max_rct_failures_per_block);
77+
int get_max_apt_failures_per_block(uint16_t *max_apt_failures_per_block) const;
78+
int get_max_rct_failures_per_block(uint16_t *max_rct_failures_per_block) const;
8179

8280

8381
virtual ~SwiftRngApi();
8482

8583

8684
private:
87-
bool is_context_initialized() { return m_is_initialized; }
85+
bool is_context_initialized() const { return m_is_initialized; }
8886
void initialize();
8987
void clear_last_error_msg();
9088
void close_USB_lib();
@@ -95,42 +93,42 @@ class SwiftRngApi {
9593

9694
void sha256_initialize();
9795
void sha512_initialize();
98-
void sha256_stampSerialNumber(void *input_block);
96+
void sha256_stampSerialNumber(uint32_t *input_block);
9997
void sha256_initializeSerialNumber(uint32_t init_value);
100-
int sha512_generateHash(uint64_t *src, int16_t len, uint64_t *dst);
98+
int sha512_generateHash(const uint64_t *src, int16_t len, uint64_t *dst);
10199
void sha512_hashCurrentBlock();
102-
uint64_t sha512_sigma1(uint64_t *x);
100+
uint64_t sha512_sigma1(const uint64_t *x);
103101
int sha256_selfTest();
104102
int sha512_selfTest();
105103
int xorshift64_selfTest();
106104
uint32_t sha256_ch(uint32_t *x, uint32_t *y, uint32_t *z);
107-
uint32_t sha256_maj(uint32_t *x, uint32_t *y, uint32_t *z);
108-
uint32_t sha256_sum0(uint32_t *x);
109-
uint32_t sha256_sum1(uint32_t *x);
110-
uint32_t sha256_sigma0(uint32_t *x);
111-
uint32_t sha256_sigma1(uint32_t *x);
112-
uint64_t sha512_ch(uint64_t *x, uint64_t *y, uint64_t *z);
113-
uint64_t sha512_maj(uint64_t *x, uint64_t *y, uint64_t *z);
114-
uint64_t sha512_sum0(uint64_t *x);
115-
uint64_t sha512_sum1(uint64_t *x);
116-
uint64_t sha512_sigma0(uint64_t *x);
105+
uint32_t sha256_maj(const uint32_t *x, const uint32_t *y, const uint32_t *z);
106+
uint32_t sha256_sum0(const uint32_t *x);
107+
uint32_t sha256_sum1(const uint32_t *x);
108+
uint32_t sha256_sigma0(const uint32_t *x);
109+
uint32_t sha256_sigma1(const uint32_t *x);
110+
uint64_t sha512_ch(const uint64_t *x, const uint64_t *y, const uint64_t *z);
111+
uint64_t sha512_maj(const uint64_t *x, const uint64_t *y, const uint64_t *z);
112+
uint64_t sha512_sum0(const uint64_t *x);
113+
uint64_t sha512_sum1(const uint64_t *x);
114+
uint64_t sha512_sigma0(const uint64_t *x);
117115
void sha256_hashCurrentBlock();
118-
int sha256_generateHash(uint32_t *src, int16_t len, uint32_t *dst);
119-
void swrng_printErrorMessage(const string &err_msg);
116+
int sha256_generateHash(const uint32_t *src, int16_t len, uint32_t *dst);
117+
void swrng_printErrorMessage(const std::string &err_msg);
120118
int swrng_handleDeviceVersion();
121119
void swrng_clearReceiverBuffer();
122-
uint64_t xorshift64_postProcessWord(uint64_t raw_word);
120+
uint64_t xorshift64_postProcessWord(uint64_t raw_word) const;
123121
void xorshift64_postProcessWords(uint64_t *buffer, int num_elements);
124122
void xorshift64_postProcess(uint8_t *buffer, int num_elements);
125-
int swrng_snd_rcv_usb_data(char *snd, int size_snd, char *rcv, int size_rcv, int op_timeout_secs);
123+
int swrng_snd_rcv_usb_data(const char *snd, int size_snd, char *rcv, int size_rcv, int op_timeout_secs);
126124
int swrng_chip_read_data(char *buff, int length, int op_timeout_secs);
127125
void swrng_contextReset();
128126
int swrng_getEntropyBytes();
129127
int swrng_rcv_rnd_bytes();
130128
void test_samples();
131-
void swrng_updateDevInfoList(DeviceInfoList* dev_info_list, int *curt_found_dev_num);
132-
uint32_t rotr32(uint32_t sb, uint32_t w) { return (((w) >> (sb)) | ((w) << (32-(sb)))); }
133-
uint64_t rotr64(uint64_t sb, uint64_t w) { return (((w) >> (sb)) | ((w) << (64-(sb)))); }
129+
void swrng_updateDevInfoList(DeviceInfoList* dev_info_list, int *curt_found_dev_num) const;
130+
uint32_t rotr32(uint32_t sb, uint32_t w) const { return ((w) >> (sb)) | ((w) << (32-(sb))); }
131+
uint64_t rotr64(uint64_t sb, uint64_t w) const { return ((w) >> (sb)) | ((w) << (64-(sb))); }
134132

135133
private:
136134

@@ -145,14 +143,14 @@ class SwiftRngApi {
145143
static const int c_max_cdc_com_ports {80};
146144

147145
// USB device hardware ID string used in windows
148-
static const wstring c_hardware_id;
146+
static const std::wstring c_hardware_id;
149147
#endif
150148

151149
// Device specific USB bulk end point number for OUT operation
152-
const int SWRNG_BULK_EP_OUT {0x01};
150+
const unsigned char c_bulk_ep_out {0x01};
153151

154152
// Device specific USB bulk end point number for IN operation
155-
const int SWRNG_BULK_EP_IN {0x81};
153+
const unsigned char c_bulk_ep_in {0x81};
156154

157155
// Method id for using SHA-256 for post processing
158156
const int c_sha256_pp_method_id {0};
@@ -191,18 +189,48 @@ class SwiftRngApi {
191189

192190
// A structure used for generating SHA-256 hash
193191
struct {
194-
uint32_t a, b, c, d, e, f, g, h;
195-
uint32_t h0, h1, h2, h3, h4, h5, h6, h7;
196-
uint32_t tmp1, tmp2;
192+
uint32_t a;
193+
uint32_t b;
194+
uint32_t c;
195+
uint32_t d;
196+
uint32_t e;
197+
uint32_t f;
198+
uint32_t g;
199+
uint32_t h;
200+
uint32_t h0;
201+
uint32_t h1;
202+
uint32_t h2;
203+
uint32_t h3;
204+
uint32_t h4;
205+
uint32_t h5;
206+
uint32_t h6;
207+
uint32_t h7;
208+
uint32_t tmp1;
209+
uint32_t tmp2;
197210
uint32_t w[64];
198211
uint32_t blockSerialNumber;
199212
} m_sha256_ctxt;
200213

201214
// A structure used for generating SHA-512 hash
202215
struct {
203-
uint64_t a,b,c,d,e,f,g,h;
204-
uint64_t h0,h1,h2,h3,h4,h5,h6,h7;
205-
uint64_t tmp1, tmp2;
216+
uint64_t a;
217+
uint64_t b;
218+
uint64_t c;
219+
uint64_t d;
220+
uint64_t e;
221+
uint64_t f;
222+
uint64_t g;
223+
uint64_t h;
224+
uint64_t h0;
225+
uint64_t h1;
226+
uint64_t h2;
227+
uint64_t h3;
228+
uint64_t h4;
229+
uint64_t h5;
230+
uint64_t h6;
231+
uint64_t h7;
232+
uint64_t tmp1;
233+
uint64_t tmp2;
206234
uint64_t w[80];
207235
} m_sha512_ctxt;
208236

@@ -292,7 +320,7 @@ class SwiftRngApi {
292320

293321
// A storage for holding the last error message. Many operations, when failed, will store the error message in this buffer.
294322
// The error text message can be retrieved with `get_last_error_message()` method.
295-
ostringstream m_last_error_log_oss;
323+
std::ostringstream m_last_error_log_oss;
296324

297325
// Number of bytes to allocate for `m_last_error_log_char`;
298326
const int c_max_last_error_log_size {256};
@@ -338,16 +366,16 @@ class SwiftRngApi {
338366
//
339367
// Text constants
340368
//
341-
static const string c_dev_not_open_msg;
342-
static const string c_freq_table_invalid_msg;
343-
static const string c_cannot_disable_pp_msg;
344-
static const string c_pp_op_not_supported_msg;
345-
static const string c_diag_op_not_supported_msg;
346-
static const string c_pp_method_not_supported_msg;
347-
static const string c_cannot_get_freq_table_for_device_msg;
348-
static const string c_too_many_devices_msg;
349-
static const string c_cannot_read_device_descriptor_msg;
350-
static const string c_libusb_init_failure_msg;
369+
static const std::string c_dev_not_open_msg;
370+
static const std::string c_freq_table_invalid_msg;
371+
static const std::string c_cannot_disable_pp_msg;
372+
static const std::string c_pp_op_not_supported_msg;
373+
static const std::string c_diag_op_not_supported_msg;
374+
static const std::string c_pp_method_not_supported_msg;
375+
static const std::string c_cannot_get_freq_table_for_device_msg;
376+
static const std::string c_too_many_devices_msg;
377+
static const std::string c_cannot_read_device_descriptor_msg;
378+
static const std::string c_libusb_init_failure_msg;
351379

352380
};
353381

linux-and-macOS/swrng/api-inc/USBSerialDevice.h

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* USBSerialDevice.h
3-
* Ver 1.1
3+
* Ver 1.2
44
*/
55

66
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
77
8-
Copyright (C) 2014-2020 TectroLabs, https://tectrolabs.com
8+
Copyright (C) 2014-2023 TectroLabs, https://tectrolabs.com
99
1010
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
1111
INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
@@ -28,34 +28,37 @@
2828
#include <sys/types.h>
2929
#include <sys/file.h>
3030
#include <ctype.h>
31-
32-
#define MAX_DEVICE_COUNT (25)
33-
#define MAX_SIZE_DEVICE_NAME (128)
31+
#include <sstream>
3432

3533
class USBSerialDevice {
36-
private:
37-
int fd;
38-
int lock;
39-
char devNames[MAX_DEVICE_COUNT][MAX_SIZE_DEVICE_NAME];
40-
int activeDevCount;
41-
bool deviceConnected;
42-
char lastError[512];
43-
void setErrMsg(const char *errMessage);
44-
void clearErrMsg();
45-
void purgeComm();
4634
public:
4735
USBSerialDevice();
4836
virtual ~USBSerialDevice();
4937
void initialize();
50-
bool isConnected();
38+
bool is_connected() const;
5139
bool connect(const char *devicePath);
5240
bool disconnect();
53-
const char* getLastErrMsg();
54-
int sendCommand(unsigned char *snd, int sizeSnd, int *bytesSent);
55-
int receiveDeviceData(unsigned char *rcv, int sizeRcv, int *bytesReceived);
56-
int getConnectedDeviceCount();
57-
void scanForConnectedDevices();
58-
bool retrieveConnectedDevice(char *devName, int devNum);
41+
void clear_error_log();
42+
std::string get_error_log() const;
43+
int send_command(const unsigned char *snd, int sizeSnd, int *bytesSent);
44+
int receive_data(unsigned char *rcv, int sizeRcv, int *bytesReceived);
45+
int get_device_count() const;
46+
void scan_available_devices();
47+
bool retrieve_device_path(char *devName, int devNum) const;
48+
49+
private:
50+
void set_error_message(const char *error_message);
51+
void purge_comm_data() const;
52+
53+
private:
54+
static const int c_max_devices = 25;
55+
static const int c_max_size_device_name = 128;
56+
int m_fd {-1};
57+
int m_lock;
58+
char c_device_names[c_max_devices][c_max_size_device_name];
59+
int m_active_device_count {0};
60+
bool m_device_connected {false};
61+
std::ostringstream m_error_log_oss;
5962

6063
};
6164

0 commit comments

Comments
 (0)