Exemplo n.º 1
0
static int verify_chain( void )
{
    memory_header *prv = heap.first, *cur;

    if( prv == NULL || verify_header( prv ) != 0 )
    {
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: verification of first header "
                                  "failed\n" );
#endif
        return( 1 );
    }

    if( heap.first->prev != NULL )
    {
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: verification failed: "
                                  "first->prev != NULL\n" );
#endif
        return( 1 );
    }

    cur = heap.first->next;

    while( cur != NULL )
    {
        if( verify_header( cur ) != 0 )
        {
#if defined(MBEDTLS_MEMORY_DEBUG)
            mbedtls_fprintf( stderr, "FATAL: verification of header "
                                      "failed\n" );
#endif
            return( 1 );
        }

        if( cur->prev != prv )
        {
#if defined(MBEDTLS_MEMORY_DEBUG)
            mbedtls_fprintf( stderr, "FATAL: verification failed: "
                                      "cur->prev != prv\n" );
#endif
            return( 1 );
        }

        prv = cur;
        cur = cur->next;
    }

    return( 0 );
}
Exemplo n.º 2
0
static int verify_chain()
{
    memory_header *prv = heap.first, *cur = heap.first->next;

    if( verify_header( heap.first ) != 0 )
    {
#if defined(POLARSSL_MEMORY_DEBUG)
        polarssl_fprintf( stderr, "FATAL: verification of first header "
                                  "failed\n" );
#endif
        return( 1 );
    }

    if( heap.first->prev != NULL )
    {
#if defined(POLARSSL_MEMORY_DEBUG)
        polarssl_fprintf( stderr, "FATAL: verification failed: "
                                  "first->prev != NULL\n" );
#endif
        return( 1 );
    }

    while( cur != NULL )
    {
        if( verify_header( cur ) != 0 )
        {
#if defined(POLARSSL_MEMORY_DEBUG)
            polarssl_fprintf( stderr, "FATAL: verification of header "
                                      "failed\n" );
#endif
            return( 1 );
        }

        if( cur->prev != prv )
        {
#if defined(POLARSSL_MEMORY_DEBUG)
            polarssl_fprintf( stderr, "FATAL: verification failed: "
                                      "cur->prev != prv\n" );
#endif
            return( 1 );
        }

        prv = cur;
        cur = cur->next;
    }

    return( 0 );
}
Exemplo n.º 3
0
/* Verify that the buffer looks sane */
static int verify_buffer(const uint8_t *buf)
{
	int len; /* Including 32bit CRC */
	uint32_t calc_crc;
	uint32_t buf_crc;

	len = verify_header(buf + HEADER_OFFSET);
	if (len < 0) {
		debug("Invalid header\n");
		return -1;
	}

	if (len < HEADER_OFFSET || len > PADDED_SIZE) {
		debug("Invalid header length (%i)\n", len);
		return -1;
	}

	/*
	 * Adjust length to the base of the CRC.
	 * Check the CRC.
	*/
	len -= 4;

	calc_crc = ~pbl_crc32(0, (const char *)buf, len);

	buf_crc = le32_to_cpu(*((uint32_t *)(buf + len)));

	if (buf_crc != calc_crc) {
		fprintf(stderr, "CRC32 does not match (%08x != %08x)\n",
			buf_crc, calc_crc);
		return -1;
	}

	return 0;
}
Exemplo n.º 4
0
Arquivo: mob.c Projeto: 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;
}
Exemplo n.º 5
0
static int load_image(int fileidx, int argc, char **argv)
{
    FILE *file;
    uint16_t i, tgasize, nr;
    struct rgb *imidx;


    if (argc - fileidx <= 0) {
        fprintf(stderr, "No input file specified\n");
        usage();
        return -1;
    }

    input_file = argv[fileidx];

    file = fopen(input_file, "rb");
    if (file == NULL) {
        fprintf(stderr, "Cannot open %s file!\n",argv[1]);
        return -1;
    }

    nr = fread(&tga, sizeof(struct tga_header), 1, file);
    if (nr != 1) {
        fprintf(stderr, "Unable to read file header.\n");
        return -1;
    }

    if(verify_header(&tga)) {
        fprintf(stderr, "Unsupported file format\n");
        dump_header(&tga);
        return -1;
    }

    tgasize = tga.width * tga.height;

    image = malloc(tgasize * sizeof(struct rgb));

    // FIXME: these shouldn't be here
    image_out_4bit = malloc(tgasize * sizeof(struct fbit));
    image_out_scr2 = malloc(MAX_SCR2_SIZE * sizeof(struct scr2));

    if (image == NULL) {
        fprintf(stderr, "Unable to allocate memory\n");
        return -1;
    }

    nr = fread(image, sizeof(struct rgb), tgasize, file);
    if (nr != tgasize) {
        fprintf(stderr, "Unable to read file data.\n");
        return -1;
    }

    fclose(file);

    return 0;
}
Exemplo n.º 6
0
int parse_wakeup_sources(FILE *f, wakeup_source_handler handler, void *data)
{
    char buf[4096 + 128]; /* wake_lock names can be up to a page in length +
                             room for the integer fields. This isn't portable
                             for larger page sizes, but let's hope no one is
                             using a name anywhere near this long. */
    char *ret;
    int rc;
    int count = 0;

    ret = fgets(buf, sizeof(buf), f);
    if (ret != buf) {
        if (!feof(f) && errno) {
            perror("fgets first line");
            return -errno;
        } else {
            fprintf(stderr, "Failed to read first line of wakeup_sources\n");
            return -1;
        }
    }
    rc = verify_header(buf);
    if (rc)
        return rc;

    while (fgets(buf, sizeof(buf), f) == buf) {
        struct wakeup_source *wup;

        rc = parse_wakeup_source(buf, &wup);
        if (rc)
            return rc;
        count++;
        pr_debug("name %-12s\ttime %" PRIu64 "\n", wup->name, wup->last_change);

        if (!handler) {
            wakeup_source_unref(wup);
            continue;
        }
        rc = handler(wup, data);
        wakeup_source_unref(wup);
        if (rc)
            return rc;
    };
    if (!feof(f) && errno) {
        perror("fgets ");
        return -errno;
    }

    return count;
}
Exemplo n.º 7
0
int su_load(su_state *s, su_reader reader, void *data) {
	prototype_t *prot = su_allocate(s, NULL, sizeof(prototype_t));
	reader_buffer_t *buffer = buffer_open(s, reader, data);

	if (verify_header(s, buffer)) {
		buffer_close(s, buffer);
		return -1;
	}

	if (read_prototype(s, buffer, prot)) {
		buffer_close(s, buffer);
		return -1;
	}

	buffer_close(s, buffer);
	gc_insert_object(s, &prot->gc, PROTOTYPE);
	lambda(s, prot, -1);
	return 0;
}
Exemplo n.º 8
0
int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
{
	struct verify_header *hdr;
	struct io_u *io_u = *io_u_ptr;
	unsigned int header_size, hdr_inc, hdr_num = 0;
	void *p;
	int ret;

	if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
		return 0;
	/*
	 * If the IO engine is faking IO (like null), then just pretend
	 * we verified everything.
	 */
	if (td->io_ops->flags & FIO_FAKEIO)
		return 0;

	if (io_u->flags & IO_U_F_TRIMMED) {
		ret = verify_trimmed_io_u(td, io_u);
		goto done;
	}

	hdr_inc = get_hdr_inc(td, io_u);

	ret = 0;
	for (p = io_u->buf; p < io_u->buf + io_u->buflen;
	     p += hdr_inc, hdr_num++) {
		struct vcont vc = {
			.io_u		= io_u,
			.hdr_num	= hdr_num,
			.td		= td,
		};
		unsigned int verify_type;

		if (ret && td->o.verify_fatal)
			break;

		header_size = __hdr_size(td->o.verify);
		if (td->o.verify_offset)
			memswp(p, p + td->o.verify_offset, header_size);
		hdr = p;

		/*
		 * Make rand_seed check pass when have verifysort or
		 * verify_backlog.
		 */
		if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
			io_u->rand_seed = hdr->rand_seed;

		if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
			ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
			if (ret)
				return ret;
		}

		if (td->o.verify != VERIFY_NONE)
			verify_type = td->o.verify;
		else
			verify_type = hdr->verify_type;

		switch (verify_type) {
		case VERIFY_MD5:
			ret = verify_io_u_md5(hdr, &vc);
			break;
		case VERIFY_CRC64:
			ret = verify_io_u_crc64(hdr, &vc);
			break;
		case VERIFY_CRC32C:
		case VERIFY_CRC32C_INTEL:
			ret = verify_io_u_crc32c(hdr, &vc);
			break;
		case VERIFY_CRC32:
			ret = verify_io_u_crc32(hdr, &vc);
			break;
		case VERIFY_CRC16:
			ret = verify_io_u_crc16(hdr, &vc);
			break;
		case VERIFY_CRC7:
			ret = verify_io_u_crc7(hdr, &vc);
			break;
		case VERIFY_SHA256:
			ret = verify_io_u_sha256(hdr, &vc);
			break;
		case VERIFY_SHA512:
			ret = verify_io_u_sha512(hdr, &vc);
			break;
		case VERIFY_XXHASH:
			ret = verify_io_u_xxhash(hdr, &vc);
			break;
		case VERIFY_META:
			ret = verify_io_u_meta(hdr, &vc);
			break;
		case VERIFY_SHA1:
			ret = verify_io_u_sha1(hdr, &vc);
			break;
		case VERIFY_PATTERN:
		case VERIFY_PATTERN_NO_HDR:
			ret = verify_io_u_pattern(hdr, &vc);
			break;
		default:
			log_err("Bad verify type %u\n", hdr->verify_type);
			ret = EINVAL;
		}

		if (ret && verify_type != hdr->verify_type)
			log_err("fio: verify type mismatch (%u media, %u given)\n",
					hdr->verify_type, verify_type);
	}

done:
	if (ret && td->o.verify_fatal)
		fio_mark_td_terminate(td);

	return ret;
}
Exemplo n.º 9
0
bool a78_cart_slot_device::call_load()
{
	if (m_cart)
	{
		UINT32 len;

		if (software_entry() != nullptr)
		{
			const char *pcb_name;
			bool has_ram = get_software_region("ram") ? TRUE : FALSE;
			bool has_nvram = get_software_region("nvram") ? TRUE : FALSE;
			len = get_software_region_length("rom");

			m_cart->rom_alloc(len, tag());
			memcpy(m_cart->get_rom_base(), get_software_region("rom"), len);

			if ((pcb_name = get_feature("slot")) != nullptr)
				m_type = a78_get_pcb_id(pcb_name);
			else
				m_type = A78_TYPE0;

			if (has_ram)
				m_cart->ram_alloc(get_software_region_length("ram"));
			if (has_nvram)
			{
				m_cart->nvram_alloc(get_software_region_length("nvram"));
				battery_load(m_cart->get_nvram_base(), get_software_region_length("nvram"), 0xff);
			}
		}
		else
		{
			// Load and check the header
			int mapper;
			char head[128];
			fread(head, 128);

			if (verify_header((char *)head) == IMAGE_VERIFY_FAIL)
				return IMAGE_INIT_FAIL;

			len = (head[49] << 24) | (head[50] << 16) | (head[51] << 8) | head[52];
			if (len + 128 > length())
			{
				logerror("Invalid length in the header. The game might be corrupted.\n");
				len = length() - 128;
			}

			// let's try to auto-fix some common errors in the header
			mapper = validate_header((head[53] << 8) | head[54], TRUE);

			switch (mapper & 0x2e)
			{
				case 0x0000:
					m_type = BIT(mapper, 0) ? A78_TYPE1 : A78_TYPE0;
					break;
				case 0x0002:
					m_type = BIT(mapper, 0) ? A78_TYPE3 : A78_TYPE2;
					break;
				case 0x0006:
					m_type = A78_TYPE6;
					break;
				case 0x000a:
					m_type = A78_TYPEA;
					break;
				case 0x0022:
				case 0x0026:
					if (len > 0x40000)
						m_type = A78_MEGACART;
					else
						m_type = A78_VERSABOARD;
					break;
			}

			// check if cart has a POKEY at $0450 (typically a VersaBoard variant)
			if (mapper & 0x40)
			{
				if (m_type != A78_TYPE2)
				{
					m_type &= ~0x02;
					m_type += A78_POKEY0450;
				}
			}

			// check special bits, which override the previous
			if ((mapper & 0xff00) == 0x0100)
				m_type = A78_ACTIVISION;
			else if ((mapper & 0xff00) == 0x0200)
				m_type = A78_ABSOLUTE;

			logerror("Cart type: 0x%x\n", m_type);

			if (head[58] == 1)
			{
				osd_printf_info("This cart supports external NVRAM using HSC.\n");
				osd_printf_info("Run it with the High Score Cart mounted to exploit this feature.\n");
			}
			else if (head[58] == 2)
			{
				osd_printf_info("This cart supports external NVRAM using SaveKey.\n");
				osd_printf_info("This is not supported in MAME currently.\n");
			}

			if (head[63])
			{
				osd_printf_info("This cart requires XBoarD / XM expansion\n");
				osd_printf_info("Run it through the expansion to exploit this feature.\n");
			}

			internal_header_logging((UINT8 *)head, length());

			m_cart->rom_alloc(len, tag());
			fread(m_cart->get_rom_base(), len);

			if (m_type == A78_TYPE6)
				m_cart->ram_alloc(0x4000);
			if (m_type == A78_MEGACART || (m_type >= A78_VERSABOARD && m_type <= A78_VERSA_POK450))
				m_cart->ram_alloc(0x8000);
			if (m_type == A78_XB_BOARD || m_type == A78_XM_BOARD)
				m_cart->ram_alloc(0x20000);
			if (m_type == A78_HSC || m_type == A78_XM_BOARD)
			{
				m_cart->nvram_alloc(0x800);
				battery_load(m_cart->get_nvram_base(), 0x800, 0xff);
			}
		}

		//printf("Type: %s\n", a78_get_slot(m_type));
	}
	return IMAGE_INIT_PASS;
}
Exemplo n.º 10
0
Arquivo: verify.c Projeto: Rapaka/fio
int verify_io_u(struct thread_data *td, struct io_u *io_u)
{
	struct verify_header *hdr;
	unsigned int header_size, hdr_inc, hdr_num = 0;
	void *p;
	int ret;

	if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
		return 0;
	if (io_u->flags & IO_U_F_TRIMMED) {
		ret = verify_trimmed_io_u(td, io_u);
		goto done;
	}

	hdr_inc = get_hdr_inc(td, io_u);

	ret = 0;
	for (p = io_u->buf; p < io_u->buf + io_u->buflen;
	     p += hdr_inc, hdr_num++) {
		struct vcont vc = {
			.io_u		= io_u,
			.hdr_num	= hdr_num,
			.td		= td,
		};
		unsigned int verify_type;

		if (ret && td->o.verify_fatal)
			break;

		header_size = __hdr_size(td->o.verify);
		if (td->o.verify_offset)
			memswp(p, p + td->o.verify_offset, header_size);
		hdr = p;

		if (!verify_header(io_u, hdr)) {
			log_err("verify: bad magic header %x, wanted %x at "
				"file %s offset %llu, length %u\n",
				hdr->magic, FIO_HDR_MAGIC,
				io_u->file->file_name,
				io_u->offset + hdr_num * hdr->len, hdr->len);
			return EILSEQ;
		}

		if (td->o.verify != VERIFY_NONE)
			verify_type = td->o.verify;
		else
			verify_type = hdr->verify_type;

		switch (verify_type) {
		case VERIFY_MD5:
			ret = verify_io_u_md5(hdr, &vc);
			break;
		case VERIFY_CRC64:
			ret = verify_io_u_crc64(hdr, &vc);
			break;
		case VERIFY_CRC32C:
		case VERIFY_CRC32C_INTEL:
			ret = verify_io_u_crc32c(hdr, &vc);
			break;
		case VERIFY_CRC32:
			ret = verify_io_u_crc32(hdr, &vc);
			break;
		case VERIFY_CRC16:
			ret = verify_io_u_crc16(hdr, &vc);
			break;
		case VERIFY_CRC7:
			ret = verify_io_u_crc7(hdr, &vc);
			break;
		case VERIFY_SHA256:
			ret = verify_io_u_sha256(hdr, &vc);
			break;
		case VERIFY_SHA512:
			ret = verify_io_u_sha512(hdr, &vc);
			break;
		case VERIFY_META:
			ret = verify_io_u_meta(hdr, &vc);
			break;
		case VERIFY_SHA1:
			ret = verify_io_u_sha1(hdr, &vc);
			break;
		case VERIFY_PATTERN:
			ret = verify_io_u_pattern(hdr, &vc);
			break;
		default:
			log_err("Bad verify type %u\n", hdr->verify_type);
			ret = EINVAL;
		}

		if (ret && verify_type != hdr->verify_type)
			log_err("fio: verify type mismatch (%u media, %u given)\n",
					hdr->verify_type, verify_type);
	}

done:
	if (ret && td->o.verify_fatal)
		td->terminate = 1;

	return ret;
}
int verify_io_u(struct thread_data *td, struct io_u *io_u)
{
    struct verify_header *hdr;
    unsigned int header_size, hdr_inc, hdr_num = 0;
    void *p;
    int ret;

    if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
        return 0;
    if (io_u->flags & IO_U_F_TRIMMED) {
        ret = verify_trimmed_io_u(td, io_u);
        goto done;
    }

    hdr_inc = get_hdr_inc(td, io_u);

    ret = 0;
    for (p = io_u->buf; p < io_u->buf + io_u->buflen;
            p += hdr_inc, hdr_num++) {
        struct vcont vc = {
            .io_u		= io_u,
            .hdr_num	= hdr_num,
            .td		= td,
        };
        unsigned int verify_type;

        if (ret && td->o.verify_fatal)
            break;

        header_size = __hdr_size(td->o.verify);
        if (td->o.verify_offset)
            memswp(p, p + td->o.verify_offset, header_size);
        hdr = p;

        /*
         * Make rand_seed check pass when have verifysort or
         * verify_backlog.
         */
        if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
            io_u->rand_seed = hdr->rand_seed;

        ret = verify_header(io_u, hdr);
        switch (ret) {
        case 0:
            break;
        case 1:
            log_err("verify: bad magic header %x, wanted %x at "
                    "file %s offset %llu, length %u\n",
                    hdr->magic, FIO_HDR_MAGIC,
                    io_u->file->file_name,
                    io_u->offset + hdr_num * hdr->len, hdr->len);
            return EILSEQ;
            break;
        case 2:
            log_err("fio: verify header exceeds buffer length (%u "
                    "> %lu)\n", hdr->len, io_u->buflen);
            return EILSEQ;
            break;
        case 3:
            log_err("verify: bad header rand_seed %"PRIu64
                    ", wanted %"PRIu64" at file %s offset %llu, "
                    "length %u\n",
                    hdr->rand_seed, io_u->rand_seed,
                    io_u->file->file_name,
                    io_u->offset + hdr_num * hdr->len, hdr->len);
            return EILSEQ;
            break;
        case 4:
            return EILSEQ;
            break;
        default:
            log_err("verify: unknown header error at file %s "
                    "offset %llu, length %u\n",
                    io_u->file->file_name,
                    io_u->offset + hdr_num * hdr->len, hdr->len);
            return EILSEQ;
        }

        if (td->o.verify != VERIFY_NONE)
            verify_type = td->o.verify;
        else
            verify_type = hdr->verify_type;

        switch (verify_type) {
        case VERIFY_MD5:
            ret = verify_io_u_md5(hdr, &vc);
            break;
        case VERIFY_CRC64:
            ret = verify_io_u_crc64(hdr, &vc);
            break;
        case VERIFY_CRC32C:
        case VERIFY_CRC32C_INTEL:
            ret = verify_io_u_crc32c(hdr, &vc);
            break;
        case VERIFY_CRC32:
            ret = verify_io_u_crc32(hdr, &vc);
            break;
        case VERIFY_CRC16:
            ret = verify_io_u_crc16(hdr, &vc);
            break;
        case VERIFY_CRC7:
            ret = verify_io_u_crc7(hdr, &vc);
            break;
        case VERIFY_SHA256:
            ret = verify_io_u_sha256(hdr, &vc);
            break;
        case VERIFY_SHA512:
            ret = verify_io_u_sha512(hdr, &vc);
            break;
        case VERIFY_XXHASH:
            ret = verify_io_u_xxhash(hdr, &vc);
            break;
        case VERIFY_META:
            ret = verify_io_u_meta(hdr, &vc);
            break;
        case VERIFY_SHA1:
            ret = verify_io_u_sha1(hdr, &vc);
            break;
        case VERIFY_PATTERN:
            ret = verify_io_u_pattern(hdr, &vc);
            break;
        default:
            log_err("Bad verify type %u\n", hdr->verify_type);
            ret = EINVAL;
        }

        if (ret && verify_type != hdr->verify_type)
            log_err("fio: verify type mismatch (%u media, %u given)\n",
                    hdr->verify_type, verify_type);
    }

done:
    if (ret && td->o.verify_fatal)
        td->terminate = 1;

    return ret;
}