Ejemplo n.º 1
0
int parse_privacy_key (const char *text, privacy_key_t *res, int reqeol) {
    int i = 0, j;
    unsigned x, y;
    if (verbosity > 1) {
        fprintf (stderr, "parsing privacy key '%s', reqeol=%d\n", text, reqeol);
    }
    while ((text[i] | 0x20) >= 'a' && (text[i] | 0x20) <= 'z' && i <= 32) {
        i++;
    }
    if (i <= 0 || i == 32) {
        return -1;
    }
    j = i;
    if (text[j] >= '0' && text[j] <= '9') {
        y = 0;
        while (text[j] >= '0' && text[j] <= '9') {
            if (j >= 48 || y > 0x7fffffff / 10) {
                return -1;
            }
            y = y * 10 + (text[j] - '0');
            j++;
        }
    } else if (text[j] == '*') {
        y = ~0;
        j++;
    } else {
        if (text[j] != '_' && text[j] != ' ' && text[j] != ',' && text[j]) {
            return -1;
        }
        while ((signed char) text[j] >= '0') {
            j++;
            if (j >= 64) {
                return -1;
            }
        }
        y = compute_crc32 (text + i, j - i);
    }
    if (text[j]) {
        if (reqeol > 0) {
            return -1;
        }
        if (text[j] != ' ' && (text[j] != ',' || reqeol != -1)) {
            return -1;
        }
    }
    x = compute_crc32 (text, i);
    *res = (((unsigned long long) x) << 32) + y;
    if (verbosity > 1) {
        fprintf (stderr, "privacy key = %016llx\n", *res);
    }
    return j;
}
Ejemplo n.º 2
0
static int rpcc_send_handshake_packet (struct connection *c) {
  struct rpcc_data *D = RPCC_DATA (c);
  static struct rpc_handshake_packet P;
  if (!PID.ip) {
    init_client_PID (c->our_ip);
    if (!PID.ip) {
      PID.ip = get_my_ipv4 ();
    }
  }
  memset (&P, 0, sizeof (P));
  P.len = sizeof (P);
  P.seq_num = -1;
  P.type = RPC_HANDSHAKE;
  P.flags = 0;
  if (!D->remote_pid.port) {
    D->remote_pid.ip = (c->remote_ip == 0x7f000001 ? 0 : c->remote_ip);
    D->remote_pid.port = c->remote_port;
  }
  memcpy (&P.sender_pid, &PID, sizeof (struct process_id));
  memcpy (&P.peer_pid, &D->remote_pid, sizeof (struct process_id));
  P.crc32 = compute_crc32 (&P, sizeof (P) - 4);
  write_out (&c->Out, &P, sizeof (P));
  RPCC_FUNC(c)->flush_packet (c);

  return 0;
}
Ejemplo n.º 3
0
int command_check_hexstring(int argc, char **argv, int optind,
    int flags) {
  int ret = EXIT_SUCCESS;
  int i = optind-1;
  uint32_t strcrc, crc;
  long int cast;

  if(flags & CALC_PRINT_NUMERICAL)
    cast = (uint32_t) strtol(hexarg, NULL, 10);
  else
    cast = (uint32_t) strtol(hexarg, NULL, 16);
  if((cast == LONG_MIN || cast == LONG_MAX) &&
    errno == ERANGE)
    cast = 0;
  strcrc = (uint32_t) cast;

  while(argv[++i]) {
    if(check_access_flags_v(argv[i], F_OK | R_OK, 1) != 0) {
      log_failure(argv[i], "not accessible, not a file or doesn't exist");
      ret = EXIT_FAILURE;
      continue;
    }
    if((crc = compute_crc32(argv[i])) == strcrc)
      log_success(argv[i], "match");
    else 
      log_failure(argv[i], "mismatch: %08X is really %08X", strcrc, crc);
  }

  return ret;
}
Ejemplo n.º 4
0
int rpcc_init_crypto (struct connection *c) {
  if (!(RPCC_DATA(c)->crypto_flags & 2)) {
    return rpcc_init_fake_crypto (c);
  }

  RPCC_DATA(c)->nonce_time = time (0);

  aes_generate_nonce (RPCC_DATA(c)->nonce);

  static struct rpc_nonce_packet buf;
  memset (&buf, 0, sizeof (buf));
  memcpy (buf.crypto_nonce, RPCC_DATA(c)->nonce, 16);
  buf.crypto_ts = RPCC_DATA(c)->nonce_time;
  buf.len = sizeof (buf);
  buf.seq_num = -2;
  buf.type = RPC_NONCE;
  buf.key_select = get_crypto_key_id ();
  buf.crypto_schema = RPC_CRYPTO_AES;
  buf.crc32 = compute_crc32 (&buf, sizeof (buf) - 4);

  write_out (&c->Out, &buf, sizeof (buf));

  assert ((RPCC_DATA(c)->crypto_flags & 14) == 2);
  RPCC_DATA(c)->crypto_flags |= 4;
 
  mark_all_processed (&c->Out);

  return 1;
}
Ejemplo n.º 5
0
void fstree_remove_file(const char *path)
{
	struct dir_record *dr;
	struct vfile_record *vfr;
	uint16_t key = ~0;
	size_t length;
	char *filename;
	int rev;

	filename = split_basename(path, dirname);
	length = (size_t) strlen((char *) filename);
	key = compute_crc32(key, (const unsigned char *) filename, length);

	if (!(dr = find_dr(dirname))) {
		PHOENIXFS_DBG("fstree_remove_file:: missing %s", dirname);
		return;
	}
	if (!(vfr = find(dr->vroot, key, 0))) {
		PHOENIXFS_DBG("fstree_remove_file:: missing %s", filename);
		return;
	}
	for (rev = 0; ; rev++) {
		if (!vfr->history[rev])
			break;
		free(vfr->history[rev]);
	}
	PHOENIXFS_DBG("fstree_remove_file:: %s", path);
	delete(dr->vroot, key);
}
Ejemplo n.º 6
0
void insert_dr(struct dir_record *dr)
{
	uint16_t key = ~0;
	size_t length;

	length = (size_t) strlen((char *) dr->name);
	key = compute_crc32(key, (const unsigned char *) dr->name, length);
	PHOENIXFS_DBG("insert_dr:: %08X", key);
	fsroot = insert(fsroot, key, dr);
}
Ejemplo n.º 7
0
void insert_vfr(struct dir_record *dr, struct vfile_record *vfr)
{
	uint16_t key = ~0;
	size_t length;

	length = (size_t) strlen((char *) vfr->name);
	key = compute_crc32(key, (const unsigned char *) vfr->name, length);
	PHOENIXFS_DBG("insert_vfr:: %08X", key);
	dr->vroot = insert(dr->vroot, key, vfr);
}
Ejemplo n.º 8
0
int tl_expression_compute_magic (struct tl_compiler *C, struct tl_expression *E) {
  if (E->type != tlet_simple) {
    return 0;
  }
  unsigned m = compute_crc32 (E->text, strlen (E->text));
  if (E->magic && E->magic != m) {
     return tl_failf (C, "tl_expression_compute_magic: magic in schema (0x%x) isn't equal to computed magic (0x%x), expr: %s", E->magic, m, E->text);
  }
  E->magic = m;
  return 0;
}
Ejemplo n.º 9
0
int main(int argc, char **argv) {
  uint32_t crcNEW = 0;
  unsigned long crcOLD = 0L;
  crcNEW = compute_crc32(argv[argc-1]);
  crcOLD = computeCRC32(argv[argc-1]);
  printf("crcNEW = %08X\ncrcOLD = %08lX\n",
      crcNEW, crcOLD);
  printf("sizeof(uint32_t)=%" PRIuPTR, sizeof(uint32_t));
  puts("");
  printf("sizeof(unsigned long)=%" PRIuPTR, sizeof(unsigned long));
  puts("");
  return 0;
}
Ejemplo n.º 10
0
struct dir_record *find_dr(const char *path)
{
	struct dir_record *dr;
	uint16_t key = ~0;
	size_t length;

