/** * tifiles_file_is_os: * @filename: a filename as string. * * Check whether file is a FLASH OS file (tib or XXu) * * Return value: a boolean value. **/ TIEXPORT2 int TICALL tifiles_file_is_os(const char *filename) { int i; char *e = tifiles_fext_get(filename); if (!strcmp(e, "")) return 0; if (!tifiles_file_is_ti(filename)) return 0; if(tifiles_file_is_tib(filename)) return !0; if(tifiles_file_is_tno(filename)) return !0; for (i = 1; i < CALC_MAX + 1; i++) { if (!g_ascii_strcasecmp(e, FLASH_OS_FILE_EXT[i])) return !0; } return 0; }
/** * tnsp_file_read_flash: * @filename: name of flash file to open. * @content: where to store the file content. * * Load the flash file into a #FlashContent structure. * * Structure content must be freed with #tifiles_content_delete_flash when * no longer used. If error occurs, the structure content is released for you. * * Return value: an error code, 0 otherwise. **/ int tnsp_file_read_flash(const char *filename, FlashContent *content) { FILE *f; int c; if (!tifiles_file_is_tno(filename)) return ERR_INVALID_FILE; if (content == NULL) { tifiles_critical("%s: an argument is NULL", __FUNCTION__); return ERR_INVALID_FILE; } f = g_fopen(filename, "rb"); if (f == NULL) { tifiles_info("Unable to open this file: %s\n", filename); return ERR_FILE_OPEN; } content->model = CALC_NSPIRE; for(c = 0; c != ' '; c=fgetc(f)); content->revision_major = fgetc(f); fgetc(f); content->revision_minor = fgetc(f); fgetc(f); for(c = 0; c != ' '; c=fgetc(f)); if (fscanf(f, "%i", &(content->data_length)) < 1) { goto tfrf; } rewind(f); content->data_part = (uint8_t *)g_malloc0(content->data_length); if (content->data_part == NULL) { fclose(f); tifiles_content_delete_flash(content); return ERR_MALLOC; } content->next = NULL; if(fread(content->data_part, 1, content->data_length, f) < content->data_length) goto tfrf; fclose(f); return 0; tfrf: // release on exit tifiles_critical("%s: error reading / understanding file %s", __FUNCTION__, filename); fclose(f); tifiles_content_delete_flash(content); return ERR_FILE_IO; }
/** * tnsp_file_read_flash: * @filename: name of flash file to open. * @content: where to store the file content. * * Load the flash file into a #FlashContent structure. * * Structure content must be freed with #tifiles_content_delete_flash when * no longer used. If error occurs, the structure content is released for you. * * Return value: an error code, 0 otherwise. **/ int tnsp_file_read_flash(const char *filename, FlashContent *content) { FILE *f; int c; long cur_pos; uint32_t file_size; int ret = ERR_FILE_IO; if (content == NULL) { tifiles_critical("%s: an argument is NULL", __FUNCTION__); return ERR_INVALID_FILE; } if (!tifiles_file_is_tno(filename)) { ret = ERR_INVALID_FILE; goto tfrf2; } f = g_fopen(filename, "rb"); if (f == NULL) { tifiles_info("Unable to open this file: %s", filename); ret = ERR_FILE_OPEN; goto tfrf2; } if (fseek(f, 0, SEEK_END) < 0) goto tfrf; cur_pos = ftell(f); if (cur_pos < 0) goto tfrf; if (fseek(f, 0, SEEK_SET) < 0) goto tfrf; // The Nspire series' members have at best 128 MB of Flash (TODO: modify this code if this no longer holds). // Flash files larger than that size are insane. if (cur_pos >= (128L << 20)) { ret = ERR_INVALID_FILE; goto tfrf; } file_size = (uint32_t)cur_pos; content->model = CALC_NSPIRE; // Skip chars. c = 0; while (c != ' ') { c = fgetc(f); if (c == EOF) { goto tfrf; } } // Read revision major. c = fgetc(f); if (c == EOF) { goto tfrf; } content->revision_major = c; // Skip char. c = fgetc(f); if (c == EOF) { goto tfrf; } // Read revision minor. c = fgetc(f); if (c == EOF) { goto tfrf; } content->revision_minor = c; // Skip chars. c = fgetc(f); if (c == EOF) { goto tfrf; } c = 0; while (c != ' ') { c = fgetc(f); if (c == EOF) { goto tfrf; } } if (fscanf(f, "%i", &(content->data_length)) < 1) { goto tfrf; } if (content->data_length > file_size) { ret = ERR_INVALID_FILE; goto tfrf; } if (fseek(f, 0, SEEK_SET) < 0) goto tfrf; content->data_part = (uint8_t *)g_malloc0(content->data_length); if (content->data_part == NULL) { fclose(f); tifiles_content_delete_flash(content); return ERR_MALLOC; } content->next = NULL; if(fread(content->data_part, 1, content->data_length, f) < content->data_length) goto tfrf; fclose(f); return 0; tfrf: // release on exit tifiles_critical("%s: error reading / understanding file %s", __FUNCTION__, filename); fclose(f); tfrf2: tifiles_content_delete_flash(content); return ret; }