From 1ed4114bb3b9e701dcf6b2d8f1c6da2b47979d4d Mon Sep 17 00:00:00 2001 From: M.Gergő Date: Fri, 8 May 2020 21:10:20 +0200 Subject: escaping --- src/ini_read.c | 59 +++++++++++++++++++++++++++++++++------------------------ src/ini_rw.h | 4 ++-- src/main.c | 30 ++++++++++++++++++++++++++--- tests/test.ini | 3 +-- tests/test0.ini | 11 +++++++---- 5 files changed, 71 insertions(+), 36 deletions(-) diff --git a/src/ini_read.c b/src/ini_read.c index d4d535d..ac23548 100644 --- a/src/ini_read.c +++ b/src/ini_read.c @@ -19,12 +19,13 @@ struct lci_data fsmdata; size_t strNullLen(const char *str){ if(str == NULL){ return 0; - } else { + } else { return strlen(str); } } char *strResize(char *ptr, size_t oldsize, size_t newsize){ + //return ptr; char *tmp; if(newsize == 0){ //pucol @@ -33,23 +34,22 @@ char *strResize(char *ptr, size_t oldsize, size_t newsize){ } else if(newsize != oldsize){ //Változtat tmp = (char *) malloc(newsize*sizeof(char)); + memset(tmp, 0, newsize); + if(tmp == NULL){ //Hiba esetén nem nyúl hozzá return ptr; } else if(ptr == NULL) { - memset(tmp, 0, newsize); - ptr = tmp; + //memset(tmp, 0, newsize); return tmp; } else if(newsize > oldsize) { // FEL - memset(tmp, 0, newsize); + //memset(tmp, 0, newsize); strncpy(tmp, ptr, oldsize); // old < new - free(ptr); - ptr = tmp; + free(ptr); return tmp; } else if(newsize < oldsize){ //LE - memset(tmp, 0, newsize); + //memset(tmp, 0, newsize); strncpy(tmp, ptr, newsize); // new < old free(ptr); - ptr = tmp; return tmp; } else { //Ide sosem jutunk return ptr; @@ -76,17 +76,8 @@ char unescape(char c){ return '\v'; } else if(c == 'e'){ return 0x1B; - /* } else if(c == '\\'){ //Backslash - return '\\'; - } else if(c == '\''){ //Apostrophe - return '\''; - } else if(c == '\"'){ //Double quotation mark - return '\"'; - } else if(c == '?'){ //Question mark - return '?'; - */ - } else if(c < 0x20){ - return '~'; +/* } else if(c < 0x20){ + return '~'; */ } else { // jó az eredeti return c; } @@ -148,7 +139,7 @@ int64_t getFileMaxLineLen(FILE *tfd){ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ - int64_t i,j; + int64_t i,j; enum ini_states pstate=Start, state=Start; char cc; @@ -161,10 +152,12 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ switch (state) { case Start: + if(data->nodeState == MULTILINE){ //Elküldjük gyűjteni state = DqmW; // Vagy ValW ?? j = data->valueLen-2; //(x-1) => \0; (x-2) => j--; data->value = strResize(data->value, data->valueLen, data->valueLen+len); // strResize(ptr, oldsize, newsize) + data->valueLen += len; i--; } else if(in[i] == '\n' || in[i] == '\r' || in[i] == '\0'){ //sorvége state = Stop; // Sorvége @@ -175,6 +168,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); + data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->sectionStartPos = -1; //Hogy pucoljon @@ -182,12 +176,14 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = SectEndW; data->section = strResize(data->section, data->sectionLen, len); + data->sectionLen = len; memset(data->section, 0, len); data->sectionStartPos = i; //zárójeleket is veszi! } else if(isalnum(in[i]) ){ //Változó jön j = -1; state = EqW1; data->param = strResize(data->param, data->paramLen, len); + data->paramLen = len; memset(data->param, 0, len); data->paramStartPos = i; i--; @@ -208,6 +204,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); + data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->sectionStartPos = -1; //Hogy pucoljon @@ -215,12 +212,14 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = SectEndW; data->section = strResize(data->section, data->sectionLen, len); + data->sectionLen = len; memset(data->section, 0, len); data->sectionStartPos = i; } else if (isalnum(in[i])){ //Változó lesz belőle j = -1; state = EqW1; data->param = strResize(data->param, data->paramLen, len); + data->paramLen = len; memset(data->param, 0, len); data->paramStartPos = i; i--; @@ -281,6 +280,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); + data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; @@ -334,6 +334,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); + data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; @@ -342,6 +343,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = DqmW; data->value = strResize(data->value, data->valueLen, len); + data->valueLen = len; memset(data->value, 0, len); data->valueStartPos = i; data->nodeState = CONTINUE; @@ -351,6 +353,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = ValW; data->value = strResize(data->value, data->valueLen, len); + data->valueLen = len; memset(data->value, 0, len); data->valueStartPos = i; data->nodeState = CONTINUE; @@ -377,6 +380,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ state = CommEndW; j = -1; data->comment = strResize(data->comment, data->commentLen, len); + data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; @@ -431,6 +435,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); + data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; @@ -445,7 +450,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case DqmW: //dupla idézőjelben az érték - if(pstate != Bslsh && in[i] == '\"'){ //újabb idézőjel -> string vége + if(/*pstate != Bslsh &&*/ in[i] == '\"'){ //újabb idézőjel -> string vége data->value[j] = '\0'; data->valueLen = j+1; data->nodeState = READY; @@ -478,6 +483,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case Error: data->errorMsg = strResize(data->errorMsg, data->errorMsgLen, 256); + data->errorMsgLen = 256; memset(data->errorMsg, 0, 256) ; if(pstate == SectEndW || pstate == SectEndD){ @@ -502,7 +508,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ if(in[i] == '\n' || in[i] == '\r' || in[i] == '\0' || pstate == Error){ //Sorvége, maradunk state = Stop; if(strNullLen(data->section) == 0){data->sectionLen = 0;} - if(strNullLen(data->param) == 0){data->paramLen = 0;} + if(/*data->nodeState != MULTILINE &&*/ strNullLen(data->param) == 0){data->paramLen = 0;} if(strNullLen(data->value) == 0){data->valueLen = 0;} if(strNullLen(data->comment) == 0){data->commentLen = 0;} if(strNullLen(data->errorMsg) == 0){data->errorMsgLen = 0;} @@ -622,8 +628,8 @@ struct lci_data *iniReadOut(const char *filename){ if(list == NULL){ list = curr; } - if(prev != NULL && prev->section != NULL && curr != NULL && curr->section != NULL){ - strncpy(curr->section, prev->section, curr->lineLen); + if(prev != NULL && prev != curr && prev->section != NULL && curr != NULL && curr->section != NULL){ + strncpy(curr->section, prev->section, curr->sectionLen); curr->sectionLen = prev->sectionLen; } if(curr != NULL){ @@ -650,11 +656,14 @@ struct lci_data *iniReadOut(const char *filename){ prev = curr; } else { //Az előzőhöz hozzávesszük a mostanit prev->next = curr; - if(prev->next != NULL){ //Csak, ha lett új felvéve + if(prev->next != NULL && curr->nodeState != MULTILINE){ //Csak, ha lett új felvéve prev = prev->next; } } + if(curr != NULL && curr->nodeState == ERROR){ + return list; + } pos = 0; memset(buff, 0, linemax); } else { diff --git a/src/ini_rw.h b/src/ini_rw.h index 10b21d3..9a84c10 100644 --- a/src/ini_rw.h +++ b/src/ini_rw.h @@ -2,7 +2,7 @@ #define INI_READ_H_INCLUDED // #include "deftypes.h" #include /* int64_t*/ - + typedef struct lci_data { @@ -12,7 +12,7 @@ typedef struct lci_data { int64_t lineLen; char *section; - int64_t sectionLen; + int64_t sectionLen; int64_t sectionStartPos; char *param; int64_t paramLen; diff --git a/src/main.c b/src/main.c index 72f1718..9aa15e5 100644 --- a/src/main.c +++ b/src/main.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include /* PATH_MAX*/ #include "ini_rw.h" @@ -26,12 +26,36 @@ int main() buff3 = malloc((len+3)*sizeof(char)); buff4 = malloc((len+3)*sizeof(char)); buff5 = malloc((elen+3)*sizeof(char)); +memset(buff1, 0, (len+3)*sizeof(char)); +memset(buff2, 0, (len+3)*sizeof(char)); +memset(buff3, 0, (len+3)*sizeof(char)); +memset(buff4, 0, (len+3)*sizeof(char)); +memset(buff5, 0, (elen+3)*sizeof(char)); + char *aa, *bb, *cc; + aa = (char *) malloc(20*sizeof(char)); + aa[0] = 'q'; + aa[1] = 'w'; + aa[2] = 'e'; + aa[3] = '\0'; + bb = (char *) malloc(30*sizeof(char)); + + cc=aa; + strncpy(bb,aa,20); + free(aa); + free(NULL); + + printf("\n%s\n", bb); + + - ini = iniReadOut(filename); + - printf("\n"); + + printf("\nLineMax: %d\n\n",len); + + ini = iniReadOut(filename); lens=18; lenp=18; diff --git a/tests/test.ini b/tests/test.ini index 46710f0..c7094a5 100644 --- a/tests/test.ini +++ b/tests/test.ini @@ -21,8 +21,7 @@ parameter2 = "I like \t emojis 😍 but, don't like mosquitoes カ ! :)" ; Even! parameter3 = "With double quotation mark \ I can write multiline values, but I shoud \ -escapse the trailing newline with '\' sign! -" +escapse the trailing newline with '\' sign!" [A B€BCD] ghijkl=val1 ; komment diff --git a/tests/test0.ini b/tests/test0.ini index 65dc41c..1707147 100644 --- a/tests/test0.ini +++ b/tests/test0.ini @@ -1,11 +1,14 @@ [A BBCD] ad = "Withkdoublekque\ -alffsdasdabbbbbbbbbbbf\ -wqeweaaaaaaaaaaaa111aa\ -aabcd=efgh !" +alffsdasdabbbpbbbkbbbf\ \;sadasd \ +asdasdasdasdssssssssddssaaaddé éŁ fdfsadwaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaerwerasdasdasdassdt\ +aabcd=efgh !\ " +; +; +;yyyyyyyyyyyyyyyyyyyyyyyyyyyyy ppp = aaa -;parameter3 = "With double quotation mark \I can write multiline values, but I shoud \ escapse the trailing newline with '\' sign!" +;parameter3 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsign!" ghijkl=val1 ; komment ghijkl = val2 ;kom1 -- cgit v1.2.3