view objgrep/lowlevel.c @ 292:6791499809e0

gtm900/fw-reg-config: note that the main config regs are as expected
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 09 Jun 2019 22:56:27 +0000
parents c25367bb7656
children
line wrap: on
line source

/*
 * This C module implements the low-level steps of file mapping and access.
 */

#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "globals.h"

mmap_objfile()
{
	int fd;
	struct stat st;

	fd = open(objfilename, O_RDONLY);
	if (fd < 0) {
		perror(objfilename);
		exit(2);
	}
	fstat(fd, &st);
	if (!S_ISREG(st.st_mode)) {
		fprintf(stderr, "error: %s is not a regular file\n",
			objfilename);
		exit(2);
	}
	objfile_tot_size = st.st_size;
	objfilemap = mmap(NULL, objfile_tot_size, PROT_READ, MAP_PRIVATE,
			  fd, 0L);
	if (objfilemap == MAP_FAILED) {
		perror("mmap");
		exit(2);
	}
	close(fd);
}

mmap_binfile()
{
	int fd;
	struct stat st;

	fd = open(binfilename, O_RDONLY);
	if (fd < 0) {
		perror(binfilename);
		exit(2);
	}
	fstat(fd, &st);
	if (!S_ISREG(st.st_mode)) {
		fprintf(stderr, "error: %s is not a regular file\n",
			binfilename);
		exit(2);
	}
	binfile_tot_size = st.st_size;
	binfilemap = mmap(NULL, binfile_tot_size, PROT_READ, MAP_PRIVATE,
			  fd, 0L);
	if (binfilemap == MAP_FAILED) {
		perror("mmap");
		exit(2);
	}
	close(fd);
}

unsigned
get_u16(ptr)
	u_char *ptr;
{
	return ptr[0] | ptr[1] << 8;
}

get_s16(ptr)
	u_char *ptr;
{
	int i;

	i = ptr[0] | ptr[1] << 8;
	if (i >= 32768)
		i -= 65536;
	return(i);
}

unsigned
get_u32(ptr)
	u_char *ptr;
{
	return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
}