2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 bool Base64::convertToBase64 (OutputStream
& base64Result
, const void* sourceData
, size_t sourceDataSize
)
28 static const char lookup
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29 auto* source
= static_cast<const uint8
*> (sourceData
);
31 while (sourceDataSize
> 0)
34 auto byte0
= *source
++;
35 frame
[0] = lookup
[(byte0
& 0xfcu
) >> 2];
36 uint32 bits
= (byte0
& 0x03u
) << 4;
38 if (sourceDataSize
> 1)
40 auto byte1
= *source
++;
41 frame
[1] = lookup
[bits
| ((byte1
& 0xf0u
) >> 4)];
42 bits
= (byte1
& 0x0fu
) << 2;
44 if (sourceDataSize
> 2)
46 auto byte2
= *source
++;
47 frame
[2] = lookup
[bits
| ((byte2
& 0xc0u
) >> 6)];
48 frame
[3] = lookup
[byte2
& 0x3fu
];
53 frame
[2] = lookup
[bits
];
60 frame
[1] = lookup
[bits
];
66 if (! base64Result
.write (frame
, 4))
73 bool Base64::convertFromBase64 (OutputStream
& binaryOutput
, StringRef base64TextInput
)
75 for (auto s
= base64TextInput
.text
; ! s
.isEmpty();)
79 for (int i
= 0; i
< 4; ++i
)
81 auto c
= (uint32
) s
.getAndAdvance();
83 if (c
>= 'A' && c
<= 'Z') c
-= 'A';
84 else if (c
>= 'a' && c
<= 'z') c
-= 'a' - 26;
85 else if (c
>= '0' && c
<= '9') c
+= 52 - '0';
86 else if (c
== '+') c
= 62;
87 else if (c
== '/') c
= 63;
88 else if (c
== '=') { c
= 64; if (i
<= 1) return false; }
94 binaryOutput
.writeByte ((char) ((data
[0] << 2) | (data
[1] >> 4)));
98 binaryOutput
.writeByte ((char) ((data
[1] << 4) | (data
[2] >> 2)));
101 binaryOutput
.writeByte ((char) ((data
[2] << 6) | data
[3]));
108 String
Base64::toBase64 (const void* sourceData
, size_t sourceDataSize
)
110 MemoryOutputStream
m ((sourceDataSize
* 4) / 3 + 3);
111 bool ok
= convertToBase64 (m
, sourceData
, sourceDataSize
);
112 jassertquiet (ok
); // should always succeed for this simple case
116 String
Base64::toBase64 (const String
& text
)
118 return toBase64 (text
.toRawUTF8(), strlen (text
.toRawUTF8()));
122 //==============================================================================
123 //==============================================================================
126 class Base64Tests
: public UnitTest
130 : UnitTest ("Base64 class", UnitTestCategories::text
)
133 static MemoryBlock
createRandomData (Random
& r
)
135 MemoryOutputStream m
;
137 for (int i
= r
.nextInt (400); --i
>= 0;)
138 m
.writeByte ((char) r
.nextInt (256));
140 return m
.getMemoryBlock();
143 void runTest() override
145 beginTest ("Base64");
147 auto r
= getRandom();
149 for (int i
= 1000; --i
>= 0;)
151 auto original
= createRandomData (r
);
152 auto asBase64
= Base64::toBase64 (original
.getData(), original
.getSize());
153 MemoryOutputStream out
;
154 expect (Base64::convertFromBase64 (out
, asBase64
));
155 auto result
= out
.getMemoryBlock();
156 expect (result
== original
);
161 static Base64Tests base64Tests
;