Exemplo n.º 1
0
// Read a Section of a DataVault from Disk
static eEsifError DataVault_GetFromSource(
	DataVaultPtr self,
	void *bufPtr,
	UInt32 bytes,
	size_t offset
	)
{
	eEsifError rc = ESIF_OK;
	size_t bytesRead = 0;

	ESIF_ASSERT(self != NULL);

	if (IOStream_Open(self->stream) != 0) {
		rc = ESIF_E_IO_ERROR;
		goto exit;
	}

	bytesRead = IOStream_ReadAt(self->stream, bufPtr, bytes, offset);
	if (bytesRead != bytes) {
		rc = ESIF_E_IO_ERROR;
	}

exit:
	IOStream_Close(self->stream);
	return rc;
}
Exemplo n.º 2
0
// Read DataVault from Disk
eEsifError DataVault_ReadVault(DataVaultPtr self)
{
	eEsifError rc = ESIF_OK;
	IOStreamPtr memStreamPtr = NULL;
	IOStreamPtr orgStreamPtr = NULL;
	DataVaultHeader header = {0};
	UInt32 hdrSize = 0;
	UInt32 min_version = 0;
	UInt32 max_version = 0;

	orgStreamPtr = self->stream;

	// Read the file into a memory stream for faster accesses (hash, size, etc.)
	rc = IOStream_CloneAsMemoryStream(self->stream, &memStreamPtr);
	if (rc != ESIF_OK) {
		goto exit;
	}

	self->stream = memStreamPtr;

	// Read in the header
	rc = DataVault_ReadHeader(self, &header);
	if (rc != ESIF_OK) {
		goto exit;
	}

	hdrSize = header.headersize;
	if (hdrSize < sizeof(header)) {
		rc = ESIF_E_NOT_SUPPORTED;
		goto exit;
	}

	// Verify signature
	if (memcmp(header.signature, ESIFDV_SIGNATURE, sizeof(header.signature)) != 0) {
		rc = ESIF_E_NOT_SUPPORTED;
		goto exit;
	}

	// The DataVault's Major.Minor Version must be <= the App's Major.Minor Version and at least Major.1
	min_version = ESIFDV_VERSION(ESIFDV_MAJOR_VERSION_MIN, 0, 0);
	max_version = ESIFDV_VERSION(ESIFDV_MAJOR_VERSION, ESIFDV_MINOR_VERSION, ESIFDV_MAX_REVISION);
	if ((header.version > max_version) || (header.version < min_version)) {
		rc = ESIF_E_NOT_SUPPORTED;
		goto exit;
	}

	// Save the header flags and version
	self->flags = header.flags;
	self->version = header.version;

	rc = DataVault_ReadPayload(self);

exit:
	IOStream_Close(self->stream);
	self->stream = orgStreamPtr; // Restore the original stream
	//esif_ccb_free(dataHdrsPtr);
	IOStream_Destroy(memStreamPtr);
	return rc;
}
Exemplo n.º 3
0
// Read the data vault header
static eEsifError DataVault_ReadHeader(
	DataVaultPtr self,
	DataVaultHeaderPtr headerPtr
	)
{
	eEsifError rc = ESIF_OK;
	size_t bytesRead = 0;

	ESIF_ASSERT(self != NULL);
	ESIF_ASSERT(headerPtr != NULL);

	if (IOStream_Open(self->stream) != 0) {
		goto exit;
	}

	bytesRead = IOStream_ReadAt(self->stream, headerPtr, sizeof(*headerPtr), 0);
	if (bytesRead !=  sizeof(*headerPtr)) {
		rc = ESIF_E_IO_ERROR;
	}
exit:
	IOStream_Close(self->stream);
	return rc;
}
Exemplo n.º 4
0
// Read a Section of a DataVault from Disk
static eEsifError DataVault_ReadBlock (
	DataVaultPtr self,
	void *buf_ptr,
	UInt32 buf_len,
	size_t offset
	)
{
	eEsifError rc     = ESIF_E_NOT_FOUND;	// TODO
	IOStreamPtr vault = self->stream;

	// Open DB File or Memory Block
	if (IOStream_Open(vault) != 0) {
		return rc;
	}

	// Seek and Read Buffer
	if (IOStream_Seek(vault, (long)offset, SEEK_SET) == 0 && IOStream_Read(vault, buf_ptr, buf_len) == buf_len) {
		rc = ESIF_OK;
	}

	IOStream_Close(vault);
	return rc;
}
Exemplo n.º 5
0
// Send DSP
enum esif_rc esif_send_dsp(
    char *filename,
    u8 dstId
)
{
    enum esif_rc rc = ESIF_OK;
    int edpSize = 512;
    int cmdSize = 0;
    struct esif_ipc *ipcPtr = NULL;
    struct esif_ipc_command *commandPtr = NULL;
    struct esif_command_send_dsp *dspCommandPtr = NULL;
    struct edp_dir edpDir;
    size_t bytesRead;
    char *edpName        = 0;
    IOStreamPtr ioPtr    = IOStream_Create();
    EsifDataPtr nameSpace = 0;
    EsifDataPtr key       = 0;
    EsifDataPtr value     = 0;

    if (ioPtr == NULL) {
        ESIF_TRACE_ERROR("Fail to create IOStream\n");
        rc = ESIF_E_NO_MEMORY;
        goto exit;
    }

    //
    // If we have a filename provided use the contents of the file as the CPC
    // note this is opaque it is up to the receiver to verify that this is in fact
    // a CPC.
    //
    if (NULL == filename) {
        ESIF_TRACE_ERROR("Filename is null\n");
        rc = ESIF_E_PARAMETER_IS_NULL;
        goto exit;
    }

    // Use name portion of filename for the DataVault key (C:\path\file.edp = file.edp)
    edpName  = strrchr(filename, *ESIF_PATH_SEP);
    edpName  = (edpName ? ++edpName : filename);
    nameSpace = EsifData_CreateAs(ESIF_DATA_STRING, ESIF_DSP_NAMESPACE, 0, ESIFAUTOLEN);
    key       = EsifData_CreateAs(ESIF_DATA_STRING, edpName, 0, ESIFAUTOLEN);
    value     = EsifData_CreateAs(ESIF_DATA_AUTO, NULL, ESIF_DATA_ALLOCATE, 0);

    if (nameSpace == NULL || key == NULL || value == NULL) {
        rc = ESIF_E_NO_MEMORY;
        goto exit;
    }

    // Look for EDP file on disk first then DataVault (static or file)
    if (!esif_ccb_file_exists(filename) && EsifConfigGet(nameSpace, key, value) == ESIF_OK) {
        filename = edpName;
        IOStream_SetMemory(ioPtr, (BytePtr)value->buf_ptr, value->data_len);
    } else {
        IOStream_SetFile(ioPtr, filename, (char *)"rb");
    }

    /* FIND CPC within EDP file */
    if (IOStream_Open(ioPtr) != 0) {
        ESIF_TRACE_ERROR("File not found (%s)\n", filename);
        rc = ESIF_E_IO_OPEN_FAILED;
        goto exit;
    }
    bytesRead  = IOStream_Read(ioPtr, &edpDir, sizeof(struct edp_dir));
    if (!esif_verify_edp(&edpDir, bytesRead)) {
        ESIF_TRACE_ERROR("Invalid EDP Header: Signature=%4.4s Version=%d\n", (char *)&edpDir.signature, edpDir.version);
        rc = ESIF_E_NOT_SUPPORTED;
        goto exit;
    }
    edpSize = edpDir.fpc_offset - edpDir.cpc_offset;
    IOStream_Seek(ioPtr, edpDir.cpc_offset, SEEK_SET);

    if (edpSize > MAX_EDP_SIZE) {
        ESIF_TRACE_ERROR("The edp size %d is larger than maximum edp size\n", edpSize);
        rc = -ESIF_E_UNSPECIFIED;
        goto exit;
    }

    cmdSize = edpSize + sizeof(*dspCommandPtr);

    ipcPtr = esif_ipc_alloc_command(&commandPtr, cmdSize);
    if (NULL == ipcPtr || NULL == commandPtr) {
        ESIF_TRACE_ERROR("Fail to allocate esif_ipc/esif_ipc_command\n");
        rc = ESIF_E_NO_MEMORY;
        goto exit;
    }

    commandPtr->type = ESIF_COMMAND_TYPE_SEND_DSP;
    commandPtr->req_data_type = ESIF_DATA_STRUCTURE;
    commandPtr->req_data_offset = 0;
    commandPtr->req_data_len = cmdSize;
    commandPtr->rsp_data_type = ESIF_DATA_VOID;
    commandPtr->rsp_data_offset = 0;
    commandPtr->rsp_data_len = 0;

    dspCommandPtr = (struct esif_command_send_dsp *)(commandPtr + 1);
    dspCommandPtr->id = dstId;
    dspCommandPtr->data_len = edpSize;

    bytesRead = IOStream_Read(ioPtr, dspCommandPtr + 1, edpSize);
    ESIF_TRACE_DEBUG("loaded file %s bytes %d\n", filename, (int)bytesRead);

    ESIF_TRACE_INFO("CPC file %s(%d) sent to participant %d\n", filename, edpSize, dstId);

    ipc_execute(ipcPtr);

    if (ESIF_OK != ipcPtr->return_code) {
        ESIF_TRACE_ERROR("ipc error code = %s(%d)\n", esif_rc_str(ipcPtr->return_code), ipcPtr->return_code);
        rc = ipcPtr->return_code;
        goto exit;
    }

    rc = commandPtr->return_code;
    if ((rc != ESIF_OK) && (rc != ESIF_E_DSP_ALREADY_LOADED)) {
        ESIF_TRACE_ERROR("primitive error code = %s(%d)\n", esif_rc_str(commandPtr->return_code), commandPtr->return_code);
        goto exit;
    }
exit:
    if (NULL != ipcPtr) {
        esif_ipc_free(ipcPtr);
    }
    if (NULL != ioPtr) {
        IOStream_Close(ioPtr);
        IOStream_Destroy(ioPtr);
    }
    EsifData_Destroy(nameSpace);
    EsifData_Destroy(key);
    EsifData_Destroy(value);
    return rc;
}
Exemplo n.º 6
0
static eEsifError DataVault_ReadPayload(DataVaultPtr self)
{
	eEsifError rc = ESIF_OK;
	IOStreamPtr streamPtr = NULL;
	int vrc = EPERM;
	size_t fileOffset = 0;
	size_t curFileOffset = 0;
	size_t pairSize = 0;
	esif_flags_t item_flags = 0;
	EsifData key = { ESIF_DATA_STRING };
	EsifData value = { ESIF_DATA_VOID };

	ESIF_ASSERT(self != NULL);

	streamPtr = self->stream;

	// Open File or Memory Block
	if ((vrc = IOStream_Open(streamPtr)) != 0) {
		if (vrc == ENOENT) {
			rc = ESIF_E_NOT_FOUND;
		}
		else {
			rc = ESIF_E_IO_OPEN_FAILED;
		}
		goto exit;
	}

	// Move the file pointer to start of data
	fileOffset = sizeof(DataVaultHeader);
	if (IOStream_Seek(streamPtr, fileOffset, SEEK_SET) != EOK) {
		rc = ESIF_E_IO_ERROR;
		goto exit;
	}

	// Read Data and add to DataVault
	while (rc == ESIF_OK) {
		EsifData_ctor(&key);
		EsifData_ctor(&value);

		item_flags = 0;

		rc = DataVault_ReadNextKeyValuePair(self, &item_flags, &key, &value);
		if (rc != ESIF_OK) {
			if (ESIF_E_ITERATION_DONE == rc) {
				rc = ESIF_OK;
			}
			break;
		}

		// Validate that the data read is from within the region specified by the header
		curFileOffset = IOStream_GetOffset(streamPtr);
		pairSize = curFileOffset - fileOffset;

		fileOffset = curFileOffset;

		// Add value (including allocated buf_ptr) to cache
		DataCache_InsertValue(self->cache, (esif_string)key.buf_ptr, &value, item_flags);

		EsifData_dtor(&key);
		EsifData_dtor(&value);
	}
exit:
	IOStream_Close(streamPtr);
	EsifData_dtor(&key);
	EsifData_dtor(&value);
	return rc;
}
Exemplo n.º 7
0
// Read DataVault from Disk
eEsifError DataVault_ReadVault (DataVaultPtr self)
{
	eEsifError rc = ESIF_E_UNSPECIFIED;	// TODO
	int vrc = EPERM;
	DataVaultHeader header;
	UInt32 min_version;
	UInt32 max_version;
	esif_flags_t item_flags;
	IOStreamPtr vault = self->stream;

	// TODO: Locking

	// Open File or Memory Block
	if ((vrc = IOStream_Open(vault)) != 0) {
		if (vrc == ENOENT) {
			rc = ESIF_E_NOT_FOUND;
		}
		return rc;
	}

	// Read and Verify File Header
	// The DataVault's Major.Minor Version must be <= the App's Major.Minor Version and at least Major.0
	memset(&header, 0, sizeof(header));
	IOStream_Read(vault, &header, 4);	// E51F#### where #### is header size, including this
	IOStream_Read(vault, ((UInt8*)&header) + 4, esif_ccb_min(header.headersize, sizeof(header)) - 4);
	min_version = ESIFDV_VERSION(ESIFDV_MAJOR_VERSION, 0, 0);
	max_version = ESIFDV_VERSION(ESIFDV_MAJOR_VERSION, ESIFDV_MINOR_VERSION, ESIFDV_MAX_REVISION);
	if (memcmp(header.signature, ESIFDV_SIGNATURE, sizeof(header.signature)) != 0 || header.version > max_version || header.version < min_version) {
		IOStream_Close(vault);
		return ESIF_E_NOT_SUPPORTED;
	}
	if (header.headersize > sizeof(header)) {
		IOStream_Seek(vault, header.headersize - sizeof(header), SEEK_CUR);
	}
	self->flags = header.flags;
	if (IOStream_GetType(vault) == StreamMemory) {
		self->flags |= ESIF_SERVICE_CONFIG_STATIC;
	}
	rc = ESIF_OK;

	// Read Data and add to DataVault
	while (IOStream_Read(vault, &item_flags, sizeof(item_flags)) == sizeof(item_flags)) {
		EsifData key;
		EsifData value;

		// Read Key
		EsifData_ctor(&key);
		EsifData_ctor(&value);
		key.type = ESIF_DATA_STRING;
		IOStream_Read(vault, &key.data_len, sizeof(key.data_len));
		if (key.data_len > MAX_DV_DATALEN) {
			rc = ESIF_E_PARAMETER_IS_OUT_OF_BOUNDS;
			break;
		}
		switch (IOStream_GetType(vault)) {
		case StreamMemory:
			key.buf_len = 0;
			key.buf_ptr = IOStream_GetMemoryBuffer(vault) + IOStream_GetOffset(vault);
			IOStream_Seek(vault, key.data_len, SEEK_CUR);
			item_flags &= ~ESIF_SERVICE_CONFIG_NOCACHE;	// ignore for Memory Vaults
			break;

		case StreamFile:
		default:
			key.buf_len = key.data_len;
			key.buf_ptr = esif_ccb_malloc(key.buf_len);
			if (!key.buf_ptr) {
				rc = ESIF_E_NO_MEMORY;
				break;
			}
			IOStream_Read(vault, key.buf_ptr, key.data_len);
			((esif_string)(key.buf_ptr))[key.data_len - 1] = 0;
			break;
		}
		if (rc != ESIF_OK) {
			EsifData_dtor(&key);
			break;
		}

		// Read Value
		IOStream_Read(vault, &value.type, sizeof(value.type));
		IOStream_Read(vault, &value.data_len, sizeof(value.data_len));
		if (value.data_len > MAX_DV_DATALEN) {
			EsifData_dtor(&key);
			break;
		}

		// If NOCACHE mode, use buf_ptr to store the file offset of the data and skip the file
		if (item_flags & ESIF_SERVICE_CONFIG_NOCACHE) {
			size_t offset = IOStream_GetOffset(vault);
			IOStream_Seek(vault, value.data_len, SEEK_CUR);
			value.buf_ptr = (void*)offset;
			value.buf_len = 0;	// data_len = original length
		} else {
			// Use static pointer for static data vaults (unless encrypted), otherwise make a dynamic copy
			if (IOStream_GetType(vault) == StreamMemory && !(item_flags & ESIF_SERVICE_CONFIG_ENCRYPT)) {
				value.buf_len = 0;	// static
				value.buf_ptr = IOStream_GetMemoryBuffer(vault) + IOStream_GetOffset(vault);
				IOStream_Seek(vault, value.data_len, SEEK_CUR);
			} else {
				value.buf_len = value.data_len;	// dynamic
				value.buf_ptr = esif_ccb_malloc(value.buf_len);
				if (!value.buf_ptr) {
					EsifData_dtor(&key);
					rc = ESIF_E_NO_MEMORY;
					break;
				}
				IOStream_Read(vault, value.buf_ptr, value.data_len);
			}
			// Decrypt Encrypted Data?
			if (item_flags & ESIF_SERVICE_CONFIG_ENCRYPT) {
				UInt32 byte;
				for (byte = 0; byte < value.data_len; byte++)
					((UInt8*)(value.buf_ptr))[byte] = ~((UInt8*)(value.buf_ptr))[byte];
			}
		}
		// Add value (including allocated buf_ptr) to cache
		DataCache_SetValue(self->cache, (esif_string)key.buf_ptr, value, item_flags);
		value.buf_ptr = 0;
		item_flags    = 0;
		EsifData_dtor(&key);
		EsifData_dtor(&value);
	}
	IOStream_Close(vault);
	return rc;
}
Exemplo n.º 8
0
// Write DataVault to Disk
eEsifError DataVault_WriteVault (DataVaultPtr self)
{
	eEsifError rc = ESIF_E_NOT_FOUND;
	DataVaultHeader header;
	struct esif_ccb_file dv_file    = {0};
	struct esif_ccb_file dv_filebak = {0};
	IOStreamPtr vault    = 0;
	IOStreamPtr vaultBak = 0;
	u32 idx;

	if (FLAGS_TEST(self->flags, ESIF_SERVICE_CONFIG_STATIC | ESIF_SERVICE_CONFIG_READONLY)) {
		return ESIF_E_READONLY;
	}

	// TODO: Locking
	vault = IOStream_Create();
	if (!vault) {
		return ESIF_E_NO_MEMORY;
	}

	// If any rows contain NOCACHE PERSIST values, we need to make a copy the original DataVault while creating the new one
	// esif_ccb_sprintf(MAX_PATH, dv_file.filename, "%s%s", esif_build_path(dv_file.filename, MAX_PATH, NULL, self->name), ESIFDV_FILEEXT);
	esif_ccb_sprintf(MAX_PATH, dv_file.filename, "%s%s%s", ESIFDV_DIR, self->name, ESIFDV_FILEEXT);

	for (idx = 0; idx < self->cache->size; idx++)
		if (FLAGS_TESTALL(self->cache->elements[idx].flags, ESIF_SERVICE_CONFIG_NOCACHE | ESIF_SERVICE_CONFIG_PERSIST) &&
			self->cache->elements[idx].value.buf_len == 0) {
			struct stat filebak_stat = {0};
            esif_ccb_sprintf(MAX_PATH, dv_filebak.filename, "%s%s%s", ESIFDV_DIR, self->name, ESIFDV_BAKFILEEXT);

			// Delete BAK file if it exists
			if (esif_ccb_stat(dv_filebak.filename, &filebak_stat) == 0) {
				esif_ccb_unlink(dv_filebak.filename);
			}
			if (esif_ccb_rename(dv_file.filename, dv_filebak.filename) == 0) {
				if ((vaultBak = IOStream_Create()) == NULL) {
					rc = ESIF_E_NO_MEMORY;
				}
				if (!vaultBak || IOStream_OpenFile(vaultBak, dv_filebak.filename, "rb") != 0) {
					IOStream_Destroy(vault);
					IOStream_Destroy(vaultBak);
					return rc;
				}
			}
			break;
		}

	// Create DataVault, Overwrite if necessary
	IOStream_SetFile(vault, self->stream->file.name, "wb");
	if (IOStream_Open(vault) != 0) {
		if (vaultBak) {
			IOStream_Destroy(vaultBak);
			esif_ccb_unlink(dv_filebak.filename);
		}
		IOStream_Destroy(vault);
		return rc;
	}

	// Create File Header
	memset(&header, 0, sizeof(header));
	esif_ccb_memcpy(&header.signature, ESIFDV_SIGNATURE, sizeof(header.signature));
	header.headersize = sizeof(header);
	header.version    = ESIFDV_VERSION(ESIFDV_MAJOR_VERSION, ESIFDV_MINOR_VERSION, ESIFDV_REVISION);
	header.flags = 0;	// TODO: get from self->flags

	// Write File Header
	IOStream_Seek(vault, 0, SEEK_SET);
	IOStream_Write(vault, &header, sizeof(header));
	rc = ESIF_OK;

	// Write All Persisted Rows from Sorted List to DataVault
	for (idx = 0; idx < self->cache->size; idx++) {
		DataCacheEntryPtr keypair = &self->cache->elements[idx];
		if (keypair->flags & ESIF_SERVICE_CONFIG_PERSIST) {
			UInt8 *buffer     = 0;
			UInt32 buffer_len = 0;
			UInt32 byte = 0;

			IOStream_Write(vault, &keypair->flags, sizeof(keypair->flags));
			IOStream_Write(vault, &keypair->key.data_len, sizeof(keypair->key.data_len));
			IOStream_Write(vault, keypair->key.buf_ptr, keypair->key.data_len);

			IOStream_Write(vault, &keypair->value.type, sizeof(keypair->value.type));
			IOStream_Write(vault, &keypair->value.data_len, sizeof(keypair->value.data_len));

			// Read NOCACHE Entries from Backup file
			if (keypair->flags & ESIF_SERVICE_CONFIG_NOCACHE) {
				size_t offset = IOStream_GetOffset(vault);

				// Read Block from BAK file
				if (keypair->value.buf_len == 0) {
					size_t bakoffset = (size_t)keypair->value.buf_ptr;
					buffer     = (UInt8*)esif_ccb_malloc(keypair->value.data_len);
					buffer_len = keypair->value.data_len;
					if (!buffer) {
						rc = ESIF_E_NO_MEMORY;
						break;
					}
					if (IOStream_Seek(vaultBak, bakoffset, SEEK_SET) != 0 || IOStream_Read(vaultBak, buffer, buffer_len) != buffer_len) {
						esif_ccb_free(buffer);
						rc = ESIF_E_UNSPECIFIED;// TODO: ESIF_E_IOERROR;
						break;
					}
					keypair->value.buf_ptr = (void*)offset;
				}
				// Convert internal storage to NOCACHE
				else {
					buffer     = (UInt8*)keypair->value.buf_ptr;
					buffer_len = keypair->value.data_len;
					keypair->value.buf_ptr = (void*)offset;
					keypair->value.buf_len = 0;
				}
			}

			// Encrypt Data?
			if (keypair->flags & ESIF_SERVICE_CONFIG_ENCRYPT) {
				if (!buffer) {
					buffer     = (UInt8*)esif_ccb_malloc(keypair->value.data_len);
					buffer_len = keypair->value.data_len;
					if (!buffer) {
						rc = ESIF_E_NO_MEMORY;
						break;
					}
				}
				for (byte = 0; byte < keypair->value.data_len; byte++)
					buffer[byte] = ~((UInt8*)(keypair->value.buf_ptr))[byte];
			}

			if (buffer) {
				IOStream_Write(vault, buffer, buffer_len);
				esif_ccb_free(buffer);
			} else {
				IOStream_Write(vault, keypair->value.buf_ptr, keypair->value.data_len);
			}
		}
	}

	// Rollback on Error
	if (rc != ESIF_OK) {
		IOStream_Destroy(vaultBak);
		IOStream_Destroy(vault);
		esif_ccb_unlink(dv_file.filename);
		IGNORE_RESULT(esif_ccb_rename(dv_filebak.filename, dv_file.filename));
		return rc;
	}
	// Remove BAK file and Commit
	if (vaultBak) {
		IOStream_Close(vaultBak);
		esif_ccb_unlink(dv_filebak.filename);
		IOStream_Destroy(vaultBak);
	}
	IOStream_Close(vault);
	IOStream_Destroy(vault);
	return rc;
}