- MmsValue: fixed unaligned memory access problems for MMS_FLOAT variables

pull/244/head
Michael Zillgith 5 years ago
parent 028553968a
commit 428332fed6

@ -688,11 +688,25 @@ MmsValue_newIntegerFromInt64(int64_t integer);
LIB61850_API MmsValue* LIB61850_API MmsValue*
MmsValue_newUnsignedFromUint32(uint32_t integer); MmsValue_newUnsignedFromUint32(uint32_t integer);
/**
* \brief Create a new 32 bit wide float variable and initialize with value
*
* \param value the initial value
*
* \return new MmsValue instance of type MMS_FLOAT
*/
LIB61850_API MmsValue* LIB61850_API MmsValue*
MmsValue_newFloat(float variable); MmsValue_newFloat(float value);
/**
* \brief Create a new 64 bit wide float variable and initialize with value
*
* \param value the initial value
*
* \return new MmsValue instance of type MMS_FLOAT
*/
LIB61850_API MmsValue* LIB61850_API MmsValue*
MmsValue_newDouble(double variable); MmsValue_newDouble(double value);
/** /**
* \brief Create a (deep) copy of an MmsValue instance * \brief Create a (deep) copy of an MmsValue instance

@ -536,7 +536,7 @@ MmsValue_setBitStringFromIntegerBigEndian(MmsValue* self, uint32_t intValue)
} }
MmsValue* MmsValue*
MmsValue_newFloat(float variable) MmsValue_newFloat(float value)
{ {
MmsValue* self = (MmsValue*) GLOBAL_MALLOC(sizeof(MmsValue)); MmsValue* self = (MmsValue*) GLOBAL_MALLOC(sizeof(MmsValue));
@ -545,40 +545,44 @@ MmsValue_newFloat(float variable)
self->value.floatingPoint.formatWidth = 32; self->value.floatingPoint.formatWidth = 32;
self->value.floatingPoint.exponentWidth = 8; self->value.floatingPoint.exponentWidth = 8;
*((float*) self->value.floatingPoint.buf) = variable; memcpy(self->value.floatingPoint.buf, &value, sizeof(float));
} }
return self; return self;
} }
void void
MmsValue_setFloat(MmsValue* value, float newFloatValue) MmsValue_setFloat(MmsValue* self, float newFloatValue)
{ {
if (value->type == MMS_FLOAT) { if (self->type == MMS_FLOAT) {
if (value->value.floatingPoint.formatWidth == 32) { if (self->value.floatingPoint.formatWidth == 32) {
*((float*) value->value.floatingPoint.buf) = newFloatValue; memcpy(self->value.floatingPoint.buf, &newFloatValue, sizeof(float));
} }
else if (value->value.floatingPoint.formatWidth == 64) { else if (self->value.floatingPoint.formatWidth == 64) {
*((double*) value->value.floatingPoint.buf) = (double) newFloatValue; double newDoubleValue = (double) newFloatValue;
memcpy(self->value.floatingPoint.buf, &newDoubleValue, sizeof(double));
} }
} }
} }
void void
MmsValue_setDouble(MmsValue* value, double newFloatValue) MmsValue_setDouble(MmsValue* self, double newDoubleValue)
{ {
if (value->type == MMS_FLOAT) { if (self->type == MMS_FLOAT) {
if (value->value.floatingPoint.formatWidth == 32) { if (self->value.floatingPoint.formatWidth == 32) {
*((float*) value->value.floatingPoint.buf) = (float) newFloatValue; float newFloatValue = (float) newDoubleValue;
memcpy(self->value.floatingPoint.buf, &newFloatValue, sizeof(float));
} }
else if (value->value.floatingPoint.formatWidth == 64) { else if (self->value.floatingPoint.formatWidth == 64) {
*((double*) value->value.floatingPoint.buf) = newFloatValue; memcpy(self->value.floatingPoint.buf, &newDoubleValue, sizeof(double));
} }
} }
} }
MmsValue* MmsValue*
MmsValue_newDouble(double variable) MmsValue_newDouble(double value)
{ {
MmsValue* self = (MmsValue*) GLOBAL_CALLOC(1, sizeof(MmsValue)); MmsValue* self = (MmsValue*) GLOBAL_CALLOC(1, sizeof(MmsValue));
@ -587,7 +591,7 @@ MmsValue_newDouble(double variable)
self->value.floatingPoint.formatWidth = 64; self->value.floatingPoint.formatWidth = 64;
self->value.floatingPoint.exponentWidth = 11; self->value.floatingPoint.exponentWidth = 11;
*((double*) self->value.floatingPoint.buf) = variable; memcpy(self->value.floatingPoint.buf, &value, sizeof(double));
} }
return self; return self;
@ -939,13 +943,16 @@ MmsValue_toFloat(const MmsValue* self)
if (self->value.floatingPoint.formatWidth == 32) { if (self->value.floatingPoint.formatWidth == 32) {
float val; float val;
val = *((float*) (self->value.floatingPoint.buf)); memcpy(&val, self->value.floatingPoint.buf, sizeof(float));
return val; return val;
} }
else if (self->value.floatingPoint.formatWidth == 64) { else if (self->value.floatingPoint.formatWidth == 64) {
float val; double val;
val = *((double*) (self->value.floatingPoint.buf));
return val; memcpy(&val, self->value.floatingPoint.buf, sizeof(double));
return (float) val;
} }
} }
@ -956,18 +963,24 @@ double
MmsValue_toDouble(const MmsValue* self) MmsValue_toDouble(const MmsValue* self)
{ {
if (self->type == MMS_FLOAT) { if (self->type == MMS_FLOAT) {
double val;
if (self->value.floatingPoint.formatWidth == 32) { if (self->value.floatingPoint.formatWidth == 32) {
val = (double) *((float*) (self->value.floatingPoint.buf)); float val;
return val;
memcpy(&val, self->value.floatingPoint.buf, sizeof(float));
return (double) val;
} }
if (self->value.floatingPoint.formatWidth == 64) { if (self->value.floatingPoint.formatWidth == 64) {
val = *((double*) (self->value.floatingPoint.buf)); double val;
memcpy(&val, self->value.floatingPoint.buf, sizeof(double));
return val; return val;
} }
} }
return 0.f; return (double) 0.f;
} }
uint32_t uint32_t

Loading…
Cancel
Save