	length = (size_t) strlen((char *) path);
	key = compute_crc32(key, (const unsigned char *) path, length);
	if (!(dr = find(fsroot, key, 0))) {
		PHOENIXFS_DBG("find_dr:: missing %s", path);
		return NULL;
	}
	PHOENIXFS_DBG("find_dr:: found %s", path);
	return dr;
}
Ejemplo n.º 11
0
void fstree_insert_update_file(const char *path, const char *follow)
{
	struct dir_record *dr;
	struct vfile_record *vfr;
	struct file_record *fr, *new_fr;
	uint16_t key = ~0;
	char *filename;
	size_t length;

	filename = split_basename(path, dirname);
	if (!(dr = find_dr(dirname)))
		goto DR;
	else {
		length = (size_t) strlen((char *) filename);
		key = compute_crc32(key, (const unsigned char *) filename, length);
		if (!(vfr = find(dr->vroot, key, 0))) {
			PHOENIXFS_DBG("fstree_insert_update_file:: missing vfr: %s", filename);
			goto VFR;
		}
		else {
			if (vfr->HEAD >= 0)
				fr = vfr->history[vfr->HEAD];
			else
				fr = NULL;
			goto FR;
		}
	}
DR:
	dr = make_dr(dirname);
	insert_dr(dr);
VFR:
	vfr = make_vfr(filename);
	insert_vfr(dr, vfr);
FR:
	if (!(new_fr = make_fr(path, follow))) {
		PHOENIXFS_DBG("fstree_insert_update_file:: Can't make fr %s", path);
		return;
	}

	/* If content is present in the old fr, don't make a new fr */
	if (fr && !memcmp(fr->sha1, new_fr->sha1, 20)) {
		PHOENIXFS_DBG("fstree_insert_update_file:: unmodified: %s", path);
		free(new_fr);
		return;
	}
	insert_fr(vfr, new_fr);
}
Ejemplo n.º 12
0
static int rpcc_send_handshake_error_packet (struct connection *c, int error_code) {
  static struct rpc_handshake_error_packet P;
  if (!PID.pid) {
    init_client_PID (c->our_ip);
  }
  memset (&P, 0, sizeof (P));
  P.len = sizeof (P);
  P.seq_num = -1;
  P.type = RPC_HANDSHAKE_ERROR;
  P.error_code = error_code;
  memcpy (&P.sender_pid, &PID, sizeof (PID));
  P.crc32 = compute_crc32 (&P, sizeof (P) - 4);
  write_out (&c->Out, &P, sizeof (P));
  RPCC_FUNC(c)->flush_packet (c);

  return 0;
}
Ejemplo n.º 13
0
/* Free random image, and optionally update crc32 based on its data */
static uint32_t
free_random_image (uint32_t initcrc,
		   pixman_image_t *img,
		   pixman_format_code_t fmt)
{
    uint32_t crc32 = 0;
    int stride = pixman_image_get_stride (img);
    uint32_t *data = pixman_image_get_data (img);
    int height = pixman_image_get_height (img);

    if (fmt != PIXMAN_null)
    {
	/* mask unused 'x' part */
	if (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt) &&
	    PIXMAN_FORMAT_DEPTH (fmt) != 0)
	{
	    int i;
	    uint32_t *data = pixman_image_get_data (img);
	    uint32_t mask = (1 << PIXMAN_FORMAT_DEPTH (fmt)) - 1;

	    if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_BGRA ||
		PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_RGBA)
	    {
		mask <<= (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt));
	    }

	    for (i = 0; i < 32; i++)
		mask |= mask << (i * PIXMAN_FORMAT_BPP (fmt));

	    for (i = 0; i < stride * height / 4; i++)
		data[i] &= mask;
	}

	/* swap endiannes in order to provide identical results on both big
	 * and litte endian systems
	 */
	image_endian_swap (img);
	crc32 = compute_crc32 (initcrc, data, stride * height);
    }

    pixman_image_unref (img);
    free (data);

    return crc32;
}
Ejemplo n.º 14
0
int rpcc_default_execute (struct connection *c, int op, int len) {
  vkprintf (1, "rpcc_execute: fd=%d, op=%d, len=%d\n", c->fd, op, len);
  if (op == RPC_PING && len == 24) {
    c->last_response_time = precise_now;
    static int Q[12];
    assert (read_in (&c->In, Q, 24) == 24);
    static int P[12];
    P[0] = 24;
    P[1] = RPCC_DATA(c)->out_packet_num++;
    P[2] = RPC_PONG;    
    P[3] = Q[3];
    P[4] = Q[4];
    P[5] = compute_crc32 (P, 20);
    vkprintf (1, "Received ping from fd=%d. ping_id = %lld. Sending pong\n", c->fd, *(long long *)(Q + 3));
    write_out (&c->Out, P, 24);
    RPCC_FUNC(c)->flush_packet(c);
    return 24;
  }
  return SKIP_ALL_BYTES;
}
Ejemplo n.º 15
0
int rpcc_init_fake_crypto (struct connection *c) {
  if (!(RPCC_DATA(c)->crypto_flags & 1)) {
    return -1;
  }

  static struct rpc_nonce_packet buf;
  memset (&buf, 0, sizeof (buf));
  buf.len = sizeof (buf);
  buf.seq_num = -2;
  buf.type = RPC_NONCE;
  buf.crypto_schema = RPC_CRYPTO_NONE;
  buf.crc32 = compute_crc32 (&buf, sizeof (buf) - 4);

  write_out (&c->Out, &buf, sizeof (buf));

  assert ((RPCC_DATA(c)->crypto_flags & 14) == 0);
  RPCC_DATA(c)->crypto_flags |= 4;
 
  return 1;
}
Ejemplo n.º 16
0
static uint32_t
get_checksum_crc(hash_obj_t *seg_hash, int data_size)
{
	int protection;
	int offset = 0;
	uint32_t crc;
	hash_obj_t *sec_hash;
	hash_obj_t *pkt_hash;
	unsigned char *buffer;

	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
	    SECTION_TYPE);
	if (sec_hash == NULL) {
		return ((uint32_t)-1);
	}

	buffer = alloca(data_size);
	if (buffer == NULL) {
		return ((uint32_t)-1);
	}

	/* traverse the packet object list for all the tags and payload */
	for (pkt_hash = seg_hash->u.seg_obj->pkt_obj_list; pkt_hash != NULL;
	    pkt_hash = pkt_hash->u.pkt_obj->next) {
		(void) memcpy(buffer + offset, &pkt_hash->u.pkt_obj->tag,
		    pkt_hash->u.pkt_obj->tag_size);
		offset += pkt_hash->u.pkt_obj->tag_size;
		(void) memcpy(buffer + offset, pkt_hash->u.pkt_obj->payload,
		    pkt_hash->u.pkt_obj->paylen);
		offset += pkt_hash->u.pkt_obj->paylen;
	}

	protection = sec_hash->u.sec_obj->section.protection;

	if (protection == READ_ONLY_SECTION) { /* read-only section */
		crc = compute_crc32(buffer, data_size);
	} else {		/* read/write section */
		crc = compute_checksum32(buffer, data_size);
	}
	return (crc);	/* computed crc */
}
Ejemplo n.º 17
0
int command_check(int argc, char **argv, int optind, int flags) {
  int ret = EXIT_SUCCESS;
  int i = optind-1;
  int  ci, ti;
  uint32_t compcrc, matchcrc;
  char *string;
  char results[9];
  regmatch_t rmatch;
  regex_t regex;

  while(argv[++i]) {
    if(check_access_flags_v(argv[i], F_OK | R_OK, 1) != 0) {
      log_info(argv[i], "Inaccessbile file, skipping.");
      continue;
    }
    string = get_basename((char*)argv[i]);
    compile_regex(&regex, crcregex, REG_ICASE);
    switch(regexec((const regex_t*) &regex, string, 1, &rmatch, 0)) {
      case 0:
        for(ci = rmatch.rm_so, ti = 0; ci < rmatch.rm_eo; ++ci)
            results[ti++] = string[ci];
        results[ti] = '\0';
        break;
      case REG_NOMATCH:
        log_info(argv[i], "Does not contain a hexstring, ignoring.");
        continue;
    }
    regfree(&regex);
    compcrc = compute_crc32(argv[i]);
    matchcrc = (uint32_t) strtol(results, NULL, 16);
    if(compcrc == matchcrc)
      log_success(argv[i], "OK");
    else {
      log_failure(argv[i], "Mismatch: %08X is really %08X", matchcrc, compcrc);
      ret = EXIT_FAILURE;
    }
  } /* while */

  return ret;
}
Ejemplo n.º 18
0
struct vfile_record *find_vfr(const char *path)
{
	uint16_t key = ~0;
	struct dir_record *dr;
	struct vfile_record *vfr;
	char *filename;
	size_t length;

