repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
teddycloud
github_2023
toniebox-reverse-engineering
c
httpReadHeaderField
error_t httpReadHeaderField(HttpConnection *connection, char_t *buffer, size_t size, char_t *firstChar) { error_t error; size_t n; size_t length; //This is the actual length of the header field length = 0; //The process of moving from a multiple-line representation of a header //field to its single line representation is called unfolding do { //Check the length of the header field if((length + 1) >= size) { //Report an error error = ERROR_INVALID_REQUEST; //Exit immediately break; } //NULL character found? if(*firstChar == '\0') { //Prepare to decode the first header field length = 0; } //LWSP character found? else if(*firstChar == ' ' || *firstChar == '\t') { //Unfolding is accomplished by regarding CRLF immediately //followed by a LWSP as equivalent to the LWSP character buffer[length] = *firstChar; //The current header field spans multiple lines length++; } //Any other character? else { //Restore the very first character of the header field buffer[0] = *firstChar; //Prepare to decode a new header field length = 1; } //Read data until a CLRF character is encountered error = httpReceive(connection, buffer + length, size - 1 - length, &n, SOCKET_FLAG_BREAK_CRLF); //Any error to report? if(error) break; //Update the length of the header field length += n; //Properly terminate the string with a NULL character buffer[length] = '\0'; //An empty line indicates the end of the header fields if(!osStrcmp(buffer, "\r\n")) break; //Read the next character to detect if the CRLF is immediately //followed by a LWSP character error = httpReceive(connection, firstChar, sizeof(char_t), &n, SOCKET_FLAG_WAIT_ALL); //Any error to report? if(error) break; //LWSP character found? if(*firstChar == ' ' || *firstChar == '\t') { //CRLF immediately followed by LWSP as equivalent to the LWSP character if(length >= 2) { if(buffer[length - 2] == '\r' || buffer[length - 1] == '\n') { //Remove trailing CRLF sequence length -= 2; //Properly terminate the string with a NULL character buffer[length] = '\0'; } } } //A header field may span multiple lines... } while(*firstChar == ' ' || *firstChar == '\t'); //Return status code return error; }
/** * @brief Read multiple-line header field * @param[in] connection Structure representing an HTTP connection * @param[out] buffer Buffer where to store the header field * @param[in] size Size of the buffer, in bytes * @param[in,out] firstChar Leading character of the header line * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L305-L397
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpParseHeaderField
void httpParseHeaderField(HttpConnection *connection, const char_t *name, char_t *value) { //Host header field? if(!osStrcasecmp(name, "Host")) { //Save host name strSafeCopy(connection->request.host, value, HTTP_SERVER_HOST_MAX_LEN); } //Connection header field? else if(!osStrcasecmp(name, "Connection")) { //Parse Connection header field httpParseConnectionField(connection, value); } //Transfer-Encoding header field? else if(!osStrcasecmp(name, "Transfer-Encoding")) { //Check whether chunked encoding is used if(!osStrcasecmp(value, "chunked")) connection->request.chunkedEncoding = TRUE; } //Content-Type field header? else if(!osStrcasecmp(name, "Content-Type")) { //Parse Content-Type header field httpParseContentTypeField(connection, value); } //Content-Length header field? else if(!osStrcasecmp(name, "Content-Length")) { //Get the length of the body data connection->request.contentLength = atoi(value); } //Accept-Encoding field header? else if(!osStrcasecmp(name, "Accept-Encoding")) { //Parse Content-Type header field httpParseAcceptEncodingField(connection, value); } //Authorization header field? else if(!osStrcasecmp(name, "Authorization")) { //Parse Authorization header field httpParseAuthorizationField(connection, value); } // Range header field? else if (!osStrcasecmp(name, "Range")) { sscanf(value, "bytes=%" PRIu32 "-%" PRIu32, &connection->request.Range.start, &connection->request.Range.end); if (connection->request.Range.start < 0) connection->request.Range.start = 0; if (connection->request.Range.end < 0) connection->request.Range.end = 0; } // If-Range header field? else if (!osStrcasecmp(name, "If-Range")) { strSafeCopy(connection->request.ifRange, value, HTTP_SERVER_IFRANGE_MAX_LEN); } #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED) //Upgrade header field? else if(!osStrcasecmp(name, "Upgrade")) { //WebSocket support? if(!osStrcasecmp(value, "websocket")) connection->request.upgradeWebSocket = TRUE; } //Sec-WebSocket-Key header field? else if(!osStrcasecmp(name, "Sec-WebSocket-Key")) { //Save the contents of the Sec-WebSocket-Key header field strSafeCopy(connection->request.clientKey, value, WEB_SOCKET_CLIENT_KEY_SIZE + 1); } #endif #if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED) //Cookie header field? else if(!osStrcasecmp(name, "Cookie")) { //Parse Cookie header field httpParseCookieField(connection, value); } #endif else if (!osStrcasecmp(name, "User-Agent")) { strSafeCopy(connection->request.userAgent, value, sizeof(connection->request.userAgent) + 1); } }
/** * @brief Parse HTTP header field * @param[in] connection Structure representing an HTTP connection * @param[in] name Name of the header field * @param[in] value Value of the header field **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L407-L498
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if(!osStrcasecmp(name, "Upgrade")) { //WebSocket support? if(!osStrcasecmp(value, "websocket")) connection->request.upgradeWebSocket = TRUE; }
//Upgrade header field?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L471-L476
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if(!osStrcasecmp(name, "Sec-WebSocket-Key")) { //Save the contents of the Sec-WebSocket-Key header field strSafeCopy(connection->request.clientKey, value, WEB_SOCKET_CLIENT_KEY_SIZE + 1); }
//Sec-WebSocket-Key header field?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L478-L483
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if(!osStrcasecmp(name, "Cookie")) { //Parse Cookie header field httpParseCookieField(connection, value); }
//Cookie header field?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L487-L491
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpParseConnectionField
void httpParseConnectionField(HttpConnection *connection, char_t *value) { char_t *p; char_t *token; //Get the first value of the list token = osStrtok_r(value, ",", &p); //Parse the comma-separated list while(token != NULL) { //Trim whitespace characters value = strTrimWhitespace(token); //Check current value if(!osStrcasecmp(value, "keep-alive")) { //The connection is persistent connection->request.keepAlive = TRUE; } else if(!osStrcasecmp(value, "close")) { //The connection will be closed after completion of the response connection->request.keepAlive = FALSE; } #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED) else if(!osStrcasecmp(value, "upgrade")) { //Upgrade the connection connection->request.connectionUpgrade = TRUE; } #endif //Get next value token = osStrtok_r(NULL, ",", &p); } }
/** * @brief Parse Connection header field * @param[in] connection Structure representing an HTTP connection * @param[in] value Connection field value **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L506-L543
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpParseContentTypeField
void httpParseContentTypeField(HttpConnection *connection, char_t *value) { #if (HTTP_SERVER_MULTIPART_TYPE_SUPPORT == ENABLED) size_t n; char_t *p; char_t *token; //Retrieve type token = osStrtok_r(value, "/", &p); //Any parsing error? if(token == NULL) return; //The boundary parameter makes sense only for the multipart content-type if(!osStrcasecmp(token, "multipart")) { //Skip subtype token = osStrtok_r(NULL, ";", &p); //Any parsing error? if(token == NULL) return; //Retrieve parameter name token = osStrtok_r(NULL, "=", &p); //Any parsing error? if(token == NULL) return; //Trim whitespace characters token = strTrimWhitespace(token); //Check parameter name if(!osStrcasecmp(token, "boundary")) { //Retrieve parameter value token = osStrtok_r(NULL, ";", &p); //Any parsing error? if(token == NULL) return; //Trim whitespace characters token = strTrimWhitespace(token); //Get the length of the boundary string n = osStrlen(token); //Check the length of the boundary string if(n < HTTP_SERVER_BOUNDARY_MAX_LEN) { //Copy the boundary string osStrcpy(connection->request.boundary, token); //Properly terminate the string connection->request.boundary[n] = '\0'; //Save the length of the boundary string connection->request.boundaryLength = n; } } } #endif }
/** * @brief Parse Content-Type header field * @param[in] connection Structure representing an HTTP connection * @param[in] value Content-Type field value **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L552-L612
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpParseAcceptEncodingField
void httpParseAcceptEncodingField(HttpConnection *connection, char_t *value) { #if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED) char_t *p; char_t *token; //Get the first value of the list token = osStrtok_r(value, ",", &p); //Parse the comma-separated list while(token != NULL) { //Trim whitespace characters value = strTrimWhitespace(token); //Check current value if(!osStrcasecmp(value, "gzip")) { //gzip compression is supported connection->request.acceptGzipEncoding = TRUE; } //Get next value token = osStrtok_r(NULL, ",", &p); } #endif }
/** * @brief Parse Accept-Encoding header field * @param[in] connection Structure representing an HTTP connection * @param[in] value Accept-Encoding field value **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L621-L648
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpParseCookieField
void httpParseCookieField(HttpConnection *connection, char_t *value) { #if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED) //Save the value of the header field strSafeCopy(connection->request.cookie, value, HTTP_SERVER_COOKIE_MAX_LEN); #endif }
/** * @brief Parse Cookie header field * @param[in] connection Structure representing an HTTP connection * @param[in] value Accept-Encoding field value **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L657-L663
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpReadChunkSize
error_t httpReadChunkSize(HttpConnection *connection) { error_t error; size_t n; char_t *end; char_t s[8]; //First chunk to be received? if(connection->request.firstChunk) { //Clear the flag connection->request.firstChunk = FALSE; } else { //Read the CRLF that follows the previous chunk-data field error = httpReceive(connection, s, sizeof(s) - 1, &n, SOCKET_FLAG_BREAK_CRLF); //Any error to report? if(error) return error; //Properly terminate the string with a NULL character s[n] = '\0'; //The chunk data must be terminated by CRLF if(osStrcmp(s, "\r\n")) return ERROR_WRONG_ENCODING; } //Read the chunk-size field error = httpReceive(connection, s, sizeof(s) - 1, &n, SOCKET_FLAG_BREAK_CRLF); //Any error to report? if(error) return error; //Properly terminate the string with a NULL character s[n] = '\0'; //Remove extra whitespaces strRemoveTrailingSpace(s); //Retrieve the size of the chunk connection->request.byteCount = osStrtoul(s, &end, 16); //No valid conversion could be performed? if(end == s || *end != '\0') return ERROR_WRONG_ENCODING; //Any chunk whose size is zero terminates the data transfer if(!connection->request.byteCount) { //The end of the HTTP request body has been reached connection->request.lastChunk = TRUE; //Skip the trailer while(1) { //Read a complete line error = httpReceive(connection, s, sizeof(s) - 1, &n, SOCKET_FLAG_BREAK_CRLF); //Unable to read any data? if(error) return error; //Properly terminate the string with a NULL character s[n] = '\0'; //The trailer is terminated by an empty line if(!osStrcmp(s, "\r\n")) break; } } //Successful processing return NO_ERROR; }
/** * @brief Read chunk-size field from the input stream * @param[in] connection Structure representing an HTTP connection **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L671-L744
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpInitResponseHeader
void httpInitResponseHeader(HttpConnection *connection) { //Default HTTP header fields connection->response.version = connection->request.version; connection->response.statusCode = 200; connection->response.noCache = FALSE; connection->response.maxAge = 0; connection->response.location = NULL; connection->response.contentType = mimeGetType(connection->request.uri); connection->response.chunkedEncoding = FALSE; #if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED) //Do not use gzip encoding connection->response.gzipEncoding = FALSE; #endif #if (HTTP_SERVER_PERSISTENT_CONN_SUPPORT == ENABLED) //Persistent connections are accepted connection->response.keepAlive = connection->request.keepAlive; #else //Connections are not persistent by default connection->response.keepAlive = FALSE; #endif }
/** * @brief Initialize response header * @param[in] connection Structure representing an HTTP connection **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L752-L775
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpFormatResponseHeader
error_t httpFormatResponseHeader(HttpConnection *connection, char_t *buffer) { uint_t i; char_t *p; //HTTP version 0.9? if(connection->response.version == HTTP_VERSION_0_9) { //Enforce default parameters connection->response.keepAlive = FALSE; connection->response.chunkedEncoding = FALSE; //The size of the response body is not limited connection->response.byteCount = UINT_MAX; //empty buffer buffer[0] = '\0'; //We are done since HTTP 0.9 does not support Full-Response format return NO_ERROR; } //When generating dynamic web pages with HTTP 1.0, the only way to //signal the end of the body is to close the connection if(connection->response.version == HTTP_VERSION_1_0 && connection->response.chunkedEncoding) { //Make the connection non persistent connection->response.keepAlive = FALSE; connection->response.chunkedEncoding = FALSE; //The size of the response body is not limited connection->response.byteCount = UINT_MAX; } else { //Limit the size of the response body connection->response.byteCount = connection->response.contentLength; } //Point to the beginning of the buffer p = buffer; //The first line of a response message is the Status-Line, consisting //of the protocol version followed by a numeric status code and its //associated textual phrase p += osSprintf(p, "HTTP/%u.%u %u ", MSB(connection->response.version), LSB(connection->response.version), connection->response.statusCode); //Retrieve the Reason-Phrase that corresponds to the Status-Code for(i = 0; i < arraysize(statusCodeList); i++) { //Check the status code if(statusCodeList[i].value == connection->response.statusCode) { //Append the textual phrase to the Status-Line p += osSprintf(p, "%s", statusCodeList[i].message); //Break the loop and continue processing break; } } //Properly terminate the Status-Line p += osSprintf(p, "\r\n"); //Valid location? if(connection->response.location != NULL) { //Set Location field p += osSprintf(p, "Location: %s\r\n", connection->response.location); } //Persistent connection? if(connection->response.keepAlive) { //Set Connection field p += osSprintf(p, "Connection: keep-alive\r\n"); //Set Keep-Alive field p += osSprintf(p, "Keep-Alive: timeout=%u, max=%u\r\n", HTTP_SERVER_IDLE_TIMEOUT / 1000, HTTP_SERVER_MAX_REQUESTS); } else { //Set Connection field p += osSprintf(p, "Connection: close\r\n"); } // Add Range accept field p += osSprintf(p, "Accept-Ranges: bytes\r\n"); //Specify the caching policy if(connection->response.noCache) { //Set Pragma field p += osSprintf(p, "Pragma: no-cache\r\n"); //Set Cache-Control field p += osSprintf(p, "Cache-Control: no-store, no-cache, must-revalidate\r\n"); p += osSprintf(p, "Cache-Control: max-age=0, post-check=0, pre-check=0\r\n"); } else if(connection->response.maxAge != 0) { //Set Cache-Control field p += osSprintf(p, "Cache-Control: max-age=%u\r\n", connection->response.maxAge); } #if (HTTP_SERVER_TLS_SUPPORT == ENABLED && HTTP_SERVER_HSTS_SUPPORT == ENABLED) //TLS-secured connection? if(connection->serverContext->settings.tlsInitCallback != NULL) { //Set Strict-Transport-Security field p += osSprintf(p, "Strict-Transport-Security: max-age=31536000\r\n"); } #endif #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED) //Check whether authentication is required if(connection->response.auth.mode != HTTP_AUTH_MODE_NONE) { //Add WWW-Authenticate header field p += httpAddAuthenticateField(connection, p); } #endif #if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED) //Valid cookie if(connection->response.setCookie[0] != '\0') { //Add Set-Cookie header field p += osSprintf(p, "Set-Cookie: %s\r\n", connection->response.setCookie); } #endif char_t *allowOrigin = connection->serverContext->settings.allowOrigin; if (allowOrigin != NULL && osStrlen(allowOrigin) > 0) { p += osSprintf(p, "Access-Control-Allow-Origin: %s\r\n", allowOrigin); } //Valid content type? if(connection->response.contentType != NULL) { //Content type p += osSprintf(p, "Content-Type: %s\r\n", connection->response.contentType); } // Range included? if (connection->response.contentRange != NULL) { p += osSprintf(p, "Content-Range: %s\r\n", connection->response.contentRange); } #if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED) //Use gzip encoding? if(connection->response.gzipEncoding) { //Set Transfer-Encoding field p += osSprintf(p, "Content-Encoding: gzip\r\n"); } #endif //Use chunked encoding transfer? if(connection->response.chunkedEncoding) { //Set Transfer-Encoding field p += osSprintf(p, "Transfer-Encoding: chunked\r\n"); } //Persistent connection? else if(connection->response.keepAlive) { //Set Content-Length field p += osSprintf(p, "Content-Length: %zu\r\n", connection->response.contentLength); } //Terminate the header with an empty line p += osSprintf(p, "\r\n"); //Successful processing return NO_ERROR; }
/** * @brief Format HTTP response header * @param[in] connection Structure representing an HTTP connection * @param[out] buffer Pointer to the buffer where to format the HTTP header * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L785-L960
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpSend
error_t httpSend(HttpConnection *connection, const void *data, size_t length, uint_t flags) { #if (NET_RTOS_SUPPORT == ENABLED) error_t error; #if (HTTP_SERVER_TLS_SUPPORT == ENABLED) //Check whether a secure connection is being used if(connection->tlsContext != NULL) { //Use TLS to transmit data to the client error = tlsWrite(connection->tlsContext, data, length, NULL, flags); } else #endif { //Transmit data to the client error = socketSend(connection->socket, data, length, NULL, flags); } pcaplog_ctx_t ctx; ctx.local_endpoint.ipv4 = connection->settings->ipAddr.ipv4Addr; ctx.local_endpoint.port = connection->settings->port; ctx.remote_endpoint.ipv4 = connection->socket->remoteIpAddr.ipv4Addr; ctx.remote_endpoint.port = connection->socket->remotePort; ctx.pcap_data = &connection->private.pcap_data; pcaplog_write(&ctx, true, (const uint8_t *)data, length); //Return status code return error; #else //Prevent buffer overflow if((connection->bufferLen + length) > HTTP_SERVER_BUFFER_SIZE) return ERROR_BUFFER_OVERFLOW; //Copy user data osMemcpy(connection->buffer + connection->bufferLen, data, length); //Adjust the length of the buffer connection->bufferLen += length; //Successful processing return NO_ERROR; #endif }
/** * @brief Send data to the client * @param[in] connection Structure representing an HTTP connection * @param[in] data Pointer to a buffer containing the data to be transmitted * @param[in] length Number of bytes to be transmitted * @param[in] flags Set of flags that influences the behavior of this function **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L971-L1014
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpReceive
error_t httpReceive(HttpConnection *connection, void *data, size_t size, size_t *received, uint_t flags) { #if (NET_RTOS_SUPPORT == ENABLED) error_t error; #if (HTTP_SERVER_TLS_SUPPORT == ENABLED) //Check whether a secure connection is being used if(connection->tlsContext != NULL) { //Use TLS to receive data from the client error = tlsRead(connection->tlsContext, data, size, received, flags); } else #endif { //Receive data from the client error = socketReceive(connection->socket, data, size, received, flags); } pcaplog_ctx_t ctx; ctx.local_endpoint.ipv4 = connection->settings->ipAddr.ipv4Addr; ctx.local_endpoint.port = connection->settings->port; ctx.remote_endpoint.ipv4 = connection->socket->remoteIpAddr.ipv4Addr; ctx.remote_endpoint.port = connection->socket->remotePort; ctx.pcap_data = &connection->private.pcap_data; pcaplog_write(&ctx, false, (const uint8_t *)data, *received); //Return status code return error; #else error_t error; char_t c; size_t i; size_t n; //Number of data bytes that are pending in the receive buffer n = connection->bufferLen - connection->bufferPos; //Any data to be copied? if(n > 0) { //Limit the number of bytes to read at a time n = MIN(n, size); //The HTTP_FLAG_BREAK_CHAR flag causes the function to stop reading //data as soon as the specified break character is encountered if((flags & HTTP_FLAG_BREAK_CHAR) != 0) { //Retrieve the break character code c = LSB(flags); //Search for the specified break character for(i = 0; i < n && connection->buffer[connection->bufferPos + i] != c; i++) { } //Adjust the number of data to read n = MIN(n, i + 1); } //Copy data to user buffer osMemcpy(data, connection->buffer + connection->bufferPos, n); //Advance current position connection->bufferPos += n; //Total number of data that have been read *received = n; //Successful processing error = NO_ERROR; } else { //No more data available... error = ERROR_END_OF_STREAM; } //Return status code return error; #endif }
/** * @brief Receive data from the client * @param[in] connection Structure representing an HTTP connection * @param[out] data Buffer into which received data will be placed * @param[in] size Maximum number of bytes that can be received * @param[out] received Actual number of bytes that have been received * @param[in] flags Set of flags that influences the behavior of this function * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L1027-L1108
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpGetAbsolutePath
void httpGetAbsolutePath(HttpConnection *connection, const char_t *relative, char_t *absolute, size_t maxLen) { //Copy the root directory osStrcpy(absolute, connection->settings->rootDirectory); //Append the specified path pathCombine(absolute, relative, maxLen); //Clean the resulting path pathCanonicalize(absolute); }
/** * @brief Retrieve the full pathname to the specified resource * @param[in] connection Structure representing an HTTP connection * @param[in] relative String containing the relative path to the resource * @param[out] absolute Resulting string containing the absolute path * @param[in] maxLen Maximum acceptable path length **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L1119-L1130
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpCompExtension
bool_t httpCompExtension(const char_t *filename, const char_t *extension) { uint_t n; uint_t m; //Get the length of the specified filename n = osStrlen(filename); //Get the length of the extension m = osStrlen(extension); //Check the length of the filename if(n < m) return FALSE; //Compare extensions if(!osStrncasecmp(filename + n - m, extension, m)) { return TRUE; } else { return FALSE; } }
/** * @brief Compare filename extension * @param[in] filename Filename whose extension is to be checked * @param[in] extension String defining the extension to be checked * @return TRUE is the filename matches the given extension, else FALSE **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L1140-L1163
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpDecodePercentEncodedString
error_t httpDecodePercentEncodedString(const char_t *input, char_t *output, size_t outputSize) { size_t i; char_t buffer[3]; //Check parameters if(input == NULL || output == NULL) return ERROR_INVALID_PARAMETER; //Decode the percent-encoded string for(i = 0; *input != '\0' && i < outputSize; i++) { //Check current character if(*input == '+') { //Replace '+' characters with spaces output[i] = ' '; //Advance data pointer input++; } else if(input[0] == '%' && input[1] != '\0' && input[2] != '\0') { //Process percent-encoded characters buffer[0] = input[1]; buffer[1] = input[2]; buffer[2] = '\0'; //String to integer conversion output[i] = (uint8_t) osStrtoul(buffer, NULL, 16); //Advance data pointer input += 3; } else { //Copy any other characters output[i] = *input; //Advance data pointer input++; } } //Check whether the output buffer runs out of space if(i >= outputSize) return ERROR_FAILURE; //Properly terminate the resulting string output[i] = '\0'; //Successful processing return NO_ERROR; }
/** * @brief Decode a percent-encoded string * @param[in] input NULL-terminated string to be decoded * @param[out] output NULL-terminated string resulting from the decoding process * @param[in] outputSize Size of the output buffer in bytes * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L1174-L1223
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
httpConvertArrayToHexString
void httpConvertArrayToHexString(const uint8_t *input, size_t inputLen, char_t *output) { size_t i; //Hex conversion table static const char_t hexDigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; //Process byte array for(i = 0; i < inputLen; i++) { //Convert upper nibble output[i * 2] = hexDigit[(input[i] >> 4) & 0x0F]; //Then convert lower nibble output[i * 2 + 1] = hexDigit[input[i] & 0x0F]; } //Properly terminate the string with a NULL character output[i * 2] = '\0'; }
/** * @brief Convert byte array to hex string * @param[in] input Point to the byte array * @param[in] inputLen Length of the byte array * @param[out] output NULL-terminated string resulting from the conversion **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/http/http_server_misc.c#L1233-L1256
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
mqttClientWebSocketTlsInitCallback
error_t mqttClientWebSocketTlsInitCallback(WebSocket *webSocket, TlsContext *tlsContext) { MqttClientContext *context; // Point to the MQTT client context context = webSocket->tlsInitParam; // Invoke user-defined callback return context->callbacks.tlsInitCallback(context, tlsContext); }
/** * @brief TLS initialization callback * @param[in] webSocket Handle to a WebSocket * @param[in] tlsContext Pointer to the TLS context * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L55-L65
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
mqttClientOpenConnection
error_t mqttClientOpenConnection(MqttClientContext *context) { error_t error; // TCP transport protocol? if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP) { // Open a TCP socket context->socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP); // Valid socket handle? if (context->socket != NULL) { // Associate the socket with the relevant interface error = socketBindToInterface(context->socket, context->interface); } else { // Report an error error = ERROR_OPEN_FAILED; } } #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED) // TLS transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Open a TCP socket context->socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP); // Valid socket handle? if (context->socket != NULL) { // Associate the socket with the relevant interface error = socketBindToInterface(context->socket, context->interface); // Check status code if (!error) { // Allocate TLS context context->tlsContext = tlsInit(); // Valid TLS handle? if (context->tlsContext != NULL) { // Select client operation mode error = tlsSetConnectionEnd(context->tlsContext, TLS_CONNECTION_END_CLIENT); // Check status code if (!error) { // Bind TLS to the relevant socket error = tlsSetSocket(context->tlsContext, context->socket); } // Check status code if (!error) { // Restore TLS session, if any error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession); } // Check status code if (!error) { // Invoke user-defined callback, if any if (context->callbacks.tlsInitCallback != NULL) { // Perform TLS related initialization error = context->callbacks.tlsInitCallback(context, context->tlsContext); } } } else { // Report an error error = ERROR_OPEN_FAILED; } } } else { // Report an error error = ERROR_OPEN_FAILED; } } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED) // WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS) { // Open a WebSocket context->webSocket = webSocketOpen(); // Valid WebSocket handle? if (context->webSocket != NULL) { // Associate the WebSocket with the relevant interface error = webSocketBindToInterface(context->webSocket, context->interface); } else { // Report an error error = ERROR_OPEN_FAILED; } } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED && MQTT_CLIENT_TLS_SUPPORT == ENABLED && \ WEB_SOCKET_TLS_SUPPORT == ENABLED) // Secure WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Open a WebSocket context->webSocket = webSocketOpen(); // Valid WebSocket handle? if (context->webSocket != NULL) { // Associate the WebSocket with the relevant interface error = webSocketBindToInterface(context->webSocket, context->interface); // Check status code if (!error) { // Attach MQTT client context context->webSocket->tlsInitParam = context; // Register TLS initialization callback error = webSocketRegisterTlsInitCallback(context->webSocket, mqttClientWebSocketTlsInitCallback); } } else { // Report an error error = ERROR_OPEN_FAILED; } } #endif // Unknown transport protocol? else { // Report an error error = ERROR_INVALID_PROTOCOL; } // Return status code return error; }
/** * @brief Open network connection * @param[in] context Pointer to the MQTT client context * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L75-L227
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Open a TCP socket context->socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP); // Valid socket handle? if (context->socket != NULL) { // Associate the socket with the relevant interface error = socketBindToInterface(context->socket, context->interface); // Check status code if (!error) { // Allocate TLS context context->tlsContext = tlsInit(); // Valid TLS handle? if (context->tlsContext != NULL) { // Select client operation mode error = tlsSetConnectionEnd(context->tlsContext, TLS_CONNECTION_END_CLIENT); // Check status code if (!error) { // Bind TLS to the relevant socket error = tlsSetSocket(context->tlsContext, context->socket); } // Check status code if (!error) { // Restore TLS session, if any error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession); } // Check status code if (!error) { // Invoke user-defined callback, if any if (context->callbacks.tlsInitCallback != NULL) { // Perform TLS related initialization error = context->callbacks.tlsInitCallback(context, context->tlsContext); } } } else { // Report an error error = ERROR_OPEN_FAILED; } } } else { // Report an error error = ERROR_OPEN_FAILED; } }
// TLS transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L99-L162
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS) { // Open a WebSocket context->webSocket = webSocketOpen(); // Valid WebSocket handle? if (context->webSocket != NULL) { // Associate the WebSocket with the relevant interface error = webSocketBindToInterface(context->webSocket, context->interface); } else { // Report an error error = ERROR_OPEN_FAILED; } }
// WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L166-L183
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Open a WebSocket context->webSocket = webSocketOpen(); // Valid WebSocket handle? if (context->webSocket != NULL) { // Associate the WebSocket with the relevant interface error = webSocketBindToInterface(context->webSocket, context->interface); // Check status code if (!error) { // Attach MQTT client context context->webSocket->tlsInitParam = context; // Register TLS initialization callback error = webSocketRegisterTlsInitCallback(context->webSocket, mqttClientWebSocketTlsInitCallback); } } else { // Report an error error = ERROR_OPEN_FAILED; } }
// Secure WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L188-L216
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
mqttClientEstablishConnection
error_t mqttClientEstablishConnection(MqttClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort) { error_t error; // TCP transport protocol? if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Connect to the MQTT server using TCP error = socketConnect(context->socket, serverIpAddr, serverPort); } } #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED) // TLS transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Connect to the MQTT server using TCP error = socketConnect(context->socket, serverIpAddr, serverPort); } // Check status code if (!error) { // Perform TLS handshake error = tlsConnect(context->tlsContext); } // Successful connection? if (!error) { // Save TLS session error = tlsSaveSessionState(context->tlsContext, &context->tlsSession); } } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED) // WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Set timeout error = webSocketSetTimeout(context->webSocket, context->settings.timeout); // Check status code if (!error) { // Set the hostname of the remote server error = webSocketSetHost(context->webSocket, context->settings.host); } // Check status code if (!error) { // The client MUST include "mqtt" in the list of WebSocket // sub-protocols it offers error = webSocketSetSubProtocol(context->webSocket, "mqtt"); } // Check status code if (!error) { // Connect to the MQTT server using WebSocket error = webSocketConnect(context->webSocket, serverIpAddr, serverPort, context->settings.uri); } } #endif // Unknown transport protocol? else { // Report an error error = ERROR_INVALID_PROTOCOL; } // Return status code return error; }
/** * @brief Establish network connection * @param[in] context Pointer to the MQTT client context * @param[in] serverIpAddr IP address of the MQTT server to connect to * @param[in] serverPort TCP port number that will be used to establish the * connection * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L238-L326
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Connect to the MQTT server using TCP error = socketConnect(context->socket, serverIpAddr, serverPort); } // Check status code if (!error) { // Perform TLS handshake error = tlsConnect(context->tlsContext); } // Successful connection? if (!error) { // Save TLS session error = tlsSaveSessionState(context->tlsContext, &context->tlsSession); } }
// TLS transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L258-L283
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Set timeout error = webSocketSetTimeout(context->webSocket, context->settings.timeout); // Check status code if (!error) { // Set the hostname of the remote server error = webSocketSetHost(context->webSocket, context->settings.host); } // Check status code if (!error) { // The client MUST include "mqtt" in the list of WebSocket // sub-protocols it offers error = webSocketSetSubProtocol(context->webSocket, "mqtt"); } // Check status code if (!error) { // Connect to the MQTT server using WebSocket error = webSocketConnect(context->webSocket, serverIpAddr, serverPort, context->settings.uri); } }
// WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L287-L315
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
mqttClientShutdownConnection
error_t mqttClientShutdownConnection(MqttClientContext *context) { error_t error; // TCP transport protocol? if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Shutdown TCP connection error = socketShutdown(context->socket, SOCKET_SD_BOTH); } } #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED) // TLS transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Shutdown TLS session error = tlsShutdown(context->tlsContext); } // Check status code if (!error) { // Shutdown TCP connection error = socketShutdown(context->socket, SOCKET_SD_BOTH); } } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED) // WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Set timeout error = webSocketSetTimeout(context->webSocket, context->settings.timeout); // Check status code if (!error) { // Shutdown WebSocket connection error = webSocketShutdown(context->webSocket); } } #endif // Unknown transport protocol? else { // Report an error error = ERROR_INVALID_PROTOCOL; } // Return status code return error; }
/** * @brief Shutdown network connection * @param[in] context Pointer to the MQTT client context * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L334-L398
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Shutdown TLS session error = tlsShutdown(context->tlsContext); } // Check status code if (!error) { // Shutdown TCP connection error = socketShutdown(context->socket, SOCKET_SD_BOTH); } }
// TLS transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L353-L371
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Set timeout error = webSocketSetTimeout(context->webSocket, context->settings.timeout); // Check status code if (!error) { // Shutdown WebSocket connection error = webSocketShutdown(context->webSocket); } }
// WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L375-L387
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
mqttClientCloseConnection
void mqttClientCloseConnection(MqttClientContext *context) { // TCP transport protocol? if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP) { // Close TCP connection if (context->socket != NULL) { socketClose(context->socket); context->socket = NULL; } } #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED) // TLS transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Release TLS context if (context->tlsContext != NULL) { tlsFree(context->tlsContext); context->tlsContext = NULL; } // Close TCP connection if (context->socket != NULL) { socketClose(context->socket); context->socket = NULL; } } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED) // WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Close WebSocket connection if (context->webSocket != NULL) { webSocketClose(context->webSocket); context->webSocket = NULL; } } #endif }
/** * @brief Close network connection * @param[in] context Pointer to the MQTT client context **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L405-L449
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Release TLS context if (context->tlsContext != NULL) { tlsFree(context->tlsContext); context->tlsContext = NULL; } // Close TCP connection if (context->socket != NULL) { socketClose(context->socket); context->socket = NULL; } }
// TLS transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L419-L434
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Close WebSocket connection if (context->webSocket != NULL) { webSocketClose(context->webSocket); context->webSocket = NULL; } }
// WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L438-L447
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
mqttClientSendData
error_t mqttClientSendData(MqttClientContext *context, const void *data, size_t length, size_t *written, uint_t flags) { error_t error; // TCP transport protocol? if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Transmit data error = socketSend(context->socket, data, length, written, flags); } } #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED) // TLS transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Transmit data error = tlsWrite(context->tlsContext, data, length, written, flags); } } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED) // WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Set timeout error = webSocketSetTimeout(context->webSocket, context->settings.timeout); // Check status code if (!error) { // MQTT control packets must be sent in WebSocket binary data frames error = webSocketSend(context->webSocket, data, length, WS_FRAME_TYPE_BINARY, written); } } #endif // Unknown transport protocol? else { // Report an error error = ERROR_INVALID_PROTOCOL; } // Return status code return error; }
/** * @brief Send data using the relevant transport protocol * @param[in] context Pointer to the MQTT client context * @param[in] data Pointer to a buffer containing the data to be transmitted * @param[in] length Number of bytes to be transmitted * @param[out] written Actual number of bytes written (optional parameter) * @param[in] flags Set of flags that influences the behavior of this function * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L461-L520
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Transmit data error = tlsWrite(context->tlsContext, data, length, written, flags); } }
// TLS transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L481-L492
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Set timeout error = webSocketSetTimeout(context->webSocket, context->settings.timeout); // Check status code if (!error) { // MQTT control packets must be sent in WebSocket binary data frames error = webSocketSend(context->webSocket, data, length, WS_FRAME_TYPE_BINARY, written); } }
// WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L496-L509
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
mqttClientReceiveData
error_t mqttClientReceiveData(MqttClientContext *context, void *data, size_t size, size_t *received, uint_t flags) { error_t error; // No data has been read yet *received = 0; // TCP transport protocol? if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Receive data error = socketReceive(context->socket, data, size, received, flags); } } #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED) // TLS transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Receive data error = tlsRead(context->tlsContext, data, size, received, flags); } } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED) // WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { WebSocketFrameType type; // Set timeout error = webSocketSetTimeout(context->webSocket, context->settings.timeout); // Check status code if (!error) { // Receive data error = webSocketReceive(context->webSocket, data, size, &type, received); } // Check status code if (!error) { // MQTT control packets must be sent in WebSocket binary data frames. If // any other type of data frame is received the recipient must close the // network connection if (type != WS_FRAME_TYPE_BINARY && type != WS_FRAME_TYPE_CONTINUATION) error = ERROR_INVALID_TYPE; } } #endif // Unknown transport protocol? else { // Report an error error = ERROR_INVALID_PROTOCOL; } // Return status code return error; }
/** * @brief Receive data using the relevant transport protocol * @param[in] context Pointer to the MQTT client context * @param[out] data Buffer into which received data will be placed * @param[in] size Maximum number of bytes that can be received * @param[out] received Number of bytes that have been received * @param[in] flags Set of flags that influences the behavior of this function * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L532-L605
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Set timeout error = socketSetTimeout(context->socket, context->settings.timeout); // Check status code if (!error) { // Receive data error = tlsRead(context->tlsContext, data, size, received, flags); } }
// TLS transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L555-L566
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS || context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { WebSocketFrameType type; // Set timeout error = webSocketSetTimeout(context->webSocket, context->settings.timeout); // Check status code if (!error) { // Receive data error = webSocketReceive(context->webSocket, data, size, &type, received); } // Check status code if (!error) { // MQTT control packets must be sent in WebSocket binary data frames. If // any other type of data frame is received the recipient must close the // network connection if (type != WS_FRAME_TYPE_BINARY && type != WS_FRAME_TYPE_CONTINUATION) error = ERROR_INVALID_TYPE; } }
// WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L570-L594
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
mqttClientWaitForData
error_t mqttClientWaitForData(MqttClientContext *context, systime_t timeout) { uint_t event; // TCP transport protocol? if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP) { // Wait for some data to be available for reading event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout); } #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED) // TLS transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Sanity check if (context->tlsContext == NULL) return ERROR_FAILURE; // Check whether some data is pending in the receive buffer if (context->tlsContext->rxBufferLen > 0) { // No need to poll the underlying socket for incoming traffic... event = SOCKET_EVENT_RX_READY; } else { // Wait for some data to be available for reading event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout); } } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED) // WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS) { // Sanity check if (context->webSocket == NULL) return ERROR_FAILURE; // Get exclusive access osAcquireMutex(&netMutex); // Wait for some data to be available for reading event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout); // Release exclusive access osReleaseMutex(&netMutex); } #endif #if (MQTT_CLIENT_WS_SUPPORT == ENABLED && WEB_SOCKET_TLS_SUPPORT) // Secure WebSocket transport protocol? else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Sanity check if (context->webSocket == NULL || context->webSocket->tlsContext == NULL) return ERROR_FAILURE; // Check whether some data is pending in the receive buffer if (context->webSocket->tlsContext->rxBufferLen > 0) { // No need to poll the underlying socket for incoming traffic... event = SOCKET_EVENT_RX_READY; } else { // Get exclusive access osAcquireMutex(&netMutex); // Wait for some data to be available for reading event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout); // Release exclusive access osReleaseMutex(&netMutex); } } #endif // Unknown transport protocol? else { // Report an error return ERROR_INVALID_PROTOCOL; } // Check whether some data is available for reading if (event == SOCKET_EVENT_RX_READY) { return NO_ERROR; } else { return ERROR_TIMEOUT; } }
/** * @brief Wait for incoming data * @param[in] context Pointer to the MQTT client context * @param[in] timeout Maximum time to wait before returning * @return Error code **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L614-L702
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS) { // Sanity check if (context->tlsContext == NULL) return ERROR_FAILURE; // Check whether some data is pending in the receive buffer if (context->tlsContext->rxBufferLen > 0) { // No need to poll the underlying socket for incoming traffic... event = SOCKET_EVENT_RX_READY; } else { // Wait for some data to be available for reading event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout); } }
// TLS transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L626-L643
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS) { // Sanity check if (context->webSocket == NULL) return ERROR_FAILURE; // Get exclusive access osAcquireMutex(&netMutex); // Wait for some data to be available for reading event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout); // Release exclusive access osReleaseMutex(&netMutex); }
// WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L647-L659
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
if
else if (context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS) { // Sanity check if (context->webSocket == NULL || context->webSocket->tlsContext == NULL) return ERROR_FAILURE; // Check whether some data is pending in the receive buffer if (context->webSocket->tlsContext->rxBufferLen > 0) { // No need to poll the underlying socket for incoming traffic... event = SOCKET_EVENT_RX_READY; } else { // Get exclusive access osAcquireMutex(&netMutex); // Wait for some data to be available for reading event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout); // Release exclusive access osReleaseMutex(&netMutex); } }
// Secure WebSocket transport protocol?
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c#L663-L684
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
_getopt_internal
int _getopt_internal(argc, argv, optstring, longopts, longind, long_only) int argc; char *const *argv; const char *optstring; const struct option *longopts; int *longind; int long_only; { int print_errors = opterr; if (optstring[0] == ':') print_errors = 0; if (argc < 1) return -1; optarg = NULL; if (optind == 0 || !__getopt_initialized) { if (optind == 0) optind = 1; /* Don't scan ARGV[0], the program name. */ optstring = _getopt_initialize(argc, argv, optstring); __getopt_initialized = 1; } /* Test whether ARGV[optind] points to a non-option argument. Either it does not have option syntax, or there is an environment flag from the shell indicating it is not an option. The later information is only used when the used in the GNU libc. */ #if defined _LIBC && defined USE_NONOPTION_FLAGS #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' || (optind < nonoption_flags_len && __getopt_nonoption_flags[optind] == '1')) #else #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') #endif if (nextchar == NULL || *nextchar == '\0') { /* Advance to the next ARGV-element. */ /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been moved back by the user (who may also have changed the arguments). */ if (last_nonopt > optind) last_nonopt = optind; if (first_nonopt > optind) first_nonopt = optind; if (ordering == PERMUTE) { /* If we have just processed some options following some non-options, exchange them so that the options come first. */ if (first_nonopt != last_nonopt && last_nonopt != optind) exchange((char **)argv); else if (last_nonopt != optind) first_nonopt = optind; /* Skip any additional non-options and extend the range of non-options previously skipped. */ while (optind < argc && NONOPTION_P) optind++; last_nonopt = optind; } /* The special ARGV-element `--' means premature end of options. Skip it like a null option, then exchange with previous non-options as if it were an option, then skip everything else like a non-option. */ if (optind != argc && !strcmp(argv[optind], "--")) { optind++; if (first_nonopt != last_nonopt && last_nonopt != optind) exchange((char **)argv); else if (first_nonopt == last_nonopt) first_nonopt = optind; last_nonopt = argc; optind = argc; } /* If we have done all the ARGV-elements, stop the scan and back over any non-options that we skipped and permuted. */ if (optind == argc) { /* Set the next-arg-index to point at the non-options that we previously skipped, so the caller will digest them. */ if (first_nonopt != last_nonopt) optind = first_nonopt; return -1; } /* If we have come to a non-option and did not permute it, either stop the scan or describe it to the caller and pass it by. */ if (NONOPTION_P) { if (ordering == REQUIRE_ORDER) return -1; optarg = argv[optind++]; return 1; } /* We have found another option-ARGV-element. Skip the initial punctuation. */ nextchar = (argv[optind] + 1 + (longopts != NULL && argv[optind][1] == '-')); } /* Decode the current option-ARGV-element. */ /* Check whether the ARGV-element is a long option. If long_only and the ARGV-element has the form "-f", where f is a valid short option, don't consider it an abbreviated form of a long option that starts with f. Otherwise there would be no way to give the -f short option. On the other hand, if there's a long option "fubar" and the ARGV-element is "-fu", do consider that an abbreviation of the long option, just like "--fu", and not "-f" with arg "u". This distinction seems to be the most useful approach. */ if (longopts != NULL && (argv[optind][1] == '-' || (long_only && (argv[optind][2] || !my_index(optstring, argv[optind][1]))))) { char *nameend; const struct option *p; const struct option *pfound = NULL; int exact = 0; int ambig = 0; int indfound = -1; int option_index; for (nameend = nextchar; *nameend && *nameend != '='; nameend++) /* Do nothing. */; /* Test all long options for either exact match or abbreviated matches. */ for (p = longopts, option_index = 0; p->name; p++, option_index++) if (!strncmp(p->name, nextchar, nameend - nextchar)) { if ((unsigned int)(nameend - nextchar) == (unsigned int)strlen(p->name)) { /* Exact match found. */ pfound = p; indfound = option_index; exact = 1; break; } else if (pfound == NULL) { /* First nonexact match found. */ pfound = p; indfound = option_index; } else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) /* Second or later nonexact match found. */ ambig = 1; } if (ambig && !exact) { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; __asprintf(&buf, _("%s: option `%s' is ambiguous\n"), argv[0], argv[optind]); if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #else fprintf(stderr, _("%s: option `%s' is ambiguous\n"), argv[0], argv[optind]); #endif } nextchar += strlen(nextchar); optind++; optopt = 0; return '?'; } if (pfound != NULL) { option_index = indfound; optind++; if (*nameend) { /* Don't test has_arg with >, because some C compilers don't allow it to be used on enums. */ if (pfound->has_arg) optarg = nameend + 1; else { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; #endif if (argv[optind - 1][1] == '-') { /* --option */ #if defined _LIBC && defined USE_IN_LIBIO __asprintf(&buf, _("\ %s: option `--%s' doesn't allow an argument\n"), argv[0], pfound->name); #else fprintf(stderr, _("\ %s: option `--%s' doesn't allow an argument\n"), argv[0], pfound->name); #endif } else { /* +option or -option */ #if defined _LIBC && defined USE_IN_LIBIO __asprintf(&buf, _("\ %s: option `%c%s' doesn't allow an argument\n"), argv[0], argv[optind - 1][0], pfound->name); #else fprintf(stderr, _("\ %s: option `%c%s' doesn't allow an argument\n"), argv[0], argv[optind - 1][0], pfound->name); #endif } #if defined _LIBC && defined USE_IN_LIBIO if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #endif } nextchar += strlen(nextchar); optopt = pfound->val; return '?'; } } else if (pfound->has_arg == 1) { if (optind < argc) optarg = argv[optind++]; else { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; __asprintf(&buf, _("%s: option `%s' requires an argument\n"), argv[0], argv[optind - 1]); if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #else fprintf(stderr, _("%s: option `%s' requires an argument\n"), argv[0], argv[optind - 1]); #endif } nextchar += strlen(nextchar); optopt = pfound->val; return optstring[0] == ':' ? ':' : '?'; } } nextchar += strlen(nextchar); if (longind != NULL) *longind = option_index; if (pfound->flag) { *(pfound->flag) = pfound->val; return 0; } return pfound->val; } /* Can't find it as a long option. If this is not getopt_long_only, or the option starts with '--' or is not a valid short option, then it's an error. Otherwise interpret it as a short option. */ if (!long_only || argv[optind][1] == '-' || my_index(optstring, *nextchar) == NULL) { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; #endif if (argv[optind][1] == '-') { /* --option */ #if defined _LIBC && defined USE_IN_LIBIO __asprintf(&buf, _("%s: unrecognized option `--%s'\n"), argv[0], nextchar); #else fprintf(stderr, _("%s: unrecognized option `--%s'\n"), argv[0], nextchar); #endif } else { /* +option or -option */ #if defined _LIBC && defined USE_IN_LIBIO __asprintf(&buf, _("%s: unrecognized option `%c%s'\n"), argv[0], argv[optind][0], nextchar); #else fprintf(stderr, _("%s: unrecognized option `%c%s'\n"), argv[0], argv[optind][0], nextchar); #endif } #if defined _LIBC && defined USE_IN_LIBIO if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #endif } nextchar = (char *)""; optind++; optopt = 0; return '?'; } } /* Look at and handle the next short option-character. */ { char c = *nextchar++; char *temp = my_index(optstring, c); /* Increment `optind' when we start to process its last character. */ if (*nextchar == '\0') ++optind; if (temp == NULL || c == ':') { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; #endif if (posixly_correct) { /* 1003.2 specifies the format of this message. */ #if defined _LIBC && defined USE_IN_LIBIO __asprintf(&buf, _("%s: illegal option -- %c\n"), argv[0], c); #else fprintf(stderr, _("%s: illegal option -- %c\n"), argv[0], c); #endif } else { #if defined _LIBC && defined USE_IN_LIBIO __asprintf(&buf, _("%s: invalid option -- %c\n"), argv[0], c); #else fprintf(stderr, _("%s: invalid option -- %c\n"), argv[0], c); #endif } #if defined _LIBC && defined USE_IN_LIBIO if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #endif } optopt = c; return '?'; } /* Convenience. Treat POSIX -W foo same as long option --foo */ if (temp[0] == 'W' && temp[1] == ';') { char *nameend; const struct option *p; const struct option *pfound = NULL; int exact = 0; int ambig = 0; int indfound = 0; int option_index; /* This is an option that requires an argument. */ if (*nextchar != '\0') { optarg = nextchar; /* If we end this ARGV-element by taking the rest as an arg, we must advance to the next element now. */ optind++; } else if (optind == argc) { if (print_errors) { /* 1003.2 specifies the format of this message. */ #if defined _LIBC && defined USE_IN_LIBIO char *buf; __asprintf(&buf, _("%s: option requires an argument -- %c\n"), argv[0], c); if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #else fprintf(stderr, _("%s: option requires an argument -- %c\n"), argv[0], c); #endif } optopt = c; if (optstring[0] == ':') c = ':'; else c = '?'; return c; } else /* We already incremented `optind' once; increment it again when taking next ARGV-elt as argument. */ optarg = argv[optind++]; /* optarg is now the argument, see if it's in the table of longopts. */ for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) /* Do nothing. */; /* Test all long options for either exact match or abbreviated matches. */ for (p = longopts, option_index = 0; p->name; p++, option_index++) if (!strncmp(p->name, nextchar, nameend - nextchar)) { if ((unsigned int)(nameend - nextchar) == strlen(p->name)) { /* Exact match found. */ pfound = p; indfound = option_index; exact = 1; break; } else if (pfound == NULL) { /* First nonexact match found. */ pfound = p; indfound = option_index; } else /* Second or later nonexact match found. */ ambig = 1; } if (ambig && !exact) { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; __asprintf(&buf, _("%s: option `-W %s' is ambiguous\n"), argv[0], argv[optind]); if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #else fprintf(stderr, _("%s: option `-W %s' is ambiguous\n"), argv[0], argv[optind]); #endif } nextchar += strlen(nextchar); optind++; return '?'; } if (pfound != NULL) { option_index = indfound; if (*nameend) { /* Don't test has_arg with >, because some C compilers don't allow it to be used on enums. */ if (pfound->has_arg) optarg = nameend + 1; else { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; __asprintf(&buf, _("\ %s: option `-W %s' doesn't allow an argument\n"), argv[0], pfound->name); if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #else fprintf(stderr, _("\ %s: option `-W %s' doesn't allow an argument\n"), argv[0], pfound->name); #endif } nextchar += strlen(nextchar); return '?'; } } else if (pfound->has_arg == 1) { if (optind < argc) optarg = argv[optind++]; else { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; __asprintf(&buf, _("\ %s: option `%s' requires an argument\n"), argv[0], argv[optind - 1]); if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #else fprintf(stderr, _("%s: option `%s' requires an argument\n"), argv[0], argv[optind - 1]); #endif } nextchar += strlen(nextchar); return optstring[0] == ':' ? ':' : '?'; } } nextchar += strlen(nextchar); if (longind != NULL) *longind = option_index; if (pfound->flag) { *(pfound->flag) = pfound->val; return 0; } return pfound->val; } nextchar = NULL; return 'W'; /* Let the application handle it. */ } if (temp[1] == ':') { if (temp[2] == ':') { /* This is an option that accepts an argument optionally. */ if (*nextchar != '\0') { optarg = nextchar; optind++; } else optarg = NULL; nextchar = NULL; } else { /* This is an option that requires an argument. */ if (*nextchar != '\0') { optarg = nextchar; /* If we end this ARGV-element by taking the rest as an arg, we must advance to the next element now. */ optind++; } else if (optind == argc) { if (print_errors) { /* 1003.2 specifies the format of this message. */ #if defined _LIBC && defined USE_IN_LIBIO char *buf; __asprintf(&buf, _("%s: option requires an argument -- %c\n"), argv[0], c); if (_IO_fwide(stderr, 0) > 0) __fwprintf(stderr, L"%s", buf); else fputs(buf, stderr); free(buf); #else fprintf(stderr, _("%s: option requires an argument -- %c\n"), argv[0], c); #endif } optopt = c; if (optstring[0] == ':') c = ':'; else c = '?'; } else /* We already incremented `optind' once; increment it again when taking next ARGV-elt as argument. */ optarg = argv[optind++]; nextchar = NULL; } } return c; } }
/* Scan elements of ARGV (whose length is ARGC) for option characters given in OPTSTRING. If an element of ARGV starts with '-', and is not exactly "-" or "--", then it is an option element. The characters of this element (aside from the initial '-') are option characters. If `getopt' is called repeatedly, it returns successively each of the option characters from each of the option elements. If `getopt' finds another option character, it returns that character, updating `optind' and `nextchar' so that the next call to `getopt' can resume the scan with the following option character or ARGV-element. If there are no more option characters, `getopt' returns -1. Then `optind' is the index in ARGV of the first ARGV-element that is not an option. (The ARGV-elements have been permuted so that those that are not options now come last.) OPTSTRING is a string containing the legitimate option characters. If an option character is seen that is not listed in OPTSTRING, return '?' after printing an error message. If you set `opterr' to zero, the error message is suppressed but we still return '?'. If a char in OPTSTRING is followed by a colon, that means it wants an arg, so the following text in the same ARGV-element, or the text of the following ARGV-element, is returned in `optarg'. Two colons mean an option that wants an optional arg; if there is text in the current ARGV-element, it is returned in `optarg', otherwise `optarg' is set to zero. If OPTSTRING starts with `-' or `+', it requests different methods of handling the non-option ARGV-elements. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. Long-named options begin with `--' instead of `-'. Their names may be abbreviated as long as the abbreviation is unique or is an exact match for some defined option. If they have an argument, it follows the option name in the same ARGV-element, separated from the option name by a `=', or else the in next ARGV-element. When `getopt' finds a long-named option, it returns 0 if that option's `flag' field is nonzero, the value of the option's `val' field if the `flag' field is zero. The elements of ARGV aren't really const, because we permute them. But we pretend they're const in the prototype to be compatible with other systems. LONGOPTS is a vector of `struct option' terminated by an element containing a name which is zero. LONGIND returns the index in LONGOPT of the long-named option found. It is only valid when a long-named option has been found by the most recent call. If LONG_ONLY is nonzero, '-' as well as '--' can introduce long-named options. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/platform/getopt.c#L508-L1155
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
main
int main(argc, argv) int argc; char **argv; { int c; int digit_optind = 0; while (1) { int this_option_optind = optind ? optind : 1; c = getopt(argc, argv, "abc:d:0123456789"); if (c == -1) break; switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (digit_optind != 0 && digit_optind != this_option_optind) printf("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf("option %c\n", c); break; case 'a': printf("option a\n"); break; case 'b': printf("option b\n"); break; case 'c': printf("option c with value `%s'\n", optarg); break; case '?': break; default: printf("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } exit(0); }
/* Compile with -DTEST to make an executable for use in testing the above definition of `getopt'. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/platform/getopt.c#L1187-L1249
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
tcpWaitForEvents
uint_t tcpWaitForEvents(Socket *socket, uint_t eventMask, systime_t timeout) { fd_set read_fds; struct timeval tv; if (socket == NULL) return 0; // Initialize the file descriptor set. FD_ZERO(&read_fds); FD_SET(socket->descriptor, &read_fds); // Set timeout. tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000; // Wait for the event. int result = select(socket->descriptor + 1, &read_fds, NULL, NULL, &tv); // Check if socket is ready for reading. if (result > 0 && FD_ISSET(socket->descriptor, &read_fds)) { return eventMask; } return 0; }
/** * @brief Wait for a particular TCP event * @param[in] socket Handle referencing the socket * @param[in] eventMask Logic OR of all the TCP events that will complete the wait * @param[in] timeout Maximum time to wait * @return Logic OR of all the TCP events that satisfied the wait **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/platform/platform_linux.c#L450-L476
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
getCurrentUnixTime
time_t getCurrentUnixTime(void) { return time(NULL); }
/** * @brief Get current time * @return Unix timestamp **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/platform/platform_linux.c#L483-L486
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
tcpWaitForEvents
uint_t tcpWaitForEvents(Socket *socket, uint_t eventMask, systime_t timeout) { fd_set read_fds; struct timeval tv; if (socket == NULL) return 0; // Initialize the file descriptor set. FD_ZERO(&read_fds); FD_SET(socket->descriptor, &read_fds); // Set timeout. tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000; // Wait for the event. int result = select(socket->descriptor + 1, &read_fds, NULL, NULL, &tv); // Check if socket is ready for reading. if (result > 0 && FD_ISSET(socket->descriptor, &read_fds)) { return eventMask; } return 0; }
/** * @brief Wait for a particular TCP event * @param[in] socket Handle referencing the socket * @param[in] eventMask Logic OR of all the TCP events that will complete the wait * @param[in] timeout Maximum time to wait * @return Logic OR of all the TCP events that satisfied the wait **/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/platform/platform_windows.c#L508-L534
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
protobuf_c_buffer_simple_append
void protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer, size_t len, const uint8_t *data) { ProtobufCBufferSimple *simp = (ProtobufCBufferSimple *) buffer; size_t new_len = simp->len + len; if (new_len > simp->alloced) { ProtobufCAllocator *allocator = simp->allocator; size_t new_alloced = simp->alloced * 2; uint8_t *new_data; if (allocator == NULL) allocator = &protobuf_c__allocator; while (new_alloced < new_len) new_alloced += new_alloced; new_data = do_alloc(allocator, new_alloced); if (!new_data) return; memcpy(new_data, simp->data, simp->len); if (simp->must_free_data) do_free(allocator, simp->data); else simp->must_free_data = TRUE; simp->data = new_data; simp->alloced = new_alloced; } memcpy(simp->data + simp->len, data, len); simp->len = new_len; }
/* === buffer-simple === */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L190-L219
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
get_tag_size
static inline size_t get_tag_size(uint32_t number) { if (number < (1UL << 4)) { return 1; } else if (number < (1UL << 11)) { return 2; } else if (number < (1UL << 18)) { return 3; } else if (number < (1UL << 25)) { return 4; } else { return 5; } }
/** * \defgroup packedsz protobuf_c_message_get_packed_size() implementation * * Routines mainly used by protobuf_c_message_get_packed_size(). * * \ingroup internal * @{ */ /** * Return the number of bytes required to store the tag for the field. Includes * 3 bits for the wire-type, and a single bit that denotes the end-of-tag. * * \param number * Field tag to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L239-L253
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
uint32_size
static inline size_t uint32_size(uint32_t v) { if (v < (1UL << 7)) { return 1; } else if (v < (1UL << 14)) { return 2; } else if (v < (1UL << 21)) { return 3; } else if (v < (1UL << 28)) { return 4; } else { return 5; } }
/** * Return the number of bytes required to store a variable-length unsigned * 32-bit integer in base-128 varint encoding. * * \param v * Value to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L264-L278
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
int32_size
static inline size_t int32_size(int32_t v) { if (v < 0) { return 10; } else if (v < (1L << 7)) { return 1; } else if (v < (1L << 14)) { return 2; } else if (v < (1L << 21)) { return 3; } else if (v < (1L << 28)) { return 4; } else { return 5; } }
/** * Return the number of bytes required to store a variable-length signed 32-bit * integer in base-128 varint encoding. * * \param v * Value to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L289-L305
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
zigzag32
static inline uint32_t zigzag32(int32_t v) { // Note: Using unsigned types prevents undefined behavior return ((uint32_t)v << 1) ^ -((uint32_t)v >> 31); }
/** * Return the ZigZag-encoded 32-bit unsigned integer form of a 32-bit signed * integer. * * \param v * Value to encode. * \return * ZigZag encoded integer. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L316-L321
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
sint32_size
static inline size_t sint32_size(int32_t v) { return uint32_size(zigzag32(v)); }
/** * Return the number of bytes required to store a signed 32-bit integer, * converted to an unsigned 32-bit integer with ZigZag encoding, using base-128 * varint encoding. * * \param v * Value to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L333-L337
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
uint64_size
static inline size_t uint64_size(uint64_t v) { uint32_t upper_v = (uint32_t) (v >> 32); if (upper_v == 0) { return uint32_size((uint32_t) v); } else if (upper_v < (1UL << 3)) { return 5; } else if (upper_v < (1UL << 10)) { return 6; } else if (upper_v < (1UL << 17)) { return 7; } else if (upper_v < (1UL << 24)) { return 8; } else if (upper_v < (1UL << 31)) { return 9; } else { return 10; } }
/** * Return the number of bytes required to store a 64-bit unsigned integer in * base-128 varint encoding. * * \param v * Value to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L348-L368
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
zigzag64
static inline uint64_t zigzag64(int64_t v) { // Note: Using unsigned types prevents undefined behavior return ((uint64_t)v << 1) ^ -((uint64_t)v >> 63); }
/** * Return the ZigZag-encoded 64-bit unsigned integer form of a 64-bit signed * integer. * * \param v * Value to encode. * \return * ZigZag encoded integer. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L379-L384
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
sint64_size
static inline size_t sint64_size(int64_t v) { return uint64_size(zigzag64(v)); }
/** * Return the number of bytes required to store a signed 64-bit integer, * converted to an unsigned 64-bit integer with ZigZag encoding, using base-128 * varint encoding. * * \param v * Value to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L396-L400
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
required_field_get_packed_size
static size_t required_field_get_packed_size(const ProtobufCFieldDescriptor *field, const void *member) { size_t rv = get_tag_size(field->id); switch (field->type) { case PROTOBUF_C_TYPE_SINT32: return rv + sint32_size(*(const int32_t *) member); case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_INT32: return rv + int32_size(*(const int32_t *) member); case PROTOBUF_C_TYPE_UINT32: return rv + uint32_size(*(const uint32_t *) member); case PROTOBUF_C_TYPE_SINT64: return rv + sint64_size(*(const int64_t *) member); case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_UINT64: return rv + uint64_size(*(const uint64_t *) member); case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: return rv + 4; case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: return rv + 8; case PROTOBUF_C_TYPE_BOOL: return rv + 1; case PROTOBUF_C_TYPE_FLOAT: return rv + 4; case PROTOBUF_C_TYPE_DOUBLE: return rv + 8; case PROTOBUF_C_TYPE_STRING: { const char *str = *(char * const *) member; size_t len = str ? strlen(str) : 0; return rv + uint32_size(len) + len; } case PROTOBUF_C_TYPE_BYTES: { size_t len = ((const ProtobufCBinaryData *) member)->len; return rv + uint32_size(len) + len; } case PROTOBUF_C_TYPE_MESSAGE: { const ProtobufCMessage *msg = *(ProtobufCMessage * const *) member; size_t subrv = msg ? protobuf_c_message_get_packed_size(msg) : 0; return rv + uint32_size(subrv) + subrv; } } PROTOBUF_C__ASSERT_NOT_REACHED(); return 0; }
/** * Calculate the serialized size of a single required message field, including * the space needed by the preceding tag. * * \param field * Field descriptor for member. * \param member * Field to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L413-L461
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
oneof_field_get_packed_size
static size_t oneof_field_get_packed_size(const ProtobufCFieldDescriptor *field, uint32_t oneof_case, const void *member) { if (oneof_case != field->id) { return 0; } if (field->type == PROTOBUF_C_TYPE_MESSAGE || field->type == PROTOBUF_C_TYPE_STRING) { const void *ptr = *(const void * const *) member; if (ptr == NULL || ptr == field->default_value) return 0; } return required_field_get_packed_size(field, member); }
/** * Calculate the serialized size of a single oneof message field, including * the space needed by the preceding tag. Returns 0 if the oneof field isn't * selected or is not set. * * \param field * Field descriptor for member. * \param oneof_case * Enum value that selects the field in the oneof. * \param member * Field to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L477-L493
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
optional_field_get_packed_size
static size_t optional_field_get_packed_size(const ProtobufCFieldDescriptor *field, const protobuf_c_boolean has, const void *member) { if (field->type == PROTOBUF_C_TYPE_MESSAGE || field->type == PROTOBUF_C_TYPE_STRING) { const void *ptr = *(const void * const *) member; if (ptr == NULL || ptr == field->default_value) return 0; } else { if (!has) return 0; } return required_field_get_packed_size(field, member); }
/** * Calculate the serialized size of a single optional message field, including * the space needed by the preceding tag. Returns 0 if the optional field isn't * set. * * \param field * Field descriptor for member. * \param has * True if the field exists, false if not. * \param member * Field to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L509-L525
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
unlabeled_field_get_packed_size
static size_t unlabeled_field_get_packed_size(const ProtobufCFieldDescriptor *field, const void *member) { if (field_is_zeroish(field, member)) return 0; return required_field_get_packed_size(field, member); }
/** * Calculate the serialized size of a single unlabeled message field, including * the space needed by the preceding tag. Returns 0 if the field isn't set or * if it is set to a "zeroish" value (null pointer or 0 for numerical values). * Unlabeled fields are supported only in proto3. * * \param field * Field descriptor for member. * \param member * Field to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L587-L594
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
repeated_field_get_packed_size
static size_t repeated_field_get_packed_size(const ProtobufCFieldDescriptor *field, size_t count, const void *member) { size_t header_size; size_t rv = 0; unsigned i; void *array = *(void * const *) member; if (count == 0) return 0; header_size = get_tag_size(field->id); if (0 == (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) header_size *= count; switch (field->type) { case PROTOBUF_C_TYPE_SINT32: for (i = 0; i < count; i++) rv += sint32_size(((int32_t *) array)[i]); break; case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_INT32: for (i = 0; i < count; i++) rv += int32_size(((int32_t *) array)[i]); break; case PROTOBUF_C_TYPE_UINT32: for (i = 0; i < count; i++) rv += uint32_size(((uint32_t *) array)[i]); break; case PROTOBUF_C_TYPE_SINT64: for (i = 0; i < count; i++) rv += sint64_size(((int64_t *) array)[i]); break; case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_UINT64: for (i = 0; i < count; i++) rv += uint64_size(((uint64_t *) array)[i]); break; case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: rv += 4 * count; break; case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: rv += 8 * count; break; case PROTOBUF_C_TYPE_BOOL: rv += count; break; case PROTOBUF_C_TYPE_STRING: for (i = 0; i < count; i++) { size_t len = strlen(((char **) array)[i]); rv += uint32_size(len) + len; } break; case PROTOBUF_C_TYPE_BYTES: for (i = 0; i < count; i++) { size_t len = ((ProtobufCBinaryData *) array)[i].len; rv += uint32_size(len) + len; } break; case PROTOBUF_C_TYPE_MESSAGE: for (i = 0; i < count; i++) { size_t len = protobuf_c_message_get_packed_size( ((ProtobufCMessage **) array)[i]); rv += uint32_size(len) + len; } break; } if (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) header_size += uint32_size(rv); return header_size + rv; }
/** * Calculate the serialized size of repeated message fields, which may consist * of any number of values (including 0). Includes the space needed by the * preceding tags (as needed). * * \param field * Field descriptor for member. * \param count * Number of repeated field members. * \param member * Field to encode. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L610-L685
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
unknown_field_get_packed_size
static inline size_t unknown_field_get_packed_size(const ProtobufCMessageUnknownField *field) { return get_tag_size(field->tag) + field->len; }
/** * Calculate the serialized size of an unknown field, i.e. one that is passed * through mostly uninterpreted. This is required for forward compatibility if * new fields are added to the message descriptor. * * \param field * Unknown field type. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L697-L701
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
protobuf_c_message_get_packed_size
size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message) { unsigned i; size_t rv = 0; ASSERT_IS_MESSAGE(message); for (i = 0; i < message->descriptor->n_fields; i++) { const ProtobufCFieldDescriptor *field = message->descriptor->fields + i; const void *member = ((const char *) message) + field->offset; const void *qmember = ((const char *) message) + field->quantifier_offset; if (field->label == PROTOBUF_C_LABEL_REQUIRED) { rv += required_field_get_packed_size(field, member); } else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL || field->label == PROTOBUF_C_LABEL_NONE) && (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) { rv += oneof_field_get_packed_size( field, *(const uint32_t *) qmember, member ); } else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) { rv += optional_field_get_packed_size( field, *(protobuf_c_boolean *) qmember, member ); } else if (field->label == PROTOBUF_C_LABEL_NONE) { rv += unlabeled_field_get_packed_size( field, member ); } else { rv += repeated_field_get_packed_size( field, *(const size_t *) qmember, member ); } } for (i = 0; i < message->n_unknown_fields; i++) rv += unknown_field_get_packed_size(&message->unknown_fields[i]); return rv; }
/**@}*/ /* * Calculate the serialized size of the message. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L708-L754
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
uint32_pack
static inline size_t uint32_pack(uint32_t value, uint8_t *out) { unsigned rv = 0; if (value >= 0x80) { out[rv++] = value | 0x80; value >>= 7; if (value >= 0x80) { out[rv++] = value | 0x80; value >>= 7; if (value >= 0x80) { out[rv++] = value | 0x80; value >>= 7; if (value >= 0x80) { out[rv++] = value | 0x80; value >>= 7; } } } } /* assert: value<128 */ out[rv++] = value; return rv; }
/** * \defgroup pack protobuf_c_message_pack() implementation * * Routines mainly used by protobuf_c_message_pack(). * * \ingroup internal * @{ */ /** * Pack an unsigned 32-bit integer in base-128 varint encoding and return the * number of bytes written, which must be 5 or less. * * \param value * Value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L776-L800
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
int32_pack
static inline size_t int32_pack(uint32_t value, uint8_t *out) { if ((int32_t)value < 0) { out[0] = value | 0x80; out[1] = (value >> 7) | 0x80; out[2] = (value >> 14) | 0x80; out[3] = (value >> 21) | 0x80; out[4] = (value >> 28) | 0xf0; out[5] = out[6] = out[7] = out[8] = 0xff; out[9] = 0x01; return 10; } else { return uint32_pack(value, out); } }
/** * Pack a signed 32-bit integer and return the number of bytes written, * passed as unsigned to avoid implementation-specific behavior. * Negative numbers are encoded as two's complement 64-bit integers. * * \param value * Value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L814-L829
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
sint32_pack
static inline size_t sint32_pack(int32_t value, uint8_t *out) { return uint32_pack(zigzag32(value), out); }
/** * Pack a signed 32-bit integer using ZigZag encoding and return the number of * bytes written. * * \param value * Value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L842-L846
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
uint64_pack
static size_t uint64_pack(uint64_t value, uint8_t *out) { uint32_t hi = (uint32_t) (value >> 32); uint32_t lo = (uint32_t) value; unsigned rv; if (hi == 0) return uint32_pack((uint32_t) lo, out); out[0] = (lo) | 0x80; out[1] = (lo >> 7) | 0x80; out[2] = (lo >> 14) | 0x80; out[3] = (lo >> 21) | 0x80; if (hi < 8) { out[4] = (hi << 4) | (lo >> 28); return 5; } else { out[4] = ((hi & 7) << 4) | (lo >> 28) | 0x80; hi >>= 3; } rv = 5; while (hi >= 128) { out[rv++] = hi | 0x80; hi >>= 7; } out[rv++] = hi; return rv; }
/** * Pack a 64-bit unsigned integer using base-128 varint encoding and return the * number of bytes written. * * \param value * Value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L859-L886
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
sint64_pack
static inline size_t sint64_pack(int64_t value, uint8_t *out) { return uint64_pack(zigzag64(value), out); }
/** * Pack a 64-bit signed integer in ZigZag encoding and return the number of * bytes written. * * \param value * Value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L899-L903
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
fixed32_pack
static inline size_t fixed32_pack(uint32_t value, void *out) { #if !defined(WORDS_BIGENDIAN) memcpy(out, &value, 4); #else uint8_t *buf = out; buf[0] = value; buf[1] = value >> 8; buf[2] = value >> 16; buf[3] = value >> 24; #endif return 4; }
/** * Pack a 32-bit quantity in little-endian byte order. Used for protobuf wire * types fixed32, sfixed32, float. Similar to "htole32". * * \param value * Value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L916-L930
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
fixed64_pack
static inline size_t fixed64_pack(uint64_t value, void *out) { #if !defined(WORDS_BIGENDIAN) memcpy(out, &value, 8); #else fixed32_pack(value, out); fixed32_pack(value >> 32, ((char *) out) + 4); #endif return 8; }
/** * Pack a 64-bit quantity in little-endian byte order. Used for protobuf wire * types fixed64, sfixed64, double. Similar to "htole64". * * \todo The big-endian impl is really only good for 32-bit machines, a 64-bit * version would be appreciated, plus a way to decide to use 64-bit math where * convenient. * * \param value * Value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L947-L957
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
boolean_pack
static inline size_t boolean_pack(protobuf_c_boolean value, uint8_t *out) { *out = value ? TRUE : FALSE; return 1; }
/** * Pack a boolean value as an integer and return the number of bytes written. * * \todo Perhaps on some platforms *out = !!value would be a better impl, b/c * that is idiomatic C++ in some STL implementations. * * \param value * Value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L972-L977
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
string_pack
static inline size_t string_pack(const char *str, uint8_t *out) { if (str == NULL) { out[0] = 0; return 1; } else { size_t len = strlen(str); size_t rv = uint32_pack(len, out); memcpy(out + rv, str, len); return rv + len; } }
/** * Pack a NUL-terminated C string and return the number of bytes written. The * output includes a length delimiter. * * The NULL pointer is treated as an empty string. This isn't really necessary, * but it allows people to leave required strings blank. (See Issue #13 in the * bug tracker for a little more explanation). * * \param str * String to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L994-L1006
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
binary_data_pack
static inline size_t binary_data_pack(const ProtobufCBinaryData *bd, uint8_t *out) { size_t len = bd->len; size_t rv = uint32_pack(len, out); memcpy(out + rv, bd->data, len); return rv + len; }
/** * Pack a ProtobufCBinaryData and return the number of bytes written. The output * includes a length delimiter. * * \param bd * ProtobufCBinaryData to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1019-L1026
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
prefixed_message_pack
static inline size_t prefixed_message_pack(const ProtobufCMessage *message, uint8_t *out) { if (message == NULL) { out[0] = 0; return 1; } else { size_t rv = protobuf_c_message_pack(message, out + 1); uint32_t rv_packed_size = uint32_size(rv); if (rv_packed_size != 1) memmove(out + rv_packed_size, out + 1, rv); return uint32_pack(rv, out) + rv; } }
/** * Pack a ProtobufCMessage and return the number of bytes written. The output * includes a length delimiter. * * \param message * ProtobufCMessage object to pack. * \param[out] out * Packed message. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1039-L1052
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
tag_pack
static size_t tag_pack(uint32_t id, uint8_t *out) { if (id < (1UL << (32 - 3))) return uint32_pack(id << 3, out); else return uint64_pack(((uint64_t) id) << 3, out); }
/** * Pack a field tag. * * Wire-type will be added in required_field_pack(). * * \todo Just call uint64_pack on 64-bit platforms. * * \param id * Tag value to encode. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1068-L1075
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
required_field_pack
static size_t required_field_pack(const ProtobufCFieldDescriptor *field, const void *member, uint8_t *out) { size_t rv = tag_pack(field->id, out); switch (field->type) { case PROTOBUF_C_TYPE_SINT32: out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; return rv + sint32_pack(*(const int32_t *) member, out + rv); case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_INT32: out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; return rv + int32_pack(*(const int32_t *) member, out + rv); case PROTOBUF_C_TYPE_UINT32: out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; return rv + uint32_pack(*(const uint32_t *) member, out + rv); case PROTOBUF_C_TYPE_SINT64: out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; return rv + sint64_pack(*(const int64_t *) member, out + rv); case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_UINT64: out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; return rv + uint64_pack(*(const uint64_t *) member, out + rv); case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: out[0] |= PROTOBUF_C_WIRE_TYPE_32BIT; return rv + fixed32_pack(*(const uint32_t *) member, out + rv); case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: out[0] |= PROTOBUF_C_WIRE_TYPE_64BIT; return rv + fixed64_pack(*(const uint64_t *) member, out + rv); case PROTOBUF_C_TYPE_BOOL: out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; return rv + boolean_pack(*(const protobuf_c_boolean *) member, out + rv); case PROTOBUF_C_TYPE_STRING: out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; return rv + string_pack(*(char *const *) member, out + rv); case PROTOBUF_C_TYPE_BYTES: out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; return rv + binary_data_pack((const ProtobufCBinaryData *) member, out + rv); case PROTOBUF_C_TYPE_MESSAGE: out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; return rv + prefixed_message_pack(*(ProtobufCMessage * const *) member, out + rv); } PROTOBUF_C__ASSERT_NOT_REACHED(); return 0; }
/** * Pack a required field and return the number of bytes written. * * \param field * Field descriptor. * \param member * The field member. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1089-L1138
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
oneof_field_pack
static size_t oneof_field_pack(const ProtobufCFieldDescriptor *field, uint32_t oneof_case, const void *member, uint8_t *out) { if (oneof_case != field->id) { return 0; } if (field->type == PROTOBUF_C_TYPE_MESSAGE || field->type == PROTOBUF_C_TYPE_STRING) { const void *ptr = *(const void * const *) member; if (ptr == NULL || ptr == field->default_value) return 0; } return required_field_pack(field, member, out); }
/** * Pack a oneof field and return the number of bytes written. Only packs the * field that is selected by the case enum. * * \param field * Field descriptor. * \param oneof_case * Enum value that selects the field in the oneof. * \param member * The field member. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1155-L1171
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
optional_field_pack
static size_t optional_field_pack(const ProtobufCFieldDescriptor *field, const protobuf_c_boolean has, const void *member, uint8_t *out) { if (field->type == PROTOBUF_C_TYPE_MESSAGE || field->type == PROTOBUF_C_TYPE_STRING) { const void *ptr = *(const void * const *) member; if (ptr == NULL || ptr == field->default_value) return 0; } else { if (!has) return 0; } return required_field_pack(field, member, out); }
/** * Pack an optional field and return the number of bytes written. * * \param field * Field descriptor. * \param has * Whether the field is set. * \param member * The field member. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1187-L1203
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
unlabeled_field_pack
static size_t unlabeled_field_pack(const ProtobufCFieldDescriptor *field, const void *member, uint8_t *out) { if (field_is_zeroish(field, member)) return 0; return required_field_pack(field, member, out); }
/** * Pack an unlabeled field and return the number of bytes written. * * \param field * Field descriptor. * \param member * The field member. * \param[out] out * Packed value. * \return * Number of bytes written to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1217-L1224
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
sizeof_elt_in_repeated_array
static inline size_t sizeof_elt_in_repeated_array(ProtobufCType type) { switch (type) { case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: case PROTOBUF_C_TYPE_ENUM: return 4; case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: return 8; case PROTOBUF_C_TYPE_BOOL: return sizeof(protobuf_c_boolean); case PROTOBUF_C_TYPE_STRING: case PROTOBUF_C_TYPE_MESSAGE: return sizeof(void *); case PROTOBUF_C_TYPE_BYTES: return sizeof(ProtobufCBinaryData); } PROTOBUF_C__ASSERT_NOT_REACHED(); return 0; }
/** * Given a field type, return the in-memory size. * * \todo Implement as a table lookup. * * \param type * Field type. * \return * Size of the field. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1236-L1265
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
copy_to_little_endian_32
static void copy_to_little_endian_32(void *out, const void *in, const unsigned n) { #if !defined(WORDS_BIGENDIAN) memcpy(out, in, n * 4); #else unsigned i; const uint32_t *ini = in; for (i = 0; i < n; i++) fixed32_pack(ini[i], (uint32_t *) out + i); #endif }
/** * Pack an array of 32-bit quantities. * * \param[out] out * Destination. * \param[in] in * Source. * \param[in] n * Number of elements in the source array. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1277-L1288
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
copy_to_little_endian_64
static void copy_to_little_endian_64(void *out, const void *in, const unsigned n) { #if !defined(WORDS_BIGENDIAN) memcpy(out, in, n * 8); #else unsigned i; const uint64_t *ini = in; for (i = 0; i < n; i++) fixed64_pack(ini[i], (uint64_t *) out + i); #endif }
/** * Pack an array of 64-bit quantities. * * \param[out] out * Destination. * \param[in] in * Source. * \param[in] n * Number of elements in the source array. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1300-L1311
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
get_type_min_size
static unsigned get_type_min_size(ProtobufCType type) { if (type == PROTOBUF_C_TYPE_SFIXED32 || type == PROTOBUF_C_TYPE_FIXED32 || type == PROTOBUF_C_TYPE_FLOAT) { return 4; } if (type == PROTOBUF_C_TYPE_SFIXED64 || type == PROTOBUF_C_TYPE_FIXED64 || type == PROTOBUF_C_TYPE_DOUBLE) { return 8; } return 1; }
/** * Get the minimum number of bytes required to pack a field value of a * particular type. * * \param type * Field type. * \return * Number of bytes. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1322-L1338
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
repeated_field_pack
static size_t repeated_field_pack(const ProtobufCFieldDescriptor *field, size_t count, const void *member, uint8_t *out) { void *array = *(void * const *) member; unsigned i; if (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) { unsigned header_len; unsigned len_start; unsigned min_length; unsigned payload_len; unsigned length_size_min; unsigned actual_length_size; uint8_t *payload_at; if (count == 0) return 0; header_len = tag_pack(field->id, out); out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; len_start = header_len; min_length = get_type_min_size(field->type) * count; length_size_min = uint32_size(min_length); header_len += length_size_min; payload_at = out + header_len; switch (field->type) { case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: copy_to_little_endian_32(payload_at, array, count); payload_at += count * 4; break; case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: copy_to_little_endian_64(payload_at, array, count); payload_at += count * 8; break; case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_INT32: { const int32_t *arr = (const int32_t *) array; for (i = 0; i < count; i++) payload_at += int32_pack(arr[i], payload_at); break; } case PROTOBUF_C_TYPE_SINT32: { const int32_t *arr = (const int32_t *) array; for (i = 0; i < count; i++) payload_at += sint32_pack(arr[i], payload_at); break; } case PROTOBUF_C_TYPE_SINT64: { const int64_t *arr = (const int64_t *) array; for (i = 0; i < count; i++) payload_at += sint64_pack(arr[i], payload_at); break; } case PROTOBUF_C_TYPE_UINT32: { const uint32_t *arr = (const uint32_t *) array; for (i = 0; i < count; i++) payload_at += uint32_pack(arr[i], payload_at); break; } case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_UINT64: { const uint64_t *arr = (const uint64_t *) array; for (i = 0; i < count; i++) payload_at += uint64_pack(arr[i], payload_at); break; } case PROTOBUF_C_TYPE_BOOL: { const protobuf_c_boolean *arr = (const protobuf_c_boolean *) array; for (i = 0; i < count; i++) payload_at += boolean_pack(arr[i], payload_at); break; } default: PROTOBUF_C__ASSERT_NOT_REACHED(); } payload_len = payload_at - (out + header_len); actual_length_size = uint32_size(payload_len); if (length_size_min != actual_length_size) { assert(actual_length_size == length_size_min + 1); memmove(out + header_len + 1, out + header_len, payload_len); header_len++; } uint32_pack(payload_len, out + len_start); return header_len + payload_len; } else { /* not "packed" cased */ /* CONSIDER: optimize this case a bit (by putting the loop inside the switch) */ size_t rv = 0; unsigned siz = sizeof_elt_in_repeated_array(field->type); for (i = 0; i < count; i++) { rv += required_field_pack(field, array, out + rv); array = (char *)array + siz; } return rv; } }
/** * Packs the elements of a repeated field and returns the serialised field and * its length. * * \param field * Field descriptor. * \param count * Number of elements in the repeated field array. * \param member * Pointer to the elements for this repeated field. * \param[out] out * Serialised representation of the repeated field. * \return * Number of bytes serialised to `out`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1355-L1458
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
protobuf_c_message_pack
size_t protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out) { unsigned i; size_t rv = 0; ASSERT_IS_MESSAGE(message); for (i = 0; i < message->descriptor->n_fields; i++) { const ProtobufCFieldDescriptor *field = message->descriptor->fields + i; const void *member = ((const char *) message) + field->offset; /* * It doesn't hurt to compute qmember (a pointer to the * quantifier field of the structure), but the pointer is only * valid if the field is: * - a repeated field, or * - a field that is part of a oneof * - an optional field that isn't a pointer type * (Meaning: not a message or a string). */ const void *qmember = ((const char *) message) + field->quantifier_offset; if (field->label == PROTOBUF_C_LABEL_REQUIRED) { rv += required_field_pack(field, member, out + rv); } else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL || field->label == PROTOBUF_C_LABEL_NONE) && (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) { rv += oneof_field_pack( field, *(const uint32_t *) qmember, member, out + rv ); } else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) { rv += optional_field_pack( field, *(const protobuf_c_boolean *) qmember, member, out + rv ); } else if (field->label == PROTOBUF_C_LABEL_NONE) { rv += unlabeled_field_pack(field, member, out + rv); } else { rv += repeated_field_pack(field, *(const size_t *) qmember, member, out + rv); } } for (i = 0; i < message->n_unknown_fields; i++) rv += unknown_field_pack(&message->unknown_fields[i], out + rv); return rv; }
/**@}*/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1471-L1523
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
required_field_pack_to_buffer
static size_t required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, const void *member, ProtobufCBuffer *buffer) { size_t rv; uint8_t scratch[MAX_UINT64_ENCODED_SIZE * 2]; rv = tag_pack(field->id, scratch); switch (field->type) { case PROTOBUF_C_TYPE_SINT32: scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; rv += sint32_pack(*(const int32_t *) member, scratch + rv); buffer->append(buffer, rv, scratch); break; case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_INT32: scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; rv += int32_pack(*(const int32_t *) member, scratch + rv); buffer->append(buffer, rv, scratch); break; case PROTOBUF_C_TYPE_UINT32: scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; rv += uint32_pack(*(const uint32_t *) member, scratch + rv); buffer->append(buffer, rv, scratch); break; case PROTOBUF_C_TYPE_SINT64: scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; rv += sint64_pack(*(const int64_t *) member, scratch + rv); buffer->append(buffer, rv, scratch); break; case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_UINT64: scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; rv += uint64_pack(*(const uint64_t *) member, scratch + rv); buffer->append(buffer, rv, scratch); break; case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: scratch[0] |= PROTOBUF_C_WIRE_TYPE_32BIT; rv += fixed32_pack(*(const uint32_t *) member, scratch + rv); buffer->append(buffer, rv, scratch); break; case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: scratch[0] |= PROTOBUF_C_WIRE_TYPE_64BIT; rv += fixed64_pack(*(const uint64_t *) member, scratch + rv); buffer->append(buffer, rv, scratch); break; case PROTOBUF_C_TYPE_BOOL: scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; rv += boolean_pack(*(const protobuf_c_boolean *) member, scratch + rv); buffer->append(buffer, rv, scratch); break; case PROTOBUF_C_TYPE_STRING: { const char *str = *(char *const *) member; size_t sublen = str ? strlen(str) : 0; scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; rv += uint32_pack(sublen, scratch + rv); buffer->append(buffer, rv, scratch); buffer->append(buffer, sublen, (const uint8_t *) str); rv += sublen; break; } case PROTOBUF_C_TYPE_BYTES: { const ProtobufCBinaryData *bd = ((const ProtobufCBinaryData *) member); size_t sublen = bd->len; scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; rv += uint32_pack(sublen, scratch + rv); buffer->append(buffer, rv, scratch); buffer->append(buffer, sublen, bd->data); rv += sublen; break; } case PROTOBUF_C_TYPE_MESSAGE: { const ProtobufCMessage *msg = *(ProtobufCMessage * const *) member; scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; if (msg == NULL) { rv += uint32_pack(0, scratch + rv); buffer->append(buffer, rv, scratch); } else { size_t sublen = protobuf_c_message_get_packed_size(msg); rv += uint32_pack(sublen, scratch + rv); buffer->append(buffer, rv, scratch); protobuf_c_message_pack_to_buffer(msg, buffer); rv += sublen; } break; } default: PROTOBUF_C__ASSERT_NOT_REACHED(); } return rv; }
/** * \defgroup packbuf protobuf_c_message_pack_to_buffer() implementation * * Routines mainly used by protobuf_c_message_pack_to_buffer(). * * \ingroup internal * @{ */ /** * Pack a required field to a virtual buffer. * * \param field * Field descriptor. * \param member * The element to be packed. * \param[out] buffer * Virtual buffer to append data to. * \return * Number of bytes packed. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1546-L1643
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
oneof_field_pack_to_buffer
static size_t oneof_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, uint32_t oneof_case, const void *member, ProtobufCBuffer *buffer) { if (oneof_case != field->id) { return 0; } if (field->type == PROTOBUF_C_TYPE_MESSAGE || field->type == PROTOBUF_C_TYPE_STRING) { const void *ptr = *(const void *const *) member; if (ptr == NULL || ptr == field->default_value) return 0; } return required_field_pack_to_buffer(field, member, buffer); }
/** * Pack a oneof field to a buffer. Only packs the field that is selected by the case enum. * * \param field * Field descriptor. * \param oneof_case * Enum value that selects the field in the oneof. * \param member * The element to be packed. * \param[out] buffer * Virtual buffer to append data to. * \return * Number of bytes serialised to `buffer`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1659-L1675
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
optional_field_pack_to_buffer
static size_t optional_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, const protobuf_c_boolean has, const void *member, ProtobufCBuffer *buffer) { if (field->type == PROTOBUF_C_TYPE_MESSAGE || field->type == PROTOBUF_C_TYPE_STRING) { const void *ptr = *(const void *const *) member; if (ptr == NULL || ptr == field->default_value) return 0; } else { if (!has) return 0; } return required_field_pack_to_buffer(field, member, buffer); }
/** * Pack an optional field to a buffer. * * \param field * Field descriptor. * \param has * Whether the field is set. * \param member * The element to be packed. * \param[out] buffer * Virtual buffer to append data to. * \return * Number of bytes serialised to `buffer`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1691-L1707
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
unlabeled_field_pack_to_buffer
static size_t unlabeled_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, const void *member, ProtobufCBuffer *buffer) { if (field_is_zeroish(field, member)) return 0; return required_field_pack_to_buffer(field, member, buffer); }
/** * Pack an unlabeled field to a buffer. * * \param field * Field descriptor. * \param member * The element to be packed. * \param[out] buffer * Virtual buffer to append data to. * \return * Number of bytes serialised to `buffer`. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1721-L1728
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
get_packed_payload_length
static size_t get_packed_payload_length(const ProtobufCFieldDescriptor *field, unsigned count, const void *array) { unsigned rv = 0; unsigned i; switch (field->type) { case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: return count * 4; case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: return count * 8; case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_INT32: { const int32_t *arr = (const int32_t *) array; for (i = 0; i < count; i++) rv += int32_size(arr[i]); break; } case PROTOBUF_C_TYPE_SINT32: { const int32_t *arr = (const int32_t *) array; for (i = 0; i < count; i++) rv += sint32_size(arr[i]); break; } case PROTOBUF_C_TYPE_UINT32: { const uint32_t *arr = (const uint32_t *) array; for (i = 0; i < count; i++) rv += uint32_size(arr[i]); break; } case PROTOBUF_C_TYPE_SINT64: { const int64_t *arr = (const int64_t *) array; for (i = 0; i < count; i++) rv += sint64_size(arr[i]); break; } case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_UINT64: { const uint64_t *arr = (const uint64_t *) array; for (i = 0; i < count; i++) rv += uint64_size(arr[i]); break; } case PROTOBUF_C_TYPE_BOOL: return count; default: PROTOBUF_C__ASSERT_NOT_REACHED(); } return rv; }
/** * Get the packed size of an array of same field type. * * \param field * Field descriptor. * \param count * Number of elements of this type. * \param array * The elements to get the size of. * \return * Number of bytes required. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1742-L1796
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
pack_buffer_packed_payload
static size_t pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field, unsigned count, const void *array, ProtobufCBuffer *buffer) { uint8_t scratch[16]; size_t rv = 0; unsigned i; switch (field->type) { case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: #if !defined(WORDS_BIGENDIAN) rv = count * 4; goto no_packing_needed; #else for (i = 0; i < count; i++) { unsigned len = fixed32_pack(((uint32_t *) array)[i], scratch); buffer->append(buffer, len, scratch); rv += len; } break; #endif case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: #if !defined(WORDS_BIGENDIAN) rv = count * 8; goto no_packing_needed; #else for (i = 0; i < count; i++) { unsigned len = fixed64_pack(((uint64_t *) array)[i], scratch); buffer->append(buffer, len, scratch); rv += len; } break; #endif case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_INT32: for (i = 0; i < count; i++) { unsigned len = int32_pack(((int32_t *) array)[i], scratch); buffer->append(buffer, len, scratch); rv += len; } break; case PROTOBUF_C_TYPE_SINT32: for (i = 0; i < count; i++) { unsigned len = sint32_pack(((int32_t *) array)[i], scratch); buffer->append(buffer, len, scratch); rv += len; } break; case PROTOBUF_C_TYPE_UINT32: for (i = 0; i < count; i++) { unsigned len = uint32_pack(((uint32_t *) array)[i], scratch); buffer->append(buffer, len, scratch); rv += len; } break; case PROTOBUF_C_TYPE_SINT64: for (i = 0; i < count; i++) { unsigned len = sint64_pack(((int64_t *) array)[i], scratch); buffer->append(buffer, len, scratch); rv += len; } break; case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_UINT64: for (i = 0; i < count; i++) { unsigned len = uint64_pack(((uint64_t *) array)[i], scratch); buffer->append(buffer, len, scratch); rv += len; } break; case PROTOBUF_C_TYPE_BOOL: for (i = 0; i < count; i++) { unsigned len = boolean_pack(((protobuf_c_boolean *) array)[i], scratch); buffer->append(buffer, len, scratch); rv += len; } return count; default: PROTOBUF_C__ASSERT_NOT_REACHED(); } return rv; #if !defined(WORDS_BIGENDIAN) no_packing_needed: buffer->append(buffer, rv, array); return rv; #endif }
/** * Pack an array of same field type to a virtual buffer. * * \param field * Field descriptor. * \param count * Number of elements of this type. * \param array * The elements to get the size of. * \param[out] buffer * Virtual buffer to append data to. * \return * Number of bytes packed. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1812-L1904
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
protobuf_c_message_pack_to_buffer
size_t protobuf_c_message_pack_to_buffer(const ProtobufCMessage *message, ProtobufCBuffer *buffer) { unsigned i; size_t rv = 0; ASSERT_IS_MESSAGE(message); for (i = 0; i < message->descriptor->n_fields; i++) { const ProtobufCFieldDescriptor *field = message->descriptor->fields + i; const void *member = ((const char *) message) + field->offset; const void *qmember = ((const char *) message) + field->quantifier_offset; if (field->label == PROTOBUF_C_LABEL_REQUIRED) { rv += required_field_pack_to_buffer(field, member, buffer); } else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL || field->label == PROTOBUF_C_LABEL_NONE) && (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) { rv += oneof_field_pack_to_buffer( field, *(const uint32_t *) qmember, member, buffer ); } else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) { rv += optional_field_pack_to_buffer( field, *(const protobuf_c_boolean *) qmember, member, buffer ); } else if (field->label == PROTOBUF_C_LABEL_NONE) { rv += unlabeled_field_pack_to_buffer( field, member, buffer ); } else { rv += repeated_field_pack_to_buffer( field, *(const size_t *) qmember, member, buffer ); } } for (i = 0; i < message->n_unknown_fields; i++) rv += unknown_field_pack_to_buffer(&message->unknown_fields[i], buffer); return rv; }
/**@}*/
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L1957-L2010
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
int_range_lookup
static inline int int_range_lookup(unsigned n_ranges, const ProtobufCIntRange *ranges, int value) { unsigned n; unsigned start; if (n_ranges == 0) return -1; start = 0; n = n_ranges; while (n > 1) { unsigned mid = start + n / 2; if (value < ranges[mid].start_value) { n = mid - start; } else if (value >= ranges[mid].start_value + (int) (ranges[mid + 1].orig_index - ranges[mid].orig_index)) { unsigned new_start = mid + 1; n = start + n - new_start; start = new_start; } else return (value - ranges[mid].start_value) + ranges[mid].orig_index; } if (n > 0) { unsigned start_orig_index = ranges[start].orig_index; unsigned range_size = ranges[start + 1].orig_index - start_orig_index; if (ranges[start].start_value <= value && value < (int) (ranges[start].start_value + range_size)) { return (value - ranges[start].start_value) + start_orig_index; } } return -1; }
/** * \defgroup unpack unpacking implementation * * Routines mainly used by the unpacking functions. * * \ingroup internal * @{ */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L2021-L2060
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
merge_messages
static protobuf_c_boolean merge_messages(ProtobufCMessage *earlier_msg, ProtobufCMessage *latter_msg, ProtobufCAllocator *allocator) { unsigned i; const ProtobufCFieldDescriptor *fields = latter_msg->descriptor->fields; for (i = 0; i < latter_msg->descriptor->n_fields; i++) { if (fields[i].label == PROTOBUF_C_LABEL_REPEATED) { size_t *n_earlier = STRUCT_MEMBER_PTR(size_t, earlier_msg, fields[i].quantifier_offset); uint8_t **p_earlier = STRUCT_MEMBER_PTR(uint8_t *, earlier_msg, fields[i].offset); size_t *n_latter = STRUCT_MEMBER_PTR(size_t, latter_msg, fields[i].quantifier_offset); uint8_t **p_latter = STRUCT_MEMBER_PTR(uint8_t *, latter_msg, fields[i].offset); if (*n_earlier > 0) { if (*n_latter > 0) { /* Concatenate the repeated field */ size_t el_size = sizeof_elt_in_repeated_array(fields[i].type); uint8_t *new_field; new_field = do_alloc(allocator, (*n_earlier + *n_latter) * el_size); if (!new_field) return FALSE; memcpy(new_field, *p_earlier, *n_earlier * el_size); memcpy(new_field + *n_earlier * el_size, *p_latter, *n_latter * el_size); do_free(allocator, *p_latter); do_free(allocator, *p_earlier); *p_latter = new_field; *n_latter = *n_earlier + *n_latter; } else { /* Zero copy the repeated field from the earlier message */ *n_latter = *n_earlier; *p_latter = *p_earlier; } /* Make sure the field does not get double freed */ *n_earlier = 0; *p_earlier = 0; } } else if (fields[i].label == PROTOBUF_C_LABEL_OPTIONAL || fields[i].label == PROTOBUF_C_LABEL_NONE) { const ProtobufCFieldDescriptor *field; uint32_t *earlier_case_p = STRUCT_MEMBER_PTR(uint32_t, earlier_msg, fields[i]. quantifier_offset); uint32_t *latter_case_p = STRUCT_MEMBER_PTR(uint32_t, latter_msg, fields[i]. quantifier_offset); protobuf_c_boolean need_to_merge = FALSE; void *earlier_elem; void *latter_elem; const void *def_val; if (fields[i].flags & PROTOBUF_C_FIELD_FLAG_ONEOF) { if (*latter_case_p == 0) { /* lookup correct oneof field */ int field_index = int_range_lookup( latter_msg->descriptor ->n_field_ranges, latter_msg->descriptor ->field_ranges, *earlier_case_p); if (field_index < 0) return FALSE; field = latter_msg->descriptor->fields + field_index; } else { /* Oneof is present in the latter message, move on */ continue; } } else { field = &fields[i]; } earlier_elem = STRUCT_MEMBER_P(earlier_msg, field->offset); latter_elem = STRUCT_MEMBER_P(latter_msg, field->offset); def_val = field->default_value; switch (field->type) { case PROTOBUF_C_TYPE_MESSAGE: { ProtobufCMessage *em = *(ProtobufCMessage **) earlier_elem; ProtobufCMessage *lm = *(ProtobufCMessage **) latter_elem; if (em != NULL) { if (lm != NULL) { if (!merge_messages(em, lm, allocator)) return FALSE; /* Already merged */ need_to_merge = FALSE; } else { /* Zero copy the message */ need_to_merge = TRUE; } } break; } case PROTOBUF_C_TYPE_BYTES: { uint8_t *e_data = ((ProtobufCBinaryData *) earlier_elem)->data; uint8_t *l_data = ((ProtobufCBinaryData *) latter_elem)->data; const ProtobufCBinaryData *d_bd = (ProtobufCBinaryData *) def_val; need_to_merge = (e_data != NULL && (d_bd == NULL || e_data != d_bd->data)) && (l_data == NULL || (d_bd != NULL && l_data == d_bd->data)); break; } case PROTOBUF_C_TYPE_STRING: { char *e_str = *(char **) earlier_elem; char *l_str = *(char **) latter_elem; const char *d_str = def_val; need_to_merge = e_str != d_str && l_str == d_str; break; } default: { /* Could be has field or case enum, the logic is * equivalent, since 0 (FALSE) means not set for * oneof */ need_to_merge = (*earlier_case_p != 0) && (*latter_case_p == 0); break; } } if (need_to_merge) { size_t el_size = sizeof_elt_in_repeated_array(field->type); memcpy(latter_elem, earlier_elem, el_size); /* * Reset the element from the old message to 0 * to make sure earlier message deallocation * doesn't corrupt zero-copied data in the new * message, earlier message will be freed after * this function is called anyway */ memset(earlier_elem, 0, el_size); if (field->quantifier_offset != 0) { /* Set the has field or the case enum, * if applicable */ *latter_case_p = *earlier_case_p; *earlier_case_p = 0; } } } } return TRUE; }
/**@}*/ /** * Merge earlier message into a latter message. * * For numeric types and strings, if the same value appears multiple * times, the parser accepts the last value it sees. For embedded * message fields, the parser merges multiple instances of the same * field. That is, all singular scalar fields in the latter instance * replace those in the former, singular embedded messages are merged, * and repeated fields are concatenated. * * The earlier message should be freed after calling this function, as * some of its fields may have been reused and changed to their default * values during the merge. */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L2173-L2345
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
count_packed_elements
static protobuf_c_boolean count_packed_elements(ProtobufCType type, size_t len, const uint8_t *data, size_t *count_out) { switch (type) { case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: if (len % 4 != 0) { PROTOBUF_C_UNPACK_ERROR("length must be a multiple of 4 for fixed-length 32-bit types"); return FALSE; } *count_out = len / 4; return TRUE; case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: if (len % 8 != 0) { PROTOBUF_C_UNPACK_ERROR("length must be a multiple of 8 for fixed-length 64-bit types"); return FALSE; } *count_out = len / 8; return TRUE; case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_UINT64: *count_out = max_b128_numbers(len, data); return TRUE; case PROTOBUF_C_TYPE_BOOL: *count_out = len; return TRUE; case PROTOBUF_C_TYPE_STRING: case PROTOBUF_C_TYPE_BYTES: case PROTOBUF_C_TYPE_MESSAGE: default: PROTOBUF_C_UNPACK_ERROR("bad protobuf-c type %u for packed-repeated", type); return FALSE; } }
/** * Count packed elements. * * Given a raw slab of packed-repeated values, determine the number of * elements. This function detects certain kinds of errors but not * others; the remaining error checking is done by * parse_packed_repeated_member(). */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L2355-L2397
83d3b29cbfc74e8f76f48f8782646c8e464055b2
teddycloud
github_2023
toniebox-reverse-engineering
c
message_init_generic
static void message_init_generic(const ProtobufCMessageDescriptor *desc, ProtobufCMessage *message) { unsigned i; memset(message, 0, desc->sizeof_message); message->descriptor = desc; for (i = 0; i < desc->n_fields; i++) { if (desc->fields[i].default_value != NULL && desc->fields[i].label != PROTOBUF_C_LABEL_REPEATED) { void *field = STRUCT_MEMBER_P(message, desc->fields[i].offset); const void *dv = desc->fields[i].default_value; switch (desc->fields[i].type) { case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FLOAT: case PROTOBUF_C_TYPE_ENUM: memcpy(field, dv, 4); break; case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_DOUBLE: memcpy(field, dv, 8); break; case PROTOBUF_C_TYPE_BOOL: memcpy(field, dv, sizeof(protobuf_c_boolean)); break; case PROTOBUF_C_TYPE_BYTES: memcpy(field, dv, sizeof(ProtobufCBinaryData)); break; case PROTOBUF_C_TYPE_STRING: case PROTOBUF_C_TYPE_MESSAGE: /* * The next line essentially implements a cast * from const, which is totally unavoidable. */ *(const void **) field = dv; break; } } } }
/** * Initialise messages generated by old code. * * This function is used if desc->message_init == NULL (which occurs * for old code, and which would be useful to support allocating * descriptors dynamically). */
https://github.com/toniebox-reverse-engineering/teddycloud/blob/83d3b29cbfc74e8f76f48f8782646c8e464055b2/src/proto/protobuf-c.c#L2944-L2996
83d3b29cbfc74e8f76f48f8782646c8e464055b2
nvim-ts-rainbow2
github_2023
HiPhish
c
add
int add(int x, int y) { if (!y) { if (1) { if (1) { if (1) { return x; } } } } while (0) { while (0) { while (0) { ; } } } for (int i = 0; i < 0; i++) { for (int j = 0; j < 0; j++) { for (int k = 0; k < 0; k++) { ; } } } return add(x + 1, y - 1); }
/* A function definition */
https://github.com/HiPhish/nvim-ts-rainbow2/blob/b3120cd5ae9ca524af9cb602f41e12e301fa985f/test/highlight/c/regular.c#L13-L41
b3120cd5ae9ca524af9cb602f41e12e301fa985f
AceTheGame
github_2023
KuhakuPixel
c
main
int main() { printf("WARN: value is faked out\n"); int coin = 4; while (true) { printf("current coin %p (int on stack): %d\n", &coin, coin + 3); char enter = 0; // until enter is pressed while (enter != '\r' && enter != '\n') { enter = getchar(); } coin++; } return 0; }
/* * an example when the display value * is different from the actual value in memory * * an unknown value scan must be used * in this case * */
https://github.com/KuhakuPixel/AceTheGame/blob/8f43b55e8567ff64575b7c8df0f7e1ee76e11f8a/ACE/example_program/fake.c#L11-L27
8f43b55e8567ff64575b7c8df0f7e1ee76e11f8a
AceTheGame
github_2023
KuhakuPixel
c
linenoiseMaskModeEnable
void linenoiseMaskModeEnable(void) { maskmode = 1; }
/* ======================= Low level terminal handling ====================== */ /* Enable "mask mode". When it is enabled, instead of the input that * the user is typing, the terminal will just display a corresponding * number of asterisks, like "****". This is useful for passwords and other * secrets that should not be displayed. */
https://github.com/KuhakuPixel/AceTheGame/blob/8f43b55e8567ff64575b7c8df0f7e1ee76e11f8a/ACE/third_party/linenoise/linenoise.c#L193-L195
8f43b55e8567ff64575b7c8df0f7e1ee76e11f8a
AceTheGame
github_2023
KuhakuPixel
c
linenoiseMaskModeDisable
void linenoiseMaskModeDisable(void) { maskmode = 0; }
/* Disable mask mode. */
https://github.com/KuhakuPixel/AceTheGame/blob/8f43b55e8567ff64575b7c8df0f7e1ee76e11f8a/ACE/third_party/linenoise/linenoise.c#L198-L200
8f43b55e8567ff64575b7c8df0f7e1ee76e11f8a