robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include <assert.h> 2#include <stddef.h> 3#include <stdint.h> 4#include <string.h> 5 6#include "auto/config.h" // IWYU pragma: keep 7#include "nvim/ascii_defs.h" 8#include "nvim/base64.h" 9#include "nvim/memory.h" 10 11#ifdef HAVE_BE64TOH 12# include ENDIAN_INCLUDE_FILE 13#endif 14 15#include "base64.c.generated.h" 16 17static const char alphabet []= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ; 18 19// Indices are 1-based because we use 0 to indicate a letter that is not part of the alphabet 20static const uint8_t char_to_index [256 ]= { 21 ['A' ]= 1 , ['B' ]= 2 , ['C' ]= 3 , ['D' ]= 4 , ['E' ]= 5 , ['F' ]= 6 , ['G' ]= 7 , ['H' ]= 8 , 22 ['I' ]= 9 , ['J' ]= 10 , ['K' ]= 11 , ['L' ]= 12 , ['M' ]= 13 , ['N' ]= 14 , ['O' ]= 15 , ['P' ]= 16 , 23 ['Q' ]= 17 , ['R' ]= 18 , ['S' ]= 19 , ['T' ]= 20 , ['U' ]= 21 , ['V' ]= 22 , ['W' ]= 23 , ['X' ]= 24 , 24 ['Y' ]= 25 , ['Z' ]= 26 , ['a' ]= 27 , ['b' ]= 28 , ['c' ]= 29 , ['d' ]= 30 , ['e' ]= 31 , ['f' ]= 32 , 25 ['g' ]= 33 , ['h' ]= 34 , ['i' ]= 35 , ['j' ]= 36 , ['k' ]= 37 , ['l' ]= 38 , ['m' ]= 39 , ['n' ]= 40 , 26 ['o' ]= 41 , ['p' ]= 42 , ['q' ]= 43 , ['r' ]= 44 , ['s' ]= 45 , ['t' ]= 46 , ['u' ]= 47 , ['v' ]= 48 , 27 ['w' ]= 49 , ['x' ]= 50 , ['y' ]= 51 , ['z' ]= 52 , ['0' ]= 53 , ['1' ]= 54 , ['2' ]= 55 , ['3' ]= 56 , 28 ['4' ]= 57 , ['5' ]= 58 , ['6' ]= 59 , ['7' ]= 60 , ['8' ]= 61 , ['9' ]= 62 , ['+' ]= 63 , ['/' ]= 64 , 29}; 30 31#ifndef HAVE_BE64TOH 32static inline uint64_t vim_htobe64 (uint64_t host_64bits ) 33{ 34# ifdef ORDER_BIG_ENDIAN 35return host_64bits ; 36# else 37uint8_t * buf = (uint8_t * )& host_64bits ; 38uint64_t ret = 0 ; 39for (size_t i = 8 ;i ;i -- ) { 40ret |= ((uint64_t )buf [i - 1 ]) << ((8 - i )* 8 ); 41 } 42return ret ; 43# endif 44} 45 46static inline uint32_t vim_htobe32 (uint32_t host_32bits ) 47{ 48# ifdef ORDER_BIG_ENDIAN 49return host_32bits ; 50# else 51uint8_t * buf = (uint8_t * )& host_32bits ; 52uint32_t ret = 0 ; 53for (size_t i = 4 ;i ;i -- ) { 54ret |= ((uint32_t )buf [i - 1 ]) << ((4 - i )* 8 ); 55 } 56return ret ; 57# endif 58} 59# define htobe64 vim_htobe64 60# define htobe32 vim_htobe32 61#endif 62 63/// Encode a string using Base64. 64/// 65/// @param src String to encode 66/// @param src_len Length of the string 67/// @return Base64 encoded string 68char * base64_encode (const char * src ,size_t src_len ) 69FUNC_ATTR_NONNULL_ALL 70{ 71assert (src != NULL ); 72 73const size_t out_len = ((src_len + 2 ) /3 )* 4 ; 74char * dest = xmalloc (out_len + 1 ); 75 76size_t src_i = 0 ; 77size_t out_i = 0 ; 78 79const uint8_t * s = (const uint8_t * )src ; 80 81// Read 8 bytes at a time as much as we can 82for (;src_i + 7 < src_len ;src_i += 6 ) { 83uint64_t bits_h ; 84memcpy (& bits_h ,& s [src_i ],sizeof (uint64_t )); 85const uint64_t bits_be = htobe64 (bits_h ); 86dest [out_i + 0 ]= alphabet [(bits_be >>58 )& 0x3F ]; 87dest [out_i + 1 ]= alphabet [(bits_be >>52 )& 0x3F ]; 88dest [out_i + 2 ]= alphabet [(bits_be >>46 )& 0x3F ]; 89dest [out_i + 3 ]= alphabet [(bits_be >>40 )& 0x3F ]; 90dest [out_i + 4 ]= alphabet [(bits_be >>34 )& 0x3F ]; 91dest [out_i + 5 ]= alphabet [(bits_be >>28 )& 0x3F ]; 92dest [out_i + 6 ]= alphabet [(bits_be >>22 )& 0x3F ]; 93dest [out_i + 7 ]= alphabet [(bits_be >>16 )& 0x3F ]; 94out_i += sizeof (uint64_t ); 95 } 96 97for (;src_i + 3 < src_len ;src_i += 3 ) { 98uint32_t bits_h ; 99memcpy (& bits_h ,& s [src_i ],sizeof (uint32_t )); 100const uint32_t bits_be = htobe32 (bits_h ); 101dest [out_i + 0 ]= alphabet [(bits_be >>26 )& 0x3F ]; 102dest [out_i + 1 ]= alphabet [(bits_be >>20 )& 0x3F ]; 103dest [out_i + 2 ]= alphabet [(bits_be >>14 )& 0x3F ]; 104dest [out_i + 3 ]= alphabet [(bits_be >>8 )& 0x3F ]; 105out_i += sizeof (uint32_t ); 106 } 107 108if (src_i + 2 < src_len ) { 109dest [out_i + 0 ]= alphabet [s [src_i ] >>2 ]; 110dest [out_i + 1 ]= alphabet [((s [src_i ]& 0x3 ) <<4 ) | (s [src_i + 1 ] >>4 )]; 111dest [out_i + 2 ]= alphabet [(s [src_i + 1 ]& 0xF ) <<2 | (s [src_i + 2 ] >>6 )]; 112dest [out_i + 3 ]= alphabet [(s [src_i + 2 ]& 0x3F )]; 113out_i += 4 ; 114 }else if (src_i + 1 < src_len ) { 115dest [out_i + 0 ]= alphabet [s [src_i ] >>2 ]; 116dest [out_i + 1 ]= alphabet [((s [src_i ]& 0x3 ) <<4 ) | (s [src_i + 1 ] >>4 )]; 117dest [out_i + 2 ]= alphabet [(s [src_i + 1 ]& 0xF ) <<2 ]; 118out_i += 3 ; 119 }else if (src_i < src_len ) { 120dest [out_i + 0 ]= alphabet [s [src_i ] >>2 ]; 121dest [out_i + 1 ]= alphabet [(s [src_i ]& 0x3 ) <<4 ]; 122out_i += 2 ; 123 } 124 125for (;out_i < out_len ;out_i ++ ) { 126dest [out_i ]= '=' ; 127 } 128 129dest [out_len ]= NUL ; 130 131return dest ; 132} 133 134/// Decode a Base64 encoded string. 135/// 136/// The returned string is NOT null-terminated, because the decoded string may 137/// contain embedded NULLs. Use the output parameter out_lenp to determine the 138/// length of the returned string. 139/// 140/// @param src Base64 encoded string 141/// @param src_len Length of {src} 142/// @param [out] out_lenp Returns the length of the decoded string 143/// @return Decoded string 144char * base64_decode (const char * src ,size_t src_len ,size_t * out_lenp ) 145FUNC_ATTR_NONNULL_ALL 146{ 147assert (src != NULL ); 148assert (out_lenp != NULL ); 149 150char * dest = NULL ; 151 152if (src_len %4 != 0 ) { 153 gotoinvalid ; 154 } 155 156size_t out_len = (src_len /4 )* 3 ; 157if (src_len >=1 && src [src_len - 1 ]== '=' ) { 158out_len -- ; 159 } 160if (src_len >=2 && src [src_len - 2 ]== '=' ) { 161out_len -- ; 162 } 163 164const uint8_t * s = (const uint8_t * )src ; 165 166dest = xmalloc (out_len ); 167 168int acc = 0 ; 169int acc_len = 0 ; 170size_t out_i = 0 ; 171size_t src_i = 0 ; 172int leftover_i = -1 ; 173 174for (;src_i < src_len ;src_i ++ ) { 175const uint8_t c = s [src_i ]; 176const uint8_t d = char_to_index [c ]; 177if (d == 0 ) { 178if (c == '=' ) { 179leftover_i = (int )src_i ; 180break ; 181 } 182 gotoinvalid ; 183 } 184 185acc = ((acc <<6 )& 0xFFF )+ (d - 1 ); 186acc_len += 6 ; 187if (acc_len >=8 ) { 188acc_len -= 8 ; 189dest [out_i ]= (char )(acc >>acc_len ); 190out_i += 1 ; 191 } 192 } 193 194if (acc_len > 4 || ((acc & ((1 <<acc_len )- 1 ))!= 0 )) { 195 gotoinvalid ; 196 } 197 198if (leftover_i > -1 ) { 199int padding_len = acc_len /2 ; 200int padding_chars = 0 ; 201for (; (size_t )leftover_i < src_len ;leftover_i ++ ) { 202const uint8_t c = s [leftover_i ]; 203if (c != '=' ) { 204 gotoinvalid ; 205 } 206padding_chars += 1 ; 207 } 208 209if (padding_chars != padding_len ) { 210 gotoinvalid ; 211 } 212 } 213 214* out_lenp = out_len ; 215 216return dest ; 217 218invalid : 219if (dest ) { 220xfree ((void * )dest ); 221 } 222 223* out_lenp = 0 ; 224 225return NULL ; 226}