diff --git a/README.md b/README.md index cd90bca..340249f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Google OAuth2.0 Access Token generation Arduino Library v1.1.0 +# Google OAuth2.0 Access Token generation Arduino Library v1.1.2 This is the library will create the OAuth2.0 access token used in the Google API's http request (REST). @@ -9,6 +9,8 @@ See [How to Create Service Account Private Key](#how-to-create-service-account-p This library supports ESP8266 and ESP32 MCU from Espressif. +Supports ethernet in ESP32 using LAN8720, TLK110 and IP101 Ethernet modules and ESP8266 using ENC28J60, W5100 and W5500 Ethernet modules. + ## Tested Devices diff --git a/examples/Ethernet/ESP32/ESP32.ino b/examples/Ethernet/ESP32/ESP32.ino new file mode 100644 index 0000000..ff80f18 --- /dev/null +++ b/examples/Ethernet/ESP32/ESP32.ino @@ -0,0 +1,201 @@ + + +/** + * This example show how to get access token using ESP32 and LAN8720 Ethernet module. + * + * Created by K. Suwatchai (Mobizt) + * + * Email: suwatchai@outlook.com + * + * Github: https://github.com/mobizt/ESP-Mail-Client + * + * Copyright (c) 2021 mobizt + * +*/ + +/** + * There are many sources for LAN8720 and ESP32 interconnection on the internet which may + * work for your LAN8720 board. + * + * Some methods worked unless no IP is available. + * + * This modification and interconnection provided in this example are mostly worked as + * the 50 MHz clock was created internally in ESP32 which GPIO 17 is set to be output of this clock + * and feeds to the LAN8720 chip XTAL input. + * + * The on-board LAN8720 50 MHz XTAL chip will be disabled by connect its enable pin or pin 1 to GND. + * + * Please see the images in the folder "modified_LAN8720_board_images" for how to modify the LAN8720 board. + * + * The LAN8720 Ethernet modified board and ESP32 board wiring connection. + * + * ESP32 LAN8720 + * + * GPIO17 - EMAC_CLK_OUT_180 nINT/REFCLK - LAN8720 XTAL1/CLKIN 4k7 Pulldown + * GPIO22 - EMAC_TXD1 TX1 + * GPIO19 - EMAC_TXD0 TX0 + * GPIO21 - EMAC_TX_EN TX_EN + * GPIO26 - EMAC_RXD1 RX1 + * GPIO25 - EMAC_RXD0 RX0 + * GPIO27 - EMAC_RX_DV CRS + * GPIO23 - MDC MDC + * GPIO18 - MDIO MDIO + * GND GND + * 3V3 VCC + * +*/ + +//In case of Gmail, to send the Email via port 465 (SSL), less secure app option should be enabled in the account settings. https://myaccount.google.com/lesssecureapps?pli=1 + +#include + +#include + +/** These credentials are taken from Service Account key file (JSON) + * https://cloud.google.com/iam/docs/service-accounts +*/ +// See https://github.com/mobizt/ESP-Signer#how-to-create-service-account-private-key + +#define PROJECT_ID "The project ID" //Taken from "project_id" key in JSON file. +#define CLIENT_EMAIL "Client Email" //Taken from "client_email" key in JSON file. +const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----\\n-----END PRIVATE KEY-----\n"; //Taken from "private_key" key in JSON file. + +#ifdef ETH_CLK_MODE +#undef ETH_CLK_MODE +#endif +#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT //RMII clock output from GPIO17 + +// Pin# of the enable signal for the external crystal oscillator (-1 to disable) +#define ETH_POWER_PIN -1 + +// Type of the Ethernet PHY (LAN8720 or TLK110) +#define ETH_TYPE ETH_PHY_LAN8720 + +// I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110) +#define ETH_ADDR 1 + +// Pin# of the I²C clock signal for the Ethernet PHY +#define ETH_MDC_PIN 23 + +// Pin# of the I²C IO signal for the Ethernet PHY +#define ETH_MDIO_PIN 18 + +static bool eth_connected = false; + +SignerConfig config; + +bool beginReady = false; + +void tokenStatusCallback(TokenInfo info); + +void WiFiEvent(WiFiEvent_t event) +{ +#if defined(ESP32) + //Do not run any function here to prevent stack overflow or nested interrupt + switch (event) + { + case SYSTEM_EVENT_ETH_START: + Serial.println("ETH Started"); + //set eth hostname here + ETH.setHostname("esp32-ethernet"); + + break; + case SYSTEM_EVENT_ETH_CONNECTED: + Serial.println("ETH Connected"); + break; + case SYSTEM_EVENT_ETH_GOT_IP: + Serial.print("ETH MAC: "); + Serial.print(ETH.macAddress()); + Serial.print(", IPv4: "); + Serial.print(ETH.localIP()); + if (ETH.fullDuplex()) + { + Serial.print(", FULL_DUPLEX"); + } + Serial.print(", "); + Serial.print(ETH.linkSpeed()); + Serial.println("Mbps"); + eth_connected = true; + + break; + case SYSTEM_EVENT_ETH_DISCONNECTED: + Serial.println("ETH Disconnected"); + eth_connected = false; + break; + case SYSTEM_EVENT_ETH_STOP: + Serial.println("ETH Stopped"); + eth_connected = false; + break; + default: + break; + } +#endif +} + +void begin() +{ + beginReady = true; + + /* Assign the sevice account credentials and private key (required) */ + config.service_account.data.client_email = CLIENT_EMAIL; + config.service_account.data.project_id = PROJECT_ID; + config.service_account.data.private_key = PRIVATE_KEY; + + /* Expired period in seconds (optional). Default is 3600 sec.*/ + config.signer.expiredSeconds = 3600; + + /* Seconds to refresh the token before expired (optional). Default is 60 sec.*/ + config.signer.preRefreshSeconds = 60; + + config.token_status_callback = tokenStatusCallback; + + config.signer.tokens.scope = "https://www.googleapis.com/auth/spreadsheets,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/drive.file"; + + Signer.begin(&config); +} + +void setup() +{ + Serial.begin(115200); + Serial.println(); + + WiFi.onEvent(WiFiEvent); + +#if defined(ESP32) + ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE); +#endif +} + +void loop() +{ + if (eth_connected && !beginReady) + { + begin(); + } + + //Check to status and also refresh the access token + bool ready = Signer.tokenReady(); + if (ready) + { + int t = Signer.getExpiredTimestamp() - config.signer.preRefreshSeconds - time(nullptr); + + Serial.print("Remaining seconds to refresh the token, "); + Serial.println(t); + delay(1000); + } +} + +void tokenStatusCallback(TokenInfo info) +{ + if (info.status == esp_signer_token_status_error) + { + Serial.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str()); + Serial.printf("Token error: %s\n", Signer.getTokenError(info).c_str()); + } + else + { + Serial.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str()); + if (info.status == esp_signer_token_status_ready) + Serial.printf("Token: %s\n", Signer.accessToken().c_str()); + } +} diff --git a/examples/Ethernet/ESP32/modified_LAN8720_board_images/lan8720_modified_board.png b/examples/Ethernet/ESP32/modified_LAN8720_board_images/lan8720_modified_board.png new file mode 100644 index 0000000..44d0d79 Binary files /dev/null and b/examples/Ethernet/ESP32/modified_LAN8720_board_images/lan8720_modified_board.png differ diff --git a/examples/Ethernet/ESP32/modified_LAN8720_board_images/lan8720_modified_schematic.png b/examples/Ethernet/ESP32/modified_LAN8720_board_images/lan8720_modified_schematic.png new file mode 100644 index 0000000..9dc3d11 Binary files /dev/null and b/examples/Ethernet/ESP32/modified_LAN8720_board_images/lan8720_modified_schematic.png differ diff --git a/examples/Ethernet/ESP8266/ESP8266.ino b/examples/Ethernet/ESP8266/ESP8266.ino new file mode 100644 index 0000000..ff178e9 --- /dev/null +++ b/examples/Ethernet/ESP8266/ESP8266.ino @@ -0,0 +1,148 @@ + +/** + * Created by K. Suwatchai (Mobizt) + * + * Email: k_suwatchai@hotmail.com + * + * Github: https://github.com/mobizt + * + * Copyright (c) 2021 mobizt + * +*/ + +//This example show how to get access token using ESP8266 and ENC28J60 Ethernet module. + +/** + * + * The ENC28J60 Ethernet module and ESP8266 board, SPI port wiring connection. + * + * ESP8266 (Wemos D1 Mini or NodeMCU) ENC28J60 + * + * GPIO12 (D6) - MISO SO + * GPIO13 (D7) - MOSI SI + * GPIO14 (D5) - SCK SCK + * GPIO16 (D0) - CS CS + * GND GND + * 3V3 VCC + * +*/ +#include + +#include + + +#include +//#include +//#include + +/** These credentials are taken from Service Account key file (JSON) + * https://cloud.google.com/iam/docs/service-accounts +*/ +// See https://github.com/mobizt/ESP-Signer#how-to-create-service-account-private-key + +#define PROJECT_ID "The project ID" //Taken from "project_id" key in JSON file. +#define CLIENT_EMAIL "Client Email" //Taken from "client_email" key in JSON file. +const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----\\n-----END PRIVATE KEY-----\n"; //Taken from "private_key" key in JSON file. + +#define ETH_CS_PIN 16 //D0 + +ENC28J60lwIP eth(ETH_CS_PIN); +//Wiznet5100lwIP eth(ETH_CS_PIN); +//Wiznet5500lwIP eth(ETH_CS_PIN); + +SignerConfig config; + +bool beginReady = false; + +void tokenStatusCallback(TokenInfo info); + +void begin() +{ + beginReady = true; + + /* Assign the sevice account credentials and private key (required) */ + config.service_account.data.client_email = CLIENT_EMAIL; + config.service_account.data.project_id = PROJECT_ID; + config.service_account.data.private_key = PRIVATE_KEY; + + /* Expired period in seconds (optional). Default is 3600 sec.*/ + config.signer.expiredSeconds = 3600; + + /* Seconds to refresh the token before expired (optional). Default is 60 sec.*/ + config.signer.preRefreshSeconds = 60; + + config.token_status_callback = tokenStatusCallback; + + config.signer.tokens.scope = "https://www.googleapis.com/auth/spreadsheets,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/drive.file"; + + Signer.begin(&config); +} + +void setup() +{ + + Serial.begin(115200); + Serial.println(); + +#if defined(ESP8266) + SPI.begin(); + SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz? + SPI.setBitOrder(MSBFIRST); + SPI.setDataMode(SPI_MODE0); + eth.setDefault(); // use ethernet for default route + if (!eth.begin()) + { + Serial.println("ethernet hardware not found ... sleeping"); + while (1) + { + delay(1000); + } + } + else + { + Serial.print("connecting ethernet"); + while (!eth.connected()) + { + Serial.print("."); + delay(1000); + } + } + Serial.println(); + Serial.print("ethernet IP address: "); + Serial.println(eth.localIP()); +#endif +} + +void loop() +{ + if (!beginReady) + { + begin(); + } + + //Check to status and also refresh the access token + bool ready = Signer.tokenReady(); + if (ready) + { + int t = Signer.getExpiredTimestamp() - config.signer.preRefreshSeconds - time(nullptr); + + Serial.print("Remaining seconds to refresh the token, "); + Serial.println(t); + delay(1000); + } +} + +void tokenStatusCallback(TokenInfo info) +{ + if (info.status == esp_signer_token_status_error) + { + Serial.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str()); + Serial.printf("Token error: %s\n", Signer.getTokenError(info).c_str()); + } + else + { + Serial.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str()); + if (info.status == esp_signer_token_status_ready) + Serial.printf("Token: %s\n", Signer.accessToken().c_str()); + } +} diff --git a/library.json b/library.json index ac006c8..1c701b1 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP Signer", - "version": "1.1.0", + "version": "1.1.2", "keywords": "communication, REST, esp32, esp8266, arduino", "description": "The Google OAuth2.0 access token generation for Google REST API's http request.", "repository": { diff --git a/library.properties b/library.properties index 8403762..8b5eae9 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=ESP Signer -version=1.1.0 +version=1.1.2 author=Mobizt diff --git a/src/ESPSigner.cpp b/src/ESPSigner.cpp index 5c6aa8f..c978d90 100644 --- a/src/ESPSigner.cpp +++ b/src/ESPSigner.cpp @@ -1,5 +1,5 @@ /** - * Google's OAuth2.0 Access token Generation class, Signer.h version 1.1.0 + * Google's OAuth2.0 Access token Generation class, Signer.h version 1.1.2 * * This library used RS256 for signing algorithm. * @@ -7,7 +7,7 @@ * * This library supports Espressif ESP8266 and ESP32 * - * Created November 12, 2021 + * Created December 20, 2021 * * This work is a part of Firebase ESP Client library * Copyright (c) 2021 K. Suwatchai (Mobizt) @@ -143,9 +143,10 @@ bool ESP_Signer::parseSAFile() if (parseJsonResponse(esp_signer_pgm_str_17)) { - tmp = (char *)ut->newP(strlen(config->signer.result->to())); + size_t len = strlen(config->signer.result->to()); + tmp = (char *)ut->newP(len); size_t c = 0; - for (size_t i = 0; i < strlen(config->signer.result->to()); i++) + for (size_t i = 0; i < len; i++) { if (config->signer.result->to()[i] == '\\') { @@ -1123,15 +1124,15 @@ bool ESP_Signer::requestTokens() ut->appendP(req, esp_signer_pgm_str_42); ut->appendP(req, esp_signer_pgm_str_43); - ut->appendP(req, esp_signer_pgm_str_63); + ut->appendP(req, esp_signer_pgm_str_4); ut->appendP(req, esp_signer_pgm_str_64); ut->appendP(req, esp_signer_pgm_str_65); req += NUM2S(strlen(config->signer.json->raw())).get(); - ut->appendP(req, esp_signer_pgm_str_63); + ut->appendP(req, esp_signer_pgm_str_4); ut->appendP(req, esp_signer_pgm_str_66); ut->appendP(req, esp_signer_pgm_str_67); - ut->appendP(req, esp_signer_pgm_str_63); - ut->appendP(req, esp_signer_pgm_str_63); + ut->appendP(req, esp_signer_pgm_str_4); + ut->appendP(req, esp_signer_pgm_str_4); req += config->signer.json->raw(); #if defined(ESP32) @@ -1173,6 +1174,12 @@ bool ESP_Signer::requestTokens() error.message = config->signer.result->to(); } + if (error.code != 0 && config->signer.tokens.token_type == esp_signer_token_type_oauth2_access_token) + { + //new jwt needed as it is already cleared + config->signer.step = esp_signer_jwt_generation_step_encode_header_payload; + } + config->signer.tokens.error = error; tokenInfo.status = config->signer.tokens.status; tokenInfo.type = config->signer.tokens.token_type; diff --git a/src/ESPSigner.h b/src/ESPSigner.h index b23546c..65b59d5 100644 --- a/src/ESPSigner.h +++ b/src/ESPSigner.h @@ -1,5 +1,5 @@ /** - * Google's OAuth2.0 Access token Generation class, Signer.h version 1.1.0 + * Google's OAuth2.0 Access token Generation class, Signer.h version 1.1.2 * * This library used RS256 for signing algorithm. * @@ -7,7 +7,7 @@ * * This library supports Espressif ESP8266 and ESP32 * - * Created November 12, 2021 + * Created December 20, 2021 * * This work is a part of Firebase ESP Client library * Copyright (c) 2021 K. Suwatchai (Mobizt) diff --git a/src/SignerCommon.h b/src/SignerCommon.h index 9acff42..2c5602e 100644 --- a/src/SignerCommon.h +++ b/src/SignerCommon.h @@ -1,6 +1,6 @@ /** - * Created April 4, 2021 + * Created December 12, 2021 * * Copyright (c) 2021 K. Suwatchai (Mobizt) * @@ -36,16 +36,16 @@ #include #include -#include "json/FirebaseJson.h" +#include "./json/FirebaseJson.h" #if defined(ESP32) #include -#include "wcs/esp32/ESP_Signer_TCP_Client.h" +#include "./wcs/esp32/ESP_Signer_TCP_Client.h" #elif defined(ESP8266) #include #include #include -#include "wcs/esp8266/ESP_Signer_TCP_Client.h" +#include "./wcs/esp8266/ESP_Signer_TCP_Client.h" #define FS_NO_GLOBALS #endif @@ -302,8 +302,8 @@ typedef struct esp_signer_cfg_t SignerConfig; typedef std::function esp_signer_callback_function_t; -static const char esp_signer_pgm_str_1[] PROGMEM = "true"; -static const char esp_signer_pgm_str_2[] PROGMEM = "double"; +//static const char esp_signer_pgm_str_1[] PROGMEM = "true"; +//static const char esp_signer_pgm_str_2[] PROGMEM = "double"; static const char esp_signer_pgm_str_3[] PROGMEM = "Connection: "; static const char esp_signer_pgm_str_4[] PROGMEM = "\r\n"; static const char esp_signer_pgm_str_5[] PROGMEM = "Content-Type: "; @@ -320,7 +320,7 @@ static const char esp_signer_pgm_str_15[] PROGMEM = "project_id"; static const char esp_signer_pgm_str_16[] PROGMEM = "private_key_id"; static const char esp_signer_pgm_str_17[] PROGMEM = "private_key"; static const char esp_signer_pgm_str_18[] PROGMEM = "client_email"; -static const char esp_signer_pgm_str_19[] PROGMEM = "client_email"; +static const char esp_signer_pgm_str_19[] PROGMEM = "client_id"; static const char esp_signer_pgm_str_20[] PROGMEM = "tokenProcessingTask"; static const char esp_signer_pgm_str_21[] PROGMEM = "system time was not set"; static const char esp_signer_pgm_str_22[] PROGMEM = "RSA private key parsing failed"; @@ -364,7 +364,7 @@ static const char esp_signer_pgm_str_59[] PROGMEM = "urn:ietf:params:oauth:grant static const char esp_signer_pgm_str_60[] PROGMEM = "assertion"; static const char esp_signer_pgm_str_61[] PROGMEM = " HTTP/1.1\r\n"; static const char esp_signer_pgm_str_62[] PROGMEM = "Host: "; -static const char esp_signer_pgm_str_63[] PROGMEM = "\r\n"; +//static const char esp_signer_pgm_str_63[] PROGMEM = "\r\n"; static const char esp_signer_pgm_str_64[] PROGMEM = "User-Agent: ESP\r\n"; static const char esp_signer_pgm_str_65[] PROGMEM = "Content-Length: "; static const char esp_signer_pgm_str_66[] PROGMEM = "Content-Type: "; diff --git a/src/SignerUtils.h b/src/SignerUtils.h index ef60b79..67e31b4 100644 --- a/src/SignerUtils.h +++ b/src/SignerUtils.h @@ -1628,32 +1628,6 @@ class SignerUtils } #endif - MBSTRING getBoundary(size_t len) - { - char *tmp = strP(esp_signer_boundary_table); - char *buf = (char *)newP(len); - if (len) - { - --len; - buf[0] = tmp[0]; - buf[1] = tmp[1]; - for (size_t n = 2; n < len; n++) - { - int key = rand() % (int)(strlen(tmp) - 1); - buf[n] = tmp[key]; - } - buf[len] = '\0'; - } - MBSTRING s = buf; - delP(&buf); - delP(&tmp); - return s; - } - - bool boolVal(const char *v) - { - return strposP(v, esp_signer_pgm_str_1, 0) > -1; - } bool waitIdle(int &httpCode) { @@ -1695,25 +1669,47 @@ class SignerUtils bool status = WiFi.status() == WL_CONNECTED; + if (config) + status |= ethLinkUp(&config->spi_ethernet_module); + if (dataTime > 0) { - if (millis() - dataTime > 30000) - return false; + if (config) + { + if (config->timeout.serverResponse < 1000 || config->timeout.serverResponse > 1000) + config->timeout.serverResponse = 10000; + + if (millis() - dataTime > config->timeout.serverResponse) + return false; + } + else + { + if (millis() - dataTime > 10 * 1000) + return false; + } } if (!status) { - if (config->_int.esp_signer_reconnect_wifi) + if (config) { - if (millis() - config->_int.esp_signer_last_reconnect_millis > config->_int.esp_signer_reconnect_tmo) + if (config->_int.esp_signer_reconnect_wifi) { - WiFi.reconnect(); - config->_int.esp_signer_last_reconnect_millis = millis(); + if (config->timeout.wifiReconnect < 10000 || config->timeout.wifiReconnect > 5 *60 *1000) + config->timeout.wifiReconnect = 10000; + if (millis() - config->_int.esp_signer_last_reconnect_millis > config->timeout.wifiReconnect) + { + WiFi.reconnect(); + config->_int.esp_signer_last_reconnect_millis = millis(); + } } } status = WiFi.status() == WL_CONNECTED; + + if (config) + status |= ethLinkUp(&config->spi_ethernet_module); } return status; diff --git a/src/json/FirebaseJson.cpp b/src/json/FirebaseJson.cpp index 7f6a316..fa2cd84 100644 --- a/src/json/FirebaseJson.cpp +++ b/src/json/FirebaseJson.cpp @@ -1,10 +1,10 @@ /* - * FirebaseJson, version 2.6.1 + * FirebaseJson, version 2.6.3 * * The Easiest Arduino library to parse, create and edit JSON object using a relative path. * - * November 29, 2021 + * December 20, 2021 * * Features * - Using path to access node element in search style e.g. json.get(result,"a/b/c") @@ -59,7 +59,7 @@ FirebaseJsonBase &FirebaseJsonBase::mClear() if (root != NULL) MB_JSON_Delete(root); root = NULL; - clearS(buf); + buf.clear(); errorPos = -1; return *this; } @@ -196,7 +196,7 @@ void FirebaseJsonBase::makeList(const char *str, std::vector &keys, ch MBSTRING s; while (current != -1) { - clearS(s); + s.clear(); substr(s, str, previous, current - previous); trim(s); if (s.length() > 0) @@ -206,7 +206,7 @@ void FirebaseJsonBase::makeList(const char *str, std::vector &keys, ch current = strpos(str, delim, previous); } - clearS(s); + s.clear(); if (previous > 0 && current == -1) substr(s, str, previous, strlen(str) - previous); @@ -216,14 +216,14 @@ void FirebaseJsonBase::makeList(const char *str, std::vector &keys, ch trim(s); if (s.length() > 0) keys.push_back(s); - clearS(s); + s.clear(); } void FirebaseJsonBase::clearList(std::vector &keys) { size_t len = keys.size(); for (size_t i = 0; i < len; i++) - clearS(keys[i]); + keys[i].clear(); for (int i = len - 1; i >= 0; i--) keys.erase(keys.begin() + i); keys.clear(); @@ -366,12 +366,12 @@ size_t FirebaseJsonBase::mIteratorBegin(MB_JSON *parent, std::vector * void FirebaseJsonBase::mIteratorEnd(bool clearBuf) { if (clearBuf) - clearS(buf); - clearS(iterator_data.path); + buf.clear(); + iterator_data.path.clear(); iterator_data.buf_size = 0; iterator_data.buf_offset = 0; iterator_data.result.clear(); - clearList(iterator_data.pathList); + iterator_data.pathList.clear(); iterator_data.depth = -1; iterator_data._depth = 0; iterator_data.searchEnable = false; @@ -490,7 +490,7 @@ void FirebaseJsonBase::collectResult(MB_JSON *e, const char *key, int arrIndex, iterator_data.pathList[iterator_data.pathList.size() - 1] = ar.c_str(); else iterator_data.pathList.push_back(ar.c_str()); - clearS(ar); + ar.clear(); } if (criteria->path.length() > 0) @@ -560,7 +560,7 @@ void FirebaseJsonBase::collectResult(MB_JSON *e, const char *key, int arrIndex, iterator_data.path += path; iterator_data.path += (const char *)FLASH_MCR("\""); - clearS(path); + path.clear(); } } } @@ -691,8 +691,8 @@ bool FirebaseJsonBase::checkKeys(struct fb_js_search_criteria_t *criteria) if (matches) iterator_data.searchKeyDepth = iterator_data.depth; - clearS(sPath); - clearS(cPath); + sPath.clear(); + cPath.clear(); return matches; } @@ -760,7 +760,7 @@ void FirebaseJsonBase::toBuf(fb_json_serialize_mode mode) char *out = mode == fb_json_serialize_mode_pretty ? MB_JSON_Print(root) : MB_JSON_PrintUnformatted(root); if (out) { - storeS(buf, out, false); + buf = out; MB_JSON_free(out); } } @@ -769,13 +769,13 @@ void FirebaseJsonBase::toBuf(fb_json_serialize_mode mode) bool FirebaseJsonBase::mReadClient(Client *client) { //blocking read - clearS(buf); + buf.clear(); if (readClient(client, buf)) { if (root != NULL) MB_JSON_Delete(root); root = parse(buf.c_str()); - clearS(buf); + buf.clear(); return root != NULL; } return false; @@ -789,7 +789,7 @@ bool FirebaseJsonBase::mReadStream(Stream *s, int timeoutMS) if (root != NULL) MB_JSON_Delete(root); root = parse(buf.c_str()); - clearS(buf); + buf.clear(); return root != NULL; } return false; @@ -861,6 +861,8 @@ void FirebaseJsonBase::mGetPath(MBSTRING &path, std::vector paths, int size_t FirebaseJsonBase::mGetSerializedBufferLength(bool prettify) { + if (!root) + return 0; return MB_JSON_SerializedBufferLength(root, prettify); } @@ -1119,7 +1121,7 @@ size_t FirebaseJsonBase::mSearch(MB_JSON *parent, FirebaseJsonData *result, stru { if (result == NULL) { - clearS(buf); + buf.clear(); iterator_data.path += (const char *)FLASH_MCR("]"); buf = iterator_data.path.c_str(); } @@ -1142,7 +1144,7 @@ size_t FirebaseJsonBase::mSearch(MB_JSON *parent, FirebaseJsonData *result, stru { if (result == NULL) { - clearS(buf); + buf.clear(); mGetPath(buf, iterator_data.pathList); } else diff --git a/src/json/FirebaseJson.h b/src/json/FirebaseJson.h index e86ddf5..38bf5ef 100644 --- a/src/json/FirebaseJson.h +++ b/src/json/FirebaseJson.h @@ -1,9 +1,9 @@ /* - * FirebaseJson, version 2.6.1 + * FirebaseJson, version 2.6.3 * * The Easiest Arduino library to parse, create and edit JSON object using a relative path. * - * November 29, 2021 + * December 20, 2021 * * Features * - Using path to access node element in search style e.g. json.get(result,"a/b/c") @@ -500,7 +500,6 @@ namespace FB_JS unsigned long dataTime = 0; }; }; - class PGM2S { public: @@ -561,7 +560,7 @@ class PGM2S ESP.setExternalHeap(); #endif - bool nn = ((p = (void *)malloc(len)) > 0); + bool nn = ((p = (void *)malloc(newLen)) > 0); #if defined(ESP8266_USE_EXTERNAL_HEAP) ESP.resetHeap(); @@ -1360,22 +1359,9 @@ class FirebaseJsonBase delay(0); } - void clearS(MBSTRING &s) - { - s.clear(); - MBSTRING().swap(s); - } - void shrinkS(MBSTRING &s) { -#if defined(ESP32) s.shrink_to_fit(); -#else - MBSTRING t = s; - clearS(s); - s = t; - clearS(t); -#endif } void delP(void *ptr) @@ -1587,35 +1573,6 @@ class FirebaseJsonBase buf[i] = '\0'; } - void storeS(MBSTRING &s, const char *v, bool append) - { - if (!append) - clearS(s); -#if defined(ESP32) - s += v; - s.shrink_to_fit(); -#else - if (!append) - s = v; - else - { - MBSTRING t = s; - t += v; - clearS(s); - s = t; - clearS(t); - } -#endif - } - - void storeS(MBSTRING &s, char v, bool append) - { - MBSTRING t; - t += v; - storeS(s, t.c_str(), append); - clearS(t); - } - inline int ishex(int x) { return (x >= '0' && x <= '9') || @@ -2068,7 +2025,7 @@ class FirebaseJsonBase if (headerEnded) { - clearS(buf); + buf.clear(); //parse header string to get the header field isHeader = false; parseRespHeader(header, response); @@ -2157,7 +2114,7 @@ class FirebaseJsonBase void clearSerialData(struct FB_JS::serial_data_t &data) { - clearS(data.buf); + data.buf.clear(); data.start = -1; data.end = -1; data.pos = -1; @@ -2208,7 +2165,7 @@ class FirebaseJsonBase bool ret = false; if (data.end > data.start) { - storeS(buf, data.buf.c_str(), false); + buf = data.buf.c_str(); ret = true; } diff --git a/src/json/MB_JSON/MB_JSON.c b/src/json/MB_JSON/MB_JSON.c index df9ae9b..3a334a0 100644 --- a/src/json/MB_JSON/MB_JSON.c +++ b/src/json/MB_JSON/MB_JSON.c @@ -1,7 +1,9 @@ /* - MB_JSON.c v1.0.0 based on the modified version of cJSON.c v1.7.14 (Sept 3, 2020) + MB_JSON.c v1.0.1 based on the modified version of cJSON.c v1.7.14 (Sept 3, 2020) + + All original static cJSON functions and static variables will be prefixed with MB_JSON_. - Created August 15, 2021 + Created December 20, 2021 Copyright (c) 2021 Mobizt (K. Suwatchai) @@ -291,7 +293,7 @@ MB_JSON_Delete(MB_JSON *item) } /* get the decimal point character of the current locale */ -static unsigned char get_decimal_point(void) +static unsigned char MB_JSON_get_decimal_point(void) { #ifdef ENABLE_LOCALES struct lconv *lconv = localeconv(); @@ -308,23 +310,23 @@ typedef struct size_t offset; size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ MB_JSON_internal_hooks hooks; -} parse_buffer; +} MB_JSON_parse_buffer; /* check if the given size is left to read in a given parse buffer (starting with 1) */ -#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +#define MB_JSON_can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) /* check if the buffer can be accessed at the given index (starting with 0) */ -#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) -#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +#define MB_JSON_can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define MB_JSON_cannot_access_at_index(buffer, index) (!MB_JSON_can_access_at_index(buffer, index)) /* get a pointer to the buffer at the position */ -#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) +#define MB_JSON_buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) /* Parse the input text to generate a number, and populate the result into item. */ -static MB_JSON_bool parse_number(MB_JSON *const item, parse_buffer *const input_buffer) +static MB_JSON_bool MB_JSON_parse_number(MB_JSON *const item, MB_JSON_parse_buffer *const input_buffer) { double number = 0; unsigned char *after_end = NULL; unsigned char number_c_string[64]; - unsigned char decimal_point = get_decimal_point(); + unsigned char decimal_point = MB_JSON_get_decimal_point(); size_t i = 0; if ((input_buffer == NULL) || (input_buffer->content == NULL)) @@ -335,9 +337,9 @@ static MB_JSON_bool parse_number(MB_JSON *const item, parse_buffer *const input_ /* 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)) && MB_JSON_can_access_at_index(input_buffer, i); i++) { - switch (buffer_at_offset(input_buffer)[i]) + switch (MB_JSON_buffer_at_offset(input_buffer)[i]) { case '0': case '1': @@ -353,7 +355,7 @@ static MB_JSON_bool parse_number(MB_JSON *const item, parse_buffer *const input_ case '-': case 'e': case 'E': - number_c_string[i] = buffer_at_offset(input_buffer)[i]; + number_c_string[i] = MB_JSON_buffer_at_offset(input_buffer)[i]; break; case '.': @@ -452,17 +454,17 @@ typedef struct MB_JSON_bool noalloc; MB_JSON_bool format; /* is this print a formatted print */ MB_JSON_internal_hooks hooks; -} printbuffer; +} MB_JSON_printbuffer; typedef struct { size_t size; size_t depth; MB_JSON_bool format; -} buffer_len_data_t; +} MB_JSON_buffer_len_data_t; -/* realloc printbuffer if necessary to have at least "needed" bytes more */ -static unsigned char *ensure(printbuffer *const p, size_t needed) +/* realloc MB_JSON_printbuffer if necessary to have at least "needed" bytes more */ +static unsigned char *MB_JSON_ensure(MB_JSON_printbuffer *const p, size_t needed) { unsigned char *newbuffer = NULL; size_t newsize = 0; @@ -548,8 +550,8 @@ static unsigned char *ensure(printbuffer *const p, size_t needed) return newbuffer + p->offset; } -/* calculate the new length of the string in a printbuffer and update the offset */ -static void update_offset(printbuffer *const buffer) +/* calculate the new length of the string in a MB_JSON_printbuffer and update the offset */ +static void MB_JSON_update_offset(MB_JSON_printbuffer *const buffer) { const unsigned char *buffer_pointer = NULL; if ((buffer == NULL) || (buffer->buffer == NULL)) @@ -562,21 +564,21 @@ static void update_offset(printbuffer *const buffer) } /* securely comparison of floating-point variables */ -static MB_JSON_bool compare_double(double a, double b) +static MB_JSON_bool MB_JSON_compare_double(double a, double b) { double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); return (fabs(a - b) <= maxVal * DBL_EPSILON); } /* Render the number nicely from the given item into a string. */ -static MB_JSON_bool print_number(const MB_JSON *const item, printbuffer *const output_buffer) +static MB_JSON_bool MB_JSON_print_number(const MB_JSON *const item, MB_JSON_printbuffer *const output_buffer) { unsigned char *output_pointer = NULL; double d = item->valuedouble; int length = 0; size_t i = 0; unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ - unsigned char decimal_point = get_decimal_point(); + unsigned char decimal_point = MB_JSON_get_decimal_point(); double test = 0.0; if (output_buffer == NULL) @@ -595,7 +597,7 @@ static MB_JSON_bool print_number(const MB_JSON *const item, printbuffer *const o 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) || !MB_JSON_compare_double((double)test, d)) { /* If not, print with 17 decimal places of precision */ length = sprintf((char *)number_buffer, "%1.17g", d); @@ -609,7 +611,7 @@ static MB_JSON_bool print_number(const MB_JSON *const item, printbuffer *const o } /* reserve appropriate space in the output */ - output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + output_pointer = MB_JSON_ensure(output_buffer, (size_t)length + sizeof("")); if (output_pointer == NULL) { return false; @@ -635,7 +637,7 @@ static MB_JSON_bool print_number(const MB_JSON *const item, printbuffer *const o } /* parse 4 digit hexadecimal number */ -static unsigned parse_hex4(const unsigned char *const input) +static unsigned MB_JSON_parse_hex4(const unsigned char *const input) { unsigned int h = 0; size_t i = 0; @@ -672,7 +674,7 @@ 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 MB_JSON_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; @@ -689,7 +691,7 @@ static unsigned char utf16_literal_to_utf8(const unsigned char *const input_poin } /* get the first utf16 sequence */ - first_code = parse_hex4(first_sequence + 2); + first_code = MB_JSON_parse_hex4(first_sequence + 2); /* check that the code is valid */ if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) @@ -717,7 +719,7 @@ static unsigned char utf16_literal_to_utf8(const unsigned char *const input_poin } /* get the second utf16 sequence */ - second_code = parse_hex4(second_sequence + 2); + second_code = MB_JSON_parse_hex4(second_sequence + 2); /* check that the code is valid */ if ((second_code < 0xDC00) || (second_code > 0xDFFF)) { @@ -792,15 +794,15 @@ static unsigned char utf16_literal_to_utf8(const unsigned char *const input_poin } /* Parse the input text into an unescaped cinput, and populate item. */ -static MB_JSON_bool parse_string(MB_JSON *const item, parse_buffer *const input_buffer) +static MB_JSON_bool MB_JSON_parse_string(MB_JSON *const item, MB_JSON_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; + const unsigned char *input_pointer = MB_JSON_buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = MB_JSON_buffer_at_offset(input_buffer) + 1; unsigned char *output_pointer = NULL; unsigned char *output = NULL; /* not a string */ - if (buffer_at_offset(input_buffer)[0] != '\"') + if (MB_JSON_buffer_at_offset(input_buffer)[0] != '\"') { goto fail; } @@ -830,7 +832,7 @@ static MB_JSON_bool parse_string(MB_JSON *const item, parse_buffer *const input_ } /* This is at most how much we need for the output */ - allocation_length = (size_t)(input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + allocation_length = (size_t)(input_end - MB_JSON_buffer_at_offset(input_buffer)) - skipped_bytes; output = (unsigned char *)input_buffer->hooks.allocate(allocation_length + sizeof("")); if (output == NULL) { @@ -880,7 +882,7 @@ static MB_JSON_bool parse_string(MB_JSON *const item, parse_buffer *const input_ /* UTF-16 literal */ case 'u': - sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + sequence_length = MB_JSON_utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); if (sequence_length == 0) { /* failed to convert UTF16-literal to UTF-8 */ @@ -920,7 +922,7 @@ static MB_JSON_bool parse_string(MB_JSON *const item, parse_buffer *const input_ return false; } -static MB_JSON_bool get_string_buffer_length_ptr(const unsigned char *const input, buffer_len_data_t *const buf_len) +static MB_JSON_bool MB_JSON_get_string_buffer_length_ptr(const unsigned char *const input, MB_JSON_buffer_len_data_t *const buf_len) { const unsigned char *input_pointer = NULL; size_t output_length = 0; @@ -966,7 +968,7 @@ static MB_JSON_bool get_string_buffer_length_ptr(const unsigned char *const inpu } /* Render the cstring provided to an escaped version that can be printed. */ -static MB_JSON_bool print_string_ptr(const unsigned char *const input, printbuffer *const output_buffer) +static MB_JSON_bool MB_JSON_print_string_ptr(const unsigned char *const input, MB_JSON_printbuffer *const output_buffer) { const unsigned char *input_pointer = NULL; unsigned char *output = NULL; @@ -983,7 +985,7 @@ static MB_JSON_bool print_string_ptr(const unsigned char *const input, printbuff /* empty string */ if (input == NULL) { - output = ensure(output_buffer, sizeof("\"\"")); + output = MB_JSON_ensure(output_buffer, sizeof("\"\"")); if (output == NULL) { return false; @@ -1019,7 +1021,7 @@ static MB_JSON_bool print_string_ptr(const unsigned char *const input, printbuff } output_length = (size_t)(input_pointer - input) + escape_characters; - output = ensure(output_buffer, output_length + sizeof("\"\"")); + output = MB_JSON_ensure(output_buffer, output_length + sizeof("\"\"")); if (output == NULL) { return false; @@ -1087,43 +1089,43 @@ static MB_JSON_bool print_string_ptr(const unsigned char *const input, printbuff return true; } -static MB_JSON_bool get_string_buffer_length(const MB_JSON *const item, buffer_len_data_t *const buf_len) +static MB_JSON_bool MB_JSON_get_string_buffer_length(const MB_JSON *const item, MB_JSON_buffer_len_data_t *const buf_len) { - return get_string_buffer_length_ptr((unsigned char *)item->valuestring, buf_len); + return MB_JSON_get_string_buffer_length_ptr((unsigned char *)item->valuestring, buf_len); } /* Invoke print_string_ptr (which is useful) on an item. */ -static MB_JSON_bool print_string(const MB_JSON *const item, printbuffer *const p) +static MB_JSON_bool MB_JSON_print_string(const MB_JSON *const item, MB_JSON_printbuffer *const p) { - return print_string_ptr((unsigned char *)item->valuestring, p); + return MB_JSON_print_string_ptr((unsigned char *)item->valuestring, p); } /* Predeclare these prototypes. */ -static MB_JSON_bool parse_value(MB_JSON *const item, parse_buffer *const input_buffer); -static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const output_buffer); -static MB_JSON_bool parse_array(MB_JSON *const item, parse_buffer *const input_buffer); -static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const output_buffer); -static MB_JSON_bool parse_object(MB_JSON *const item, parse_buffer *const input_buffer); -static MB_JSON_bool print_object(const MB_JSON *const item, printbuffer *const output_buffer); +static MB_JSON_bool MB_JSON_parse_value(MB_JSON *const item, MB_JSON_parse_buffer *const input_buffer); +static MB_JSON_bool MB_JSON_print_value(const MB_JSON *const item, MB_JSON_printbuffer *const output_buffer); +static MB_JSON_bool MB_JSON_parse_array(MB_JSON *const item, MB_JSON_parse_buffer *const input_buffer); +static MB_JSON_bool MB_JSON_print_array(const MB_JSON *const item, MB_JSON_printbuffer *const output_buffer); +static MB_JSON_bool MB_JSON_parse_object(MB_JSON *const item, MB_JSON_parse_buffer *const input_buffer); +static MB_JSON_bool MB_JSON_print_object(const MB_JSON *const item, MB_JSON_printbuffer *const output_buffer); -static MB_JSON_bool get_object_buffer_length(const MB_JSON *const item, buffer_len_data_t *const buf_len); -static MB_JSON_bool get_array_buffer_length(const MB_JSON *const item, buffer_len_data_t *const buf_len); -static MB_JSON_bool get_value_buffer_length(const MB_JSON *const item, buffer_len_data_t *const buf_len); +static MB_JSON_bool MB_JSON_get_object_buffer_length(const MB_JSON *const item, MB_JSON_buffer_len_data_t *const buf_len); +static MB_JSON_bool MB_JSON_get_array_buffer_length(const MB_JSON *const item, MB_JSON_buffer_len_data_t *const buf_len); +static MB_JSON_bool MB_JSON_get_value_buffer_length(const MB_JSON *const item, MB_JSON_buffer_len_data_t *const buf_len); /* Utility to jump whitespace and cr/lf */ -static parse_buffer *buffer_skip_whitespace(parse_buffer *const buffer) +static MB_JSON_parse_buffer *MB_JSON_buffer_skip_whitespace(MB_JSON_parse_buffer *const buffer) { if ((buffer == NULL) || (buffer->content == NULL)) { return NULL; } - if (cannot_access_at_index(buffer, 0)) + if (MB_JSON_cannot_access_at_index(buffer, 0)) { return buffer; } - while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) + while (MB_JSON_can_access_at_index(buffer, 0) && (MB_JSON_buffer_at_offset(buffer)[0] <= 32)) { buffer->offset++; } @@ -1137,14 +1139,14 @@ 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 MB_JSON_parse_buffer *MB_JSON_skip_utf8_bom(MB_JSON_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 (MB_JSON_can_access_at_index(buffer, 4) && (strncmp((const char *)MB_JSON_buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) { buffer->offset += 3; } @@ -1172,7 +1174,7 @@ MB_JSON_ParseWithOpts(const char *value, const char **return_parse_end, MB_JSON_ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, MB_JSON_bool require_null_terminated) { - parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0}}; + MB_JSON_parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0}}; MB_JSON *item = NULL; /* reset error position */ @@ -1195,7 +1197,7 @@ MB_JSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char goto fail; } - if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) + if (!MB_JSON_parse_value(item, MB_JSON_buffer_skip_whitespace(MB_JSON_skip_utf8_bom(&buffer)))) { /* parse failure. ep is set. */ goto fail; @@ -1204,15 +1206,15 @@ MB_JSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ if (require_null_terminated) { - buffer_skip_whitespace(&buffer); - if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') + MB_JSON_buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || MB_JSON_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 *)MB_JSON_buffer_at_offset(&buffer); } return item; @@ -1266,19 +1268,19 @@ MB_JSON_ParseWithLength(const char *value, size_t buffer_length) size_t MB_JSON_SerializedBufferLength(const MB_JSON *const item, MB_JSON_bool format) { - buffer_len_data_t buf_len[1]; + MB_JSON_buffer_len_data_t buf_len[1]; buf_len->depth = 0; buf_len->format = format > 0 ? true : false; buf_len->size = 0; - get_value_buffer_length(item, buf_len); + MB_JSON_get_value_buffer_length(item, buf_len); return buf_len->size; } -static unsigned char *print(const MB_JSON *const item, MB_JSON_bool format, const MB_JSON_internal_hooks *const hooks) +static unsigned char *MB_JSON_print(const MB_JSON *const item, MB_JSON_bool format, const MB_JSON_internal_hooks *const hooks) { static const size_t default_buffer_size = 256; - printbuffer buffer[1]; + MB_JSON_printbuffer buffer[1]; unsigned char *printed = NULL; memset(buffer, 0, sizeof(buffer)); @@ -1294,11 +1296,11 @@ static unsigned char *print(const MB_JSON *const item, MB_JSON_bool format, cons } /* print the value */ - if (!print_value(item, buffer)) + if (!MB_JSON_print_value(item, buffer)) { goto fail; } - update_offset(buffer); + MB_JSON_update_offset(buffer); /* check if reallocate is available */ if (hooks->reallocate != NULL) @@ -1344,19 +1346,19 @@ static unsigned char *print(const MB_JSON *const item, MB_JSON_bool format, cons MB_JSON_PUBLIC(char *) MB_JSON_Print(const MB_JSON *item) { - return (char *)print(item, true, &MB_JSON_global_hooks); + return (char *)MB_JSON_print(item, true, &MB_JSON_global_hooks); } MB_JSON_PUBLIC(char *) MB_JSON_PrintUnformatted(const MB_JSON *item) { - return (char *)print(item, false, &MB_JSON_global_hooks); + return (char *)MB_JSON_print(item, false, &MB_JSON_global_hooks); } MB_JSON_PUBLIC(char *) MB_JSON_PrintBuffered(const MB_JSON *item, int prebuffer, MB_JSON_bool fmt) { - printbuffer p = {0, 0, 0, 0, 0, 0, {0, 0, 0}}; + MB_JSON_printbuffer p = {0, 0, 0, 0, 0, 0, {0, 0, 0}}; if (prebuffer < 0) { @@ -1375,7 +1377,7 @@ MB_JSON_PrintBuffered(const MB_JSON *item, int prebuffer, MB_JSON_bool fmt) p.format = fmt; p.hooks = MB_JSON_global_hooks; - if (!print_value(item, &p)) + if (!MB_JSON_print_value(item, &p)) { MB_JSON_global_hooks.deallocate(p.buffer); return NULL; @@ -1387,7 +1389,7 @@ MB_JSON_PrintBuffered(const MB_JSON *item, int prebuffer, MB_JSON_bool fmt) MB_JSON_PUBLIC(MB_JSON_bool) MB_JSON_PrintPreallocated(MB_JSON *item, char *buffer, const int length, const MB_JSON_bool format) { - printbuffer p = {0, 0, 0, 0, 0, 0, {0, 0, 0}}; + MB_JSON_printbuffer p = {0, 0, 0, 0, 0, 0, {0, 0, 0}}; if ((length < 0) || (buffer == NULL)) { @@ -1401,11 +1403,11 @@ MB_JSON_PrintPreallocated(MB_JSON *item, char *buffer, const int length, const M p.format = format; p.hooks = MB_JSON_global_hooks; - return print_value(item, &p); + return MB_JSON_print_value(item, &p); } /* Parser core - when encountering text, process appropriately. */ -static MB_JSON_bool parse_value(MB_JSON *const item, parse_buffer *const input_buffer) +static MB_JSON_bool MB_JSON_parse_value(MB_JSON *const item, MB_JSON_parse_buffer *const input_buffer) { if ((input_buffer == NULL) || (input_buffer->content == NULL)) { @@ -1414,21 +1416,21 @@ static MB_JSON_bool parse_value(MB_JSON *const item, parse_buffer *const input_b /* 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 (MB_JSON_can_read(input_buffer, 4) && (strncmp((const char *)MB_JSON_buffer_at_offset(input_buffer), "null", 4) == 0)) { item->type = MB_JSON_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 (MB_JSON_can_read(input_buffer, 5) && (strncmp((const char *)MB_JSON_buffer_at_offset(input_buffer), "false", 5) == 0)) { item->type = MB_JSON_False; input_buffer->offset += 5; return true; } /* true */ - if (can_read(input_buffer, 4) && (strncmp((const char *)buffer_at_offset(input_buffer), "true", 4) == 0)) + if (MB_JSON_can_read(input_buffer, 4) && (strncmp((const char *)MB_JSON_buffer_at_offset(input_buffer), "true", 4) == 0)) { item->type = MB_JSON_True; item->valueint = 1; @@ -1436,30 +1438,30 @@ static MB_JSON_bool parse_value(MB_JSON *const item, parse_buffer *const input_b return true; } /* string */ - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) + if (MB_JSON_can_access_at_index(input_buffer, 0) && (MB_JSON_buffer_at_offset(input_buffer)[0] == '\"')) { - return parse_string(item, input_buffer); + return MB_JSON_parse_string(item, input_buffer); } /* number */ - if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) + if (MB_JSON_can_access_at_index(input_buffer, 0) && ((MB_JSON_buffer_at_offset(input_buffer)[0] == '-') || ((MB_JSON_buffer_at_offset(input_buffer)[0] >= '0') && (MB_JSON_buffer_at_offset(input_buffer)[0] <= '9')))) { - return parse_number(item, input_buffer); + return MB_JSON_parse_number(item, input_buffer); } /* array */ - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) + if (MB_JSON_can_access_at_index(input_buffer, 0) && (MB_JSON_buffer_at_offset(input_buffer)[0] == '[')) { - return parse_array(item, input_buffer); + return MB_JSON_parse_array(item, input_buffer); } /* object */ - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) + if (MB_JSON_can_access_at_index(input_buffer, 0) && (MB_JSON_buffer_at_offset(input_buffer)[0] == '{')) { - return parse_object(item, input_buffer); + return MB_JSON_parse_object(item, input_buffer); } return false; } -static MB_JSON_bool get_value_buffer_length(const MB_JSON *const item, buffer_len_data_t *const buf_len) +static MB_JSON_bool MB_JSON_get_value_buffer_length(const MB_JSON *const item, MB_JSON_buffer_len_data_t *const buf_len) { switch ((item->type) & 0xFF) @@ -1489,13 +1491,13 @@ static MB_JSON_bool get_value_buffer_length(const MB_JSON *const item, buffer_le } case MB_JSON_String: - return get_string_buffer_length(item, buf_len); + return MB_JSON_get_string_buffer_length(item, buf_len); case MB_JSON_Array: - return get_array_buffer_length(item, buf_len); + return MB_JSON_get_array_buffer_length(item, buf_len); case MB_JSON_Object: - return get_object_buffer_length(item, buf_len); + return MB_JSON_get_object_buffer_length(item, buf_len); default: return false; @@ -1503,7 +1505,7 @@ static MB_JSON_bool get_value_buffer_length(const MB_JSON *const item, buffer_le } /* Render a value to text. */ -static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const output_buffer) +static MB_JSON_bool MB_JSON_print_value(const MB_JSON *const item, MB_JSON_printbuffer *const output_buffer) { unsigned char *output = NULL; @@ -1515,7 +1517,7 @@ static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const ou switch ((item->type) & 0xFF) { case MB_JSON_NULL: - output = ensure(output_buffer, 5); + output = MB_JSON_ensure(output_buffer, 5); if (output == NULL) { return false; @@ -1524,7 +1526,7 @@ static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const ou return true; case MB_JSON_False: - output = ensure(output_buffer, 6); + output = MB_JSON_ensure(output_buffer, 6); if (output == NULL) { return false; @@ -1533,7 +1535,7 @@ static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const ou return true; case MB_JSON_True: - output = ensure(output_buffer, 5); + output = MB_JSON_ensure(output_buffer, 5); if (output == NULL) { return false; @@ -1542,7 +1544,7 @@ static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const ou return true; case MB_JSON_Number: - return print_number(item, output_buffer); + return MB_JSON_print_number(item, output_buffer); case MB_JSON_Raw: { @@ -1553,7 +1555,7 @@ static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const ou } raw_length = strlen(item->valuestring) + sizeof(""); - output = ensure(output_buffer, raw_length); + output = MB_JSON_ensure(output_buffer, raw_length); if (output == NULL) { return false; @@ -1563,13 +1565,13 @@ static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const ou } case MB_JSON_String: - return print_string(item, output_buffer); + return MB_JSON_print_string(item, output_buffer); case MB_JSON_Array: - return print_array(item, output_buffer); + return MB_JSON_print_array(item, output_buffer); case MB_JSON_Object: - return print_object(item, output_buffer); + return MB_JSON_print_object(item, output_buffer); default: return false; @@ -1577,7 +1579,7 @@ static MB_JSON_bool print_value(const MB_JSON *const item, printbuffer *const ou } /* Build an array from input text. */ -static MB_JSON_bool parse_array(MB_JSON *const item, parse_buffer *const input_buffer) +static MB_JSON_bool MB_JSON_parse_array(MB_JSON *const item, MB_JSON_parse_buffer *const input_buffer) { MB_JSON *head = NULL; /* head of the linked list */ MB_JSON *current_item = NULL; @@ -1588,22 +1590,22 @@ static MB_JSON_bool parse_array(MB_JSON *const item, parse_buffer *const input_b } input_buffer->depth++; - if (buffer_at_offset(input_buffer)[0] != '[') + if (MB_JSON_buffer_at_offset(input_buffer)[0] != '[') { /* not an array */ goto fail; } input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) + MB_JSON_buffer_skip_whitespace(input_buffer); + if (MB_JSON_can_access_at_index(input_buffer, 0) && (MB_JSON_buffer_at_offset(input_buffer)[0] == ']')) { /* empty array */ goto success; } /* check if we skipped to the end of the buffer */ - if (cannot_access_at_index(input_buffer, 0)) + if (MB_JSON_cannot_access_at_index(input_buffer, 0)) { input_buffer->offset--; goto fail; @@ -1637,15 +1639,15 @@ static MB_JSON_bool parse_array(MB_JSON *const item, parse_buffer *const input_b /* parse next value */ input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_value(current_item, input_buffer)) + MB_JSON_buffer_skip_whitespace(input_buffer); + if (!MB_JSON_parse_value(current_item, input_buffer)) { goto fail; /* failed to parse value */ } - buffer_skip_whitespace(input_buffer); - } while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + MB_JSON_buffer_skip_whitespace(input_buffer); + } while (MB_JSON_can_access_at_index(input_buffer, 0) && (MB_JSON_buffer_at_offset(input_buffer)[0] == ',')); - if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') + if (MB_JSON_cannot_access_at_index(input_buffer, 0) || MB_JSON_buffer_at_offset(input_buffer)[0] != ']') { goto fail; /* expected end of array */ } @@ -1674,7 +1676,7 @@ static MB_JSON_bool parse_array(MB_JSON *const item, parse_buffer *const input_b return false; } -MB_JSON_bool get_array_buffer_length(const MB_JSON *const item, buffer_len_data_t *const buf_len) +MB_JSON_bool MB_JSON_get_array_buffer_length(const MB_JSON *const item, MB_JSON_buffer_len_data_t *const buf_len) { MB_JSON *current_element = item->child; @@ -1694,7 +1696,7 @@ MB_JSON_bool get_array_buffer_length(const MB_JSON *const item, buffer_len_data_ while (current_element != NULL) { - if (!get_value_buffer_length(current_element, buf_len)) + if (!MB_JSON_get_value_buffer_length(current_element, buf_len)) return false; if (current_element->next) @@ -1729,7 +1731,7 @@ MB_JSON_bool get_array_buffer_length(const MB_JSON *const item, buffer_len_data_ } /* Render an array to text */ -static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const output_buffer) +static MB_JSON_bool MB_JSON_print_array(const MB_JSON *const item, MB_JSON_printbuffer *const output_buffer) { unsigned char *output_pointer = NULL; size_t length = 0; @@ -1742,7 +1744,7 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou /* Compose the output array. */ /* opening square bracket */ - output_pointer = ensure(output_buffer, 1); + output_pointer = MB_JSON_ensure(output_buffer, 1); if (output_pointer == NULL) { return false; @@ -1755,7 +1757,7 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou //add newline and tab after [ if (output_buffer->format) { - output_pointer = ensure(output_buffer, 2); + output_pointer = MB_JSON_ensure(output_buffer, 2); if (output_pointer == NULL) { return false; @@ -1764,7 +1766,7 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou output_buffer->offset += 1; size_t i; - output_pointer = ensure(output_buffer, output_buffer->depth); + output_pointer = MB_JSON_ensure(output_buffer, output_buffer->depth); if (output_pointer == NULL) { return false; @@ -1778,15 +1780,15 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou while (current_element != NULL) { - if (!print_value(current_element, output_buffer)) + if (!MB_JSON_print_value(current_element, output_buffer)) { return false; } - update_offset(output_buffer); + MB_JSON_update_offset(output_buffer); if (current_element->next) { length = (size_t)(output_buffer->format ? 2 : 1); - output_pointer = ensure(output_buffer, length + 1); + output_pointer = MB_JSON_ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; @@ -1802,7 +1804,7 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou if (output_buffer->format) { size_t i; - output_pointer = ensure(output_buffer, output_buffer->depth + 1); + output_pointer = MB_JSON_ensure(output_buffer, output_buffer->depth + 1); if (output_pointer == NULL) { return false; @@ -1819,7 +1821,7 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou current_element = current_element->next; } - output_pointer = ensure(output_buffer, 2); + output_pointer = MB_JSON_ensure(output_buffer, 2); if (output_pointer == NULL) { return false; @@ -1829,7 +1831,7 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou //add newline and tab before ] if (output_buffer->format) { - output_pointer = ensure(output_buffer, 2); + output_pointer = MB_JSON_ensure(output_buffer, 2); if (output_pointer == NULL) { return false; @@ -1838,7 +1840,7 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou output_buffer->offset += 1; size_t i; - output_pointer = ensure(output_buffer, output_buffer->depth + 1); + output_pointer = MB_JSON_ensure(output_buffer, output_buffer->depth + 1); if (output_pointer == NULL) { return false; @@ -1857,7 +1859,7 @@ static MB_JSON_bool print_array(const MB_JSON *const item, printbuffer *const ou } /* Build an object from the text. */ -static MB_JSON_bool parse_object(MB_JSON *const item, parse_buffer *const input_buffer) +static MB_JSON_bool MB_JSON_parse_object(MB_JSON *const item, MB_JSON_parse_buffer *const input_buffer) { MB_JSON *head = NULL; /* linked list head */ MB_JSON *current_item = NULL; @@ -1868,20 +1870,20 @@ static MB_JSON_bool parse_object(MB_JSON *const item, parse_buffer *const input_ } input_buffer->depth++; - if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) + if (MB_JSON_cannot_access_at_index(input_buffer, 0) || (MB_JSON_buffer_at_offset(input_buffer)[0] != '{')) { goto fail; /* not an object */ } input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) + MB_JSON_buffer_skip_whitespace(input_buffer); + if (MB_JSON_can_access_at_index(input_buffer, 0) && (MB_JSON_buffer_at_offset(input_buffer)[0] == '}')) { goto success; /* empty object */ } /* check if we skipped to the end of the buffer */ - if (cannot_access_at_index(input_buffer, 0)) + if (MB_JSON_cannot_access_at_index(input_buffer, 0)) { input_buffer->offset--; goto fail; @@ -1915,33 +1917,33 @@ static MB_JSON_bool parse_object(MB_JSON *const item, parse_buffer *const input_ /* parse the name of the child */ input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_string(current_item, input_buffer)) + MB_JSON_buffer_skip_whitespace(input_buffer); + if (!MB_JSON_parse_string(current_item, input_buffer)) { goto fail; /* failed to parse name */ } - buffer_skip_whitespace(input_buffer); + MB_JSON_buffer_skip_whitespace(input_buffer); /* swap valuestring and string, because we parsed the name */ current_item->string = current_item->valuestring; current_item->valuestring = NULL; - if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) + if (MB_JSON_cannot_access_at_index(input_buffer, 0) || (MB_JSON_buffer_at_offset(input_buffer)[0] != ':')) { goto fail; /* invalid object */ } /* parse the value */ input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_value(current_item, input_buffer)) + MB_JSON_buffer_skip_whitespace(input_buffer); + if (!MB_JSON_parse_value(current_item, input_buffer)) { goto fail; /* failed to parse value */ } - buffer_skip_whitespace(input_buffer); - } while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + MB_JSON_buffer_skip_whitespace(input_buffer); + } while (MB_JSON_can_access_at_index(input_buffer, 0) && (MB_JSON_buffer_at_offset(input_buffer)[0] == ',')); - if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) + if (MB_JSON_cannot_access_at_index(input_buffer, 0) || (MB_JSON_buffer_at_offset(input_buffer)[0] != '}')) { goto fail; /* expected end of object */ } @@ -1969,7 +1971,7 @@ static MB_JSON_bool parse_object(MB_JSON *const item, parse_buffer *const input_ return false; } -static MB_JSON_bool get_object_buffer_length(const MB_JSON *const item, buffer_len_data_t *const buf_len) +static MB_JSON_bool MB_JSON_get_object_buffer_length(const MB_JSON *const item, MB_JSON_buffer_len_data_t *const buf_len) { size_t length = 0; MB_JSON *current_item = item->child; @@ -1991,7 +1993,7 @@ static MB_JSON_bool get_object_buffer_length(const MB_JSON *const item, buffer_l buf_len->size += buf_len->depth; /* get key length */ - if (!get_string_buffer_length_ptr((unsigned char *)current_item->string, buf_len)) + if (!MB_JSON_get_string_buffer_length_ptr((unsigned char *)current_item->string, buf_len)) return false; length = (size_t)(buf_len->format ? 2 : 1); @@ -1999,7 +2001,7 @@ static MB_JSON_bool get_object_buffer_length(const MB_JSON *const item, buffer_l buf_len->size += length; /* get value length */ - if (!get_value_buffer_length(current_item, buf_len)) + if (!MB_JSON_get_value_buffer_length(current_item, buf_len)) return false; /* add comma length if not last */ @@ -2024,7 +2026,7 @@ static MB_JSON_bool get_object_buffer_length(const MB_JSON *const item, buffer_l } /* Render an object to text. */ -static MB_JSON_bool print_object(const MB_JSON *const item, printbuffer *const output_buffer) +static MB_JSON_bool MB_JSON_print_object(const MB_JSON *const item, MB_JSON_printbuffer *const output_buffer) { unsigned char *output_pointer = NULL; size_t length = 0; @@ -2037,7 +2039,7 @@ static MB_JSON_bool print_object(const MB_JSON *const item, printbuffer *const o /* Compose the output: */ length = (size_t)(output_buffer->format && current_item != NULL ? 2 : 1); /* fmt: {\n */ - output_pointer = ensure(output_buffer, length + 1); + output_pointer = MB_JSON_ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; @@ -2060,7 +2062,7 @@ static MB_JSON_bool print_object(const MB_JSON *const item, printbuffer *const o if (output_buffer->format) { size_t i; - output_pointer = ensure(output_buffer, output_buffer->depth); + output_pointer = MB_JSON_ensure(output_buffer, output_buffer->depth); if (output_pointer == NULL) { return false; @@ -2073,14 +2075,14 @@ static MB_JSON_bool print_object(const MB_JSON *const item, printbuffer *const o } /* print key */ - if (!print_string_ptr((unsigned char *)current_item->string, output_buffer)) + if (!MB_JSON_print_string_ptr((unsigned char *)current_item->string, output_buffer)) { return false; } - update_offset(output_buffer); + MB_JSON_update_offset(output_buffer); length = (size_t)(output_buffer->format ? 2 : 1); - output_pointer = ensure(output_buffer, length); + output_pointer = MB_JSON_ensure(output_buffer, length); if (output_pointer == NULL) { return false; @@ -2093,15 +2095,15 @@ static MB_JSON_bool print_object(const MB_JSON *const item, printbuffer *const o output_buffer->offset += length; /* print value */ - if (!print_value(current_item, output_buffer)) + if (!MB_JSON_print_value(current_item, output_buffer)) { return false; } - update_offset(output_buffer); + MB_JSON_update_offset(output_buffer); /* print comma if not last */ length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); - output_pointer = ensure(output_buffer, length + 1); + output_pointer = MB_JSON_ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; @@ -2121,7 +2123,7 @@ static MB_JSON_bool print_object(const MB_JSON *const item, printbuffer *const o current_item = current_item->next; } - output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + output_pointer = MB_JSON_ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); if (output_pointer == NULL) { return false; @@ -2169,7 +2171,7 @@ MB_JSON_GetArraySize(const MB_JSON *array) return (int)size; } -static MB_JSON *get_array_item(const MB_JSON *array, size_t index) +static MB_JSON *MB_JSON_get_array_item(const MB_JSON *array, size_t index) { MB_JSON *current_child = NULL; @@ -2196,10 +2198,10 @@ MB_JSON_GetArrayItem(const MB_JSON *array, int index) return NULL; } - return get_array_item(array, (size_t)index); + return MB_JSON_get_array_item(array, (size_t)index); } -static MB_JSON *get_object_item(const MB_JSON *const object, const char *const name, const MB_JSON_bool case_sensitive) +static MB_JSON *MB_JSON_get_object_item(const MB_JSON *const object, const char *const name, const MB_JSON_bool case_sensitive) { MB_JSON *current_element = NULL; @@ -2235,13 +2237,13 @@ static MB_JSON *get_object_item(const MB_JSON *const object, const char *const n MB_JSON_PUBLIC(MB_JSON *) MB_JSON_GetObjectItem(const MB_JSON *const object, const char *const string) { - return get_object_item(object, string, false); + return MB_JSON_get_object_item(object, string, false); } MB_JSON_PUBLIC(MB_JSON *) MB_JSON_GetObjectItemCaseSensitive(const MB_JSON *const object, const char *const string) { - return get_object_item(object, string, true); + return MB_JSON_get_object_item(object, string, true); } MB_JSON_PUBLIC(MB_JSON_bool) @@ -2251,14 +2253,14 @@ MB_JSON_HasObjectItem(const MB_JSON *object, const char *string) } /* Utility for array list handling. */ -static void suffix_object(MB_JSON *prev, MB_JSON *item) +static void MB_JSON_suffix_object(MB_JSON *prev, MB_JSON *item) { prev->next = item; item->prev = prev; } /* Utility for handling references. */ -static MB_JSON *create_reference(const MB_JSON *item, const MB_JSON_internal_hooks *const hooks) +static MB_JSON *MB_JSON_create_reference(const MB_JSON *item, const MB_JSON_internal_hooks *const hooks) { MB_JSON *reference = NULL; if (item == NULL) @@ -2279,7 +2281,7 @@ static MB_JSON *create_reference(const MB_JSON *item, const MB_JSON_internal_hoo return reference; } -static MB_JSON_bool add_item_to_array(MB_JSON *array, MB_JSON *item) +static MB_JSON_bool MB_JSON_add_item_to_array(MB_JSON *array, MB_JSON *item) { MB_JSON *child = NULL; @@ -2304,7 +2306,7 @@ static MB_JSON_bool add_item_to_array(MB_JSON *array, MB_JSON *item) /* append to the end */ if (child->prev) { - suffix_object(child->prev, item); + MB_JSON_suffix_object(child->prev, item); array->child->prev = item; } } @@ -2316,7 +2318,7 @@ static MB_JSON_bool add_item_to_array(MB_JSON *array, MB_JSON *item) MB_JSON_PUBLIC(MB_JSON_bool) MB_JSON_AddItemToArray(MB_JSON *array, MB_JSON *item) { - return add_item_to_array(array, item); + return MB_JSON_add_item_to_array(array, item); } #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) @@ -2334,7 +2336,7 @@ static void *cast_away_const(const void *string) #pragma GCC diagnostic pop #endif -static MB_JSON_bool add_item_to_object(MB_JSON *const object, const char *const string, MB_JSON *const item, const MB_JSON_internal_hooks *const hooks, const MB_JSON_bool constant_key) +static MB_JSON_bool MB_JSON_add_item_to_object(MB_JSON *const object, const char *const string, MB_JSON *const item, const MB_JSON_internal_hooks *const hooks, const MB_JSON_bool constant_key) { char *new_key = NULL; int new_type = MB_JSON_Invalid; @@ -2368,20 +2370,20 @@ static MB_JSON_bool add_item_to_object(MB_JSON *const object, const char *const item->string = new_key; item->type = new_type; - return add_item_to_array(object, item); + return MB_JSON_add_item_to_array(object, item); } MB_JSON_PUBLIC(MB_JSON_bool) MB_JSON_AddItemToObject(MB_JSON *object, const char *string, MB_JSON *item) { - return add_item_to_object(object, string, item, &MB_JSON_global_hooks, false); + return MB_JSON_add_item_to_object(object, string, item, &MB_JSON_global_hooks, false); } /* Add an item to an object with constant string as key */ MB_JSON_PUBLIC(MB_JSON_bool) MB_JSON_AddItemToObjectCS(MB_JSON *object, const char *string, MB_JSON *item) { - return add_item_to_object(object, string, item, &MB_JSON_global_hooks, true); + return MB_JSON_add_item_to_object(object, string, item, &MB_JSON_global_hooks, true); } MB_JSON_PUBLIC(MB_JSON_bool) @@ -2392,7 +2394,7 @@ MB_JSON_AddItemReferenceToArray(MB_JSON *array, MB_JSON *item) return false; } - return add_item_to_array(array, create_reference(item, &MB_JSON_global_hooks)); + return MB_JSON_add_item_to_array(array, MB_JSON_create_reference(item, &MB_JSON_global_hooks)); } MB_JSON_PUBLIC(MB_JSON_bool) @@ -2403,14 +2405,14 @@ MB_JSON_AddItemReferenceToObject(MB_JSON *object, const char *string, MB_JSON *i return false; } - return add_item_to_object(object, string, create_reference(item, &MB_JSON_global_hooks), &MB_JSON_global_hooks, false); + return MB_JSON_add_item_to_object(object, string, MB_JSON_create_reference(item, &MB_JSON_global_hooks), &MB_JSON_global_hooks, false); } MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddNullToObject(MB_JSON *const object, const char *const name) { MB_JSON *null = MB_JSON_CreateNull(); - if (add_item_to_object(object, name, null, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, null, &MB_JSON_global_hooks, false)) { return null; } @@ -2423,7 +2425,7 @@ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddTrueToObject(MB_JSON *const object, const char *const name) { MB_JSON *true_item = MB_JSON_CreateTrue(); - if (add_item_to_object(object, name, true_item, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, true_item, &MB_JSON_global_hooks, false)) { return true_item; } @@ -2436,7 +2438,7 @@ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddFalseToObject(MB_JSON *const object, const char *const name) { MB_JSON *false_item = MB_JSON_CreateFalse(); - if (add_item_to_object(object, name, false_item, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, false_item, &MB_JSON_global_hooks, false)) { return false_item; } @@ -2449,7 +2451,7 @@ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddBoolToObject(MB_JSON *const object, const char *const name, const MB_JSON_bool boolean) { MB_JSON *bool_item = MB_JSON_CreateBool(boolean); - if (add_item_to_object(object, name, bool_item, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, bool_item, &MB_JSON_global_hooks, false)) { return bool_item; } @@ -2462,7 +2464,7 @@ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddNumberToObject(MB_JSON *const object, const char *const name, const double number) { MB_JSON *number_item = MB_JSON_CreateNumber(number); - if (add_item_to_object(object, name, number_item, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, number_item, &MB_JSON_global_hooks, false)) { return number_item; } @@ -2475,7 +2477,7 @@ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddStringToObject(MB_JSON *const object, const char *const name, const char *const string) { MB_JSON *string_item = MB_JSON_CreateString(string); - if (add_item_to_object(object, name, string_item, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, string_item, &MB_JSON_global_hooks, false)) { return string_item; } @@ -2488,7 +2490,7 @@ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddRawToObject(MB_JSON *const object, const char *const name, const char *const raw) { MB_JSON *raw_item = MB_JSON_CreateRaw(raw); - if (add_item_to_object(object, name, raw_item, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, raw_item, &MB_JSON_global_hooks, false)) { return raw_item; } @@ -2501,7 +2503,7 @@ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddObjectToObject(MB_JSON *const object, const char *const name) { MB_JSON *object_item = MB_JSON_CreateObject(); - if (add_item_to_object(object, name, object_item, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, object_item, &MB_JSON_global_hooks, false)) { return object_item; } @@ -2514,7 +2516,7 @@ MB_JSON_PUBLIC(MB_JSON *) MB_JSON_AddArrayToObject(MB_JSON *const object, const char *const name) { MB_JSON *array = MB_JSON_CreateArray(); - if (add_item_to_object(object, name, array, &MB_JSON_global_hooks, false)) + if (MB_JSON_add_item_to_object(object, name, array, &MB_JSON_global_hooks, false)) { return array; } @@ -2568,7 +2570,7 @@ MB_JSON_DetachItemFromArray(MB_JSON *array, int which) return NULL; } - return MB_JSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); + return MB_JSON_DetachItemViaPointer(array, MB_JSON_get_array_item(array, (size_t)which)); } MB_JSON_PUBLIC(void) @@ -2616,10 +2618,10 @@ MB_JSON_InsertItemInArray(MB_JSON *array, int which, MB_JSON *newitem) return false; } - after_inserted = get_array_item(array, (size_t)which); + after_inserted = MB_JSON_get_array_item(array, (size_t)which); if (after_inserted == NULL) { - return add_item_to_array(array, newitem); + return MB_JSON_add_item_to_array(array, newitem); } newitem->next = after_inserted; @@ -2694,10 +2696,10 @@ MB_JSON_ReplaceItemInArray(MB_JSON *array, int which, MB_JSON *newitem) return false; } - return MB_JSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); + return MB_JSON_ReplaceItemViaPointer(array, MB_JSON_get_array_item(array, (size_t)which), newitem); } -static MB_JSON_bool replace_item_in_object(MB_JSON *object, const char *string, MB_JSON *replacement, MB_JSON_bool case_sensitive) +static MB_JSON_bool MB_JSON_replace_item_in_object(MB_JSON *object, const char *string, MB_JSON *replacement, MB_JSON_bool case_sensitive) { if ((replacement == NULL) || (string == NULL)) { @@ -2712,19 +2714,19 @@ static MB_JSON_bool replace_item_in_object(MB_JSON *object, const char *string, replacement->string = (char *)MB_JSON_strdup((const unsigned char *)string, &MB_JSON_global_hooks); replacement->type &= ~MB_JSON_StringIsConst; - return MB_JSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); + return MB_JSON_ReplaceItemViaPointer(object, MB_JSON_get_object_item(object, string, case_sensitive), replacement); } MB_JSON_PUBLIC(MB_JSON_bool) MB_JSON_ReplaceItemInObject(MB_JSON *object, const char *string, MB_JSON *newitem) { - return replace_item_in_object(object, string, newitem, false); + return MB_JSON_replace_item_in_object(object, string, newitem, false); } MB_JSON_PUBLIC(MB_JSON_bool) MB_JSON_ReplaceItemInObjectCaseSensitive(MB_JSON *object, const char *string, MB_JSON *newitem) { - return replace_item_in_object(object, string, newitem, true); + return MB_JSON_replace_item_in_object(object, string, newitem, true); } /* Create basic types: */ @@ -2932,7 +2934,7 @@ MB_JSON_CreateIntArray(const int *numbers, int count) } else { - suffix_object(p, n); + MB_JSON_suffix_object(p, n); } p = n; } @@ -2974,7 +2976,7 @@ MB_JSON_CreateFloatArray(const float *numbers, int count) } else { - suffix_object(p, n); + MB_JSON_suffix_object(p, n); } p = n; } @@ -3016,7 +3018,7 @@ MB_JSON_CreateDoubleArray(const double *numbers, int count) } else { - suffix_object(p, n); + MB_JSON_suffix_object(p, n); } p = n; } @@ -3058,7 +3060,7 @@ MB_JSON_CreateStringArray(const char *const *strings, int count) } else { - suffix_object(p, n); + MB_JSON_suffix_object(p, n); } p = n; } @@ -3156,7 +3158,7 @@ MB_JSON_Duplicate(const MB_JSON *item, MB_JSON_bool recurse) return NULL; } -static void skip_oneline_comment(char **input) +static void MB_JSON_skip_oneline_comment(char **input) { *input += MB_JSON_static_strlen("//"); @@ -3170,7 +3172,7 @@ static void skip_oneline_comment(char **input) } } -static void skip_multiline_comment(char **input) +static void MB_JSON_skip_multiline_comment(char **input) { *input += MB_JSON_static_strlen("/*"); @@ -3184,7 +3186,7 @@ static void skip_multiline_comment(char **input) } } -static void minify_string(char **input, char **output) +static void MB_JSON_minify_string(char **input, char **output) { (*output)[0] = (*input)[0]; *input += MB_JSON_static_strlen("\""); @@ -3234,11 +3236,11 @@ MB_JSON_Minify(char *json) case '/': if (json[1] == '/') { - skip_oneline_comment(&json); + MB_JSON_skip_oneline_comment(&json); } else if (json[1] == '*') { - skip_multiline_comment(&json); + MB_JSON_skip_multiline_comment(&json); } else { @@ -3247,7 +3249,7 @@ MB_JSON_Minify(char *json) break; case '\"': - minify_string(&json, (char **)&into); + MB_JSON_minify_string(&json, (char **)&into); break; default: @@ -3410,7 +3412,7 @@ MB_JSON_Compare(const MB_JSON *const a, const MB_JSON *const b, const MB_JSON_bo return true; case MB_JSON_Number: - if (compare_double(a->valuedouble, b->valuedouble)) + if (MB_JSON_compare_double(a->valuedouble, b->valuedouble)) { return true; } @@ -3461,7 +3463,7 @@ MB_JSON_Compare(const MB_JSON *const a, const MB_JSON *const b, const MB_JSON_bo MB_JSON_ArrayForEach(a_element, a) { /* TODO This has O(n^2) runtime, which is horrible! */ - b_element = get_object_item(b, a_element->string, case_sensitive); + b_element = MB_JSON_get_object_item(b, a_element->string, case_sensitive); if (b_element == NULL) { return false; @@ -3477,7 +3479,7 @@ MB_JSON_Compare(const MB_JSON *const a, const MB_JSON *const b, const MB_JSON_bo * TODO: Do this the proper way, this is just a fix for now */ MB_JSON_ArrayForEach(b_element, b) { - a_element = get_object_item(a, b_element->string, case_sensitive); + a_element = MB_JSON_get_object_item(a, b_element->string, case_sensitive); if (a_element == NULL) { return false; diff --git a/src/json/MB_JSON/MB_JSON.h b/src/json/MB_JSON/MB_JSON.h index 1776b8b..637db89 100644 --- a/src/json/MB_JSON/MB_JSON.h +++ b/src/json/MB_JSON/MB_JSON.h @@ -1,7 +1,9 @@ /* - MB_JSON.h v1.0.0 based on the modified version of cJSON.h v1.7.14 (Sept 3, 2020) + MB_JSON.h v1.0.1 based on the modified version of cJSON.h v1.7.14 (Sept 3, 2020) + + All original static cJSON functions and static variables will be prefixed with MB_JSON_. - Created August 15, 2021 + Created December 20, 2021 Copyright (c) 2021 Mobizt (K. Suwatchai) diff --git a/src/wcs/esp32/ESP_Signer_TCP_Client.h b/src/wcs/esp32/ESP_Signer_TCP_Client.h index 41d06ad..846d0e7 100644 --- a/src/wcs/esp32/ESP_Signer_TCP_Client.h +++ b/src/wcs/esp32/ESP_Signer_TCP_Client.h @@ -39,14 +39,13 @@ #include #include #include -#include "FS_Config.h" +#include "../../FS_Config.h" #include #if defined(ESP_SIGNER_USE_PSRAM) #define FIREBASEJSON_USE_PSRAM #endif -#include "./json/FirebaseJson.h" - +#include "../../json/FirebaseJson.h" #if __has_include() #include @@ -62,7 +61,7 @@ #endif #define FORMAT_FLASH FORMAT_FLASH_IF_MOUNT_FAILED -#include "wcs/HTTPCode.h" +#include "../HTTPCode.h" static const char esp_idf_branch_str[] PROGMEM = "release/v"; diff --git a/src/wcs/esp8266/ESP_Signer_TCP_Client.h b/src/wcs/esp8266/ESP_Signer_TCP_Client.h index 7029091..241add2 100644 --- a/src/wcs/esp8266/ESP_Signer_TCP_Client.h +++ b/src/wcs/esp8266/ESP_Signer_TCP_Client.h @@ -60,7 +60,7 @@ #define FS_NO_GLOBALS #include -#include "FS_Config.h" +#include "../../FS_Config.h" #if defined DEFAULT_FLASH_FS #define FLASH_FS DEFAULT_FLASH_FS @@ -74,7 +74,7 @@ #define FIREBASEJSON_USE_PSRAM #endif -#include "./json/FirebaseJson.h" +#include "../../json/FirebaseJson.h" #if defined __has_include @@ -102,7 +102,7 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#include "wcs/HTTPCode.h" +#include "../HTTPCode.h" struct esp_signer_sd_config_info_t {