Use modern string parsing functions in RtspParser

This commit is contained in:
Cameron Gutman
2023-09-12 20:53:04 -05:00
parent 91b4186b8a
commit 5a19e52921
+50 -24
View File
@@ -27,7 +27,7 @@ static int getMessageLength(PRTSP_MESSAGE msg) {
// Add length of response-specific strings // Add length of response-specific strings
else { else {
char statusCodeStr[16]; char statusCodeStr[16];
sprintf(statusCodeStr, "%d", msg->message.response.statusCode); snprintf(statusCodeStr, sizeof(statusCodeStr), "%d", msg->message.response.statusCode);
count += strlen(statusCodeStr); count += strlen(statusCodeStr);
count += strlen(msg->message.response.statusString); count += strlen(msg->message.response.statusString);
// two spaces and \r\n // two spaces and \r\n
@@ -320,9 +320,22 @@ void freeOptionList(POPTION_ITEM optionsHead) {
} }
} }
bool appendString(char* dest, int* destOffset, int* destRemainingLength, char* source) {
int ret = snprintf(&dest[*destOffset], *destRemainingLength, "%s", source);
if (ret < 0 || ret >= *destRemainingLength) {
LC_ASSERT(false);
return false;
}
*destOffset += ret;
*destRemainingLength -= ret;
return true;
}
// Serialize the message struct into a string containing the RTSP message // Serialize the message struct into a string containing the RTSP message
char* serializeRtspMessage(PRTSP_MESSAGE msg, int* serializedLength) { char* serializeRtspMessage(PRTSP_MESSAGE msg, int* serializedLength) {
int size = getMessageLength(msg); int size = getMessageLength(msg);
int offset = 0;
char* serializedMessage; char* serializedMessage;
POPTION_ITEM current = msg->options; POPTION_ITEM current = msg->options;
char statusCodeStr[16]; char statusCodeStr[16];
@@ -334,44 +347,53 @@ char* serializeRtspMessage(PRTSP_MESSAGE msg, int* serializedLength) {
if (msg->type == TYPE_REQUEST) { if (msg->type == TYPE_REQUEST) {
// command [space] // command [space]
strcpy(serializedMessage, msg->message.request.command); if (!appendString(serializedMessage, &offset, &size, msg->message.request.command) || !appendString(serializedMessage, &offset, &size, " ")) {
strcat(serializedMessage, " "); goto fail;
}
// target [space] // target [space]
strcat(serializedMessage, msg->message.request.target); if (!appendString(serializedMessage, &offset, &size, msg->message.request.target) || !appendString(serializedMessage, &offset, &size, " ")) {
strcat(serializedMessage, " "); goto fail;
}
// protocol \r\n // protocol \r\n
strcat(serializedMessage, msg->protocol); if (!appendString(serializedMessage, &offset, &size, msg->protocol) || !appendString(serializedMessage, &offset, &size, "\r\n")) {
strcat(serializedMessage, "\r\n"); goto fail;
}
} }
else { else {
// protocol [space] // protocol [space]
strcpy(serializedMessage, msg->protocol); if (!appendString(serializedMessage, &offset, &size, msg->protocol) || !appendString(serializedMessage, &offset, &size, " ")) {
strcat(serializedMessage, " "); goto fail;
}
// status code [space] // status code [space]
sprintf(statusCodeStr, "%d", msg->message.response.statusCode); snprintf(statusCodeStr, sizeof(statusCodeStr), "%d", msg->message.response.statusCode);
strcat(serializedMessage, statusCodeStr); if (!appendString(serializedMessage, &offset, &size, statusCodeStr) || !appendString(serializedMessage, &offset, &size, " ")) {
strcat(serializedMessage, " "); goto fail;
}
// status str\r\n // status str\r\n
strcat(serializedMessage, msg->message.response.statusString); if (!appendString(serializedMessage, &offset, &size, msg->message.response.statusString) || !appendString(serializedMessage, &offset, &size, "\r\n")) {
strcat(serializedMessage, "\r\n"); goto fail;
}
} }
// option content\r\n // option content\r\n
while (current != NULL) { while (current != NULL) {
strcat(serializedMessage, current->option); if (!appendString(serializedMessage, &offset, &size, current->option) || !appendString(serializedMessage, &offset, &size, ": ")) {
strcat(serializedMessage, ": "); goto fail;
strcat(serializedMessage, current->content); }
strcat(serializedMessage, "\r\n"); if (!appendString(serializedMessage, &offset, &size, current->content) || !appendString(serializedMessage, &offset, &size, "\r\n")) {
goto fail;
}
current = current->next; current = current->next;
} }
// Final \r\n // Final \r\n
strcat(serializedMessage, "\r\n"); if (!appendString(serializedMessage, &offset, &size, "\r\n")) {
goto fail;
}
// payload // payload
if (msg->payload != NULL) { if (msg->payload != NULL) {
int offset; if (msg->payloadLength > size) {
goto fail;
// Find end of the RTSP message header }
for (offset = 0; serializedMessage[offset] != 0; offset++);
// Add the payload after // Add the payload after
memcpy(&serializedMessage[offset], msg->payload, msg->payloadLength); memcpy(&serializedMessage[offset], msg->payload, msg->payloadLength);
@@ -379,10 +401,14 @@ char* serializeRtspMessage(PRTSP_MESSAGE msg, int* serializedLength) {
*serializedLength = offset + msg->payloadLength; *serializedLength = offset + msg->payloadLength;
} }
else { else {
*serializedLength = (int)strlen(serializedMessage); *serializedLength = offset;
} }
return serializedMessage; return serializedMessage;
fail:
free(serializedMessage);
return NULL;
} }
// Free everything in a msg struct // Free everything in a msg struct