Example #1
0
void
extract_files(struct tailq *queue, char *infile)
{
    char html_fname[MAX_FILENAME];
    char *file;
    int newLineIndex = 0, length = 0;
    unsigned long fileLen;
    
    file = mmap_file(infile);
    
    if (!file)
        return;
    
    fileLen = strlen(file);
    
    // Read each line into the queue
    for (int i = 0; i<fileLen+1; i++) {
        if (file[i] == '\n' || (i > 0 && file[i] == '\0' && file[i-1] != '\n')) {
            length = i-newLineIndex;
            strncpy(html_fname, file+newLineIndex, length);
            html_fname[length] = '\0';
            file_add(queue, html_fname);
            newLineIndex = i+1;
        }
    }
}
Example #2
0
int test(const char* file) {
    const int max_file_size = 1.8 * 1024 * 1024 * 1024;
    const int step = 1024;

    char* pc = (char*)mmap_file(file, max_file_size);
    if (pc == NULL) {
        printf("mmap file failed!\n");
        exit(-1);
    }

    write_buffer(pc, max_file_size, step);

    for (int j = 1; j <= 10; ++j) {
        printf("sleep %dth second\n", j);
        sleep(1);
    }
    printf("start msync...\n");
    msync((void*)pc, max_file_size, MS_ASYNC);
    sleep(10);

    printf("start munmap...\n");
    munmap((void*)pc, max_file_size);

    return 0;
}
Example #3
0
static int run_dump_radio(const char *ifname)
{
	int i;
	char path[1024];
	struct file_map m;
	struct radio_entry *e;

	check_daemon();
	snprintf(path, sizeof(path), DB_RD_FILE, ifname);

	if (mmap_file(path, sizeof(struct radio_entry), &m))
	{
		fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
		return 1;
	}

	for (i = 0; i < m.size; i += sizeof(struct radio_entry))
	{
		e = (struct radio_entry *) &m.mmap[i];

		if (!e->time)
			continue;

		printf("[ %u, %d, %d, %d ]%s\n",
			ntohl(e->time),
			e->rate, e->rssi, e->noise,
			((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
	}

	umap_file(&m);

	return 0;
}
Example #4
0
int				main(int ac, char **av)
{
	char		*path;
	char		*file;
	struct stat	buf;

	if ((ac = ft_options(&av, NM_OPTIONS, 0)))
	{
		while (*av)
		{
			path = *av;
			file = 0;
			if (ac == 1)
				path = "a.out";
			if (ac > 2)
				ft_printf("\n%s:\n", path);
			if (mmap_file(path, &buf, &file) == SUCCESS)
			{
				ft_nm(file, path);
				munmap_file(file, &buf);
			}
			av++;
		}
	}
	return (FT_EXIT);
}
Example #5
0
static int run_dump_load(void)
{
	int i;
	char path[1024];
	struct file_map m;
	struct load_entry *e;

	check_daemon();
	snprintf(path, sizeof(path), DB_LD_FILE);

	if (mmap_file(path, sizeof(struct load_entry), &m))
	{
		fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
		return 1;
	}

	for (i = 0; i < m.size; i += sizeof(struct load_entry))
	{
		e = (struct load_entry *) &m.mmap[i];

		if (!e->time)
			continue;

		printf("[ %u, %u, %u, %u ]%s\n",
			ntohl(e->time),
			ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
			((i + sizeof(struct load_entry)) < m.size) ? "," : "");
	}

	umap_file(&m);

	return 0;
}
Example #6
0
void import(Module *state, array_t *params)
{
    size_t length = 0;
    CORD input = NULL;
    Module *target = NULL;
    char in_file[MAX_LIB_NAME];

    ARITY(import, 2);
    REQUIRES(import, 0, STR);
    REQUIRES(import, 1, STR);

    if(tst_search(state->imports, ARG(1)->data)) {
        // already loaded so just skip it
        return;
    }

    if(Token_copy(ARG(0), in_file, MAX_LIB_NAME, TK_STR) == -1) {
        die(state, "requested module name %s is too long, must be less than %d.",
                in_file, MAX_LIB_NAME);
    }

    input = mmap_file(in_file, &length);
    assert(input && "Failed to open the file you requested.");

    // compile the other file using the code size specified by the parent
    target = Module_create(in_file, state->max_code_size);

    // have to add it here to prevent recursive loads from going in a loop
    state->imports = tst_insert(state->imports, ARG(1)->start, ARG(1)->len, target);
    assert(state->imports && "Error importing into the parent namespace.");

    if(!Module_compile(target, input, length)) {
        die(target, "error parsing imported module %s.", in_file);
    }
}
Example #7
0
static int run_dump_ifname(const char *ifname)
{
	int i;
	char path[1024];
	struct file_map m;
	struct traffic_entry *e;

	check_daemon();
	snprintf(path, sizeof(path), DB_IF_FILE, ifname);

	if (mmap_file(path, sizeof(struct traffic_entry), &m))
	{
		fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
		return 1;
	}

	for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
	{
		e = (struct traffic_entry *) &m.mmap[i];

		if (!e->time)
			continue;

		printf("[ %u, %u, %" PRIu32
			   ", %u, %u ]%s\n",
			ntohl(e->time),
			ntohl(e->rxb), ntohl(e->rxp),
			ntohl(e->txb), ntohl(e->txp),
			((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
	}

	umap_file(&m);

	return 0;
}
Example #8
0
int
main(int argc, char **argv)
{
    const char *file = "foo";
    const size_t sz = 16384;
    char *malloc_buf;
    void *mmap_buf;
    int fd;

    srand(time(NULL));

    generate_random_file(file, sz);

    fd = open(file, O_RDONLY, 0);
    if (fd < 0)
	err(1, "open %s", file);

    malloc_buf = read_file(fd, sz);
    mmap_buf = mmap_file(fd, sz);
    close(fd);
    unlink(file);
    if (memcmp(malloc_buf, mmap_buf, sz) != 0)
	return 1;
    return 0;
}
int main(int argc, char *argv[])
{
	char *dir;

	if (argc != 2 && argc != 3)
		fail("usage: ungpkg filename.pkg [target]");

	pkg = mmap_file(argv[1]);

	if (argc == 2) {
		dir = malloc(0x31);
		memset(dir, 0, 0x31);
		memset(dir, 0, 0x30);
		memcpy(dir, pkg + 0x30, 0x30);
	} else {
		dir = argv[2];
	}

	MKDIR(dir, 0777);

	if (chdir(dir) != 0)
		fail("chdir(%s)", dir);

	offset = be64(pkg + 0x20);
	size = be64(pkg + 0x28);

	if (be16(pkg + 0x04) & 0x8000)
		decrypt_retail_pkg();
	else
		decrypt_debug_pkg();

	unpack_pkg();

	return 0;
}
Example #10
0
File: mob.c Project: gerdr/m0
bool m0_mob_load(m0_interp *interp, const char *name, FILE *err)
{
	struct loader loader = { interp, name, err, NULL, 0, NULL, 0 };

	if(!mmap_file(&loader))
		goto FAIL;

	if(!verify_header(&loader))
		goto FAIL;

	if(!load_chunks(&loader))
		goto FAIL;

	if(!verify_chunks(&loader))
		goto FAIL;

	return 1;

FAIL:
	if(loader.mapping)
	{
		bool status = m0_platform_munmap(loader.mapping, loader.size);
		assert(status == 1);
	}

	return 0;
}
int main(int argc, char *argv[])
{
	FILE *fp;
	u8 bfr[ALIGNMENT];

	get_args(argc, argv);

	elf_size = get_filesize(elf_name);
	elf = mmap_file(elf_name);

	parse_elf();

	meta_header_size = 0x80 + ehdr.e_phnum * (0x30 + 0x20 + 0x60) + 0x30;
	info_offset = 0x70;
	elf_offset = 0x90;
	phdr_offset = elf_offset + ehdr.e_ehsize;
	sec_offset = round_up(phdr_offset + ehdr.e_phentsize * ehdr.e_phnum, ALIGNMENT);
	version_offset = round_up(sec_offset + ehdr.e_phnum *  0x20, ALIGNMENT);
	ctrl_offset = round_up(version_offset + 0x10, ALIGNMENT);
	meta_offset = round_up(ctrl_offset + 0x70, ALIGNMENT);
	header_size = round_up(meta_offset + meta_header_size, 0x80);

	if (compression)
		compress_elf();
	else
		fill_phdr_map();
	
	build_sce_hdr();
	build_info_hdr();
	build_ctrl_hdr();
	build_sec_hdr();
	build_version_hdr();
	build_meta_hdr();

	self = malloc(header_size + elf_size);
	memset(self, 0, header_size + elf_size);

	build_hdr();
	write_elf();
	calculate_hashes();
	sign_hdr();

	sce_encrypt_data(self);
	sce_encrypt_header(self, &ks);

	fp = fopen(self_name, "wb");
	if (fp == NULL)
		fail("fopen(%s) failed", self_name);

	if (fwrite(self, header_size + compressed_size, 1, fp) != 1)
		fail("unable to write self");

	memset(bfr, 0, sizeof bfr);
	fwrite(bfr, round_up(compressed_size, ALIGNMENT) - compressed_size, 1, fp);

	fclose(fp);

	return 0;
}
Example #12
0
void *mmap_filename(const char *file, int shared, int prot, size_t *sz){
  int fd = open(file, O_RDONLY);
  if(fd < 0){
    perror("open");
    return NULL;
  }
  void *buf = mmap_file(fd, shared, prot, sz);
  close(fd);
  return buf;
}
void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
{
    HANDLE fm, h;
   
    void* map = MAP_FAILED;

    const DWORD dwFileOffsetLow = (sizeof(off_t) <= sizeof(DWORD)) ? (DWORD)off : (DWORD)(off & 0xFFFFFFFFL);
    const DWORD dwFileOffsetHigh = (sizeof(off_t) <= sizeof(DWORD)) ? (DWORD)0 : (DWORD)((off >> 32) & 0xFFFFFFFFL);
    
	const DWORD protect = mmap_page(prot);
    const DWORD desiredAccess = mmap_file(prot);

    const off_t maxSize = off + (off_t)len;

    const DWORD dwMaxSizeLow = (sizeof(off_t) <= sizeof(DWORD)) ? (DWORD)maxSize : (DWORD)(maxSize & 0xFFFFFFFFL);
    const DWORD dwMaxSizeHigh = (sizeof(off_t) <= sizeof(DWORD)) ? (DWORD)0 : (DWORD)((maxSize >> 32) & 0xFFFFFFFFL);

    errno = 0;
   
    if (len == 0 || (flags & MAP_FIXED) != 0 || prot == PROT_EXEC)	// Here we check for unsupported flags
    {
        errno = EINVAL;
        return MAP_FAILED;
    }
   
    h = ((flags & MAP_ANONYMOUS) == 0) ? (HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE;

    if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE)
    {
        errno = EBADF;
        return MAP_FAILED;
    }

    fm = CreateFileMapping(h, NULL, protect, dwMaxSizeHigh, dwMaxSizeLow, NULL);

    if (fm == NULL)
    {
        errno = windowsErrorToErrno(GetLastError());
        return MAP_FAILED;
    }
 
    map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len);

    CloseHandle(fm);
 
    if (map == NULL)
    {
        errno = windowsErrorToErrno(GetLastError());
        return MAP_FAILED;
    }

    return map;
}
Example #14
0
bed_file_t *bed_open(char *filename) {
    size_t len;
    char *data = mmap_file(&len, filename);

    bed_file_t *bed_file = (bed_file_t *) malloc(sizeof(bed_file_t));
    bed_file->filename = filename;
    bed_file->data = data;
    bed_file->data_len = len;
    bed_file->header_entries = linked_list_new(COLLECTION_MODE_SYNCHRONIZED);
    bed_file->records = linked_list_new(COLLECTION_MODE_SYNCHRONIZED);
    return bed_file;
}
Example #15
0
int main(int argc, char *argv[]){

	if (argc == 2) {
		pup = mmap_file(argv[1]);
		printf( "PUP Unpacker\n");
		Unpup(argv[1]);

	} else if(argc == 3) {
		if (strcmp(argv[1], "-d") != 0)
			if(strcmp(argv[1], "-r") !=0)
				if(strcmp(argv[1], "-rd") !=0)
					fail("invalid option: %s", argv[1]);
		
		if(strcmp(argv[1], "-d")==0)
			set_dbg_flag();

		if(strcmp(argv[1], "-r")==0)
			read_only=1;

		if(strcmp(argv[1], "-rd")==0){
			read_only=1;
			set_dbg_flag();
		}

		printf( "PUP Unpacker\n");
		pup = mmap_file(argv[2]);
		dmsg("PUP File: %s\n",argv[2]);
		Unpup(argv[2]);

	}else {
		fail("usage: %s PSP2UPDAT.PUP\n"
			"	-d	| debug messages\n"
			"	-r	| read only mode\n"
			"	-rd	| read only + debug messages"
			,argv[0]);
	}


	return 0;
}
Example #16
0
static void get_files(const char *d)
{
	DIR *dir;
	struct dirent *de;
	struct stat st;
	char path[256];
	u32 i;
	u64 offset;

	dir = opendir(d);
	if (dir == NULL)
		fail("opendir");

	offset = 0;
	i = 0;
	while ((de = readdir(dir))) {
		if (n_files == MAX_FILES)
			fail("file overflow. increase MAX_FILES");

		if (strcmp(de->d_name, ".") == 0)
			continue;

		if (strcmp(de->d_name, "..") == 0)
			continue;

		if (strlen(de->d_name) > 0x20)
			fail("name too long: %s", de->d_name);

		snprintf(path, sizeof path, "%s/%s", d, de->d_name);

		memset(&files[i], 0, sizeof(*files));
		strncpy(files[i].name, de->d_name, 0x19);

		if (stat(path, &st) < 0)
			fail("cannot stat %s", path);

		if (!S_ISREG(st.st_mode))
			fail("not a file: %s", de->d_name);

		files[i].size = st.st_size;

		files[i].ptr = mmap_file(path);
		if (files[i].ptr == NULL)
			fail("unable to mmap %s", path);

		files[i].offset = offset;
		offset = round_up(offset + files[i].size, 0x20);

		i++;
		n_files++;
	}
}
int main(int argc, char *argv[])
{
	if (argc != 3)
		fail("usage: cosunpack dump.b directory");

	cos = mmap_file(argv[1]);

	new_dir(argv[2]);

	do_toc(cos);

	return 0;
}
Example #18
0
int main(int argc, char *argv[]){

	if (argc == 2) {
		self = mmap_file(argv[1]);
		printf( "readself          - v0.1  By deroad\n\n");
		readself();
	}else {
		fail("usage: %s file.self"
			,argv[0]);
	}


	return 0;
}
Example #19
0
int main(int argc, char** argv) {
    uint8_t *image_buf;
    int fd;
    struct bpb33* bpb;
    if (argc < 2) {
	usage(argv[0]);
    }

    image_buf = mmap_file(argv[1], &fd);
    bpb = check_bootsector(image_buf);

    
	//uint16_t cluster = 0;

   // struct direntry *dirent = (struct direntry*)cluster_to_addr(cluster, image_buf, bpb);

	int	FATsz = bpb->bpbSectors;
	int *refcount = (int *)malloc(sizeof(int)*FATsz);
	memset((int*)refcount, 0, FATsz * sizeof(int));  //creates array of all 0s of FAT size
	
	/*int i = 0;
    for ( ; i < bpb->bpbRootDirEnts; i++)
    { 
        uint16_t followclust = print_dirent(dirent,0, image_buf, bpb, refcount );
        if (is_valid_cluster(followclust, bpb))

            follow_dir(followclust, 1, image_buf, bpb, refcount);

        dirent++;
    }*/



	//where we run dope functions
	printf("\n");
	checkandfix(image_buf, bpb, refcount);
	orphan_search(image_buf, bpb, refcount, FATsz);

	free(refcount);
	






    unmmap_file(image_buf, &fd);
    return 0;
}
Example #20
0
int main(int argc, char **argv)
{
  if (argc < 3) {
    fprintf(stderr, "call: %s queryfile file\n", argv[0]);
    return 1;
  }
  size_t m;
  const void *q = mmap_file(argv[1], &m);
  if (!q) {
    perror(0);
    return 1;
  }
  size_t n;
  const void *t = mmap_file(argv[2], &n);
  if (!q) {
    perror(0);
    return 1;
  }
  const void *p = memmem(t, n, q, m);
  if (!p)
    return 1;
  printf("%zd\n", p-t);
  return 0;
}
Example #21
0
int main(int argc, char *argv[])
{
	if (argc != 3)
		fail("usage: unpkg filename.pkg target");

	pkg = mmap_file(argv[1]);

	mkdir(argv[2], 0777);

	if (chdir(argv[2]) != 0)
		fail("chdir");

	parse_pkg();

	return 0;
}
Example #22
0
/*
 * Update the ELF file residing at @p path, replacing all occurrences
 * of @p search with @p replace in that file's ".debug_info" or
 * ".debug_str" section.
 * The replacement string must be the same length or shorter than
 * the search string.
 * Returns 0 on success (whether or not ".debug_info" section was
 * found or updated).
 * Returns 1 on serious error that should cause distcc to fail.
 */
static int update_debug_info(const char *path, const char *search,
                              const char *replace) {
  struct stat st;
  int fd;
  void *base;

  base = mmap_file(path, &fd, &st);
  if (base == NULL) {
    return 0;
  }

  update_section(path, base, st.st_size, ".debug_info", search, replace);
  update_section(path, base, st.st_size, ".debug_str", search, replace);

  return munmap_file(base, path, fd, &st);
}
Example #23
0
/**
 *
 * Testovaci modul pre scanner.c
 */
int main( int argc, char *argv[] )
{
    if( argc != 2 )
    {
        fprintf( stderr, "Error: No input file specified !\n" );
        return E_OTHER;
    }

    E_ERROR_TYPE ret_val;
    char *handle_subor;     /**< abstrakcia zdrojoveho handle_suboru */
    unsigned file_size;       /**< velkost suboru */
    
    ret_val = mmap_file( argv[1], &handle_subor, &file_size );

    if ( ret_val != E_OK )
        return E_INTERPRET_ERROR;
    
    char *subor = handle_subor;
    
    if ( check_file_header( &subor ) != E_OK ) // kontrola '<?php ' na zaciatku handle_suboru
    {
        fprintf( stderr, "Error: Invalid source file.\n" );
        free(handle_subor);
        return E_SYNTAX;
    }
    
    
    T_token token;
    token.ttype = E_INVLD;
    
    scanner_init( subor, file_size - 5); // scanner dostava subor o 6 znakov mensi koli '<?php '
        
        
    printf("---------------------------");
    while(token.ttype != E_EOF)
    {
        scanner_get_token(&token);
        print_token(&token);
        getchar();
    }
    printf("---------------------------\n");
    
    free( handle_subor );
    return EXIT_SUCCESS;
}
Example #24
0
int main(int argc, char** argv)
{
    uint8_t *image_buf;
    int fd;
    struct bpb33* bpb;
    if (argc != 2)
    {
	usage(argv[0]);
    }
    char filename[128];
    image_buf = mmap_file(argv[1], &fd);
    bpb = check_bootsector(image_buf);
    traverse_root(filename, image_buf, bpb);

    unmmap_file(image_buf, &fd);

    return 0;
}
Example #25
0
int main(int argc, char** argv) {
    uint8_t *image_buf;
    int fd;
    struct bpb33* bpb;
    if (argc < 2) {
        usage(argv[0]);
    }

    image_buf = mmap_file(argv[1], &fd);
    bpb = check_bootsector(image_buf);
    traverse_root(image_buf, bpb);
    foster_orphans(image_buf, bpb);

    free_clusters();
    free(bpb);
    unmmap_file(image_buf, &fd);
    return 0;
}
Example #26
0
static struct buffer read_input_into_buffer(const char *filename){
  int fd = open(filename, O_RDONLY);
  if(fd == -1){
    perror("open");
    return null_buffer;
  }
  errno = 0;
  struct buffer file_buf = mmap_file(fd, 0);
  if(errno != 0){
    return null_buffer;//mmap_file calls perror
  }
  close(fd);//mmap internally dups the file descriptor
  if(errno != 0){
    perror("close");
    return null_buffer;
  }
  return file_buf;
}
Example #27
0
int main(int argc, char** argv)
{
    uint8_t *image_buf;
    int fd;
    struct bpb33* bpb;
    if (argc != 2)
    {
	usage(argv[0]);
    }

    image_buf = mmap_file(argv[1], &fd);
    bpb = check_bootsector(image_buf);
    printf("Root directory address is: %lu\n", root_dir_addr(image_buf, bpb));
    traverse_root(image_buf, bpb);

    unmmap_file(image_buf, &fd);

    return 0;
}
Example #28
0
int main(int argc, char * argv[]) {
	int i = 0;
	char *ptr = NULL;
	char *string = NULL;
	int fd = -1;
	Header* p;
	char *tab = NULL;
	char size = sizeof(char)*7;
	name_file(&i);
	/*debug_println_ptr("dedans address ", &address);*/
	fd = open_file("t");
	if (fd != -1){
		ptr = (char*)mmap_file();
		string = (char*)mymalloc(size);
		if (string == NULL) {
			perror("Erreur d'allocation");
		}
		strcpy(string, "coucou ");

		/*p = (void*)string - sizeof(Header);
		printf("%s\n", string);
		printf("string:%d  size:%d  address:%d\n", (int)string, p->size, address);*/
		tab = (char*)mymalloc(size);
		if (tab == NULL) {
			perror("Erreur d'allocation");
		}
		strcpy(tab, "coucou ");
		p = (void*)string - sizeof(Header);
		
		/* printf("string -> %s | tab -> %s | poniteur address %d string address %d file %d\n", string, tab, (int)p, (int)string, (int)ptr); */
		printf("\n p->size %d\n", p->size);
		/*debug_println_ptr("dehors address ", &address);*/
		myfree(string);
		string = NULL;
		/*printf("string -> %s | tab -> %s\n", string, tab);*/
		
	}
	else
		printf("Erreur %d\n", fd);
	exit(EXIT_SUCCESS);
}
Example #29
0
gff_file_t *gff_open(char *filename) 
{
    size_t len;
    char *data = mmap_file(&len, filename);

    gff_file_t *gff_file = (gff_file_t *) malloc(sizeof(gff_file_t));
    gff_file->filename = filename;
    gff_file->data = data;
    gff_file->data_len = len;
    gff_file->header_entries = cp_list_create_list(COLLECTION_MODE_MULTIPLE_VALUES | COLLECTION_MODE_DEEP,
                                                   NULL,
                                                   NULL,
                                                   (cp_destructor_fn) gff_header_entry_free
                                                  );
    gff_file->records = cp_list_create(COLLECTION_MODE_MULTIPLE_VALUES | COLLECTION_MODE_DEEP,
                                       NULL,
                                       NULL,
                                       (cp_destructor_fn) gff_record_free
                                      );
    return gff_file;
}
Example #30
0
int main(int argc, char** argv) {
    uint8_t *image_buf;
    int fd;
    struct bpb33* bpb;
    if (argc < 2) {
	usage(argv[0]);
    }

    image_buf = mmap_file(argv[1], &fd);
    bpb = check_bootsector(image_buf);

    // your code should start here...






    unmmap_file(image_buf, &fd);
    return 0;
}