This commit is contained in:
Liam Waldron 2025-03-08 22:27:53 -05:00
parent f94ed28d93
commit 60996e3de2
2 changed files with 113 additions and 250 deletions

View File

@ -1,245 +0,0 @@
/*
* data.c - Data structure functions
*
* This file is part of libglacier.
*
* Liblacier is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* Libglacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Libglacier. If
* not, see <https://www.gnu.org/licenses/>.
*/
/* ALL FUNCTIONS IN THIS FILE DEFINED IN: data.h */
/*
* node
*
* DESCRIPTION: Definition of node type.
* DEFINED IN: data.h
*
*/
struct node {
char *data;
struct node *children[MAX_CHILDREN];
int numChildren;
};
/*
* create_node
*
* DESCRIPTION: Create a dependency tree node.
* PARAMETERS: char *data
* DEFINED IN: data.h
*
*/
struct node
*create_node(char *data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = strdup(data);
newNode->numChildren = 0;
return newNode;
}
/*
* add_child
*
* DESCRIPTION: Add a child node to a parent node.
* PARAMETERS: struct node *parent, struct node *child
* DEFINED IN: data.h
*
*/
void
add_child(struct node *parent, struct node *child)
{
if (parent->numChildren < MAX_CHILDREN) {
parent->children[parent->numChildren++] = child;
} else {
if (LG_VERBOSE == 1) { errlog("Maximum number of children exceeded"); }
exit(1);
}
}
/*
* print_tree
*
* DESCRIPTION: Print a dependency tree.
* PARAMETERS: struct node *root, int level
* DEFINED IN: data.h
*
*/
void
print_tree(struct node *root, int level)
{
if (root == NULL) {
return;
}
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%s\n", root->data);
for (int i = 0; i < root->numChildren; i++) {
print_tree(root->children[i], level + 1);
}
}
/*
* queue
*
* DESCRIPTION: Definition of queue type.
* DEFINED IN: data.h
*
*/
typedef struct {
int items[MAX_SIZE];
int front;
int rear;
} queue;
/*
* init_queue
*
* DESCRIPTION: Initialize a queue.
* PARAMETERS: queue *q
* DEFINED IN: data.h
*
*/
void
init_queue(queue *q)
{
q -> front = -1;
q -> rear = 0;
}
/*
* queue_is_empty
*
* DESCRIPTION: Check if queue is empty.
* PARAMETERS: struct node *root, int level
* DEFINED IN: data.h
*
*/
bool queue_is_empty(queue *q) { return (q -> front == q -> rear -1); }
/*
* queue_is_full
*
* DESCRIPTION: Check if queue is full.
* PARAMETERS: queue *q
* DEFINED IN: data.h
*
*/
bool queue_is_full(queue *q) { return (q -> rear == MAX_SIZE); }
/*
* enqueue
*
* DESCRIPTION: Enqueue an element at the back of the queue.
* PARAMETERS: queue *q, int value
* DEFINED IN: data.h
*
*/
void
enqueue (queue *q, int value)
{
if (queue_is_full(q)) {
printf("Queue is full\n");
return;
}
q -> items[q -> rear] = value;
q -> rear++;
}
/*
* dequeue
*
* DESCRIPTION: Dequeue the element at the front of the queue.
* PARAMETERS: queue *q, int value
* DEFINED IN: data.h
*
*/
void
dequeue(queue *q)
{
if (queue_is_empty(q)) {
printf("Queue is empty\n");
return;
}
q -> front++;
}
/*
* peek
*
* DESCRIPTION: View the element at the front of the queue.
* PARAMETERS: struct node *root, int level
* DEFINED IN: data.h
*
*/
int
peek(queue *q)
{
if (queue_is_empty(q)) {
printf("Queue is empty\n");
return -1;
}
return q -> items[q -> front + 1];
}
/*
* print_queue
*
* DESCRIPTION: Print the queue.
* PARAMETERS: queue *q
* DEFINED IN: data.h
*
*/
void
print_queue(queue *q)
{
if (queue_is_empty(q)) {
printf("Queue is empty\n");
return;
}
printf("Current Queue: ");
for (int i = q -> front + 1; i < q -> rear; i++) {
printf("%d ", q -> items[i]);
}
printf("\n");
}
/*
* mkworkspace
*
* DESCRIPTION: Creates a new Glacier workspace in /tmp.
* PARAMETERS: None.
* DEFINED IN: pkgops.h
*
*/

View File

@ -639,7 +639,16 @@ run_make_task(char TASK[])
}
}
void
/*
* print_hash
*
* DESCRIPTION: Prints a specified hash string
* PARAMETERS: unsigned char *hash, unsigned int length
* DEFINED IN: security.h
*
*/
int
print_hash(uchar *hash, uint length)
{
for (uint index = 0; index < length; index++) {
@ -647,14 +656,113 @@ print_hash(uchar *hash, uint length)
}
printf("\n");
return 0;
}
void
stash_hash(uchar *hash, uint length)
/*
* stash_hash
*
* DESCRIPTION: Stores a hash inside a string
* PARAMETERS: unsigned char *stored_hash, unsigned char *hash, unsigned int length
* DEFINED IN: security.h
*
*/
int
stash_hash(uchar *stored_hash, uchar *hash, uint length)
{
char stored_hash[32];
if (sizeof(stored_hash) != 32 && LG_VERBOSE == 1) {
errlog("in stash_hash()");
errlog("size of stored hash string must equal 32 bytes");
return 1;
}
else if (sizeof(stored_hash) != 32) {
return 1;
}
uint max_size_of_input = 3;
for (uint index = 0; index < length; index++) {
sprintf(stored_hash, "%02x", hash[index]);
if (hash[index] > max_size_of_input) {
errlog("in stash_hash()");
errlog("size of input exceeds maximum allowed size (hash[index] > 3)");
return 1;
}
snprintf(stored_hash[index], max_size_of_input, "%02x", hash[index]);
}
return 0;
}
/*
* hash_file
*
* DESCRIPTION: Performs a hashing operation on a file and stores the result
* PARAMETERS: unsigned char *h
* DEFINED IN: security.h
*
*/
int
hash_file(const char *filename, uchar *destination_string)
{
FILE *data = fopen(filename, "rb");
if (! data && LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error opening file handle");
return 1;
}
else if (! data) {
return 1;
}
EVP_MD_CTX *context = EVP_MD_CTX_new();
if (! context && LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error creating envelope context");
return 2;
}
else if (! context) {
return 2;
}
if (EVP_DigestInit_ex(context, EVP_sha256(), NULL) != 1) {
if (LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error initializing digest");
}
EVP_MD_CTX_free(context);
fclose(data);
return 3;
}
if (ferror(data)) {
if (LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error reading file");
}
EVP_MD_CTX_free(context);
fclose(data);
return 4;
}
fclose(data);
if (sizeof(destination_hash) != EVP_MAX_MD_SIZE && LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("destination hash is invalid size");
errlog("ensure hash is declared with size EVP_MAX_MD_SIZE");
return 5;
}
else if (sizeof(destination_hash) != EVP_MAX_MD_SIZE) {
return 5;
}
uint hash_length;
if (EVP_DigestFinal_ex(context, destination_hash, &hash_length) != 1) {
EVP_MD_CTX_free(context);
return 6;
}
}