pianod2
multisource multiuser scriptable networked music player
Functions | Variables
utility.cpp File Reference

Various helpful templates, classes and functions. More...

#include <config.h>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdint>
#include <cassert>
#include <exception>
#include <stdexcept>
#include "utility.h"
Include dependency graph for utility.cpp:

Functions

static const char * title_adjusted (const char *title)
 
int compare_title_order (const std::string &astr, const std::string &bstr)
 Compare titles, ignoring 'a', 'an', or 'the'. More...
 
static int count_words (const char *start, const char *end)
 Count words. More...
 
bool compare_person_or_title (const std::string &astr, const std::string &bstr)
 Compare an artist's name, which might be a person's name or a band. More...
 
std::string makeIdentity (std::string s)
 Remove whitespace and other desireable characters, and make all lowercase for use as an ID. More...
 
std::vector< std::string > split_string (const std::string &value)
 Split a string to words. More...
 
std::vector< std::string > split_string (const std::string &value, const std::string separator, const bool include_empty_last)
 Split a string on a fixed pattern. More...
 
std::string join_strings (const std::vector< std::string > &list, const std::string joiner)
 Join strings together. More...
 
lamerkey_t compute_crc (const char *data, lamerkey_t divisor, lamerkey_t remainder)
 Compute a key from a string using a CRC algorithm. More...
 
lamerkey_t create_key_from_string (const char *source)
 Generate a key from a string. More...
 
char * lamer_cipher (const char *keystr, const char *item)
 Encipher or decipher an item based on a key. More...
 
std::string lamer_cipher (const std::string &keystr, const std::string &item)
 

Variables

static const lamerkey_t lamer_crc_generator = 0xae060ed2
 Symmetrical cipher using a text string as a key. More...
 
static const int lamer_keybits = sizeof (lamerkey_t) * 8 - 1
 

Detailed Description

Various helpful templates, classes and functions.

Function Documentation

◆ compare_person_or_title()

bool compare_person_or_title ( const std::string &  astr,
const std::string &  bstr 
)

Compare an artist's name, which might be a person's name or a band.

If it's a title, it may have 'a', 'an', or 'the', or maybe not. If it's a person, it may be /first last/ or /last, first/.

Parameters
astrFirst title.
bstrSecond title.
Returns
true if values are equivalent, false if not.
Warning
Because this function guesses type of name by looking at the parameters, int return would not be sort-consistent. Thus, return type bool.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ compare_title_order()

int compare_title_order ( const std::string &  astr,
const std::string &  bstr 
)

Compare titles, ignoring 'a', 'an', or 'the'.

Parameters
astrFirst title.
bstrSecond title.
Returns
< 0 if astr < bstr; 0 if equal; >0 if astr > bstr.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_crc()

lamerkey_t compute_crc ( const char *  data,
lamerkey_t  divisor,
lamerkey_t  remainder 
)

Compute a key from a string using a CRC algorithm.

If VARIABLE_SIZE_DIVISORS is set, the CRC length is determined dynamically by the value in the divisor. Otherwise, it is determined by the length of the generator set in constants above.

Parameters
dataThe string to use to generate the key.
divisorThe generator/divisor for performing the CRC.
remainderFor unit testing; set to 0 for generating keys. For test purposes, reinvoke this function pass the function's result for remainder. The result should be 0; if not, it's broken.
Returns
The computed key.
Here is the caller graph for this function:

◆ count_words()

static int count_words ( const char *  start,
const char *  end 
)
static

Count words.

Parameters
startStart of string to count.
endEnd of string to count.
Returns
Number of words between start and end.
Here is the caller graph for this function:

◆ create_key_from_string()

lamerkey_t create_key_from_string ( const char *  source)

Generate a key from a string.

Parameters
sourceThe string.
Returns
The key.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ join_strings()

std::string join_strings ( const std::vector< std::string > &  list,
const std::string  joiner 
)

Join strings together.

Parameters
listThe strings to join.
joinerThe string to insert between list strings.
Returns
The assembled string.
Here is the caller graph for this function:

◆ lamer_cipher() [1/2]

char* lamer_cipher ( const char *  keystr,
const char *  item 
)

Encipher or decipher an item based on a key.

Parameters
keystrThe string to use to generate a key.
itemThe string to encipher or decipher.
Returns
The enciphered/deciphered string, freshly malloced; or NULL on error. Original strings are unmodified.
Warning
Allocates space on the heap for the returned string. Caller must free() this when done with the string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lamer_cipher() [2/2]

std::string lamer_cipher ( const std::string &  keystr,
const std::string &  item 
)
Here is the call graph for this function:

◆ makeIdentity()

std::string makeIdentity ( std::string  s)

Remove whitespace and other desireable characters, and make all lowercase for use as an ID.

◆ split_string() [1/2]

std::vector<std::string> split_string ( const std::string &  value)

Split a string to words.

Mid-word punctuation is retained.

  • "Don't stop thinking" -> "Don't", "stop", "thinking"
  • "'Lyin' Eyes" -> "Lyin", "eyes"
    Parameters
    valueThe string to split.
    Returns
    Words.
Here is the caller graph for this function:

◆ split_string() [2/2]

std::vector<std::string> split_string ( const std::string &  value,
const std::string  separator,
const bool  include_empty_last 
)

Split a string on a fixed pattern.

Parameters
valueThe string to split.
separatorThe pattern for splitting the string. It is removed from the result strings.
include_empty_lastIf false, then if the separator occurs at end of string, then the null space following it is omitted from the results.
Returns
1 or more strings.

◆ title_adjusted()

static const char* title_adjusted ( const char *  title)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ lamer_crc_generator

const lamerkey_t lamer_crc_generator = 0xae060ed2
static

Symmetrical cipher using a text string as a key.

Author
Perette Barella
Date
2014-03-11

This is a simplistic cipher intended to be difficult enough to inconvenience hand deciphering, but nothing truly secure. In short: Enough to keep the riff-raff out.

Usernames cannot be changed in pianod. The user's name will be used to generate the key by feeding it through a 32-bit CRC generator. Note that a 32-bit generator yields 31 bits of CRC. We will then encipher the password symetrically with the key/CRC as follows:

For each character, starting at the beginning:
XOR the character with the least significant bits of
the CRC as follows:
0x01–0x1f (000x xxxx): Assert(0), or do nothing.
0x20–0x3f (001x xxxx): XOR 5 bits
0x40–0x7f (01xx xxxx): XOR 6 bits
0x80–0xff (1xxx xxxx), XOR 7 bits of the CRC.
Rotate right the CRC by the number of bits "consumed".

This is certainly not NSA quality, and there are hazards of inventing your own encryption system; but given the inherent insecurity of anyone being able to strategically add printf ("%s's password is %s\n", user->password, user->name) and recompiling, this is adequate. It is, after all, only a music server.

◆ lamer_keybits

const int lamer_keybits = sizeof (lamerkey_t) * 8 - 1
static