tor  master
Macros | Functions
util_format.c File Reference

Miscellaneous functions for encoding and decoding various things in base{16,32,64}. More...

#include "orconfig.h"
#include "torlog.h"
#include "util.h"
#include "util_format.h"
#include "torint.h"
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
Include dependency graph for util_format.c:

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

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)
 
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 hex_decode_digit (char c)
 
int base16_decode (char *dest, size_t destlen, const char *src, size_t srclen)
 

Detailed Description

Miscellaneous functions for encoding and decoding various things in base{16,32,64}.

Macro Definition Documentation

◆ ENCODE_CHAR

#define ENCODE_CHAR (   ch)
Value:
STMT_BEGIN \
*d++ = ch; \
if (flags & BASE64_ENCODE_MULTILINE) { \
if (++linelen % BASE64_OPENSSL_LINELEN == 0) { \
linelen = 0; \
*d++ = '\n'; \
} \
} \
STMT_END
Definition: d.py:1

◆ X

#define X   255

Special values used for the base64_decode_table

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.

Here is the caller graph for this function:

◆ base16_encode()

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.

Here is the caller graph for this function:

◆ base32_decode()

int base32_decode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen 
)

Implements base32 decoding as in RFC 4648. Returns 0 if successful, -1 otherwise.

Here is the caller graph for this function:

◆ base32_encode()

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

Implements base32 encoding as in RFC 4648.

Here is the caller graph for this function:

◆ 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.

Here is the caller graph for this function:

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 or external padding to the output stream.

Here is the call graph for this function:
Here is the caller graph for this function:

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

Here is the caller graph for this function:

◆ hex_decode_digit()

int hex_decode_digit ( char  c)

Helper: given a hex digit, return its value, or -1 if it isn't hex.

Here is the caller graph for this function: