Skip to content
Snippets Groups Projects
Commit 4d2797c7 authored by Dmitriy Gerasimov's avatar Dmitriy Gerasimov
Browse files

[+] DAP_DELETE macro

[*] Small fixes in ceil function
[+] sections and key-value read of config files
parent b0623cc5
No related branches found
No related tags found
1 merge request!24Support 3689
cmake_minimum_required(VERSION 2.8)
project (dap_core)
set(CORE_SRCS dap_common.c dap_config.c dap_math_ops.h)
add_library(${PROJECT_NAME} STATIC ${CORE_SRCS})
include_directories(common/)
set(CORE_SRCS dap_common.c dap_config.c common/memwipe.c)
set(CORE_HEADERS dap_common.h dap_config.h dap_math_ops.h common/memwipe.h common/int-util.h)
add_library(${PROJECT_NAME} STATIC ${CORE_SRCS} ${CORE_HEADERS})
target_link_libraries(${PROJECT_NAME} ev)
......
......@@ -11,6 +11,7 @@
#define DAP_NEW_Z(a) ( (a*) calloc(1,sizeof(a)))
#define DAP_NEW_Z_SIZE(a,b) ( (a*) calloc(1,b))
#define DAP_DELETE(a) free(a)
#define DAP_DUP(a) (__typeof(a) ret = memcpy(ret,a,sizeof(*a)) )
......
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "dap_common.h"
#include "dap_config.h"
#define LOG_TAG "dap_config"
typedef struct dap_config_internal
{
dap_config_item_t * root;
} dap_config_internal_t;
#define DAP_CONFIG_INTERNAL(a) ( (dap_config_internal_t* ) a->_internal )
char *s_configs_path = "/opt/dap/etc";
/**
* @brief dap_config_init
* @param a_configs_path
* @param a_configs_path If NULL path is set to default
* @return
*/
int dap_config_init(const char * a_configs_path)
{
if( a_configs_path ){
s_configs_path = strdup(a_configs_path);
char cmd[1024];
snprintf(cmd,sizeof(cmd),"test -d %s || mkdir -p %s",a_configs_path,a_configs_path);
system(cmd);
}
}
/**
......@@ -30,7 +47,137 @@ void dap_config_deinit()
*/
dap_config_t * dap_config_open(const char * a_name)
{
dap_config_t * ret = NULL;
if ( a_name ){
log_it(L_DEBUG,"Looking for config name %s...",a_name);
size_t l_config_path_size_max = strlen(a_name)+6+strlen(s_configs_path);
char *l_config_path = DAP_NEW_SIZE(char,l_config_path_size_max);
snprintf(l_config_path,l_config_path_size_max, "%s/%s.cfg",s_configs_path,a_name);
FILE * f = fopen(l_config_path,"r");
if ( f ){
log_it(L_DEBUG,"Opened config %s",a_name);
ret = DAP_NEW_Z(dap_config_t);
dap_config_internal_t * l_config_internal = DAP_NEW_Z(dap_config_internal_t);
ret->_internal = l_config_internal;
char buf[1024];
size_t l_global_offset=0;
size_t l_buf_size=0;
size_t l_buf_pos_line_start=0;
size_t l_buf_pos_line_end=0;
dap_config_item_t * l_section_current = NULL ;
bool l_is_space_now = false;
while ( feof(f)==0){ // Break on lines
size_t i;
l_global_offset += (l_buf_size = fread(buf,1,sizeof(buf),f) );
for (i=0; i< l_buf_size; i++){
if( (buf[i] == '\r') || (buf[i] == '\n' ) ){
if( ! l_is_space_now){
l_buf_pos_line_end = i;
l_is_space_now = true;
//if(l_buf_pos_line_end)
// l_buf_pos_line_end--;
if(l_buf_pos_line_end != l_buf_pos_line_start ){ // Line detected
char *l_line = NULL;
size_t l_line_length = 0;
size_t j;
// Trimming spaces and skip the line if commented
for ( j = l_buf_pos_line_start; j < l_buf_pos_line_end; j++ ){
if ( buf[j] == '#' )
break;
if (buf[j] != ' ' ){
l_line_length = (l_buf_pos_line_end - j);
break;
}
}
if( l_line_length ){
l_line = DAP_NEW_SIZE(char,l_line_length+1);
memcpy(l_line,buf+j,l_line_length);
l_line[l_line_length] = 0;
// Process trimmed line
if( (l_line[0] == '[' ) && (l_line[l_line_length-1] == ']' ) ){ // Section detected
//log_it(L_DEBUG, "Raw line '%s'",l_line);
char * l_section_name = strdup(l_line+1);
size_t l_section_name_length = (l_line_length - 2);
l_section_name[l_section_name_length]='\0';
log_it(L_DEBUG,"Config section '%s'",l_section_name);
dap_config_item_t * l_item = DAP_NEW_Z(dap_config_item_t);
l_item->header.name = l_section_name;
l_item->header.next = l_config_internal->root;
l_config_internal->root = l_item;
l_section_current = l_item;
}else{ // key-value line
//log_it(L_DEBUG,"Read line '%s'",l_line);
char l_param_name[256];
size_t l_param_name_size=0;
size_t l_param_value_size=0;
char l_param_value[1024];
l_param_name[0] = 0;
l_param_value[0] = 0;
for ( j = 0; j < l_line_length; j++ ){ // Parse param name
if ( ( l_line[j] == ' ' )|| ( l_line[j] == '=' ) ||( l_line[j] == '\t' ) ){ // Param name
l_param_name_size = j;
if (l_param_name_size > (sizeof(l_param_name) -1) ){
l_param_name_size = (sizeof(l_param_name) - 1 );
log_it(L_WARNING,"Too long param name in config, %u is more than %u maximum",
j,sizeof(l_param_name) -1);
}
strncpy(l_param_name,l_line,j);
l_param_name[j] = 0;
break;
}
}
for (; j < l_line_length; j++ ){ // Find beginning of param value
if ( ( l_line[j] != '\t' ) && ( l_line[j] != ' ' ) && ( l_line[j] != '=' ) ){
break;
}
}
l_param_value_size = l_line_length - j;
if (l_param_value_size ){
if (l_param_value_size > (sizeof(l_param_value) -1) ){
l_param_value_size = (sizeof(l_param_value) - 1 );
log_it(L_WARNING,"Too long param value in config, %u is more than %u maximum",
l_line_length - j,sizeof(l_param_value) -1);
}
strncpy(l_param_value,l_line +j, l_param_value_size);
l_param_value[l_param_value_size] = '\0';
for(j=l_param_value_size-1; j>=0; j--){
if( (l_param_value[j] ==' ') || (l_param_value[j] =='\t') ){
l_param_value[j] = '\0';
}else{
break;
}
}
}
log_it(L_DEBUG," Param '%s' = '%s'", l_param_name, l_param_value);
}
DAP_DELETE(l_line);
}
}
}
continue;
}else{
if (l_is_space_now){
l_is_space_now = false;
l_buf_pos_line_start = i;
}
}
}
}
}else{
log_it(L_ERROR,"Can't open config file '%s' (%s)",l_config_path,strerror(errno));
}
}else{
log_it(L_ERROR,"Config name is NULL");
}
return ret;
}
/**
......@@ -63,7 +210,7 @@ int32_t dap_config_get_item_int32(dap_config_t * a_config, const char * a_sectio
*/
const char * dap_config_get_item_str(dap_config_t * a_config, const char * a_section_path, const char * a_item_name)
{
return NULL;
}
/**
......
......@@ -2,7 +2,8 @@
#define _DAP_MATH_OPS_H_
#include <stdint.h>
#include "monero_crypto/common/int-util.h"
#include "common/int-util.h"
#if defined(__GNUC__) ||defined (__clang__)
#if __SIZEOF_INT128__ == 16
......
......@@ -3,7 +3,7 @@ project (dap_crypto)
set(CRYPTO_SRCS
dap_enc.c
dap_enc_base64.c
dap_enc_aes.c
dap_enc_newhope.c
......@@ -12,7 +12,7 @@ set(CRYPTO_SRCS
set(CRYPTO_HEADERS
dap_enc.h
dap_enc_base64.h
dap_enc_aes.h
dap_enc_newhope.h
......
......@@ -4,7 +4,7 @@
#include <ctype.h>
#include <stdlib.h>
#include "enc_base64.h"
#include "dap_enc_base64.h"
#define B64_TRUE 1
#define B64_FALSE 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment