Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YGOPRO-520DIY
ygopro
Commits
be81756e
Commit
be81756e
authored
Jun 18, 2025
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refa pre_seed to base64
parent
e0abe3c8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
259 additions
and
0 deletions
+259
-0
gframe/base64.h
gframe/base64.h
+259
-0
No files found.
gframe/base64.h
0 → 100644
View file @
be81756e
#ifndef BASE64_H
#define BASE64_H
#include <string>
#include <cstring>
const
char
kBase64Alphabet
[]
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/"
;
class
Base64
{
public:
static
bool
Encode
(
const
std
::
string
&
in
,
std
::
string
*
out
)
{
int
i
=
0
,
j
=
0
;
size_t
enc_len
=
0
;
unsigned
char
a3
[
3
];
unsigned
char
a4
[
4
];
out
->
resize
(
EncodedLength
(
in
));
int
input_len
=
in
.
size
();
std
::
string
::
const_iterator
input
=
in
.
begin
();
while
(
input_len
--
)
{
a3
[
i
++
]
=
*
(
input
++
);
if
(
i
==
3
)
{
a3_to_a4
(
a4
,
a3
);
for
(
i
=
0
;
i
<
4
;
i
++
)
{
(
*
out
)[
enc_len
++
]
=
kBase64Alphabet
[
a4
[
i
]];
}
i
=
0
;
}
}
if
(
i
)
{
for
(
j
=
i
;
j
<
3
;
j
++
)
{
a3
[
j
]
=
'\0'
;
}
a3_to_a4
(
a4
,
a3
);
for
(
j
=
0
;
j
<
i
+
1
;
j
++
)
{
(
*
out
)[
enc_len
++
]
=
kBase64Alphabet
[
a4
[
j
]];
}
while
((
i
++
<
3
))
{
(
*
out
)[
enc_len
++
]
=
'='
;
}
}
return
(
enc_len
==
out
->
size
());
}
static
bool
Encode
(
const
unsigned
char
*
input
,
size_t
input_length
,
unsigned
char
*
out
,
size_t
out_length
)
{
int
i
=
0
,
j
=
0
;
unsigned
char
*
out_begin
=
out
;
unsigned
char
a3
[
3
];
unsigned
char
a4
[
4
];
size_t
encoded_length
=
EncodedLength
(
input_length
);
if
(
out_length
<
encoded_length
+
1
)
return
false
;
std
::
memset
(
out
,
0
,
out_length
);
while
(
input_length
--
)
{
a3
[
i
++
]
=
*
input
++
;
if
(
i
==
3
)
{
a3_to_a4
(
a4
,
a3
);
for
(
i
=
0
;
i
<
4
;
i
++
)
{
*
out
++
=
kBase64Alphabet
[
a4
[
i
]];
}
i
=
0
;
}
}
if
(
i
)
{
for
(
j
=
i
;
j
<
3
;
j
++
)
{
a3
[
j
]
=
'\0'
;
}
a3_to_a4
(
a4
,
a3
);
for
(
j
=
0
;
j
<
i
+
1
;
j
++
)
{
*
out
++
=
kBase64Alphabet
[
a4
[
j
]];
}
while
((
i
++
<
3
))
{
*
out
++
=
'='
;
}
}
return
(
out
==
(
out_begin
+
encoded_length
));
}
static
bool
Decode
(
const
std
::
string
&
in
,
std
::
string
*
out
)
{
int
i
=
0
,
j
=
0
;
size_t
dec_len
=
0
;
unsigned
char
a3
[
3
];
unsigned
char
a4
[
4
];
int
input_len
=
in
.
size
();
std
::
string
::
const_iterator
input
=
in
.
begin
();
out
->
resize
(
DecodedLength
(
in
));
while
(
input_len
--
)
{
if
(
*
input
==
'='
)
{
break
;
}
a4
[
i
++
]
=
*
(
input
++
);
if
(
i
==
4
)
{
for
(
i
=
0
;
i
<
4
;
i
++
)
{
a4
[
i
]
=
b64_lookup
(
a4
[
i
]);
}
a4_to_a3
(
a3
,
a4
);
for
(
i
=
0
;
i
<
3
;
i
++
)
{
(
*
out
)[
dec_len
++
]
=
a3
[
i
];
}
i
=
0
;
}
}
if
(
i
)
{
for
(
j
=
i
;
j
<
4
;
j
++
)
{
a4
[
j
]
=
'\0'
;
}
for
(
j
=
0
;
j
<
4
;
j
++
)
{
a4
[
j
]
=
b64_lookup
(
a4
[
j
]);
}
a4_to_a3
(
a3
,
a4
);
for
(
j
=
0
;
j
<
i
-
1
;
j
++
)
{
(
*
out
)[
dec_len
++
]
=
a3
[
j
];
}
}
return
(
dec_len
==
out
->
size
());
}
static
bool
Decode
(
const
unsigned
char
*
input
,
size_t
input_length
,
unsigned
char
*
out
,
size_t
out_length
)
{
int
i
=
0
,
j
=
0
;
unsigned
char
*
out_begin
=
out
;
unsigned
char
a3
[
3
];
unsigned
char
a4
[
4
];
size_t
decoded_length
=
DecodedLength
(
input
,
input_length
);
if
(
out_length
<
decoded_length
)
return
false
;
while
(
input_length
--
)
{
if
(
*
input
==
'='
)
{
break
;
}
a4
[
i
++
]
=
*
(
input
++
);
if
(
i
==
4
)
{
for
(
i
=
0
;
i
<
4
;
i
++
)
{
a4
[
i
]
=
b64_lookup
(
a4
[
i
]);
}
a4_to_a3
(
a3
,
a4
);
for
(
i
=
0
;
i
<
3
;
i
++
)
{
*
out
++
=
a3
[
i
];
}
i
=
0
;
}
}
if
(
i
)
{
for
(
j
=
i
;
j
<
4
;
j
++
)
{
a4
[
j
]
=
'\0'
;
}
for
(
j
=
0
;
j
<
4
;
j
++
)
{
a4
[
j
]
=
b64_lookup
(
a4
[
j
]);
}
a4_to_a3
(
a3
,
a4
);
for
(
j
=
0
;
j
<
i
-
1
;
j
++
)
{
*
out
++
=
a3
[
j
];
}
}
return
(
out
==
(
out_begin
+
decoded_length
));
}
static
int
DecodedLength
(
const
unsigned
char
*
in
,
size_t
in_length
)
{
int
numEq
=
0
;
const
unsigned
char
*
in_end
=
in
+
in_length
;
while
(
*--
in_end
==
'='
)
++
numEq
;
return
((
6
*
in_length
)
/
8
)
-
numEq
;
}
static
int
DecodedLength
(
const
std
::
string
&
in
)
{
int
numEq
=
0
;
int
n
=
in
.
size
();
for
(
std
::
string
::
const_reverse_iterator
it
=
in
.
rbegin
();
*
it
==
'='
;
++
it
)
{
++
numEq
;
}
return
((
6
*
n
)
/
8
)
-
numEq
;
}
inline
static
int
EncodedLength
(
size_t
length
)
{
return
(
length
+
2
-
((
length
+
2
)
%
3
))
/
3
*
4
;
}
inline
static
int
EncodedLength
(
const
std
::
string
&
in
)
{
return
EncodedLength
(
in
.
length
());
}
inline
static
void
StripPadding
(
std
::
string
*
in
)
{
while
(
!
in
->
empty
()
&&
*
(
in
->
rbegin
())
==
'='
)
in
->
resize
(
in
->
size
()
-
1
);
}
private:
static
inline
void
a3_to_a4
(
unsigned
char
*
a4
,
unsigned
char
*
a3
)
{
a4
[
0
]
=
(
a3
[
0
]
&
0xfc
)
>>
2
;
a4
[
1
]
=
((
a3
[
0
]
&
0x03
)
<<
4
)
+
((
a3
[
1
]
&
0xf0
)
>>
4
);
a4
[
2
]
=
((
a3
[
1
]
&
0x0f
)
<<
2
)
+
((
a3
[
2
]
&
0xc0
)
>>
6
);
a4
[
3
]
=
(
a3
[
2
]
&
0x3f
);
}
static
inline
void
a4_to_a3
(
unsigned
char
*
a3
,
unsigned
char
*
a4
)
{
a3
[
0
]
=
(
a4
[
0
]
<<
2
)
+
((
a4
[
1
]
&
0x30
)
>>
4
);
a3
[
1
]
=
((
a4
[
1
]
&
0xf
)
<<
4
)
+
((
a4
[
2
]
&
0x3c
)
>>
2
);
a3
[
2
]
=
((
a4
[
2
]
&
0x3
)
<<
6
)
+
a4
[
3
];
}
static
inline
unsigned
char
b64_lookup
(
unsigned
char
c
)
{
if
(
c
>=
'A'
&&
c
<=
'Z'
)
return
c
-
'A'
;
if
(
c
>=
'a'
&&
c
<=
'z'
)
return
c
-
71
;
if
(
c
>=
'0'
&&
c
<=
'9'
)
return
c
+
4
;
if
(
c
==
'+'
)
return
62
;
if
(
c
==
'/'
)
return
63
;
return
255
;
}
};
#endif // BASE64_H
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment