Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Base64.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Cryptography_Base64_h_
5#define _Stroika_Foundation_Cryptography_Base64_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <string>
10#include <vector>
11
13#include "Stroika/Foundation/Common/Common.h"
17
18/**
19 * \file
20 *
21 * TODO:
22 *
23 * @todo EncodeBase64 should return STROIKA string, or BLOB - not std::string? Or maybe std::string
24 * best - but document why - cuz always ascii data so more compact?? No - not good reason cuz
25 * we can construct a SUBTYPE of String that takes advantage of it.
26 *
27 * @todo DecodeBase64() should have an overload taking BinaryInputStream, and EncodeBase64()
28 * should have an overload with BinaryOutputStream (keeping existing overloads).
29 *
30 * @todo Tons todo optimizing this implementation (to not use temporary objects and
31 * avoiding copying).
32 *
33 */
34
35namespace Stroika::Foundation::Cryptography::Encoding::Algorithm::Base64 {
36
37 Memory::BLOB Decode (span<const char> s);
38 Memory::BLOB Decode (const string& s);
39 Memory::BLOB Decode (const u8string& s);
40 Memory::BLOB Decode (const Characters::String& s);
41 void Decode (const string& s, const Streams::OutputStream::Ptr<byte>& out);
42
43 enum class LineBreak : uint8_t {
44 eLF_LB,
45 eCRLF_LB,
46 eAuto_LB = eCRLF_LB
47 };
48 struct Options {
49 LineBreak fLineBreak{LineBreak::eAuto_LB};
50 };
51
52 /**
53 * \note BLOB overload redundant, but provided since slight performance tweak (@todo maybe replace with span<byte> for that case/reason)
54 */
55 string Encode (const Streams::InputStream::Ptr<byte>& from, const Options& o = {});
56 string Encode (const Memory::BLOB& from, const Options& o = {});
57
58}
59
60namespace Stroika::Foundation::Cryptography::Encoding::Algorithm {
61
62 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline Memory::BLOB DecodeBase64 (span<const char> s)
63 {
64 return Base64::Decode (s);
65 }
66 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline Memory::BLOB DecodeBase64 (const string& s)
67 {
68 return Base64::Decode (s);
69 }
70 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline Memory::BLOB DecodeBase64 (const u8string& s)
71 {
72 return Base64::Decode (s);
73 }
74 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline Memory::BLOB DecodeBase64 (const Characters::String& s)
75 {
76 return Base64::Decode (s);
77 }
78 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline void DecodeBase64 (const string& s, const Streams::OutputStream::Ptr<byte>& out)
79 {
80 Base64::Decode (s, out);
81 }
82
83 using Base64::LineBreak; // deprecated
84 [[deprecated ("Since Stroika v3.0d5 use Base64::Encode")]] inline string EncodeBase64 (const Streams::InputStream::Ptr<byte>& from,
85 LineBreak lb = LineBreak::eAuto_LB)
86 {
87 return Base64::Encode (from, Base64::Options{.fLineBreak = lb});
88 }
89 [[deprecated ("Since Stroika v3.0d5 use Base64::Encode")]] inline string EncodeBase64 (const Memory::BLOB& from, LineBreak lb = LineBreak::eAuto_LB)
90 {
91 return Base64::Encode (from, Base64::Options{.fLineBreak = lb});
92 }
93
94}
95
96/*
97 ********************************************************************************
98 ***************************** Implementation Details ***************************
99 ********************************************************************************
100 */
101
102#endif /*_Stroika_Foundation_Cryptography_Base64_h_*/
string Encode(const Streams::InputStream::Ptr< byte > &from, const Options &o={})
Definition Base64.cpp:301