Skip to content

Commit 0e2f68c

Browse files
committed
added functions for freeing memory
1 parent 0fcb944 commit 0e2f68c

5 files changed

Lines changed: 48 additions & 4 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.PHONY: all clean
22

33
CC = gcc
4-
CFLAGS = -c -Wall -Wextra -Wpedantic
4+
CFLAGS = -c -I include -Wall -Wextra -Wpedantic
55

66
all: buildProject
77

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ typedef struct {
5757

5858
/*
5959
Applies PKCS7 padding to data.
60-
Your data at the provided address does not change. A copy is created, to which the adding padding is applied.
60+
Your data at the provided address does not change.
61+
A copy is created, to which the adding padding is applied.
6162
WARNING: use only 0 < BLOCK_SIZE < 256
6263
*/
6364
PKCS7_Padding* addPadding(const void* const data, const uint64_t dataLength, const uint8_t BLOCK_SIZE);
@@ -78,7 +79,8 @@ typedef struct {
7879
7980
/*
8081
Remove PKCS7 padding from data.
81-
Your data at the provided address does not change. A copy is created, to which the removing padding is applied.
82+
Your data at the provided address does not change.
83+
A copy is created, to which the removing padding is applied.
8284
*/
8385
PKCS7_unPadding* removePadding(const void* const data, const uint64_t dataLength);
8486
```
@@ -96,6 +98,19 @@ typedef enum {
9698
BLOCK_SIZE_CUSTOM_VALUE = 0 /* you can set your own constant to use */
9799
} paddingBlockSize; /* can be used as third argument to the function addPadding() */
98100
```
101+
When finished, use the following functions to free memory.
102+
103+
```C
104+
/*
105+
Frees the memory that was allocated for padding structure.
106+
*/
107+
void freePaddingResult(PKCS7_Padding* puddingResult);
108+
109+
/*
110+
Frees the memory that was allocated for unpadding structure.
111+
*/
112+
void freeUnPaddingResult(PKCS7_unPadding* unPuddingResult);
113+
```
99114
100115
# Demonstration
101116

include/PKCS7.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef PKCS7_H
22
#define PKCS7_H
33

4+
#include <stdint.h>
5+
46
/*
57
Examples of commonly used block sizes for data padding.
68
WARNING: block size for PKCS7 padding can be 0 < BLOCK_SIZE < 256 bytes.
@@ -46,4 +48,15 @@ typedef struct {
4648
*/
4749
PKCS7_unPadding* removePadding(const void* const data, const uint64_t dataLength);
4850

51+
52+
/*
53+
Frees the memory that was allocated for padding structure.
54+
*/
55+
void freePaddingResult(PKCS7_Padding* puddingResult);
56+
57+
/*
58+
Frees the memory that was allocated for unpadding structure.
59+
*/
60+
void freeUnPaddingResult(PKCS7_unPadding* unPuddingResult);
61+
4962
#endif // PKCS7_H

src/PKCS7.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <stdlib.h>
33
#include <string.h>
44
#include <stdint.h>
5-
#include "./../include/PKCS7.h"
5+
#include "PKCS7.h"
66

77
PKCS7_Padding* addPadding(const void* const data, const uint64_t dataLength, const uint8_t BLOCK_SIZE)
88
{
@@ -64,4 +64,16 @@ PKCS7_unPadding* removePadding(const void* const data, const uint64_t dataLength
6464
unpaddingResult->dataWithoutPadding = dataWithoutPadding;
6565

6666
return unpaddingResult;
67+
}
68+
69+
void freePaddingResult(PKCS7_Padding* puddingResult)
70+
{
71+
free(puddingResult->dataWithPadding);
72+
free(puddingResult);
73+
}
74+
75+
void freeUnPaddingResult(PKCS7_unPadding* unPuddingResult)
76+
{
77+
free(unPuddingResult->dataWithoutPadding);
78+
free(unPuddingResult);
6779
}

test/PKCS7_test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ void demonstrationPaddingOnTestInput(const uint8_t* const testDataExample, const
123123
ptrToUnpaddingDataResult++;
124124
}
125125
printf("\n\n************************************\n\n");
126+
127+
freePaddingResult(structWithPaddingResult);
128+
freeUnPaddingResult(structWithUnpaddingResult);
129+
free(testData);
126130
}
127131

128132
void printDescription(void)

0 commit comments

Comments
 (0)