Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:13

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSImplBase64
0007  *
0008  * @brief This header file provides the interfaces of the
0009  *   @ref RTEMSImplBase64.
0010  */
0011 
0012 /*
0013  * Copyright (C) 2020, 2024 embedded brains GmbH & Co. KG
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifndef _RTEMS_BASE64_H
0038 #define _RTEMS_BASE64_H
0039 
0040 #include <rtems/dev/io.h>
0041 
0042 #ifdef __cplusplus
0043 extern "C" {
0044 #endif /* __cplusplus */
0045 
0046 /**
0047  * @defgroup RTEMSImplBase64 Base64 Encoding and Decoding
0048  *
0049  * @ingroup RTEMSImpl
0050  *
0051  * @brief This group contains support functions for base64 and base64url
0052  *   encoding and decoding.
0053  *
0054  * @{
0055  */
0056 
0057 /**
0058  * @brief Maps a 6-bit integer to the corresponding base64 encoding.
0059  */
0060 extern const uint8_t _Base64_Encoding[ 64 ];
0061 
0062 /**
0063  * @brief Maps a 6-bit integer to the corresponding base64url encoding.
0064  */
0065 extern const uint8_t _Base64url_Encoding[ 64 ];
0066 
0067 /**
0068  * @brief Outputs the source buffer in base64 encoding.
0069  *
0070  * After word length of output characters produced by the encoding a word break
0071  * is produced.
0072  *
0073  * @param put_char is the put character function used to output the encoded
0074  *   source buffer.
0075  *
0076  * @param arg is the argument passed to the put character function.
0077  *
0078  * @param src is the pointer to the source buffer begin.
0079  *
0080  * @param srclen is the length of the source buffer in bytes.
0081  *
0082  * @param wordbreak is the word break string.
0083  *
0084  * @param wordlen is the word length in bytes.  If the word length is less than
0085  *   four, then a word length of four will be used.
0086  *
0087  * @return Returns the count of output characters.
0088  */
0089 int _Base64_Encode(
0090   IO_Put_char  put_char,
0091   void        *arg,
0092   const void  *src,
0093   size_t       len,
0094   const char  *wordbreak,
0095   int          wordlen
0096 );
0097 
0098 /**
0099  * @brief Outputs the source buffer in base64url encoding.
0100  *
0101  * After word length of output characters produced by the encoding a word break
0102  * is produced.
0103  *
0104  * @param put_char is the put character function used to output the encoded
0105  *   source buffer.
0106  *
0107  * @param arg is the argument passed to the put character function.
0108  *
0109  * @param src is the pointer to the source buffer begin.
0110  *
0111  * @param srclen is the length of the source buffer in bytes.
0112  *
0113  * @param wordbreak is the word break string.
0114  *
0115  * @param wordlen is the word length in bytes.  If the word length is less than
0116  *   four, then a word length of four will be used.
0117  *
0118  * @return Returns the count of output characters.
0119  */
0120 int _Base64url_Encode(
0121   IO_Put_char  put_char,
0122   void        *arg,
0123   const void  *src,
0124   size_t       len,
0125   const char  *wordbreak,
0126   int          wordlen
0127 );
0128 
0129 /**
0130  * @brief Represents the base64 and base64url decoder state.
0131  */
0132 typedef enum {
0133   BASE64_DECODE_STATE_0,
0134   BASE64_DECODE_STATE_1,
0135   BASE64_DECODE_STATE_2,
0136   BASE64_DECODE_STATE_3
0137 } Base64_Decode_state;
0138 
0139 /**
0140  * @brief Contains the base64 and base64url decoder control.
0141  */
0142 typedef struct {
0143   Base64_Decode_state state;
0144   uint8_t            *target;
0145   const uint8_t      *target_end;
0146 } Base64_Decode_control;
0147 
0148 /**
0149  * @brief Maps a 7-bit character to the associated 6-bit integer as defined by
0150  *   the base64 or base64url encoding or a special value.
0151  */
0152 extern const uint8_t _Base64_Decoding[ 128 ];
0153 
0154 /**
0155  * @brief Initializes the base64 decoder.
0156  *
0157  * @param[out] self is the base64 decoder control to initialize.
0158  *
0159  * @param[out] target is the base address of the target area for decoding.
0160  *
0161  * @param target_size is the size in bytes of the target area for decoding.
0162  */
0163 void _Base64_Decode_initialize(
0164   Base64_Decode_control *self,
0165   uint8_t               *target,
0166   size_t                 target_size
0167 );
0168 
0169 /**
0170  * @brief Represents the base64 and base64url decoder status.
0171  */
0172 typedef enum {
0173   BASE64_DECODE_SUCCESS,
0174   BASE64_DECODE_OVERFLOW,
0175   BASE64_DECODE_INVALID_INPUT
0176 } Base64_Decode_status;
0177 
0178 /**
0179  * @brief Decodes the character.
0180  *
0181  * The decoder accepts base64 and base64url encodings.  White space is ignored.
0182  *
0183  * @param[in, out] self is the base64 decoder control.
0184  *
0185  * @param ch is the character to decode.
0186  */
0187 Base64_Decode_status _Base64_Decode(
0188   Base64_Decode_control *self,
0189   char                   ch
0190 );
0191 
0192 /** @} */
0193 
0194 #ifdef __cplusplus
0195 }
0196 #endif /* __cplusplus */
0197 
0198 #endif /* _RTEMS_BASE64_H */