	filename = split_basename(path, dirname);
	if (!(dr = find_dr(dirname)) || !dr->vroot) {
		PHOENIXFS_DBG("find_vfr:: not found %s", path);
		return NULL;
	}
	length = (size_t) strlen((char *) filename);
	key = compute_crc32(key, (const unsigned char *) filename, length);
	if (!(vfr = find(dr->vroot, key, 0))) {
		PHOENIXFS_DBG("find_vfr:: not found %s", path);
		return NULL;
	}
	PHOENIXFS_DBG("find_vfr:: found %s", path);
	return vfr;
}
Ejemplo n.º 19
0
int storage_memory_repair (char *input_filename, char *output_filename) {
  long long bad_images = 0, good_images = 0, hide_logevents = 0;
  vkprintf (3, "%s (%s, %s)\n", __func__, input_filename, output_filename);
  last_mtime = 0;
  file_t A;
  file_open (&A, input_filename, O_RDONLY, 1);

  wfd = open (output_filename, O_WRONLY | O_CREAT, 0660);
  if (wfd < 0) {
    kprintf ("Couldn't create %s. %m\n", output_filename);
    exit (1);
  }

  if (lock_whole_file (wfd, F_WRLCK) <= 0) {
    kprintf ("lock_whole_file (%s, F_WRLCK) fail.\n", output_filename);
    exit (1);
  }

  if (A.size % 4) {
    kprintf ("%s: file '%s' size (%lld) isn't multiple of 4.\n", __func__, input_filename, A.size);
    return -1;
  }
  char *start = malloc (A.size), *a = start;
  if (a == NULL) {
    kprintf ("%s: Not enough memory for load input file ('%s'), file size is %lld bytes.\n",
      __func__, input_filename, A.size);
    return -1;
  }
  if (read (A.fd, a, A.size) != A.size) {
    kprintf ("%s: fail to read whole file '%s'. %m\n", __func__, input_filename);
    return -1;
  }

  crc32_logpos_t P;
  P.log_pos = STORAGE_LEV_START_SIZE;
  P.crc32_complement = crc32_partial (a, STORAGE_LEV_START_SIZE, -1);
  assert (*((unsigned *) a) == LEV_START);
  memcpy (&volume_id, a + 24, 8);

  if (STORAGE_LEV_START_SIZE != write (wfd, a, STORAGE_LEV_START_SIZE)) {
    kprintf ("%s: writing LEV_START to %s fail. %m\n", __func__, output_filename);
    return -1;
  }

  int last_local_id = 0;
  long long avail_bytes = A.size - STORAGE_LEV_START_SIZE;
  a += STORAGE_LEV_START_SIZE;

  while (avail_bytes >= sizeof (struct lev_storage_file) + 20) {
/*
    if (!last_local_id) {
      if (a != start + STORAGE_LEV_START_SIZE) {
        kprintf ("%s: there isn't LEV_STORAGE_FILE after LEV_START in file '%s'.\n",
          __func__, input_filename);
        return -1;
      }
    }
*/
    struct lev_storage_file *f = (struct lev_storage_file *) a;
    if (f->type != LEV_STORAGE_FILE && f->type != LEV_STORAGE_HIDE_FILE) {
      vkprintf (3, "%s: type isn't LEV_STORAGE_FILE or LEV_STORAGE_HIDE_FILE\n, off: %lld\n", __func__, (long long) (a - start));
      a += 4;
      avail_bytes -= 4;
      continue;
    }
    struct lev_crc32 *h = (struct lev_crc32 *) (a - 20);
    if (h->type != LEV_CRC32) {
      vkprintf (2, "%s: previous CRC32 logevent isn't found, off: %lld\n", __func__, (long long) (a - start));
      a += 4;
      avail_bytes -= 4;
      continue;
    }
    if (f->size < 0 || avail_bytes < (long long) sizeof (struct lev_storage_file) + f->size + 20LL) {
      vkprintf (2, "%s: size isn't good (local_id(?):%d), off: %lld\n", __func__,
        f->local_id, (long long) (a - start));
      a += 4;
      avail_bytes -= 4;
      continue;
    }
    int sz = (f->size + 3) & -4;
    struct lev_crc32 *t = (struct lev_crc32 *) (a + sizeof (struct lev_storage_file) + sz);
    if (t->type != LEV_CRC32) {
      vkprintf (2, "%s: next CRC32 logevent isn't found, off: %lld\n", __func__, (long long) (a - start));
      a += 4;
      avail_bytes -= 4;
      continue;
    }
    int ok = -1;
    if (a != start + STORAGE_LEV_START_SIZE) {
      ok = (crc32_partial (h, 20 + sizeof (struct lev_storage_file) + sz, ~h->crc32) == ~t->crc32);
    } else {
      ok = (crc32_partial (a, sizeof (struct lev_storage_file) + sz, P.crc32_complement) == ~t->crc32);
    }
    if (!ok) {
      vkprintf (2, "%s: crc32 between CRC32 logevent failed, off: %lld\n", __func__, (long long) (a - start));
      a += 4;
      avail_bytes -= 4;
      continue;
    }
    if (f->type == LEV_STORAGE_FILE) {
      unsigned char tmp[16];
      md5 (f->data, f->size, tmp);
      //TODO: try to fix one bit code
      if (memcmp (f->md5, tmp, 16)) {
        vkprintf (2, "%s: md5 is mismatched, off: %lld\n", __func__, (long long) (a - start));
        a += 4;
        avail_bytes -= 4;
        continue;
      }
    }
    if (f->type == LEV_STORAGE_HIDE_FILE && f->size) {
      vkprintf (2, "%s: LEV_STORAGE_FILE size isn't equal to 0, off: %lld\n", __func__, (long long) (a - start));
      a += 4;
      avail_bytes -= 4;
      continue;
    }
    if (f->type == LEV_STORAGE_FILE) {
      int i;
      if (last_mtime < f->mtime) {
        last_mtime = f->mtime;
      }
      assert (last_local_id < f->local_id);
      for (i = last_local_id + 1; i < f->local_id; i++) {
        struct lev_storage_file e;
        memcpy (&e, f, sizeof (e));
        e.secret = 0;
        e.size = 159;
        md5 (one_pix_jpg, e.size, e.md5);
        e.local_id = i;
        e.content_type = ct_jpeg;
        e.crc32 = compute_crc32 (one_pix_jpg, e.size);
        e.mtime = 437086800;
        vkprintf (1, "local_id = %d, bad image\n", i);
        bad_images++;
        if (write_image (&e, (char *) one_pix_jpg, &P) < 0) {
          return -1;
        }
      }
      last_local_id = f->local_id;
      good_images++;
    }
    if (f->type == LEV_STORAGE_HIDE_FILE) {
      hide_logevents++;
    }
    if (write_image (f, a + sizeof (struct lev_storage_file), &P) < 0) {
      return -1;
    }
    a += sizeof (struct lev_storage_file) + sz + 20;
    avail_bytes -= sizeof (struct lev_storage_file) + sz + 20;
  }
  assert (!fsync (wfd));
  long long off = lseek (wfd, 0, SEEK_CUR);
  assert (off >= 0);
  assert (!close (wfd));
  kprintf ("%lld bad images, %lld good_images, %lld hide logevents, lost %lld bytes.\n", bad_images, good_images, hide_logevents, (long long) A.size - off);
  fprintf (stderr, "NOTICE: all old indeces for volume %lld must be deleted.\n", volume_id);

  return 0;
}
Ejemplo n.º 20
0
/*
 * Composite operation with pseudorandom images
 */
