Beispiel #1
0
static void MakeMainAddr(void)
{
	unsigned short iaddr = 0;
	
	iaddr = MAKE_SHORT(ParaUni.addr_sn);
	main_addr[0] = ((iaddr%100)/10 << 4) + iaddr%10;
	iaddr /= 100;
	main_addr[1] = ((iaddr%100)/10 << 4) + iaddr%10;
	iaddr /= 100;
	main_addr[2] = ((iaddr%100)/10 << 4) + iaddr%10;
}
Beispiel #2
0
void parseElf32(FILE* fp, long fileSize)
{
    const int sizeOfElfHeader = 0x34;
    struct ELF_Header_32      elfHeader32 = {{ 0 }};

    unsigned char* buffer = (unsigned char*)malloc(sizeOfElfHeader);
    fread(buffer, sizeof(unsigned char), sizeOfElfHeader, fp);
    char mag_str[4] = { buffer[0], buffer[1], buffer[2], buffer[3] };

    // 32-bit ELF Header
    memcpy(elfHeader32.ei_mag, mag_str, 4);
    elfHeader32.ei_class =      buffer[0x04];
    elfHeader32.ei_data =       buffer[0x05];
    elfHeader32.ei_version =    buffer[0x06];
    elfHeader32.ei_osabi =      buffer[0x07];
    elfHeader32.ei_abiversion = buffer[0x08];
    elfHeader32.e_type =    MAKE_SHORT(buffer[0x11], buffer[0x10]);
    elfHeader32.e_machine = MAKE_SHORT(buffer[0x13], buffer[0x12]);
    elfHeader32.e_version =
      MAKE_LONG(buffer[0x17], buffer[0x16], buffer[0x15], buffer[0x14]);
    elfHeader32.e_entry =
      MAKE_LONG(buffer[0x1B], buffer[0x1A], buffer[0x19], buffer[0x18]);
    elfHeader32.e_phoff =
      MAKE_LONG(buffer[0x1F], buffer[0x1E], buffer[0x1D], buffer[0x1C]);
    elfHeader32.e_shoff =
      MAKE_LONG(buffer[0x23], buffer[0x22], buffer[0x21], buffer[0x20]);
    elfHeader32.e_flags =
      MAKE_LONG(buffer[0x27], buffer[0x26], buffer[0x25], buffer[0x24]);
    elfHeader32.e_ehsize =    MAKE_SHORT(buffer[0x29], buffer[0x28]);
    elfHeader32.e_phentsize = MAKE_SHORT(buffer[0x2B], buffer[0x2A]);
    elfHeader32.e_phnum =     MAKE_SHORT(buffer[0x2D], buffer[0x2C]);
    elfHeader32.e_shentsize = MAKE_SHORT(buffer[0x2F], buffer[0x2E]);
    elfHeader32.e_shnum =     MAKE_SHORT(buffer[0x31], buffer[0x30]);
    elfHeader32.e_shstrndx =  MAKE_SHORT(buffer[0x33], buffer[0x32]);
    free(buffer);


    // 32-bit Program Headers
    // if not there already, seek to program header offset found in elfHeader
    if (sizeOfElfHeader != elfHeader32.e_phoff) {
        if (fseek(fp, elfHeader32.e_phoff, SEEK_SET) != 0) {
            printf("[ELF] Error seeking to elfHeader32.e_phoff (parse).\n");
            // TODO: Clean exit in cases like this.
            exit(-1);
        }
    }
    if (elfHeader32.e_phentsize !=
            (unsigned short)sizeof(struct Program_Header_32)) {
        printf("[ELF Fatal Error] elfHeader32.e_phentsize == ");
        printf("sizeof(Program_Header_32)\n[Is file corrupt?]\n");
        // TODO: Clean exit in cases like this.
        exit(-1);
    }

    int i = 0;
    int sizeOfProgramHeaders = elfHeader32.e_phnum * elfHeader32.e_phentsize;
    buffer = (unsigned char*)malloc(sizeOfProgramHeaders);
    fread(buffer, sizeof(unsigned char), sizeOfProgramHeaders, fp);
    struct Program_Header_32* programHeaders =
        (struct Program_Header_32*)malloc(sizeOfProgramHeaders);

    for (i = 0; i < elfHeader32.e_phnum; i++) {
        int cur_phoff = (i * elfHeader32.e_phentsize);
        struct Program_Header_32 cur_programHeader = { 0 };

        cur_programHeader.p_type =
            MAKE_LONG(buffer[cur_phoff+0x03], buffer[cur_phoff+0x02],
                buffer[cur_phoff+0x01], buffer[cur_phoff+0x00]);
        cur_programHeader.p_offset =
            MAKE_LONG(buffer[cur_phoff+0x07], buffer[cur_phoff+0x06],
              buffer[cur_phoff+0x05], buffer[cur_phoff+0x04]);
        cur_programHeader.p_vaddr =
            MAKE_LONG(buffer[cur_phoff+0x0B], buffer[cur_phoff+0x0A],
                buffer[cur_phoff+0x09], buffer[cur_phoff+0x08]);
        cur_programHeader.p_paddr =
            MAKE_LONG(buffer[cur_phoff+0x0F], buffer[cur_phoff+0x0E],
              buffer[cur_phoff+0x0D], buffer[cur_phoff+0x0C]);
        cur_programHeader.p_filesz =
            MAKE_LONG(buffer[cur_phoff+0x13], buffer[cur_phoff+0x12],
                buffer[cur_phoff+0x11], buffer[cur_phoff+0x10]);
        cur_programHeader.p_memsz =
            MAKE_LONG(buffer[cur_phoff+0x17], buffer[cur_phoff+0x16],
                buffer[cur_phoff+0x15], buffer[cur_phoff+0x14]);
        cur_programHeader.p_flags =
            MAKE_LONG(buffer[cur_phoff+0x1B], buffer[cur_phoff+0x1A],
                buffer[cur_phoff+0x19], buffer[cur_phoff+0x18]);
        cur_programHeader.p_align =
            MAKE_LONG(buffer[cur_phoff+0x1F], buffer[cur_phoff+0x1E],
                buffer[cur_phoff+0x1D], buffer[cur_phoff+0x1C]);

        memcpy(&programHeaders[i], &cur_programHeader,
            elfHeader32.e_phentsize);
    }
    free(buffer);


    // 32-bit Section Headers
    if (fseek(fp, elfHeader32.e_shoff, SEEK_SET) != 0) {
        printf("[ELF] Error seeking to elfHeader32.e_shoff (parse).\n");
        // TODO: Clean exit in cases like this.
        free(programHeaders);
        exit(-1);
    }
    if (elfHeader32.e_shentsize !=
      (unsigned short)sizeof(struct Section_Header_32)) {
      printf("[ELF Fatal Error] elfHeader32.e_shentsize == ");
        printf("sizeof(Section_Header_32)\n[Is file corrupt?]\n");
        // TODO: Clean exit in cases like this.
        free(programHeaders);
        exit(-1);
    }

    int sizeOfSectionHeaders = elfHeader32.e_shnum * elfHeader32.e_shentsize;
    buffer = (unsigned char*)malloc(sizeOfSectionHeaders);
    fread(buffer, sizeof(unsigned char), sizeOfSectionHeaders, fp);
    struct Section_Header_32* sectionHeaders =
        (struct Section_Header_32*)malloc(sizeOfSectionHeaders);

    for (i = 0; i < elfHeader32.e_shnum; i++) {
        int cur_shoff = (i * elfHeader32.e_shentsize);
        struct Section_Header_32 cur_sectionHeader = { 0 };

        cur_sectionHeader.sh_name =
            MAKE_LONG(buffer[cur_shoff+0x03], buffer[cur_shoff+0x02],
                buffer[cur_shoff+0x01], buffer[cur_shoff+0x00]);
        cur_sectionHeader.sh_type =
            MAKE_LONG(buffer[cur_shoff+0x07], buffer[cur_shoff+0x06],
              buffer[cur_shoff+0x05], buffer[cur_shoff+0x04]);
        cur_sectionHeader.sh_flags =
            MAKE_LONG(buffer[cur_shoff+0x0B], buffer[cur_shoff+0x0A],
                buffer[cur_shoff+0x09], buffer[cur_shoff+0x08]);
        cur_sectionHeader.sh_address =
            MAKE_LONG(buffer[cur_shoff+0x0F], buffer[cur_shoff+0x0E],
              buffer[cur_shoff+0x0D], buffer[cur_shoff+0x0C]);
        cur_sectionHeader.sh_offset =
            MAKE_LONG(buffer[cur_shoff+0x13], buffer[cur_shoff+0x12],
                buffer[cur_shoff+0x11], buffer[cur_shoff+0x10]);
        cur_sectionHeader.sh_size =
            MAKE_LONG(buffer[cur_shoff+0x17], buffer[cur_shoff+0x16],
                buffer[cur_shoff+0x15], buffer[cur_shoff+0x14]);
        cur_sectionHeader.sh_link =
            MAKE_LONG(buffer[cur_shoff+0x1B], buffer[cur_shoff+0x1A],
                buffer[cur_shoff+0x19], buffer[cur_shoff+0x18]);
        cur_sectionHeader.sh_info =
            MAKE_LONG(buffer[cur_shoff+0x1F], buffer[cur_shoff+0x1E],
                buffer[cur_shoff+0x1D], buffer[cur_shoff+0x1C]);
        cur_sectionHeader.sh_addralign =
            MAKE_LONG(buffer[cur_shoff+0x23], buffer[cur_shoff+0x22],
                buffer[cur_shoff+0x21], buffer[cur_shoff+0x20]);
        cur_sectionHeader.sh_entsize =
            MAKE_LONG(buffer[cur_shoff+0x27], buffer[cur_shoff+0x26],
                buffer[cur_shoff+0x25], buffer[cur_shoff+0x24]);

        memcpy(&sectionHeaders[i], &cur_sectionHeader,
            elfHeader32.e_shentsize);
    }
    free(buffer);

    // Now, store the shstrtab
    int shstrtab_addr =
        sectionHeaders[elfHeader32.e_shstrndx].sh_offset;
    if (!shstrtab_addr || fseek(fp, shstrtab_addr, SEEK_SET) != 0) {
        printf("[ELF] Error seeking to shstrtab_addr (parse).\n");
        // TODO: Clean exit in cases like this.
        free(programHeaders);
        free(sectionHeaders);
        exit(-1);
    }
    int shstrtab_size = sectionHeaders[elfHeader32.e_shstrndx].sh_size;
    char* shstrtab = (char*)malloc(
      sizeof(char)*shstrtab_size);
    fread(shstrtab, sizeof(char), shstrtab_size, fp);

    // Printing!
    unsigned char* codeSection = malloc(sectionHeaders[1].sh_size);
    fseek(fp, sectionHeaders[1].sh_offset, SEEK_SET); // TODO: Error checking
    fread(codeSection, sizeof(unsigned char), sectionHeaders[1].sh_size, fp);
    disassemble_x86(&shstrtab[sectionHeaders[1].sh_name],
        sectionHeaders[1].sh_address,
        codeSection,
        sectionHeaders[1].sh_size);
    free(codeSection);
    /* arbitrary separator here */
    codeSection = malloc(sectionHeaders[2].sh_size);
    fseek(fp, sectionHeaders[2].sh_offset, SEEK_SET); // TODO: Error checking
    fread(codeSection, sizeof(unsigned char), sectionHeaders[2].sh_size, fp);
    disassemble_x86(&shstrtab[sectionHeaders[2].sh_name],
        sectionHeaders[2].sh_address,
        codeSection,
        sectionHeaders[2].sh_size);
    free(codeSection);
    /* arbitrary separator here */
    printSectionHeader32(sectionHeaders[3], shstrtab);
    codeSection = malloc(sectionHeaders[3].sh_size);
    fseek(fp, sectionHeaders[3].sh_offset, SEEK_SET); // TODO: Error checking
    fread(codeSection, sizeof(unsigned char), sectionHeaders[3].sh_size, fp);
    disassemble_x86(&shstrtab[sectionHeaders[3].sh_name],
        sectionHeaders[3].sh_address,
        codeSection,
        sectionHeaders[3].sh_size);
    free(codeSection);


    // cleanup
    free(programHeaders);
    free(sectionHeaders);
    free(shstrtab);
}
static int adjust_output(image_segment *image, scan_parameters *scanp, 
		scanner_parameters *scannerp)
