char * fs_read (const char *path) { FILE *file = fs_open(path, FS_OPEN_READ); if (NULL == file) return NULL; char *data = fs_fread(file); fclose(file); return data; }
uint16 as_read_binary(asgard_handle * handle, uint16 offset, uchar * buffer, uchar size) { ef_bin_header header; fs_file * file = as_get_current_file(handle); if(file == NULL) return APDU_NO_EF_SELECTED; if(file->entry.type & FS_TYPE_DIR) return APDU_COMMAND_INVALID; fs_fseek(file, 0); fs_fread(file, &header, sizeof(ef_bin_header)); //getting ef information from ef header first if(header.structure == EF_TRANSPARENT) { return APDU_INSTRUCTION_INVALID; //wrong command } if(header.status != EF_VALID) { return APDU_INVALID_STATUS; } if(offset + size <= (header.size + sizeof(ef_bin_header))) { fs_fseek(file, offset + sizeof(ef_bin_header)); fs_fread(file, buffer, size); return APDU_SUCCESS; //read success } return APDU_OUT_OF_RANGE; //wrong offset and length }
static size_t fat_fread(struct fs *fs, void *ptr, size_t byte_size, fs_file *stream) { if(stream->fs != fs) return -1; if(stream->opaque == (void *)0) return -1; struct fat_file_block_offset opaque; opaque.cluster = (uint32_t)stream->opaque; opaque.f_block = 0; return fs_fread(fat_get_next_bdev_block_num, fs, ptr, byte_size, stream, (void*)&opaque); }
//read record for cyclic and linier fixed uint16 as_read_record(asgard_handle * handle, uint16 rec_no, uchar * buffer, uchar size) { ef_rec_header header; fs_file * file = as_get_current_file(handle); uchar bytes_readed; uint16 offset; //uchar actual_index; if(file == NULL) return APDU_NO_EF_SELECTED; if(file->entry.type & FS_TYPE_DIR) return APDU_COMMAND_INVALID; fs_fseek(file, 0); fs_fread(file, (uchar *)&header, sizeof(ef_rec_header)); //getting ef information from ef header first if((header.structure == EF_LINFIX) || (header.structure == EF_CYCLIC)) { return APDU_INSTRUCTION_INVALID; //wrong command } if(header.status != EF_VALID) { return APDU_INVALID_STATUS; } if(size != header.rec_size) return APDU_OUT_OF_RANGE; //check for record size if(header.structure == EF_LINFIX) { //read operation for linier fixed offset = sizeof(ef_rec_header) + (header.rec_size * rec_no); //calculate offset for linier fixed handle->cur_rec_index = rec_no; //update handle current record fs_fseek(file, offset); bytes_readed = fs_fread(file, buffer, header.rec_size); if(bytes_readed == header.rec_size) return APDU_SUCCESS; //return APDU_FATAL_ERROR; } else if(header.structure == EF_CYCLIC) { //read operation for cyclic if(rec_no == 0xffff) rec_no = header.num_of_records - 1; //check if rec_no is negative rec_no = ((rec_no + header.first) % header.num_of_records); //calculate modded record number handle->cur_rec_index = rec_no; //update handle current record offset = sizeof(ef_rec_header) + (header.rec_size * rec_no); //calculate offset for cyclic fs_fseek(file, offset); bytes_readed = fs_fread(file, buffer, header.rec_size); if(bytes_readed == header.rec_size) return APDU_SUCCESS; //return APDU_FATAL_ERROR; } return APDU_FATAL_ERROR; }
/* rehabilitate currently selected file */ uint16 as_rehabilitate(asgard_handle * handle) { ef_bin_header header; //standard ef header uchar bytes_wrote = 0; fs_file * file = as_get_current_file(handle); if(file == NULL) return APDU_NO_EF_SELECTED; fs_fseek(file, 0); fs_fread(file, &header, sizeof(ef_bin_header)); //getting ef information from ef header first if((header.status & EF_VALID) == 0) { return APDU_INVALID_STATUS; } header.status |= EF_VALID; fs_fseek(file, 0); bytes_wrote = fs_fwrite(file, &header, sizeof(ef_bin_header)); //set new ef information if(bytes_wrote != sizeof(ef_bin_header)) return APDU_FATAL_ERROR; return APDU_SUCCESS; }