Tor
0.4.7.0-alpha-dev
|
Miscellaneous functions for encoding and decoding various things in base{16,32,64}. More...
#include "orconfig.h"
#include "lib/encoding/binascii.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/cc/torint.h"
#include "lib/string/compat_ctype.h"
#include "lib/intmath/muldiv.h"
#include "lib/malloc/malloc.h"
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
Go to the source code of this file.
Macros | |
#define | BASE64_OPENSSL_LINELEN 64 |
#define | ENCODE_CHAR(ch) |
#define | ENCODE_N(idx) ENCODE_CHAR(base64_encode_table[(n >> ((3 - idx) * 6)) & 0x3f]) |
#define | ENCODE_PAD() ENCODE_CHAR('=') |
#define | X 255 |
#define | SP 64 |
#define | PAD 65 |
Functions | |
const char * | hex_str (const char *from, size_t fromlen) |
size_t | base32_encoded_size (size_t srclen) |
void | base32_encode (char *dest, size_t destlen, const char *src, size_t srclen) |
int | base32_decode (char *dest, size_t destlen, const char *src, size_t srclen) |
size_t | base64_encode_size (size_t srclen, int flags) |
size_t | base64_decode_maxsize (size_t srclen) |
int | base64_encode (char *dest, size_t destlen, const char *src, size_t srclen, int flags) |
int | base64_encode_nopad (char *dest, size_t destlen, const uint8_t *src, size_t srclen) |
int | base64_decode (char *dest, size_t destlen, const char *src, size_t srclen) |
void | base16_encode (char *dest, size_t destlen, const char *src, size_t srclen) |
int | base16_decode (char *dest, size_t destlen, const char *src, size_t srclen) |
Variables | |
static const char | base64_encode_table [64] |
static const uint8_t | base64_decode_table [256] |
Miscellaneous functions for encoding and decoding various things in base{16,32,64}.
Definition in file binascii.c.
#define ENCODE_CHAR | ( | ch | ) |
#define X 255 |
Special values used for the base64_decode_table
Definition at line 356 of file binascii.c.
int base16_decode | ( | char * | dest, |
size_t | destlen, | ||
const char * | src, | ||
size_t | srclen | ||
) |
Given a hexadecimal string of srclen bytes in src, decode it and store the result in the destlen-byte buffer at dest. Return the number of bytes decoded on success, -1 on failure. If destlen is greater than INT_MAX or less than half of srclen, -1 is returned.
Definition at line 506 of file binascii.c.
Referenced by cdm_entry_get_sha3_value(), connection_dir_retry_bridges(), decode_hashed_passwords(), dir_routerdesc_download_failed(), getinfo_helper_downloads_bridge(), getinfo_helper_downloads_cert(), getinfo_helper_downloads_desc(), hexdigest_to_digest(), and parse_one_diff_hash().
void base16_encode | ( | char * | dest, |
size_t | destlen, | ||
const char * | src, | ||
size_t | srclen | ||
) |
Encode the srclen bytes at src in a NUL-terminated, uppercase hexadecimal string; store it in the destlen-byte buffer dest.
Definition at line 478 of file binascii.c.
Referenced by authority_cert_is_denylisted(), crypto_pk_get_fingerprint(), crypto_pk_get_hashed_fingerprint(), do_hash_password(), hex_str(), microdesc_relay_is_outdated_dirserver(), node_get_verbose_nickname(), node_get_verbose_nickname_by_id(), orconn_target_get_name(), and rep_hist_dump_stats().
int base32_decode | ( | char * | dest, |
size_t | destlen, | ||
const char * | src, | ||
size_t | srclen | ||
) |
Implements base32 decoding as in RFC 4648. Return the number of bytes decoded if successful; -1 otherwise.
Definition at line 90 of file binascii.c.
Referenced by hs_parse_address_no_log().
void base32_encode | ( | char * | dest, |
size_t | destlen, | ||
const char * | src, | ||
size_t | srclen | ||
) |
Implements base32 encoding as in RFC 4648.
Definition at line 60 of file binascii.c.
Referenced by hs_build_address(), and hs_lookup_last_hid_serv_request().
int base64_decode | ( | char * | dest, |
size_t | destlen, | ||
const char * | src, | ||
size_t | srclen | ||
) |
Base64 decode srclen bytes of data from src. Write the result into dest, if it will fit within destlen bytes. Return the number of bytes written on success; -1 if destlen is too short, or other failure.
NOTE 1: destlen is checked conservatively, as though srclen contained no spaces or padding.
NOTE 2: This implementation does not check for the correct number of padding "=" characters at the end of the string, and does not check for internal padding characters.
Definition at line 396 of file binascii.c.
Referenced by commit_decode(), curve25519_public_from_base64(), decode_auth_client(), decode_hashed_passwords(), digest256_from_base64(), digest_from_base64(), ed25519_signature_from_base64(), and reveal_decode().
size_t base64_decode_maxsize | ( | size_t | srclen | ) |
Return an upper bound on the number of bytes that might be needed to hold the data from decoding the base64 string srclen. This is only an upper bound, since some part of the base64 string might be padding or space.
Definition at line 187 of file binascii.c.
int base64_encode | ( | char * | dest, |
size_t | destlen, | ||
const char * | src, | ||
size_t | srclen, | ||
int | flags | ||
) |
Base64 encode srclen bytes of data from src. Write the result into dest, if it will fit within destlen bytes. Return the number of bytes written on success; -1 if destlen is too short, or other failure.
If flags&BASE64_ENCODE_MULTILINE is true, return encoded output in multiline format (64 character, ‘
’ terminated lines).
Definition at line 215 of file binascii.c.
Referenced by base64_encode_nopad(), commit_encode(), curve25519_public_to_base64(), encode_client_auth_cred_for_control_port(), encode_legacy_key(), pem_encode(), reveal_encode(), and sr_srv_encode().
int base64_encode_nopad | ( | char * | dest, |
size_t | destlen, | ||
const uint8_t * | src, | ||
size_t | srclen | ||
) |
As base64_encode, but do not add any internal spaces, and remove external padding from the output stream. dest must be at least base64_encode_size(srclen, 0), including space for the removed external padding.
Definition at line 329 of file binascii.c.
Referenced by curve25519_public_to_base64(), digest256_to_base64(), digest_to_base64(), and ed25519_signature_to_base64().
size_t base64_encode_size | ( | size_t | srclen, |
int | flags | ||
) |
Return the Base64 encoded size of srclen bytes of data in bytes.
(WATCH OUT: This API does not count the terminating NUL byte, but base32_encoded_size does.)
If flags&BASE64_ENCODE_MULTILINE is true, return the size of the encoded output as multiline output (64 character, ‘
’ terminated lines).
Definition at line 166 of file binascii.c.
Referenced by alloc_http_authenticator(), base64_encode(), encrypt_desc_data_and_base64(), and pem_encoded_size().
const char* hex_str | ( | const char * | from, |
size_t | fromlen | ||
) |
Return a pointer to a NUL-terminated hexadecimal string encoding the first fromlen bytes of from. (fromlen must be <= 32.) The result does not need to be deallocated, but repeated calls to hex_str will trash old results.
Definition at line 34 of file binascii.c.
Referenced by bridge_resolve_conflicts(), channel_dump_statistics(), entry_guard_describe(), geoip_db_digest(), and hs_control_desc_event_upload().
|
static |
Internal table mapping byte values to what they represent in base64. Numbers 0..63 are 6-bit integers. SPs are spaces, and should be skipped. Xs are invalid and must not appear in base64. PAD indicates end-of-string.
Definition at line 364 of file binascii.c.
|
static |
Internal table mapping 6 bit values to the Base64 alphabet.
Definition at line 195 of file binascii.c.