Changeset 211

Show
Ignore:
Timestamp:
07/14/05 16:12:46
Author:
rachel
Message:

first stab at rationalising some error codes, and
introducing the use of error codes rather than strings

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/examples/packet-dump.c

    r209 r211  
    55#include "util.h" 
    66#include "lists.h" 
     7#include "ops_errors.h" 
    78 
    89#include <unistd.h> 
     
    238239        break; 
    239240 
     241    case OPS_PARSER_ERRCODE: 
     242        printf("parse error: %s (0x%x): %s\n", 
     243               ops_errcode(content->errcode.errcode, OPS_LANG_ENGLISH), 
     244               content->errcode.errcode, 
     245               ops_error(content->errcode.errcode, OPS_LANG_ENGLISH)); 
     246        break; 
     247 
    240248    case OPS_PARSER_PACKET_END: 
    241249        print_packet(&content->packet); 
  • openpgpsdk/trunk/include/packet-show.cast

    r172 r211  
    22// probably not this one! 
    33 
    4 char *show_packet_tag(ops_packet_tag_t packet_tag, packet_tag_map_t *packet_tag_map) -> char *str_from_map(int packet_tag, map_t *packet_tag_map) 
    5 char *show_sig_type(ops_sig_type_t sig_type, sig_type_map_t *sig_type_map) -> char *str_from_map(int sig_type, map_t *sig_type_map) 
    6 char *show_pka(ops_public_key_algorithm_t pka, public_key_algorithm_map_t *pka_map) -> char *str_from_map(int pka, map_t *pka_map) 
    7 char *show_ss_type(ops_ss_type_t ss_type, ss_type_map_t *ss_type_map) -> char *str_from_map(int ss_type, map_t *ss_type_map) 
    8 char *show_ss_rr_code(ops_ss_rr_code_t ss_rr_code, ss_rr_code_map_t *ss_rr_code_map) -> char *str_from_map(int ss_rr_code, map_t *ss_rr_code_map) 
    9 char *show_hash_algorithm(unsigned char hash,+map_t *hash_algorithm_map) -> char *str_from_map(int hash,map_t *hash_algorithm_map) 
     4char *show_packet_tag(ops_packet_tag_t packet_tag, packet_tag_map_t *packet_tag_map) -> char *ops_str_from_map(int packet_tag, map_t *packet_tag_map) 
     5char *show_sig_type(ops_sig_type_t sig_type, sig_type_map_t *sig_type_map) -> char *ops_str_from_map(int sig_type, map_t *sig_type_map) 
     6char *show_pka(ops_public_key_algorithm_t pka, public_key_algorithm_map_t *pka_map) -> char *ops_str_from_map(int pka, map_t *pka_map) 
     7char *show_ss_type(ops_ss_type_t ss_type, ss_type_map_t *ss_type_map) -> char *ops_str_from_map(int ss_type, map_t *ss_type_map) 
     8char *show_ss_rr_code(ops_ss_rr_code_t ss_rr_code, ss_rr_code_map_t *ss_rr_code_map) -> char *ops_str_from_map(int ss_rr_code, map_t *ss_rr_code_map) 
     9char *show_hash_algorithm(unsigned char hash,+map_t *hash_algorithm_map) -> char *ops_str_from_map(int hash,map_t *hash_algorithm_map) 
  • openpgpsdk/trunk/include/packet.h

    r203 r211  
    99#include <openssl/bn.h> 
    1010#include "types.h" 
     11#include "ops_errors.h" 
    1112 
    1213/** General-use structure for variable-length data  
     
    143144    OPS_PTAG_SS_ALL                     =0x103, /*!< Internal Use: select all subtags */ 
    144145    OPS_PARSER_PACKET_END               =0x104, 
     146    OPS_PARSER_ERRCODE                  =0x105, /*! < Internal Use: Parser Error with errcode returned */ 
    145147 
    146148    /* signature subpackets (0x200-2ff) (type+0x200) */ 
     
    196198    const char *error; /*!< error message. */ 
    197199    } ops_parser_error_t; 
     200 
     201/** Structure to hold one error code */ 
     202typedef struct 
     203    { 
     204    ops_errcode_t errcode; 
     205    } ops_parser_errcode_t; 
    198206 
    199207/** Structure to hold one packet tag. 
     
    660668    { 
    661669    ops_parser_error_t          error; 
     670    ops_parser_errcode_t        errcode; 
    662671    ops_ptag_t                  ptag; 
    663672    ops_public_key_t            public_key; 
  • openpgpsdk/trunk/include/util.h

    r191 r211  
    3636#define DECONST(type,p) (((type *(*)(const type *))ops_fcast(_deconst))(p)) 
    3737 
     38char *ops_str_from_map(int code, map_t *map); 
     39 
    3840#endif 
  • openpgpsdk/trunk/src/Makefile.template

    r208 r211  
    1111LIBOBJS = packet-parse.o util.o openssl_crypto.o accumulate.o build.o \ 
    1212        memory.o fingerprint.o hash.o keyring.o signature.o compress.o \ 
    13         packet-show.o create.o validate.o lists.o 
     13        packet-show.o create.o validate.o lists.o ops_errors.o 
    1414 
    1515headers: 
  • openpgpsdk/trunk/src/packet-parse.c

    r210 r211  
    99#include "compress.h" 
    1010#include "lists.h" 
     11#include "ops_errors.h" 
    1112 
    1213#include <assert.h> 
     
    107108/*! macro to run CallBack function specifying a parser error has occurred */ 
    108109#define E               CB(OPS_PARSER_ERROR,&content); return 0 
    109 /*! set error code in content and run CallBack to handle error, then return */ 
     110/*! set error code in content and run CallBack to handle error */ 
     111#define ERRCODE(err)    do { C.errcode.errcode=err; CB(OPS_PARSER_ERRCODE,&content); } while(0) 
     112/*! set error text in content and run CallBack to handle error, then return */ 
    110113#define ERR(err)        do { C.error.error=err; E; } while(0) 
    111 /*! set error code in content and run CallBack to handle warning, do not return */ 
     114/*! set error text in content and run CallBack to handle warning, do not return */ 
    112115#define WARN(warn)      do { C.error.error=warn; CB(OPS_PARSER_ERROR,&content);; } while(0) 
    113116/*! \todo descr ERR1 macro */ 
     
    130133    content->content.error.error=buf; 
    131134    } 
     135 
     136/** 
     137 * low-level function to read data from reader function 
     138 * 
     139 * Use this function, rather than calling the reader directly. 
     140 * 
     141 * If the accumulate flag is set in *opt, the function 
     142 * adds the read data to the accumulated data, and updates  
     143 * the accumulated length. This is useful if, for example,  
     144 * the application wants access to the raw data as well as the 
     145 * parsed data. 
     146 * 
     147 * \param *dest 
     148 * \param *plength 
     149 * \param flags 
     150 * \param *opt 
     151 * 
     152 * \return OPS_R_OK 
     153 * \return OPS_R_PARTIAL_READ 
     154 * \return OPS_R_EOF 
     155 * \return OPS_R_EARLY_EOF 
     156 *  
     157 * \sa #opt_reader_ret_t, ops_reader_fd() for details of return codes 
     158 */ 
    132159 
    133160static ops_reader_ret_t base_read(unsigned char *dest,unsigned *plength, 
     
    165192 * \param *reader       Our reader 
    166193 * \param length        How many bytes to read 
    167  * \return              OPS_PR_OK on success, reader's return value otherwise 
    168  * 
     194 * \return              OPS_R_OK on success, reader's return value otherwise 
     195 * 
     196 * \sa #ops_reader_ret_t for possible return codes 
    169197 */ 
    170198static ops_reader_ret_t read_scalar(unsigned *result,unsigned length, 
     
    214242 
    215243    if(!region->indeterminate && region->length_read+length > region->length) 
    216         ERR("Not enough data left"); 
     244        { 
     245        ERRCODE(OPS_E_P_NOT_ENOUGH_DATA); 
     246        return 0; 
     247        } 
    217248 
    218249    ret=base_read(dest,&length,region->indeterminate ? OPS_RETURN_LENGTH : 0, 
     
    220251 
    221252    if(ret != OPS_R_OK && ret != OPS_R_PARTIAL_READ) 
    222         ERR("Read failed"); 
     253        { 
     254        ERRCODE(OPS_E_R_READ_FAILED); 
     255        return 0; 
     256        } 
    223257 
    224258    region->last_read=length; 
     
    614648 
    615649    case OPS_PARSER_ERROR: 
     650    case OPS_PARSER_ERRCODE: 
    616651        break; 
    617652 
  • openpgpsdk/trunk/src/packet-show.c

    r178 r211  
    265265    } 
    266266 
    267 /** 
    268  * Searches the given map for the given type. 
    269  * Returns a human-readable descriptive string if found, 
    270  * returns NULL if not found 
    271  * 
    272  * It is the responsibility of the calling function to handle the 
    273  * error case sensibly (i.e. don't just print out the return string. 
    274  *  
    275  */ 
    276 static char *str_from_map_or_null(int type, map_t *map) 
    277     { 
    278     map_t *row; 
    279  
    280     for ( row=map; row->string != NULL; row++ ) 
    281         if (row->type == type) 
    282             return row->string; 
    283     return NULL; 
    284     } 
    285  
    286 /** 
    287  * Searches the given map for the given type. 
    288  * Returns a readable string if found, "Unknown" if not. 
    289  */ 
    290  
    291 char *str_from_map(int type, map_t *map) 
    292     { 
    293     char *str; 
    294     str=str_from_map_or_null(type,map); 
    295     if (str) 
    296         return(str); 
    297     else 
    298         return("Unknown"); 
    299     } 
    300  
    301267static char *str_from_bitfield_or_null(unsigned char octet, bit_map_t *map) 
    302268    { 
     
    547513char *ops_show_ss_preferred_compression(unsigned char octet) 
    548514    { 
    549     return(str_from_map(octet,compression_algorithm_map)); 
     515    return(ops_str_from_map(octet,compression_algorithm_map)); 
    550516    } 
    551517 
     
    604570char *ops_show_ss_preferred_ska(unsigned char octet) 
    605571    { 
    606     return(str_from_map(octet,symmetric_key_algorithm_map)); 
     572    return(ops_str_from_map(octet,symmetric_key_algorithm_map)); 
    607573    } 
    608574 
  • openpgpsdk/trunk/src/util.c

    r183 r211  
    99#include <assert.h> 
    1010#include <unistd.h> 
     11 
     12/** 
     13 * Searches the given map for the given type. 
     14 * Returns a human-readable descriptive string if found, 
     15 * returns NULL if not found 
     16 * 
     17 * It is the responsibility of the calling function to handle the 
     18 * error case sensibly (i.e. don't just print out the return string. 
     19 *  
     20 */ 
     21static char *str_from_map_or_null(int type, map_t *map) 
     22    { 
     23    map_t *row; 
     24 
     25    for ( row=map; row->string != NULL; row++ ) 
     26        if (row->type == type) 
     27            return row->string; 
     28    return NULL; 
     29    } 
     30 
     31/** 
     32 * \ingroup Utils 
     33 * 
     34 * Searches the given map for the given type. 
     35 * Returns a readable string if found, "Unknown" if not. 
     36 */ 
     37 
     38char *ops_str_from_map(int type, map_t *map) 
     39    { 
     40    char *str; 
     41    str=str_from_map_or_null(type,map); 
     42    if (str) 
     43        return(str); 
     44    else 
     45        return("Unknown"); 
     46    } 
    1147 
    1248void hexdump(const unsigned char *src,size_t length) 
     
    4682 * descriptor in "arg_" into the buffer starting at "dest" using the 
    4783 * rules contained in "flags" 
    48  *  
     84 * 
    4985 * \param       dest    Pointer to previously allocated buffer 
    5086 * \param       plength Number of bytes to try to read 
     
    5692 * \return      OPS_R_EARLY_EOF if not enough bytes were read, and OPS_RETURN_LENGTH was not set in "flags" 
    5793 * \return      OPS_R_OK        if expected length was read 
     94 * 
     95 * \sa enum opt_reader_ret_t 
     96 * 
    5897 * \todo change arg_ to typesafe?  
    5998 */