Пример #1
0
int read_chunkfile(char * chunkfile, char *data){
/* An example chunk file is:
0 b129b015e8a4258dc8c0d0b3a63acaeda8d14cd8
1 8085b28ac91f81435f9108388a3a4c8d9b01e84a
*/
/* 	This read a chunkfile, convert the string hashvalue into binary value, and write into
	"data" parameter, return the size of the "data" parameter
*/
	FILE *fp = fopen( chunkfile, "r");
  	if( fp == NULL ){
    	printf("%s can not be found\n", chunkfile);
    	return -1;
  	}
  	int index = 0;
  	char line[40];
  	memset(line, 0, 40);
  	int count = 0;
  	while( fscanf(fp, "%d %s\n", &index, line)  > 0 ){
  		/* the max size of this line should less then 40 */
  		if( strlen( line ) > 40 )
  			return -1;
  		hex2binary((char*)line, 40, (uint8_t *)(data + count));
  		count += 20;
  		memset(line, 0, 40);

  	}
  	data[count] = '\0';
  	fclose(fp);
	return count;  	
}
Пример #2
0
TEST(sml_proc_par_value, parse_time) {
	hex2binary("72620472620265000000FF", sml_buf_get_current_buf(buf));
	sml_proc_par_value *t = sml_proc_par_value_parse(buf);
	TEST_ASSERT_NOT_NULL(t);
	TEST_ASSERT_EQUAL(SML_PROC_PAR_VALUE_TAG_TIME, *(t->tag));
	TEST_ASSERT_EQUAL(11, buf->cursor);
}
Пример #3
0
TEST(sml_number, parse_unsigned32_optional) {
	hex2binary("01", sml_buf_get_current_buf(buf));
	u32 *n = sml_u32_parse(buf);
	TEST_ASSERT_NULL(n);
	TEST_ASSERT_EQUAL(1, buf->cursor);
	sml_u32_free( n );
}
Пример #4
0
TEST(sml_number, parse_unsigned8) {
	hex2binary("6201", sml_buf_get_current_buf(buf));
	u8 *n = sml_u8_parse(buf);
	TEST_ASSERT_EQUAL(1, *n);
	TEST_ASSERT_EQUAL(2, buf->cursor);
	sml_u8_free( n );
}
Пример #5
0
/* @brief First converts decimalNumber from decimal to hex. Next, it passes the
 *        result to the given hex2binary function.
 * @param decimalNumber: Number to be converted.
 * @param bytesNeeded: The maximum possible number of hex bytes needed to
 *        represent the number.
 * @param binaryNumber: buffer where the binary number is stored
 */
