1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_Base64_h__ 8 #define mozilla_Base64_h__ 9 10 #include "nsString.h" 11 12 class nsIInputStream; 13 14 namespace mozilla { 15 16 [[nodiscard]] nsresult Base64EncodeInputStream(nsIInputStream* aInputStream, 17 nsACString& aDest, 18 uint32_t aCount, 19 uint32_t aOffset = 0); 20 [[nodiscard]] nsresult Base64EncodeInputStream(nsIInputStream* aInputStream, 21 nsAString& aDest, 22 uint32_t aCount, 23 uint32_t aOffset = 0); 24 25 // Encode 8-bit data of a given length and append the Base64 encoded data to 26 // aBase64. 27 [[nodiscard]] nsresult Base64EncodeAppend(const char* aBinary, 28 uint32_t aBinaryLen, 29 nsAString& aBase64); 30 [[nodiscard]] nsresult Base64EncodeAppend(const char* aBinary, 31 uint32_t aBinaryLen, 32 nsACString& aBase64); 33 [[nodiscard]] nsresult Base64EncodeAppend(const nsACString& aBinary, 34 nsACString& aBase64); 35 [[nodiscard]] nsresult Base64EncodeAppend(const nsACString& aBinary, 36 nsAString& aBase64); 37 38 [[nodiscard]] nsresult Base64Encode(const char* aBinary, uint32_t aBinaryLen, 39 char** aBase64); 40 [[nodiscard]] nsresult Base64Encode(const char* aBinary, uint32_t aBinaryLen, 41 nsACString& aBase64); 42 [[nodiscard]] nsresult Base64Encode(const char* aBinary, uint32_t aBinaryLen, 43 nsAString& aBase64); 44 [[nodiscard]] nsresult Base64Encode(const nsACString& aBinary, 45 nsACString& aBase64); 46 [[nodiscard]] nsresult Base64Encode(const nsACString& aBinary, 47 nsAString& aBase64); 48 49 // The high bits of any characters in aBinary are dropped. 50 [[nodiscard]] nsresult Base64Encode(const nsAString& aBinary, 51 nsAString& aBase64); 52 53 [[nodiscard]] nsresult Base64Decode(const char* aBase64, uint32_t aBase64Len, 54 char** aBinary, uint32_t* aBinaryLen); 55 [[nodiscard]] nsresult Base64Decode(const nsACString& aBase64, 56 nsACString& aBinary); 57 58 // The high bits of any characters in aBase64 are dropped. 59 [[nodiscard]] nsresult Base64Decode(const nsAString& aBase64, 60 nsAString& aBinary); 61 [[nodiscard]] nsresult Base64Decode(const nsAString& aBase64, 62 nsACString& aBinary); 63 64 enum class Base64URLEncodePaddingPolicy { 65 Include, 66 Omit, 67 }; 68 69 /** 70 * Converts |aBinary| to an unpadded, Base64 URL-encoded string per RFC 4648. 71 * Aims to encode the data in constant time. The caller retains ownership 72 * of |aBinary|. 73 */ 74 [[nodiscard]] nsresult Base64URLEncode( 75 uint32_t aBinaryLen, const uint8_t* aBinary, 76 Base64URLEncodePaddingPolicy aPaddingPolicy, nsACString& aBase64); 77 78 enum class Base64URLDecodePaddingPolicy { 79 Require, 80 Ignore, 81 Reject, 82 }; 83 84 /** 85 * Decodes a Base64 URL-encoded |aBase64| into |aBinary|. 86 */ 87 [[nodiscard]] nsresult Base64URLDecode( 88 const nsACString& aBase64, Base64URLDecodePaddingPolicy aPaddingPolicy, 89 FallibleTArray<uint8_t>& aBinary); 90 91 } // namespace mozilla 92 93 #endif 94