Просмотр исходного кода

Merge SourceForge SVN repository, OpenV2G 0.9.6

Merge SourceForge SVN repository, OpenV2G 0.9.6
Martin-P 1 месяц назад
Родитель
Сommit
733d4f43cb
7 измененных файлов с 201 добавлено и 109 удалено
  1. 7 2
      README.txt
  2. 64 11
      src/codec/DecoderChannel.c
  3. 3 2
      src/codec/EncoderChannel.c
  4. 30 30
      src/codec/ErrorCodes.h
  5. 44 29
      src/codec/MethodsBag.c
  6. 37 29
      src/codec/MethodsBag.h
  7. 16 6
      src/test/main_example.c

+ 7 - 2
README.txt

@@ -1,12 +1,12 @@
 -------------------------------------------------------------------------
 OpenV2G - an open source project implementing the basic functionality of the ISO IEC 15118 vehicle to grid (V2G) communication interface 
-Version 0.9.5, released March 11, 2022
+Version 0.9.6, released January 14, 2025
 http://openv2g.sourceforge.net/
 
 Please report bugs via the SourceForge bug tracking system at http://sourceforge.net/tracker/?group_id=350113.
 Thank you.
 
-Copyright (C) 2007-2022 Siemens AG
+Copyright (C) 2007-2025 Siemens AG
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published
@@ -21,6 +21,11 @@ GNU Lesser General Public License for more details.
 You should have received a copy of the GNU Lesser General Public License
 along with this program. If not, see <http://www.gnu.org/licenses/>.
 
+-------------------------------------------------------------------------
+CHANGES from version 0.9.5:
+-------------------------------------------------------------------------
+* fix overflow when decoding large integer numbers
+
 -------------------------------------------------------------------------
 CHANGES from version 0.9.4:
 -------------------------------------------------------------------------

+ 64 - 11
src/codec/DecoderChannel.c

@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2022 Siemens AG
+ * Copyright (C) 2007-2025 Siemens AG
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published
@@ -18,7 +18,7 @@
 /*******************************************************************
  *
  * @author Daniel.Peintner.EXT@siemens.com
- * @version 2022-03-08 
+ * @version 2025-01-14 
  * @contact Richard.Kuntschke@siemens.com
  *
  * <p>Code generated by EXIdizer</p>
@@ -36,7 +36,7 @@
 #include "BitInputStream.h"
 #include "EXITypes.h"
 #include "MethodsBag.h"
-/*#include "v2gEXICoder.h"*/
+/*#include "EXICoder.h"*/
 #include "ErrorCodes.h"
 
 #if MEMORY_ALLOCATION == DYNAMIC_ALLOCATION