/* Needing a good cleanup */
{
	/* light and dark points for the CCD sensor in question 
	 * (stored in file as 0-1024, scaled to 0-65536) */
	unsigned long hi, lo;
	/* The result of our calculations */
	unsigned long result;
	unsigned long temp;
	/* The CCD sensor which read the current pixel - this is a tricky value
	   to get right. */
	int ccd, scaled_xoff;
	/* Loop variables */
	unsigned int scanline, pixelnum, colour;
	unsigned long int pixel_address;
	unsigned int cols = scanp->mode ? 3 : 1;

	for (scanline = 0; scanline < image->height; scanline++)
	{
		for (pixelnum = 0; pixelnum < image->width; pixelnum++)
		{
			/* Figure out CCD sensor number */
			/* MAGIC FORMULA ALERT! */
			ccd = (pixelnum << (scannerp->natural_xresolution - 
						scanp->xresolution)) + (1 << 
						(scannerp->natural_xresolution 
						 - scanp->xresolution)) - 1;

			scaled_xoff = scanp->xoffset << 
				(scannerp->natural_xresolution - 
				 scanp->xresolution);

			ccd += scaled_xoff;	

			for (colour = 0; colour < cols; colour++)
			{
				/* Address of pixel under scrutiny */
				pixel_address = 
					(scanline * image->width * cols * 2) +
					(pixelnum * cols * 2) + (colour * 2);

				/* Dark value is easy 
				 * Range of lo is 0-18k */
				lo = (scannerp->blackweight[ccd]) * 3;

				/* Light value depends on the colour, 
				 * and is an average in greyscale mode. */
				if (scanp->mode == 1) /* RGB */
				{
					switch (colour)
					{
						case 0: hi = scannerp->redweight[ccd] * 3; 
							break;
						case 1: hi = scannerp->greenweight[ccd] * 3; 
							break;
						default: hi = scannerp->blueweight[ccd] * 3; 
							 break;
					}
				}
				else /* Grey - scanned using green */
				{
					hi = scannerp->greenweight[ccd] * 3; 
				}

				/* Check for bad calibration data as it 
				   can cause a divide-by-0 error */
				if (hi <= lo)
				{
					DBG(1, "adjust_output: Bad cal data!"
							" hi: %ld lo: %ld\n"
							"Recalibrate, that "
							"should fix it.\n", 
							hi, lo);
					return -1;
				}

				/* Start with the pixel value in result */
				result = MAKE_SHORT(*(image->image_data + 
							pixel_address), 
						*(image->image_data + 
							pixel_address + 1));

				result = result >> 6; /* Range now = 0-1023 */
					/*
				if (scanline == 10)
					DBG(200, "adjust_output: Initial pixel"
							" value: %ld\n", 
							result);
							*/
				result *= 54;         /* Range now = 0-54k */

				/* Clip to dark and light values */
				if (result < lo) result = lo;
				if (result > hi) result = hi;

				/* result = (base-lo) * max_value / (hi-lo) */
				temp = result - lo;
				temp *= 65536;
				temp /= (hi - lo);

				/* Clip output  result has been clipped to lo,
				 * and hi >= lo, so temp can't be < 0 */
				if (temp > 65535)
					temp = 65535;
				/*
				if (scanline == 10)
				{
					DBG(200, "adjust_output: %d: base = "
							"%lu, result %lu (%lu "
							"- %lu)\n", pixelnum, 
							result, temp, lo, hi);
				}			
				*/
				result = temp;

				/* Store the value back where it came 
				 * from (always bigendian) */
				*(image->image_data + pixel_address)
					= HIGH_BYTE(result);
				*(image->image_data + pixel_address+1)
					= LOW_BYTE(result);
			}
		}
	}
	/*DBG(100, "Finished adjusting output\n");*/
	return 0;
}