boolean vm_hashsum(const char* name, char* digest, vm_che_type type, VMUINT type_size) {
	VMWCHAR src[OTA_MAX_PATH_LEN];
	VMFILE handle;
	VMUINT size, read, done;
	vm_stche che_ctx;
	
	uint8_t buffer[DIGEST_SIZE_BUFFER];
	uint8_t che_hash[DIGEST_SIZE_MAX];
	boolean result = true;

	LOTAUPDATE_DEBUG("vm_md5sum - checking file %s\r\n", name);
	if (vm_ascii_to_ucs2(src, OTA_MAX_PATH_LEN*sizeof(VMWCHAR), (VMSTR) name) < 0) {
		LOTAUPDATE_DEBUG("vm_md5sum - error converting path to VMWCHAR\r\n");
		return false;
	}
	if((handle = vm_file_open(src, MODE_READ, true)) < 0) {
		LOTAUPDATE_DEBUG("vm_md5sum - error opening file\r\n");
		return false;
	}
	if(vm_file_getfilesize(handle, &size) != 0) {
		LOTAUPDATE_DEBUG("vm_md5sum - error getting file size");
		result = false;
		goto vm_md5sum_cleanup;
	}

	// initialize the che context and hash
	memset(che_hash, 0, type_size);
	vm_che_init(&che_ctx, type);
	done = 0;
	while(done < size) {
		vm_file_read(handle, buffer, DIGEST_SIZE_BUFFER, &read);
		done += read;
		vm_che_process(&che_ctx, type, VM_CHE_MODE_NULL, VM_CHE_HASH, buffer, che_hash, read, done == size);
	}

	for(int i=0; i<type_size; i++) {
		sprintf(&digest[i*2], "%02x", che_hash[i]);
	}
	digest[2*type_size] = 0;
	
	result = true;
	vm_che_deinit(&che_ctx);
	
vm_md5sum_cleanup:
	vm_file_close(handle);
	return result;
}
Пример #2
0
/*****************************************************************************
 * FUNCTION
 *  get_file_size
 * DESCRIPTION
 *  Gets the file specified size. 
 * PARAMETERS
 *	f_name			[IN]	VMSTR		Complete file name of the file	
 * RETURNS
 *	file_size		[OUT]	VMINT		The file's size in int
*****************************************************************************/
VMINT get_file_size(VMSTR f_name){
		VMFILE  file_handle; 
		//VMCHAR  f_name[MRE_STR_SIZE_MAX + 1] ;
		VMWCHAR f_wname[MRE_STR_SIZE_MAX + 1];
		VMCHAR  show_data[MRE_STR_SIZE_MAX + 1 + 10];
		VMWCHAR default_ucs[MRE_STR_SIZE_MAX + 1];
		VMCHAR show_text[MRE_STR_SIZE_MAX + 1];
		VMUINT nread;
		VMINT file_size;
		vm_log_debug ("Entering get_file_size function ");
		//sprintf (f_name, "%c:\\%s", mre_get_drv(), file_name);
		vm_log_debug("file name : %s", f_name);

		/* string format conversion */
		vm_ascii_to_ucs2 (f_wname, MRE_STR_SIZE_MAX, f_name);

		file_handle = vm_file_open(f_wname, MODE_WRITE, TRUE); //Mode as Binary cuz image, right?
		vm_file_getfilesize(file_handle, &file_size);
		vm_file_close(file_handle); //Don't need the file no more. Closing it.
		return file_size;
}