[Hamlib-commits] Hamlib -- Ham radio control libraries branch master updated. 21cbd7704738d6e7da778
Library to control radio transceivers and receivers
Brought to you by:
n0nb
From: n0nb <n0...@us...> - 2023-12-18 16:31:06
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "Hamlib -- Ham radio control libraries". The branch, master has been updated via 21cbd7704738d6e7da778c06145463e33f77b611 (commit) via a3676e0e3b7f20f5a85cbbccb6adc0fdcd4a0f96 (commit) via 3a6073d4013b63ab7e00e27e69ff6340ff7ca147 (commit) via 98edf92240ff64c62321886cd7f2c8e60e9dcb5e (commit) via 19bf90bbb48f80a0b1bc24ae9ab14896b1922a9e (commit) via bc6f874675a2c66930457557f3f66086ec465afa (commit) via dbb8f4d5a7d68e90633fe9357d9a42c7eedb0819 (commit) via 1f38d591539f4b47889e8b8e792c9ccbb5900de2 (commit) via 248d5e44a93d773119508e5bf4c363d1e5434342 (commit) via 027b7801899e67133c153b3918205bf5464946e1 (commit) via ac7c908036533b629ce70269e3d6b992c58ed3f8 (commit) via 50711dcfb8c88729bf50291fd68034b16e485236 (commit) via 11876d7cc4344d70354c28ad3c763c5999aab69a (commit) via e267c6276495bee71eaf217715874cb56bcb4d6b (commit) via e1b3ac2a95a052f8aa23b2aac2fa170a50b2771e (commit) via 67e57f6e4db5f6de582445113c4785fb116ec01c (commit) via f81d49cdabafa9254f539e1fa66fecf393674b20 (commit) via 5dac79340fad70c6422ff686b9ff58dfc996afed (commit) via 10accf17b83227ef16e27e15c7bf7df599cb89c8 (commit) via 18c88c9f3859c3201eaac30e000a69e6d5e232c2 (commit) via 5dc55ff9e1ea74732190eb57470e1a8a39a8a4a3 (commit) via 81db043fbd500c0a44f95c94a343d1a952976e75 (commit) via 27d7e8bd2d2013c60ec87e1393874ece8fea0273 (commit) via 5f0c8691d7fdc7ccafee7fb888fbdaf9a4182931 (commit) via e9f9286f212f4874f90d34aa6100481cfbab0910 (commit) via b25b4960512f0ca2411bc5509cec023d62fa4683 (commit) via 72fba4eb5b0d08de4818a5b56ecb08e02524e1a6 (commit) via 63c615a452ec066ca989600eb8d8bd24d2c45bf2 (commit) via ea823e911c5afdab7120e8b8d8f336e572428734 (commit) from ae69772d2a5dd8adad0c8de51b6c44db5f060e35 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 21cbd7704738d6e7da778c06145463e33f77b611 Author: Mike Black W9MDB <mdb...@ya...> Date: Mon Dec 18 09:11:19 2023 -0600 Add retries in easycomm.c diff --git a/rotators/easycomm/easycomm.c b/rotators/easycomm/easycomm.c index 3007d3491..419c16fd8 100644 --- a/rotators/easycomm/easycomm.c +++ b/rotators/easycomm/easycomm.c @@ -50,6 +50,7 @@ easycomm_transaction(ROT *rot, const char *cmdstr, char *data, size_t data_len) { struct rot_state *rs; int retval; + int retry = rot->caps->retry; rig_debug(RIG_DEBUG_TRACE, "%s called: %s\n", __func__, cmdstr); @@ -59,33 +60,39 @@ easycomm_transaction(ROT *rot, const char *cmdstr, char *data, size_t data_len) } rs = &rot->state; - rig_flush(&rs->rotport); - retval = write_block(&rs->rotport, (unsigned char *) cmdstr, strlen(cmdstr)); - if (retval != RIG_OK) + do { - goto transaction_quit; - } + rig_flush(&rs->rotport); + retval = write_block(&rs->rotport, (unsigned char *) cmdstr, strlen(cmdstr)); - if (data == NULL) - { - return RIG_OK; /* don't want a reply */ - } + if (retval != RIG_OK) + { + goto transaction_quit; + } - retval = read_string(&rs->rotport, (unsigned char *) data, data_len, - "\n", 1, 0, 1); + if (data == NULL) + { + return RIG_OK; /* don't want a reply */ + } - if (retval < 0) - { - rig_debug(RIG_DEBUG_TRACE, "%s read_string failed with status %d:%s\n", __func__, - retval, strerror(retval)); - goto transaction_quit; - } - else - { - rig_debug(RIG_DEBUG_TRACE, "%s read_string: %s\n", __func__, data); - retval = RIG_OK; + retval = read_string(&rs->rotport, (unsigned char *) data, data_len, + "\n", 1, 0, 1); + + if (retval < 0) + { + rig_debug(RIG_DEBUG_TRACE, "%s read_string failed with status %d:%s\n", + __func__, + retval, strerror(retval)); + goto transaction_quit; + } + else + { + rig_debug(RIG_DEBUG_TRACE, "%s read_string: %s\n", __func__, data); + retval = RIG_OK; + } } + while (--retry && retval != RIG_OK); transaction_quit: return retval; @@ -520,7 +527,7 @@ const struct rot_caps easycomm1_rot_caps = ROT_MODEL(ROT_MODEL_EASYCOMM1), .model_name = "EasycommI", .mfg_name = "Hamlib", - .version = "20220109.0", + .version = "20231218.0", .copyright = "LGPL", .status = RIG_STATUS_STABLE, .rot_type = ROT_TYPE_OTHER, @@ -556,7 +563,7 @@ const struct rot_caps easycomm2_rot_caps = ROT_MODEL(ROT_MODEL_EASYCOMM2), .model_name = "EasycommII", .mfg_name = "Hamlib", - .version = "20191206.0", + .version = "20231218.0", .copyright = "LGPL", .status = RIG_STATUS_STABLE, .rot_type = ROT_TYPE_OTHER, @@ -602,7 +609,7 @@ const struct rot_caps easycomm3_rot_caps = ROT_MODEL(ROT_MODEL_EASYCOMM3), .model_name = "EasycommIII", .mfg_name = "Hamlib", - .version = "20201203.0", + .version = "2022312180", .copyright = "LGPL", .status = RIG_STATUS_STABLE, .rot_type = ROT_TYPE_OTHER, commit a3676e0e3b7f20f5a85cbbccb6adc0fdcd4a0f96 Author: Mike Black W9MDB <mdb...@ya...> Date: Mon Dec 18 09:05:15 2023 -0600 Improve error output in easycom.c to show message diff --git a/rotators/easycomm/easycomm.c b/rotators/easycomm/easycomm.c index af3a9ae69..3007d3491 100644 --- a/rotators/easycomm/easycomm.c +++ b/rotators/easycomm/easycomm.c @@ -77,8 +77,8 @@ easycomm_transaction(ROT *rot, const char *cmdstr, char *data, size_t data_len) if (retval < 0) { - rig_debug(RIG_DEBUG_TRACE, "%s read_string failed with status %d\n", __func__, - retval); + rig_debug(RIG_DEBUG_TRACE, "%s read_string failed with status %d:%s\n", __func__, + retval, strerror(retval)); goto transaction_quit; } else commit 3a6073d4013b63ab7e00e27e69ff6340ff7ca147 Author: Mike Black W9MDB <mdb...@ya...> Date: Mon Dec 18 08:25:15 2023 -0600 astyle files diff --git a/src/network.c b/src/network.c index bb0a1f6eb..3f519348d 100644 --- a/src/network.c +++ b/src/network.c @@ -917,8 +917,9 @@ void *multicast_publisher(void *arg) struct rig_state *rs = &rig->state; struct rig_spectrum_line spectrum_line; uint8_t packet_type = MULTICAST_PUBLISHER_DATA_PACKET_TYPE_SPECTRUM; - multicast_publisher_priv_data *mcast_publisher_priv = (multicast_publisher_priv_data *) - rs->multicast_publisher_priv_data; + multicast_publisher_priv_data *mcast_publisher_priv = + (multicast_publisher_priv_data *) + rs->multicast_publisher_priv_data; struct sockaddr_in dest_addr; int socket_fd = args->socket_fd; @@ -984,6 +985,7 @@ void *multicast_publisher(void *arg) strerror(errno)); } } + mcast_publisher_priv->thread_id = 0; rig_debug(RIG_DEBUG_VERBOSE, "%s(%d): Stopped multicast publisher\n", __FILE__, @@ -1320,10 +1322,12 @@ int network_multicast_publisher_start(RIG *rig, const char *multicast_addr, status = network_init(); #ifdef __MINGW32__ // always RIG_OK if not Windows + if (status != RIG_OK) { RETURNFUNC(status); } + #endif socket_fd = socket(AF_INET, SOCK_DGRAM, 0); @@ -1445,7 +1449,7 @@ int network_multicast_publisher_stop(RIG *rig) if (mcast_publisher_priv->thread_id != 0) { - int err = pthread_join(mcast_publisher_priv->thread_id, NULL); + int err = pthread_join(mcast_publisher_priv->thread_id, NULL); if (err) { commit 98edf92240ff64c62321886cd7f2c8e60e9dcb5e Author: Mike Black W9MDB <mdb...@ya...> Date: Mon Dec 18 07:53:55 2023 -0600 Update rig.h documentation a bit diff --git a/include/hamlib/rig.h b/include/hamlib/rig.h index f8a5a72ad..718e55d90 100644 --- a/include/hamlib/rig.h +++ b/include/hamlib/rig.h @@ -291,6 +291,8 @@ typedef unsigned int tone_t; /** * \brief Port type + * + * Note: All rigs may use a network:port address ( e.g. tcp/serial adapter) */ typedef enum rig_port_e { RIG_PORT_NONE = 0, /*!< No port */ commit 19bf90bbb48f80a0b1bc24ae9ab14896b1922a9e Author: Mike Black W9MDB <mdb...@ya...> Date: Mon Dec 18 07:13:23 2023 -0600 Minor update to rigctl.1 rigctld.1 diff --git a/doc/man1/rigctl.1 b/doc/man1/rigctl.1 index 771051601..db4535e2e 100644 --- a/doc/man1/rigctl.1 +++ b/doc/man1/rigctl.1 @@ -81,8 +81,7 @@ Use .I device as the file name of the port connected to the radio. .IP -Often a serial port, but could be a USB to serial adapter. Typically -.IR /dev/ttyS0 ", " /dev/ttyS1 ", " /dev/ttyUSB0 , +Typically /dev/ttyS0 ", " /dev/ttyS1 ", " /dev/ttyUSB0 , etc. on Linux, .IR COM1 ", " COM2 , etc. on MS Windows. The BSD flavors and Mac OS/X have their own designations. diff --git a/doc/man1/rigctld.1 b/doc/man1/rigctld.1 index 56b08109b..fa8277a4e 100644 --- a/doc/man1/rigctld.1 +++ b/doc/man1/rigctld.1 @@ -127,8 +127,7 @@ Use .I device as the file name of the port connected to the radio. .IP -Often a serial port, but could be a USB to serial adapter. Typically -.IR /dev/ttyS0 ", " /dev/ttyS1 ", " /dev/ttyUSB0 , +Typically /dev/ttyS0 ", " /dev/ttyS1 ", " /dev/ttyUSB0 , etc. on Linux, .IR COM1 ", " COM2 , etc. on MS Windows. The BSD flavors and Mac OS/X have their own designations. commit bc6f874675a2c66930457557f3f66086ec465afa Author: Mike Black W9MDB <mdb...@ya...> Date: Sun Dec 17 23:13:32 2023 -0600 Fix wireless detection diff --git a/src/network.c b/src/network.c index ada95a529..bb0a1f6eb 100644 --- a/src/network.c +++ b/src/network.c @@ -1142,17 +1142,17 @@ void *multicast_receiver(void *arg) dest_addr.sin_family = AF_INET; #ifdef __MINGW32__ - // Windows cannot bind to multicast group addresses for some unknown reason + // Windows wireless cannot bind to multicast group addresses for some unknown reason if (is_wireless()) { rig_debug(RIG_DEBUG_VERBOSE, - "%s: no wireless detect so INADDR_ANY is being used\n", __func__); + "%s: wireless detected so localhost is being used\n", __func__); + dest_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); } else { rig_debug(RIG_DEBUG_VERBOSE, - "%s: wireless detected so localhost is being used\n", __func__); - dest_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + "%s: no wireless detect so INADDR_ANY is being used\n", __func__); } #else commit dbb8f4d5a7d68e90633fe9357d9a42c7eedb0819 Author: Mike Black W9MDB <mdb...@ya...> Date: Sun Dec 17 22:29:21 2023 -0600 Remove some debug statements diff --git a/src/network.c b/src/network.c index cc3d287d3..ada95a529 100644 --- a/src/network.c +++ b/src/network.c @@ -1208,9 +1208,7 @@ void *multicast_receiver(void *arg) efds = rfds; select_result = select(socket_fd + 1, &rfds, NULL, &efds, &timeout); - rig_debug(RIG_DEBUG_VERBOSE,"%s(%d)\n", __func__, __LINE__); - rig_debug(RIG_DEBUG_VERBOSE,"%s(%d)\n", __func__, __LINE__); if (rs->multicast_receiver_run == 0) { rig_debug(RIG_DEBUG_VERBOSE, "%s(%d): pselect signal\n", __func__, __LINE__); commit 1f38d591539f4b47889e8b8e792c9ccbb5900de2 Author: Mike Black W9MDB <mdb...@ya...> Date: Sun Dec 17 17:38:28 2023 -0600 Fix Windows rigctl shutdown https://github.com/Hamlib/Hamlib/issues/1448 diff --git a/src/network.c b/src/network.c index 867aa04ed..cc3d287d3 100644 --- a/src/network.c +++ b/src/network.c @@ -470,6 +470,7 @@ extern void sync_callback(int lock); //! @cond Doxygen_Suppress #define MULTICAST_DATA_PIPE_TIMEOUT_MILLIS 1000 +#define MULTICAST_DATA_PIPE_TIMEOUT_USEC 100000 #if defined(WIN32) && defined(HAVE_WINDOWS_H) @@ -535,8 +536,7 @@ static int multicast_publisher_read_data(multicast_publisher_args { ssize_t result; - result = async_pipe_wait_for_data(mcast_publisher_args->data_pipe, - MULTICAST_DATA_PIPE_TIMEOUT_MILLIS); + result = async_pipe_wait_for_data(mcast_publisher_args->data_pipe, 100); if (result < 0) { @@ -660,8 +660,8 @@ static int multicast_publisher_read_data(const multicast_publisher_args ssize_t result; int retval; - timeout.tv_sec = MULTICAST_DATA_PIPE_TIMEOUT_MILLIS / 1000; - timeout.tv_usec = 0; + timeout.tv_sec = 0; + timeout.tv_usec = MULTICAST_DATA_PIPE_TIMEOUT_USEC; FD_ZERO(&rfds); FD_SET(fd, &rfds); @@ -917,6 +917,8 @@ void *multicast_publisher(void *arg) struct rig_state *rs = &rig->state; struct rig_spectrum_line spectrum_line; uint8_t packet_type = MULTICAST_PUBLISHER_DATA_PACKET_TYPE_SPECTRUM; + multicast_publisher_priv_data *mcast_publisher_priv = (multicast_publisher_priv_data *) + rs->multicast_publisher_priv_data; struct sockaddr_in dest_addr; int socket_fd = args->socket_fd; @@ -982,8 +984,9 @@ void *multicast_publisher(void *arg) strerror(errno)); } } + mcast_publisher_priv->thread_id = 0; - rig_debug(RIG_DEBUG_VERBOSE, "%s(%d): Stopping multicast publisher\n", __FILE__, + rig_debug(RIG_DEBUG_VERBOSE, "%s(%d): Stopped multicast publisher\n", __FILE__, __LINE__); return NULL; } @@ -1198,14 +1201,16 @@ void *multicast_receiver(void *arg) int select_result; ssize_t result; - timeout.tv_sec = 1; - timeout.tv_usec = 0; + timeout.tv_sec = 0; + timeout.tv_usec = 100000; FD_ZERO(&rfds); FD_SET(socket_fd, &rfds); efds = rfds; select_result = select(socket_fd + 1, &rfds, NULL, &efds, &timeout); + rig_debug(RIG_DEBUG_VERBOSE,"%s(%d)\n", __func__, __LINE__); + rig_debug(RIG_DEBUG_VERBOSE,"%s(%d)\n", __func__, __LINE__); if (rs->multicast_receiver_run == 0) { rig_debug(RIG_DEBUG_VERBOSE, "%s(%d): pselect signal\n", __func__, __LINE__); @@ -1215,7 +1220,7 @@ void *multicast_receiver(void *arg) if (select_result == 0) { // Select timed out -// rig_debug(RIG_DEBUG_ERR, "%s: select timeout\n", __FILE__); + //rig_debug(RIG_DEBUG_ERR, "%s: select timeout\n", __FILE__); continue; } @@ -1268,7 +1273,7 @@ void *multicast_receiver(void *arg) rs->multicast_receiver_run = 0; - rig_debug(RIG_DEBUG_VERBOSE, "%s(%d): Stopping multicast receiver\n", __FILE__, + rig_debug(RIG_DEBUG_VERBOSE, "%s(%d): Stopped multicast receiver\n", __FILE__, __LINE__); return NULL; } @@ -1316,10 +1321,12 @@ int network_multicast_publisher_start(RIG *rig, const char *multicast_addr, status = network_init(); +#ifdef __MINGW32__ // always RIG_OK if not Windows if (status != RIG_OK) { RETURNFUNC(status); } +#endif socket_fd = socket(AF_INET, SOCK_DGRAM, 0); @@ -1367,7 +1374,6 @@ int network_multicast_publisher_start(RIG *rig, const char *multicast_addr, } rs->snapshot_packet_sequence_number = 0; - rs->multicast_publisher_run = 1; rs->multicast_publisher_priv_data = calloc(1, sizeof(multicast_publisher_priv_data)); @@ -1441,7 +1447,7 @@ int network_multicast_publisher_stop(RIG *rig) if (mcast_publisher_priv->thread_id != 0) { - int err = pthread_join(mcast_publisher_priv->thread_id, NULL); + int err = pthread_join(mcast_publisher_priv->thread_id, NULL); if (err) { commit 248d5e44a93d773119508e5bf4c363d1e5434342 Author: Mike Black W9MDB <mdb...@ya...> Date: Sun Dec 17 12:52:34 2023 -0600 Fix cppcheck warning in newcat.c diff --git a/rigs/yaesu/newcat.c b/rigs/yaesu/newcat.c index 4585cc06e..3a392bdf4 100644 --- a/rigs/yaesu/newcat.c +++ b/rigs/yaesu/newcat.c @@ -7565,7 +7565,7 @@ int newcat_get_trn(RIG *rig, int *trn) SNPRINTF(priv->cmd_str, sizeof(priv->cmd_str), "%s%c", command, cat_term); /* Get Auto Information */ - if (RIG_OK != (err = newcat_get_cmd(rig))) + if (RIG_OK != newcat_get_cmd(rig)) { // if we failed to get AI we turn it off and try again SNPRINTF(priv->cmd_str, sizeof(priv->cmd_str), "%s0%c", command, cat_term); commit 027b7801899e67133c153b3918205bf5464946e1 Author: Mike Black W9MDB <mdb...@ya...> Date: Sun Dec 17 06:19:08 2023 -0600 Fix FT857 set_vfo cppcheck warning as get_vfo is always RIG_OK diff --git a/rigs/yaesu/ft857.c b/rigs/yaesu/ft857.c index 7964cdd93..09dbbe861 100644 --- a/rigs/yaesu/ft857.c +++ b/rigs/yaesu/ft857.c @@ -636,14 +636,7 @@ int ft857_set_vfo(RIG *rig, vfo_t vfo) rig_debug(RIG_DEBUG_VERBOSE, "%s: called \n", __func__); - int retval = ft857_get_vfo(rig, &curvfo); - - if (retval != RIG_OK) - { - rig_debug(RIG_DEBUG_ERR, "%s: error get_vfo '%s'\n", __func__, - rigerror(retval)); - return retval; - } + ft857_get_vfo(rig, &curvfo); // retval is always RIG_OK so ignore it if (curvfo == vfo) { commit ac7c908036533b629ce70269e3d6b992c58ed3f8 Author: Mike Black W9MDB <mdb...@ya...> Date: Sat Dec 16 17:09:11 2023 -0600 Add DSTAR to flrig.c -- thanks to Philip Rose GM3ZZA diff --git a/rigs/dummy/flrig.c b/rigs/dummy/flrig.c index c1b467797..f3c02ea3a 100644 --- a/rigs/dummy/flrig.c +++ b/rigs/dummy/flrig.c @@ -50,7 +50,7 @@ RIG_MODE_PKTLSB | RIG_MODE_PKTUSB |\ RIG_MODE_SSB | RIG_MODE_LSB | RIG_MODE_USB |\ RIG_MODE_FM | RIG_MODE_WFM | RIG_MODE_FMN | RIG_MODE_PKTFM |\ - RIG_MODE_C4FM) + RIG_MODE_C4FM | RIG_MODE_DSTAR) #define FLRIG_LEVELS (RIG_LEVEL_AF | RIG_LEVEL_RF | RIG_LEVEL_MICGAIN | RIG_LEVEL_STRENGTH | RIG_LEVEL_RFPOWER_METER | RIG_LEVEL_RFPOWER_METER_WATTS | RIG_LEVEL_RFPOWER | RIG_LEVEL_SWR) @@ -143,7 +143,7 @@ struct rig_caps flrig_caps = RIG_MODEL(RIG_MODEL_FLRIG), .model_name = "FLRig", .mfg_name = "FLRig", - .version = "20231213.0", + .version = "20231216.0", .copyright = "LGPL", .status = RIG_STATUS_STABLE, .rig_type = RIG_TYPE_TRANSCEIVER, @@ -242,6 +242,7 @@ static struct s_modeMap modeMap[] = {RIG_MODE_RTTY, NULL}, {RIG_MODE_RTTYR, NULL}, {RIG_MODE_C4FM, NULL}, + {RIG_MODE_DSTAR, NULL}, {0, NULL} }; @@ -1092,6 +1093,7 @@ static int flrig_open(RIG *rig) else if (streq(p, "UCW")) { modeMapAdd(&modes, RIG_MODE_CW, p); } else if (streq(p, "C4FM")) { modeMapAdd(&modes, RIG_MODE_C4FM, p); } else if (streq(p, "SPEC")) { modeMapAdd(&modes, RIG_MODE_SPEC, p); } + else if (streq(p, "DV")) { modeMapAdd(&modes, RIG_MODE_DSTAR, p); } else if (streq(p, "DRM")) // we don't support DRM yet (or maybe ever) { rig_debug(RIG_DEBUG_VERBOSE, "%s: no mapping for mode %s\n", __func__, p); commit 50711dcfb8c88729bf50291fd68034b16e485236 Author: Mike Black W9MDB <mdb...@ya...> Date: Sat Dec 16 15:03:21 2023 -0600 Remove dead code in flex6xxx.c diff --git a/rigs/kenwood/flex6xxx.c b/rigs/kenwood/flex6xxx.c index 7e3d3f324..50e21d15b 100644 --- a/rigs/kenwood/flex6xxx.c +++ b/rigs/kenwood/flex6xxx.c @@ -847,8 +847,6 @@ int powersdr_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val) if (ptt == RIG_PTT_OFF) { val->f = priv->swr; return RIG_OK;} cmd = "ZZRM8"; // get SWR - len = 5; - ans = 8; retval = kenwood_transaction(rig, cmd, lvlbuf, sizeof(lvlbuf)); if (retval != RIG_OK) { val->f = priv->swr; return RIG_OK;}; commit 11876d7cc4344d70354c28ad3c763c5999aab69a Author: Mike Black W9MDB <mdb...@ya...> Date: Sat Dec 16 15:02:01 2023 -0600 Fix ptt shadow in flex6xxx.c diff --git a/rigs/kenwood/flex6xxx.c b/rigs/kenwood/flex6xxx.c index a386b6366..7e3d3f324 100644 --- a/rigs/kenwood/flex6xxx.c +++ b/rigs/kenwood/flex6xxx.c @@ -841,7 +841,7 @@ int powersdr_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val) case RIG_LEVEL_SWR: { struct kenwood_priv_caps *priv = kenwood_caps(rig); - ptt_t ptt = 0; + ptt = 0; rig_get_ptt(rig, RIG_VFO_CURR, &ptt); if (ptt == RIG_PTT_OFF) { val->f = priv->swr; return RIG_OK;} @@ -948,7 +948,7 @@ int powersdr_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val) case RIG_LEVEL_RFPOWER_METER_WATTS: { // if not ptt then no power is going out so return 0W - ptt_t ptt; + ptt = 0; rig_get_ptt(rig, RIG_VFO_TX, &ptt); if (!ptt) { val->f = 0; return RIG_OK; } commit e267c6276495bee71eaf217715874cb56bcb4d6b Author: Mike Black W9MDB <mdb...@ya...> Date: Sat Dec 16 11:37:58 2023 -0600 astyle files diff --git a/amplifiers/gemini/gemini.c b/amplifiers/gemini/gemini.c index 6f69b0678..df7ad27f7 100644 --- a/amplifiers/gemini/gemini.c +++ b/amplifiers/gemini/gemini.c @@ -103,8 +103,9 @@ int gemini_transaction(AMP *amp, const char *cmd, char *response, if (response) // if response expected get it { response[0] = 0; - int len = read_string(&rs->ampport, (unsigned char *) response, response_len, "\n", - 1, 0, 1); + int len = read_string(&rs->ampport, (unsigned char *) response, response_len, + "\n", + 1, 0, 1); if (len < 0) { diff --git a/lib/cJSON.c b/lib/cJSON.c index 524ba4641..d9b70dcd0 100644 --- a/lib/cJSON.c +++ b/lib/cJSON.c @@ -85,7 +85,8 @@ #endif #endif -typedef struct { +typedef struct +{ const unsigned char *json; size_t position; } error; @@ -93,10 +94,10 @@ static error global_error = { NULL, 0 }; CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) { - return (const char*) (global_error.json + global_error.position); + return (const char *)(global_error.json + global_error.position); } -CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON *const item) { if (!cJSON_IsString(item)) { @@ -106,7 +107,7 @@ CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) return item->valuestring; } -CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON *const item) { if (!cJSON_IsNumber(item)) { @@ -118,19 +119,21 @@ CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ #if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 15) - #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. +#error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #endif -CJSON_PUBLIC(const char*) cJSON_Version(void) +CJSON_PUBLIC(const char *) cJSON_Version(void) { static char version[15]; - sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, + CJSON_VERSION_PATCH); return version; } /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ -static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) +static int case_insensitive_strcmp(const unsigned char *string1, + const unsigned char *string2) { if ((string1 == NULL) || (string2 == NULL)) { @@ -142,7 +145,7 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned return 0; } - for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + for (; tolower(*string1) == tolower(*string2); (void)string1++, string2++) { if (*string1 == '\0') { @@ -162,7 +165,7 @@ typedef struct internal_hooks #if defined(_MSC_VER) /* work around MSVC error C2322: '...' address of dllimport '...' is not static */ -static void * CJSON_CDECL internal_malloc(size_t size) +static void *CJSON_CDECL internal_malloc(size_t size) { return malloc(size); } @@ -170,7 +173,7 @@ static void CJSON_CDECL internal_free(void *pointer) { free(pointer); } -static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) +static void *CJSON_CDECL internal_realloc(void *pointer, size_t size) { return realloc(pointer, size); } @@ -185,7 +188,8 @@ static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; -static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) +static unsigned char *cJSON_strdup(const unsigned char *string, + const internal_hooks *const hooks) { size_t length = 0; unsigned char *copy = NULL; @@ -195,18 +199,20 @@ static unsigned char* cJSON_strdup(const unsigned char* string, const internal_h return NULL; } - length = strlen((const char*)string) + sizeof(""); - copy = (unsigned char*)hooks->allocate(length); + length = strlen((const char *)string) + sizeof(""); + copy = (unsigned char *)hooks->allocate(length); + if (copy == NULL) { return NULL; } + memcpy(copy, string, length); return copy; } -CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks) { if (hooks == NULL) { @@ -218,12 +224,14 @@ CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) } global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) { global_hooks.allocate = hooks->malloc_fn; } global_hooks.deallocate = free; + if (hooks->free_fn != NULL) { global_hooks.deallocate = hooks->free_fn; @@ -231,6 +239,7 @@ CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) /* use realloc only if both free and malloc are used */ global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) { global_hooks.reallocate = realloc; @@ -238,9 +247,10 @@ CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) } /* Internal constructor. */ -static cJSON *cJSON_New_Item(const internal_hooks * const hooks) +static cJSON *cJSON_New_Item(const internal_hooks *const hooks) { - cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); + cJSON *node = (cJSON *)hooks->allocate(sizeof(cJSON)); + if (node) { memset(node, '\0', sizeof(cJSON)); @@ -253,21 +263,26 @@ static cJSON *cJSON_New_Item(const internal_hooks * const hooks) CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) { cJSON *next = NULL; + while (item != NULL) { next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) { cJSON_Delete(item->child); } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) { global_hooks.deallocate(item->valuestring); } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) { global_hooks.deallocate(item->string); } + global_hooks.deallocate(item); item = next; } @@ -302,7 +317,8 @@ typedef struct #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) /* Parse the input text to generate a number, and populate the result into item. */ -static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) +static cJSON_bool parse_number(cJSON *const item, + parse_buffer *const input_buffer) { double number = 0; unsigned char *after_end = NULL; @@ -318,39 +334,42 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu /* copy the number into a temporary buffer and replace '.' with the decimal point * of the current locale (for strtod) * This also takes care of '\0' not necessarily being available for marking the end of the input */ - for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) + for (i = 0; (i < (sizeof(number_c_string) - 1)) + && can_access_at_index(input_buffer, i); i++) { switch (buffer_at_offset(input_buffer)[i]) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '+': - case '-': - case 'e': - case 'E': - number_c_string[i] = buffer_at_offset(input_buffer)[i]; - break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; - case '.': - number_c_string[i] = decimal_point; - break; + case '.': + number_c_string[i] = decimal_point; + break; - default: - goto loop_end; + default: + goto loop_end; } } + loop_end: number_c_string[i] = '\0'; - number = strtod((const char*)number_c_string, (char**)&after_end); + number = strtod((const char *)number_c_string, (char **)&after_end); + if (number_c_string == after_end) { return false; /* parse_error */ @@ -397,28 +416,35 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) return object->valuedouble = number; } -CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) +CJSON_PUBLIC(char *) cJSON_SetValuestring(cJSON *object, + const char *valuestring) { char *copy = NULL; + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference)) { return NULL; } + if (strlen(valuestring) <= strlen(object->valuestring)) { strcpy(object->valuestring, valuestring); return object->valuestring; } - copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); + + copy = (char *) cJSON_strdup((const unsigned char *)valuestring, &global_hooks); + if (copy == NULL) { return NULL; } + if (object->valuestring != NULL) { cJSON_free(object->valuestring); } + object->valuestring = copy; return copy; @@ -436,7 +462,7 @@ typedef struct } printbuffer; /* realloc printbuffer if necessary to have at least "needed" bytes more */ -static unsigned char* ensure(printbuffer * const p, size_t needed) +static unsigned char *ensure(printbuffer *const p, size_t needed) { unsigned char *newbuffer = NULL; size_t newsize = 0; @@ -459,12 +485,14 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) } needed += p->offset + 1; + if (needed <= p->length) { return p->buffer + p->offset; } - if (p->noalloc) { + if (p->noalloc) + { return NULL; } @@ -489,7 +517,8 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) if (p->hooks.reallocate != NULL) { /* reallocate with realloc if available */ - newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); + newbuffer = (unsigned char *)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) { p->hooks.deallocate(p->buffer); @@ -502,7 +531,8 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) else { /* otherwise reallocate manually */ - newbuffer = (unsigned char*)p->hooks.allocate(newsize); + newbuffer = (unsigned char *)p->hooks.allocate(newsize); + if (!newbuffer) { p->hooks.deallocate(p->buffer); @@ -515,6 +545,7 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) memcpy(newbuffer, p->buffer, p->offset + 1); p->hooks.deallocate(p->buffer); } + p->length = newsize; p->buffer = newbuffer; @@ -522,16 +553,18 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) } /* calculate the new length of the string in a printbuffer and update the offset */ -static void update_offset(printbuffer * const buffer) +static void update_offset(printbuffer *const buffer) { const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) { return; } + buffer_pointer = buffer->buffer + buffer->offset; - buffer->offset += strlen((const char*)buffer_pointer); + buffer->offset += strlen((const char *)buffer_pointer); } /* securely comparison of floating-point variables */ @@ -542,7 +575,8 @@ static cJSON_bool compare_double(double a, double b) } /* Render the number nicely from the given item into a string. */ -static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) +static cJSON_bool print_number(const cJSON *const item, + printbuffer *const output_buffer) { unsigned char *output_pointer = NULL; double d = item->valuedouble; @@ -560,22 +594,23 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out /* This checks for NaN and Infinity */ if (isnan(d) || isinf(d)) { - length = sprintf((char*)number_buffer, "null"); + length = sprintf((char *)number_buffer, "null"); + } + else if (d == (double)item->valueint) + { + length = sprintf((char *)number_buffer, "%d", item->valueint); } - else if(d == (double)item->valueint) - { - length = sprintf((char*)number_buffer, "%d", item->valueint); - } else { /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ - length = sprintf((char*)number_buffer, "%1.15g", d); + length = sprintf((char *)number_buffer, "%1.15g", d); /* Check whether the original double can be recovered */ - if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) + if ((sscanf((char *)number_buffer, "%lg", &test) != 1) + || !compare_double((double)test, d)) { /* If not, print with 17 decimal places of precision */ - length = sprintf((char*)number_buffer, "%1.17g", d); + length = sprintf((char *)number_buffer, "%1.17g", d); } } @@ -587,6 +622,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out /* reserve appropriate space in the output */ output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) { return false; @@ -604,6 +640,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out output_pointer[i] = number_buffer[i]; } + output_pointer[i] = '\0'; output_buffer->offset += (size_t)length; @@ -612,7 +649,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out } /* parse 4 digit hexadecimal number */ -static unsigned parse_hex4(const unsigned char * const input) +static unsigned parse_hex4(const unsigned char *const input) { unsigned int h = 0; size_t i = 0; @@ -649,7 +686,9 @@ static unsigned parse_hex4(const unsigned char * const input) /* converts a UTF-16 literal to UTF-8 * A literal can be one or two sequences of the form \uXXXX */ -static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) +static unsigned char utf16_literal_to_utf8(const unsigned char *const + input_pointer, const unsigned char *const input_end, + unsigned char **output_pointer) { long unsigned int codepoint = 0; unsigned int first_code = 0; @@ -695,6 +734,7 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi /* get the second utf16 sequence */ second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ if ((second_code < 0xDC00) || (second_code > 0xDFFF)) { @@ -745,12 +785,14 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi } /* encode as utf8 */ - for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; + utf8_position--) { /* 10xxxxxx */ (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); codepoint >>= 6; } + /* encode first byte */ if (utf8_length > 1) { @@ -770,7 +812,8 @@ fail: } /* Parse the input text into an unescaped cinput, and populate item. */ -static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) +static cJSON_bool parse_string(cJSON *const item, + parse_buffer *const input_buffer) { const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; @@ -787,7 +830,9 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu /* calculate approximate size of the output (overestimate) */ size_t allocation_length = 0; size_t skipped_bytes = 0; - while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) + + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) + && (*input_end != '\"')) { /* is escape sequence */ if (input_end[0] == '\\') @@ -797,19 +842,26 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu /* prevent buffer overflow when last input character is a backslash */ goto fail; } + skipped_bytes++; input_end++; } + input_end++; } - if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) + + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) + || (*input_end != '\"')) { goto fail; /* string ended unexpectedly */ } /* This is at most how much we need for the output */ - allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; - output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); + allocation_length = (size_t)(input_end - buffer_at_offset( + input_buffer)) - skipped_bytes; + output = (unsigned char *)input_buffer->hooks.allocate(allocation_length + + sizeof("")); + if (output == NULL) { goto fail; /* allocation failure */ @@ -817,6 +869,7 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu } output_pointer = output; + /* loop through the string literal */ while (input_pointer < input_end) { @@ -828,6 +881,7 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu else { unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) { goto fail; @@ -835,40 +889,49 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu switch (input_pointer[1]) { - case 'b': - *output_pointer++ = '\b'; - break; - case 'f': - *output_pointer++ = '\f'; - break; - case 'n': - *output_pointer++ = '\n'; - break; - case 'r': - *output_pointer++ = '\r'; - break; - case 't': - *output_pointer++ = '\t'; - break; - case '\"': - case '\\': - case '/': - *output_pointer++ = input_pointer[1]; - break; - - /* UTF-16 literal */ - case 'u': - sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); - if (sequence_length == 0) - { - /* failed to convert UTF16-literal to UTF-8 */ - goto fail; - } - break; - - default: + case 'b': + *output_pointer++ = '\b'; + break; + + case 'f': + *output_pointer++ = '\f'; + break; + + case 'n': + *output_pointer++ = '\n'; + break; + + case 'r': + *output_pointer++ = '\r'; + break; + + case 't': + *output_pointer++ = '\t'; + break; + + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, + &output_pointer); + + if (sequence_length == 0) + { + /* failed to convert UTF16-literal to UTF-8 */ goto fail; + } + + break; + + default: + goto fail; } + input_pointer += sequence_length; } } @@ -877,14 +940,15 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu *output_pointer = '\0'; item->type = cJSON_String; - item->valuestring = (char*)output; + item->valuestring = (char *)output; - input_buffer->offset = (size_t) (input_end - input_buffer->content); + input_buffer->offset = (size_t)(input_end - input_buffer->content); input_buffer->offset++; return true; fail: + if (output != NULL) { input_buffer->hooks.deallocate(output); @@ -899,7 +963,8 @@ fail: } /* Render the cstring provided to an escaped version that can be printed. */ -static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +static cJSON_bool print_string_ptr(const unsigned char *const input, + printbuffer *const output_buffer) { const unsigned char *input_pointer = NULL; unsigned char *output = NULL; @@ -917,11 +982,13 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe if (input == NULL) { output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) { return false; } - strcpy((char*)output, "\"\""); + + strcpy((char *)output, "\"\""); return true; } @@ -931,28 +998,32 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe { switch (*input_pointer) { - case '\"': - case '\\': - case '\b': - case '\f': - case '\n': - case '\r': - case '\t': - /* one character escape sequence */ - escape_characters++; - break; - default: - if (*input_pointer < 32) - { - /* UTF-16 escape sequence uXXXX */ - escape_characters += 5; - } - break; + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + + default: + if (*input_pointer < 32) + { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + + break; } } + output_length = (size_t)(input_pointer - input) + escape_characters; output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) { return false; @@ -971,10 +1042,13 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe output[0] = '\"'; output_pointer = output + 1; + /* copy the string */ - for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) + for (input_pointer = input; *input_pointer != '\0'; + (void)input_pointer++, output_pointer++) { - if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) + if ((*input_pointer > 31) && (*input_pointer != '\"') + && (*input_pointer != '\\')) { /* normal character, copy */ *output_pointer = *input_pointer; @@ -983,37 +1057,46 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe { /* character needs to be escaped */ *output_pointer++ = '\\'; + switch (*input_pointer) { - case '\\': - *output_pointer = '\\'; - break; - case '\"': - *output_pointer = '\"'; - break; - case '\b': - *output_pointer = 'b'; - break; - case '\f': - *output_pointer = 'f'; - break; - case '\n': - *output_pointer = 'n'; - break; - case '\r': - *output_pointer = 'r'; - break; - case '\t': - *output_pointer = 't'; - break; - default: - /* escape and print as unicode codepoint */ - sprintf((char*)output_pointer, "u%04x", *input_pointer); - output_pointer += 4; - break; + case '\\': + *output_pointer = '\\'; + break; + + case '\"': + *output_pointer = '\"'; + break; + + case '\b': + *output_pointer = 'b'; + break; + + case '\f': + *output_pointer = 'f'; + break; + + case '\n': + *output_pointer = 'n'; + break; + + case '\r': + *output_pointer = 'r'; + break; + + case '\t': + *output_pointer = 't'; + break; + + default: + /* escape and print as unicode codepoint */ + sprintf((char *)output_pointer, "u%04x", *input_pointer); + output_pointer += 4; + break; } } } + output[output_length + 1] = '\"'; output[output_length + 2] = '\0'; @@ -1021,21 +1104,27 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe } /* Invoke print_string_ptr (which is useful) on an item. */ -static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) +static cJSON_bool print_string(const cJSON *const item, printbuffer *const p) { - return print_string_ptr((unsigned char*)item->valuestring, p); + return print_string_ptr((unsigned char *)item->valuestring, p); } /* Predeclare these prototypes. */ -static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); -static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); -static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); -static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); -static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); -static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_value(cJSON *const item, + parse_buffer *const input_buffer); +static cJSON_bool print_value(const cJSON *const item, + printbuffer *const output_buffer); +static cJSON_bool parse_array(cJSON *const item, + parse_buffer *const input_buffer); +static cJSON_bool print_array(const cJSON *const item, + printbuffer *const output_buffer); +static cJSON_bool parse_object(cJSON *const item, + parse_buffer *const input_buffer); +static cJSON_bool print_object(const cJSON *const item, + printbuffer *const output_buffer); /* Utility to jump whitespace and cr/lf */ -static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +static parse_buffer *buffer_skip_whitespace(parse_buffer *const buffer) { if ((buffer == NULL) || (buffer->content == NULL)) { @@ -1049,7 +1138,7 @@ static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) { - buffer->offset++; + buffer->offset++; } if (buffer->offset == buffer->length) @@ -1061,14 +1150,15 @@ static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) } /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ -static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) +static parse_buffer *skip_utf8_bom(parse_buffer *const buffer) { if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) { return NULL; } - if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) + if (can_access_at_index(buffer, 4) + && (strncmp((const char *)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) { buffer->offset += 3; } @@ -1076,7 +1166,8 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) return buffer; } -CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, + const char **return_parse_end, cJSON_bool require_null_terminated) { size_t buffer_length; @@ -1088,11 +1179,14 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return /* Adding null character size due to require_null_terminated. */ buffer_length = strlen(value) + sizeof(""); - return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); + return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, + require_null_terminated); } /* Parse an object - create a new root, and populate. */ -CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, + size_t buffer_length, const char **return_parse_end, + cJSON_bool require_null_terminated) { parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; cJSON *item = NULL; @@ -1106,12 +1200,13 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer goto fail; } - buffer.content = (const unsigned char*)value; + buffer.content = (const unsigned char *)value; buffer.length = buffer_length; buffer.offset = 0; buffer.hooks = global_hooks; item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ { goto fail; @@ -1127,19 +1222,22 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer if (require_null_terminated) { buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') { goto fail; } } + if (return_parse_end) { - *return_parse_end = (const char*)buffer_at_offset(&buffer); + *return_parse_end = (const char *)buffer_at_offset(&buffer); } return item; fail: + if (item != NULL) { cJSON_Delete(item); @@ -1148,7 +1246,7 @@ fail: if (value != NULL) { error local_error; - local_error.json = (const unsigned char*)value; + local_error.json = (const unsigned char *)value; local_error.position = 0; if (buffer.offset < buffer.length) @@ -1162,7 +1260,7 @@ fail: if (return_parse_end != NULL) { - *return_parse_end = (const char*)local_error.json + local_error.position; + *return_parse_end = (const char *)local_error.json + local_error.position; } global_error = local_error; @@ -1177,14 +1275,16 @@ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) return cJSON_ParseWithOpts(value, 0, 0); } -CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, + size_t buffer_length) { return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); } #define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) -static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) +static unsigned char *print(const cJSON *const item, cJSON_bool format, + const internal_hooks *const hooks) { static const size_t default_buffer_size = 256; printbuffer buffer[1]; @@ -1193,10 +1293,11 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i memset(buffer, 0, sizeof(buffer)); /* create buffer */ - buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); + buffer->buffer = (unsigned char *) hooks->allocate(default_buffer_size); buffer->length = default_buffer_size; buffer->format = format; buffer->hooks = *hooks; + if (buffer->buffer == NULL) { goto fail; @@ -1207,24 +1308,31 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i { goto fail; } + update_offset(buffer); /* check if reallocate is available */ if (hooks->reallocate != NULL) { - printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); - if (printed == NULL) { + printed = (unsigned char *) hooks->reallocate(buffer->buffer, + buffer->offset + 1); + + if (printed == NULL) + { goto fail; } + buffer->buffer = NULL; } else /* otherwise copy the JSON over to a new buffer */ { - printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + printed = (unsigned char *) hooks->allocate(buffer->offset + 1); + if (printed == NULL) { goto fail; } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); printed[buffer->offset] = '\0'; /* just to be sure */ @@ -1235,6 +1343,7 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i return printed; fail: + if (buffer->buffer != NULL) { hooks->deallocate(buffer->buffer); @@ -1251,15 +1360,16 @@ fail: /* Render a cJSON item/entity/structure to text. */ CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) { - return (char*)print(item, true, &global_hooks); + return (char *)print(item, true, &global_hooks); } CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) { - return (char*)print(item, false, &global_hooks); + return (char *)print(item, false, &global_hooks); } -CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, + cJSON_bool fmt) { printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; @@ -1268,7 +1378,8 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON return NULL; } - p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); + p.buffer = (unsigned char *)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) { return NULL; @@ -1286,10 +1397,11 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON return NULL; } - return (char*)p.buffer; + return (char *)p.buffer; } -CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, + const int length, const cJSON_bool format) { printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; @@ -1298,7 +1410,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, cons return false; } - p.buffer = (unsigned char*)buffer; + p.buffer = (unsigned char *)buffer; p.length = (size_t)length; p.offset = 0; p.noalloc = true; @@ -1309,7 +1421,8 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, cons } /* Parser core - when encountering text, process appropriately. */ -static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) +static cJSON_bool parse_value(cJSON *const item, + parse_buffer *const input_buffer) { if ((input_buffer == NULL) || (input_buffer->content == NULL)) { @@ -1318,44 +1431,59 @@ static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buf /* parse the different types of values */ /* null */ - if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) + if (can_read(input_buffer, 4) + && (strncmp((const char *)buffer_at_offset(input_buffer), "null", 4) == 0)) { item->type = cJSON_NULL; input_buffer->offset += 4; return true; } + /* false */ - if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) + if (can_read(input_buffer, 5) + ... [truncated message content] |