restricted.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <string.h>
  22. #include <file-io.h>
  23. #include <guilib.h>
  24. #include <lcd.h>
  25. #include <input.h>
  26. #include "glyph.h"
  27. #include "sha1.h"
  28. #include "search.h"
  29. #include "wikilib.h"
  30. #include "lcd_buf_draw.h"
  31. #include "wl-keyboard.h"
  32. #include "restricted.h"
  33. #include "bigram.h"
  34. #include "history.h"
  35. #include "delay.h"
  36. #include "wiki_info.h"
  37. #define BLACK_SPACE_START RESULT_START
  38. enum restricted_screen_e {
  39. RESTRICTED_SCREEN_FIRST_TIME_PASSWORD,
  40. RESTRICTED_SCREEN_SET_PASSWORD,
  41. RESTRICTED_SCREEN_CONFIRM_PASSWORD,
  42. RESTRICTED_SCREEN_CHECK_PASSWORD,
  43. RESTRICTED_SCREEN_FILTER_ON_OFF,
  44. RESTRICTED_SCREEN_FILTER_OPTION,
  45. RESTRICTED_SCREEN_CHANGE_PASSWORD,
  46. };
  47. enum filter_option_e {
  48. FILTER_OPTION_TO_SET_NONE,
  49. FILTER_OPTION_TO_SET_ON,
  50. FILTER_OPTION_TO_SET_OFF,
  51. };
  52. extern int restricted_article;
  53. extern int display_mode;
  54. extern int last_display_mode;
  55. extern int article_offset;
  56. static char password_string[MAX_PASSWORD_LEN];
  57. static char password1[MAX_PASSWORD_LEN];
  58. static int password_str_len = 0;
  59. static int restricted_screen_mode;
  60. static int filter_option_to_set = FILTER_OPTION_TO_SET_NONE;
  61. int restriction_filter_off = -1;
  62. char restriction_pass1[20];
  63. extern long saved_idx_article;
  64. int init_filtering = 0;
  65. void first_time_password(int flag)
  66. {
  67. int i;
  68. unsigned char *pText;
  69. char str[256];
  70. int width;
  71. guilib_fb_lock();
  72. memset(&framebuffer[(BLACK_SPACE_START - 6)* LCD_VRAM_WIDTH_PIXELS / 8], 0, 6 * LCD_VRAM_WIDTH_PIXELS / 8);
  73. memset(&framebuffer[BLACK_SPACE_START * LCD_VRAM_WIDTH_PIXELS / 8], 0xFF, (LCD_HEIGHT_LINES - BLACK_SPACE_START)* LCD_VRAM_WIDTH_PIXELS / 8);
  74. framebuffer[149 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  75. memset(&framebuffer[149 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  76. framebuffer[149 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  77. for (i = 150; i <= 168; i++)
  78. {
  79. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xF8;
  80. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  81. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x1F;
  82. }
  83. framebuffer[169 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  84. memset(&framebuffer[169 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  85. framebuffer[169 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  86. if (flag)
  87. {
  88. pText = get_nls_text("protection");
  89. width = extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  90. if (strlen(str))
  91. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 10, str, strlen(str), 1);
  92. width = extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  93. if (strlen(str))
  94. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 30, str, strlen(str), 1);
  95. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  96. if (strlen(str))
  97. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 50, str, strlen(str), 1);
  98. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  99. if (strlen(str))
  100. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 70, str, strlen(str), 1);
  101. }
  102. else
  103. {
  104. pText = get_nls_text("restricted");
  105. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  106. if (strlen(str))
  107. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 10, str, strlen(str), 1);
  108. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  109. if (strlen(str))
  110. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 30, str, strlen(str), 1);
  111. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  112. if (strlen(str))
  113. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 50, str, strlen(str), 1);
  114. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  115. if (strlen(str))
  116. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 70, str, strlen(str), 1);
  117. }
  118. pText = get_nls_text("set_password");
  119. render_string(SUBTITLE_FONT_IDX, -1, 151, pText, strlen(pText), 0);
  120. guilib_fb_unlock();
  121. display_mode = DISPLAY_MODE_RESTRICTED;
  122. keyboard_set_mode(KEYBOARD_RESTRICTED);
  123. restricted_screen_mode = RESTRICTED_SCREEN_FIRST_TIME_PASSWORD;
  124. }
  125. void enter_password_screen(char *msg)
  126. {
  127. int i;
  128. unsigned char *pText;
  129. display_mode = DISPLAY_MODE_RESTRICTED;
  130. keyboard_set_mode(KEYBOARD_PASSWORD_CHAR);
  131. guilib_fb_lock();
  132. keyboard_paint();
  133. memset(&framebuffer[(BLACK_SPACE_START - 6)* LCD_VRAM_WIDTH_PIXELS / 8], 0, 6 * LCD_VRAM_WIDTH_PIXELS / 8);
  134. memset(&framebuffer[BLACK_SPACE_START * LCD_VRAM_WIDTH_PIXELS / 8], 0xFF,
  135. (LCD_HEIGHT_LINES - BLACK_SPACE_START - keyboard_height())* LCD_VRAM_WIDTH_PIXELS / 8);
  136. render_string(SUBTITLE_FONT_IDX, -1, 50, msg, strlen(msg), 1);
  137. framebuffer[82 * LCD_VRAM_WIDTH_PIXELS / 8 + 4] = 0xFC;
  138. memset(&framebuffer[82 * LCD_VRAM_WIDTH_PIXELS / 8 + 5], 0, 16);
  139. framebuffer[82 * LCD_VRAM_WIDTH_PIXELS / 8 + 21] = 0x07;
  140. framebuffer[82 * LCD_VRAM_WIDTH_PIXELS / 8 + 22] = 0x80;
  141. memset(&framebuffer[82 * LCD_VRAM_WIDTH_PIXELS / 8 + 23], 0, 2);
  142. framebuffer[82 * LCD_VRAM_WIDTH_PIXELS / 8 + 25] = 0x1F;
  143. for (i = 83; i <= 103; i++)
  144. {
  145. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 4] = 0xF8;
  146. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 5], 0, 16);
  147. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 21] = 0x03;
  148. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 22] = 0x00;
  149. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 23], 0, 2);
  150. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 25] = 0x0F;
  151. }
  152. framebuffer[104 * LCD_VRAM_WIDTH_PIXELS / 8 + 4] = 0xFC;
  153. memset(&framebuffer[104 * LCD_VRAM_WIDTH_PIXELS / 8 + 5], 0, 16);
  154. framebuffer[104 * LCD_VRAM_WIDTH_PIXELS / 8 + 21] = 0x07;
  155. framebuffer[104 * LCD_VRAM_WIDTH_PIXELS / 8 + 22] = 0x80;
  156. memset(&framebuffer[104 * LCD_VRAM_WIDTH_PIXELS / 8 + 23], 0, 2);
  157. framebuffer[104 * LCD_VRAM_WIDTH_PIXELS / 8 + 25] = 0x1F;
  158. pText = get_nls_text("ok");
  159. render_string(SUBTITLE_FONT_IDX, 180, 85, pText, strlen(pText), 0);
  160. guilib_fb_unlock();
  161. }
  162. void filter_on_off(void)
  163. {
  164. int i;
  165. unsigned char *pText;
  166. char str[256];
  167. guilib_fb_lock();
  168. memset(&framebuffer[(BLACK_SPACE_START - 6)* LCD_VRAM_WIDTH_PIXELS / 8], 0, 6 * LCD_VRAM_WIDTH_PIXELS / 8);
  169. memset(&framebuffer[BLACK_SPACE_START * LCD_VRAM_WIDTH_PIXELS / 8], 0xFF, (LCD_HEIGHT_LINES - BLACK_SPACE_START)* LCD_VRAM_WIDTH_PIXELS / 8);
  170. framebuffer[135 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  171. memset(&framebuffer[135 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  172. framebuffer[135 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  173. for (i = 136; i <= 155; i++)
  174. {
  175. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xF8;
  176. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  177. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x1F;
  178. }
  179. framebuffer[156 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  180. memset(&framebuffer[156 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  181. framebuffer[156 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  182. framebuffer[164 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  183. memset(&framebuffer[164 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  184. framebuffer[164 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  185. for (i = 165; i <= 184; i++)
  186. {
  187. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xF8;
  188. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  189. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x1F;
  190. }
  191. framebuffer[185 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  192. memset(&framebuffer[185 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  193. framebuffer[185 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  194. pText = get_nls_text("set_filter");
  195. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  196. if (strlen(str))
  197. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 10, str, strlen(str), 1);
  198. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  199. if (strlen(str))
  200. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 30, str, strlen(str), 1);
  201. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  202. if (strlen(str))
  203. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 50, str, strlen(str), 1);
  204. extract_str_fitting_width(&pText, str, LCD_BUF_WIDTH_PIXELS - LCD_LEFT_MARGIN, SUBTITLE_FONT_IDX);
  205. if (strlen(str))
  206. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, BLACK_SPACE_START + 70, str, strlen(str), 1);
  207. pText = get_nls_text("keep_filter_on");
  208. render_string(SUBTITLE_FONT_IDX, -1, 137, pText, strlen(pText), 0);
  209. pText = get_nls_text("keep_filter_off");
  210. render_string(SUBTITLE_FONT_IDX, -1, 166, pText, strlen(pText), 0);
  211. guilib_fb_unlock();
  212. keyboard_set_mode(KEYBOARD_FILTER_ON_OFF);
  213. }
  214. void filter_option(void)
  215. {
  216. int i;
  217. unsigned char *pText;
  218. display_mode = DISPLAY_MODE_RESTRICTED;
  219. keyboard_set_mode(KEYBOARD_FILTER_OPTION);
  220. restricted_screen_mode = RESTRICTED_SCREEN_FILTER_OPTION;
  221. guilib_fb_lock();
  222. memset(&framebuffer[(BLACK_SPACE_START - 6)* LCD_VRAM_WIDTH_PIXELS / 8], 0, 6 * LCD_VRAM_WIDTH_PIXELS / 8);
  223. memset(&framebuffer[BLACK_SPACE_START * LCD_VRAM_WIDTH_PIXELS / 8], 0xFF, (LCD_HEIGHT_LINES - BLACK_SPACE_START)* LCD_VRAM_WIDTH_PIXELS / 8);
  224. pText = get_nls_text("filter_options");
  225. render_string(TITLE_FONT_IDX, -1, 60, pText, strlen(pText), 1);
  226. framebuffer[106 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  227. memset(&framebuffer[106 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  228. framebuffer[106 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  229. for (i = 107; i <= 126; i++)
  230. {
  231. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xF8;
  232. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  233. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x1F;
  234. }
  235. framebuffer[127 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  236. memset(&framebuffer[127 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  237. framebuffer[127 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  238. framebuffer[135 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  239. memset(&framebuffer[135 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  240. framebuffer[135 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  241. for (i = 136; i <= 155; i++)
  242. {
  243. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xF8;
  244. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  245. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x1F;
  246. }
  247. framebuffer[156 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  248. memset(&framebuffer[156 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  249. framebuffer[156 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  250. framebuffer[164 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  251. memset(&framebuffer[164 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  252. framebuffer[164 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  253. for (i = 165; i <= 185; i++)
  254. {
  255. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xF8;
  256. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  257. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x1F;
  258. }
  259. framebuffer[186 * LCD_VRAM_WIDTH_PIXELS / 8 + 1] = 0xFC;
  260. memset(&framebuffer[186 * LCD_VRAM_WIDTH_PIXELS / 8 + 2], 0, 26);
  261. framebuffer[186 * LCD_VRAM_WIDTH_PIXELS / 8 + 28] = 0x3F;
  262. pText = get_nls_text("turn_filter_on");
  263. render_string(SUBTITLE_FONT_IDX, -1, 108, pText, strlen(pText), 0);
  264. pText = get_nls_text("turn_filter_off");
  265. render_string(SUBTITLE_FONT_IDX, -1, 137, pText, strlen(pText), 0);
  266. pText = get_nls_text("change_password");
  267. render_string(SUBTITLE_FONT_IDX, -1, 166, pText, strlen(pText), 0);
  268. guilib_fb_unlock();
  269. keyboard_set_mode(KEYBOARD_FILTER_OPTION);
  270. }
  271. void save_password(int flag)
  272. {
  273. int fd;
  274. SHA1Context sha;
  275. fd = wl_open("wiki.pas", WL_O_CREATE);
  276. if (fd >= 0)
  277. {
  278. if (password_str_len > 0)
  279. {
  280. SHA1Reset(&sha);
  281. SHA1Input(&sha, (const unsigned char *) password_string, password_str_len);
  282. SHA1Result(&sha);
  283. memcpy(restriction_pass1, sha.Message_Digest, 20);
  284. }
  285. wl_write(fd, restriction_pass1, 20);
  286. if (flag > 1)
  287. {
  288. SHA1Reset(&sha);
  289. SHA1Input(&sha, (const unsigned char *) restriction_pass1, 20);
  290. SHA1Result(&sha);
  291. wl_write(fd, sha.Message_Digest, 20);
  292. }
  293. wl_close(fd);
  294. #ifdef INCLUDED_FROM_KERNEL
  295. delay_us(200000); // for some reason, save may not work if no delay
  296. #endif
  297. }
  298. }
  299. void handle_password_key(char keycode)
  300. {
  301. switch (restricted_screen_mode)
  302. {
  303. case RESTRICTED_SCREEN_FIRST_TIME_PASSWORD:
  304. case RESTRICTED_SCREEN_SET_PASSWORD:
  305. case RESTRICTED_SCREEN_CONFIRM_PASSWORD:
  306. case RESTRICTED_SCREEN_CHECK_PASSWORD:
  307. case RESTRICTED_SCREEN_CHANGE_PASSWORD:
  308. if (keycode == 'Y') {
  309. if (restricted_screen_mode == RESTRICTED_SCREEN_FIRST_TIME_PASSWORD)
  310. {
  311. password_str_len = 0;
  312. keyboard_set_mode(KEYBOARD_PASSWORD_CHAR);
  313. enter_password_screen(get_nls_text("set_password"));
  314. restricted_screen_mode = RESTRICTED_SCREEN_SET_PASSWORD;
  315. }
  316. else if (restricted_screen_mode == RESTRICTED_SCREEN_SET_PASSWORD && password_str_len > 0)
  317. {
  318. restricted_screen_mode = RESTRICTED_SCREEN_CONFIRM_PASSWORD;
  319. strcpy(password1, password_string);
  320. password_str_len = 0;
  321. if (restriction_filter_off == -1)
  322. enter_password_screen(get_nls_text("re_enter_password"));
  323. else
  324. enter_password_screen(get_nls_text("re_enter_new_password"));
  325. }
  326. else if (restricted_screen_mode == RESTRICTED_SCREEN_CONFIRM_PASSWORD && password_str_len > 0)
  327. {
  328. if (strcmp(password1, password_string))
  329. {
  330. password_str_len = 0;
  331. restricted_screen_mode = RESTRICTED_SCREEN_SET_PASSWORD;
  332. enter_password_screen(get_nls_text("passwords_not_match"));
  333. }
  334. else
  335. {
  336. restriction_filter_off = 0;
  337. save_password(1);
  338. restricted_screen_mode = RESTRICTED_SCREEN_FILTER_ON_OFF;
  339. filter_on_off();
  340. }
  341. }
  342. else if (password_str_len > 0)
  343. {
  344. check_password();
  345. }
  346. } else if (keycode == WL_KEY_BACKSPACE) {
  347. password_remove_char();
  348. } else if (is_supported_search_char(keycode)) {
  349. password_add_char(tolower(keycode));
  350. }
  351. break;
  352. case RESTRICTED_SCREEN_FILTER_ON_OFF:
  353. if (keycode == 'Y') {
  354. restriction_filter_off = 0;
  355. if (init_filtering)
  356. {
  357. init_filtering = 0;
  358. search_set_selection(-1);
  359. display_mode = DISPLAY_MODE_INDEX;
  360. keyboard_set_mode(KEYBOARD_CHAR);
  361. repaint_search();
  362. }
  363. else
  364. {
  365. display_mode = DISPLAY_MODE_ARTICLE;
  366. display_retrieved_article(saved_idx_article);
  367. }
  368. } else if (keycode == 'N') {
  369. restriction_filter_off = 1;
  370. save_password(2);
  371. if (init_filtering)
  372. {
  373. init_filtering = 0;
  374. search_set_selection(-1);
  375. display_mode = DISPLAY_MODE_INDEX;
  376. keyboard_set_mode(KEYBOARD_CHAR);
  377. repaint_search();
  378. }
  379. else
  380. {
  381. display_mode = DISPLAY_MODE_ARTICLE;
  382. display_retrieved_article(saved_idx_article);
  383. }
  384. }
  385. break;
  386. case RESTRICTED_SCREEN_FILTER_OPTION:
  387. if (keycode == 'Y') {
  388. filter_option_to_set = FILTER_OPTION_TO_SET_ON;
  389. password_str_len = 0;
  390. check_password();
  391. }
  392. else if (keycode == 'N') {
  393. filter_option_to_set = FILTER_OPTION_TO_SET_OFF;
  394. password_str_len = 0;
  395. check_password();
  396. }
  397. if (keycode == 'P') {
  398. password_str_len = 0;
  399. keyboard_set_mode(KEYBOARD_PASSWORD_CHAR);
  400. enter_password_screen(get_nls_text("enter_old_password"));
  401. restricted_screen_mode = RESTRICTED_SCREEN_CHANGE_PASSWORD;
  402. }
  403. break;
  404. default:
  405. break;
  406. }
  407. }
  408. void check_password()
  409. {
  410. SHA1Context sha;
  411. char pass_sha1[20];
  412. if (password_str_len <= 0)
  413. {
  414. if (restricted_screen_mode != RESTRICTED_SCREEN_FILTER_OPTION)
  415. filter_option_to_set = FILTER_OPTION_TO_SET_NONE;
  416. restricted_screen_mode = RESTRICTED_SCREEN_CHECK_PASSWORD;
  417. enter_password_screen(get_nls_text("enter_password"));
  418. }
  419. else
  420. {
  421. SHA1Reset(&sha);
  422. SHA1Input(&sha, (const unsigned char *) password_string, password_str_len);
  423. SHA1Result(&sha);
  424. memcpy(pass_sha1, sha.Message_Digest, 20);
  425. if (memcmp(pass_sha1, restriction_pass1, 20))
  426. {
  427. password_str_len = 0;
  428. if (restricted_screen_mode != RESTRICTED_SCREEN_CHANGE_PASSWORD)
  429. restricted_screen_mode = RESTRICTED_SCREEN_CHECK_PASSWORD;
  430. enter_password_screen(get_nls_text("try_again"));
  431. }
  432. else
  433. {
  434. if (restricted_screen_mode == RESTRICTED_SCREEN_CHANGE_PASSWORD)
  435. {
  436. password_str_len = 0;
  437. keyboard_set_mode(KEYBOARD_PASSWORD_CHAR);
  438. enter_password_screen(get_nls_text("enter_new_password"));
  439. restricted_screen_mode = RESTRICTED_SCREEN_SET_PASSWORD;
  440. }
  441. else if (filter_option_to_set == FILTER_OPTION_TO_SET_ON)
  442. {
  443. restriction_filter_off = 0;
  444. save_password(1);
  445. last_display_mode = DISPLAY_MODE_ARTICLE;
  446. display_mode = DISPLAY_MODE_ARTICLE;
  447. display_retrieved_article(saved_idx_article);
  448. }
  449. else if (filter_option_to_set == FILTER_OPTION_TO_SET_OFF)
  450. {
  451. restriction_filter_off = 1;
  452. save_password(2);
  453. last_display_mode = DISPLAY_MODE_ARTICLE;
  454. display_mode = DISPLAY_MODE_ARTICLE;
  455. display_retrieved_article(saved_idx_article);
  456. }
  457. else
  458. {
  459. display_mode = DISPLAY_MODE_ARTICLE;
  460. display_retrieved_article(saved_idx_article);
  461. }
  462. }
  463. }
  464. }
  465. int init_article_filter(void)
  466. {
  467. int fd;
  468. int len;
  469. char restriction_pass2[20];
  470. SHA1Context sha;
  471. unsigned char *pText;
  472. if (restriction_filter_off == -1)
  473. {
  474. fd = wl_open("wiki.pas", WL_O_RDONLY);
  475. if (fd >= 0)
  476. {
  477. len = wl_read(fd, restriction_pass1, 20);
  478. if (len < 20)
  479. memset(restriction_pass1, 0, 20);
  480. else
  481. {
  482. len = wl_read(fd, restriction_pass2, 20);
  483. if (len < 20)
  484. memset(restriction_pass2, 0, 20);
  485. SHA1Reset(&sha);
  486. SHA1Input(&sha, (const unsigned char *) restriction_pass1, 20);
  487. SHA1Result(&sha);
  488. if (!memcmp(sha.Message_Digest, restriction_pass2, 20))
  489. restriction_filter_off = 1;
  490. else
  491. restriction_filter_off = 0;
  492. }
  493. wl_close(fd);
  494. }
  495. }
  496. if (restriction_filter_off == -1)
  497. {
  498. init_filtering = 1;
  499. memset(&framebuffer[0], 0, (BLACK_SPACE_START - 1)* LCD_VRAM_WIDTH_PIXELS / 8);
  500. pText = get_nls_text("parental_controls");
  501. render_string(TITLE_FONT_IDX, LCD_LEFT_MARGIN, LCD_TOP_MARGIN, pText, strlen(pText), 0);
  502. first_time_password(1);
  503. return -1;
  504. }
  505. else
  506. return 0;
  507. }
  508. int check_restriction(long idx_article)
  509. {
  510. int fd;
  511. int len;
  512. char restriction_pass2[20];
  513. SHA1Context sha;
  514. char title[MAX_TITLE_SEARCH];
  515. init_filtering = 0;
  516. if (restriction_filter_off == -1)
  517. {
  518. fd = wl_open("wiki.pas", WL_O_RDONLY);
  519. if (fd >= 0)
  520. {
  521. len = wl_read(fd, restriction_pass1, 20);
  522. if (len < 20)
  523. memset(restriction_pass1, 0, 20);
  524. else
  525. {
  526. len = wl_read(fd, restriction_pass2, 20);
  527. if (len < 20)
  528. memset(restriction_pass2, 0, 20);
  529. SHA1Reset(&sha);
  530. SHA1Input(&sha, (const unsigned char *) restriction_pass1, 20);
  531. SHA1Result(&sha);
  532. if (!memcmp(sha.Message_Digest, restriction_pass2, 20))
  533. restriction_filter_off = 1;
  534. else
  535. restriction_filter_off = 0;
  536. }
  537. wl_close(fd);
  538. }
  539. }
  540. if (restriction_filter_off == -1)
  541. {
  542. get_article_title_from_idx(saved_idx_article, title);
  543. memset(&framebuffer[0], 0, (BLACK_SPACE_START - 1)* LCD_VRAM_WIDTH_PIXELS / 8);
  544. render_string(TITLE_FONT_IDX, LCD_LEFT_MARGIN, LCD_TOP_MARGIN, title, strlen(title), 0);
  545. first_time_password(0);
  546. return -1;
  547. } else if (restriction_filter_off)
  548. return 0; // ok
  549. else
  550. {
  551. get_article_title_from_idx(saved_idx_article, title);
  552. memset(&framebuffer[0], 0, (BLACK_SPACE_START - 1)* LCD_VRAM_WIDTH_PIXELS / 8);
  553. render_string(TITLE_FONT_IDX, LCD_LEFT_MARGIN, LCD_TOP_MARGIN, title, strlen(title), 0);
  554. password_str_len = 0;
  555. check_password();
  556. return -1;
  557. }
  558. }
  559. int password_add_char(char c)
  560. {
  561. if(c == 0x20 && password_str_len > 0 && password_string[password_str_len-1] == 0x20)
  562. return -1;
  563. if (password_str_len >= MAX_PASSWORD_LEN - 1) // reserve one byte for '\0'
  564. return -1;
  565. if (!password_str_len && c == 0x20)
  566. return -1;
  567. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0);
  568. if ('A' <= c && c <= 'Z')
  569. c += 32;
  570. password_string[password_str_len++] = c;
  571. password_string[password_str_len] = '\0';
  572. render_string(SUBTITLE_FONT_IDX, 38, 85, password_string, strlen(password_string), 0);
  573. return 0;
  574. }
  575. /*
  576. * return value - 0: remove ok, -1: no key to remove
  577. */
  578. int password_remove_char(void)
  579. {
  580. int x;
  581. if (password_str_len == 0)
  582. return -1;
  583. password_string[--password_str_len] = '\0';
  584. x = render_string(SUBTITLE_FONT_IDX, 38, 85, password_string, strlen(password_string), 0);
  585. guilib_clear_area(x, 85, 172, 103);
  586. return 0;
  587. }
  588. int clear_password_string(void)
  589. {
  590. if (password_str_len == 0)
  591. return -1;
  592. password_str_len = 0;
  593. password_string[0] = '\0';
  594. return 0;
  595. }
  596. int get_password_string_len(void)
  597. {
  598. return password_str_len;
  599. }
  600. void draw_restricted_mark(char *screen_buf)
  601. {
  602. int x, y;
  603. ucs4_t u;
  604. unsigned char *pText;
  605. unsigned char **pUTF8;
  606. for (y = 0; y < 25; y++)
  607. {
  608. for (x = 27; x < 30; x++)
  609. {
  610. if (y < 5 || y == 24)
  611. screen_buf[y * LCD_VRAM_WIDTH_PIXELS / 8 + x] = 0;
  612. else if (y == 5 || y == 23)
  613. {
  614. if (x == 27)
  615. screen_buf[y * LCD_VRAM_WIDTH_PIXELS / 8 + x] = 0x3F;
  616. else if (x == 28)
  617. screen_buf[y * LCD_VRAM_WIDTH_PIXELS / 8 + x] = 0xFF;
  618. else
  619. screen_buf[y * LCD_VRAM_WIDTH_PIXELS / 8 + x] = 0xE0;
  620. }
  621. else
  622. {
  623. if (x == 27)
  624. screen_buf[y * LCD_VRAM_WIDTH_PIXELS / 8 + x] = 0x7F;
  625. else if (x == 28)
  626. screen_buf[y * LCD_VRAM_WIDTH_PIXELS / 8 + x] = 0xFF;
  627. else
  628. screen_buf[y * LCD_VRAM_WIDTH_PIXELS / 8 + x] = 0xF0;
  629. }
  630. }
  631. }
  632. pText = get_nls_text("r");
  633. pUTF8 = &pText;
  634. u = UTF8_to_UCS4(pUTF8);
  635. buf_draw_bmf_char(screen_buf, u, SUBTITLE_FONT_IDX - 1, 221, 6, 1);
  636. }