void dec2hex2binary(int decimalNumber, int bytesNeeded, uint8_t* binaryNumber){

  int quotient;
  int i=bytesNeeded-1, temp;
  char hexadecimalNumber[bytesNeeded];
  memset(hexadecimalNumber, 0, bytesNeeded);
  quotient = decimalNumber;

  while(quotient!=0){
    temp = quotient % 16;

    if(temp < 10){
      temp = temp + 48;
    } else {
      temp = temp + 55;
    }

    hexadecimalNumber[i] = temp;
    quotient = quotient / 16;
    i--;
  }

  i = 0;

  while(i < bytesNeeded){
    if(hexadecimalNumber[i] != '\0'){
      break;
    }
    hexadecimalNumber[i] = '0';
    i++;
  }

  hex2binary(hexadecimalNumber, bytesNeeded, binaryNumber);
}
Пример #6
0
TEST(sml_value, parse_integer64_fewer_bytes) {
	hex2binary("58FFFFFFFFFFFF0F", sml_buf_get_current_buf(buf));
	sml_value *v = sml_value_parse(buf);

	TEST_ASSERT_EQUAL(-241, *(v->data.int64));
	TEST_ASSERT_EQUAL((SML_TYPE_INTEGER | SML_TYPE_NUMBER_64), v->type);
}
Пример #7
0
TEST(sml_value, parse_optional) {
	hex2binary("01", sml_buf_get_current_buf(buf));
	sml_value *v = sml_value_parse(buf);

	TEST_ASSERT_NULL(v);
	TEST_ASSERT_EQUAL(1, buf->cursor);
}
Пример #8
0
TEST(sml_sequence, parse_octet_string) {
	hex2binary("720648616C6C6F0648616C6C6F", sml_buf_get_current_buf(buf));

	sml_sequence *seq = sml_sequence_parse(buf, (void *) &sml_octet_string_parse, (void (*)(void *))&sml_octet_string_free);
	TEST_ASSERT_NOT_NULL(seq);
	TEST_ASSERT_EQUAL(2, seq->elems_len);
}
void init_hasChunks(char* has_chunk_file_name) {
    FILE* has_chunk_file = fopen(has_chunk_file_name,"r");

    int chunk_num = 0;
    char read_buffer[CHUNK_FILE_LINE_BUFFER_SIZE];
    while (fgets(read_buffer, CHUNK_FILE_LINE_BUFFER_SIZE, has_chunk_file)) {
        chunk_num++;
    }
    
    /* set the global variable*/
    has_chunks_number = chunk_num;
    has_chunks = malloc(sizeof(struct chunk_s) * chunk_num);

    memset(read_buffer, 0, CHUNK_FILE_LINE_BUFFER_SIZE);
    fseek(has_chunk_file, 0, SEEK_SET);

    int count = 0;
    char hash_buffer[SHA1_HASH_SIZE*2];
    while (fgets(read_buffer, CHUNK_FILE_LINE_BUFFER_SIZE, has_chunk_file)) {
        sscanf(read_buffer,"%d %s", &(has_chunks[count].id), hash_buffer);
        hex2binary(hash_buffer, SHA1_HASH_SIZE * 2, has_chunks[count].hash_binary);
        has_chunks[count].data = NULL;
        count++;
    } 
    fclose(has_chunk_file);

    printf("********CHUNKS I HAVE**********\n");
    int i = 0;
    for(i = 0; i < chunk_num; i++){
        print_chunk(&has_chunks[i]);
    }
    printf("*******END CHUNKS I HAVE********\n");
}
Пример #10
0
TEST(sml_status, parse_not_unsigned) {
	hex2binary("5001", sml_buf_get_current_buf(buf));
	sml_status *s = sml_status_parse(buf);

	TEST_ASSERT_NULL(s);
	TEST_ASSERT_TRUE(sml_buf_has_errors(buf));
}
Пример #11
0
char *get_hash_from_master_chunkfile(int offset, bt_config_t* config){
	char *hash = malloc(20);
	char *master_file = config->chunk_file;
	FILE *fp = fopen(master_file, "r");
	if( fp == NULL ){
		printf("Can not locate file %s\n", hash);
		return NULL;
	}

	char content_path[512];
	char temp[40];
	int index;
	/* read the content path first */
	fscanf(fp, "%s %s\n", temp, content_path);
	fscanf(fp, "%s\n", temp);
	memset(temp,0 , 40);
	while( fscanf(fp,"%d %s",&index, temp ) > 0){
		if( index == offset ){
			/* calculate the binary hash from the master chunk file */
			hex2binary((char*)temp, 40, (uint8_t *)(hash));
			return hash;
		}
	}
	free(hash);
	return NULL;
}
Пример #12
0
TEST(sml_status, parse_optional) {
	hex2binary("01", sml_buf_get_current_buf(buf));
	sml_status *s = sml_status_parse(buf);

	TEST_ASSERT_NULL(s);
	TEST_ASSERT_FALSE(sml_buf_has_errors(buf));
	TEST_ASSERT_EQUAL(1, buf->cursor);
}
Пример #13
0
TEST(sml_tree, parse_with_child) {
	hex2binary("730648616C6C6F0171730648616C6C6F0101", sml_buf_get_current_buf(buf));
	sml_tree *t = sml_tree_parse(buf);

	TEST_ASSERT_NOT_NULL(t);
	TEST_ASSERT_NOT_NULL(t->child_list[0]);
	TEST_ASSERT_EQUAL(1, t->child_list_len);
}
Пример #14
0
TEST(sml_tree, parse_with_error_child) {
	hex2binary("730648616C6C6F0171720648616C6C6F0101", sml_buf_get_current_buf(buf));
	sml_tree *t = sml_tree_parse(buf);

	TEST_ASSERT_NULL(t);

	sml_tree_free( t );
}
Пример #15
0
TEST(sml_value, parse_boolean) {
	hex2binary("4200", sml_buf_get_current_buf(buf));
	sml_value *v = sml_value_parse(buf);

	TEST_ASSERT_NOT_NULL(v);
	TEST_ASSERT_EQUAL(SML_TYPE_BOOLEAN, v->type);
	TEST_ASSERT_FALSE(*(v->data.boolean));
}
Пример #16
0
TEST(sml_status, parse_status8) {
	hex2binary("6201", sml_buf_get_current_buf(buf));
	sml_status *s = sml_status_parse(buf);

	TEST_ASSERT_NOT_NULL(s);
	TEST_ASSERT_EQUAL(1, *(s->data.status8));
	TEST_ASSERT_EQUAL((SML_TYPE_UNSIGNED | SML_TYPE_NUMBER_8), s->type);
}
Пример #17
0
TEST(sml_tree_path, parse) {
	hex2binary("720648616C6C6F0264", sml_buf_get_current_buf(buf));
	sml_tree_path *t = sml_tree_path_parse(buf);
	TEST_ASSERT_NOT_NULL(t);
	TEST_ASSERT_EQUAL(2, t->path_entries_len);
	TEST_ASSERT_EQUAL(0, sml_octet_string_cmp_with_hex(t->path_entries[0], "48616C6C6F"));
	TEST_ASSERT_EQUAL(0, sml_octet_string_cmp_with_hex(t->path_entries[1], "64"));
}
Пример #18
0
TEST(sml_value, parse_octet_string) {
	hex2binary("0648616C6C6F", sml_buf_get_current_buf(buf));
	sml_value *v = sml_value_parse(buf);

	TEST_ASSERT_NOT_NULL(v);
	TEST_ASSERT_EQUAL(SML_TYPE_OCTET_STRING, v->type);
	expected_octet_string(v->data.bytes, "Hallo", 5);
}
Пример #19
0
static int do_update_binary(int argc, char **argv)
{
	u8 buf[240];
	int r, err = 1, in_len;
	int offs;
	sc_path_t path;
	sc_file_t *file;
	char *in_str;
	
	if (argc < 2 || argc > 3)
		goto usage;
	if (arg_to_path(argv[0], &path, 0) != 0)
		goto usage;
	offs = strtol(argv[1],NULL,10);

	in_str = argv[2];
	printf("in: %i; %s\n", offs, in_str);
	if (*in_str=='\"')   {
		in_len = strlen(in_str)-2 >= sizeof(buf) ? sizeof(buf)-1 : strlen(in_str)-2;
		strncpy((char *) buf, in_str+1, in_len);
	} else {
		in_len = hex2binary(buf, sizeof(buf), in_str);
		if (!in_len) {
			printf("unable to parse hex value\n");
			return -1;
		}
	}
	
	r = sc_select_file(card, &path, &file);
	if (r) {
		check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file);
		return -1;
	}

	if (file->ef_structure != SC_FILE_EF_TRANSPARENT)   {
		printf("EF structure should be SC_FILE_EF_TRANSPARENT\n");
		goto err;
	}
	
	r = sc_update_binary(card, offs, buf, in_len, 0);
	if (r < 0) {
		printf("Cannot update %04X; return %i\n", file->id, r);
		goto err;
	}

	printf("Total of %d bytes written to %04X at %i offset.\n", 
	       r, file->id, offs);

	err = 0;

