From e36526d770b86164439a80800d661f67f65b40cf Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Sat, 28 Jun 2025 11:22:19 -0400 Subject: [PATCH] Ready to to utf-8 coding test --- lib/devsql.c | 71 ++++++++++++++++++++++++++++++++++++---------------- lib/devsql.h | 6 ++--- lib/unisql.c | 38 ++++++++++++++++------------ lib/unisql.h | 2 +- 4 files changed, 75 insertions(+), 42 deletions(-) diff --git a/lib/devsql.c b/lib/devsql.c index 67f60f3..177d14e 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -492,34 +492,58 @@ return (SQLPTR *)sql; /* (no character to be misunderstood by database) */ /* */ /********************************************************/ -PUBLIC char *sql_cleanstr(SQLPTR *sqlptr,char *str) +PUBLIC char *sql_goodkey(SQLPTR *sqlptr,char *key) { -#define OPEP "devsql.c:sql_cleanstr," -char *cleanstr; +#define OPEP "devsql.c:sql_goodkey," + +char *goodkey; +char *sqlkey; SQLTYP *sql; +int phase; +_Bool proceed; -cleanstr=(char *)0; +goodkey=(char *)0; +sqlkey=(char *)0; sql=(SQLTYP *)sqlptr; -if (str!=(char *)0) { - (void) sql_checkencoding(str); - switch(sql->sqldb) { - case (db_postgres) : - cleanstr=pos_cleanquote(str); +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //do we hav a goot data-base key + if ((key==(char *)0)||(strlen(key)==0)) { + (void) rou_alert(0,"%s dbkey is NULL or empty (Bug?)",OPEP); + phase=999; //trouble trouble + } break; - case (db_maria) : - cleanstr=mar_cleanquote(str); + case 1 : //checking the key encoding + if ((sqlkey=sql_checkencoding(key))==(char *)0) { + (void) rou_alert(0,"%s dbkey <%s> encoding is wrong (config?)",OPEP,key); + phase=999; //trouble trouble + } break; - case (db_unknown) : - default : - (void) rou_alert(0,"%s Unknown SQL daemon (type='%d')",OPEP,sql->sqldb); + case 2 : //cleaning quote according daemon type + switch(sql->sqldb) { + case (db_postgres) : + goodkey=pos_cleanquote(sqlkey); + break; + case (db_maria) : + goodkey=mar_cleanquote(sqlkey); + break; + case (db_unknown) : + default : + (void) rou_alert(0,"%s Unknown SQL daemon (type='%d')",OPEP,sql->sqldb); + break; + } + sqlkey=rou_freestr(sqlkey); + break; + default : //SAFE Guard + proceed=false; break; } + phase++; } -else - cleanstr=strdup("NULL"); -return cleanstr; - +return goodkey; #undef OPEP } /* @@ -536,13 +560,15 @@ PUBLIC _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr) { #define OPEP "devsql.c:sql_mngusr," -SQLTYP *sql; _Bool isok; +char *goodkey; +SQLTYP *sql; int phase; _Bool proceed; -sql=(SQLTYP *)sqlptr; isok=false; +goodkey=(char *)0; +sql=(SQLTYP *)sqlptr; phase=0; proceed=true; while (proceed==true) { @@ -552,7 +578,7 @@ while (proceed==true) { (void) rou_alert(0,"%s SQL pointer is NUll (Bug?)",OPEP); phase=999; } - if (sql_check_key(email)==false) { + if ((goodkey=sql_goodkey(sql,email))==(char *)0) { (void) rou_alert(0,"%s %s table, key <%s> not acceptable!", OPEP,EMAILS,email); phase=999; @@ -561,12 +587,13 @@ while (proceed==true) { case 1 : //getting user information switch (action) { case sql_select : - if ((*usr=select_user(sql,email))!=(USRTYP *)0) + if ((*usr=select_user(sql,goodkey))!=(USRTYP *)0) isok=true; break; default : (void) rou_alert(0,"%s action='%d' not yet implemented!",OPEP,action); } + goodkey=rou_freestr(goodkey); break; default : proceed=false; diff --git a/lib/devsql.h b/lib/devsql.h index 308df00..1ae647f 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -19,9 +19,9 @@ extern SQLPTR *sql_opensql(); //procedure to close an previously opened SQL channel extern SQLPTR *sql_closesql(SQLPTR *sqlptr); -//procedure to make sure a string with database -//is clean -extern char *sql_cleanstr(SQLPTR *sqlptr,char *str); +//procedure to make sure a string is acceptable +//as key search by database +extern char *sql_goodkey(SQLPTR *sqlptr,char *key); //procedure to get information on exiting user extern _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr); diff --git a/lib/unisql.c b/lib/unisql.c index a4f7205..184b34b 100644 --- a/lib/unisql.c +++ b/lib/unisql.c @@ -14,7 +14,7 @@ #include "subrou.h" #include "unisql.h" -#define DLANG "UNICODE" +#define DLANG "UTF-8" /* @@ -29,7 +29,7 @@ static char *cnvconvert(char *lfrom,char *lto,char *encoded) { -#define OPEP "rousql.c:cnvconvert," +#define OPEP "unisql.c:cnvconvert," char *converted; char *marker; @@ -141,34 +141,37 @@ return ses; /* dubious charactere with plain ASCII */ /* */ /************************************************/ -PUBLIC void sql_checkencoding(char *strencoded) +PUBLIC char *sql_checkencoding(char *strencoded) { +#define OPEP "unisql.c:sql_checkencoding," + +char *converted; char *ptr; int phase; int proceed; +converted=(char *)0; ptr=(char *)0; phase=0; proceed=true; while (proceed==true) { switch (phase) { - case 0 : /*autoconvert string */ + case 0 : //set the destination convertion if (((ptr=getenv("DB_LANG"))==(char *)0)||(strlen(ptr)==0)) ptr=DLANG; - if ((ptr=cnvconvert(ptr,ptr,strencoded))!=(char *)0) { - (void) free(ptr); - phase=999; /*everything fine */ - } break; - case 1 : /*setting to plain ASCII*/ - if (strencoded!=(char *)0) { /*alway */ - while (*strencoded!='\000') { - if (isascii(*strencoded)==0) - *strencoded='_'; - strencoded++; - } - } + case 1 : + if ((converted=cnvconvert(DLANG,ptr,strencoded))!=(char *)0) + phase=999; //everything fine converting to DB_LANG + break; + case 2 : //converting in very plain ascii + if ((converted=cnvconvert(DLANG,ptr,strencoded))!=(char *)0) + phase=999; //simple char now + break; + case 3 : //conversion trouble + (void) rou_alert(0,"%s Wrong encoding for db access key <%s> (Config?)", + OPEP,strencoded); break; default : /*SAFE Guard */ proceed=false; @@ -176,6 +179,9 @@ while (proceed==true) { } phase++; } +return converted; + +#undef OPEP } /* diff --git a/lib/unisql.h b/lib/unisql.h index 45fba33..764a16f 100644 --- a/lib/unisql.h +++ b/lib/unisql.h @@ -42,7 +42,7 @@ extern USRTYP *sql_freeusr(USRTYP *usr); extern SESTYP *sql_freeses(SESTYP *ses); //Procedure to check if string is properly coded according DB_LANG -extern void sql_checkencoding(char *strencoded); +extern char *sql_checkencoding(char *strencoded); //Procedure to check if a SQL key is acceptable extern _Bool sql_check_key(char *key); -- 2.47.3