mirror of
https://github.com/transmission/transmission.git
synced 2025-12-24 20:35:36 +00:00
On a way to factoring out OpenSSL support to a standalone file to ease
addition of other crypto libraries support in the future, move helpers
providing BASE64 encoding and decoding to crypto-utils.{c,h}. OpenSSL-
related functionality is moved to crypto-utils-openssl.c.
Add new functions to be implemented by crypto backends:
* tr_base64_encode_impl - encode from binary to BASE64,
* tr_base64_decode_impl - decode from BASE64 to binary.
Change `tr_base64_encode` and `tr_base64_decode` functions to expect
non-negative input data length which is considered real and never adjusted.
To process null-terminated strings (which was achieved before by passing 0
or -1 as input data length), add new `tr_base64_encode_str` and
`tr_base64_decode_str` functions which do not accept input data length as
an argument but calculate it on their own.
This commit is contained in:
@@ -812,82 +812,6 @@ tr_urlParse (const char * url_in,
|
||||
return err;
|
||||
}
|
||||
|
||||
#include <string.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/buffer.h>
|
||||
|
||||
char *
|
||||
tr_base64_encode (const void * input, int length, int * setme_len)
|
||||
{
|
||||
int retlen = 0;
|
||||
char * ret = NULL;
|
||||
|
||||
if (input != NULL)
|
||||
{
|
||||
BIO * b64;
|
||||
BIO * bmem;
|
||||
BUF_MEM * bptr;
|
||||
|
||||
if (length < 1)
|
||||
length = (int)strlen (input);
|
||||
|
||||
bmem = BIO_new (BIO_s_mem ());
|
||||
b64 = BIO_new (BIO_f_base64 ());
|
||||
BIO_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
|
||||
b64 = BIO_push (b64, bmem);
|
||||
BIO_write (b64, input, length);
|
||||
(void) BIO_flush (b64);
|
||||
BIO_get_mem_ptr (b64, &bptr);
|
||||
ret = tr_strndup (bptr->data, bptr->length);
|
||||
retlen = bptr->length;
|
||||
BIO_free_all (b64);
|
||||
}
|
||||
|
||||
if (setme_len)
|
||||
*setme_len = retlen;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *
|
||||
tr_base64_decode (const void * input,
|
||||
int length,
|
||||
int * setme_len)
|
||||
{
|
||||
char * ret;
|
||||
BIO * b64;
|
||||
BIO * bmem;
|
||||
int retlen;
|
||||
|
||||
if (length < 1)
|
||||
length = strlen (input);
|
||||
|
||||
ret = tr_new0 (char, length);
|
||||
b64 = BIO_new (BIO_f_base64 ());
|
||||
bmem = BIO_new_mem_buf ((unsigned char*)input, length);
|
||||
bmem = BIO_push (b64, bmem);
|
||||
retlen = BIO_read (bmem, ret, length);
|
||||
if (!retlen)
|
||||
{
|
||||
/* try again, but with the BIO_FLAGS_BASE64_NO_NL flag */
|
||||
BIO_free_all (bmem);
|
||||
b64 = BIO_new (BIO_f_base64 ());
|
||||
BIO_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
|
||||
bmem = BIO_new_mem_buf ((unsigned char*)input, length);
|
||||
bmem = BIO_push (b64, bmem);
|
||||
retlen = BIO_read (bmem, ret, length);
|
||||
}
|
||||
|
||||
if (setme_len)
|
||||
*setme_len = retlen;
|
||||
|
||||
BIO_free_all (bmem);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
||||
Reference in New Issue
Block a user