ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
strings.c File Reference

Session string generation implementation. More...

Go to the source code of this file.

Functions

asciichat_error_t acds_string_init (void)
 Initialize random number generator for string generation.
 
asciichat_error_t acds_string_generate (char *output, size_t output_size)
 Generate random session string.
 
bool acds_string_validate (const char *str)
 Validate session string format.
 

Detailed Description

Session string generation implementation.

Definition in file strings.c.

Function Documentation

◆ acds_string_generate()

asciichat_error_t acds_string_generate ( char *  output,
size_t  output_size 
)

Generate random session string.

Parameters
outputOutput buffer for session string
output_sizeSize of output buffer (should be at least 48 bytes)
Returns
ASCIICHAT_OK on success, error code otherwise

Generates strings in format: adjective-noun-noun Example: "swift-river-mountain" (20 characters)

Entropy: ~100 adjectives * 100 nouns * 100 nouns = 1 million combinations (Full wordlist version will have ~10 million combinations)

Definition at line 55 of file strings.c.

55 {
56 if (!output || output_size < 48) {
57 return SET_ERRNO(ERROR_INVALID_PARAM, "output buffer must be at least 48 bytes");
58 }
59
60 // Pick random adjective
61 uint32_t adj_idx = randombytes_uniform((uint32_t)adjectives_count);
62 const char *adj = adjectives[adj_idx];
63
64 // Pick two random nouns
65 uint32_t noun1_idx = randombytes_uniform((uint32_t)nouns_count);
66 uint32_t noun2_idx = randombytes_uniform((uint32_t)nouns_count);
67 const char *noun1 = nouns[noun1_idx];
68 const char *noun2 = nouns[noun2_idx];
69
70 // Format: adjective-noun-noun
71 int written = snprintf(output, output_size, "%s-%s-%s", adj, noun1, noun2);
72 if (written < 0 || (size_t)written >= output_size) {
73 return SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Session string too long for buffer");
74 }
75
76 log_debug("Generated session string: %s", output);
77 return ASCIICHAT_OK;
78}
unsigned int uint32_t
Definition common.h:58
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
@ ASCIICHAT_OK
Definition error_codes.h:48
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
Definition error_codes.h:98
#define log_debug(...)
Log a DEBUG message.

References ASCIICHAT_OK, ERROR_BUFFER_OVERFLOW, ERROR_INVALID_PARAM, log_debug, and SET_ERRNO.

Referenced by session_create().

◆ acds_string_init()

asciichat_error_t acds_string_init ( void  )

Initialize random number generator for string generation.

Returns
ASCIICHAT_OK on success, error code otherwise

Must be called before acds_string_generate(). Uses libsodium's randombytes for cryptographically secure randomness.

Definition at line 44 of file strings.c.

44 {
45 // libsodium's randombytes is already initialized by sodium_init()
46 // which should be called at program startup
47 if (sodium_init() < 0) {
48 return SET_ERRNO(ERROR_CRYPTO_INIT, "Failed to initialize libsodium");
49 }
50
51 log_debug("Session string generator initialized (%zu adjectives, %zu nouns)", adjectives_count, nouns_count);
52 return ASCIICHAT_OK;
53}
@ ERROR_CRYPTO_INIT
Definition error_codes.h:55

References ASCIICHAT_OK, ERROR_CRYPTO_INIT, log_debug, and SET_ERRNO.

Referenced by main().

◆ acds_string_validate()

bool acds_string_validate ( const char *  str)

Validate session string format.

Parameters
strSession string to validate
Returns
true if valid format, false otherwise

Valid format:

  • Lowercase letters only
  • Exactly 2 hyphens (3 words)
  • No leading/trailing hyphens
  • Length <= 47 characters

Definition at line 80 of file strings.c.

80 {
81 if (!str) {
82 return false;
83 }
84
85 size_t len = strlen(str);
86 if (len == 0 || len > 47) {
87 return false;
88 }
89
90 // Must not start or end with hyphen
91 if (str[0] == '-' || str[len - 1] == '-') {
92 return false;
93 }
94
95 // Count hyphens and validate characters
96 int hyphen_count = 0;
97 for (size_t i = 0; i < len; i++) {
98 char c = str[i];
99 if (c == '-') {
100 hyphen_count++;
101 // No consecutive hyphens
102 if (i > 0 && str[i - 1] == '-') {
103 return false;
104 }
105 } else if (!islower(c)) {
106 // Only lowercase letters and hyphens allowed
107 return false;
108 }
109 }
110
111 // Must have exactly 2 hyphens (3 words)
112 return hyphen_count == 2;
113}

Referenced by session_create().