]> bicyclesonthemoon.info Git - ott/enhance/blobdiff - pal-cgi.c
palette tools online
[ott/enhance] / pal-cgi.c
index 0ffe0dcd5aadd8ccf26870d268730c2166cb2df0..0e7e4013a355ed0e45077998374118e06ccee177 100644 (file)
--- a/pal-cgi.c
+++ b/pal-cgi.c
-// insertframe.c\r
-// The online interface for the tools to insert and extract indexed images\r
-// 25.06.2015\r
-// \r
-// Copyright (C) 2015  Balthasar Szczepański\r
-// \r
-// This program is free software: you can redistribute it and/or modify\r
-// it under the terms of the GNU Affero General Public License as\r
-// published by the Free Software Foundation, either version 3 of the\r
-// License, or (at your option) any later version.\r
-// \r
-// This program is distributed in the hope that it will be useful,\r
-// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-// GNU Affero General Public License for more details.\r
-// \r
-// You should have received a copy of the GNU Affero General Public License\r
-// along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
-// \r
+/*\r
+pal-cgi.c\r
+The online interface for the tools to insert and extract indexed images\r
+05.12.2022\r
 \r
-// Requires cgilib (http://www.infodrom.org/projects/cgilib/)\r
-        \r
-#include <cgi.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <unistd.h>\r
-#include <string.h>\r
-#include <sys/wait.h>\r
-#include <sys/stat.h>\r
+Copyright (C) 2015, 2022  Balthasar Szczepański\r
 \r
-#define WGET_PATH  "/usr/bin/wget"\r
-#define INS_PATH   "./insert"\r
-#define EXT_PATH   "./extract"\r
-#define DIF_PATH   "./seediff"\r
+This program is free software: you can redistribute it and/or modify\r
+it under the terms of the GNU Affero General Public License as\r
+published by the Free Software Foundation, either version 3 of the\r
+License, or (at your option) any later version.\r
 \r
-#define RM_PATH    "/bin/rm"\r
-#define LOG_PATH   "/eizm/log/insertframe/insertframe.log"\r
-#define USERAGENT  "Hidden frames. (1190.bicyclesonthemoon.dnsd.info/insertframe/insertframe.htm)"\r
-#define MSTD_PATH  "/pro/insertframe/nclr.png"\r
+This program is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+GNU Affero General Public License for more details.\r
 \r
-int main (int argc, char *argv[]);\r
-void mustard(int mustard);\r
+You should have received a copy of the GNU Affero General Public License\r
+along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
 \r
-FILE *pix;\r
-unsigned short act;\r
+Requires cgilib (http://www.infodrom.org/projects/cgilib/)\r
+*/\r
 \r
+#include <stdio.h>\r
+#include <stdint.h>\r
+#include <unistd.h>\r
+#include <stdlib.h>\r
+#include <sys/wait.h>\r
+#include <stdint.h>\r
+#include <inttypes.h>\r
+\r
+#include <cgi.h>\r
+\r
+#include "online-core.h"\r
+#include "nclr.h"\r
 \r
-char url0[1024];\r
-char path0[256];\r
-char url1[1024];\r
-char path1[256];\r
-s_cgi *cgi;\r
-int t;\r
-pid_t sub;\r
+#define INSERT     0\r
+#define EXTRACT_H  1\r
+#define EXTRACT_V  2\r
+#define DETECT     3\r
+#define MAX_ACTION DETECT\r
 \r
