Rev 8559 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8559 | rgimad | 1 | /* Copyright (C) 2021- Rustem Gimadutdinov (rgimad), GPLv2 */ |
2 | #include |
||
3 | #include |
||
4 | #include |
||
5 | #include |
||
8747 | turbocat | 6 | #include |
8559 | rgimad | 7 | #include |
8 | |||
8747 | turbocat | 9 | #define DATA(type, addr, offset) *((type*)((uint8_t*)addr+offset)) |
10 | #define X_W(X, W) ((X<<16)+W) |
||
11 | #define Y_H X_W |
||
12 | |||
8559 | rgimad | 13 | #define WINDOW_WIDTH 400 |
14 | #define WINDOW_HEIGHT 180 |
||
15 | #define EDIT_BOX_PASSWORD_LEN_MAXLEN 16 |
||
16 | #define EDIT_BOX_PASSWORD_GENERATED_MAXLEN 256 |
||
17 | #define FONT_SIZE_DEFAULT 0x10000000 |
||
18 | #define CHECKBOX_ENABLED 2 |
||
19 | |||
8747 | turbocat | 20 | const char WINDOW_TITLE[] = "PasswordGen v0.2"; |
8559 | rgimad | 21 | |
8747 | turbocat | 22 | ksys_colors_table_t sys_color_table; |
8559 | rgimad | 23 | const color_t DRAWTEXT_FLAG_DEFAULT = 0x90000000; |
24 | |||
25 | edit_box *edit_box_password_len; |
||
26 | char edit_box_password_len_buf[EDIT_BOX_PASSWORD_LEN_MAXLEN]; |
||
27 | |||
28 | edit_box *edit_box_password_generated; |
||
29 | char edit_box_password_generated_buf[EDIT_BOX_PASSWORD_GENERATED_MAXLEN]; |
||
30 | |||
31 | check_box *check_box_az; |
||
32 | check_box *check_box_AZ; |
||
33 | check_box *check_box_09; |
||
34 | check_box *check_box_spec_char; |
||
35 | |||
36 | enum MYCOLORS { |
||
37 | GREEN = 0x067D06, |
||
38 | BLUE = 0x0000FF, |
||
39 | RED = 0xFF0000, |
||
40 | BLACK = 0x000000, |
||
41 | WHITE = 0xFFFFFF, |
||
42 | GREY = 0x919191 |
||
43 | }; |
||
44 | |||
45 | enum BUTTONS { |
||
46 | BTN_QUIT = 1, |
||
47 | BTN_GENERATE = 10, |
||
48 | BTN_COPY = 20 |
||
49 | // |
||
50 | }; |
||
51 | |||
52 | const char char_set_az[] = "abcdefghijklmnopqrstuvwxyz"; |
||
53 | const char char_set_AZ[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
||
54 | const char char_set_09[] = "0123456789"; |
||
55 | const char char_set_spec_char[] = "!@#$%^&*_"; |
||
56 | |||
57 | char resulting_char_set[75]; |
||
58 | char password_generated[EDIT_BOX_PASSWORD_GENERATED_MAXLEN]; |
||
59 | |||
60 | /* ---------------------------------------------------------- */ |
||
61 | |||
62 | void generate_password_internal(char *dst, int length, int flag_az, int flag_AZ, int flag_09, int flag_spec_char) { |
||
63 | int i, resulting_char_set_len; |
||
64 | resulting_char_set[0] = '\0'; |
||
65 | if (flag_az == 0 && flag_AZ == 0 && flag_09 == 0 && flag_spec_char == 0) { return; } |
||
66 | if (flag_az) { strcat(resulting_char_set, char_set_az); } |
||
67 | if (flag_AZ) { strcat(resulting_char_set, char_set_AZ); } |
||
68 | if (flag_09) { strcat(resulting_char_set, char_set_09); } |
||
69 | if (flag_spec_char) { strcat(resulting_char_set, char_set_spec_char); } |
||
70 | resulting_char_set_len = strlen(resulting_char_set); |
||
8747 | turbocat | 71 | //debug_printf("resulting_char_set = %s\n", resulting_char_set |
8559 | rgimad | 72 | for (i = 0; i < length; i++) { |
73 | dst[i] = resulting_char_set[rand() % resulting_char_set_len]; |
||
74 | } |
||
75 | dst[length] = '\0'; |
||
76 | } |
||
77 | |||
78 | void notify_show(char *text) { |
||
8747 | turbocat | 79 | _ksys_exec("/sys/@notify", text); |
8559 | rgimad | 80 | } |
81 | |||
82 | void* safe_malloc(size_t size) { |
||
83 | void *p = malloc(size); |
||
84 | if (p == NULL) { |
||
85 | notify_show("'Memory allocation error!' -E"); |
||
86 | exit(0); |
||
87 | } else { |
||
88 | return p; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | void copy_to_clipboard(char *text) { |
||
93 | char *temp_buffer = safe_malloc(EDIT_BOX_PASSWORD_GENERATED_MAXLEN + 12); |
||
94 | memset(temp_buffer, 0, EDIT_BOX_PASSWORD_GENERATED_MAXLEN); |
||
8747 | turbocat | 95 | DATA(char,temp_buffer,4) = KSYS_CLIP_TEXT; /* TEXT */ |
96 | DATA(char,temp_buffer,8) = KSYS_CLIP_CP866; /* CP866 */ |
||
8559 | rgimad | 97 | strncpy(temp_buffer+12, text, EDIT_BOX_PASSWORD_GENERATED_MAXLEN - 1); |
8747 | turbocat | 98 | _ksys_clip_set(strlen(text) + 12, temp_buffer); |
8559 | rgimad | 99 | notify_show("'Copied to clipboard!' -I"); |
100 | free(temp_buffer); |
||
101 | } |
||
102 | |||
103 | edit_box* create_edit_box(unsigned int width, unsigned int left, unsigned int top, |
||
104 | unsigned int color, unsigned int shift_color, unsigned int focus_border_color, |
||
105 | unsigned int blur_border_color, unsigned int text_color, unsigned int max, |
||
106 | char *text, void *mouse_variable, unsigned int flags) |
||
107 | { |
||
108 | edit_box *eb = (edit_box*)safe_malloc(sizeof(edit_box)); |
||
109 | memset(eb, 0, sizeof(edit_box)); |
||
110 | eb->width = width; |
||
111 | eb->left = left; |
||
112 | eb->top = top; |
||
113 | eb->color = color; |
||
114 | eb->shift_color = shift_color; |
||
115 | eb->focus_border_color = focus_border_color; |
||
116 | eb->blur_border_color = blur_border_color; |
||
117 | eb->text_color = text_color; |
||
118 | eb->max = max; |
||
119 | eb->text = text; |
||
120 | eb->mouse_variable = mouse_variable; |
||
121 | eb->flags = flags; |
||
122 | return eb; |
||
123 | } |
||
124 | |||
125 | check_box* create_check_box(unsigned int left_s, unsigned int top_s, unsigned int ch_text_margin, |
||
126 | unsigned int color, unsigned int border_color, unsigned int text_color, char *text, unsigned int flags) |
||
127 | { |
||
128 | check_box *cb = (check_box*)safe_malloc(sizeof(check_box)); |
||
129 | memset(cb, 0, sizeof(check_box)); |
||
130 | cb->left_s = left_s; |
||
131 | cb->top_s = top_s; |
||
132 | cb->ch_text_margin = ch_text_margin; |
||
133 | cb->color = color; |
||
134 | cb->border_color = border_color; |
||
135 | cb->text_color = text_color; |
||
136 | cb->text = text; |
||
137 | cb->flags = flags; |
||
138 | return cb; |
||
139 | } |
||
140 | /* ---------------------------------------------------------- */ |
||
141 | |||
142 | void redraw_window() { |
||
8747 | turbocat | 143 | ksys_pos_t win_pos = _ksys_get_mouse_pos(KSYS_MOUSE_SCREEN_POS); |
144 | _ksys_start_draw(); |
||
145 | _ksys_create_window(win_pos.x, win_pos.y, WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, sys_color_table.work_area, 0x14); |
||
8559 | rgimad | 146 | |
147 | edit_box_draw(edit_box_password_len); |
||
148 | |||
8747 | turbocat | 149 | _ksys_draw_text("Password length: ", 15, 34 + 5, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_text); |
150 | _ksys_draw_text("Characters: ", 15, 68, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_text); |
||
151 | _ksys_draw_text("Generated password: ", 15, 102 + 5, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_text); |
||
8559 | rgimad | 152 | |
153 | check_box_draw2(check_box_az); |
||
154 | check_box_draw2(check_box_AZ); |
||
155 | check_box_draw2(check_box_09); |
||
156 | check_box_draw2(check_box_spec_char); |
||
157 | |||
158 | edit_box_draw(edit_box_password_generated); |
||
159 | |||
8747 | turbocat | 160 | _ksys_define_button(170, 136, 100, 30, BTN_GENERATE, sys_color_table.work_button); |
161 | _ksys_draw_text("Generate!", 182, 136 + 7, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_button_text); |
||
8559 | rgimad | 162 | |
8747 | turbocat | 163 | _ksys_define_button(370-60, 136, 60, 30, BTN_COPY, sys_color_table.work_button); |
164 | _ksys_draw_text("Copy", 370 - 60 + 15, 136 + 7, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_button_text); |
||
8559 | rgimad | 165 | |
8747 | turbocat | 166 | _ksys_end_draw(); |
8559 | rgimad | 167 | } |
168 | |||
169 | /* create and initialize components */ |
||
170 | void create_components() { |
||
171 | edit_box_password_len = create_edit_box(70, 150, 34, WHITE, sys_color_table.work_button, 0, GREY, FONT_SIZE_DEFAULT, EDIT_BOX_PASSWORD_LEN_MAXLEN - 2, edit_box_password_len_buf, NULL, 0/*ed_focus*/); |
||
172 | edit_box_set_text(edit_box_password_len, "10"); |
||
173 | |||
174 | check_box_az = create_check_box(X_W(110, 15), Y_H(68,15), 10, WHITE, BLUE, BLACK | FONT_SIZE_DEFAULT, "a-z", CHECKBOX_ENABLED); |
||
175 | init_checkbox2(check_box_az); |
||
176 | |||
177 | check_box_AZ = create_check_box(X_W(170, 15), Y_H(68,15), 10, WHITE, BLUE, BLACK | FONT_SIZE_DEFAULT, "A-Z", CHECKBOX_ENABLED); |
||
178 | init_checkbox2(check_box_AZ); |
||
179 | |||
180 | check_box_09 = create_check_box(X_W(230, 15), Y_H(68,15), 10, WHITE, BLUE, BLACK | FONT_SIZE_DEFAULT, "0-9", CHECKBOX_ENABLED); |
||
181 | init_checkbox2(check_box_09); |
||
182 | |||
183 | check_box_spec_char = create_check_box(X_W(290, 15), Y_H(68,15), 10, WHITE, BLUE, BLACK | FONT_SIZE_DEFAULT, "!@#$%^&*_", 0); |
||
184 | init_checkbox2(check_box_spec_char); |
||
185 | |||
186 | edit_box_password_generated = create_edit_box(200, 170, 102, WHITE, sys_color_table.work_button, 0, GREY, FONT_SIZE_DEFAULT, EDIT_BOX_PASSWORD_GENERATED_MAXLEN - 2, edit_box_password_generated_buf, NULL, 0/*ed_focus*/); |
||
187 | } |
||
188 | |||
189 | void generate_and_show_password() { |
||
190 | int f_az, f_AZ, f_09, f_spec, psw_len = atoi(edit_box_password_len_buf); |
||
191 | if (psw_len == 0 || psw_len >= EDIT_BOX_PASSWORD_GENERATED_MAXLEN - 2) { |
||
192 | notify_show("'Incorrect password length' -E"); |
||
193 | } else { |
||
194 | f_az = check_box_az->flags & CHECKBOX_ENABLED; |
||
195 | f_AZ = check_box_AZ->flags & CHECKBOX_ENABLED; |
||
196 | f_09 = check_box_09->flags & CHECKBOX_ENABLED; |
||
197 | f_spec = check_box_spec_char->flags & CHECKBOX_ENABLED; |
||
198 | if (f_az == 0 && f_AZ == 0 && f_09 == 0 && f_spec == 0) { |
||
199 | notify_show("'You must choose at least one character set' -E"); |
||
200 | return; |
||
201 | } |
||
202 | generate_password_internal(password_generated, psw_len, f_az, f_AZ, f_09, f_spec); |
||
203 | edit_box_set_text(edit_box_password_generated, password_generated); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | int main(int argc, const char *argv[]) { |
||
208 | int gui_event; /* variable for storing event */ |
||
209 | uint32_t pressed_button = 0; /* code of button pressed in window */ |
||
210 | unsigned int keyval; /* for saving pressed key */ |
||
211 | |||
212 | srand(time(0)*2/3); /* seeding the pseudo random number generator*/ |
||
213 | kolibri_boxlib_init(); |
||
8747 | turbocat | 214 | _ksys_get_system_colors(&sys_color_table); |
215 | _ksys_set_event_mask(0xC0000027); |
||
8559 | rgimad | 216 | create_components(); /* create and init some visual components */ |
217 | |||
218 | do |
||
219 | { |
||
8747 | turbocat | 220 | gui_event = _ksys_get_event(); |
8559 | rgimad | 221 | switch(gui_event) |
222 | { |
||
8747 | turbocat | 223 | case KSYS_EVENT_NONE: |
8559 | rgimad | 224 | break; |
8747 | turbocat | 225 | case KSYS_EVENT_REDRAW: |
8559 | rgimad | 226 | redraw_window(); |
227 | break; |
||
8747 | turbocat | 228 | case KSYS_EVENT_MOUSE: |
8559 | rgimad | 229 | edit_box_mouse(edit_box_password_len); |
230 | check_box_mouse2(check_box_az); |
||
231 | check_box_mouse2(check_box_AZ); |
||
232 | check_box_mouse2(check_box_09); |
||
233 | check_box_mouse2(check_box_spec_char); |
||
234 | edit_box_mouse(edit_box_password_generated); |
||
235 | break; |
||
8747 | turbocat | 236 | case KSYS_EVENT_KEY: |
237 | keyval = _ksys_get_key().val; |
||
8559 | rgimad | 238 | edit_box_key(edit_box_password_len, keyval); |
239 | edit_box_key(edit_box_password_generated, keyval); |
||
240 | break; |
||
8747 | turbocat | 241 | case KSYS_EVENT_BUTTON: |
242 | pressed_button = _ksys_get_button(); |
||
8559 | rgimad | 243 | switch (pressed_button) |
244 | { |
||
245 | case BTN_GENERATE: |
||
246 | generate_and_show_password(); |
||
247 | redraw_window(); |
||
248 | break; |
||
249 | |||
250 | case BTN_COPY: |
||
251 | copy_to_clipboard(edit_box_password_generated_buf); |
||
252 | redraw_window(); |
||
253 | break; |
||
254 | |||
255 | case BTN_QUIT: |
||
8747 | turbocat | 256 | return 0; |
8559 | rgimad | 257 | break; |
258 | } |
||
259 | } |
||
260 | } while(1); |
||
8747 | turbocat | 261 | return 0; |
262 | }>16)+W) |