storage.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. #include "flip_storage/storage.h"
  2. // Forward declaration for use in other functions
  3. static bool load_flip_social_settings(
  4. char *ssid,
  5. size_t ssid_size,
  6. char *password,
  7. size_t password_size,
  8. char *login_username_logged_out,
  9. size_t username_out_size,
  10. char *login_username_logged_in,
  11. size_t username_in_size,
  12. char *login_password_logged_out,
  13. size_t password_out_size,
  14. char *change_password_logged_in,
  15. size_t change_password_size,
  16. char *change_bio_logged_in,
  17. size_t change_bio_size,
  18. char *is_logged_in,
  19. size_t is_logged_in_size);
  20. void save_settings(
  21. const char *wifi_ssid,
  22. const char *wifi_password,
  23. const char *username,
  24. const char *password)
  25. {
  26. // Create the directory for saving settings
  27. char directory_path[256];
  28. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  29. // Create the directory
  30. Storage *storage = furi_record_open(RECORD_STORAGE);
  31. storage_common_mkdir(storage, directory_path);
  32. // Open the settings file
  33. File *file = storage_file_alloc(storage);
  34. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  35. {
  36. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  37. storage_file_free(file);
  38. furi_record_close(RECORD_STORAGE);
  39. return;
  40. }
  41. // Save the wifi_ssid length and data
  42. size_t wifi_ssid_length = strlen(wifi_ssid) + 1; // Include null terminator
  43. if (storage_file_write(file, &wifi_ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  44. storage_file_write(file, wifi_ssid, wifi_ssid_length) != wifi_ssid_length)
  45. {
  46. FURI_LOG_E(TAG, "Failed to write wifi_SSID");
  47. }
  48. // Save the wifi_password length and data
  49. size_t wifi_password_length = strlen(wifi_password) + 1; // Include null terminator
  50. if (storage_file_write(file, &wifi_password_length, sizeof(size_t)) != sizeof(size_t) ||
  51. storage_file_write(file, wifi_password, wifi_password_length) != wifi_password_length)
  52. {
  53. FURI_LOG_E(TAG, "Failed to write wifi_password");
  54. }
  55. // Save the username length and data
  56. size_t username_length = strlen(username) + 1; // Include null terminator
  57. if (storage_file_write(file, &username_length, sizeof(size_t)) != sizeof(size_t) ||
  58. storage_file_write(file, username, username_length) != username_length)
  59. {
  60. FURI_LOG_E(TAG, "Failed to write username");
  61. }
  62. // Save the password length and data
  63. size_t password_length = strlen(password) + 1; // Include null terminator
  64. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  65. storage_file_write(file, password, password_length) != password_length)
  66. {
  67. FURI_LOG_E(TAG, "Failed to write password");
  68. }
  69. storage_file_close(file);
  70. storage_file_free(file);
  71. furi_record_close(RECORD_STORAGE);
  72. }
  73. bool load_settings(
  74. char *wifi_ssid,
  75. size_t wifi_ssid_size,
  76. char *wifi_password,
  77. size_t wifi_password_size,
  78. char *username,
  79. size_t username_size,
  80. char *password,
  81. size_t password_size)
  82. {
  83. Storage *storage = furi_record_open(RECORD_STORAGE);
  84. File *file = storage_file_alloc(storage);
  85. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  86. {
  87. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  88. storage_file_free(file);
  89. furi_record_close(RECORD_STORAGE);
  90. return false; // Return false if the file does not exist
  91. }
  92. // Load the wifi_ssid
  93. size_t wifi_ssid_length;
  94. if (storage_file_read(file, &wifi_ssid_length, sizeof(size_t)) != sizeof(size_t) || wifi_ssid_length > wifi_ssid_size ||
  95. storage_file_read(file, wifi_ssid, wifi_ssid_length) != wifi_ssid_length)
  96. {
  97. FURI_LOG_E(TAG, "Failed to read wifi_SSID");
  98. storage_file_close(file);
  99. storage_file_free(file);
  100. furi_record_close(RECORD_STORAGE);
  101. return false;
  102. }
  103. wifi_ssid[wifi_ssid_length - 1] = '\0'; // Ensure null-termination
  104. // Load the wifi_password
  105. size_t wifi_password_length;
  106. if (storage_file_read(file, &wifi_password_length, sizeof(size_t)) != sizeof(size_t) || wifi_password_length > wifi_password_size ||
  107. storage_file_read(file, wifi_password, wifi_password_length) != wifi_password_length)
  108. {
  109. FURI_LOG_E(TAG, "Failed to read wifi_password");
  110. storage_file_close(file);
  111. storage_file_free(file);
  112. furi_record_close(RECORD_STORAGE);
  113. return false;
  114. }
  115. wifi_password[wifi_password_length - 1] = '\0'; // Ensure null-termination
  116. // Load the username
  117. size_t username_length;
  118. if (storage_file_read(file, &username_length, sizeof(size_t)) != sizeof(size_t) || username_length > username_size ||
  119. storage_file_read(file, username, username_length) != username_length)
  120. {
  121. FURI_LOG_E(TAG, "Failed to read username");
  122. storage_file_close(file);
  123. storage_file_free(file);
  124. furi_record_close(RECORD_STORAGE);
  125. return false;
  126. }
  127. username[username_length - 1] = '\0'; // Ensure null-termination
  128. // Load the password
  129. size_t password_length;
  130. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  131. storage_file_read(file, password, password_length) != password_length)
  132. {
  133. FURI_LOG_E(TAG, "Failed to read password");
  134. storage_file_close(file);
  135. storage_file_free(file);
  136. furi_record_close(RECORD_STORAGE);
  137. return false;
  138. }
  139. password[password_length - 1] = '\0'; // Ensure null-termination
  140. storage_file_close(file);
  141. storage_file_free(file);
  142. furi_record_close(RECORD_STORAGE);
  143. return true;
  144. }
  145. bool save_char(
  146. const char *path_name, const char *value)
  147. {
  148. if (!value)
  149. {
  150. return false;
  151. }
  152. // Create the directory for saving settings
  153. char directory_path[256];
  154. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  155. // Create the directory
  156. Storage *storage = furi_record_open(RECORD_STORAGE);
  157. storage_common_mkdir(storage, directory_path);
  158. // Open the settings file
  159. File *file = storage_file_alloc(storage);
  160. char file_path[256];
  161. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/%s.txt", path_name);
  162. // Open the file in write mode
  163. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  164. {
  165. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  166. storage_file_free(file);
  167. furi_record_close(RECORD_STORAGE);
  168. return false;
  169. }
  170. // Write the data to the file
  171. size_t data_size = strlen(value) + 1; // Include null terminator
  172. if (storage_file_write(file, value, data_size) != data_size)
  173. {
  174. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  175. storage_file_close(file);
  176. storage_file_free(file);
  177. furi_record_close(RECORD_STORAGE);
  178. return false;
  179. }
  180. storage_file_close(file);
  181. storage_file_free(file);
  182. furi_record_close(RECORD_STORAGE);
  183. return true;
  184. }
  185. bool load_char(
  186. const char *path_name,
  187. char *value,
  188. size_t value_size)
  189. {
  190. if (!value)
  191. {
  192. return false;
  193. }
  194. Storage *storage = furi_record_open(RECORD_STORAGE);
  195. File *file = storage_file_alloc(storage);
  196. char file_path[256];
  197. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/%s.txt", path_name);
  198. // Open the file for reading
  199. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  200. {
  201. storage_file_free(file);
  202. furi_record_close(RECORD_STORAGE);
  203. return NULL; // Return false if the file does not exist
  204. }
  205. // Read data into the buffer
  206. size_t read_count = storage_file_read(file, value, value_size);
  207. if (storage_file_get_error(file) != FSE_OK)
  208. {
  209. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  210. storage_file_close(file);
  211. storage_file_free(file);
  212. furi_record_close(RECORD_STORAGE);
  213. return false;
  214. }
  215. // Ensure null-termination
  216. value[read_count - 1] = '\0';
  217. storage_file_close(file);
  218. storage_file_free(file);
  219. furi_record_close(RECORD_STORAGE);
  220. return true;
  221. }
  222. bool save_world(
  223. const char *name,
  224. const char *world_data)
  225. {
  226. // Create the directory for saving settings
  227. char directory_path[256];
  228. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  229. // Create the directory
  230. Storage *storage = furi_record_open(RECORD_STORAGE);
  231. storage_common_mkdir(storage, directory_path);
  232. // Open the settings file
  233. File *file = storage_file_alloc(storage);
  234. char file_path[256];
  235. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  236. // Open the file in write mode
  237. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  238. {
  239. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  240. storage_file_free(file);
  241. furi_record_close(RECORD_STORAGE);
  242. return false;
  243. }
  244. // Write the data to the file
  245. size_t data_size = strlen(world_data) + 1; // Include null terminator
  246. if (storage_file_write(file, world_data, data_size) != data_size)
  247. {
  248. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  249. storage_file_close(file);
  250. storage_file_free(file);
  251. furi_record_close(RECORD_STORAGE);
  252. return false;
  253. }
  254. storage_file_close(file);
  255. storage_file_free(file);
  256. furi_record_close(RECORD_STORAGE);
  257. return true;
  258. }
  259. bool save_world_furi(FuriString *name, FuriString *world_data)
  260. {
  261. // Create the directory for saving settings
  262. char directory_path[256];
  263. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  264. // Create the directory
  265. Storage *storage = furi_record_open(RECORD_STORAGE);
  266. storage_common_mkdir(storage, directory_path);
  267. // Open the settings file
  268. File *file = storage_file_alloc(storage);
  269. char file_path[256];
  270. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(name));
  271. // Open the file in write mode
  272. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  273. {
  274. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  275. storage_file_free(file);
  276. furi_record_close(RECORD_STORAGE);
  277. return false;
  278. }
  279. // Write the data to the file
  280. size_t data_size = furi_string_size(world_data) + 1; // Include null terminator
  281. if (storage_file_write(file, furi_string_get_cstr(world_data), data_size) != data_size)
  282. {
  283. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  284. storage_file_close(file);
  285. storage_file_free(file);
  286. furi_record_close(RECORD_STORAGE);
  287. return false;
  288. }
  289. storage_file_close(file);
  290. storage_file_free(file);
  291. furi_record_close(RECORD_STORAGE);
  292. return true;
  293. }
  294. bool load_world(
  295. const char *name,
  296. char *json_data,
  297. size_t json_data_size)
  298. {
  299. Storage *storage = furi_record_open(RECORD_STORAGE);
  300. File *file = storage_file_alloc(storage);
  301. char file_path[256];
  302. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  303. // Open the file for reading
  304. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  305. {
  306. storage_file_free(file);
  307. furi_record_close(RECORD_STORAGE);
  308. return NULL; // Return false if the file does not exist
  309. }
  310. // Read data into the buffer
  311. size_t read_count = storage_file_read(file, json_data, json_data_size);
  312. if (storage_file_get_error(file) != FSE_OK)
  313. {
  314. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  315. storage_file_close(file);
  316. storage_file_free(file);
  317. furi_record_close(RECORD_STORAGE);
  318. return false;
  319. }
  320. // Ensure null-termination
  321. json_data[read_count - 1] = '\0';
  322. storage_file_close(file);
  323. storage_file_free(file);
  324. furi_record_close(RECORD_STORAGE);
  325. return true;
  326. }
  327. FuriString *load_furi_world(
  328. const char *name)
  329. {
  330. Storage *storage = furi_record_open(RECORD_STORAGE);
  331. File *file = storage_file_alloc(storage);
  332. char file_path[128];
  333. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  334. // Open the file for reading
  335. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  336. {
  337. storage_file_free(file);
  338. furi_record_close(RECORD_STORAGE);
  339. return NULL; // Return false if the file does not exist
  340. }
  341. // Allocate a FuriString to hold the received data
  342. FuriString *str_result = furi_string_alloc();
  343. if (!str_result)
  344. {
  345. FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString");
  346. storage_file_close(file);
  347. storage_file_free(file);
  348. furi_record_close(RECORD_STORAGE);
  349. return NULL;
  350. }
  351. // Reset the FuriString to ensure it's empty before reading
  352. furi_string_reset(str_result);
  353. // Define a buffer to hold the read data
  354. uint8_t *buffer = (uint8_t *)malloc(MAX_FILE_SHOW);
  355. if (!buffer)
  356. {
  357. FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer");
  358. furi_string_free(str_result);
  359. storage_file_close(file);
  360. storage_file_free(file);
  361. furi_record_close(RECORD_STORAGE);
  362. return NULL;
  363. }
  364. // Read data into the buffer
  365. size_t read_count = storage_file_read(file, buffer, MAX_FILE_SHOW);
  366. if (storage_file_get_error(file) != FSE_OK)
  367. {
  368. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  369. furi_string_free(str_result);
  370. storage_file_close(file);
  371. storage_file_free(file);
  372. furi_record_close(RECORD_STORAGE);
  373. return NULL;
  374. }
  375. // Append each byte to the FuriString
  376. for (size_t i = 0; i < read_count; i++)
  377. {
  378. furi_string_push_back(str_result, buffer[i]);
  379. }
  380. // Check if there is more data beyond the maximum size
  381. char extra_byte;
  382. storage_file_read(file, &extra_byte, 1);
  383. // Clean up
  384. storage_file_close(file);
  385. storage_file_free(file);
  386. furi_record_close(RECORD_STORAGE);
  387. free(buffer);
  388. return str_result;
  389. }
  390. bool world_exists(const char *name)
  391. {
  392. if (!name)
  393. {
  394. FURI_LOG_E(TAG, "Invalid name");
  395. return false;
  396. }
  397. Storage *storage = furi_record_open(RECORD_STORAGE);
  398. if (!storage)
  399. {
  400. FURI_LOG_E(TAG, "Failed to open storage");
  401. return false;
  402. }
  403. char file_path[256];
  404. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  405. bool does_exist = storage_file_exists(storage, file_path);
  406. // Clean up
  407. furi_record_close(RECORD_STORAGE);
  408. return does_exist;
  409. }
  410. bool save_world_names(const FuriString *json)
  411. {
  412. // Create the directory for saving settings
  413. char directory_path[256];
  414. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  415. // Create the directory
  416. Storage *storage = furi_record_open(RECORD_STORAGE);
  417. storage_common_mkdir(storage, directory_path);
  418. // Open the settings file
  419. File *file = storage_file_alloc(storage);
  420. char file_path[128];
  421. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  422. // Open the file in write mode
  423. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  424. {
  425. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  426. storage_file_free(file);
  427. furi_record_close(RECORD_STORAGE);
  428. return false;
  429. }
  430. // Write the data to the file
  431. size_t data_size = furi_string_size(json) + 1; // Include null terminator
  432. if (storage_file_write(file, furi_string_get_cstr(json), data_size) != data_size)
  433. {
  434. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  435. storage_file_close(file);
  436. storage_file_free(file);
  437. furi_record_close(RECORD_STORAGE);
  438. return false;
  439. }
  440. storage_file_close(file);
  441. storage_file_free(file);
  442. furi_record_close(RECORD_STORAGE);
  443. return true;
  444. }
  445. FuriString *flip_social_info(char *key)
  446. {
  447. char ssid[64];
  448. char password[64];
  449. char login_username_logged_out[64];
  450. char login_username_logged_in[64];
  451. char login_password_logged_out[64];
  452. char change_password_logged_in[64];
  453. char change_bio_logged_in[64];
  454. char is_logged_in[64];
  455. FuriString *result = furi_string_alloc();
  456. if (!result)
  457. {
  458. FURI_LOG_E(TAG, "Failed to allocate FuriString");
  459. return NULL;
  460. }
  461. if (!load_flip_social_settings(ssid, sizeof(ssid), password, sizeof(password), login_username_logged_out, sizeof(login_username_logged_out), login_username_logged_in, sizeof(login_username_logged_in), login_password_logged_out, sizeof(login_password_logged_out), change_password_logged_in, sizeof(change_password_logged_in), change_bio_logged_in, sizeof(change_bio_logged_in), is_logged_in, sizeof(is_logged_in)))
  462. {
  463. FURI_LOG_E(TAG, "Failed to load flip social settings");
  464. return NULL;
  465. }
  466. if (strcmp(key, "ssid") == 0)
  467. {
  468. furi_string_set_str(result, ssid);
  469. }
  470. else if (strcmp(key, "password") == 0)
  471. {
  472. furi_string_set_str(result, password);
  473. }
  474. else if (strcmp(key, "login_username_logged_out") == 0)
  475. {
  476. furi_string_set_str(result, login_username_logged_out);
  477. }
  478. else if (strcmp(key, "login_username_logged_in") == 0)
  479. {
  480. furi_string_set_str(result, login_username_logged_in);
  481. }
  482. else if (strcmp(key, "login_password_logged_out") == 0)
  483. {
  484. furi_string_set_str(result, login_password_logged_out);
  485. }
  486. else if (strcmp(key, "change_password_logged_in") == 0)
  487. {
  488. furi_string_set_str(result, change_password_logged_in);
  489. }
  490. else if (strcmp(key, "change_bio_logged_in") == 0)
  491. {
  492. furi_string_set_str(result, change_bio_logged_in);
  493. }
  494. else if (strcmp(key, "is_logged_in") == 0)
  495. {
  496. furi_string_set_str(result, is_logged_in);
  497. }
  498. FURI_LOG_E(TAG, "Invalid key");
  499. furi_string_free(result);
  500. return NULL;
  501. }
  502. bool is_logged_in_to_flip_social()
  503. {
  504. // load flip social settings and check if logged in
  505. FuriString *is_logged_in = flip_social_info("is_logged_in");
  506. if (!is_logged_in)
  507. {
  508. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  509. return false;
  510. }
  511. bool logged_in = furi_string_cmp(is_logged_in, "true") == 0;
  512. free(is_logged_in);
  513. return logged_in;
  514. }
  515. static bool load_flip_social_settings(
  516. char *ssid,
  517. size_t ssid_size,
  518. char *password,
  519. size_t password_size,
  520. char *login_username_logged_out,
  521. size_t username_out_size,
  522. char *login_username_logged_in,
  523. size_t username_in_size,
  524. char *login_password_logged_out,
  525. size_t password_out_size,
  526. char *change_password_logged_in,
  527. size_t change_password_size,
  528. char *change_bio_logged_in,
  529. size_t change_bio_size,
  530. char *is_logged_in,
  531. size_t is_logged_in_size)
  532. {
  533. Storage *storage = furi_record_open(RECORD_STORAGE);
  534. File *file = storage_file_alloc(storage);
  535. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  536. {
  537. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  538. storage_file_free(file);
  539. furi_record_close(RECORD_STORAGE);
  540. return false; // Return false if the file does not exist
  541. }
  542. // Load the ssid
  543. size_t ssid_length;
  544. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  545. storage_file_read(file, ssid, ssid_length) != ssid_length)
  546. {
  547. FURI_LOG_E(TAG, "Failed to read SSID");
  548. storage_file_close(file);
  549. storage_file_free(file);
  550. furi_record_close(RECORD_STORAGE);
  551. return false;
  552. }
  553. else
  554. {
  555. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  556. }
  557. // Load the password
  558. size_t password_length;
  559. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  560. storage_file_read(file, password, password_length) != password_length)
  561. {
  562. FURI_LOG_E(TAG, "Failed to read password");
  563. storage_file_close(file);
  564. storage_file_free(file);
  565. furi_record_close(RECORD_STORAGE);
  566. return false;
  567. }
  568. else
  569. {
  570. password[password_length - 1] = '\0'; // Ensure null-termination
  571. }
  572. // Load the login_username_logged_out
  573. size_t username_out_length;
  574. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  575. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  576. {
  577. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  578. // storage_file_close(file);
  579. // storage_file_free(file);
  580. // furi_record_close(RECORD_STORAGE);
  581. // return false;
  582. }
  583. else
  584. {
  585. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  586. }
  587. // Load the login_username_logged_in
  588. size_t username_in_length;
  589. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  590. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  591. {
  592. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  593. // storage_file_close(file);
  594. // storage_file_free(file);
  595. // furi_record_close(RECORD_STORAGE);
  596. // return false;
  597. }
  598. else
  599. {
  600. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  601. }
  602. // Load the login_password_logged_out
  603. size_t password_out_length;
  604. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  605. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  606. {
  607. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  608. // storage_file_close(file);
  609. // storage_file_free(file);
  610. // furi_record_close(RECORD_STORAGE);
  611. // return false;
  612. }
  613. else
  614. {
  615. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  616. }
  617. // Load the change_password_logged_in
  618. size_t change_password_length;
  619. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  620. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  621. {
  622. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  623. // storage_file_close(file);
  624. // storage_file_free(file);
  625. // furi_record_close(RECORD_STORAGE);
  626. // return false;
  627. }
  628. else
  629. {
  630. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  631. }
  632. // Load the is_logged_in
  633. size_t is_logged_in_length;
  634. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  635. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  636. {
  637. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  638. // storage_file_close(file);
  639. // storage_file_free(file);
  640. // furi_record_close(RECORD_STORAGE);
  641. // return false;
  642. }
  643. else
  644. {
  645. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  646. }
  647. // Load the change_bio_logged_in
  648. size_t change_bio_length;
  649. if (storage_file_read(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) || change_bio_length > change_bio_size ||
  650. storage_file_read(file, change_bio_logged_in, change_bio_length) != change_bio_length)
  651. {
  652. FURI_LOG_E(TAG, "Failed to read change_bio_logged_in");
  653. // storage_file_close(file);
  654. // storage_file_free(file);
  655. // furi_record_close(RECORD_STORAGE);
  656. // return false;
  657. }
  658. else
  659. {
  660. change_bio_logged_in[change_bio_length - 1] = '\0'; // Ensure null-termination
  661. }
  662. storage_file_close(file);
  663. storage_file_free(file);
  664. furi_record_close(RECORD_STORAGE);
  665. return true;
  666. }