LCOV - code coverage report
Current view: top level - lib - common.c (source / functions) Hit Total Coverage
Test: smtpd.info Lines: 78 107 72.9 %
Date: 2015-11-25 19:06:20 Functions: 6 7 85.7 %

          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          87 : char* common_strdup(char* input){
       8          87 :         char* duplicate = NULL;
       9          87 :         size_t length = strlen(input);
      10          87 :         duplicate = calloc(length + 1, sizeof(char));
      11          87 :         if(!duplicate){
      12           0 :                 return NULL;
      13             :         }
      14             : 
      15          87 :         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          32 : int common_strrepl(char* buffer, unsigned length, char* variable, char* replacement){
      33          32 :         char* occurence = NULL;
      34          32 :         unsigned offset = 0;
      35             :         unsigned character_offset;
      36             :         unsigned i;
      37             : 
      38          56 :         for(occurence = strstr(buffer + offset, variable); occurence; occurence = strstr(buffer + offset, variable)){
      39             :                 //check whether the replacement actually fits in here
      40          24 :                 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          24 :                 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          24 :                 else if(strlen(replacement) < strlen(variable)){
      54          24 :                         character_offset = strlen(variable) - strlen(replacement);
      55             : 
      56             :                         //begin in front
      57         324 :                         for(i = 0; i <= strlen(occurence + strlen(variable)); i++){
      58         300 :                                 occurence[strlen(replacement) + i] = occurence[strlen(replacement) + i + character_offset];
      59             :                         }
      60             :                 }
      61             : 
      62             :                 //insert the replacement
      63          24 :                 strncpy(occurence, replacement, strlen(replacement));
      64             : 
      65             :                 //increase the offset
      66          24 :                 offset = (occurence - buffer) + strlen(replacement);
      67             :         }
      68             : 
      69          32 :         return 0;
      70             : }
      71             : 
      72          31 : char* common_strappf(char* target, unsigned* target_allocated, char* fmt, ...){
      73             :         va_list args, copy;
      74             : 
      75          31 :         size_t target_len = target ? strlen(target):0;
      76          31 :         int additional_length = 0;
      77             : 
      78          31 :         va_start(args, fmt);
      79             : 
      80          31 :         va_copy(copy, args);
      81          31 :         additional_length = vsnprintf(NULL, 0, fmt, copy);
      82          31 :         va_end(copy);
      83             : 
      84          31 :         if(additional_length < 0){
      85           0 :                 va_end(args);
      86           0 :                 return NULL;
      87             :         }
      88             : 
      89          31 :         if(target_len + additional_length >= *target_allocated){
      90             :                 //reallocate
      91          31 :                 target = realloc(target, (target_len + additional_length + 1) * sizeof(char));
      92          31 :                 if(!target){
      93           0 :                         va_end(args);
      94           0 :                         return NULL;
      95             :                 }
      96             : 
      97          31 :                 *target_allocated = target_len + additional_length + 1;
      98             :         }
      99             : 
     100          31 :         vsnprintf(target + target_len, additional_length + 1, fmt, args);
     101             : 
     102          31 :         va_end(args);
     103          31 :         return target;
     104             : }
     105             : 
     106        4602 : int common_tprintf(char* format, time_t time, char* buffer, size_t buffer_length){
     107        4602 :         struct tm* local_time = localtime(&time);
     108        4602 :         if(!local_time){
     109           0 :                 return -1;
     110             :         }
     111             : 
     112        4602 :         if(strftime(buffer, buffer_length, format, local_time) == 0){
     113             :                 //buffer too short
     114           0 :                 return -2;
     115             :         }
     116             : 
     117        4602 :         return 0;
     118             : }
     119             : 
     120           6 : ssize_t common_read_file(char* filename, uint8_t** out){
     121           6 :         uint8_t* buffer = NULL;
     122           6 :         FILE* handle = NULL;
     123           6 :         long file_size = 0;
     124             : 
     125           6 :         *out = NULL;
     126             : 
     127           6 :         handle = fopen(filename, "rb");
     128           6 :         if(!handle){
     129           0 :                 return -1;
     130             :         }
     131             : 
     132           6 :         fseek(handle, 0, SEEK_END);
     133           6 :         file_size = ftell(handle);
     134           6 :         rewind(handle);
     135             : 
     136           6 :         if(file_size < 0){
     137           0 :                 fclose(handle);
     138           0 :                 return -1;
     139             :         }
     140             : 
     141           6 :         buffer = calloc(file_size + 2, 1);
     142           6 :         if(!buffer){
     143           0 :                 fclose(handle);
     144           0 :                 return -1;
     145             :         }
     146             : 
     147           6 :         if(fread(buffer, file_size, 1, handle) != 1){
     148           0 :                 free(buffer);
     149           0 :                 fclose(handle);
     150           0 :                 return -1;
     151             :         }
     152             : 
     153           6 :         fclose(handle);
     154             : 
     155           6 :         *out = buffer;
     156           6 :         return file_size;
     157             : }
     158             : 
     159         587 : 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         587 :         size_t append_offset = *append_offset_p;
     163         587 :         ssize_t new_bytes = *new_bytes_p;
     164             :         int i;
     165             : 
     166         587 :         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         587 :         logprintf(log, LOG_DEBUG, "Next line parser called with offset %d bytes %d\n", append_offset, new_bytes);
     172             : 
     173         587 :         if(append_offset > 1 && buffer[append_offset-1] == 0 && buffer[append_offset-2] == 0){
     174             :                 //copyback clearing of last line
     175             : 
     176        2923 :                 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        2606 :                         buffer[i] = buffer[append_offset+i];
     179             :                 }
     180             : 
     181         317 :                 append_offset = 0;
     182             : 
     183         317 :                 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        4884 :         for(i=0;i<new_bytes-1;i++){ //last byte is checked in condition
     188        4614 :                 if(buffer[append_offset + i] == '\r' && buffer[append_offset + i + 1] == '\n'){
     189             :                         //terminate line
     190         317 :                         buffer[append_offset + i] = 0;
     191         317 :                         buffer[append_offset + i + 1] = 0;
     192             : 
     193             :                         //send to line processor
     194         317 :                         *append_offset_p = append_offset + i + 2; //append after the second \0
     195         317 :                         *new_bytes_p = new_bytes - i - 2;
     196             : 
     197         317 :                         logprintf(log, LOG_DEBUG, "Next line contains %d bytes\n", append_offset + i);
     198         317 :                         return append_offset + i;
     199             :                 }
     200             :         }
     201             : 
     202             :         //no more lines left
     203         270 :         *append_offset_p = append_offset + new_bytes;
     204         270 :         *new_bytes_p = 0;
     205             : 
     206         270 :         logprintf(log, LOG_DEBUG, "Incomplete line buffer contains %d bytes\n", *append_offset_p);
     207         270 :         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             : */

Generated by: LCOV version 1.11