+int insert(\r
+       char *inpix0, char *inpix1, char *outpix,\r
+       uint_fast8_t r0, uint_fast8_t r1\r
+);\r
+int extract(\r
+       char *inpix, char *outpix0, char *outpix1,\r
+       uint_fast8_t size_defined,\r
+       uint_fast8_t p0, uint_fast8_t p1\r
+);\r
+int detect(\r
+       char *inpix, char *outpix,\r
+       uint_fast8_t size_defined,\r
+       uint_fast8_t p0, uint_fast8_t p1\r
+);\r
 \r
 int main (int argc, char *argv[])\r
 {\r
-       unsigned char buf[1024];\r
-       struct stat st;\r
-       unsigned long p, i;\r
-       unsigned short q;\r
-       char **up;\r
+       s_cgi *cgi;\r
+       char tmp0[256];\r
+       char tmp1[256];\r
+       char tmp2[256];\r
+       char *in0_path;\r
+       char *in1_path;\r
        \r
-       // unsigned short r0, r1;\r
-       unsigned char n0=0;\r
-       unsigned char n1=0;\r
-\r
-       cgi=cgiInit();\r
+       char *t;\r
+       uint_fast8_t p0, p1;\r
+       uint_fast8_t size_defined = 0;\r
+       uint_fast8_t action;\r
        \r
-       sprintf(path0,"%s",(cgiGetValue(cgi,"a")!=NULL)?(cgiGetValue(cgi,"a")):"0");\r
-       act=atoi(path0);\r
-       // sprintf(path0,"%s",(cgiGetValue(cgi,"r0")!=NULL)?(cgiGetValue(cgi,"r0")):"1");\r
-       // r0=atoi(path0);\r
-       // sprintf(path0,"%s",(cgiGetValue(cgi,"r1")!=NULL)?(cgiGetValue(cgi,"r1")):"0");\r
-       // r1=atoi(path0);\r
-\r
-       up=cgiGetFiles(cgi);\r
-       if (up)\r
-       {\r
-               if (cgiGetFile(cgi,up[0])!=NULL)\r
-               {\r
-                       sprintf(path0,"%s",cgiGetFile(cgi,up[0])->tmpfile);\r
-                       sprintf(url0,"%s",cgiGetFile(cgi,up[0])->filename);\r
-                       n0=1;\r
-               }\r
-               if (cgiGetFile(cgi,up[1])!=NULL)\r
-               {\r
-                       sprintf(path1,"%s",cgiGetFile(cgi,up[1])->tmpfile);\r
-                       sprintf(url1,"%s",cgiGetFile(cgi,up[1])->filename);\r
-                       n1=1;\r
-               }\r
-       }\r
+       int r=0;\r
+       int r1=0;\r
+       int r2=0;\r
+       int r3=0;\r
+       int r4=0;\r
        \r
-       if(!n0)\r
-       {\r
-               sprintf(url0,"%s",(cgiGetValue(cgi,"inpix0")!=NULL)?(cgiGetValue(cgi,"inpix0")):"");\r
-               sprintf(path0,"/var/tmp/%lu_0",(unsigned long)getpid());\r
+       do {\r
+               make_tmp_path(tmp0, 256, 0, ".png");\r
+               make_tmp_path(tmp1, 256, 1, ".png");\r
+               make_tmp_path(tmp2, 256, 2, ".png");\r
+               \r
+               cgi=cgiInit();\r
+               \r
+               t = cgiGetValue(cgi, "a");\r
+               if (t != NULL)\r
+                       sscanf(t,"%"SCNuFAST8, &action);\r
+               else\r
+                       action = DETECT;\r
+               if (action > MAX_ACTION)\r
+                       action = DETECT;\r
                \r
-               sub=fork();\r
-               if(sub==0)\r
+               r = get_file(cgi,"inpixf0", tmp0, &in0_path);\r
+               if (r)\r
+                       r = get_file(cgi,"inpix0", tmp0, &in0_path);\r
+               if (r)\r
+                       break;\r
+               \r
+               if (action == INSERT)\r
                {\r
-                       t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path0,url0,(char *)0);\r
-                       exit(t);\r
+                       r = get_file(cgi,"inpixf1", tmp1, &in1_path);\r
+                       if (r)\r
+                               r = get_file(cgi,"inpix1", tmp1, &in1_path);\r
+                       if (r)\r
+                               break;\r
+                       \r
+                       t = cgiGetValue(cgi, "r0");\r
+                       if (t != NULL)\r
+                               sscanf(t,"%"SCNuFAST8, &p0);\r
+                       else\r
+                               p0 = 1;\r
+                       \r
+                       t = cgiGetValue(cgi, "r1");\r
+                       if (t != NULL)\r
+                               sscanf(t,"%"SCNuFAST8, &p1);\r
+                       else\r
+                               p1 = 0;\r
                }\r
-               waitpid(sub,&t,0);\r
-               if(t)mustard(t);\r
-       }\r
-       \r
-       if(!n1)\r
-       {\r
-               sprintf(url1,"%s",(cgiGetValue(cgi,"inpix1")!=NULL)?(cgiGetValue(cgi,"inpix1")):"");\r
-               sprintf(path1,"/var/tmp/%lu_1",(unsigned long)getpid());\r
-               \r
-               if(!act)\r
+               else\r
                {\r
-                       sub=fork();\r
-                       if(sub==0)\r
+                       t = cgiGetValue(cgi, "p0");\r
+                       if (t != NULL)\r
+                       {\r
+                               sscanf(t,"%"SCNuFAST8, &p0);\r
+                               size_defined=1;\r
+                       }\r
+                       else\r
+                               p0 = 0;\r
+                       \r
+                       t = cgiGetValue(cgi, "p1");\r
+                       if (t != NULL)\r
                        {\r
-                               t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path1,url1,(char *)0);\r
-                               exit(t);\r
+                               sscanf(t,"%"SCNuFAST8, &p1);\r
+                               size_defined=1;\r
                        }\r
-                       waitpid(sub,&t,0);\r
-                       if(t)mustard(t);\r
+                       else\r
+                               p1 = 0;\r
                }\r
-       }\r
-       \r
-       sub=fork();\r
-       if(sub==0)\r
-       {\r
-               switch (act)\r
+               \r
+               switch (action)\r
                {\r
-               case 0:\r
-                       t=execl(INS_PATH,INS_PATH,path0,path1,path0,(cgiGetValue(cgi,"r0")!=NULL)?(cgiGetValue(cgi,"r0")):"1",(cgiGetValue(cgi,"r1")!=NULL)?(cgiGetValue(cgi,"r1")):"0","q",(char *)0);\r
-                       exit(t);\r
-               case 1:\r
-               case 2:\r
-                       t=execl(EXT_PATH,EXT_PATH,path0,path0,path1,"q",(char *)0);\r
-                       exit(t);\r
-               case 3:\r
-                       t=execl(DIF_PATH,DIF_PATH,path0,path0,"q",(char *)0);\r
-                       exit(t);\r
-               default:\r
-                       exit(act);\r
+               case INSERT:\r
+                       r = insert(\r
+                               in0_path, in1_path, tmp2,\r
+                               p0, p1\r
+                       );\r
+                       break;\r
+               case EXTRACT_H:\r
+               case EXTRACT_V:\r
+                       r = extract(\r
+                               in0_path, tmp1, tmp2,\r
+                               size_defined,\r
+                               p0, p1\r
+                       );\r
+                       break;\r
+               case DETECT:\r
+                       r = detect(\r
+                               in0_path, tmp2,\r
+                               size_defined,\r
+                               p0, p1\r
+                       );\r
+                       break;\r
                }\r
+               if (r)\r
+                       break;\r
+               \r
+               r1 = send_file(\r
+                       (action == EXTRACT_H) ? tmp1 : tmp2,\r
+                       "image/png",\r
+                       0\r
+               );\r
+       } while (0);\r
+       if (r)\r
+       {\r
+               r1 = send_data(nclr, nclr_size, "image/png", 500);\r
        }\r
