Beispiel #1
0
/* CRC32C checksum tests, based on Intel IPPs, Chapter 13,
 * ippsCRC32C_8u() example, found at the following location:
 * http://software.intel.com/sites/products/documentation/hpc/ipp/ipps/ */
static void
test_crc32c(void)
{
    int i;
    uint8_t data[48] = {
        0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
        0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18,
        0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    /* iSCSI Read PDU */
    assert(ntohl(crc32c(data, 48)) == 0x563a96d9L);

    /* 32 bytes of all zeroes */
    for (i = 0; i < 32; i++) data[i] = 0x00;
    assert(ntohl(crc32c(data, 32)) == 0xaa36918aL);

    /* 32 bytes of all ones */
    for (i = 0; i < 32; i++) data[i] = 0xff;
    assert(ntohl(crc32c(data, 32)) == 0x43aba862L);

    /* 32 bytes of incrementing 00..1f */
    for (i = 0; i < 32; i++) data[i] = i;
    assert(ntohl(crc32c(data, 32)) == 0x4e79dd46L);

    /* 32 bytes of decrementing 1f..00 */
    for (i  = 0; i < 32; i++) data[i] = 31 - i;
    assert(ntohl(crc32c(data, 32)) == 0x5cdb3f11L);

    mark('#');
}
int main(int argc, char **argv)
{
	int c;
	unsigned long checksum = 0;
	char *str;
	char *buf;
	int length = 10;
	u64 seed = 0;
	int loop = 0;
	int i;

	while ((c = getopt(argc, argv, "l:c:s:h")) != -1) {
		switch (c) {
		case 'l':
			length = atol(optarg);
			break;
		case 'c':
			sscanf(optarg, "%li", &checksum);
			loop = 1;
			break;
		case 's':
			seed = atoll(optarg);
			break;
		case 'h':
			print_usage(1);
		case '?':
			print_usage(255);
		}
	}

	set_argv0(argv);
	str = argv[optind];

	if (!loop) {
		if (check_argc_exact(argc - optind, 1))
			print_usage(255);

		printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
		return 0;
	}
	if (check_argc_exact(argc - optind, 0))
		print_usage(255);

	buf = malloc(length);
	if (!buf)
		return -ENOMEM;
	if (seed)
		init_rand_seed(seed);

	while (1) {
		for (i = 0; i < length; i++)
			buf[i] = rand_range(94) + 33;
		if (crc32c(~1, buf, length) == checksum)
			printf("%12lu - %.*s\n", checksum, length, buf);
	}

	return 0;
}
Beispiel #3
0
int main(int argc, char **argv)
{
	int c;
	unsigned long checksum = 0;
	char *str;
	char *buf;
	int length = 10;
	int seed = getpid() ^ getppid();
	int loop = 0;
	int i;

	while ((c = getopt(argc, argv, "l:c:s:h")) != -1) {
		switch (c) {
		case 'l':
			length = atol(optarg);
			break;
		case 'c':
			sscanf(optarg, "%li", &checksum);
			loop = 1;
			break;
		case 's':
			seed = atol(optarg);
			break;
		case 'h':
			usage();
		case '?':
			return 255;
		}
	}

	str = argv[optind];

	if (!loop) {
		if (optind >= argc) {
			fprintf(stderr, "not enough arguments\n");
			return 255;
		}
		printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
		return 0;
	}

	buf = malloc(length);
	if (!buf)
		return -ENOMEM;
	srand(seed);

	while (1) {
		for (i = 0; i < length; i++)
			buf[i] = rand() % 94 + 33;
		if (crc32c(~1, buf, length) == checksum)
			printf("%12lu - %.*s\n", checksum, length, buf);
	}

	return 0;
}
Beispiel #4
0
static int tnt_log_read(struct tnt_log *l, char **buf, uint32_t *size)
{
	/* current record offset (before marker) */
	l->current_offset = ftello(l->fd);

	/* reading marker */
	char *data = NULL;
	uint32_t marker = 0;
	if (fread(&marker, sizeof(marker), 1, l->fd) != 1)
		return tnt_log_eof(l, data);

	/* seeking for marker if necessary */
	while (marker != tnt_log_marker_v11) {
		int c = fgetc(l->fd);
		if (c == EOF)
			return tnt_log_eof(l, data);
		marker = marker >> 8 | ((uint32_t) c & 0xff) <<
			 (sizeof(marker) * 8 - 8);
	}

	/* reading header */
	if (fread(&l->current.hdr, sizeof(l->current.hdr), 1, l->fd) != 1)
		return tnt_log_eof(l, data);

	/* updating offset */
	l->offset = ftello(l->fd);

	/* checking header crc, starting from lsn */
	uint32_t crc32_hdr =
		crc32c(0, (unsigned char*)&l->current.hdr + sizeof(uint32_t),
		       sizeof(struct tnt_log_header_v11) -
		       sizeof(uint32_t));
	if (crc32_hdr != l->current.hdr.crc32_hdr)
		return tnt_log_seterr(l, TNT_LOG_ECORRUPT);

	/* allocating memory and reading data */
	data = tnt_mem_alloc(l->current.hdr.len);
	if (data == NULL)
		return tnt_log_seterr(l, TNT_LOG_EMEMORY);
	if (fread(data, l->current.hdr.len, 1, l->fd) != 1)
		return tnt_log_eof(l, data);

	/* checking data crc */
	uint32_t crc32_data = crc32c(0, (unsigned char*)data, l->current.hdr.len);
	if (crc32_data != l->current.hdr.crc32_data) {
		tnt_mem_free(data);
		return tnt_log_seterr(l, TNT_LOG_ECORRUPT);
	}

	*buf = data;
	*size = l->current.hdr.len;
	return 0;
}
Beispiel #5
0
void _move_elf_syms_location(const void* location, void* new_location)
{
  int size = _get_elf_section_datasize(location);
  // stripped variant
  if (size == 0) {
    relocs.entries = 0;
    relocs.strsize = 0;
    return;
  }
  // incoming header
  auto* hdr = (elfsyms_header*) location;
  // verify CRC sanity check
  const uint32_t temp_hdr = hdr->sanity_check;
  hdr->sanity_check = 0;
  const uint32_t our_sanity = crc32c(hdr, sizeof(elfsyms_header));
  hdr->sanity_check = temp_hdr;
  if (hdr->sanity_check != our_sanity)
  {
    kprintf("ELF syms header CRC failed! "
            "(%08x vs %08x)\n", hdr->sanity_check, our_sanity);
    relocs.entries = 0;
    relocs.strsize = 0;
    return;
  }

  // verify separate checksums of symbols and strings
  uint32_t symbsize = hdr->symtab_entries * sizeof(ElfSym);
  uint32_t csum_syms = crc32c(hdr->syms, symbsize);
  uint32_t csum_strs = crc32c(&hdr->syms[hdr->symtab_entries], hdr->strtab_size);
  if (csum_syms != hdr->checksum_syms || csum_strs != hdr->checksum_strs)
  {
    if (csum_syms != hdr->checksum_syms)
      kprintf("ELF symbol tables checksum failed! "
              "(%08x vs %08x)\n", csum_syms, hdr->checksum_syms);
    if (csum_strs != hdr->checksum_strs)
      kprintf("ELF string tables checksum failed! "
              "(%08x vs %08x)\n", csum_strs, hdr->checksum_strs);
    uint32_t all = crc32c(hdr, sizeof(elfsyms_header) + size);
    kprintf("Checksum ELF section: %08x\n", all);

    relocs.entries = 0;
    relocs.strsize = 0;
    return;
  }
  // update header
  relocs.syms    = (ElfSym*) new_location;
  relocs.entries = hdr->symtab_entries;
  relocs.strsize = hdr->strtab_size;
  relocs.check_syms = hdr->checksum_syms;
  relocs.check_strs = hdr->checksum_strs;
  // copy **overlapping** symbol and string data
  memmove((char*) relocs.syms, (char*) hdr->syms, size);
}
Beispiel #6
0
int ssc_read_blkhdr(int fd, struct blk_header_info *i, off_t offset)
{
	size_t count;
	struct blk_header h, *m = &h;
	uint32_t crc = ~0;

	count = pread(fd, m, SSC_BLK_HDR_SIZE, offset);
	if (count != SSC_BLK_HDR_SIZE)
		return 1;

	crc = crc32c(crc, &m->ondisk_sz, SSC_BLK_HDR_SIZE - sizeof(m->h_csum));

	if (*(uint32_t *)m->h_csum != ~crc)
		fprintf(stderr, "crc error\n");

	SSC_GET_MAM_INFO_VAL(ondisk_sz, 32);
	SSC_GET_MAM_INFO_VAL(blk_sz, 32);
	SSC_GET_MAM_INFO_VAL(blk_type, 32);
	SSC_GET_MAM_INFO_VAL(blk_num, 64);
	SSC_GET_MAM_INFO_VAL(prev, 64);
	SSC_GET_MAM_INFO_VAL(curr, 64);
	SSC_GET_MAM_INFO_VAL(next, 64);

	return 0;
}
Beispiel #7
0
uint32_t file_crc32c(int fd, size_t fs, void *map)
{
	size_t off, n;
	uint32_t crc=0;
	int flag_map_addr_given = 1;
	if (map == NULL)
	{
		map = mmap(NULL, fs, PROT_READ, MAP_SHARED, fd, 0);
		flag_map_addr_given = 0;
		if (map == MAP_FAILED) {
			perror("mmap crc");
			exit(-1);
		}
	}
	long long remain_size = fs;
	for (off=0;remain_size>0; remain_size -= CHUNK)
	{
		n = CHUNK;
		if (remain_size < CHUNK)
			n = remain_size;
//		printf("remain_size=%d mdst=%p off=%d n=%d\n",remain_size,mdst,off,n);
		crc = crc32c(crc,map + off,n);
		off += n;
	}
	if (flag_map_addr_given == 0)
		munmap(map,fs);
	return crc;
}
Beispiel #8
0
BYTE  UpLoad2Flash( BYTE * buffer, BYTE len ){

  DWORD dwActualBootLoaderUpLoadAddr;     //! last uploaded address into the firmware

  //! calcula o CRC32 do buffer recebido 
  if ( e2prom_read( (BYTE*)&dwCRC32, BOOT_LDR_CRC32_ADDR, sizeof( DWORD )) == FALSE ){
    return BOOT_LOADER_E2PROM_ERR;
  }

  dwCRC32 = crc32c( dwCRC32, buffer, len );
  
  if ( e2prom_write( (BYTE*)&dwCRC32, BOOT_LDR_CRC32_ADDR, sizeof( DWORD )) == FALSE ){
    return BOOT_LOADER_E2PROM_ERR;
  } 

  if ( e2prom_read( (BYTE*)&dwActualBootLoaderUpLoadAddr, BOOT_LDR_PCK_CNTRL_ADDR, sizeof( DWORD )) == FALSE ){
    return BOOT_LOADER_E2PROM_ERR;
  }

  // salva o buffer recebido na Flash EEPROM
  if ( Flash_Write( buffer, FEE_BOOTLOADER + dwActualBootLoaderUpLoadAddr, len ) != len ){
    return BOOT_LOADER_FLASH_ERR;
  }

  dwActualBootLoaderUpLoadAddr += len;

  if ( e2prom_write( (BYTE*)&dwActualBootLoaderUpLoadAddr, BOOT_LDR_PCK_CNTRL_ADDR, sizeof( DWORD )) == FALSE ){
    return BOOT_LOADER_E2PROM_ERR;
  }

  return TRUE;
}
Beispiel #9
0
static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
			  unsigned int length)
{
	struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);

	mctx->crc = crc32c(mctx->crc, data, length);
}
Beispiel #10
0
/** wrapper around assoc_find which does the lazy expiration logic */
hash_item *do_item_get(struct default_engine *engine,
                       const hash_key *key) {
    rel_time_t current_time = engine->server.core->get_current_time();
    hash_item *it = assoc_find(engine,
                               crc32c(hash_key_get_key(key),
                                      hash_key_get_key_len(key), 0),
                               key);
    int was_found = 0;

    if (engine->config.verbose > 2) {
        EXTENSION_LOGGER_DESCRIPTOR *logger;
        logger = (void*)engine->server.extension->get_extension(EXTENSION_LOGGER);
        if (it == NULL) {
            logger->log(EXTENSION_LOG_DEBUG, NULL,
                        "> NOT FOUND in bucket %d, %s",
                        hash_key_get_bucket_index(key),
                        hash_key_get_client_key(key));
        } else {
            logger->log(EXTENSION_LOG_DEBUG, NULL,
                        "> FOUND KEY in bucket %d, %s",
                        hash_key_get_bucket_index(item_get_key(it)),
                        hash_key_get_client_key(item_get_key(it)));
            was_found++;
        }
    }

    if (it != NULL && engine->config.oldest_live != 0 &&
        engine->config.oldest_live <= current_time &&
        it->time <= engine->config.oldest_live) {
        do_item_unlink(engine, it);           /* MTSAFE - items.lock held */
        it = NULL;
    }

    if (it == NULL && was_found) {
        EXTENSION_LOGGER_DESCRIPTOR *logger;
        logger = (void*)engine->server.extension->get_extension(EXTENSION_LOGGER);
        logger->log(EXTENSION_LOG_DEBUG, NULL, " -nuked by flush");
        was_found--;
    }

    if (it != NULL && it->exptime != 0 && it->exptime <= current_time) {
        do_item_unlink(engine, it);           /* MTSAFE - items.lock held */
        it = NULL;
    }

    if (it == NULL && was_found) {
        EXTENSION_LOGGER_DESCRIPTOR *logger;
        logger = (void*)engine->server.extension->get_extension(EXTENSION_LOGGER);
        logger->log(EXTENSION_LOG_DEBUG, NULL, " -nuked by expire");
        was_found--;
    }

    if (it != NULL) {
        it->refcount++;
        DEBUG_REFCNT(it, '+');
        do_item_update(engine, it);
    }

    return it;
}
Beispiel #11
0
int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
			 int verify)
{
	char *result;
	u32 len;
	u32 crc = ~(u32)0;

	result = malloc(csum_size * sizeof(char));
	if (!result)
		return 1;

	len = buf->len - BTRFS_CSUM_SIZE;
	crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
	btrfs_csum_final(crc, result);

	if (verify) {
		if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
			printk("checksum verify failed on %llu found %X "
			       "wanted %X\n", (unsigned long long)buf->start,
			       *((int *)result), *((char *)buf->data));
			free(result);
			return 1;
		}
	} else {
		write_extent_buffer(buf, result, 0, csum_size);
	}
	free(result);
	return 0;
}
Beispiel #12
0
int
luksmeta_save(struct crypt_device *cd, int slot,
              const luksmeta_uuid_t uuid, const void *buf, size_t size)
{
    uint32_t length = 0;
    lm_slot_t *s = NULL;
    lm_t lm = {};
    int fd = -1;
    int r = 0;
    off_t off;

    if (uuid_is_zero(uuid))
        return -EKEYREJECTED;

    fd = read_header(cd, O_RDWR | O_SYNC, &length, &lm);
    if (fd < 0)
        return fd;

    if (slot == CRYPT_ANY_SLOT)
        slot = find_unused_slot(cd, &lm);

    r = slot >= 0 && slot < LUKS_NSLOTS ? 0 : -EBADSLT;
    if (r < 0)
        goto error;
    s = &lm.slots[slot];

    r = uuid_is_zero(s->uuid) ? 0 : -EALREADY;
    if (r < 0)
        goto error;

    s->offset = find_gap(&lm, length, size);
    r = s->offset >= ALIGN(sizeof(lm), true) ? 0 : -ENOSPC;
    if (r < 0)
        goto error;

    memcpy(s->uuid, uuid, sizeof(luksmeta_uuid_t));
    s->length = size;
    s->crc32c = crc32c(0, buf, size);

    off = s->offset - sizeof(lm);
    r = lseek(fd, off, SEEK_CUR) == -1 ? -errno : 0;
    if (r < 0)
        goto error;

    r = writeall(fd, buf, size);
    if (r < 0)
        goto error;

    off = s->offset + s->length;
    r = lseek(fd, -off, SEEK_CUR) == -1 ? -errno : 0;
    if (r < 0)
        goto error;

    r = write_header(fd, lm);

error:
    close(fd);
    return r < 0 ? r : slot;
}
Beispiel #13
0
void chksum_update(void *ctx, const unsigned char *data, unsigned int length)
{
	struct chksum_ctx *mctx = ctx;
	unsigned int mcrc;

	mcrc = crc32c(mctx->crc, data, (size_t)length);

	mctx->crc = mcrc;
}
Beispiel #14
0
void
randomCons(char *buf, sccs *s, ser_t d)
{
	u32	date;
	u32	crc1, crc2;
	char	*item;

	/*
	 * We don't want to hash realuser or realhost as that would
	 * make prevent these random bits from being stable.
	 */
	item = USER(s, d);	/* user or user/realuser */
	crc1 = crc32c(0, item, strcspn(item, "/"));
	item = HOSTNAME(s, d);	/* host or host/realhost */
	crc1 = crc32c(crc1, item, strcspn(item, "/["));

	crc2 = crc32c(0, PATHNAME(s, d), strlen(PATHNAME(s, d)));
	date = DATE(s, d);
	crc2 = crc32c(crc2, &date, sizeof(date));
	sprintf(buf, "%x%x", crc1, crc2);
}
Beispiel #15
0
uint32_t lgfs2_log_header_crc(char *buf, unsigned bsize)
{
#ifdef GFS2_HAS_LH_V2
	/* lh_crc CRCs the rest of the block starting after lh_crc */
	const off_t v1_end = offsetof(struct gfs2_log_header, lh_hash) + 4;
	const unsigned char *lb = (const unsigned char *)buf;

	return crc32c(~0, lb + v1_end + 4, bsize - v1_end - 4);
#else
	return 0;
#endif
}
Beispiel #16
0
void __elf_validate_section(const void* location)
{
  int size = _get_elf_section_datasize(location);
  // stripped variant
  if (size == 0) {
    kprintf("ELF syms are considered stripped\n");
    asm("cli; hlt");
  }
  // incoming header
  auto* hdr = (elfsyms_header*) location;
  // verify CRC sanity check
  const uint32_t temp_hdr = hdr->sanity_check;
  hdr->sanity_check = 0;
  const uint32_t our_sanity = crc32c(hdr, sizeof(elfsyms_header));
  hdr->sanity_check = temp_hdr;
  if (hdr->sanity_check != our_sanity)
  {
    kprintf("ELF syms header CRC failed! "
            "(%08x vs %08x)\n", hdr->sanity_check, our_sanity);
    asm("cli; hlt");
  }

  // verify separate checksums of symbols and strings
  uint32_t symbsize = hdr->symtab_entries * sizeof(ElfSym);
  uint32_t csum_syms = crc32c(hdr->syms, symbsize);
  uint32_t csum_strs = crc32c(&hdr->syms[hdr->symtab_entries], hdr->strtab_size);
  if (csum_syms != hdr->checksum_syms || csum_strs != hdr->checksum_strs)
  {
    if (csum_syms != hdr->checksum_syms)
      kprintf("ELF symbol tables checksum failed! "
              "(%08x vs %08x)\n", csum_syms, hdr->checksum_syms);
    if (csum_strs != hdr->checksum_strs)
      kprintf("ELF string tables checksum failed! "
              "(%08x vs %08x)\n", csum_strs, hdr->checksum_strs);
    uint32_t all = crc32c(hdr, sizeof(elfsyms_header) + size);
    kprintf("Checksum ELF section: %08x\n", all);
    asm("cli; hlt");
  }
}
Beispiel #17
0
int tc_verify_cmp(struct tc_spaces *s,
		  uint64_t lsn,
		  struct tnt_iter_storage *is,
		  struct tnt_stream_snapshot *ss)
{
	struct tc_space *space =
		tc_space_match(s, ss->log.current.row_snap.space);
	if (space == NULL)
		return -1;
	int rc = 0;
	struct tc_key *k = tc_generate_key(space, &is->t);
	if (k == NULL) {
		printf("failed to create key\n");
		rc = -1;
		goto done;
	}

