From 70126cb04f9a0635a4b61aab167d404522bdcbd5 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 25 Nov 2023 16:48:23 -0600 Subject: [PATCH] Eliminated .get() from TempBuffer objects where possible Stylistic only. Cannot remove .get() when a TempBuffer is being re-cast. Also can't remove .get() when cast can't be determined, such as when used in Serial.printf() [potential to do: explore if this can be addressed, though this is stylistic only and has no impact on code] --- src/HAP.cpp | 58 ++++++++++++++++++++++++------------------------ src/HomeSpan.cpp | 22 +++++++++--------- src/HomeSpan.h | 2 +- src/Network.cpp | 4 ++-- src/SRP.cpp | 2 +- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 05ff68e..7391aa2 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -97,10 +97,10 @@ void HAPClient::init(){ if(!nvs_get_blob(hapNVS,"CONTROLLERS",NULL,&len)){ // if found long-term Controller Pairings data from NVS TempBuffer tBuf(len/sizeof(Controller)); - nvs_get_blob(hapNVS,"CONTROLLERS",tBuf.get(),&len); // retrieve data + nvs_get_blob(hapNVS,"CONTROLLERS",tBuf,&len); // retrieve data for(int i=0;i jBuf(nBytes+1); - homeSpan.sprintfAttributes(jBuf.get()); // create JSON database (will need to re-cast to uint8_t* below) + homeSpan.sprintfAttributes(jBuf); // create JSON database (will need to re-cast to uint8_t* below) char *body; asprintf(&body,"HTTP/1.1 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",nBytes); @@ -967,8 +967,8 @@ int HAPClient::postPairingsURL(){ LOG2(client.remoteIP()); LOG2(" >>>>>>>>>>\n"); LOG2(body); - listControllers(tBuf.get()); - sendEncrypted(body,tBuf.get(),tBuf.len()); + listControllers(tBuf); + sendEncrypted(body,tBuf,tBuf.len()); free(body); return(1); @@ -1035,7 +1035,7 @@ int HAPClient::getCharacteristicsURL(char *urlBuf){ char *p2; while(char *t2=strtok_r(t1,",",&p2)){ // parse IDs t1=NULL; - ids.get()[numIDs++]=t2; + ids[numIDs++]=t2; } } } // parse URL @@ -1043,11 +1043,11 @@ int HAPClient::getCharacteristicsURL(char *urlBuf){ if(!numIDs) // could not find any IDs return(0); - int nBytes=homeSpan.sprintfAttributes(ids.get(),numIDs,flags,NULL); // get JSON response - includes terminating null (will be recast to uint8_t* below) + int nBytes=homeSpan.sprintfAttributes(ids,numIDs,flags,NULL); // get JSON response - includes terminating null (will be recast to uint8_t* below) TempBuffer jsonBuf(nBytes+1); - homeSpan.sprintfAttributes(ids.get(),numIDs,flags,jsonBuf.get()); + homeSpan.sprintfAttributes(ids,numIDs,flags,jsonBuf); - boolean sFlag=strstr(jsonBuf.get(),"status"); // status attribute found? + boolean sFlag=strstr(jsonBuf,"status"); // status attribute found? char *body; asprintf(&body,"HTTP/1.1 %s\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",!sFlag?"200 OK":"207 Multi-Status",nBytes); @@ -1108,7 +1108,7 @@ int HAPClient::putCharacteristicsURL(char *json){ int nBytes=homeSpan.sprintfAttributes(pObj,n,NULL); // get JSON response - includes terminating null (will be recast to uint8_t* below) TempBuffer jsonBuf(nBytes+1); - homeSpan.sprintfAttributes(pObj,n,jsonBuf.get()); + homeSpan.sprintfAttributes(pObj,n,jsonBuf); char *body; asprintf(&body,"HTTP/1.1 207 Multi-Status\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",nBytes); @@ -1375,7 +1375,7 @@ void HAPClient::eventNotify(SpanBuf *pObj, int nObj, int ignoreClient){ if(nBytes>0){ // if there are notifications to send to client cNum TempBuffer jsonBuf(nBytes+1); - homeSpan.sprintfNotify(pObj,nObj,jsonBuf.get(),cNum); + homeSpan.sprintfNotify(pObj,nObj,jsonBuf,cNum); char *body; asprintf(&body,"EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",nBytes); @@ -1402,7 +1402,7 @@ void HAPClient::eventNotify(SpanBuf *pObj, int nObj, int ignoreClient){ void HAPClient::tlvRespond(){ TempBuffer tBuf(tlv8.pack(NULL)); // create buffer to hold TLV data - tlv8.pack(tBuf.get()); // pack TLV records into buffer + tlv8.pack(tBuf); // pack TLV records into buffer char *body; asprintf(&body,"HTTP/1.1 200 OK\r\nContent-Type: application/pairing+tlv8\r\nContent-Length: %d\r\n\r\n",tBuf.len()); // create Body with Content Length = size of TLV data @@ -1415,10 +1415,10 @@ void HAPClient::tlvRespond(){ if(!cPair){ // unverified, unencrypted session client.print(body); - client.write(tBuf.get(),tBuf.len()); + client.write(tBuf,tBuf.len()); LOG2("------------ SENT! --------------\n"); } else { - sendEncrypted(body,tBuf.get(),tBuf.len()); + sendEncrypted(body,tBuf,tBuf.len()); } free(body); @@ -1443,12 +1443,12 @@ int HAPClient::receiveEncrypted(uint8_t *httpBuf, int messageSize){ TempBuffer tBuf(n+16); // expected number of total bytes = n bytes in encoded message + 16 bytes for appended authentication tag - if(client.read(tBuf.get(),tBuf.len())!=tBuf.len()){ + if(client.read(tBuf,tBuf.len())!=tBuf.len()){ LOG0("\n\n*** ERROR: Malformed encrypted message frame\n\n"); return(0); } - if(crypto_aead_chacha20poly1305_ietf_decrypt(httpBuf+nBytes, NULL, NULL, tBuf.get(), tBuf.len(), aad, 2, c2aNonce.get(), c2aKey)==-1){ + if(crypto_aead_chacha20poly1305_ietf_decrypt(httpBuf+nBytes, NULL, NULL, tBuf, tBuf.len(), aad, 2, c2aNonce.get(), c2aKey)==-1){ LOG0("\n\n*** ERROR: Can't Decrypt Message\n\n"); return(0); } @@ -1479,12 +1479,12 @@ void HAPClient::sendEncrypted(char *body, uint8_t *dataBuf, int dataLen){ TempBuffer tBuf(2+maxFrameSize+16); // 2-byte AAD + encrytped data + 16-byte authentication tag - tBuf.get()[0]=bodyLen%256; // store number of bytes in first frame that encrypts the Body (AAD bytes) - tBuf.get()[1]=bodyLen/256; + tBuf[0]=bodyLen%256; // store number of bytes in first frame that encrypts the Body (AAD bytes) + tBuf[1]=bodyLen/256; - crypto_aead_chacha20poly1305_ietf_encrypt(tBuf.get()+2,&nBytes,(uint8_t *)body,bodyLen,tBuf.get(),2,NULL,a2cNonce.get(),a2cKey); // encrypt the Body with authentication tag appended + crypto_aead_chacha20poly1305_ietf_encrypt(tBuf+2,&nBytes,(uint8_t *)body,bodyLen,tBuf,2,NULL,a2cNonce.get(),a2cKey); // encrypt the Body with authentication tag appended - client.write(tBuf.get(),nBytes+2); // transmit encrypted frame + client.write(tBuf,nBytes+2); // transmit encrypted frame a2cNonce.inc(); // increment nonce for(int i=0;iFRAME_SIZE) // maximum number of bytes to encrypt=FRAME_SIZE n=FRAME_SIZE; - tBuf.get()[0]=n%256; // store number of bytes that encrypts this frame (AAD bytes) - tBuf.get()[1]=n/256; + tBuf[0]=n%256; // store number of bytes that encrypts this frame (AAD bytes) + tBuf[1]=n/256; - crypto_aead_chacha20poly1305_ietf_encrypt(tBuf.get()+2,&nBytes,dataBuf+i,n,tBuf.get(),2,NULL,a2cNonce.get(),a2cKey); // encrypt the next portion of dataBuf with authentication tag appended + crypto_aead_chacha20poly1305_ietf_encrypt(tBuf+2,&nBytes,dataBuf+i,n,tBuf,2,NULL,a2cNonce.get(),a2cKey); // encrypt the next portion of dataBuf with authentication tag appended - client.write(tBuf.get(),nBytes+2); // transmit encrypted frame + client.write(tBuf,nBytes+2); // transmit encrypted frame a2cNonce.inc(); // increment nonce } @@ -1707,7 +1707,7 @@ void HAPClient::saveControllers(){ TempBuffer tBuf(controllerList.size()); // create temporary buffer to hold Controller data std::copy(controllerList.begin(),controllerList.end(),tBuf.get()); // copy data from linked list to buffer - nvs_set_blob(hapNVS,"CONTROLLERS",tBuf.get(),tBuf.len()); // update data + nvs_set_blob(hapNVS,"CONTROLLERS",tBuf,tBuf.len()); // update data nvs_commit(hapNVS); // commit to NVS } diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index de92994..534d372 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -582,9 +582,9 @@ void Span::processSerialCommand(const char *c){ HAPClient::saveControllers(); break; TempBuffer tBuf(HAPClient::listControllers(NULL)); - HAPClient::listControllers(tBuf.get()); + HAPClient::listControllers(tBuf); Serial.printf("SIZE = %d\n",tBuf.len()); - HAPClient::hexPrintRow(tBuf.get(),tBuf.len()); + HAPClient::hexPrintRow(tBuf,tBuf.len()); break; } @@ -1044,14 +1044,14 @@ void Span::processSerialCommand(const char *c){ TempBuffer tBuf(200); size_t olen; - tBuf.get()[0]='\0'; + tBuf[0]='\0'; LOG0(">>> Accessory data: "); - readSerial(tBuf.get(),199); - if(strlen(tBuf.get())==0){ + readSerial(tBuf,199); + if(strlen(tBuf)==0){ LOG0("(cancelled)\n\n"); return; } - mbedtls_base64_decode((uint8_t *)&HAPClient::accessory,sizeof(struct Accessory),&olen,(uint8_t *)tBuf.get(),strlen(tBuf.get())); + mbedtls_base64_decode((uint8_t *)&HAPClient::accessory,sizeof(struct Accessory),&olen,(uint8_t *)tBuf.get(),strlen(tBuf)); if(olen!=sizeof(struct Accessory)){ LOG0("\n*** Error in size of Accessory data - cloning cancelled. Restarting...\n\n"); reboot(); @@ -1064,14 +1064,14 @@ void Span::processSerialCommand(const char *c){ Controller tCont; while(HAPClient::controllerList.size()<16){ - tBuf.get()[0]='\0'; + tBuf[0]='\0'; LOG0(">>> Controller data: "); - readSerial(tBuf.get(),199); - if(strlen(tBuf.get())==0){ + readSerial(tBuf,199); + if(strlen(tBuf)==0){ LOG0("(done)\n"); break; } else { - mbedtls_base64_decode((uint8_t *)(&tCont),sizeof(struct Controller),&olen,(uint8_t *)tBuf.get(),strlen(tBuf.get())); + mbedtls_base64_decode((uint8_t *)(&tCont),sizeof(struct Controller),&olen,(uint8_t *)tBuf.get(),strlen(tBuf)); if(olen!=sizeof(struct Controller)){ LOG0("\n*** Error in size of Controller data - cloning cancelled. Restarting...\n\n"); reboot(); @@ -1596,7 +1596,7 @@ boolean Span::updateDatabase(boolean updateMDNS){ uint8_t tHash[48]; TempBuffer tBuf(sprintfAttributes(NULL,GET_META|GET_PERMS|GET_TYPE|GET_DESC)+1); - sprintfAttributes(tBuf.get(),GET_META|GET_PERMS|GET_TYPE|GET_DESC); + sprintfAttributes(tBuf,GET_META|GET_PERMS|GET_TYPE|GET_DESC); mbedtls_sha512_ret((uint8_t *)tBuf.get(),tBuf.len(),tHash,1); // create SHA-384 hash of JSON (can be any hash - just looking for a unique key) boolean changed=false; diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 4240f1c..07e790e 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -760,7 +760,7 @@ class SpanCharacteristic{ mbedtls_base64_encode(NULL,0,&olen,data,len); // get length of string buffer needed (mbedtls includes the trailing null in this size) TempBuffer tBuf(olen); // create temporary string buffer, with room for trailing null mbedtls_base64_encode((uint8_t*)tBuf.get(),olen,&olen,data,len ); // encode data into string buf - setString(tBuf.get()); // call setString to continue processing as if characteristic was a string + setString(tBuf); // call setString to continue processing as if characteristic was a string } template void setVal(T val, boolean notify=true){ diff --git a/src/Network.cpp b/src/Network.cpp index 86e4dfd..2997d3f 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -186,7 +186,7 @@ void Network::apConfigure(){ TempBuffer httpBuf(messageSize+1); // leave room for null character added below - int nBytes=client.read(httpBuf.get(),messageSize); // read all available bytes up to maximum allowed+1 + int nBytes=client.read(httpBuf,messageSize); // read all available bytes up to maximum allowed+1 if(nBytes!=messageSize || client.available()!=0){ badRequestError(); @@ -194,7 +194,7 @@ void Network::apConfigure(){ continue; } - httpBuf.get()[nBytes]='\0'; // add null character to enable string functions + httpBuf[nBytes]='\0'; // add null character to enable string functions char *body=(char *)httpBuf.get(); // char pointer to start of HTTP Body char *p; // char pointer used for searches diff --git a/src/SRP.cpp b/src/SRP.cpp index 96dcf45..d38ec12 100644 --- a/src/SRP.cpp +++ b/src/SRP.cpp @@ -275,7 +275,7 @@ void SRP6A::print(mbedtls_mpi *mpi, int minLogLevel){ mbedtls_mpi_write_string(mpi,16,NULL,0,&sLen); TempBuffer sBuf(sLen); - mbedtls_mpi_write_string(mpi,16,sBuf.get(),sLen,&sLen); + mbedtls_mpi_write_string(mpi,16,sBuf,sLen,&sLen); Serial.printf("%d %s\n",(sLen-1)/2,sBuf.get()); // subtract 1 for null-terminator, and then divide by 2 to get number of bytes (e.g. 4F = 2 characters, but represents just one mpi byte) }