int main(int argc, char *argv[]) { PE_FILE pe; FILE *fp, *datafile, *outfile; IMAGE_SECTION_HEADER section; unsigned long data_size, hole_size = 0; unsigned char *buff; fp = datafile = outfile = NULL; if (argc < 4) { usage(); exit(1); } parse_options(argc, argv); // opcoes //if (!(outfile = fopen(argv[argc-1], "w"))) // EXIT_ERROR("unable to write outfile"); if (!(fp = fopen(argv[argc-2], "r+b"))) EXIT_ERROR("PE file not found or unreadable"); if (!(datafile = fopen(datafile_path, "rb"))) EXIT_ERROR("datafile not found or unreadable"); pe_init(&pe, fp); if (!ispe(&pe)) EXIT_ERROR("not a valid PE file"); // switch method if (!find_section(&pe, §ion, true)) EXIT_ERROR("no code sections found"); hole_size = get_hole(&pe, §ion); if (!hole_size) EXIT_ERROR("no holes found"); printf("hole size: %ld\n", hole_size); printf("hole addr: %ld\n", ftell(pe.handle)); // <pev> </pev> fseek(datafile, 0L, SEEK_END); data_size = ftell(datafile); if (data_size + 11> hole_size) EXIT_ERROR("not enough space"); rewind(datafile); buff = xmalloc(data_size); fread(buff, sizeof(buff), 1, datafile); //'fwrite(buff, sizeof(buff), 1, pe.handle); free(buff); fclose(fp); fclose(outfile); fclose(datafile); return 0; }
void *kmalloc(uint32_t size) { /* local vars */ linknode *ptr; uint32_t base, i; /* Get free hole: */ base = get_hole(log2(nextpow2(size))); /* Allocate physical pages: */ for (i = base & 0xFFFFF000; i < base+size; i+=PAGE_SIZE) arch_vmpage_map(NULL, i, 0); /* Store info about this allocated space... */ ptr = (linknode *) get_hole(log2(16)); arch_vmpage_map(NULL, (uint32_t) ptr, 0); linkedlist_add(&usedlist, ptr); ptr->datum[0] = base; ptr->datum[1] = size; return (void *) base; }
uint32_t get_hole(uint32_t i) { if (i > U) return NULL; if (i < L) i = L; if (freelist[i].count) { linknode *ptr = freelist[i] .first; /* Remove first element from the free list. */ linkedlist_remove(&freelist[i], ptr, NULL); if (i > 11) arch_vmpage_unmap(NULL, ptr); return (uint32_t) ptr; } else { uint32_t ret = get_hole(i+1); if (ret == NULL) return ret; /* Split ret to two (2^i) chunks, one is free, the other returned. */ arch_vmpage_map(NULL, ret + pow2(i), 0); linkedlist_add(&freelist[i], (linknode *) (ret + pow2(i))); /* printk("ret: %x %dBytes\n", ret, pow2(i)); */ return ret; } }