err:
	sc_file_free(file);
	select_current_path_or_die();
	return -err;
usage:
	printf("Usage: update <file id> offs <hex value> | <'\"' enclosed string>\n");
	return -1;
}
Пример #20
0
char* get_data_from_hash(char *hash , bt_config_t* config){
	/*	read the master data file and return the 512KB data for a chunk
		Two steps:
			(1) calculate the offset in the master chunk file based on hashvalue
			(2)	read the data form master data file based on the offset
	*/
	char *data = malloc(512 * 1024);
	char temp[40];
	int index;
	char *master_file = config->chunk_file;
	FILE *fp = fopen(master_file, "r");
	if( fp == NULL ){
		printf("Can not locate file %s\n", hash);
		return NULL;
	}
	char content_path[512];
	/* read the content path first */
	fscanf(fp, "%s %s\n", temp, content_path);
	fscanf(fp, "%s\n", temp);
	memset(temp,0 , 40);

	printf("DEBUG path = %s\n",content_path);
	int find = -1;
	while( find != 1 && fscanf(fp,"%d %s",&index, temp ) > 0){
		char local[20];
		/* calculate the binary hash from the master chunk file */
		hex2binary((char*)temp, 40, (uint8_t *)(local));

		int cmp = 1;
		int i;
		/* and compare if there is a match of hash values */
		for( i = 0;i < 20;i ++){
			if( local[i] != hash[i] ){
				cmp = 0;
				break;
			}
		}

		if( cmp == 1){
			printf("find local content for has = %s\n", local);
			find = 1;
		}

		memset(temp, 0, 40);
		memset(local, 0 ,20);
	}
	printf("in Master file offset = %d\n", index);

	/* try rb instead of r*/
	FILE *content = fopen(content_path, "rb");  
	fseek(content, 512 * 1024 * index, SEEK_SET );
	fread(data,512 * 1024, 1, content);

	fclose(content);
	fclose(fp);

	return data;
}
Пример #21
0
TEST(sml_list, parse_two_entries) {
	hex2binary("727702610101010142000177026101010101420001",  sml_buf_get_current_buf(buf));
	sml_list *l = sml_list_parse(buf);

	TEST_ASSERT_FALSE(sml_buf_has_errors(buf));
	TEST_ASSERT_NOT_NULL(l);
	TEST_ASSERT_NOT_NULL(l->next);
	TEST_ASSERT_EQUAL(0, sml_octet_string_cmp_with_hex(l->obj_name, "61"));
}
TEST(sml_get_profile_pack_request, parse) {
	hex2binary("7901010101010101730648616C6C6F0648616C6C6F0648616C6C6F01", sml_buf_get_current_buf(buf));
	sml_get_profile_pack_request *r = sml_get_profile_pack_request_parse(buf);
	TEST_ASSERT_NOT_NULL(r);
	TEST_ASSERT_NOT_NULL(r->object_list);
	TEST_ASSERT_NOT_NULL(r->object_list->next);
	TEST_ASSERT_NOT_NULL(r->object_list->next->next);
	TEST_ASSERT_NULL(r->object_list->next->next->next);
	sml_get_profile_pack_request_free( r );
}
Пример #23
0
TEST(sml_value, parse_unsigned32) {
	hex2binary("6500000001", sml_buf_get_current_buf(buf));
	sml_value *v = sml_value_parse(buf);

	TEST_ASSERT_NOT_NULL(v);
	TEST_ASSERT_EQUAL(1, *(v->data.uint32));
	TEST_ASSERT_EQUAL((SML_TYPE_UNSIGNED | SML_TYPE_NUMBER_32), v->type);
	TEST_ASSERT_EQUAL(5, buf->cursor);

}
Пример #24
0
static int arg_to_path(const char *arg, sc_path_t *path, int is_id)
{
	memset(path, 0, sizeof(sc_path_t));

	if (strncasecmp(arg, "aid:", strlen("aid:")) == 0) {
		/* DF aid */
		const char *p = arg + strlen("aid:");
		path->len  = hex2binary(path->value, sizeof(path->value), p);
		path->type = SC_PATH_TYPE_DF_NAME;
	} else {
		/* file id */
		unsigned int buf[2];
		u8 cbuf[2];
	
		if (strlen(arg) != 4) {
			printf("Wrong ID length.\n");
			return -1;
		}
		if (sscanf(arg, "%02X%02X", &buf[0], &buf[1]) != 2) {
			printf("Invalid ID.\n");
			return -1;
		}
		cbuf[0] = buf[0];
		cbuf[1] = buf[1];
		if ((cbuf[0] == 0x3F && cbuf[1] == 0x00) || is_id) {
			path->len = 2;
			memcpy(path->value, cbuf, 2);
			if (is_id)
				path->type = SC_PATH_TYPE_FILE_ID;
			else
				path->type = SC_PATH_TYPE_PATH;
		} else {
			*path = current_path;
			if (path->type == SC_PATH_TYPE_DF_NAME)   {
				if (path->len > sizeof(path->aid.value))   {
					printf("Invalid length of DF_NAME path\n");
					return -1;
				}

				memcpy(path->aid.value, path->value, path->len);
				path->aid.len = path->len;

				path->type = SC_PATH_TYPE_FILE_ID;
				path->len = 0;
			}
			sc_append_path_id(path, cbuf, 2);
		}
	}

	return 0;	
}
Пример #25
0
/**
 * Convert string into a bitset. Inverse of bitset_to_str().
 *
 */
