1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* include/k5-input.h - k5input helper functions */
4 * Copyright (C) 2014 by the Massachusetts Institute of Technology.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "k5-platform.h"
39 * The k5input module defines helpers for safely consuming a fixed-sized block
40 * of memory. If an overrun or allocation failure occurs at any step,
41 * subsequent functions will return default values until the error is detected
42 * by looking at the status field.
46 const unsigned char *ptr;
52 k5_input_init(struct k5input *in, const void *ptr, size_t len)
59 /* Only set the status value of in if it hasn't already been set, so status
60 * reflects the first thing to go wrong. */
62 k5_input_set_status(struct k5input *in, int32_t status)
68 static inline const unsigned char *
69 k5_input_get_bytes(struct k5input *in, size_t len)
72 k5_input_set_status(in, EINVAL);
80 static inline unsigned char
81 k5_input_get_byte(struct k5input *in)
83 const unsigned char *ptr = k5_input_get_bytes(in, 1);
84 return (ptr == NULL) ? '\0' : *ptr;
87 static inline uint16_t
88 k5_input_get_uint16_be(struct k5input *in)
90 const unsigned char *ptr = k5_input_get_bytes(in, 2);
91 return (ptr == NULL) ? 0 : load_16_be(ptr);
94 static inline uint16_t
95 k5_input_get_uint16_le(struct k5input *in)
97 const unsigned char *ptr = k5_input_get_bytes(in, 2);
98 return (ptr == NULL) ? 0 : load_16_le(ptr);
101 static inline uint16_t
102 k5_input_get_uint16_n(struct k5input *in)
104 const unsigned char *ptr = k5_input_get_bytes(in, 2);
105 return (ptr == NULL) ? 0 : load_16_n(ptr);
108 static inline uint32_t
109 k5_input_get_uint32_be(struct k5input *in)
111 const unsigned char *ptr = k5_input_get_bytes(in, 4);
112 return (ptr == NULL) ? 0 : load_32_be(ptr);
115 static inline uint32_t
116 k5_input_get_uint32_le(struct k5input *in)
118 const unsigned char *ptr = k5_input_get_bytes(in, 4);
119 return (ptr == NULL) ? 0 : load_32_le(ptr);
122 static inline uint32_t
123 k5_input_get_uint32_n(struct k5input *in)
125 const unsigned char *ptr = k5_input_get_bytes(in, 4);
126 return (ptr == NULL) ? 0 : load_32_n(ptr);
129 static inline uint64_t
130 k5_input_get_uint64_be(struct k5input *in)
132 const unsigned char *ptr = k5_input_get_bytes(in, 8);
133 return (ptr == NULL) ? 0 : load_64_be(ptr);
136 static inline uint64_t
137 k5_input_get_uint64_le(struct k5input *in)
139 const unsigned char *ptr = k5_input_get_bytes(in, 8);
140 return (ptr == NULL) ? 0 : load_64_le(ptr);
143 #endif /* K5_BUF_H */