Tor  0.4.3.0-alpha-dev
Macros | Functions
binascii.h File Reference

Header for binascii.c. More...

#include "orconfig.h"
#include <stddef.h>
#include "lib/testsupport/testsupport.h"
#include "lib/cc/torint.h"

Go to the source code of this file.

Macros

#define BASE64_ENCODE_MULTILINE   1
 
#define BASE32_CHARS   "abcdefghijklmnopqrstuvwxyz234567"
 
#define BASE64_LEN(n)   (CEIL_DIV((n), 3) * 4)
 
#define BASE32_LEN(n)   (CEIL_DIV((n), 5) * 8)
 
#define BASE16_LEN(n)   ((n) * 2)
 
#define BASE64_BUFSIZE(n)   (BASE64_LEN(n) + 1)
 
#define BASE32_BUFSIZE(n)   (BASE32_LEN(n) + 1)
 
#define BASE16_BUFSIZE(n)   (BASE16_LEN(n) + 1)
 
#define BASE64_NOPAD_LEN(n)   (CEIL_DIV((n) * 4, 3))
 
#define BASE32_NOPAD_LEN(n)   (CEIL_DIV((n) * 8, 5))
 
#define BASE64_NOPAD_BUFSIZE(n)   (BASE64_NOPAD_LEN(n) + 1)
 
#define BASE32_NOPAD_BUFSIZE(n)   (BASE32_NOPAD_LEN(n) + 1)
 

Functions

const char * hex_str (const char *from, size_t fromlen)
 
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_decode (char *dest, size_t destlen, const char *src, size_t srclen)
 
int base64_encode_nopad (char *dest, size_t destlen, const uint8_t *src, 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 base32_encoded_size (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)
 

Detailed Description

Header for binascii.c.

Definition in file binascii.h.

Macro Definition Documentation

◆ BASE32_CHARS

#define BASE32_CHARS   "abcdefghijklmnopqrstuvwxyz234567"

Characters that can appear (case-insensitively) in a base32 encoding.

Definition at line 53 of file binascii.h.

◆ BASE64_LEN

#define BASE64_LEN (   n)    (CEIL_DIV((n), 3) * 4)

These macros don't check for overflow. Use them only for constant inputs (like array declarations). The *_LEN macros are the raw encoding lengths (without terminating NUL), while the *_BUFSIZE macros count the terminating NUL.

Definition at line 28 of file binascii.h.

Function Documentation

◆ base16_decode()

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().

◆ base16_encode()

void base16_encode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen 
)

◆ base32_decode()

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 rend_cache_lookup_v2_desc_as_dir().

◆ base32_encode()

void base32_encode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen 
)

◆ base64_decode()

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().

◆ base64_decode_maxsize()

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.

◆ base64_encode()

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_legacy_key(), pem_encode(), reveal_encode(), and sr_srv_encode().

◆ base64_encode_nopad()

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 digest256_to_base64(), digest_to_base64(), and ed25519_signature_to_base64().

◆ base64_encode_size()

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().

◆ hex_str()

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().