1 #include "swstring.h"
2 
3 /*
4  * Copyright 2010 Sven Vermeulen.
5  * Subject to the GNU Public License, version 3.
6  */
7 
8 /**
9  * zero_string - Empty the given string and fill it up with 0x00's.
10  *
11  * @param buffer Pointer to the string, should be non-NULL.
12  * @param numlen Length of the buffer. If 0 or lower, the length of the buffer * is calculated using strlen().
13  */
zero_string(char * buffer,size_t numlen)14 void zero_string(char * buffer, size_t numlen) {
15 	assert(buffer != NULL);
16 
17 	if (numlen <= 0)
18 		numlen = strlen(buffer);
19 
20 	memset(buffer, 0x00, numlen);
21 };
22 
23 /**
24  * swstrlen - Return the length of the string as an integer
25  *
26  * swstrlen supports stringlength checks on NULL as well - it returns
27  * it as 0.
28  */
swstrlen(const char * buffer)29 int swstrlen(const char * buffer) {
30 	if (buffer == NULL)
31 		return 0;
32 	else
33 		return buffer[0] == '\0' ? 0 : strlen(buffer);
34 };
35 
36 /**
37  * substitute_variable - Substitute variable in string with proper value
38  *
39  * For safety measures, length of $value needs to be 1023 or less.
40  */
substitute_variable(const char * buffer,const char * prevar,const char * postvar,const char * varname,const char * value)41 char * substitute_variable(const char * buffer, const char * prevar, const char * postvar, const char * varname, const char * value) {
42 	char * posptr = NULL;
43 	char * tmpptr = NULL;
44 	char * newbfr = NULL;
45 	int found  = 0;
46 
47 	// Get lengths
48 	int l_varname = swstrlen(varname);
49 	int l_buffer  = swstrlen(buffer);
50 	int l_prevar  = swstrlen(prevar);
51 	int l_postvar = swstrlen(postvar);
52 	int l_value   = swstrlen(value);
53 
54 	// Verify that the lengths aren't unusually large or small...
55 	if ((l_varname == 0) || (l_varname > l_buffer - l_prevar - l_postvar))
56 		return NULL;
57 	if (l_value > 1023)
58 		return NULL;
59 
60 	// Allocate room for filled in string
61 
62 	newbfr = (char *) calloc(sizeof(char), l_buffer - l_prevar - l_postvar - l_varname + l_value + 1);
63 
64 	if (prevar != NULL) {
65 		posptr = strstr(buffer, prevar);
66 		while(posptr != NULL) {
67 			if (strncmp(posptr + l_prevar, varname, l_varname) == 0) {
68 				strncpy(newbfr, buffer, l_buffer - swstrlen(posptr));  // First part
69 				strcat(newbfr, value);                                 // Add value
70 				if (postvar != NULL) {
71 					tmpptr = strstr(posptr + l_prevar, postvar);
72 					strcat(newbfr, tmpptr + l_postvar);
73 				} else {
74 					strcat(newbfr, posptr + l_prevar + l_varname);
75 				};
76 				found++;
77 			};
78 			tmpptr = strstr(posptr + l_prevar, prevar);
79 			posptr = tmpptr;
80 		};
81 	} else {
82 		posptr = strstr(buffer, varname);
83 		while(posptr != NULL) {
84 			strncpy(newbfr, buffer, l_buffer - swstrlen(posptr)); // First part
85 			strcat(newbfr, value);                                // Add value
86 			if (postvar != NULL) {
87 				tmpptr = strstr(posptr + l_varname, postvar);
88 				strcat(newbfr, tmpptr + l_postvar);
89 			} else {
90 				strcat(newbfr, posptr + l_varname);
91 			};
92 			found++;
93 			tmpptr = strstr(posptr + l_varname, varname);
94 			posptr = tmpptr;
95 		};
96 	};
97 	if (found > 0) {
98 		return newbfr;
99 	} else {
100 		free(newbfr);
101 		return NULL;
102 	};
103 };
104