@@ -173,6 +173,9 @@ int decodeUnsignedInteger16(bitstream_t* stream, uint16_t* uint16) {
 	do {
 		/* 1. Read the next octet */
 		errn = decode(stream, &b);
+		if ((8 - numberOfLeadingZeros((b & 127)) + mShift) > 16) {
+			return EXI_ERROR_INTEGER_OVERFLOW;
+		}
 		/* 2. Multiply the value of the unsigned number represented by the 7
 		 * least significant
 		 * bits of the octet by the current multiplier and add the result to
@@ -196,6 +199,9 @@ int decodeUnsignedInteger32(bitstream_t* stream, uint32_t* uint32) {
 	do {
 		/* 1. Read the next octet */
 		errn = decode(stream, &b);
+		if ((8 - numberOfLeadingZeros((b & 127)) + mShift) > 32) {
+			return EXI_ERROR_INTEGER_OVERFLOW;
+		}
 		/* 2. Multiply the value of the unsigned number represented by the 7
 		 * least significant
 		 * bits of the octet by the current multiplier and add the result to
@@ -250,11 +256,14 @@ int decodeUnsignedIntegerSizeT(bitstream_t* stream, size_t* sizeT) {
 int decodeUnsignedInteger64(bitstream_t* stream, uint64_t* uint64) {
 	unsigned int mShift = 0;
 	int errn = 0;
-	uint8_t b;
+	uint8_t b = 0;
 	*uint64 = 0L;
 
 	do {
 		errn = decode(stream, &b);
+		if ((8 - numberOfLeadingZeros((b & 127)) + mShift) > 64) {
+			return EXI_ERROR_INTEGER_OVERFLOW;
+		}
 		*uint64 += ((uint64_t) (b & 127)) << mShift;
 		mShift += 7;
 	} while (errn == 0 && (b >> 7) == 1);
@@ -262,7 +271,6 @@ int decodeUnsignedInteger64(bitstream_t* stream, uint64_t* uint64) {
 	return errn;
 }
 
-
 void _reverseArray(uint8_t *array, int number) {
     int x, t;
     number--;
@@ -577,6 +585,7 @@ int decodeUnsignedIntegerBig(bitstream_t* stream, size_t size, uint8_t* data, si
 	return errn;
 }
 
+
 int decodeInteger(bitstream_t* stream, exi_integer_t* iv) {
 	int b;
 	int errn = decodeBoolean(stream, &b);
@@ -600,15 +609,29 @@ int decodeInteger16(bitstream_t* stream, int16_t* int16) {
 	int errn = decodeBoolean(stream, &b);
 
 	if (errn == 0) {
+		/* INT16_MIN		-32768 */
+		/* INT16_MAX		 32767 */
 		if (b) {
 			/* For negative values, the Unsigned Integer holds the
 			 * magnitude of the value minus 1 */
 			errn = decodeUnsignedInteger16(stream, &uint16);
-			*int16 = (int16_t)(-(uint16 + 1));
+			if (errn == 0) {
+				if (uint16 < 32768) {
+					*int16 = (int16_t)(-(uint16 + 1));
+				} else {
+					errn = EXI_ERROR_INTEGER_OVERFLOW;
+				}
+			}
 		} else {
 			/* positive */
 			errn = decodeUnsignedInteger16(stream, &uint16);
-			*int16 = (int16_t)(uint16);
+			if (errn == 0) {
+				if (uint16 <= 32767) {
+					*int16 = (int16_t)(uint16);
+				} else {
+					errn = EXI_ERROR_INTEGER_OVERFLOW;
+				}
+			}
 		}
 	}
 
@@ -627,15 +650,31 @@ int decodeInteger32(bitstream_t* stream, int32_t* int32) {
 	int errn = decodeBoolean(stream, &b);
 
 	if (errn == 0) {
+		/* INT16_MIN		-2147483648 */
+		/* INT16_MAX		 2147483647 */
 		if (b) {
 			/* For negative values, the Unsigned Integer holds the
 			 * magnitude of the value minus 1 */
 			errn = decodeUnsignedInteger32(stream, &uint32);
-			*int32 = (-(int32_t)(uint32 + 1));
+			if (errn == 0) {
+				/* INT32_MIN (-2147483647 - 1) */
+				if ((uint32-1) < 2147483647) {
+					*int32 = (-(int32_t)(uint32 + 1));
+				} else {
+					errn = EXI_ERROR_INTEGER_OVERFLOW;
+				}
+			}
 		} else {
 			/* positive */
 			errn = decodeUnsignedInteger32(stream, &uint32);
-			*int32 = (int32_t)(uint32);
+			if (errn == 0) {
+				/* INT32_MAX 2147483647 */
+				if (uint32 <= 2147483647) {
+					*int32 = (int32_t)(uint32);
+				} else {
+					errn = EXI_ERROR_INTEGER_OVERFLOW;
+				}
+			}
 		}
 	}
 
@@ -658,11 +697,25 @@ int decodeInteger64(bitstream_t* stream, int64_t* int64) {
 			/* For negative values, the Unsigned Integer holds the
 			 * magnitude of the value minus 1 */
 			errn = decodeUnsignedInteger64(stream, &uint64);
-			*int64 = (-(int64_t)(uint64 + 1));
+			if (errn == 0) {
+				/* INT64_MIN (-9223372036854775807LL - 1)*/
+				if ((uint64-1) < 9223372036854775807LL) {
+					*int64 = (-(int64_t)(uint64 + 1));
+				} else {
+					errn = EXI_ERROR_INTEGER_OVERFLOW;
+				}
+			}
 		} else {
 			/* positive */
 			errn = decodeUnsignedInteger64(stream, &uint64);
-			*int64 = (int64_t)(uint64);
+			if (errn == 0) {
+				/** INT64_MAX 9223372036854775807LL */
+				if (uint64 <= 9223372036854775807LL) {
+					*int64 = (int64_t)(uint64);
+				} else {
+					errn = EXI_ERROR_INTEGER_OVERFLOW;
+				}
+			}
 		}
 	}
 

+ 3 - 2
src/codec/EncoderChannel.c

@@ -208,7 +208,8 @@ int encodeUnsignedInteger64(bitstream_t* stream, uint64_t n) {
 void _shiftRight7(uint8_t* buf, int len) {
 	const int shift = 7;
     unsigned char tmp = 0x00, tmp2 = 0x00;
-    for (int k = 0; k <= len; k++) {
+    int k;
+    for (k = 0; k <= len; k++) {
         if (k == 0) {
             tmp = buf[k];
             buf[k] >>= shift;
@@ -236,7 +237,7 @@ int encodeUnsignedIntegerBig(bitstream_t* stream, size_t size, uint8_t* data, si
 	int lenM1 = len - 1;
 	const int MAX_BIGINT_ARRAY = 25;
 	uint8_t lastEncode = 0;
-	uint8_t bytesToShift[MAX_BIGINT_ARRAY]; // MAXIMUM
+	uint8_t bytesToShift[MAX_BIGINT_ARRAY]; /* MAXIMUM */
 	size_t bitsToEncode = len * 8;
 
 	if(MAX_BIGINT_ARRAY <= len) {

+ 30 - 30
src/codec/ErrorCodes.h

@@ -1,32 +1,32 @@
-/*
- * Copyright (C) 2007-2018 Siemens AG
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*******************************************************************
- *
- * @author Daniel.Peintner.EXT@siemens.com
- * @version 2017-03-02 
- * @contact Richard.Kuntschke@siemens.com
- *
- * <p>Code generated by EXIdizer</p>
- * <p>Schema: V2G_CI_MsgDef.xsd</p>
- *
- *
- ********************************************************************/
-
+/*
+ * Copyright (C) 2007-2025 Siemens AG
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*******************************************************************
+ *
+ * @author Daniel.Peintner.EXT@siemens.com
+ * @version 2025-01-14 
+ * @contact Richard.Kuntschke@siemens.com
+ *
+ * <p>Code generated by EXIdizer</p>
+ * <p>Schema: V2G_CI_MsgDef.xsd</p>
+ *
+ *
+ ********************************************************************/
+
 
 
 /**
@@ -54,7 +54,7 @@ extern "C" {
 #define EXI_ERROR_OUT_OF_GRAMMAR_STACK -104
 #define EXI_ERROR_OUT_OF_RUNTIME_GRAMMAR_STACK -105
 #define EXI_ERROR_OUT_OF_QNAMES -106
-
+#define EXI_ERROR_INTEGER_OVERFLOW -107
 #define EXI_ERROR_UNKOWN_GRAMMAR_ID -108
 #define EXI_ERROR_UNKOWN_EVENT -109
 #define EXI_ERROR_UNKOWN_EVENT_CODE -110

+ 44 - 29
src/codec/MethodsBag.c

@@ -1,32 +1,32 @@
-/*
- * Copyright (C) 2007-2018 Siemens AG
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*******************************************************************
- *
- * @author Daniel.Peintner.EXT@siemens.com
- * @version 2017-03-02 
- * @contact Richard.Kuntschke@siemens.com
- *
- * <p>Code generated by EXIdizer</p>
- * <p>Schema: V2G_CI_MsgDef.xsd</p>
- *
- *
- ********************************************************************/
-
+/*
+ * Copyright (C) 2007-2025 Siemens AG
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*******************************************************************
+ *
+ * @author Daniel.Peintner.EXT@siemens.com
+ * @version 2025-01-14 
+ * @contact Richard.Kuntschke@siemens.com
+ *
+ * <p>Code generated by EXIdizer</p>
+ * <p>Schema: V2G_CI_MsgDef.xsd</p>
+ *
+ *
+ ********************************************************************/
+
 
 
 #ifndef METHODS_BAG_C
@@ -112,6 +112,21 @@ uint8_t numberOf7BitBlocksToRepresent(uint32_t n) {
 }
 
 
+int numberOfLeadingZeros(uint8_t b) {
+	int count = 0, i;
+	/* Equivalent to 10000000 */
+	int msb = 1 << (8 - 1);
+	/* Iterate over each bit */
+	for(i=0; i<8; i++) {
+		/* If leading set bit is found */
+		if((b << i) & msb) {
+			/* Terminate the loop */
+			break;
+		}
+		count++;
+	}
+	return count;
+}
 
 #endif
 

+ 37 - 29
src/codec/MethodsBag.h

@@ -1,32 +1,32 @@
-/*
- * Copyright (C) 2007-2018 Siemens AG
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*******************************************************************
- *
- * @author Daniel.Peintner.EXT@siemens.com
- * @version 2017-03-02 
- * @contact Richard.Kuntschke@siemens.com
- *
- * <p>Code generated by EXIdizer</p>
- * <p>Schema: V2G_CI_MsgDef.xsd</p>
- *
- *
- ********************************************************************/
-
+/*
+ * Copyright (C) 2007-2025 Siemens AG
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*******************************************************************
+ *
+ * @author Daniel.Peintner.EXT@siemens.com
+ * @version 2025-01-14 
+ * @contact Richard.Kuntschke@siemens.com
+ *
+ * <p>Code generated by EXIdizer</p>
+ * <p>Schema: V2G_CI_MsgDef.xsd</p>
+ *
+ *
+ ********************************************************************/
+
 
 
 /**
@@ -65,6 +65,14 @@ int exiGetCodingLength(size_t characteristics, size_t* codingLength);
  */
 uint8_t numberOf7BitBlocksToRepresent(uint32_t n);
 
+/**
+ * \brief  	Returns the number of leading zeros in number.
+ *
+ * \param       b  	number
+ * \return      number of leading zeros
+ *
+ */
+int numberOfLeadingZeros(uint8_t b);
 
 #endif
 

+ 16 - 6
src/test/main_example.c

@@ -328,9 +328,14 @@ static int sessionSetup2(struct iso2EXIDocument* exiIn, struct iso2EXIDocument*
 	init_iso2SessionSetupResType(&exiOut->V2G_Message.Body.SessionSetupRes);
 
 	exiOut->V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_OK;
-	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[0] = 0;
-	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[1] = 20;
-	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 2;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[0] = 65;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[1] = 85;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[2] = 86;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[3] = 87;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[4] = 88;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[5] = 89;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[6] = 90;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 7;
 	exiOut->V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
 	exiOut->V2G_Message.Body.SessionSetupRes.EVSETimeStamp = 123456789;
 
@@ -1504,9 +1509,14 @@ static int sessionSetup1(struct iso1EXIDocument* exiIn, struct iso1EXIDocument*
 	init_iso1SessionSetupResType(&exiOut->V2G_Message.Body.SessionSetupRes);
 
 	exiOut->V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_OK;
-	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[0] = 0;
-	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[1] = 20;
-	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 2;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[0] = 65;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[1] = 85;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[2] = 86;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[3] = 87;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[4] = 88;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[5] = 89;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.characters[6] = 90;
+	exiOut->V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 7;
 	exiOut->V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
 	exiOut->V2G_Message.Body.SessionSetupRes.EVSETimeStamp = 123456789;