//プロジェクト名cookie001 C++ソースファイルcookie001.cpp #include #include #include #include extern "C" { __declspec(dllexport) char * recvcookie(char * string01); __declspec(dllexport) void sendcookie(char * string01); __declspec(dllexport) void sendcookieutf(char * string01); __declspec(dllexport) char * shift_to_utf(char * string01,char * string02); __declspec(dllexport) char * utf_to_shift(char * string01,char * string02); __declspec(dllexport) void dispshift(char * string01); __declspec(dllexport) void disputf(char * string01); int urlencode01(char *s,char *pd); int urlencodeutf01(char *s,char *pd); int urldecode01(char *s); char * recvcookie(char * string01) { char *p,*param; int len,n,done; done = FALSE; p = param = NULL; p = getenv("HTTP_COOKIE"); if(p != NULL) { len = strlen(p); param = (char *)calloc(1,len + 2); if (param != NULL) { param = strcpy(param, p); if(param != NULL) { done = TRUE; } } } if (done) { len=strlen(string01); n=urldecode01(param); if (len>=n) { memcpy(string01,param,n); } else { * string01='\0'; } free(param); } else { * string01='\0'; } return string01; } void sendcookie(char * p) { char *param,*param1; int len,n,done; done = FALSE; param = NULL; if(p != NULL) { len = strlen(p); param = (char *)calloc(1,len*3 + 2); param1 = (char *)calloc(1,len*3 + 14); if (param != NULL && param1 != NULL) { done = TRUE; } } n=urlencode01(p,param); strcpy(param1,"Set-Cookie: "); strcat(param1,param); puts(param1); } void sendcookieutf(char * p) { char *param,*param1; int len,n,done; done = FALSE; param = NULL; if(p != NULL) { len = strlen(p); param = (char *)calloc(1,len*3 + 2); param1 = (char *)calloc(1,len*3 + 14); if (param != NULL && param1 != NULL) { done = TRUE; } } n=urlencodeutf01(p,param); strcpy(param1,"Set-Cookie: "); strcat(param1,param); puts(param1); } char * shift_to_utf(char * string01,char * string02) { char *param1,*param2; int len,n,done; done = FALSE; n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0 ); if(n != 0) { param1 = (char *)calloc(1,n*2 + 2); if (param1 != NULL) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, (unsigned short *)param1, n ); if(n != 0) { done = TRUE; } } } if (done) { done = FALSE; n = WideCharToMultiByte(CP_UTF8, 0, (unsigned short *)param1, -1, NULL, 0, NULL, NULL); if(n != 0) { param2 = (char *)calloc(1,n + 2); if (param2 != NULL) { n = WideCharToMultiByte(CP_UTF8, 0, (unsigned short *)param1, -1, param2, n, NULL, NULL); if(n != 0) { done = TRUE; } } } } if (done) { len=strlen(string02); if (len>=n) { memcpy(string02,param2,n); } else { * string02='\0'; } } else { * string02='\0'; } free(param1); free(param2); return string02; } char * utf_to_shift(char * string01,char * string02) { char *param1,*param2; int len,n,done; done = FALSE; n = MultiByteToWideChar(CP_UTF8, 0, string01, -1, NULL, 0 ); if(n != 0) { param1 = (char *)calloc(1,n*2 + 2); if (param1 != NULL) { n = MultiByteToWideChar(CP_UTF8, 0, string01, -1, (unsigned short *)param1, n ); if(n != 0) { done = TRUE; } } } if (done) { done = FALSE; n = WideCharToMultiByte(CP_ACP, 0, (unsigned short *)param1, -1, NULL, 0, NULL, NULL); if(n != 0) { param2 = (char *)calloc(1,n + 2); if (param2 != NULL) { n = WideCharToMultiByte(CP_ACP, 0, (unsigned short *)param1, -1, param2, n, NULL, NULL); if(n != 0) { done = TRUE; } } } } if (done) { len=strlen(string02); if (len>=n) { memcpy(string02,param2,n); } else { * string02='\0'; } } else { * string02='\0'; } free(param1); free(param2); return string02; } void dispshift(char * string01) { char *param1; int n; // char caption01[20]; // n = MultiByteToWideChar(CP_ACP, 0, "DispShift", -1, NULL, 0 ); // n = MultiByteToWideChar(CP_ACP, 0, "DispShift", -1, (unsigned short *)caption01, n ); n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0 ); if(n != 0) { param1 = (char *)calloc(1,n*2 + 2); if (param1 != NULL) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, (unsigned short *)param1, n ); if(n != 0) { // MessageBoxW(NULL, (const unsigned short *)param1, (const unsigned short *)caption01, MB_OK); MessageBoxW(NULL, (const unsigned short *)param1, L"DispShift", MB_OK); // wprintf(L"%s\n", param1); } } } free(param1); } void disputf(char * string01) { char *param1; int n; // char caption01[20]; // n = MultiByteToWideChar(CP_ACP, 0, "DispUTF", -1, NULL, 0 ); // n = MultiByteToWideChar(CP_ACP, 0, "DispUTF", -1, (unsigned short *)caption01, n ); n = MultiByteToWideChar(CP_UTF8, 0, string01, -1, NULL, 0 ); if(n != 0) { param1 = (char *)calloc(1,n*2 + 2); if (param1 != NULL) { n = MultiByteToWideChar(CP_UTF8, 0, string01, -1, (unsigned short *)param1, n ); if(n != 0) { // MessageBoxW(NULL, (const unsigned short *)param1, (const unsigned short *)caption01, MB_OK); MessageBoxW(NULL, (const unsigned short *)param1, L"DispUTF", MB_OK); // wprintf(L"%s\n", param1); } } } free(param1); } int urlencode01(char *s,char *pd) { int n; n = 0; while(*s) { // if( (*s >= '0' && *s <= '9') // || (*s >= 'A' && *s <= 'Z') // || (*s >= 'a' && *s <= 'z') // || (*s == '-') // || (*s == '.') // || (*s == '_') ) // { // *pd++ = *s++; // n++; // } // else if( *s == ' ' ) // { // *pd++ = '+'; // n++; // s++; // } if( *s == '%' ) { sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; } else if((((unsigned char)*s>= 0x81) && ((unsigned char)*s<=0x9F)) || (((unsigned char)*s>=0xE0) && ((unsigned char)*s<=0xEF))) { sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; } else { *pd++ = *s++; n++; } } *pd++ = '\0'; n++; *pd++ = '\0'; n++; return n; } int urlencodeutf01(char *s,char *pd) { int n; n = 0; while(*s) { // if( (*s >= '0' && *s <= '9') // || (*s >= 'A' && *s <= 'Z') // || (*s >= 'a' && *s <= 'z') // || (*s == '-') // || (*s == '.') // || (*s == '_') ) // { // *pd++ = *s++; // n++; // } // else if( *s == ' ' ) // { // *pd++ = '+'; // n++; // s++; // } if( *s == '%' ) { sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; } else if(((unsigned char)*s>= 0xC2) && ((unsigned char)*s<=0xDF)) { sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; } else if(((unsigned char)*s>= 0xE0) && ((unsigned char)*s<=0xEF)) { sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; } else if(((unsigned char)*s>= 0xF0) && ((unsigned char)*s<=0xF4)) { sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; sprintf(pd,"%%%02X",(unsigned char)*s); pd +=3; n +=3; s++; } else { *pd++ = *s++; n++; } } *pd++ = '\0'; n++; *pd++ = '\0'; n++; return n; } int urldecode01(char *s) { char *pd; int ch,n; pd = s; n = 0; while(*s) { switch(*s) { // case '&': // *pd++ = '\0'; // n++; // s++; // break; // case '+': // *pd++ = ' '; // n++; // s++; // break; case '%': sscanf(s + 1,"%02x",&ch); *pd++ = (char)ch; n++; s += 3; break; default: *pd++ = *s++; n++; break; } } *pd++ = '\0'; n++; *pd++ = '\0'; n++; return n; } }