"Fossies" - the Fresh Open Source Software Archive

Member "libspf2-1.2.10/src/libspf2/spf_utils.c" (28 Jan 2012, 3802 Bytes) of package /linux/privat/libspf2-1.2.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "spf_utils.c" see the Fossies "Dox" file reference documentation.

    1 /* 
    2  * This program is free software; you can redistribute it and/or modify
    3  * it under the terms of either:
    4  * 
    5  *   a) The GNU Lesser General Public License as published by the Free
    6  *    Software Foundation; either version 2.1, or (at your option) any
    7  *    later version,
    8  * 
    9  *   OR
   10  * 
   11  *   b) The two-clause BSD license.
   12  *
   13  * These licenses can be found with the distribution in the file LICENSES
   14  */
   15 
   16 
   17 #include "spf_sys_config.h"
   18 
   19 #ifdef STDC_HEADERS
   20 # include <stdlib.h>       /* malloc / free */
   21 # include <ctype.h>     /* isupper / tolower */
   22 #endif
   23 
   24 #ifdef HAVE_MEMORY_H
   25 #include <memory.h>
   26 #endif
   27 
   28 
   29 
   30 #include "spf.h"
   31 #include "spf_internal.h"
   32 
   33 
   34 /**
   35  * Returns the version numbers of this library.
   36  */
   37 void
   38 SPF_get_lib_version(int *major, int *minor, int *patch)
   39 {
   40     *major = SPF_LIB_VERSION_MAJOR;
   41     *minor = SPF_LIB_VERSION_MINOR;
   42     *patch = SPF_LIB_VERSION_PATCH;
   43 }
   44 
   45 
   46 
   47 /**
   48  * Sanitizes a string for printing.
   49  *
   50  * This replaces all nonprintable characters in str with a '?'.
   51  * The source string is modified in-place.
   52  */
   53 char *
   54 SPF_sanitize(SPF_server_t *spf_server, char *str)
   55 {
   56     char *p;
   57     
   58     SPF_ASSERT_NOTNULL(spf_server);
   59 
   60     if (! spf_server->sanitize)
   61         return str;
   62 
   63     if (str == NULL)
   64         return str;
   65     
   66     for (p = str; *p != '\0'; p++)
   67         if (! isprint( (unsigned char)*p ))
   68             *p = '?';
   69 
   70     return str;
   71 }
   72 
   73 
   74 
   75 
   76 
   77 /**
   78  * Converts an SPF result to a short human-readable string.
   79  */
   80 const char *
   81 SPF_strresult(SPF_result_t result)
   82 {
   83     switch (result) {
   84         case SPF_RESULT_INVALID:
   85             return "(invalid)";
   86             break;
   87 
   88         case SPF_RESULT_PASS:               /* +                            */
   89             return "pass";
   90             break;
   91 
   92         case SPF_RESULT_FAIL:               /* -                            */
   93             return "fail";
   94             break;
   95 
   96         case SPF_RESULT_SOFTFAIL:           /* ~                            */
   97             return "softfail";
   98             break;
   99 
  100         case SPF_RESULT_NEUTRAL:            /* ?                            */
  101             return "neutral";
  102             break;
  103 
  104         case SPF_RESULT_PERMERROR:          /* permanent error              */
  105             return "permerror";
  106             break;
  107 
  108         case SPF_RESULT_TEMPERROR:          /* temporary error              */
  109             return "temperror";
  110             break;
  111 
  112         case SPF_RESULT_NONE:               /* no SPF record found          */
  113             return "none";
  114             break;
  115 
  116         default:
  117             return "(error: unknown result)";
  118             break;
  119     }
  120 }
  121 
  122 
  123 
  124 /**
  125  * Converts an SPF reason to a short human-readable string.
  126  */
  127 const char *
  128 SPF_strreason(SPF_reason_t reason)
  129 {
  130     switch (reason) {
  131         case SPF_REASON_NONE:
  132             return "none";
  133             break;
  134             
  135         case SPF_REASON_LOCALHOST:
  136             return "localhost";
  137             break;
  138             
  139         case SPF_REASON_LOCAL_POLICY:
  140             return "local policy";
  141             break;
  142             
  143         case SPF_REASON_MECH:
  144             return "mechanism";
  145             break;
  146             
  147         case SPF_REASON_DEFAULT:
  148             return "default";
  149             break;
  150             
  151         case SPF_REASON_2MX:
  152             return "secondary MX";
  153             break;
  154             
  155         default:
  156             return "(invalid reason)";
  157             break;
  158         
  159     }
  160 }
  161 
  162 const char *
  163 SPF_strrrtype(ns_type rr_type)
  164 {
  165     switch (rr_type) {
  166         case ns_t_a:       return "A";
  167         case ns_t_aaaa:    return "AAAA";
  168         case ns_t_any:     return "ANY";
  169         case ns_t_invalid: return "BAD";
  170         case ns_t_mx:      return "MX";
  171         case ns_t_ptr:     return "PTR";
  172         case ns_t_spf:     return "SPF";
  173         case ns_t_txt:     return "TXT";
  174         default:           return "??";
  175     }
  176 }
  177 
  178 /**
  179  * This is NOT a general-purpose realloc. It is used only for
  180  * text buffers. It will allocate at least 64 bytes of storage.
  181  *
  182  * This function is allowed to zero all the RAM returned, so it
  183  * really isn't a realloc.
  184  *
  185  * Do not call this function from outside the library.
  186  */
  187 SPF_errcode_t
  188 SPF_recalloc(char **bufp, size_t *buflenp, size_t buflen)
  189 {
  190     char        *buf;
  191 
  192     if (*buflenp < buflen) {
  193         if (buflen < 64)
  194             buflen = 64;
  195         buf = realloc(*bufp, buflen);
  196         if (buf == NULL)
  197             return SPF_E_NO_MEMORY;
  198 
  199         // memset(buf + *buflenp, '\0', buflen - *buflenp); 
  200         *bufp = buf;
  201         *buflenp = buflen;
  202     }
  203     else {
  204         SPF_ASSERT_NOTNULL(*bufp);
  205     }
  206 
  207     memset(*bufp, '\0', *buflenp);
  208     return SPF_E_SUCCESS;
  209 }