-       waitpid(sub,&t,0);\r
-       if(t)mustard(t);\r
+       r2 = rm(tmp0);\r
+       r3 = rm(tmp1);\r
+       r4 = rm(tmp2);\r
+       if (r1)\r
+               return r1;\r
+       if (r2)\r
+               return r2;\r
+       if (r3)\r
+               return r3;\r
+       if (r4)\r
+               return r4;\r
+       return 0;\r
+}\r
+\r
+int insert(\r
+       char *inpix0, char *inpix1, char *outpix,\r
+       uint_fast8_t r0, uint_fast8_t r1\r
+)\r
+{\r
+       char s_r0[4];\r
+       char s_r1[4];\r
        \r
-       pix=fopen((act==2?path1:path0),"rb");\r
-       if(pix==NULL)\r
-               mustard(123);\r
-       fstat(fileno(pix),&st);\r
-       p=st.st_size/1024;\r
-       q=st.st_size%1024;\r
+       pid_t sub;\r
+       int r;\r
        \r
-       printf("Content-Length: %lu\n",(unsigned long)(st.st_size));\r
-       printf("Content-type: image/png\n\n");\r
-       for(i=0;i<p;++i)\r
-       {\r
-               fread(buf,1,1024,pix);\r
-               fwrite(buf,1,1024,stdout);\r
-       }\r
-       if(q!=0)\r
+       snprintf(s_r0, 4, "%"PRIuFAST8, r0);\r
+       snprintf(s_r1, 4, "%"PRIuFAST8, r1);\r
+       \r
+       sub = fork();\r
+       if (sub == 0)\r
        {\r
-               fread(buf,1,q,pix);\r
-               fwrite(buf,1,q,stdout);\r
+               r = execl(\r
+                       PAL_MIX_PATH, PAL_MIX_PATH,\r
+                       inpix0, inpix1, outpix,\r
+                       s_r0, s_r1,\r
+                       (char *)0\r
+               );\r
+               exit(r);\r
        }\r
-       fclose(pix);\r
-       fflush(stdout);\r
-       mustard(0);//no mustard\r
-       return(0);\r
+       waitpid(sub, &r, 0);\r
+       return r;\r
 }\r
 \r
