Line data Source code
1 : /* This file is part of the cmail project (http://cmail.rocks/)
2 : * (c) 2015 Fabian "cbdev" Stumpf
3 : * License: Simplified BSD (2-Clause)
4 : * For further information, consult LICENSE.txt
5 : */
6 :
7 24 : char* common_strdup(char* input){
8 24 : char* duplicate = NULL;
9 24 : size_t length = strlen(input);
10 24 : duplicate = calloc(length + 1, sizeof(char));
11 24 : if(!duplicate){
12 0 : return NULL;
13 : }
14 :
15 24 : return strncpy(duplicate, input, length); //this is ok because the memory is calloc'd
16 : }
17 :
18 0 : int common_rand(void* target, size_t bytes){
19 0 : size_t data_read = 0;
20 0 : FILE* pool = fopen(RANDOMNESS_POOL, "r");
21 :
22 0 : if(!pool){
23 0 : return -1;
24 : }
25 :
26 0 : data_read = fread(target, 1, bytes, pool);
27 :
28 0 : fclose(pool);
29 0 : return data_read;
30 : }
31 :
32 0 : int common_strrepl(char* buffer, unsigned length, char* variable, char* replacement){
33 0 : char* occurence = NULL;
34 0 : unsigned offset = 0;
35 : unsigned character_offset;
36 : unsigned i;
37 :
38 0 : for(occurence = strstr(buffer + offset, variable); occurence; occurence = strstr(buffer + offset, variable)){
39 : //check whether the replacement actually fits in here
40 0 : if((occurence - buffer) + strlen(replacement) + strlen(buffer + strlen(variable)) >= length){
41 0 : return -1;
42 : }
43 :
44 : //move the trailing part out of the way
45 0 : if(strlen(replacement) > strlen(variable)){
46 0 : character_offset = strlen(replacement) - strlen(variable);
47 :
48 : //begin at end
49 0 : for(i = 0; i <= strlen(occurence); i++){
50 0 : occurence[strlen(occurence) - i + character_offset] = occurence[strlen(occurence) - i];
51 : }
52 : }
53 0 : else if(strlen(replacement) < strlen(variable)){
54 0 : character_offset = strlen(variable) - strlen(replacement);
55 :
56 : //begin in front
57 0 : for(i = 0; i <= strlen(occurence + strlen(variable)); i++){
58 0 : occurence[strlen(replacement) + i] = occurence[strlen(replacement) + i + character_offset];
59 : }
60 : }
61 :
62 : //insert the replacement
63 0 : strncpy(occurence, replacement, strlen(replacement));
64 :
65 : //increase the offset
66 0 : offset = (occurence - buffer) + strlen(replacement);
67 : }
68 :
69 0 : return 0;
70 : }
71 :
72 0 : char* common_strappf(char* target, unsigned* target_allocated, char* fmt, ...){
73 : va_list args, copy;
74 :
75 0 : size_t target_len = target ? strlen(target):0;
76 0 : int additional_length = 0;
77 :
78 0 : va_start(args, fmt);
79 :
80 0 : va_copy(copy, args);
81 0 : additional_length = vsnprintf(NULL, 0, fmt, copy);
82 0 : va_end(copy);
83 :
84 0 : if(additional_length < 0){
85 0 : va_end(args);
86 0 : return NULL;
87 : }
88 :
89 0 : if(target_len + additional_length >= *target_allocated){
90 : //reallocate
91 0 : target = realloc(target, (target_len + additional_length + 1) * sizeof(char));
92 0 : if(!target){
93 0 : va_end(args);
94 0 : return NULL;
95 : }
96 :
97 0 : *target_allocated = target_len + additional_length + 1;
98 : }
99 :
100 0 : vsnprintf(target + target_len, additional_length + 1, fmt, args);
101 :
102 0 : va_end(args);
103 0 : return target;
104 : }
105 :
106 1406 : int common_tprintf(char* format, time_t time, char* buffer, size_t buffer_length){
107 1406 : struct tm* local_time = localtime(&time);
108 1406 : if(!local_time){
109 0 : return -1;
110 : }
111 :
112 1406 : if(strftime(buffer, buffer_length, format, local_time) == 0){
113 : //buffer too short
114 0 : return -2;
115 : }
116 :
117 1406 : return 0;
118 : }
119 :
120 4 : ssize_t common_read_file(char* filename, uint8_t** out){
121 4 : uint8_t* buffer = NULL;
122 4 : FILE* handle = NULL;
123 4 : long file_size = 0;
124 :
125 4 : *out = NULL;
126 :
127 4 : handle = fopen(filename, "rb");
128 4 : if(!handle){
129 0 : return -1;
130 : }
131 :
132 4 : fseek(handle, 0, SEEK_END);
133 4 : file_size = ftell(handle);
134 4 : rewind(handle);
135 :
136 4 : if(file_size < 0){
137 0 : fclose(handle);
138 0 : return -1;
139 : }
140 :
141 4 : buffer = calloc(file_size + 2, 1);
142 4 : if(!buffer){
143 0 : fclose(handle);
144 0 : return -1;
145 : }
146 :
147 4 : if(fread(buffer, file_size, 1, handle) != 1){
148 0 : free(buffer);
149 0 : fclose(handle);
150 0 : return -1;
151 : }
152 :
153 4 : fclose(handle);
154 :
155 4 : *out = buffer;
156 4 : return file_size;
157 : }
158 :
159 166 : ssize_t common_next_line(LOGGER log, char* buffer, size_t* append_offset_p, ssize_t* new_bytes_p){
160 : //This function needs to be called on a buffer until it returns 0,
161 : //otherwise, the append_offset points to the end of the "olddata" buffer
162 166 : size_t append_offset = *append_offset_p;
163 166 : ssize_t new_bytes = *new_bytes_p;
164 : int i;
165 :
166 166 : if(new_bytes < 0){
167 0 : logprintf(log, LOG_ERROR, "common_next_line called with error condition in last read, bailing\n");
168 0 : return 0;
169 : }
170 :
171 166 : logprintf(log, LOG_DEBUG, "Next line parser called with offset %d bytes %d\n", append_offset, new_bytes);
172 :
173 166 : if(append_offset > 1 && buffer[append_offset-1] == 0 && buffer[append_offset-2] == 0){
174 : //copyback clearing of last line
175 :
176 83 : for(i=0;i<new_bytes;i++){
177 : //logprintf(log, LOG_DEBUG, "Moving character %02X from position %d to %d\n", client_data->recv_buffer[client_data->recv_offset+i+1+c], client_data->recv_offset+i+1+c, c);
178 0 : buffer[i] = buffer[append_offset+i];
179 : }
180 :
181 83 : append_offset = 0;
182 :
183 83 : logprintf(log, LOG_DEBUG, "Copyback done, offset %d, first byte %02X, %d bytes\n", append_offset, buffer[0], new_bytes);
184 : }
185 :
186 : //scan new bytes for terminators
187 792 : for(i=0;i<new_bytes-1;i++){ //last byte is checked in condition
188 709 : if(buffer[append_offset + i] == '\r' && buffer[append_offset + i + 1] == '\n'){
189 : //terminate line
190 83 : buffer[append_offset + i] = 0;
191 83 : buffer[append_offset + i + 1] = 0;
192 :
193 : //send to line processor
194 83 : *append_offset_p = append_offset + i + 2; //append after the second \0
195 83 : *new_bytes_p = new_bytes - i - 2;
196 :
197 83 : logprintf(log, LOG_DEBUG, "Next line contains %d bytes\n", append_offset + i);
198 83 : return append_offset + i;
199 : }
200 : }
201 :
202 : //no more lines left
203 83 : *append_offset_p = append_offset + new_bytes;
204 83 : *new_bytes_p = 0;
205 :
206 83 : logprintf(log, LOG_DEBUG, "Incomplete line buffer contains %d bytes\n", *append_offset_p);
207 83 : return -1;
208 : }
209 :
210 : /*
211 : call sequence
212 :
213 : //CHECK LEFTBYTES
214 :
215 : do {
216 : line_length=next(log, buffer, recv_off, bytes);
217 : if(length<0){
218 : //procfail
219 : }
220 : else if(length>0){
221 : if(length>PROTOMAX){
222 : fail
223 : }
224 : else{
225 : handle
226 : }
227 : }
228 : }
229 : while(length>0);
230 :
231 :
232 : */
|