bitset *str_to_bitset(char *str, char **end)
{
	int			nbits = 0;
	int			bytes;
	int			n;
	int			pos;
	int			b;
        int                     len;
	bitset                  *bp;
        char                    dst[1024];

        if (!str)
                return NULL;

        /* hex string has 0x prefix */
        if (str[0] == '0' && str[1] == 'x')
                str = str + 2;

        len = strlen(str);
        if (len % 2) {
                nbits = (len + 1) << 2;
                bytes = NUM_BYTES(nbits);
                pos = (bytes << 3) - 5;
        } else {
                nbits = len << 2;
                bytes = NUM_BYTES(nbits);
                pos = (bytes << 3) - 1;
        }

        if (0)
                hex2binary(str, dst);

	bp = bitset_new(nbits);

	for (; *str != '\0' && isxdigit(*str) && pos >= 0; str++) {
		b = digittoint(*str);
		for (n = 3; n >= 0; n--, pos--) {
			if (b & (1 << n)) {
				bitset_set(bp, pos);
			}
		}
	}

	if (end != NULL)
                *end = str - 1;

	return bp;
}
Пример #26
0
int get_off_set_from_master_chunkfile(char *hash, bt_config_t* config){
	char *master_file = config->chunk_file;
	FILE *fp = fopen(master_file, "r");
	if( fp == NULL ){
		printf("Can not locate file %s\n", hash);
		return -1;
	}
	char content_path[512];
	char temp[40];
	int index;
	/* read the content path first */
	fscanf(fp, "%s %s\n", temp, content_path);
	fscanf(fp, "%s\n", temp);
	memset(temp,0 , 40);

	printf("DEBUG path = %s\n",content_path);
	int find = -1;
	while( find != 1 && fscanf(fp,"%d %s",&index, temp ) > 0){
		char local[20];
		/* calculate the binary hash from the master chunk file */
		hex2binary((char*)temp, 40, (uint8_t *)(local));

		int cmp = 1;
		int i;
		/* and compare if there is a match of hash values */
		for( i = 0;i < 20;i ++){
			if( local[i] != hash[i] ){
				cmp = 0;
				break;
			}
		}

		if( cmp == 1){
			printf("find local content for has = %s offset = %d\n", local, index);
			return index;
		}
	}
	return -1;
}
Пример #27
0
TEST(sml_number, parse_unsigned32_fewer_bytes) {
	hex2binary("64010001", sml_buf_get_current_buf(buf));
	u32 *n = sml_u32_parse(buf);
	TEST_ASSERT_EQUAL(65537, *n);
	sml_u32_free( n );
}
Пример #28
0
void decimal2binary(int d, string binary) {
  char hex[100];
  sprintf(hex, "%04X", d);
  hex2binary(hex, binary);
}
Пример #29
0
rcb_t *load_get_chunks(const char *get_chunk_file, int *cnt)
{
    FILE *fp = NULL;
    char buff[GET_LINE_LEN + 1];
    uint8_t hash[SHA1_HASH_SIZE]; //original hash, not hash string
    uint32_t wr_chk_id;
    chunk_map_t *map;
    int req_cnt = 0;
    
    rcb_t *head = NULL;
    rcb_t *p = NULL;

    fp = fopen(get_chunk_file, "r");
    if(!fp)
    {
        return NULL;
    }

    // load the chunks
    while(NULL != fgets(buff, GET_LINE_LEN + 1, fp))
    {
        char *str;

        p = create_rcb();
        if(NULL == p)
            goto err;

        str = strtok(buff, " \n");
        if(NULL == str)
           goto err;
        if(0 != str_to_uint32(buff, &wr_chk_id))
           goto err; 
        p->wr_chk_id = wr_chk_id;

        str = strtok(NULL, " \n");
        if(NULL == str)
           goto err;
        if(strlen(str) != SHA1_HASH_SIZE * 2)
            goto err; 
        hex2binary(str, SHA1_HASH_SIZE * 2, hash);

        HT_FIND(&g_chunkmap_ht, chunk_map_t, hash, hash, map);
        if(!map)
        {
            free(p);
        }
        else
        {
            p->chk_map = map;
            p->next = head;
            head = p;
            req_cnt++;
        }
    }
    fclose(fp);
    *cnt = req_cnt;
    return head;
    
err:
    if(fp)
        fclose(fp);

    if(p)
        free(p);
    
    *cnt = req_cnt;

    return head;
}
Пример #30
0
TEST(sml_number, parse_unsigned64_fewer_bytes) {
	hex2binary("67000000000001", sml_buf_get_current_buf(buf));
	u64 *n = sml_u64_parse(buf);
	TEST_ASSERT_EQUAL(1, *n);
	sml_u64_free( n );
}