Fixed maps and moved it to a new file

This commit is contained in:
Bazsalanszky 2019-10-16 20:45:19 +02:00
parent 95fa44feb5
commit 8dce51a0cf
Signed by: Bazsalanszky
GPG key ID: 0998CF5510B134D9
4 changed files with 107 additions and 47 deletions

54
modules/map.c Normal file
View file

@ -0,0 +1,54 @@
//
// Készítette: Toldi Balázs Ádám
// Dátum: 2019. 10. 16.
//
#include "map.h"
void map_init(map *m) {
m->length = 0;
m->size = 0;
m->pairs = 0;
}
bool map_isFound(map map, char* key){
for (int i = 0; i < map.length; ++i) {
if(strcmp(map.pairs[i].key,key) == 0)
return true;
}
return false;
}
char* map_getValue(map m, char* key){
for (int i = 0; i < m.length; ++i) {
if(strcmp(m.pairs[i].key,key) == 0)
return m.pairs[i].value;
}
return "UNDEFINED";
}
void map_dump(map m) {
for (int i = 0; i <m.length ; ++i) {
printf("%s %s\n",m.pairs[i].key,m.pairs[i].value);
}
}
void map_addPair(map* m,pair p) {
if (m->length >= m->size)
{
assert(m->length == m->size);
size_t new_size = (m->size + 2) * 2;
pair *new_list = realloc(m->pairs, new_size * sizeof(pair));
if (new_list == 0)
printf("OUT OF MEMORY!");
m->size = new_size;
m->pairs = new_list;
}
m->pairs[m->length++] = p;
}
pair map_make_pair(char *key,char *value) {
pair result;
strcpy(result.key,key);
strcpy(result.value,value);
return result;
}

39
modules/map.h Normal file
View file

@ -0,0 +1,39 @@
//
// Készítette: Toldi Balázs Ádám
// Dátum: 2019. 10. 16.
//
#pragma once
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct Pair{
char key[65];
char value[65];
}pair;
typedef struct Map
{
size_t size;
size_t length;
pair *pairs;
} map,config;
void initMap(map * m);
bool map_isFound(map m, char* key);
char* map_getValue(map m, char* key);
void map_addPair(map *m, pair p);
pair map_make_pair(char *key,char *value);
void map_init(map *m);
//Debug-hoz hasznos
void map_dump(map m);

View file

@ -19,7 +19,9 @@ char* generateSeed(int len){
result[len] = '\0';
return result;
}
map* getHandshakeData(char* text,int* len){
map getHandshakeData(char* text){
map result;
map_init(&result);
if (text[0] == '@')
memmove(text, text+1, strlen(text));
int count = 0;
@ -27,8 +29,6 @@ map* getHandshakeData(char* text,int* len){
if(text[i] == '&')
count++;
}
*len = count+1;
map * m = (map*) malloc(sizeof(map)*(*len));
int i =0;
const char c[2] = "&";
char *tmp;
@ -36,31 +36,16 @@ map* getHandshakeData(char* text,int* len){
while (tmp != NULL && i <count+1)
{
char key[50];
char value[50];
char key[65];
char value[65];
sscanf(tmp,"%[^=]=%s",key,value);
strcpy(m[i].key,key);
strcpy(m[i].value,value);
map_addPair(&result,map_make_pair(key,value));
++i;
tmp = strtok (NULL, "&");
}
return m;
}
bool map_isFound(map map[],int len, char* key){
for (int i = 0; i < len; ++i) {
if(strcmp(map[i].key,key) == 0)
return true;
}
return false;
return result;
}
char* map_getValue(map m[],int len, char* key){
for (int i = 0; i < len; ++i) {
if(strcmp(m[i].key,key) == 0)
return m[i].value;
}
return "UNDEFINED";
}
void md5(char *string, char outputBuffer[33]){
unsigned char hash[MD5_DIGEST_LENGTH];
MD5_CTX sha256;
@ -88,20 +73,14 @@ void logger_log(const char* _Format, ...){
strftime(buf, 26, "%Y.%m.%d. %H:%M:%S", tm_info);
char string[513];
printf("[%s]\t",buf);
fprintf(fp,"[%s]\t");
fprintf(fp,"[%s]\t",buf);
va_list args;
va_start (args, _Format);
vprintf(_Format,args);
vfprintf(fp,_Format,args);
va_end(args);
fprintf(fp,"\n");
printf(stdout,"\n");
printf("\n");
fclose(fp);
}
void map_dump(map *m, int len) {
for (int i = 0; i <len ; ++i) {
printf("%s %s\n",m[i].key,m[i].value);
}
}
}

View file

@ -2,36 +2,24 @@
// Készítette: Toldi Balázs Ádám
// Dátum: 2019. 10. 11.
//
#ifndef P2P_UTILITY_H
#define P2P_UTILITY_H
#pragma once
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <openssl/md5.h>
#include <ws2tcpip.h>
#endif
#include "modules/map.h"
#define DEFAULT_BUFLEN 1024
char* generateSeed(int len);
typedef struct Map{
char key[50];
char value[50];
} map;
map* getHandshakeData(char* text,int *len);
bool map_isFound(map m[],int len, char* key);
char* map_getValue(map m[],int len, char* key);
//Debug-hoz hasznos
void map_dump(map m[],int len);
map getHandshakeData(char* text);
void md5(char *string, char outputBuffer[33]);