bool is_part(string s1, string s2) { if(s1.size() != s2.size()) return false; if(s1.size() <2) return s1 == s2; if(s1.size() ==2) { if(s1[0] ==s2[0] && s1[1] == s2[1]) return true; if(s1[1] ==s2[0] && s1[0] == s2[1]) return true; return false; } //string temp1 = s1, temp2 = s2; //sort(temp1.begin(), temp1.end()); //sort(temp2.begin(), temp2.end()); //if(temp1 != temp2) // return false; unordered_map<char, int> cur; unordered_map<char, int> from_head; unordered_map<char, int> from_end; int len = s1.size(); for(int i=0; i<len -1; i++) { cur[s1[i]]++; from_head[s2[i]]++; from_end[s2[len-1-i]]++; if(cur == from_head) { if(is_part(s1.substr(0, i+1), s2.substr(0, i+1))) { if( is_part(s1.substr(i+1), s2.substr(i+1))) return true; } } if(cur == from_end) { if(is_part(s1.substr(0, i+1), s2.substr(len-1-i))) { return is_part(s1.substr(i+1), s2.substr(0, len-(i+1))); } return false; } } return false; }
/* * Find out how many partitions have been maped */ static int part_count(struct ptable_list_s *pnode) { int i, ptotal = 0; if (!pnode || !pnode->table) return 0; for (i = 0; i < NUM_PTABLE_ENTRIES; i++) if (is_part(&pnode->table[i]) && !is_extended_part(&pnode->table[i])) ptotal++; return ptotal + part_count(pnode->next); }
/* * returns a structure containing the beginning and ending * offsets of a partition. Note: offsets are byte offsets * not sector numbers. */ struct part_range_s *get_prange(int part_num) { int i = 0, j; fbvar_t *sectsize_var; unsigned sectsize; struct ptable_entry *entry = NULL; struct ptable_list_s *pnode, *matching_node = NULL; struct part_range_s *prange = emalloc(sizeof *prange); /* see mbr.h for a note about the partition numbering convention */ /* retrieve the sector size from the fatback variable table */ sectsize_var = get_fbvar("sectsize"); if (!sectsize_var->val.ival) { display(NORMAL, "Error: sectsize set to 0!\n"); free(sectsize_var); return NULL; } sectsize = sectsize_var->val.ival; free(sectsize_var); /* find the matching partition for the given part_num */ for (pnode = Ptable_list; pnode && !entry; pnode = pnode->next) { for (j = 0; (j < NUM_PTABLE_ENTRIES) && !entry; j++) { struct ptable_entry *part = &pnode->table[j]; if (is_part(part) && !is_extended_part(part)) { if (i == part_num) { matching_node = pnode; entry = part; } else i++; } } } /* if a matching partition for part_num was found, * compute the appropriate values */ if (entry) { prange->start = (matching_node->offset + entry->offset); prange->start *= sectsize; prange->end = prange->start; prange->end += (entry->sectors * sectsize); } return entry? prange : NULL; }
/* * Sanity check a partition table */ static int scheck_ptable(struct ptable_entry *table, sig_t sig) { int i, num_partitions = 0; struct part_range_s prange[NUM_PTABLE_ENTRIES]; fbvar_t *sectsize_var; unsigned sectsize; /* get the sector size from the fatback variable table */ sectsize_var = get_fbvar("sectsize"); if (!sectsize_var->val.ival) { display(NORMAL, "Error: sectsize set to 0!\n"); free(sectsize_var); return 0; } sectsize = sectsize_var->val.ival; free(sectsize_var); /* make sure it has the proper signature bytes */ /* if (!scheck_sig(sig)) { * return 0; * } */ /* make sure all the entries in the table are valid */ for (i = 0; i < NUM_PTABLE_ENTRIES; i++) { if (!is_part(&table[i])) { prange[i].start = 0; prange[i].end = 0; continue; } num_partitions++; /* make note of the boundries of the partition */ prange[i].start = table[i].offset * sectsize; prange[i].end = prange[i].start + (table[i].sectors * sectsize); } /* check to see if the partitions overlap */ if (num_partitions && scheck_part_range(prange)) return 1; else return 0; }
bool isScramble(string s1, string s2) { return is_part(s1, s2); }