hcp_Int hcp_InitializeParametersFromTemplate(hcp_tState* pState, hcp_tParameterSet* pParameters, hcp_tParameterTemplateSet* pTemplateSet) { hcp_Int error = HCP_NOERROR; error = hcp_InitializeParameters(pState, pParameters); if (error != HCP_NOERROR) { return error; } const hcp_Size_t templateCount = pTemplateSet->header.length; // for each template, create a new parameter hcp_Size_t templateIndex = 0; for (templateIndex = 0; templateIndex < templateCount; templateIndex++) { hcp_Size_t parameterIndex = 0; error = hcp_PushEmpty(&pParameters->header, ¶meterIndex); if (error != HCP_NOERROR) { break; } hcp_tParameter* parameter = (hcp_tParameter*)hcp_ValueAt(&pParameters->header, parameterIndex); hcp_tParameterTemplate* t = (hcp_tParameterTemplate*)hcp_ValueAt(&pTemplateSet->header, templateIndex); parameter->template_ = t; parameter->hasValue = HCP_FALSE; parameter->value.i = 0; } return error; }
hcp_Size_t HCP_CALL hcp_GetParameterSetSize(const hcp_tParameterSet* pParameters) { const hcp_Size_t length = pParameters->header.length; hcp_Size_t dataSize = 0; for (hcp_Size_t i = 0; i < length; i++) { hcp_tParameter* parameter = (hcp_tParameter*)hcp_ValueAt(&pParameters->header, i); const hcp_Uint8 typeId = parameter->template_->type; if (typeId == HCP_STRING_ID) { if (parameter->template_->length > 0) { dataSize += parameter->template_->length; } else { dataSize += parameter->value.str.length; } } else if (typeId == HCP_BLOB_ID) { dataSize += parameter->value.blb.length; } else { dataSize += hcp_GetTypeSize(typeId); } } return dataSize; }
const hcp::tCodec* hcp::Runtime::getCodec(const hcp_Size_t index) const { if (index >= 0 && index < _codecs.header.length) { return (hcp::tCodec*)hcp_ValueAt(&_codecs.header, index); } return nullptr; }
hcp_Int hcp_SetString(hcp_tParameterSet* pParameters, const hcp_tString* Name, const hcp_tString* Value) { hcp_Boolean found = HCP_FALSE; hcp_Size_t index = hcp_FindFirst(&pParameters->header, 0, (void*)Name, &found); hcp_tParameter* parameter = HCP_NULL; hcp_Int error = HCP_NOERROR; if (found == HCP_FALSE) { if ((error = hcp_PushEmpty(&pParameters->header, &index)) != HCP_NULL) { return error; } parameter = (hcp_tParameter*)hcp_ValueAt(&pParameters->header, index); } else { parameter = (hcp_tParameter*)hcp_ValueAt(&pParameters->header, index); } parameter->value.str = *Value; parameter->hasValue = HCP_TRUE; return HCP_NOERROR; }
hcp_Int hcp_InitializeCommands(hcp_tState* pState, hcp_tCommandSet* pCommands, hcp_tModel* pTemplate) { hcp_Int error = HCP_INITIALIZEVECTOR(pState, &pCommands->header, pCommands->fixed, hcp_tCommand, HCP_NULL, hcp_CompareCommand, hcp_IsCommand); const hcp_Size_t length = pTemplate->commands.header.length; hcp_Size_t i = 0; for (i = 0; i < length; i++) { hcp_Size_t commandIndex = 0; error = hcp_PushEmpty(&pCommands->header, &commandIndex); if (error != HCP_NOERROR) { return error; } hcp_tCommand* command = (hcp_tCommand*)hcp_ValueAt(&pCommands->header, commandIndex); hcp_tCommandTemplate* t = (hcp_tCommandTemplate*)hcp_ValueAt(&pTemplate->commands.header, i); command->template_ = t; // create in-parameters error = hcp_InitializeParametersFromTemplate(pState, &command->inParams, &t->inParameters); if (error != HCP_NOERROR) { hcp_Pop(&pCommands->header, commandIndex); return error; } //create out-parameters error = hcp_InitializeParametersFromTemplate(pState, &command->outParams, &t->outParameters); if (error != HCP_NOERROR) { hcp_Pop(&pCommands->header, commandIndex); return error; } } return error; }
hcp_Int hcp_GetUint8(const hcp_tProtocol* pProtocol, const hcp_szStr Key, hcp_Uint8* pDest) { *pDest = 0; hcp_tString key; key.value = Key; key.length = hcp_szStrLen(Key); key.zeroTerm = HCP_TRUE; hcp_Boolean found = HCP_FALSE; hcp_Size_t index = hcp_FindFirst(&pProtocol->header, 0, &key, &found); if (found == HCP_FALSE) { return HCP_MISSINGCODECNODE; } hcp_tProtocolNode* node = (hcp_tProtocolNode*)hcp_ValueAt(&pProtocol->header, index); *pDest = (hcp_Uint8)hcp_Atio(&node->value); return HCP_NOERROR; }
hcp_Int HCP_CALL hcp_BytesToParameters(hcp_tRuntime* R, const hcp_tBlob* pSource, const hcp_Size_t Offset, hcp_tParameterSet* pParameters, const hcp_Uint8 Endianess, hcp_DecodeString StringDecoder, void* pContext) { hcp_Size_t i = 0; const hcp_Size_t parameterCount = pParameters->header.length; hcp_Size_t bytesRead = 0; for (i = 0; i < parameterCount; i++) { hcp_tParameter* parameter = (hcp_tParameter*)hcp_ValueAt(&pParameters->header, i); const hcp_Int typeId = parameter->template_->type; hcp_Size_t typeSize = hcp_GetTypeSize(typeId); hcp_Int error = HCP_NOERROR; switch (typeId) { case HCP_UINT8_ID: { error = hcp_BytesToUint8(pSource, Offset + bytesRead, ¶meter->value.u8, Endianess); } break; case HCP_INT8_ID: { error = hcp_BytesToInt8(pSource, Offset + bytesRead, ¶meter->value.s8, Endianess); } break; case HCP_SIMPLEVERSION_ID: case HCP_UINT16_ID: { error = hcp_BytesToUint16(pSource, Offset + bytesRead, ¶meter->value.u16, Endianess); } break; case HCP_INT16_ID: { error = hcp_BytesToInt16(pSource, Offset + bytesRead, ¶meter->value.i16, Endianess); } break; case HCP_UNIXTIME_ID: case HCP_UINT32_ID: { error = hcp_BytesToUint32(pSource, Offset + bytesRead, ¶meter->value.u32, Endianess); } break; case HCP_INT32_ID: { error = hcp_BytesToInt32(pSource, Offset + bytesRead, ¶meter->value.i32, Endianess); } break; case HCP_UINT64_ID: { error = hcp_BytesToUint64(pSource, Offset + bytesRead, ¶meter->value.u64, Endianess); } break; case HCP_INT64_ID: { error = hcp_BytesToInt64(pSource, Offset + bytesRead, ¶meter->value.i64, Endianess); } break; case HCP_BLOB_ID: { if (parameter->template_->length < 1) { // no length specified, the byte-array must be the last // parameter in the list since we will consume the remaning byte(s) if (i + 1 < parameterCount) { return HCP_MISSINGBYTEARRAY_SIZE; } typeSize = pSource->length - (Offset + bytesRead); } else { typeSize = parameter->template_->length; } parameter->value.blb.maxLength = typeSize; parameter->value.blb.length = typeSize; parameter->value.blb.value = (hcp_Uint8*)((hcp_Size_t)pSource->value + Offset + bytesRead); } break; case HCP_STRING_ID: { if (StringDecoder != HCP_NULL) { typeSize = StringDecoder(R, pSource, parameter->template_, Offset + bytesRead, ¶meter->value.str, pContext); if (typeSize < 1) { return typeSize; } } else { hcp_Int32 templateLength = parameter->template_->length; hcp_Char* str = (hcp_Char*)((hcp_Size_t)pSource->value + Offset + bytesRead); hcp_tString* pString = ¶meter->value.str; // fixed size if (templateLength > 0) { // the string may contain \0 in which case // the specified length is not valid... pString->zeroTerm = HCP_FALSE; pString->length = templateLength; hcp_Int32 end = 0; for (hcp_Int32 end = 0; end < (hcp_Int32)templateLength; end++) { if (str[end] == 0) { pString->length = end; pString->zeroTerm = HCP_TRUE; break; } } pString->value = str; } // zero-terminated else if (templateLength == 0) { pString->length = R->szStrLen((hcp_szStr)str); pString->value = str; pString->zeroTerm = HCP_TRUE; typeSize = pString->length; } // consume the remaning byte(s) else { pString->length = pSource->length - Offset; pString->value = str; pString->zeroTerm = HCP_FALSE; for (hcp_Int32 end = 0; end < pString->length; end++) { if (str[end] == 0) { pString->length = end; pString->zeroTerm = HCP_TRUE; break; } } } // move past the string typeSize = pString->length; // move past the trailing \0 if avalible if (pString->zeroTerm) { typeSize++; } } } break; default: { return HCP_INVALIDTYPEID; } } bytesRead += typeSize;; } return bytesRead; }
hcp_Int HCP_CALL hcp_AppendParameters(hcp_tRuntime* R, hcp_tBlob* pDestination, hcp_tParameterSet* pParameters, const hcp_Uint8 Endianess, hcp_EncodeString StringEncoder, void* pContext) { const hcp_Size_t length = pParameters->header.length; hcp_Int error = HCP_NOERROR; for (hcp_Size_t i = 0; i < length; i++) { hcp_tParameter* parameter = (hcp_tParameter*)hcp_ValueAt(&pParameters->header, i); switch (parameter->template_->type) { case HCP_BOOLEAN_ID: case HCP_INT8_ID: case HCP_UINT8_ID: { error = hcp_AppendByte(parameter->value.u8, pDestination); }break; case HCP_UINT16_ID: { error = hcp_Uint16ToBytes(parameter->value.u16, pDestination, Endianess); }break; case HCP_INT16_ID: { error = hcp_Int16ToBytes(parameter->value.i16, pDestination, Endianess); }break; case HCP_UNIXTIME_ID: case HCP_UINT32_ID: { error = hcp_Uint32ToBytes(parameter->value.u32, pDestination, Endianess); }break; case HCP_INT32_ID: { error = hcp_Int32ToBytes(parameter->value.i32, pDestination, Endianess); }break; case HCP_UINT64_ID: { error = hcp_Uint64ToBytes(parameter->value.u64, pDestination, Endianess); }break; case HCP_INT64_ID: { error = hcp_Int64ToBytes(parameter->value.i64, pDestination, Endianess); }break; case HCP_STRING_ID: { if (StringEncoder != HCP_NULL) { error = StringEncoder(R,parameter->template_, ¶meter->value.str, pDestination, pContext); } else { const hcp_Size_t fixedLength = parameter->template_->length; const hcp_Size_t valueLength = parameter->value.str.length; if (fixedLength != 0 && fixedLength < valueLength) { error = HCP_INVALID_STRINGSIZE; } else { error = hcp_AppendString(¶meter->value.str, pDestination, HCP_ENCODING_ASCII); if (error == HCP_NOERROR && fixedLength > valueLength) { // add padding so we write the number of byte(s) specified in parameter-length error = hcp_AppendZeroes(fixedLength - valueLength, pDestination); } } } } break; case HCP_BLOB_ID: { error = hcp_AppendBlob(¶meter->value.blb, pDestination); } break; case HCP_SIMPLEVERSION_ID: { error = hcp_Uint16ToBytes(parameter->value.i16, pDestination, Endianess); } break; default: { error = HCP_INVALIDTYPEID; } break; } if (error != HCP_NOERROR) { break; } } return error; }