From 185176272465c0ce09410e5de886e0eaedc55e2b Mon Sep 17 00:00:00 2001 From: GRISHNOV Date: Wed, 18 Nov 2020 00:49:46 +0300 Subject: [PATCH] switching to using stdint types --- include/PKCS7.h | 16 ++++++++-------- src/PKCS7.c | 25 +++++++++++++------------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/include/PKCS7.h b/include/PKCS7.h index d5b74dd..0a74b47 100644 --- a/include/PKCS7.h +++ b/include/PKCS7.h @@ -17,9 +17,9 @@ typedef enum { The structure contains result of adding PKCS7 padding. */ typedef struct { - void* dataWithPadding; /* result of adding padding to the data */ - unsigned char dataLengthWithPadding; /* length of the result */ - unsigned char valueOfByteForPadding; /* used for padding byte value */ + void* dataWithPadding; /* result of adding padding to the data */ + uint64_t dataLengthWithPadding; /* length of the result */ + uint8_t valueOfByteForPadding; /* used for padding byte value */ } PKCS7_Padding; /* @@ -27,7 +27,7 @@ typedef struct { Your data at the provided address does not change. A copy is created, to which the adding padding is applied. WARNING: use only 0 < BLOCK_SIZE < 256 */ -PKCS7_Padding* addPadding(const void* const data, const unsigned int dataLength, const unsigned char BLOCK_SIZE); +PKCS7_Padding* addPadding(const void* const data, const uint64_t dataLength, const uint8_t BLOCK_SIZE); /* @@ -35,15 +35,15 @@ PKCS7_Padding* addPadding(const void* const data, const unsigned int dataLength, The structure contains result of removing PKCS7 padding. */ typedef struct { - void* dataWithoutPadding; /* result of remove padding from data */ - unsigned char dataLengthWithoutPadding; /* length of the result */ - unsigned char valueOfRemovedByteFromData; /* value of byte that was used for padding */ + void* dataWithoutPadding; /* result of remove padding from data */ + uint64_t dataLengthWithoutPadding; /* length of the result */ + uint8_t valueOfRemovedByteFromData; /* value of byte that was used for padding */ } PKCS7_unPadding; /* Remove PKCS7 padding from data. Your data at the provided address does not change. A copy is created, to which the removing padding is applied. */ -PKCS7_unPadding* removePadding(const void* const data, const unsigned int dataLength); +PKCS7_unPadding* removePadding(const void* const data, const uint64_t dataLength); #endif // PKCS7_H \ No newline at end of file diff --git a/src/PKCS7.c b/src/PKCS7.c index 24c46a4..af15bd3 100644 --- a/src/PKCS7.c +++ b/src/PKCS7.c @@ -1,9 +1,10 @@ #include #include #include +#include #include "./../include/PKCS7.h" -PKCS7_Padding* addPadding(const void* const data, const unsigned int dataLength, const unsigned char BLOCK_SIZE) +PKCS7_Padding* addPadding(const void* const data, const uint64_t dataLength, const uint8_t BLOCK_SIZE) { if (0 == BLOCK_SIZE) { @@ -18,19 +19,19 @@ PKCS7_Padding* addPadding(const void* const data, const unsigned int dataLength, exit(-1); } - unsigned char paddingBytesAmount = BLOCK_SIZE - (dataLength % BLOCK_SIZE); /* number of bytes to be appended */ + uint8_t paddingBytesAmount = BLOCK_SIZE - (dataLength % BLOCK_SIZE); /* number of bytes to be appended */ paddingResult->valueOfByteForPadding = paddingBytesAmount; /* according to the PKCS7 */ paddingResult->dataLengthWithPadding = dataLength + paddingBytesAmount; /* size of the final result */ - unsigned char* dataWithPadding = (unsigned char*) malloc(paddingResult->dataLengthWithPadding); + uint8_t* dataWithPadding = (uint8_t*) malloc(paddingResult->dataLengthWithPadding); if (NULL == paddingResult) { - perror("problem with unsigned char* dataWithPadding"); /* if memory allocation failed */ + perror("problem with uint8_t* dataWithPadding"); /* if memory allocation failed */ exit(-1); } - memcpy(dataWithPadding, data, dataLength); /* copying the original data for further adding padding */ - for (int i = 0; i < paddingBytesAmount; i++) + memcpy(dataWithPadding, data, dataLength); /* copying the original data for further adding padding */ + for (uint8_t i = 0; i < paddingBytesAmount; i++) { dataWithPadding[dataLength + i] = paddingResult->valueOfByteForPadding; /* adding padding bytes */ } @@ -39,7 +40,7 @@ PKCS7_Padding* addPadding(const void* const data, const unsigned int dataLength, return paddingResult; } -PKCS7_unPadding* removePadding(const void* const data, const unsigned int dataLength) +PKCS7_unPadding* removePadding(const void* const data, const uint64_t dataLength) { PKCS7_unPadding* unpaddingResult = (PKCS7_unPadding*) malloc(sizeof(PKCS7_unPadding)); if (NULL == unpaddingResult) @@ -48,14 +49,14 @@ PKCS7_unPadding* removePadding(const void* const data, const unsigned int dataLe exit(-1); } - unsigned char paddingBytesAmount = *((unsigned char *)(data + dataLength - 1)); /* last byte contains length of data to be deleted */ - unpaddingResult->valueOfRemovedByteFromData = paddingBytesAmount; /* according to the PKCS7 */ - unpaddingResult->dataLengthWithoutPadding = dataLength - paddingBytesAmount; /* size of the final result */ + uint8_t paddingBytesAmount = *((uint8_t *)data + dataLength - 1); /* last byte contains length of data to be deleted */ + unpaddingResult->valueOfRemovedByteFromData = paddingBytesAmount; /* according to the PKCS7 */ + unpaddingResult->dataLengthWithoutPadding = dataLength - paddingBytesAmount; /* size of the final result */ - unsigned char* dataWithoutPadding = (unsigned char*) malloc(unpaddingResult->dataLengthWithoutPadding); + uint8_t* dataWithoutPadding = (uint8_t*) malloc(unpaddingResult->dataLengthWithoutPadding); if (NULL == dataWithoutPadding) { - perror("problem with unsigned char* dataWithoutPadding"); /* if memory allocation failed */ + perror("problem with uint8_t* dataWithoutPadding"); /* if memory allocation failed */ exit(-1); }