From 0e2f68c1a06e83c5808e3c6a9a974eb3ca34ac00 Mon Sep 17 00:00:00 2001 From: GRISHNOV Date: Fri, 17 Dec 2021 03:21:27 -0800 Subject: [PATCH] added functions for freeing memory --- Makefile | 2 +- README.md | 19 +++++++++++++++++-- include/PKCS7.h | 13 +++++++++++++ src/PKCS7.c | 14 +++++++++++++- test/PKCS7_test.c | 4 ++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index f197775..27bb691 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: all clean CC = gcc -CFLAGS = -c -Wall -Wextra -Wpedantic +CFLAGS = -c -I include -Wall -Wextra -Wpedantic all: buildProject diff --git a/README.md b/README.md index f50b29b..8a4d279 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ typedef struct { /* Applies PKCS7 padding to data. - Your data at the provided address does not change. A copy is created, to which the adding padding is applied. + 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 uint64_t dataLength, const uint8_t BLOCK_SIZE); @@ -78,7 +79,8 @@ typedef struct { /* 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. + 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 uint64_t dataLength); ``` @@ -96,6 +98,19 @@ typedef enum { BLOCK_SIZE_CUSTOM_VALUE = 0 /* you can set your own constant to use */ } paddingBlockSize; /* can be used as third argument to the function addPadding() */ ``` +When finished, use the following functions to free memory. + +```C +/* + Frees the memory that was allocated for padding structure. +*/ +void freePaddingResult(PKCS7_Padding* puddingResult); + +/* + Frees the memory that was allocated for unpadding structure. +*/ +void freeUnPaddingResult(PKCS7_unPadding* unPuddingResult); +``` # Demonstration diff --git a/include/PKCS7.h b/include/PKCS7.h index 0a74b47..f561f6b 100644 --- a/include/PKCS7.h +++ b/include/PKCS7.h @@ -1,6 +1,8 @@ #ifndef PKCS7_H #define PKCS7_H +#include + /* Examples of commonly used block sizes for data padding. WARNING: block size for PKCS7 padding can be 0 < BLOCK_SIZE < 256 bytes. @@ -46,4 +48,15 @@ typedef struct { */ PKCS7_unPadding* removePadding(const void* const data, const uint64_t dataLength); + +/* + Frees the memory that was allocated for padding structure. +*/ +void freePaddingResult(PKCS7_Padding* puddingResult); + +/* + Frees the memory that was allocated for unpadding structure. +*/ +void freeUnPaddingResult(PKCS7_unPadding* unPuddingResult); + #endif // PKCS7_H \ No newline at end of file diff --git a/src/PKCS7.c b/src/PKCS7.c index af15bd3..0229176 100644 --- a/src/PKCS7.c +++ b/src/PKCS7.c @@ -2,7 +2,7 @@ #include #include #include -#include "./../include/PKCS7.h" +#include "PKCS7.h" PKCS7_Padding* addPadding(const void* const data, const uint64_t dataLength, const uint8_t BLOCK_SIZE) { @@ -64,4 +64,16 @@ PKCS7_unPadding* removePadding(const void* const data, const uint64_t dataLength unpaddingResult->dataWithoutPadding = dataWithoutPadding; return unpaddingResult; +} + +void freePaddingResult(PKCS7_Padding* puddingResult) +{ + free(puddingResult->dataWithPadding); + free(puddingResult); +} + +void freeUnPaddingResult(PKCS7_unPadding* unPuddingResult) +{ + free(unPuddingResult->dataWithoutPadding); + free(unPuddingResult); } \ No newline at end of file diff --git a/test/PKCS7_test.c b/test/PKCS7_test.c index 87928af..762f22c 100644 --- a/test/PKCS7_test.c +++ b/test/PKCS7_test.c @@ -123,6 +123,10 @@ void demonstrationPaddingOnTestInput(const uint8_t* const testDataExample, const ptrToUnpaddingDataResult++; } printf("\n\n************************************\n\n"); + + freePaddingResult(structWithPaddingResult); + freeUnPaddingResult(structWithUnpaddingResult); + free(testData); } void printDescription(void)