uint32_t
test_composite (int      testnum,
		int      verbose)
{
    int                i;
    pixman_image_t *   src_img;
    pixman_image_t *   dst_img;
    pixman_region16_t  clip;
    int                dst_width, dst_height;
    int                dst_stride;
    int                dst_x, dst_y;
    int                dst_bpp;
    pixman_op_t        op;
    uint32_t *         dst_bits;
    uint32_t           crc32;
    pixman_format_code_t mask_format, dst_format;
    pixman_trapezoid_t *traps;
    int src_x, src_y;
    int n_traps;

    static pixman_color_t colors[] =
    {
	{ 0xffff, 0xffff, 0xffff, 0xffff },
	{ 0x0000, 0x0000, 0x0000, 0x0000 },
	{ 0xabcd, 0xabcd, 0x0000, 0xabcd },
	{ 0x0000, 0x0000, 0x0000, 0xffff },
	{ 0x0101, 0x0101, 0x0101, 0x0101 },
	{ 0x7777, 0x6666, 0x5555, 0x9999 },
    };
    
    FLOAT_REGS_CORRUPTION_DETECTOR_START ();

    lcg_srand (testnum);

    op = RANDOM_ELT (operators);
    mask_format = RANDOM_ELT (mask_formats);

    /* Create source image */
    
    if (lcg_rand_n (4) == 0)
    {
	src_img = pixman_image_create_solid_fill (
	    &(colors[lcg_rand_n (ARRAY_LENGTH (colors))]));

	src_x = 10;
	src_y = 234;
    }
    else
    {
	pixman_format_code_t src_format = RANDOM_ELT(formats);
	int src_bpp = (PIXMAN_FORMAT_BPP (src_format) + 7) / 8;
	int src_width = lcg_rand_n (MAX_SRC_WIDTH) + 1;
	int src_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1;
	int src_stride = src_width * src_bpp + lcg_rand_n (MAX_STRIDE) * src_bpp;
	uint32_t *bits;

	src_x = -(src_width / 4) + lcg_rand_n (src_width * 3 / 2);
	src_y = -(src_height / 4) + lcg_rand_n (src_height * 3 / 2);

	src_stride = (src_stride + 3) & ~3;
	
	bits = (uint32_t *)make_random_bytes (src_stride * src_height);

	src_img = pixman_image_create_bits (
	    src_format, src_width, src_height, bits, src_stride);

	pixman_image_set_destroy_function (src_img, destroy_bits, bits);

	if (lcg_rand_n (8) == 0)
	{
	    pixman_box16_t clip_boxes[2];
	    int            n = lcg_rand_n (2) + 1;
	    
	    for (i = 0; i < n; i++)
	    {
		clip_boxes[i].x1 = lcg_rand_n (src_width);
		clip_boxes[i].y1 = lcg_rand_n (src_height);
		clip_boxes[i].x2 =
		    clip_boxes[i].x1 + lcg_rand_n (src_width - clip_boxes[i].x1);
		clip_boxes[i].y2 =
		    clip_boxes[i].y1 + lcg_rand_n (src_height - clip_boxes[i].y1);
		
		if (verbose)
		{
		    printf ("source clip box: [%d,%d-%d,%d]\n",
			    clip_boxes[i].x1, clip_boxes[i].y1,
			    clip_boxes[i].x2, clip_boxes[i].y2);
		}
	    }
	    
	    pixman_region_init_rects (&clip, clip_boxes, n);
	    pixman_image_set_clip_region (src_img, &clip);
	    pixman_image_set_source_clipping (src_img, 1);
	    pixman_region_fini (&clip);
	}

	image_endian_swap (src_img);
    }

    /* Create destination image */
    {
	dst_format = RANDOM_ELT(formats);
	dst_bpp = (PIXMAN_FORMAT_BPP (dst_format) + 7) / 8;
	dst_width = lcg_rand_n (MAX_DST_WIDTH) + 1;
	dst_height = lcg_rand_n (MAX_DST_HEIGHT) + 1;
	dst_stride = dst_width * dst_bpp + lcg_rand_n (MAX_STRIDE) * dst_bpp;
	dst_stride = (dst_stride + 3) & ~3;
	
	dst_bits = (uint32_t *)make_random_bytes (dst_stride * dst_height);

	dst_x = -(dst_width / 4) + lcg_rand_n (dst_width * 3 / 2);
	dst_y = -(dst_height / 4) + lcg_rand_n (dst_height * 3 / 2);
	
	dst_img = pixman_image_create_bits (
	    dst_format, dst_width, dst_height, dst_bits, dst_stride);

	image_endian_swap (dst_img);
    }

    /* Create traps */
    {
	int i;

	n_traps = lcg_rand_n (25);
	traps = fence_malloc (n_traps * sizeof (pixman_trapezoid_t));

	for (i = 0; i < n_traps; ++i)
	{
	    pixman_trapezoid_t *t = &(traps[i]);
	    
	    t->top = random_fixed (MAX_DST_HEIGHT) - MAX_DST_HEIGHT / 2;
	    t->bottom = t->top + random_fixed (MAX_DST_HEIGHT);
	    t->left.p1.x = random_fixed (MAX_DST_WIDTH) - MAX_DST_WIDTH / 2;
	    t->left.p1.y = t->top - random_fixed (50);
	    t->left.p2.x = random_fixed (MAX_DST_WIDTH) - MAX_DST_WIDTH / 2;
	    t->left.p2.y = t->bottom + random_fixed (50);
	    t->right.p1.x = t->left.p1.x + random_fixed (MAX_DST_WIDTH);
	    t->right.p1.y = t->top - random_fixed (50);
	    t->right.p2.x = t->left.p2.x + random_fixed (MAX_DST_WIDTH);
	    t->right.p2.y = t->bottom - random_fixed (50);
	}
    }
    
    if (lcg_rand_n (8) == 0)
    {
	pixman_box16_t clip_boxes[2];
	int            n = lcg_rand_n (2) + 1;
	for (i = 0; i < n; i++)
	{
	    clip_boxes[i].x1 = lcg_rand_n (dst_width);
	    clip_boxes[i].y1 = lcg_rand_n (dst_height);
	    clip_boxes[i].x2 =
		clip_boxes[i].x1 + lcg_rand_n (dst_width - clip_boxes[i].x1);
	    clip_boxes[i].y2 =
		clip_boxes[i].y1 + lcg_rand_n (dst_height - clip_boxes[i].y1);

	    if (verbose)
	    {
		printf ("destination clip box: [%d,%d-%d,%d]\n",
		        clip_boxes[i].x1, clip_boxes[i].y1,
		        clip_boxes[i].x2, clip_boxes[i].y2);
	    }
	}
	pixman_region_init_rects (&clip, clip_boxes, n);
	pixman_image_set_clip_region (dst_img, &clip);
	pixman_region_fini (&clip);
    }

    pixman_composite_trapezoids (op, src_img, dst_img, mask_format,
				 src_x, src_y, dst_x, dst_y, n_traps, traps);

    if (dst_format == PIXMAN_x8r8g8b8)
    {
	/* ignore unused part */
	for (i = 0; i < dst_stride * dst_height / 4; i++)
	    dst_bits[i] &= 0xFFFFFF;
    }

    image_endian_swap (dst_img);

    if (verbose)
    {
	int j;
	
	for (i = 0; i < dst_height; i++)
	{
	    for (j = 0; j < dst_stride; j++)
		printf ("%02X ", *((uint8_t *)dst_bits + i * dst_stride + j));

	    printf ("\n");
	}
    }

    crc32 = compute_crc32 (0, dst_bits, dst_stride * dst_height);

    fence_free (dst_bits);
    
    pixman_image_unref (src_img);
    pixman_image_unref (dst_img);
    fence_free (traps);

    FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
    return crc32;
}
Ejemplo n.º 21
0
/*
 * Composite operation with pseudorandom images
 */
