Changeset 209
- Timestamp:
- 07/13/05 14:41:42
- Files:
-
- openpgpsdk/trunk/examples/packet-dump.c (modified) (3 diffs)
- openpgpsdk/trunk/include/packet-parse.h (modified) (1 diff)
- openpgpsdk/trunk/src/packet-parse.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/examples/packet-dump.c
r208 r209 202 202 } 203 203 204 static 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 204 227 static ops_parse_callback_return_t 205 228 callback(const ops_parser_content_t *content_,void *arg_) … … 213 236 case OPS_PARSER_ERROR: 214 237 printf("parse error: %s\n",content->error.error); 238 break; 239 240 case OPS_PARSER_PACKET_END: 241 print_packet(&content->packet); 215 242 break; 216 243 … … 719 746 720 747 if (!ops_parse_and_save_errs(&opt,&errors)) 748 { 721 749 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 } 725 758 726 759 ops_ulong_list_free(&errors); openpgpsdk/trunk/include/packet-parse.h
r208 r209 65 65 int ops_parse(ops_parse_options_t *opt); 66 66 int ops_parse_and_save_errs(ops_parse_options_t *opt,ops_ulong_list_t *errs); 67 int ops_parse_errs(ops_parse_options_t *opt,ops_ulong_list_t *errs); 68 67 69 void ops_parse_and_validate(ops_parse_options_t *opt); 68 70 openpgpsdk/trunk/src/packet-parse.c
r208 r209 14 14 #include <stdlib.h> 15 15 #include <string.h> 16 #include <unistd.h> 17 #include <errno.h> 16 18 17 19 #ifdef DMALLOC … … 1648 1650 * function to handle the content. 1649 1651 * 1650 * \param *reader Our reader1651 * \param *cb The callback1652 1652 * \param *opt Parsing options 1653 * \param *pktlen On return, will contain number of bytes in packet 1653 1654 * \return 1 on success, 0 on error, -1 on EOF 1654 1655 */ 1655 static int ops_parse_one_packet(ops_parse_options_t *opt, unsigned long * offset)1656 static int ops_parse_one_packet(ops_parse_options_t *opt, unsigned long *pktlen) 1656 1657 { 1657 1658 char ptag[1]; … … 1667 1668 return -1; 1668 1669 1669 * offset+=one;1670 *pktlen=0; 1670 1671 1671 1672 assert(ret == OPS_R_OK); … … 1683 1684 if(!read_new_length(&C.ptag.length,opt)) 1684 1685 return 0; 1685 *offset+=C.ptag.length; 1686 1686 1687 } 1687 1688 else … … 1695 1696 ret=read_scalar(&C.ptag.length,1,opt); 1696 1697 assert(ret == OPS_R_OK); 1697 *offset+=1;1698 1698 break; 1699 1699 … … 1701 1701 ret=read_scalar(&C.ptag.length,2,opt); 1702 1702 assert(ret == OPS_R_OK); 1703 *offset+=2;1704 1703 break; 1705 1704 1706 1705 case OPS_PTAG_OF_LT_FOUR_BYTE: 1707 1706 ret=read_scalar(&C.ptag.length,4,opt); 1708 *offset+=4;1709 1707 assert(ret == OPS_R_OK); 1710 1708 break; … … 1768 1766 } 1769 1767 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,®ion,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 1771 1791 if(opt->accumulate) 1772 1792 { … … 1779 1799 opt->alength=0; 1780 1800 1781 /*1782 * update offset for packet contents1783 */1784 1785 *offset+=region.length;1786 1787 /* Ensure that the entire packet has been consumed1788 */1789 1790 if (region.length != region.length_read)1791 {1792 ops_data_t remainder;1793 1794 if (read_data(&remainder,®ion,opt))1795 {1796 /* now throw it away */1797 data_free(&remainder);1798 ERR("Remainder of packet consumed and discarded.");1799 }1800 else1801 ERR("Problem consuming remainder of error packet.");1802 }1803 1801 return r ? 1 : 0; 1804 1802 } … … 1847 1845 do 1848 1846 { 1849 pktlen=0;1850 1847 r=ops_parse_one_packet(opt,&pktlen); 1851 1848 if (!r) … … 1858 1855 1859 1856 /** 1857 * 1858 * \return 1 if success, 0 otherwise 1859 */ 1860 1861 int 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 /** 1860 1918 * \ingroup Parse 1861 1919 *