	/* 1. check in hash, if found then skip */
	const struct tc_key *node = k;
	mh_int_t pos = mh_pk_get(space->hash_log, &node, space, space);
	const struct tc_key *v = NULL;
	if (pos != mh_end(space->hash_log))
		v = *mh_pk_node(space->hash_log, pos);
	if (v) {
		rc = 0;
		goto done;
	}

	/* 2. if key was not found in xlog hash, then try snapshot hash */
	node = k;
	pos = mh_pk_get(space->hash_snap, &node, space, space);
	v = NULL;
	if (pos != mh_end(space->hash_snap))
		v = *mh_pk_node(space->hash_snap, pos);
	if (v) {
		/* generate and compare checksum */
		uint32_t crc = crc32c(0, (unsigned char*)is->t.data, is->t.size);
		if (crc != v->crc) {
			printf("(snapshot %"PRIu64") checksum missmatch\n", lsn);
			rc = -1;
		}
	} else {
		/* not found */
		printf("(snapshot %"PRIu64") key missed\n", lsn);
		rc = -1;
	}
done:
	free(k);
	return rc; 
}
Beispiel #18
0
static int crc32c_digest(struct ahash_request *req)
{
	struct crypto_hash_walk walk;
	u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
	u32 crc = *mctx;
	int nbytes;

	for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
	     nbytes = crypto_hash_walk_done(&walk, 0))
		crc = crc32c(crc, walk.data, nbytes);

	*(__le32 *)req->result = ~cpu_to_le32(crc);
	return 0;
}
Beispiel #19
0
static int crc32c_update(struct ahash_request *req)
{
	struct crypto_hash_walk walk;
	u32 *crcp = ahash_request_ctx(req);
	u32 crc = *crcp;
	int nbytes;

	for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
	     nbytes = crypto_hash_walk_done(&walk, 0))
		crc = crc32c(crc, walk.data, nbytes);

	*crcp = crc;
	return 0;
}
Beispiel #20
0
static void test_crc32c(void)
{
	unsigned i;

	for(i=0;TEST_CRC32C[i].data;++i) {
		uint32_t digest;
		digest = crc32c(0, (const unsigned char*)TEST_CRC32C[i].data, TEST_CRC32C[i].len);
		if (digest != TEST_CRC32C[i].digest) {
			/* LCOV_EXCL_START */
			fprintf(stderr, "Failed CRC32C test\n");
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}
}
Beispiel #21
0
int
luksmeta_load(struct crypt_device *cd, int slot,
              luksmeta_uuid_t uuid, void *buf, size_t size)
{
    uint32_t length = 0;
    lm_slot_t *s = NULL;
    lm_t lm = {};
    int fd = -1;
    int r = 0;

    if (slot < 0 || slot >= LUKS_NSLOTS)
        return -EBADSLT;
    s = &lm.slots[slot];

    fd = read_header(cd, O_RDONLY, &length, &lm);
    if (fd < 0)
        return fd;

    r = uuid_is_zero(s->uuid) ? -ENODATA : 0;
    if (r < 0)
        goto error;

    if (buf) {
        r = size >= s->length ? 0 : -E2BIG;
        if (r < 0)
            goto error;

        r = lseek(fd, s->offset - sizeof(lm), SEEK_CUR) == -1 ? -errno : 0;
        if (r < 0)
            goto error;

        r = readall(fd, buf, s->length);
        if (r < 0)
            goto error;

        r = crc32c(0, buf, s->length) == s->crc32c ? 0 : -EINVAL;
        if (r < 0)
            goto error;
    }

    memcpy(uuid, s->uuid, sizeof(luksmeta_uuid_t));
    close(fd);
    return s->length;

error:
    close(fd);
    return r;
}
static pg_crc32
AppendOnlyStorageFormat_ComputeHeaderChecksum(
	uint8			*headerPtr,
	int32			headerLen)
{
	pg_crc32	crc;
	
	Assert(headerPtr != NULL);

	/* 
	 * Compute CRC of the header. The header length does not include the
	 * header checksum.
	 */
        crc = crc32c(crc32cInit(), headerPtr, headerLen);
        crc32cFinish(crc);

	return crc;
}
Beispiel #23
0
uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
                            int crc_offset)
{
    uint32_t crc_new;
    uint32_t crc_orig;
    assert(buf != NULL);

    if (crc_offset > 0) {
        memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig));
        memset(buf + crc_offset, 0, sizeof(crc_orig));
    }

    crc_new = crc32c(crc, buf, size);
    if (crc_offset > 0) {
        memcpy(buf + crc_offset, &crc_orig, sizeof(crc_orig));
    }

    return crc_new;
}
Beispiel #24
0
LPVOID search_exp(LPVOID base, DWORD hash)
{
  PIMAGE_DOS_HEADER       dos;
  PIMAGE_NT_HEADERS       nt;
  DWORD                   cnt, rva, dll_h;
  PIMAGE_DATA_DIRECTORY   dir;
  PIMAGE_EXPORT_DIRECTORY exp;
  PDWORD                  adr;
  PDWORD                  sym;
  PWORD                   ord;
  PCHAR                   api, dll;
  LPVOID                  api_adr=NULL;
  
  dos = (PIMAGE_DOS_HEADER)base;
  nt  = RVA2VA(PIMAGE_NT_HEADERS, base, dos->e_lfanew);
  dir = (PIMAGE_DATA_DIRECTORY)nt->OptionalHeader.DataDirectory;
  rva = dir[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
  
  // if no export table, return NULL
  if (rva==0) return NULL;
  
  exp = (PIMAGE_EXPORT_DIRECTORY) RVA2VA(ULONG_PTR, base, rva);
  cnt = exp->NumberOfNames;
  
  // if no api names, return NULL
  if (cnt==0) return NULL;
  
  adr = RVA2VA(PDWORD,base, exp->AddressOfFunctions);
  sym = RVA2VA(PDWORD,base, exp->AddressOfNames);
  ord = RVA2VA(PWORD, base, exp->AddressOfNameOrdinals);
  
  do {
    // calculate hash of api string
    api = RVA2VA(PCHAR, base, sym[cnt-1]);
    // add to DLL hash and compare
    if (crc32c(api) + dll_h == hash) {
      // return address of function
      api_adr = RVA2VA(LPVOID, base, adr[ord[cnt-1]]);
      return api_adr;
    }
  } while (--cnt && api_adr==0);
  return api_adr;
}
Beispiel #25
0
void do_item_unlink(struct default_engine *engine, hash_item *it) {
    const hash_key* key = item_get_key(it);
    MEMCACHED_ITEM_UNLINK(hash_key_get_client_key(key),
                          hash_key_get_client_key_len(key),
                          it->nbytes);
    if ((it->iflag & ITEM_LINKED) != 0) {
        it->iflag &= ~ITEM_LINKED;
        cb_mutex_enter(&engine->stats.lock);
        engine->stats.curr_bytes -= ITEM_ntotal(engine, it);
        engine->stats.curr_items -= 1;
        cb_mutex_exit(&engine->stats.lock);
        assoc_delete(engine, crc32c(hash_key_get_key(key),
                                    hash_key_get_key_len(key), 0),
                     key);
        item_unlink_q(engine, it);
        if (it->refcount == 0 || engine->scrubber.force_delete) {
            item_free(engine, it);
        }
    }
}
Beispiel #26
0
static int csum_block(void *buf, u32 len)
{
	char *result;
	u32 crc = ~(u32)0;
	int ret = 0;

	result = malloc(csum_size * sizeof(char));
	if (!result) {
		fprintf(stderr, "No memory\n");
		return 1;
	}

	len -= BTRFS_CSUM_SIZE;
	crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len);
	btrfs_csum_final(crc, result);

	if (memcmp(buf, result, csum_size))
		ret = 1;
	free(result);
	return ret;
}
Beispiel #27
0
int ssc_write_blkhdr(int fd, struct blk_header_info *i, off_t offset)
{
	size_t count;
	struct blk_header h, *m = &h;
	uint32_t crc = ~0;

	SSC_PUT_MAM_INFO_VAL(ondisk_sz, 32);
	SSC_PUT_MAM_INFO_VAL(blk_sz, 32);
	SSC_PUT_MAM_INFO_VAL(blk_type, 32);
	SSC_PUT_MAM_INFO_VAL(blk_num, 64);
	SSC_PUT_MAM_INFO_VAL(prev, 64);
	SSC_PUT_MAM_INFO_VAL(curr, 64);
	SSC_PUT_MAM_INFO_VAL(next, 64);

	crc = crc32c(crc, &m->ondisk_sz, SSC_BLK_HDR_SIZE - sizeof(m->h_csum));
	*(uint32_t *)m->h_csum = ~crc;

	count = pwrite(fd, m, SSC_BLK_HDR_SIZE, offset);
	if (count != SSC_BLK_HDR_SIZE)
		return 1;

	return 0;
}
static pg_crc32
AppendOnlyStorageFormat_ComputeBlockChecksum(
	uint8			*headerPtr,
	int32			headerLen,
	int32			overallBlockLen)
{
	int32		dataOffset;
	pg_crc32	crc;
	
	Assert(headerPtr != NULL);

	/*
	 * The block checksum covers right after the header checksum through
	 * the end of the whole block (including the optional firstRowNum).
	 */
	dataOffset = headerLen + sizeof(pg_crc32);

	/* Compute CRC of the header. */
        crc = crc32c(crc32cInit(), headerPtr + dataOffset, overallBlockLen - dataOffset);
        crc32cFinish(crc);

	return crc;
}
Beispiel #29
0
static int iscsi_tcp_queue_pdu(struct iscsi_context *iscsi,
                               struct iscsi_pdu *pdu)
{
	if (pdu == NULL) {
		iscsi_set_error(iscsi, "trying to queue NULL pdu");
		return -1;
	}

