fix: pre init fixes, not tested

This commit is contained in:
wipedlifepotato 2025-08-21 04:30:28 +07:00
parent 2700a2a40e
commit c8c8c462b9
7 changed files with 63 additions and 50 deletions

View file

@ -14,26 +14,35 @@ static int printHelp(const char * exe, int exitcode)
return exitcode;
}
template <typename InCh, typename OutCh, size_t isz, size_t osz>
static int operate(std::function<std::size_t(const InCh *, size_t, OutCh *, size_t)> f, int infile, int outfile)
{
InCh inbuf[isz];
OutCh outbuf[osz];
ssize_t sz;
size_t outsz;
while((sz = read(infile, inbuf, sizeof(inbuf))) > 0)
{
outsz = f(inbuf, sz, outbuf, sizeof(outbuf));
if(outsz && outsz <= sizeof(outbuf))
{
write(outfile, outbuf, outsz);
}
else
{
return -1;
}
}
return errno;
int operate_b64_decode(int infile, int outfile) {
constexpr size_t BUFFSZ = 4096;
char inbuf[BUFFSZ*4];
uint8_t outbuf[BUFFSZ*3];
ssize_t sz;
while ((sz = read(infile, inbuf, sizeof(inbuf))) > 0) {
std::string_view chunk(inbuf, sz);
size_t outsz = i2p::data::Base64ToByteStream(chunk, outbuf, sizeof(outbuf));
if (outsz > 0) {
write(outfile, outbuf, outsz);
} else {
return -1;
}
}
return errno;
}
int operate_b64_encode(int infile, int outfile) {
constexpr size_t BUFFSZ = 4096;
uint8_t inbuf[BUFFSZ*3];
//char outbuf[BUFFSZ*4];
ssize_t sz;
while((sz = read(infile, inbuf, sizeof(inbuf))) > 0) {
std::string out = i2p::data::ByteStreamToBase64(inbuf, sz);
write(outfile, out.data(), out.size());
}
return errno;
}
int main(int argc, char * argv[])
@ -71,11 +80,11 @@ int main(int argc, char * argv[])
int retcode = 0;
if(decode)
{
retcode = operate<char, uint8_t, BUFFSZ*4, BUFFSZ*3>(i2p::data::Base64ToByteStream, infile, 1);
retcode = operate_b64_decode(infile, 1);
}
else
{
retcode = operate<uint8_t, char, BUFFSZ*3, BUFFSZ*4>(&i2p::data::ByteStreamToBase64, infile, 1);
retcode = operate_b64_encode(infile, 1);
}
close(infile);
return retcode;