Skip to content

Commit

Permalink
added functions for freeing memory
Browse files Browse the repository at this point in the history
  • Loading branch information
GRISHNOV committed Dec 17, 2021
1 parent 0fcb944 commit 0e2f68c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: all clean

CC = gcc
CFLAGS = -c -Wall -Wextra -Wpedantic
CFLAGS = -c -I include -Wall -Wextra -Wpedantic

all: buildProject

Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
```
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions include/PKCS7.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef PKCS7_H
#define PKCS7_H

#include <stdint.h>

/*
Examples of commonly used block sizes for data padding.
WARNING: block size for PKCS7 padding can be 0 < BLOCK_SIZE < 256 bytes.
Expand Down Expand Up @@ -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
14 changes: 13 additions & 1 deletion src/PKCS7.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "./../include/PKCS7.h"
#include "PKCS7.h"

PKCS7_Padding* addPadding(const void* const data, const uint64_t dataLength, const uint8_t BLOCK_SIZE)
{
Expand Down Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions test/PKCS7_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0e2f68c

Please sign in to comment.