	if (iscsi->header_digest != ISCSI_HEADER_DIGEST_NONE) {
		unsigned long crc;

		if (pdu->outdata.size < ISCSI_RAW_HEADER_SIZE + 4) {
			iscsi_set_error(iscsi, "PDU too small (%u) to contain header digest",
					(unsigned int) pdu->outdata.size);
			return -1;
		}

		crc = crc32c((char *)pdu->outdata.data, ISCSI_RAW_HEADER_SIZE);

		pdu->outdata.data[ISCSI_RAW_HEADER_SIZE+3] = (crc >> 24)&0xff;
		pdu->outdata.data[ISCSI_RAW_HEADER_SIZE+2] = (crc >> 16)&0xff;
		pdu->outdata.data[ISCSI_RAW_HEADER_SIZE+1] = (crc >>  8)&0xff;
		pdu->outdata.data[ISCSI_RAW_HEADER_SIZE+0] = (crc)      &0xff;
	}
Beispiel #30
0
int do_item_link(struct default_engine *engine, hash_item *it) {
    const hash_key* key = item_get_key(it);
    MEMCACHED_ITEM_LINK(hash_key_get_client_key(key), hash_key_get_client_key_len(key), it->nbytes);
    cb_assert((it->iflag & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    it->iflag |= ITEM_LINKED;
    it->time = engine->server.core->get_current_time();

    assoc_insert(engine, crc32c(hash_key_get_key(key),
                                hash_key_get_key_len(key), 0),
                 it);

    cb_mutex_enter(&engine->stats.lock);
    engine->stats.curr_bytes += ITEM_ntotal(engine, it);
    engine->stats.curr_items += 1;
    engine->stats.total_items += 1;
    cb_mutex_exit(&engine->stats.lock);

    /* Allocate a new CAS ID on link. */
    item_set_cas(NULL, NULL, it, get_cas_id());

    item_link_q(engine, it);

    return 1;
}