Line data Source code
1 : #include "passgen/pattern/special.h" 2 : #include "passgen/assert.h" 3 : #include "passgen/config.h" 4 : #include "passgen/util/utf8.h" 5 : #include <assert.h> 6 : #include <stdbool.h> 7 : #include <stdlib.h> 8 : 9 : #define PARAMETERS_INITIAL_SIZE 16 10 : #define PARAMETERS_MULTIPLIER 2 11 : 12 137 : void passgen_pattern_special_init( 13 : passgen_pattern_special *special, 14 : uint32_t kind) { 15 137 : special->parameters_len = 0; 16 137 : special->parameters_cap = 0; 17 137 : special->parameters = NULL; 18 : 19 137 : switch(kind) { 20 42 : case 'm': 21 42 : special->kind = PASSGEN_PATTERN_SPECIAL_MARKOV; 22 42 : break; 23 53 : case 'w': 24 53 : special->kind = PASSGEN_PATTERN_SPECIAL_WORDLIST; 25 53 : break; 26 42 : case 'p': 27 42 : special->kind = PASSGEN_PATTERN_SPECIAL_PRESET; 28 42 : break; 29 0 : default: 30 0 : passgen_assert(false); 31 0 : break; 32 : } 33 137 : } 34 : 35 137 : void passgen_pattern_special_free(passgen_pattern_special *special) { 36 137 : free(special->parameters); 37 137 : PASSGEN_CLEAR(special); 38 137 : } 39 : 40 197 : void passgen_pattern_special_push( 41 : passgen_pattern_special *special, 42 : int32_t codepoint) { 43 197 : if(special->parameters_cap == 0) { 44 10 : special->parameters_cap = PARAMETERS_INITIAL_SIZE; 45 10 : special->parameters = malloc(special->parameters_cap); 46 : } 47 : 48 : // always leave enough space for a full UTF8 character (4 bytes) plus a 49 : // NULL. 50 197 : if((special->parameters_len + 5) >= special->parameters_cap) { 51 8 : special->parameters_cap *= PARAMETERS_MULTIPLIER; 52 8 : special->parameters = 53 8 : realloc(special->parameters, special->parameters_cap); 54 : } 55 : 56 197 : int bytes = passgen_utf8_encode_codepoint( 57 197 : (uint8_t *) &special->parameters[special->parameters_len], 58 : codepoint); 59 : 60 197 : passgen_assert(bytes > 0); 61 197 : special->parameters_len += bytes; 62 : 63 197 : assert(special->parameters_len < special->parameters_cap); 64 : 65 : // always NULL-terminate the string. 66 197 : special->parameters[special->parameters_len] = 0; 67 197 : }