Path: blob/main/crypto/krb5/src/include/k5-input.h
34878 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* include/k5-input.h - k5input helper functions */2/*3* Copyright (C) 2014 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132#ifndef K5_INPUT_H33#define K5_INPUT_H3435#include "k5-platform.h"3637/*38* The k5input module defines helpers for safely consuming a fixed-sized block39* of memory. If an overrun or allocation failure occurs at any step,40* subsequent functions will return default values until the error is detected41* by looking at the status field.42*/4344struct k5input {45const unsigned char *ptr;46size_t len;47int32_t status;48};4950static inline void51k5_input_init(struct k5input *in, const void *ptr, size_t len)52{53in->ptr = ptr;54in->len = len;55in->status = 0;56}5758/* Only set the status value of in if it hasn't already been set, so status59* reflects the first thing to go wrong. */60static inline void61k5_input_set_status(struct k5input *in, int32_t status)62{63if (!in->status)64in->status = status;65}6667static inline const unsigned char *68k5_input_get_bytes(struct k5input *in, size_t len)69{70if (in->len < len)71k5_input_set_status(in, EINVAL);72if (in->status)73return NULL;74in->len -= len;75in->ptr += len;76return in->ptr - len;77}7879static inline unsigned char80k5_input_get_byte(struct k5input *in)81{82const unsigned char *ptr = k5_input_get_bytes(in, 1);83return (ptr == NULL) ? '\0' : *ptr;84}8586static inline uint16_t87k5_input_get_uint16_be(struct k5input *in)88{89const unsigned char *ptr = k5_input_get_bytes(in, 2);90return (ptr == NULL) ? 0 : load_16_be(ptr);91}9293static inline uint16_t94k5_input_get_uint16_le(struct k5input *in)95{96const unsigned char *ptr = k5_input_get_bytes(in, 2);97return (ptr == NULL) ? 0 : load_16_le(ptr);98}99100static inline uint16_t101k5_input_get_uint16_n(struct k5input *in)102{103const unsigned char *ptr = k5_input_get_bytes(in, 2);104return (ptr == NULL) ? 0 : load_16_n(ptr);105}106107static inline uint32_t108k5_input_get_uint32_be(struct k5input *in)109{110const unsigned char *ptr = k5_input_get_bytes(in, 4);111return (ptr == NULL) ? 0 : load_32_be(ptr);112}113114static inline uint32_t115k5_input_get_uint32_le(struct k5input *in)116{117const unsigned char *ptr = k5_input_get_bytes(in, 4);118return (ptr == NULL) ? 0 : load_32_le(ptr);119}120121static inline uint32_t122k5_input_get_uint32_n(struct k5input *in)123{124const unsigned char *ptr = k5_input_get_bytes(in, 4);125return (ptr == NULL) ? 0 : load_32_n(ptr);126}127128static inline uint64_t129k5_input_get_uint64_be(struct k5input *in)130{131const unsigned char *ptr = k5_input_get_bytes(in, 8);132return (ptr == NULL) ? 0 : load_64_be(ptr);133}134135static inline uint64_t136k5_input_get_uint64_le(struct k5input *in)137{138const unsigned char *ptr = k5_input_get_bytes(in, 8);139return (ptr == NULL) ? 0 : load_64_le(ptr);140}141142#endif /* K5_BUF_H */143144145