Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: utils: remove str replace function from the client library #51

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 0 additions & 94 deletions core/src/mender-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
* SOFTWARE.
*/

#include <sys/types.h>
#include <regex.h>
#include "mender-log.h"

char *
Expand Down Expand Up @@ -147,98 +145,6 @@ mender_utils_strendwith(const char *s1, const char *s2) {
return (0 == strncmp(s1 + strlen(s1) - strlen(s2), s2, strlen(s2)));
}

char *
mender_utils_str_replace(char *input, char *search, char *replace) {

assert(NULL != input);
assert(NULL != search);
assert(NULL != replace);

regex_t regex;
regmatch_t match;
char * str = input;
char * output = NULL;
size_t index = 0;
int previous_match_finish = 0;

/* Compile expression */
if (0 != regcomp(&regex, search, REG_EXTENDED)) {
/* Unable to compile expression */
mender_log_error("Unable to compile expression '%s'", search);
return NULL;
}

/* Loop until all search string are replaced */
bool loop = true;
while (true == loop) {

/* Search wanted string */
if (0 != regexec(&regex, str, 1, &match, 0)) {
/* No more string to be replaced */
loop = false;
} else {
if (match.rm_so != -1) {

/* Beginning and ending offset of the match */
int current_match_start = (int)(match.rm_so + (str - input));
int current_match_finish = (int)(match.rm_eo + (str - input));

/* Reallocate output memory */
char *tmp = (char *)realloc(output, index + (current_match_start - previous_match_finish) + 1);
if (NULL == tmp) {
mender_log_error("Unable to allocate memory");
regfree(&regex);
free(output);
return NULL;
}
output = tmp;

/* Copy string from previous match to the beginning of the current match */
memcpy(&output[index], &input[previous_match_finish], current_match_start - previous_match_finish);
index += (current_match_start - previous_match_finish);
output[index] = 0;

/* Reallocate output memory */
if (NULL == (tmp = (char *)realloc(output, index + strlen(replace) + 1))) {
mender_log_error("Unable to allocate memory");
regfree(&regex);
free(output);
return NULL;
}
output = tmp;

/* Copy replace string to the output */
strcat(output, replace);
index += strlen(replace);

/* Update previous match ending value */
previous_match_finish = current_match_finish;
}
str += match.rm_eo;
}
}

/* Reallocate output memory */
char *tmp = (char *)realloc(output, index + (strlen(input) - previous_match_finish) + 1);
if (NULL == tmp) {
mender_log_error("Unable to allocate memory");
regfree(&regex);
free(output);
return NULL;
}
output = tmp;

/* Copy the end of the string after the latest match */
memcpy(&output[index], &input[previous_match_finish], strlen(input) - previous_match_finish);
index += (strlen(input) - previous_match_finish);
output[index] = 0;

/* Release regex */
regfree(&regex);

return output;
}

char *
mender_utils_deployment_status_to_string(mender_deployment_status_t deployment_status) {

Expand Down
9 changes: 0 additions & 9 deletions include/mender-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,6 @@ bool mender_utils_strbeginwith(const char *s1, const char *s2);
*/
bool mender_utils_strendwith(const char *s1, const char *s2);

/**
* @brief Function used to replace a string in the input buffer
* @param input Input buffer
* @param search String to be replaced or regex expression
* @param replace Replacement string
* @return New string with replacements if the function succeeds, NULL otherwise
*/
char *mender_utils_str_replace(char *input, char *search, char *replace);

/**
* @brief Function used to create a key-store
* @param length Length of the key-store
Expand Down
104 changes: 102 additions & 2 deletions tests/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
*/

#include <getopt.h>
#include <stdio.h>
#include <pthread.h>
#include <regex.h>
#include <signal.h>
#include <stdio.h>
#include "mender-client.h"
#include "mender-configure.h"
#include "mender-flash.h"
Expand Down Expand Up @@ -199,6 +200,105 @@ shell_resize_cb(uint16_t terminal_width, uint16_t terminal_height) {
return MENDER_OK;
}

/**
* @brief Function used to replace a string in the input buffer
* @param input Input buffer
* @param search String to be replaced or regex expression
* @param replace Replacement string
* @return New string with replacements if the function succeeds, NULL otherwise
*/
static char *
str_replace(char *input, char *search, char *replace) {

assert(NULL != input);
assert(NULL != search);
assert(NULL != replace);

regex_t regex;
regmatch_t match;
char * str = input;
char * output = NULL;
size_t index = 0;
int previous_match_finish = 0;

/* Compile expression */
if (0 != regcomp(&regex, search, REG_EXTENDED)) {
/* Unable to compile expression */
mender_log_error("Unable to compile expression '%s'", search);
return NULL;
}

/* Loop until all search string are replaced */
bool loop = true;
while (true == loop) {

/* Search wanted string */
if (0 != regexec(&regex, str, 1, &match, 0)) {
/* No more string to be replaced */
loop = false;
} else {
if (match.rm_so != -1) {

/* Beginning and ending offset of the match */
int current_match_start = (int)(match.rm_so + (str - input));
int current_match_finish = (int)(match.rm_eo + (str - input));

/* Reallocate output memory */
char *tmp = (char *)realloc(output, index + (current_match_start - previous_match_finish) + 1);
if (NULL == tmp) {
mender_log_error("Unable to allocate memory");
regfree(&regex);
free(output);
return NULL;
}
output = tmp;

/* Copy string from previous match to the beginning of the current match */
memcpy(&output[index], &input[previous_match_finish], current_match_start - previous_match_finish);
index += (current_match_start - previous_match_finish);
output[index] = 0;

/* Reallocate output memory */
if (NULL == (tmp = (char *)realloc(output, index + strlen(replace) + 1))) {
mender_log_error("Unable to allocate memory");
regfree(&regex);
free(output);
return NULL;
}
output = tmp;

/* Copy replace string to the output */
strcat(output, replace);
index += strlen(replace);

/* Update previous match ending value */
previous_match_finish = current_match_finish;
}
str += match.rm_eo;
}
}

/* Reallocate output memory */
char *tmp = (char *)realloc(output, index + (strlen(input) - previous_match_finish) + 1);
if (NULL == tmp) {
mender_log_error("Unable to allocate memory");
regfree(&regex);
free(output);
return NULL;
}
output = tmp;

/* Copy the end of the string after the latest match */
memcpy(&output[index], &input[previous_match_finish], strlen(input) - previous_match_finish);
index += (strlen(input) - previous_match_finish);
output[index] = 0;

/* Release regex */
regfree(&regex);

return output;
}

/**
* @brief Shell write data callback
* @param data Shell data received
Expand All @@ -217,7 +317,7 @@ shell_write_cb(uint8_t *data, size_t length) {
ret = MENDER_FAIL;
goto END;
}
if (NULL == (tmp = mender_utils_str_replace(buffer, "\r|\n", "\r\n"))) {
if (NULL == (tmp = str_replace(buffer, "\r|\n", "\r\n"))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto END;
Expand Down
Loading