diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-11-29 17:21:47 +0100 |
---|---|---|
committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-11-29 17:21:47 +0100 |
commit | bc4dbc02d2ffeca03ed728f3f1f31d7cc969f655 (patch) | |
tree | c0a429fb8a419b3bab00834c0a4d30130b42520f | |
parent | 51de26be505c781dd17e4c1ff90dd1f4c46899b9 (diff) |
checkpoint
-rw-r--r-- | README.md | 31 | ||||
-rw-r--r-- | archived/wrap.c | 52 | ||||
-rw-r--r-- | box.c | 164 | ||||
-rw-r--r-- | chatty.c | 2 | ||||
-rw-r--r-- | chatty.h | 65 | ||||
-rw-r--r-- | insert.md | 68 | ||||
-rw-r--r-- | ui.h | 14 |
7 files changed, 365 insertions, 31 deletions
@@ -6,8 +6,6 @@ The idea is the following: - client for reading the messages and sending them at the same time - rooms - encryption - - ChaCha20-Poly1305 - - AES - authentication ## client @@ -60,8 +58,37 @@ The idea is the following: - *unicode and wide characters*: [C for dummies](https://c-for-dummies.com/blog/?p=2578) - *sockets*: [Nir Lichtman - Making Minimalist Chat Server in C on Linux](https://www.youtube.com/watch?v=gGfTjKwLQxY) - syscall manpages `man` +- UTF8 Comprssion: [Casey Muratori - Simple RLE Compressor](https://www.youtube.com/watch?v=kikLEdc3C1c&t=6312s) +### To Read +#### C Programming - https://www.youtube.com/watch?v=wvtFGa6XJDU - https://nullprogram.com/blog/2023/02/11/ - https://nullprogram.com/blog/2023/02/13/ - https://nullprogram.com/blog/2023/10/08/ +#### Encryption w/ Compression +- https://en.wikipedia.org/wiki/BREACH +- https://en.wikipedia.org/wiki/CRIME +- https://crypto.stackexchange.com/questions/2283/crypto-compression-algorithms +- openpgp https://www.rfc-editor.org/rfc/rfc4880 +- https://security.stackexchange.com/questions/19911/crime-how-to-beat-the-beast-successor +- https://blog.qualys.com/product-tech/2012/09/14/crime-information-leakage-attack-against-ssltls +- Algorithms: + *Symmetric* + - AESI + - Blowfish + - Twofish + - Rivest Cipher (RC4) + *Assymetric* + - Data Encryption Standard (DES) + - ECDSA + - RSA + - Diffie-Hellman + - PGP + _Hash_ + - Deflate + - Huffman Coding + - LZ77 + Other + - ChaCha20-Poly1305 + - AES(-GCM) diff --git a/archived/wrap.c b/archived/wrap.c new file mode 100644 index 0000000..57506b8 --- /dev/null +++ b/archived/wrap.c @@ -0,0 +1,52 @@ +// 1. Search backwards for whitespace +// - found? +// y) wrap +// n) break at limit +// - end? +// y) terminate +// n) goto 1. with offset += limit +void +wrap(u8* Text, u32 Len, u32 XLimit, u32 YLimit) +{ + u32 SearchingOffset = XLimit; + u32 X = SearchingOffset; + u32 Y = 0; + u8 t; + u32 PrevX = 0; + + while (X < Len) + { + // Search for whitespace to break on + while (1) + { + if (is_whitespace(Text[X])) break; + + X--; + + // if we got back to the previous position break on Text[SearchingOffset] + if (X == PrevX) + { + X = XLimit; + break; + } + } + + // break + t = Text[X]; + *(Text + X) = '\0'; + tb_printf(0, Y, 0, 0, "%s", Text + PrevX); + Text[X] = t; + Y++; + if (Y >= YLimit) break; + + // consume leading whitespace + while (is_whitespace(Text[X])) X++; + + PrevX = X; + X += XLimit; + } + + tb_printf(0, Y, 0, 0, "%s", Text + PrevX); + + return; +} @@ -0,0 +1,164 @@ +#define TB_IMPL +#include "external/termbox2.h" + +#include <locale.h> +#include <assert.h> + +#define MAX_INPUT_LEN 70 +#define DEBUG + +#include "ui.h" + +u32 +DeleteWordBackwards(u32 Pos, wchar_t* Input) +{ + while (Pos && is_whitespace(Input[Pos - 1])) + { + Input[--Pos] = 0; + } + + while (Pos && !is_whitespace(Input[Pos - 1])) + { + Input[--Pos] = 0; + } + + return Pos; +} + +typedef struct { + wchar_t ur, ru, rd, dr, lr, ud; +} box_characters; + +void +Box(u32 BoxX, u32 BoxY, u32 Width, u32 Height, box_characters *Chars) +{ + wchar_t ur, ru, rd, dr, lr, ud; + if (!Chars) + { + ur = L'╭'; + ru = L'╯'; + rd = L'╮'; + dr = L'╰'; + lr = L'─'; + ud = L'│'; + } + else + { + ur = Chars->ur; + ru = Chars->ru; + rd = Chars->rd; + dr = Chars->dr; + lr = Chars->lr; + ud = Chars->ud; + } + + Height--; + Width--; + + tb_printf(BoxX, BoxY, 0, 0, "%lc", ur); + for (u32 X = 1; X < Width; X++) + { + tb_printf(BoxX + X, BoxY, 0, 0, "%lc", lr); + } + tb_printf(BoxX + Width, BoxY, 0, 0, "%lc", rd); + + // Draw vertical bars + for (u32 Y = 1; Y < Height; Y++) + { + tb_printf(BoxX, BoxY + Y, 0, 0, "%lc", ud); + tb_printf(BoxX + Width, BoxY + Y, 0, 0, "%lc", ud); + } + + tb_printf(BoxX, BoxY + Height, 0, 0, "%lc", dr); + for (u32 X = 1; X < Width; X++) + { + tb_printf(BoxX + X, BoxY + Height, 0, 0, "%lc", lr); + } + tb_printf(BoxX + Width, BoxY + Height, 0, 0, "%lc", ru); +} + +int +main(void) +{ + assert(setlocale(LC_ALL, "")); + struct tb_event ev = {0}; + + tb_init(); + + Box(0, 0, 32, 4, 0); + tb_present(); + tb_poll_event(&ev); + tb_shutdown(); + return 1; + + wchar_t Input[MAX_INPUT_LEN] = {0}; + u32 InputIndex = 0; + u32 InputLen = 0; + // Input[InputIndex++] = 1; + + bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]); + + + while (ev.key != TB_KEY_CTRL_C) + { + tb_clear(); + + InputBox(0, 0, 32, 4, Input, InputLen, True); + + + if (InputIndex) tb_printf(0, 3, 0, 0, "'%lc'", Input[InputIndex - 1]); + tb_printf(4, 3, 0, 0, "%d#%d", InputIndex, InputLen); + + tb_present(); + tb_poll_event(&ev); + + if (ev.ch && InputLen < MAX_INPUT_LEN) + { + // Add new character to input + Input[InputIndex++] = ev.ch; + InputLen++; + continue; + } + + switch (ev.key) + { + case TB_KEY_BACKSPACE2: + { + if (!InputLen) break; + Input[InputIndex--] = 0; + InputLen--; + } break; + case TB_KEY_CTRL_W: + { + while (InputIndex && is_whitespace(Input[InputIndex - 1])) + { + Input[--InputIndex] = 0; + InputLen--; + } + while (InputIndex && !is_whitespace(Input[InputIndex - 1])) + { + Input[--InputIndex] = 0; + InputLen--; + } + + } break; + case TB_KEY_CTRL_U: + { + InputIndex = InputLen = 0; + Input[0] = 0; + } + case TB_KEY_ARROW_LEFT: + { + if (InputIndex) InputIndex--; + } break; + case TB_KEY_ARROW_RIGHT: + { + if (InputIndex < InputLen) InputIndex++; + } break; + } + + } + + tb_shutdown(); + return 0; +} @@ -569,7 +569,7 @@ main(int argc, char** argv) loggingf("Got ID: %lu\n", user.ID); // for wide character printing - assert(setlocale(LC_ALL, "") != 0); + assert(setlocale(LC_ALL, "")); // init tb_init(); @@ -1,4 +1,5 @@ #ifndef CHATTY_H +#define CHATTY_H #include <assert.h> #include <locale.h> @@ -15,25 +16,14 @@ #include <unistd.h> #include <wchar.h> -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; -typedef int8_t s8; -typedef int16_t s16; -typedef int32_t s32; -typedef int64_t s64; -typedef enum { - False = 0, - True = 1 -} Bool; - // port for chatty #define PORT 9983 // max number of bytes that can be logged at once #define LOGMESSAGE_MAX 2048 #define LOG_FMT "%H:%M:%S " #define LOG_LEN 10 +// Enable/Disable saving clients permanently to file +// #define IMPORT_ID #define Kilobytes(Value) ((Value) * 1024) #define Megabytes(Value) (Kilobytes(Value) * 1024) @@ -44,8 +34,37 @@ typedef enum { #define global_variable #define internal static -// Enable/Disable saving clients permanently to file -// #define IMPORT_ID +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; +typedef enum { + False = 0, + True = 1 +} Bool; + +// Arena Allocator +typedef struct { + void* addr; + u64 size; + u64 pos; +} Arena; +#define PushArray(arena, type, count) (type*)ArenaPush((arena), sizeof(type) * (count)) +#define PushArrayZero(arena, type, count) (type*)ArenaPushZero((arena), sizeof(type) * (count)) +#define PushStruct(arena, type) PushArray((arena), (type), 1) +#define PushStructZero(arena, type) PushArrayZero((arena), (type), 1) + +u32 wstrlen(u32* str); +void loggingf(char* format, ...); +void ArenaAlloc(Arena* arena, u64 size); +void ArenaRelease(Arena* arena); +void* ArenaPush(Arena* arena, u64 size); + +#ifdef CHATTY_IMPL global_variable s32 logfd; @@ -80,17 +99,6 @@ loggingf(char* format, ...) write(logfd, buf, n); } -// Arena Allocator -struct Arena { - void* addr; - u64 size; - u64 pos; -} typedef Arena; - -#define PushArray(arena, type, count) (type*)ArenaPush((arena), sizeof(type) * (count)) -#define PushArrayZero(arena, type, count) (type*)ArenaPushZero((arena), sizeof(type) * (count)) -#define PushStruct(arena, type) PushArray((arena), (type), 1) -#define PushStructZero(arena, type) PushArrayZero((arena), (type), 1) // Returns arena in case of success, or 0 if it failed to alllocate the memory void @@ -118,5 +126,6 @@ ArenaPush(Arena* arena, u64 size) return mem; } -#endif -#define CHATTY_H +#endif // CHATTY_IMPL + +#endif // CHATTY_H diff --git a/insert.md b/insert.md new file mode 100644 index 0000000..cb00d1c --- /dev/null +++ b/insert.md @@ -0,0 +1,68 @@ +# 1. InputBox + +InputBox(u32 BoxX, u32 BoxY, u32 BoxWidth, u32 BoxHeight, + wchar_t *Text, u32 TextLen, u32 TextIndex, + Bool Focused) + +TextIndex is where the edit cursor is in the text, so where the text should be displayed? + +It is most user friendly when the text moves as least as possible, so giving an offset will make it +scroll each time the user moves the cursor. No it should only move when the user moves the cursor +out of bounds. + + +There should be cursor logic and text logic + +# 2. Redraw text based on cursor + +```c +void +Redraw(u32 DrawnStart, u32 DrawnEnd) +{ + +} + +Box(BoxX, BoxY, Width, Height); +BoxedText(X+1, Y+1, Width-2, Height-2, + TextLen, Text); + +if (ev.key == TB_KEY_ARROW_LEFT) +{ + CursorX--; + TextIndex--; +} +else if (ev.key == TB_KEY_ARROW_RIGHT) +{ + CursorX++; + TextIndex++; +} +else if (ev.key == TB_KEY_ARROW_UP) +{ + // Scroll Up, Update TextIndex, Redraw +} +else if (ev.key == TB_KEY_ARROW_DOWN) +{ + // Scroll Down, Update TextIndex, Redraw +} +else if (ev.ch) +{ + Text[Textlen++] = ev.ch; + TextIndex++; + CursorX++; +} + +if (CursorX == BoxX + Width) +{ + // Scroll Down || Next Line (if empty) + // Update TextIndex, Redraw +} +else if (CursorX == BoxX) +{ + // Scroll up, Update TextIndex, Redraw +} +```` +It can be deterministic how we draw the text, and the cursor position is only needed to know where +to insert text. + +1. Do text insertion +2. Redrawing @@ -1,3 +1,6 @@ +#define CHATTY_IMPL +#include "chatty.h" + // Format option at a position in raw text, used when iterating to know when to toggle a color // option. typedef struct { @@ -433,6 +436,7 @@ InputBox(u32 BoxX, u32 BoxY, u32 BoxWidth, u32 BoxHeight, } #ifdef DEBUG +#ifdef MAX_INPUT_LEN tb_printf(BoxX + 1, BoxY, 0, 0, "%d/%d [%dx%d] %dx%d %d (%d,%d)+(%d,%d)", TextLen, MAX_INPUT_LEN, BoxWidth, BoxHeight, @@ -440,6 +444,16 @@ InputBox(u32 BoxX, u32 BoxY, u32 BoxWidth, u32 BoxHeight, TextOffset, TextX, TextY, XOffset, YOffset); +#else + tb_printf(BoxX + 1, BoxY, 0, 0, "%d/%d [%dx%d] %dx%d %d (%d,%d)+(%d,%d)", + TextLen, TextLen, + BoxWidth, BoxHeight, + TextWidth, TextHeight, + TextOffset, + TextX, TextY, + XOffset, YOffset); + +#endif #endif // Set the cursor |