uint32_t
test_composite (int      testnum,
		int      verbose)
{
    int                i;
    pixman_image_t *   src_img;
    pixman_image_t *   dst_img;
    pixman_transform_t transform;
    pixman_region16_t  clip;
    int                src_width, src_height;
    int                dst_width, dst_height;
    int                src_stride, dst_stride;
    int                src_x, src_y;
    int                dst_x, dst_y;
    int                src_bpp;
    int                dst_bpp;
    int                w, h;
    pixman_fixed_t     scale_x = 65536, scale_y = 65536;
    pixman_fixed_t     translate_x = 0, translate_y = 0;
    int                op;
    int                repeat = 0;
    int                src_fmt, dst_fmt;
    uint32_t *         srcbuf;
    uint32_t *         dstbuf;
    uint32_t           crc32;

    lcg_srand (testnum);

    src_bpp = (lcg_rand_n (2) == 0) ? 2 : 4;
    dst_bpp = (lcg_rand_n (2) == 0) ? 2 : 4;
    op = (lcg_rand_n (2) == 0) ? PIXMAN_OP_SRC : PIXMAN_OP_OVER;

    src_width = lcg_rand_n (MAX_SRC_WIDTH) + 1;
    src_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1;
    dst_width = lcg_rand_n (MAX_DST_WIDTH) + 1;
    dst_height = lcg_rand_n (MAX_DST_HEIGHT) + 1;
    src_stride = src_width * src_bpp + lcg_rand_n (MAX_STRIDE) * src_bpp;
    dst_stride = dst_width * dst_bpp + lcg_rand_n (MAX_STRIDE) * dst_bpp;

    if (src_stride & 3)
	src_stride += 2;

    if (dst_stride & 3)
	dst_stride += 2;

    src_x = -(src_width / 4) + lcg_rand_n (src_width * 3 / 2);
    src_y = -(src_height / 4) + lcg_rand_n (src_height * 3 / 2);
    dst_x = -(dst_width / 4) + lcg_rand_n (dst_width * 3 / 2);
    dst_y = -(dst_height / 4) + lcg_rand_n (dst_height * 3 / 2);
    w = lcg_rand_n (dst_width * 3 / 2 - dst_x);
    h = lcg_rand_n (dst_height * 3 / 2 - dst_y);

    srcbuf = (uint32_t *)malloc (src_stride * src_height);
    dstbuf = (uint32_t *)malloc (dst_stride * dst_height);

    for (i = 0; i < src_stride * src_height; i++)
	*((uint8_t *)srcbuf + i) = lcg_rand_n (256);

    for (i = 0; i < dst_stride * dst_height; i++)
	*((uint8_t *)dstbuf + i) = lcg_rand_n (256);

    src_fmt = src_bpp == 4 ? (lcg_rand_n (2) == 0 ?
                              PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;

    dst_fmt = dst_bpp == 4 ? (lcg_rand_n (2) == 0 ?
                              PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;

    src_img = pixman_image_create_bits (
        src_fmt, src_width, src_height, srcbuf, src_stride);

    dst_img = pixman_image_create_bits (
        dst_fmt, dst_width, dst_height, dstbuf, dst_stride);

    image_endian_swap (src_img, src_bpp * 8);
    image_endian_swap (dst_img, dst_bpp * 8);

    if (lcg_rand_n (8) > 0)
    {
	scale_x = -32768 * 3 + lcg_rand_N (65536 * 5);
	scale_y = -32768 * 3 + lcg_rand_N (65536 * 5);
	translate_x = lcg_rand_N (65536);
	translate_y = lcg_rand_N (65536);
	pixman_transform_init_scale (&transform, scale_x, scale_y);
	pixman_transform_translate (&transform, NULL, translate_x, translate_y);
	pixman_image_set_transform (src_img, &transform);
    }

    switch (lcg_rand_n (4))
    {
    case 0:
	repeat = PIXMAN_REPEAT_NONE;
	break;

    case 1:
	repeat = PIXMAN_REPEAT_NORMAL;
	break;

    case 2:
	repeat = PIXMAN_REPEAT_PAD;
	break;

    case 3:
	repeat = PIXMAN_REPEAT_REFLECT;
	break;

    default:
        break;
    }
    pixman_image_set_repeat (src_img, repeat);

    if (lcg_rand_n (2))
	pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
    else
	pixman_image_set_filter (src_img, PIXMAN_FILTER_BILINEAR, NULL, 0);

    if (verbose)
    {
	printf ("src_fmt=%08X, dst_fmt=%08X\n", src_fmt, dst_fmt);
	printf ("op=%d, scale_x=%d, scale_y=%d, repeat=%d\n",
	        op, scale_x, scale_y, repeat);
	printf ("translate_x=%d, translate_y=%d\n",
	        translate_x, translate_y);
	printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
	        src_width, src_height, dst_width, dst_height);
	printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
	        src_x, src_y, dst_x, dst_y);
	printf ("w=%d, h=%d\n", w, h);
    }

    if (lcg_rand_n (8) == 0)
    {
	pixman_box16_t clip_boxes[2];
	int            n = lcg_rand_n (2) + 1;

	for (i = 0; i < n; i++)
	{
	    clip_boxes[i].x1 = lcg_rand_n (src_width);
	    clip_boxes[i].y1 = lcg_rand_n (src_height);
	    clip_boxes[i].x2 =
		clip_boxes[i].x1 + lcg_rand_n (src_width - clip_boxes[i].x1);
	    clip_boxes[i].y2 =
		clip_boxes[i].y1 + lcg_rand_n (src_height - clip_boxes[i].y1);

	    if (verbose)
	    {
		printf ("source clip box: [%d,%d-%d,%d]\n",
		        clip_boxes[i].x1, clip_boxes[i].y1,
		        clip_boxes[i].x2, clip_boxes[i].y2);
	    }
	}

	pixman_region_init_rects (&clip, clip_boxes, n);
	pixman_image_set_clip_region (src_img, &clip);
	pixman_image_set_source_clipping (src_img, 1);
	pixman_region_fini (&clip);
    }

    if (lcg_rand_n (8) == 0)
    {
	pixman_box16_t clip_boxes[2];
	int            n = lcg_rand_n (2) + 1;
	for (i = 0; i < n; i++)
	{
	    clip_boxes[i].x1 = lcg_rand_n (dst_width);
	    clip_boxes[i].y1 = lcg_rand_n (dst_height);
	    clip_boxes[i].x2 =
		clip_boxes[i].x1 + lcg_rand_n (dst_width - clip_boxes[i].x1);
	    clip_boxes[i].y2 =
		clip_boxes[i].y1 + lcg_rand_n (dst_height - clip_boxes[i].y1);

	    if (verbose)
	    {
		printf ("destination clip box: [%d,%d-%d,%d]\n",
		        clip_boxes[i].x1, clip_boxes[i].y1,
		        clip_boxes[i].x2, clip_boxes[i].y2);
	    }
	}
	pixman_region_init_rects (&clip, clip_boxes, n);
	pixman_image_set_clip_region (dst_img, &clip);
	pixman_region_fini (&clip);
    }

    pixman_image_composite (op, src_img, NULL, dst_img,
                            src_x, src_y, 0, 0, dst_x, dst_y, w, h);

    if (dst_fmt == PIXMAN_x8r8g8b8)
    {
	/* ignore unused part */
	for (i = 0; i < dst_stride * dst_height / 4; i++)
	    dstbuf[i] &= 0xFFFFFF;
    }

    image_endian_swap (dst_img, dst_bpp * 8);

    if (verbose)
    {
	int j;
	
	for (i = 0; i < dst_height; i++)
	{
	    for (j = 0; j < dst_stride; j++)
		printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));

	    printf ("\n");
	}
    }

    pixman_image_unref (src_img);
    pixman_image_unref (dst_img);

    crc32 = compute_crc32 (0, dstbuf, dst_stride * dst_height);
    free (srcbuf);
    free (dstbuf);
    return crc32;
}
Ejemplo n.º 22
0
static int phoenixfs_rename(const char *path, const char *newpath)
{
	char xnewpath[PATH_MAX];
	struct dir_record *dr;
	struct vfile_record *vfr, *new_vfr;
	char *filename, *newfilename;
	uint16_t key = ~0;
	size_t length;
	uint8_t start_rev, rev_nr;

	PHOENIXFS_DBG("rename:: %s to %s", path, newpath);
	build_xpath(xpath, path, 0);
	build_xpath(xnewpath, newpath, 0);
	if (rename(xpath, xnewpath) < 0)
		return -errno;

	/* Update fstree */
	filename = split_basename(path, xpath);
	if (!(dr = find_dr(xpath))) {
		PHOENIXFS_DBG("rename:: Missing dr for %s", xpath);
		return 0;
	}

	/* Find the old vfr to copy out data from and remove */
	length = (size_t) strlen((char *) filename);
	key = compute_crc32(key, (const unsigned char *) filename, length);
	if (!(vfr = find(dr->vroot, key, 0))) {
		PHOENIXFS_DBG("rename:: Missing vfr for %s", path);
		return 0;
	}

	/* Make a new vfr and copy out history from old vfr */
	newfilename = split_basename(path, NULL);
	new_vfr = make_vfr(newfilename);

	/* Compute start_rev and rev_nr */
	if (vfr->HEAD < 0) {
		start_rev = 0;
		rev_nr = 0;
	} else if (vfr->history[(vfr->HEAD + 1) % REV_TRUNCATE]) {
		/* History is full, and is probably wrapping around */
		start_rev = (vfr->HEAD + 1) % REV_TRUNCATE;
		rev_nr = 20;
	} else {
		/* History is not completely filled */
		start_rev = 0;
		rev_nr = vfr->HEAD + 1;
	}
	PHOENIXFS_DBG("rename:: copying %d revisions", rev_nr);
	while (start_rev < rev_nr) {
		new_vfr->history[start_rev] = vfr->history[start_rev];
		start_rev = (start_rev + 1) % REV_TRUNCATE;
	}
	new_vfr->HEAD = rev_nr - 1;
	insert_vfr(dr, new_vfr);

	/* Remove old vfr */
	dr->vroot = remove_entry(dr->vroot, key);

	return 0;
}