1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* include/k5-input.h - k5input helper functions */
3 /*
4 * Copyright (C) 2014 by the Massachusetts Institute of Technology.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
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.
31 */
32
33 #ifndef K5_INPUT_H
34 #define K5_INPUT_H
35
36 #include "k5-platform.h"
37
38 /*
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.
43 */
44
45 struct k5input {
46 const unsigned char *ptr;
47 size_t len;
48 int32_t status;
49 };
50
51 static inline void
k5_input_init(struct k5input * in,const void * ptr,size_t len)52 k5_input_init(struct k5input *in, const void *ptr, size_t len)
53 {
54 in->ptr = ptr;
55 in->len = len;
56 in->status = 0;
57 }
58
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. */
61 static inline void
k5_input_set_status(struct k5input * in,int32_t status)62 k5_input_set_status(struct k5input *in, int32_t status)
63 {
64 if (!in->status)
65 in->status = status;
66 }
67
68 static inline const unsigned char *
k5_input_get_bytes(struct k5input * in,size_t len)69 k5_input_get_bytes(struct k5input *in, size_t len)
70 {
71 if (in->len < len)
72 k5_input_set_status(in, EINVAL);
73 if (in->status)
74 return NULL;
75 in->len -= len;
76 in->ptr += len;
77 return in->ptr - len;
78 }
79
80 static inline unsigned char
k5_input_get_byte(struct k5input * in)81 k5_input_get_byte(struct k5input *in)
82 {
83 const unsigned char *ptr = k5_input_get_bytes(in, 1);
84 return (ptr == NULL) ? '\0' : *ptr;
85 }
86
87 static inline uint16_t
k5_input_get_uint16_be(struct k5input * in)88 k5_input_get_uint16_be(struct k5input *in)
89 {
90 const unsigned char *ptr = k5_input_get_bytes(in, 2);
91 return (ptr == NULL) ? 0 : load_16_be(ptr);
92 }
93
94 static inline uint16_t
k5_input_get_uint16_le(struct k5input * in)95 k5_input_get_uint16_le(struct k5input *in)
96 {
97 const unsigned char *ptr = k5_input_get_bytes(in, 2);
98 return (ptr == NULL) ? 0 : load_16_le(ptr);
99 }
100
101 static inline uint16_t
k5_input_get_uint16_n(struct k5input * in)102 k5_input_get_uint16_n(struct k5input *in)
103 {
104 const unsigned char *ptr = k5_input_get_bytes(in, 2);
105 return (ptr == NULL) ? 0 : load_16_n(ptr);
106 }
107
108 static inline uint32_t
k5_input_get_uint32_be(struct k5input * in)109 k5_input_get_uint32_be(struct k5input *in)
110 {
111 const unsigned char *ptr = k5_input_get_bytes(in, 4);
112 return (ptr == NULL) ? 0 : load_32_be(ptr);
113 }
114
115 static inline uint32_t
k5_input_get_uint32_le(struct k5input * in)116 k5_input_get_uint32_le(struct k5input *in)
117 {
118 const unsigned char *ptr = k5_input_get_bytes(in, 4);
119 return (ptr == NULL) ? 0 : load_32_le(ptr);
120 }
121
122 static inline uint32_t
k5_input_get_uint32_n(struct k5input * in)123 k5_input_get_uint32_n(struct k5input *in)
124 {
125 const unsigned char *ptr = k5_input_get_bytes(in, 4);
126 return (ptr == NULL) ? 0 : load_32_n(ptr);
127 }
128
129 static inline uint64_t
k5_input_get_uint64_be(struct k5input * in)130 k5_input_get_uint64_be(struct k5input *in)
131 {
132 const unsigned char *ptr = k5_input_get_bytes(in, 8);
133 return (ptr == NULL) ? 0 : load_64_be(ptr);
134 }
135
136 static inline uint64_t
k5_input_get_uint64_le(struct k5input * in)137 k5_input_get_uint64_le(struct k5input *in)
138 {
139 const unsigned char *ptr = k5_input_get_bytes(in, 8);
140 return (ptr == NULL) ? 0 : load_64_le(ptr);
141 }
142
143 #endif /* K5_BUF_H */
144