123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- #ifndef WIN32
- #endif
- #ifdef WIN32
- #include <Windows.h>
- #include <stdio.h>
- #endif
- #include "optiga/common/Util.h"
- int32_t CompareUint64(const sUint64 *PpsSrc1, const sUint64 *PpsSrc2)
- {
- int32_t i4Retval = (int32_t) UTIL_ERROR;
- do
- {
- #ifdef ENABLE_NULL_CHECKS
- if((NULL == PpsSrc1) || (NULL == PpsSrc2))
- {
- break;
- }
- #endif
- if(PpsSrc1->dwHigherByte == PpsSrc2->dwHigherByte)
- {
- if(PpsSrc1->dwLowerByte > PpsSrc2->dwLowerByte)
- {
- i4Retval = GREATER_THAN;
- }
- else if(PpsSrc1->dwLowerByte < PpsSrc2->dwLowerByte)
- {
- i4Retval = LESSER_THAN;
- }
-
- else
- {
- i4Retval = EQUAL;
- }
- }
- else
- {
- if(PpsSrc1->dwHigherByte > PpsSrc2->dwHigherByte)
- {
- i4Retval = GREATER_THAN;
- }
-
- else
- {
- i4Retval = LESSER_THAN;
- }
- }
- }while(0);
- return i4Retval;
- }
- int32_t SubtractUint64(const sUint64 *PpsMinuend, const sUint64 *PpsSubtrahend,sUint64 *PpsDifference)
- {
- int32_t i4Retval = (int32_t) UTIL_ERROR;
- sUint64 sIntermediateVal;
- do
- {
- #ifdef ENABLE_NULL_CHECKS
- if((NULL == PpsMinuend) || (NULL == PpsSubtrahend) || (NULL == PpsDifference))
- {
- break;
- }
- #endif
- i4Retval = CompareUint64(PpsMinuend, PpsSubtrahend);
-
- if((GREATER_THAN != i4Retval) && (EQUAL != i4Retval))
- {
- i4Retval = (int32_t) UTIL_ERROR;
- break;
- }
- sIntermediateVal.dwLowerByte = PpsMinuend->dwLowerByte - PpsSubtrahend->dwLowerByte;
- sIntermediateVal.dwHigherByte = PpsMinuend->dwHigherByte - PpsSubtrahend->dwHigherByte;
- if(sIntermediateVal.dwLowerByte > PpsMinuend->dwLowerByte)
- {
- --sIntermediateVal.dwHigherByte;
- }
- PpsDifference->dwLowerByte = sIntermediateVal.dwLowerByte;
- PpsDifference->dwHigherByte = sIntermediateVal.dwHigherByte;
- i4Retval = (int32_t) UTIL_SUCCESS;
- }while(0);
- return i4Retval;
- }
- int32_t ShiftLeftUint64(sUint64 *PpsWindow, sUint64 PsShiftCount, uint8_t PbWindowSize, uint8_t PbMaxWindowSize)
- {
- int32_t i4Retval = (int32_t) UTIL_ERROR;
- uint32_t dwShiftCount = 0;
- uint32_t dwMaskShiftCount = MASK_DOUBLE_WORD;
- sUint64 sWindowSize = {0} ;
- do
- {
- #ifdef ENABLE_NULL_CHECKS
- if(NULL == PpsWindow)
- {
- break;
- }
- #endif
- sWindowSize.dwLowerByte = (uint32_t) PbWindowSize;
- i4Retval = CompareUint64(&PsShiftCount, &sWindowSize);
-
- if((GREATER_THAN == i4Retval) || (EQUAL == i4Retval))
- {
-
- PpsWindow->dwLowerByte = DEFAULT_LOWBOUND_DOUBLEWORD;
- PpsWindow->dwHigherByte = DEFAULT_LOWBOUND_DOUBLEWORD;
- i4Retval = (int32_t) UTIL_SUCCESS;
- break;
- }
-
-
- dwShiftCount = PsShiftCount.dwLowerByte;
-
- dwMaskShiftCount &= PpsWindow->dwLowerByte ;
-
- if(WORD_SIZE == PbWindowSize)
- {
-
- PpsWindow->dwLowerByte = DEFAULT_LOWBOUND_DOUBLEWORD ;
- PpsWindow->dwHigherByte <<= dwShiftCount ;
- i4Retval = (int32_t) UTIL_SUCCESS;
- break;
- }
-
- if(WORD_SIZE <= dwShiftCount)
- {
-
- PpsWindow->dwLowerByte = DEFAULT_LOWBOUND_DOUBLEWORD ;
-
- PpsWindow->dwHigherByte = DEFAULT_LOWBOUND_DOUBLEWORD ;
-
- PpsWindow->dwHigherByte |= dwMaskShiftCount;
-
- dwShiftCount -= WORD_SIZE;
-
- PpsWindow->dwHigherByte <<= dwShiftCount;
-
- PpsWindow->dwHigherByte &= MASK_DOUBLE_WORD >> (PbMaxWindowSize - PbWindowSize);
- i4Retval = (int32_t) UTIL_SUCCESS;
- break;
- }
-
-
- PpsWindow->dwLowerByte <<= dwShiftCount ;
-
- PpsWindow->dwHigherByte <<= dwShiftCount ;
-
- dwMaskShiftCount >>= WORD_SIZE - dwShiftCount;
-
- PpsWindow->dwHigherByte |= dwMaskShiftCount;
-
- PpsWindow->dwHigherByte &= MASK_DOUBLE_WORD >> (PbMaxWindowSize - PbWindowSize);
- i4Retval = (int32_t) UTIL_SUCCESS;
- }while(0);
- return i4Retval;
- }
- int32_t AddUint64(const sUint64 *PpsSrc1, const sUint64 *PpsSrc2,sUint64 *PpsDest)
- {
- int32_t i4Retval = (int32_t) UTIL_ERROR;
- sUint64 sIntermediateval;
- sIntermediateval.dwLowerByte = PpsSrc1->dwLowerByte + PpsSrc2->dwLowerByte;
- sIntermediateval.dwHigherByte = PpsSrc1->dwHigherByte + PpsSrc2->dwHigherByte;
- if(sIntermediateval.dwLowerByte < PpsSrc1->dwLowerByte)
- {
- ++sIntermediateval.dwHigherByte;
- }
- PpsDest->dwLowerByte = sIntermediateval.dwLowerByte;
- PpsDest->dwHigherByte = sIntermediateval.dwHigherByte;
- i4Retval = (int32_t) UTIL_SUCCESS;
- return i4Retval;
- }
- int32_t IncrementUint64(sUint64 *PpsSrc1)
- {
- int32_t i4Retval = (int32_t) UTIL_ERROR;
- sUint64 sOne;
- sOne.dwHigherByte = 0;
- sOne.dwLowerByte = 1;
- i4Retval = AddUint64(PpsSrc1,&sOne,PpsSrc1);
- return i4Retval;
- }
- uint16_t Utility_GetUint16 (const uint8_t* PprgbData)
- {
- uint16_t wVal;
- wVal = (uint16_t)(*PprgbData << 8);
- wVal |= (uint16_t)(*(PprgbData+1));
- return wVal;
- }
- void Utility_SetUint24 (uint8_t* PprgbData,uint32_t Pdwvalue)
- {
- #define prgbBuffer PprgbData
- *(prgbBuffer) = (uint8_t)(Pdwvalue>>16);
- *(prgbBuffer+1) = (uint8_t)(Pdwvalue>>8);
- *(prgbBuffer+2) = (uint8_t)(Pdwvalue);
- #undef prgbBuffer
- }
- uint32_t Utility_GetUint24 (const uint8_t* PprgbData)
- {
- uint32_t dwVal;
- dwVal = ((uint32_t)(*PprgbData))<< 16;
- dwVal |= ((uint32_t)(*(PprgbData+1)))<<8;
- dwVal |= (uint32_t)(*(PprgbData+2));
- return dwVal;
- }
- uint32_t Utility_GetUint32 (const uint8_t* PprgbData)
- {
- uint32_t dwVal;
- dwVal = ((uint32_t)(*PprgbData))<< 24 | ((uint32_t)(*(PprgbData + 1))<< 16 | ((uint32_t)(*(PprgbData + 2)))<< 8 | (uint32_t)(*(PprgbData + 3)));
- return dwVal;
- }
- void Utility_SetUint16 (puint8_t PprgbData,uint16_t PwValue)
- {
- *PprgbData = (uint8_t)(PwValue>>8);
- *(PprgbData+1) = (uint8_t)(PwValue);
- }
- int32_t Utility_SetBitUint64(sUint64* PprgbData, uint8_t bWindowSize, uint8_t bBitPosition)
- {
- int32_t i4Retval = (int32_t) UTIL_ERROR;
- uint8_t bShiftCount = 0;
- do
- {
- #ifdef ENABLE_NULL_CHECKS
- if(NULL == PprgbData)
- {
- break;
- }
- #endif
- if(bBitPosition > bWindowSize)
- {
- break;
- }
-
- if(bBitPosition == bWindowSize)
- {
- if(WORD_SIZE == bWindowSize)
- {
- PprgbData->dwHigherByte |= LEAST_SIGNIFICANT_BIT_HIGH;
- i4Retval = (int32_t) UTIL_SUCCESS;
- break;
- }
- PprgbData->dwLowerByte |= LEAST_SIGNIFICANT_BIT_HIGH;
- i4Retval = (int32_t) UTIL_SUCCESS;
- break;
- }
-
- bShiftCount = bWindowSize - bBitPosition;
-
- if(WORD_SIZE == bWindowSize)
- {
- PprgbData->dwHigherByte |= LEAST_SIGNIFICANT_BIT_HIGH << (bShiftCount - 1);
- i4Retval = (int32_t) UTIL_SUCCESS;
- break;
- }
-
- if(WORD_SIZE < bShiftCount)
- {
- bShiftCount -= WORD_SIZE;
- PprgbData->dwHigherByte |= LEAST_SIGNIFICANT_BIT_HIGH << (bShiftCount - 1);
- i4Retval = (int32_t) UTIL_SUCCESS;
- break;
- }
-
- PprgbData->dwLowerByte |= LEAST_SIGNIFICANT_BIT_HIGH << (bShiftCount - 1);
- i4Retval = (int32_t) UTIL_SUCCESS;
- }while(0);
- return i4Retval;
- }
- void Utility_SetUint32 (uint8_t* PprgbData,uint32_t Pdwvalue)
- {
- #define prgbBuffer PprgbData
- *(prgbBuffer) = (uint8_t)(Pdwvalue>>24);
- *(prgbBuffer + 1) = (uint8_t)(Pdwvalue>>16);
- *(prgbBuffer+2) = (uint8_t)(Pdwvalue>>8);
- *(prgbBuffer+3) = (uint8_t)(Pdwvalue);
- #undef prgbBuffer
- }
- void Utility_Memmove(puint8_t PprgbDestBuf, const puint8_t PprgbSrcBuf, uint16_t PwLength)
- {
- uint16_t wIndex=0;
- puint8_t pTempSrcBuf = PprgbSrcBuf;
- do
- {
-
- if((PprgbDestBuf > pTempSrcBuf) && (PprgbDestBuf <= (pTempSrcBuf + PwLength - 1)))
- {
- while(0 < PwLength)
- {
- PwLength -= 1;
- *(PprgbDestBuf + PwLength) = *(pTempSrcBuf + PwLength);
- }
- }
- else
- {
- while(wIndex < PwLength)
- {
- *(PprgbDestBuf + wIndex) = *(pTempSrcBuf + wIndex);
- wIndex++;
- }
- }
- }while(0);
- }
|