int fsw_streq(struct fsw_string *s1, struct fsw_string *s2) { struct fsw_string temp_s; // handle empty strings if (s1->type == FSW_STRING_TYPE_EMPTY) { temp_s.type = FSW_STRING_TYPE_ISO88591; temp_s.size = temp_s.len = 0; temp_s.data = NULL; return fsw_streq(&temp_s, s2); } if (s2->type == FSW_STRING_TYPE_EMPTY) { temp_s.type = FSW_STRING_TYPE_ISO88591; temp_s.size = temp_s.len = 0; temp_s.data = NULL; return fsw_streq(s1, &temp_s); } // check length (count of chars) if (s1->len != s2->len) return 0; if (s1->len == 0) // both strings are empty return 1; if (s1->type == s2->type) { // same type, do a dumb memory compare if (s1->size != s2->size) return 0; return fsw_memeq(s1->data, s2->data, s1->size); } // dispatch to type-specific functions #define STREQ_DISPATCH(type1, type2) \ if (s1->type == FSW_STRING_TYPE_##type1 && s2->type == FSW_STRING_TYPE_##type2) \ return fsw_streq_##type1##_##type2(s1->data, s2->data, s1->len); \ if (s2->type == FSW_STRING_TYPE_##type1 && s1->type == FSW_STRING_TYPE_##type2) \ return fsw_streq_##type1##_##type2(s2->data, s1->data, s1->len); STREQ_DISPATCH(ISO88591, UTF8); STREQ_DISPATCH(ISO88591, UTF16); STREQ_DISPATCH(ISO88591, UTF16_SWAPPED); STREQ_DISPATCH(UTF8, UTF16); STREQ_DISPATCH(UTF8, UTF16_SWAPPED); STREQ_DISPATCH(UTF16, UTF16_SWAPPED); // final fallback return 0; }
int fsw_streq_cstr(struct fsw_string *s1, const char *s2) { struct fsw_string temp_s; int i; for (i = 0; s2[i]; i++) ; temp_s.type = FSW_STRING_TYPE_ISO88591; temp_s.size = temp_s.len = i; temp_s.data = (char *)s2; return fsw_streq(s1, &temp_s); }
static fsw_status_t fsw_ext2_dir_lookup(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno, struct fsw_string *lookup_name, struct fsw_ext2_dnode **child_dno_out) { fsw_status_t status; struct fsw_shandle shand; fsw_u32 child_ino; struct ext2_dir_entry entry; struct fsw_string entry_name; // Preconditions: The caller has checked that dno is a directory node. entry_name.type = FSW_STRING_TYPE_ISO88591; // setup handle to read the directory status = fsw_shandle_open(dno, &shand); if (status) return status; // scan the directory for the file child_ino = 0; while (child_ino == 0) { // read next entry status = fsw_ext2_read_dentry(&shand, &entry); if (status) goto errorexit; if (entry.inode == 0) { // end of directory reached status = FSW_NOT_FOUND; goto errorexit; } // compare name entry_name.len = entry_name.size = entry.name_len; entry_name.data = entry.name; if (fsw_streq(lookup_name, &entry_name)) { child_ino = entry.inode; break; } } // setup a dnode for the child item status = fsw_dnode_create(dno, child_ino, FSW_DNODE_TYPE_UNKNOWN, &entry_name, child_dno_out); errorexit: fsw_shandle_close(&shand); return status; }
static fsw_status_t fsw_iso9660_dir_lookup(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno, struct fsw_string *lookup_name, struct fsw_iso9660_dnode **child_dno_out) { fsw_status_t status; struct fsw_shandle shand; struct iso9660_dirrec_buffer dirrec_buffer; struct iso9660_dirrec *dirrec = &dirrec_buffer.dirrec; // Preconditions: The caller has checked that dno is a directory node. // setup handle to read the directory status = fsw_shandle_open(dno, &shand); if (status) return status; // scan the directory for the file while (1) { // read next entry status = fsw_iso9660_read_dirrec(vol, &shand, &dirrec_buffer); if (status) goto errorexit; if (dirrec->dirrec_length == 0) { // end of directory reached status = FSW_NOT_FOUND; goto errorexit; } // skip . and .. if (dirrec->file_identifier_length == 1 && (dirrec->file_identifier[0] == 0 || dirrec->file_identifier[0] == 1)) continue; // compare name if (fsw_streq(lookup_name, &dirrec_buffer.name)) // TODO: compare case-insensitively break; } // setup a dnode for the child item status = fsw_dnode_create(dno, dirrec_buffer.ino, FSW_DNODE_TYPE_UNKNOWN, &dirrec_buffer.name, child_dno_out); if (status == FSW_SUCCESS) fsw_memcpy(&(*child_dno_out)->dirrec, dirrec, sizeof(struct iso9660_dirrec)); errorexit: fsw_shandle_close(&shand); return status; }