C语言写CGI常用函数

来源:互联网 发布:上海跳跃网络吧 编辑:程序博客网 时间:2024/06/06 01:58
int IntFromHex(char *pChars)
{
    int Hi;
    int Lo;
    int Result;

    Hi=pChars[0];
    if ('0'<=Hi&&Hi<='9') {
        Hi-='0';
    } else if ('a'<=Hi&&Hi<='f') {
        Hi-=('a'-10);
    } else if ('A'<=Hi&&Hi<='F') {
        Hi-=('A'-10);
    }
    Lo = pChars[1];
    if ('0'<=Lo&&Lo<='9') {
        Lo-='0';
    } else if ('a'<=Lo&&Lo<='f') {
        Lo-=('a'-10);
    } else if ('A'<=Lo&&Lo<='F') {
        Lo-=('A'-10);
    }
    Result=Lo+(16*Hi);
    return (Result);
}

void URLDecode(char *pEncoded)
{
    char *pDecoded;

    pDecoded=pEncoded;
    while (*pDecoded) {
        if (*pDecoded=='+') *pDecoded=' ';
        pDecoded++;
    };
    pDecoded=pEncoded;
    while (*pEncoded) {
        if (*pEncoded=='%') {
            pEncoded++;
            if (isxdigit(pEncoded[0])&&isxdigit(pEncoded[1])) {
                *pDecoded++=(char)IntFromHex(pEncoded);
                pEncoded+=2;
            }
        } else {
            *pDecoded++=*pEncoded++;
        }
    }
    *pDecoded='\0';
}

char *str2html(char *instring)
{
    static unsigned char buffer[8192];
    unsigned char ch;
    int blen=0;
    int i=0;

    memset(buffer, 0, sizeof(buffer));
    while ((instring[i])&&(i<sizeof(buffer)-1)) {
        ch=instring[i];
        if (ch==0) break;
        if ((ch!=10)&&(ch!=13)) {
            if (ch<32) { i++; continue; }
        }
        if (ch=='\"') {
            buffer[blen++]='&';
            buffer[blen++]='q';
            buffer[blen++]='u';
            buffer[blen++]='o';
            buffer[blen++]='t';
            buffer[blen++]=';';
            i++;
            continue;
        }
        if (ch=='&') {
            buffer[blen++]='&';
            buffer[blen++]='a';
            buffer[blen++]='m';
            buffer[blen++]='p';
            buffer[blen++]=';';
            i++;
            continue;
        }
        if (ch=='<') {
            buffer[blen++]='&';
            buffer[blen++]='l';
            buffer[blen++]='t';
            buffer[blen++]=';';
            i++;
            continue;
        }
        if (ch=='>') {
            buffer[blen++]='&';
            buffer[blen++]='g';
            buffer[blen++]='t';
            buffer[blen++]=';';
            i++;
            continue;
        }
        buffer[blen++]=ch;
        i++;
    }
    return (char *)buffer;
}



char *strcasestr(const char *src, const char *query)
{
    char *pToken;
    char Buffer[8192];
    char Query[64];
    int loop;

    if (strlen(src)==0) return NULL;
    memset(Buffer, 0, sizeof(Buffer));
    strncpy(Buffer, src, sizeof(Buffer)-1);
    strncpy(Query, query, sizeof(Query)-1);
    loop=0;
    while (Buffer[loop]) {
        Buffer[loop]=toupper(Buffer[loop]);
        loop++;
    }
    loop=0;
    while (Query[loop]) {
        Query[loop]=toupper(Query[loop]);
        loop++;
    }
    pToken=strstr(Buffer, Query);
    if (pToken!=NULL) {
        return (char *)src+(pToken-(char *)&Buffer);
    }
    return NULL;
}

void striprn(char *string)
{
    while ((string[strlen(string)-1]=='\r')||(string[strlen(string)-1]=='\n')) {
        string[strlen(string)-1]='\0';
    }
}
0 0
原创粉丝点击