Changeset 209

Show
Ignore:
Timestamp:
07/13/05 14:41:42
Author:
rachel
Message:

Output error packets again at end of packet-dump, together with a hexdump of contents.

Files:

Legend:

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

    r208 r209  
    202202    } 
    203203 
     204static void print_packet(const ops_packet_t *packet) 
     205    { 
     206    unsigned char *cur; 
     207    int i; 
     208    int rem; 
     209    int blksz=4; 
     210 
     211    printf("\nhexdump of packet contents follows:\n"); 
     212 
     213 
     214    for (i=1,cur=packet->raw; cur<(packet->raw+packet->length); cur+=blksz,i++) 
     215        { 
     216        rem = packet->raw+packet->length-cur; 
     217        hexdump(cur,rem<=blksz ? rem : blksz); 
     218        printf(" "); 
     219        if (!(i%8)) 
     220            printf("\n"); 
     221         
     222        } 
     223     
     224    printf("\n"); 
     225    } 
     226 
    204227static ops_parse_callback_return_t 
    205228callback(const ops_parser_content_t *content_,void *arg_) 
     
    213236    case OPS_PARSER_ERROR: 
    214237        printf("parse error: %s\n",content->error.error); 
     238        break; 
     239 
     240    case OPS_PARSER_PACKET_END: 
     241        print_packet(&content->packet); 
    215242        break; 
    216243 
     
    719746 
    720747    if (!ops_parse_and_save_errs(&opt,&errors)) 
     748        { 
    721749        printf("\n*** Warning: errors were found when parsing input\n"); 
    722  
    723     for (i=0; i<errors.used; i++) 
    724         printf("offset: %lu\n", errors.ulongs[i]); 
     750        printf("\nError packets will be printed again, together with hexdump\n"); 
     751 
     752        for (i=0; i<errors.used; i++) 
     753            printf("offset: %lu\n", errors.ulongs[i]); 
     754 
     755        if (!ops_parse_errs(&opt,&errors)) 
     756            printf("\n*** Warning: problems found when parsing errors\n"); 
     757        } 
    725758 
    726759    ops_ulong_list_free(&errors); 
  • openpgpsdk/trunk/include/packet-parse.h

    r208 r209  
    6565int ops_parse(ops_parse_options_t *opt); 
    6666int ops_parse_and_save_errs(ops_parse_options_t *opt,ops_ulong_list_t *errs); 
     67int ops_parse_errs(ops_parse_options_t *opt,ops_ulong_list_t *errs); 
     68 
    6769void ops_parse_and_validate(ops_parse_options_t *opt); 
    6870 
  • openpgpsdk/trunk/src/packet-parse.c

    r208 r209  
    1414#include <stdlib.h> 
    1515#include <string.h> 
     16#include <unistd.h> 
     17#include <errno.h> 
    1618 
    1719#ifdef DMALLOC 
     
    16481650 * function to handle the content. 
    16491651 * 
    1650  * \param *reader       Our reader 
    1651  * \param *cb           The callback 
    16521652 * \param *opt          Parsing options 
     1653 * \param *pktlen       On return, will contain number of bytes in packet 
    16531654 * \return              1 on success, 0 on error, -1 on EOF 
    16541655 */ 
    1655 static int ops_parse_one_packet(ops_parse_options_t *opt, unsigned long *offset
     1656static int ops_parse_one_packet(ops_parse_options_t *opt, unsigned long *pktlen
    16561657    { 
    16571658    char ptag[1]; 
     
    16671668        return -1; 
    16681669 
    1669     *offset+=one
     1670    *pktlen=0
    16701671 
    16711672    assert(ret == OPS_R_OK); 
     
    16831684        if(!read_new_length(&C.ptag.length,opt)) 
    16841685            return 0; 
    1685         *offset+=C.ptag.length; 
     1686 
    16861687        } 
    16871688    else 
     
    16951696            ret=read_scalar(&C.ptag.length,1,opt); 
    16961697            assert(ret == OPS_R_OK); 
    1697             *offset+=1; 
    16981698            break; 
    16991699 
     
    17011701            ret=read_scalar(&C.ptag.length,2,opt); 
    17021702            assert(ret == OPS_R_OK); 
    1703             *offset+=2; 
    17041703            break; 
    17051704 
    17061705        case OPS_PTAG_OF_LT_FOUR_BYTE: 
    17071706            ret=read_scalar(&C.ptag.length,4,opt); 
    1708             *offset+=4; 
    17091707            assert(ret == OPS_R_OK); 
    17101708            break; 
     
    17681766        } 
    17691767 
    1770     // \todo XXX: shouldn't we check that the entire packet has been consumed? 
     1768    /* Ensure that the entire packet has been consumed  
     1769     */ 
     1770 
     1771    if (region.length != region.length_read) 
     1772        { 
     1773        ops_data_t remainder; 
     1774 
     1775        if (read_data(&remainder,&region,opt)) 
     1776            { 
     1777            /* now throw it away */ 
     1778            data_free(&remainder); 
     1779            // XXX cannot give error message - it prevents hexdump occurring        ERR("Remainder of packet consumed and discarded."); 
     1780            } 
     1781        //      else 
     1782        //  ERR("Problem consuming remainder of error packet."); 
     1783        } 
     1784 
     1785    /* set pktlen */ 
     1786 
     1787    *pktlen=opt->alength; 
     1788 
     1789    /* do callback on entire packet, if desired */ 
     1790 
    17711791    if(opt->accumulate) 
    17721792        { 
     
    17791799    opt->alength=0; 
    17801800         
    1781     /* 
    1782      * update offset for packet contents 
    1783      */ 
    1784  
    1785     *offset+=region.length; 
    1786  
    1787     /* Ensure that the entire packet has been consumed  
    1788      */ 
    1789  
    1790     if (region.length != region.length_read) 
    1791         { 
    1792         ops_data_t remainder; 
    1793  
    1794         if (read_data(&remainder,&region,opt)) 
    1795             { 
    1796             /* now throw it away */ 
    1797             data_free(&remainder); 
    1798             ERR("Remainder of packet consumed and discarded."); 
    1799             } 
    1800         else 
    1801             ERR("Problem consuming remainder of error packet."); 
    1802         } 
    18031801    return r ? 1 : 0; 
    18041802    } 
     
    18471845    do 
    18481846        { 
    1849         pktlen=0; 
    18501847        r=ops_parse_one_packet(opt,&pktlen); 
    18511848        if (!r) 
     
    18581855 
    18591856/** 
     1857 * 
     1858 * \return 1 if success, 0 otherwise 
     1859 */ 
     1860 
     1861int ops_parse_errs(ops_parse_options_t *opt, ops_ulong_list_t *errs) 
     1862    { 
     1863    int err; 
     1864    int r; 
     1865    unsigned long pktlen; 
     1866    ops_reader_fd_arg_t *arg; 
     1867 
     1868    int orig_acc; 
     1869 
     1870    /* can only handle ops_reader_fd for now */ 
     1871 
     1872    if (opt->reader!=ops_reader_fd) 
     1873        { 
     1874        printf("ops_parse_errs: can only handle ops_reader_fd\n"); 
     1875        return 0; 
     1876        } 
     1877 
     1878    arg=opt->reader_arg; 
     1879 
     1880    /* store current state of accumulate flag */ 
     1881 
     1882    orig_acc=opt->accumulate; 
     1883 
     1884    /* set accumulate flag */ 
     1885 
     1886    opt->accumulate=1; 
     1887 
     1888    /* now parse each error in turn. */ 
     1889 
     1890    for (err=0; err<errs->used; err++) 
     1891        { 
     1892 
     1893        //      printf("\n***\n*** Error at offset %lu \n***\n",errs->ulongs[err]); 
     1894 
     1895        /* move stream to offset of error */ 
     1896 
     1897        r=lseek(arg->fd,errs->ulongs[err],SEEK_SET); 
     1898        if (r==-1) 
     1899            { 
     1900            printf("error %d in first lseek to offset\n", errno); 
     1901            return 0; 
     1902            } 
     1903 
     1904        /* parse packet */ 
     1905 
     1906        ops_parse_one_packet(opt,&pktlen); 
     1907 
     1908 
     1909        /* restore accumulate flag original value */ 
     1910        opt->accumulate=orig_acc; 
     1911 
     1912        } 
     1913 
     1914    return 1; 
     1915    } 
     1916 
     1917/** 
    18601918 * \ingroup Parse 
    18611919 *