-void mustard(int mustard)\r
+int extract(\r
+       char *inpix, char *outpix0, char *outpix1,\r
+       uint_fast8_t size_defined,\r
+       uint_fast8_t p0, uint_fast8_t p1\r
+)\r
 {\r
-       if(mustard)\r
+       char s_p0[4];\r
+       char s_p1[4];\r
+       \r
+       pid_t sub;\r
+       int r;\r
+       \r
+       if (size_defined)\r
        {\r
-               cgiRedirect(MSTD_PATH);\r
+               snprintf(s_p0, 4, "%"PRIuFAST8, p0);\r
+               snprintf(s_p1, 4, "%"PRIuFAST8, p1);\r
        }\r
-       sub=fork();\r
-       if(sub==0)\r
+       \r
+       sub = fork();\r
+       if (sub == 0)\r
        {\r
-               t=execl(RM_PATH,RM_PATH,"-f",path0,(char *)0);\r
-               exit(t);\r
+               r = execl(\r
+                       PAL_UNMIX_PATH, PAL_UNMIX_PATH,\r
+                       inpix, outpix0, outpix1,\r
+                       size_defined ? s_p0 : ((char *) 0),\r
+                       size_defined ? s_p1 : ((char *) 0),\r
+                       (char *) 0\r
+               );\r
+               exit(r);\r
        }\r
-       waitpid(sub,&t,0);\r
+       waitpid(sub, &r, 0);\r
+       return r;\r
+}\r
+\r
+int detect(\r
+       char *inpix, char *outpix,\r
+       uint_fast8_t size_defined,\r
+       uint_fast8_t p0, uint_fast8_t p1\r
+)\r
+{\r
+       char s_p0[4];\r
+       char s_p1[4];\r
        \r
-       sub=fork();\r
-       if(sub==0)\r
+       pid_t sub;\r
+       int r;\r
+       \r
+       if (size_defined)\r
        {\r
-               t=execl(RM_PATH,RM_PATH,"-f",path1,(char *)0);\r
-               exit(t);\r
+               snprintf(s_p0, 4, "%"PRIuFAST8, p0);\r
+               snprintf(s_p1, 4, "%"PRIuFAST8, p1);\r
        }\r
-       waitpid(sub,&t,0);\r
        \r
-       pix=fopen(LOG_PATH,"at");\r
-       if(pix!=NULL)\r
+       sub = fork();\r
+       if (sub == 0)\r
        {\r
-               fprintf(pix,"%x %x %s %s\n",mustard,act,url0,url1);\r
-               fclose(pix);\r
+               r = execl(\r
+                       PAL_MIXDIFF_PATH, PAL_MIXDIFF_PATH,\r
+                       inpix, outpix,\r
+                       size_defined ? s_p0 : ((char *) 0),\r
+                       size_defined ? s_p1 : ((char *) 0),\r
+                       (char *) 0\r
+               );\r
+               exit(r);\r
        }\r
-       exit(mustard);\r
+       waitpid(sub, &r, 0);\r
+       return r;\r
 }\r
-\r
-\r