Source release v3.2.0
This commit is contained in:
146
third_party/stringencoders/src/modp_b64.h
vendored
146
third_party/stringencoders/src/modp_b64.h
vendored
@@ -1,39 +1,40 @@
|
||||
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
|
||||
/* vi: set expandtab shiftwidth=4 tabstop=4: */
|
||||
|
||||
/**
|
||||
* \file modp_b64.h
|
||||
* \brief High performance base 64 encode and decode
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* \file
|
||||
* <PRE>
|
||||
* High performance base64 encoder / decoder
|
||||
*
|
||||
* Copyright © 2005, 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com
|
||||
* Copyright © 2005-2016 Nick Galbreath
|
||||
* All rights reserved.
|
||||
*
|
||||
* http://code.google.com/p/stringencoders/
|
||||
* https://github.com/client9/stringencoders
|
||||
*
|
||||
* Released under bsd license. See modp_b64.c for details.
|
||||
* Released under MIT license. See LICENSE for details.
|
||||
* </pre>
|
||||
*
|
||||
* This uses the standard base 64 alphabet. If you are planning
|
||||
* to embed a base 64 encoding inside a URL use modp_b64w instead.
|
||||
*
|
||||
* ATTENTION: the algorithm may require ALIGNED strings. For Intel
|
||||
* chips alignment doens't (perhaps a performance issues). But for
|
||||
* Sparc and maybe ARM, use of unaligned strings can core dump.
|
||||
* see https://github.com/client9/stringencoders/issues/1
|
||||
* see https://github.com/client9/stringencoders/issues/42
|
||||
*/
|
||||
|
||||
#ifndef COM_MODP_STRINGENCODERS_B64
|
||||
#define COM_MODP_STRINGENCODERS_B64
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define BEGIN_C extern "C" {
|
||||
#define END_C }
|
||||
#else
|
||||
#define BEGIN_C
|
||||
#define END_C
|
||||
#endif
|
||||
|
||||
BEGIN_C
|
||||
#include "extern_c_begin.h"
|
||||
#include "modp_stdint.h"
|
||||
|
||||
/**
|
||||
* Encode a raw binary string into base 64.
|
||||
* \brief Encode a raw binary string into base 64.
|
||||
* \param[out] dest should be allocated by the caller to contain
|
||||
* at least modp_b64_encode_len(len) bytes (see below)
|
||||
* This will contain the null-terminated b64 encoded result
|
||||
@@ -57,7 +58,7 @@ BEGIN_C
|
||||
* \endcode
|
||||
*
|
||||
*/
|
||||
int modp_b64_encode(char* dest, const char* str, int len);
|
||||
size_t modp_b64_encode(char* dest, const char* str, size_t len);
|
||||
|
||||
/**
|
||||
* Decode a base64 encoded string
|
||||
@@ -81,7 +82,7 @@ int modp_b64_encode(char* dest, const char* str, int len);
|
||||
* if (len == -1) { error }
|
||||
* \endcode
|
||||
*/
|
||||
int modp_b64_decode(char* dest, const char* src, int len);
|
||||
size_t modp_b64_decode(char* dest, const char* src, size_t len);
|
||||
|
||||
/**
|
||||
* Given a source string of length len, this returns the amount of
|
||||
@@ -93,7 +94,7 @@ int modp_b64_decode(char* dest, const char* src, int len);
|
||||
*
|
||||
* +1 is for any extra null.
|
||||
*/
|
||||
#define modp_b64_encode_len(A) ((A+2)/3 * 4 + 1)
|
||||
#define modp_b64_encode_len(A) ((A + 2) / 3 * 4 + 1)
|
||||
|
||||
/**
|
||||
* Given a base64 string of length len,
|
||||
@@ -129,51 +130,54 @@ int modp_b64_decode(char* dest, const char* src, int len);
|
||||
* // foo is filled out now
|
||||
* \endcode
|
||||
*/
|
||||
#define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
|
||||
#define modp_b64_encode_strlen(A) ((A + 2) / 3 * 4)
|
||||
|
||||
END_C
|
||||
#include "extern_c_end.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace modp {
|
||||
/** \brief b64 encode a cstr with len
|
||||
/** \brief b64 encode a cstr with len
|
||||
*
|
||||
* \param[in] s the input string to encode
|
||||
* \param[in] len the length of the input string
|
||||
* \return a newly allocated b64 string. Empty if failed.
|
||||
*/
|
||||
inline std::string b64_encode(const char* s, size_t len)
|
||||
{
|
||||
std::string x(modp_b64_encode_len(len), '\0');
|
||||
int d = modp_b64_encode(const_cast<char*>(x.data()), s,
|
||||
static_cast<int>(len));
|
||||
inline std::string b64_encode(const char* s, size_t len)
|
||||
{
|
||||
std::string x(modp_b64_encode_len(len), '\0');
|
||||
size_t d = modp_b64_encode(const_cast<char*>(x.data()), s, len);
|
||||
if (d == (size_t)-1) {
|
||||
x.clear();
|
||||
} else {
|
||||
x.erase(d, std::string::npos);
|
||||
return x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/** \brief b64 encode a cstr
|
||||
/** \brief b64 encode a cstr
|
||||
*
|
||||
* \param[in] s the input string to encode
|
||||
* \return a newly allocated b64 string. Empty if failed.
|
||||
*/
|
||||
inline std::string b64_encode(const char* s)
|
||||
{
|
||||
return b64_encode(s, static_cast<int>(strlen(s)));
|
||||
}
|
||||
inline std::string b64_encode(const char* s)
|
||||
{
|
||||
return b64_encode(s, strlen(s));
|
||||
}
|
||||
|
||||
/** \brief b64 encode a const std::string
|
||||
/** \brief b64 encode a const std::string
|
||||
*
|
||||
* \param[in] s the input string to encode
|
||||
* \return a newly allocated b64 string. Empty if failed.
|
||||
*/
|
||||
inline std::string b64_encode(const std::string& s)
|
||||
{
|
||||
return b64_encode(s.data(), s.size());
|
||||
}
|
||||
inline std::string b64_encode(const std::string& s)
|
||||
{
|
||||
return b64_encode(s.data(), s.size());
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* base 64 encode a string (self-modifing)
|
||||
*
|
||||
* This function is for C++ only (duh)
|
||||
@@ -181,32 +185,31 @@ namespace modp {
|
||||
* \param[in,out] s the string to be decoded
|
||||
* \return a reference to the input string
|
||||
*/
|
||||
inline std::string& b64_encode(std::string& s)
|
||||
{
|
||||
std::string x(b64_encode(s.data(), s.size()));
|
||||
s.swap(x);
|
||||
return s;
|
||||
}
|
||||
inline std::string& b64_encode(std::string& s)
|
||||
{
|
||||
std::string x(b64_encode(s.data(), s.size()));
|
||||
s.swap(x);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string b64_decode(const char* src, size_t len)
|
||||
{
|
||||
std::string x(modp_b64_decode_len(len)+1, '\0');
|
||||
int d = modp_b64_decode(const_cast<char*>(x.data()), src,
|
||||
static_cast<int>(len));
|
||||
if (d < 0) {
|
||||
x.clear();
|
||||
} else {
|
||||
x.erase(d, std::string::npos);
|
||||
}
|
||||
return x;
|
||||
inline std::string b64_decode(const char* src, size_t len)
|
||||
{
|
||||
std::string x(modp_b64_decode_len(len) + 1, '\0');
|
||||
size_t d = modp_b64_decode(const_cast<char*>(x.data()), src, len);
|
||||
if (d == (size_t)-1) {
|
||||
x.clear();
|
||||
} else {
|
||||
x.erase(d, std::string::npos);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
inline std::string b64_decode(const char* src)
|
||||
{
|
||||
return b64_decode(src, strlen(src));
|
||||
}
|
||||
inline std::string b64_decode(const char* src)
|
||||
{
|
||||
return b64_decode(src, strlen(src));
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* base 64 decode a string (self-modifing)
|
||||
* On failure, the string is empty.
|
||||
*
|
||||
@@ -215,18 +218,17 @@ namespace modp {
|
||||
* \param[in,out] s the string to be decoded
|
||||
* \return a reference to the input string
|
||||
*/
|
||||
inline std::string& b64_decode(std::string& s)
|
||||
{
|
||||
std::string x(b64_decode(s.data(), s.size()));
|
||||
s.swap(x);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string b64_decode(const std::string& s)
|
||||
{
|
||||
return b64_decode(s.data(), s.size());
|
||||
}
|
||||
inline std::string& b64_decode(std::string& s)
|
||||
{
|
||||
std::string x(b64_decode(s.data(), s.size()));
|
||||
s.swap(x);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string b64_decode(const std::string& s)
|
||||
{
|
||||
return b64_decode(s.data(), s.size());
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* __cplusplus */
|
||||
|
||||
Reference in New Issue
Block a user