You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * @author Seth Hillbrand <seth@kipro-pcb.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 3
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <base64.h>
  22. #include <cstdint>
  23. #include <cstddef>
  24. #include <limits>
  25. #include <vector>
  26. namespace {
  27. static const uint8_t ENCODE_BASE64[64] = {
  28. 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  29. 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
  30. 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  31. 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  32. 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
  33. 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
  34. 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
  35. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F,
  36. };
  37. static const uint8_t E = -1;
  38. static const uint8_t DECODE_BASE64[128] = {
  39. /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF */
  40. E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
  41. E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
  42. E, E, E, E, E, E, E, E, E, E, E, 62, E, E, E, 63,
  43. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, E, E, E, E, E, E,
  44. E, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  45. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, E, E, E, E, E,
  46. E, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  47. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, E, E, E, E, E,
  48. };
  49. } // namespace
  50. namespace base64
  51. {
  52. size_t encode_length( size_t aInputLen )
  53. {
  54. return 4 * ( ( aInputLen + 2 ) / 3 ) + ( aInputLen + 2 ) % 3 - 2;
  55. }
  56. size_t decode_length( size_t aInputLen )
  57. {
  58. if( aInputLen % 4 == 1 )
  59. return 0;
  60. else
  61. return 3 * ( ( aInputLen + 2 ) / 4 ) + ( aInputLen + 2 ) % 4 - 2;
  62. }
  63. void encode( const std::vector<uint8_t>& aInput, std::vector<uint8_t>& aOutput )
  64. {
  65. size_t end = ( aInput.size() / 3 ) * 3;
  66. aOutput.reserve( encode_length( aInput.size() ) );
  67. for( size_t i = 0; i < end; i += 3 )
  68. {
  69. unsigned value = ( aInput[i] << 16 ) | ( aInput[i + 1] << 8 ) | aInput[i + 2] ;
  70. aOutput.emplace_back( ENCODE_BASE64[( value >> 18 ) & 0x3F] );
  71. aOutput.emplace_back( ENCODE_BASE64[( value >> 12 ) & 0x3F] );
  72. aOutput.emplace_back( ENCODE_BASE64[( value >> 6 ) & 0x3F] );
  73. aOutput.emplace_back( ENCODE_BASE64[value & 0x3F] );
  74. }
  75. size_t remainder = aInput.size() - end;
  76. if( remainder )
  77. {
  78. unsigned value = aInput[end];
  79. if( remainder == 2 )
  80. {
  81. value = ( value << 10 ) | ( aInput[end + 1] << 2 );
  82. aOutput.emplace_back( ENCODE_BASE64[( value >> 12 ) & 0x3F] );
  83. }
  84. else
  85. {
  86. value <<= 4;
  87. }
  88. aOutput.emplace_back( ENCODE_BASE64[( value >> 6 ) & 0x3F] );
  89. aOutput.emplace_back( ENCODE_BASE64[value & 0x3F] );
  90. }
  91. }
  92. void decode(const std::vector<uint8_t>& aInput, std::vector<uint8_t>& aOutput )
  93. {
  94. size_t end = ( aInput.size() / 4 ) * 4;
  95. size_t decode_size = decode_length( aInput.size() );
  96. if( !decode_size )
  97. return;
  98. aOutput.reserve( decode_size );
  99. for( size_t i = 0; i < end; i++ )
  100. {
  101. unsigned value = ( DECODE_BASE64[aInput[i] & 0x7F] << 18 ) |
  102. ( DECODE_BASE64[aInput[i + 1] & 0x7F] << 12 ) |
  103. ( DECODE_BASE64[aInput[i + 2] & 0x7F] << 6 ) |
  104. DECODE_BASE64[aInput[i + 3] & 0x7F];
  105. aOutput.emplace_back( ( value >> 16 ) & 0xff );
  106. aOutput.emplace_back( ( value >> 8 ) & 0xff );
  107. aOutput.emplace_back( value & 0xff );
  108. }
  109. size_t remainder = aInput.size() - end;
  110. if( remainder )
  111. {
  112. unsigned value = ( ( DECODE_BASE64[aInput[end] & 0x7F] ) << 6 ) |
  113. ( DECODE_BASE64[aInput[end + 1] & 0x7F] );
  114. if( remainder == 3 )
  115. {
  116. value = ( value << 6 ) | ( DECODE_BASE64[aInput[end + 2] & 0x7F] );
  117. value >>= 2;
  118. aOutput.emplace_back( ( value >> 8 ) & 0xff );
  119. }
  120. else
  121. {
  122. value >>= 4;
  123. }
  124. aOutput[0] = value & 0xff ;
  125. }
  126. }
  127. } // base64