1 /*- 2 * Copyright (c) 2009 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Alistair Crooks (agc@NetBSD.org) 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 /* 30 * Copyright (c) 2005-2008 Nominet UK (www.nic.uk) 31 * All rights reserved. 32 * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted 33 * their moral rights under the UK Copyright Design and Patents Act 1988 to 34 * be recorded as the authors of this copyright work. 35 * 36 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 37 * use this file except in compliance with the License. 38 * 39 * You may obtain a copy of the License at 40 * http://www.apache.org/licenses/LICENSE-2.0 41 * 42 * Unless required by applicable law or agreed to in writing, software 43 * distributed under the License is distributed on an "AS IS" BASIS, 44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 45 * 46 * See the License for the specific language governing permissions and 47 * limitations under the License. 48 */ 49 50 /** \file 51 * Parser for OpenPGP packets - headers. 52 */ 53 54 #ifndef OPS_PACKET_PARSE_H 55 #define OPS_PACKET_PARSE_H 56 57 #include "types.h" 58 #include "packet.h" 59 60 /** __ops_region_t */ 61 typedef struct __ops_region_t { 62 struct __ops_region_t *parent; 63 unsigned length; 64 unsigned length_read; 65 unsigned last_read; /* !< length of last read, only valid 66 * in deepest child */ 67 unsigned indeterminate:1; 68 } __ops_region_t; 69 70 void __ops_init_subregion(__ops_region_t * subregion, __ops_region_t * region); 71 72 /** __ops_parse_callback_return_t */ 73 typedef enum { 74 OPS_RELEASE_MEMORY, 75 OPS_KEEP_MEMORY, 76 OPS_FINISHED 77 } __ops_parse_cb_return_t; 78 79 typedef struct __ops_parse_cb_info __ops_callback_data_t; 80 81 typedef __ops_parse_cb_return_t __ops_parse_cb_t(const __ops_packet_t *, __ops_callback_data_t *); 82 83 __ops_parse_cb_return_t 84 get_passphrase_cb(const __ops_packet_t *, __ops_callback_data_t *); 85 86 typedef struct __ops_parseinfo __ops_parseinfo_t; 87 typedef struct __ops_reader __ops_reader_t; 88 typedef struct __ops_cryptinfo __ops_cryptinfo_t; 89 90 /* 91 A reader MUST read at least one byte if it can, and should read up 92 to the number asked for. Whether it reads more for efficiency is 93 its own decision, but if it is a stacked reader it should never 94 read more than the length of the region it operates in (which it 95 would have to be given when it is stacked). 96 97 If a read is short because of EOF, then it should return the short 98 read (obviously this will be zero on the second attempt, if not the 99 first). Because a reader is not obliged to do a full read, only a 100 zero return can be taken as an indication of EOF. 101 102 If there is an error, then the callback should be notified, the 103 error stacked, and -1 should be returned. 104 105 Note that although length is a size_t, a reader will never be asked 106 to read more than INT_MAX in one go. 107 108 */ 109 110 typedef int __ops_reader_func_t(void *, size_t, __ops_error_t **, __ops_reader_t *, __ops_callback_data_t *); 111 112 typedef void __ops_reader_destroyer_t(__ops_reader_t *); 113 114 __ops_parseinfo_t *__ops_parseinfo_new(void); 115 void __ops_parseinfo_delete(__ops_parseinfo_t *); 116 __ops_error_t *__ops_parseinfo_get_errors(__ops_parseinfo_t *); 117 __ops_crypt_t *__ops_parse_get_decrypt(__ops_parseinfo_t *); 118 119 void __ops_parse_cb_set(__ops_parseinfo_t *, __ops_parse_cb_t *, void *); 120 void __ops_parse_cb_push(__ops_parseinfo_t *, __ops_parse_cb_t *, void *); 121 void *__ops_parse_cb_get_arg(__ops_callback_data_t *); 122 void *__ops_parse_cb_get_errors(__ops_callback_data_t *); 123 void __ops_reader_set(__ops_parseinfo_t *, __ops_reader_func_t *, __ops_reader_destroyer_t *, void *); 124 void __ops_reader_push(__ops_parseinfo_t *, __ops_reader_func_t *, __ops_reader_destroyer_t *, void *); 125 void __ops_reader_pop(__ops_parseinfo_t *); 126 127 void *__ops_reader_get_arg(__ops_reader_t *); 128 129 __ops_parse_cb_return_t __ops_parse_cb(const __ops_packet_t *, __ops_callback_data_t *); 130 __ops_parse_cb_return_t __ops_parse_stacked_cb(const __ops_packet_t *, __ops_callback_data_t *); 131 __ops_reader_t *__ops_parse_get_rinfo(__ops_parseinfo_t *); 132 133 int __ops_parse(__ops_parseinfo_t *, int); 134 135 void __ops_parse_and_validate(__ops_parseinfo_t *); 136 137 /** Used to specify whether subpackets should be returned raw, parsed or ignored. 138 */ 139 typedef enum { 140 OPS_PARSE_RAW, /* !< Callback Raw */ 141 OPS_PARSE_PARSED, /* !< Callback Parsed */ 142 OPS_PARSE_IGNORE /* !< Don't callback */ 143 } __ops_parse_type_t; 144 145 void 146 __ops_parse_options(__ops_parseinfo_t *, __ops_content_tag_t, __ops_parse_type_t); 147 148 bool __ops_limited_read(unsigned char *, size_t, __ops_region_t *, __ops_error_t **, __ops_reader_t *, __ops_callback_data_t *); 149 bool __ops_stacked_limited_read(unsigned char *, unsigned, __ops_region_t *, __ops_error_t **, __ops_reader_t *, __ops_callback_data_t *); 150 void __ops_parse_hash_init(__ops_parseinfo_t *, __ops_hash_alg_t, const unsigned char *); 151 void __ops_parse_hash_data(__ops_parseinfo_t *, const void *, size_t); 152 void __ops_parse_hash_finish(__ops_parseinfo_t *); 153 __ops_hash_t *__ops_parse_hash_find(__ops_parseinfo_t *, const unsigned char *); 154 155 __ops_reader_func_t __ops_stacked_read; 156 157 int __ops_decompress(__ops_region_t *, __ops_parseinfo_t *, __ops_compression_type_t); 158 bool __ops_write_compressed(const unsigned char *, const unsigned int, __ops_createinfo_t *); 159 160 #endif 161