1 Commits
1.0.0 ... 1.0.1

Author SHA1 Message Date
GRISHNOV
0e2f68c1a0 added functions for freeing memory 2021-12-17 03:21:27 -08:00
5 changed files with 48 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
.PHONY: all clean
CC = gcc
CFLAGS = -c -Wall -Wextra -Wpedantic
CFLAGS = -c -I include -Wall -Wextra -Wpedantic
all: buildProject

View File

@@ -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

View File

@@ -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.
@@ -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

View File

@@ -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)
{
@@ -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);
}

View File

@@ -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)