将字符数组中的字符放入字符串中,直到找到特定字符为止

时间:2018-12-04 06:53:06

标签: c arrays string

我想要一种可靠的方法来从字符数组中读取字符并将其放入字符串中。这将一直进行到找到\r为止。我可以遍历数组,但没有将其放入字符串的好方法。恐怕使用malloc,因为有时会将垃圾值放入字符串中。

此处payload是来自TCP数据包的HTTP数据。 \r\n\r\n指示有效载荷的结尾。

到目前为止,我的代码遍历了字符数组:

void print_payload(const unsigned char *payload, int len) {

        int i;
        const unsigned char *ch = payload;

        for (i = 0; i < len; i++) {
                if (strncmp((char*) ch, "\r\n\r\n", 4) == 0) {
                        // Indicates end of payload data.
                        break;
                } else if (strncmp((char*) ch, "\r\n", 2) == 0) {
                        //Indicates EOL
                        printf("\r\n");
                        ch++;
                        i++;
                } else if(strncmp((char*) ch, "Host:", 5) == 0){
                        printf("Host: ");
                        const unsigned char *del = ch + 6;
                        int i = 0;
                        while (del[i] != 13 ){   
                           /* 
                            *13 is decimal value for '\r'.
                            * The characters below are to be inserted
                            * in a string. Not sure how though.
                            */

                                printf("%c",del[i]);
                                i++;
                        }
                } else if(strncmp((char*) ch, "User-Agent: ", 11) == 0){
                        /*
                         * It has to implemented here as well.
                         * And in every case where my string matches.
                         */
                        printf("UserAgent: ");
                        const unsigned char* del = ch + 11;
                        int i = 0;
                        while(del[i] != 13){
                                printf("%c")    
                        }

                }                
                ch++;
        }

        printf("\r\n\r\n");
        printf("\n");

        return;
}

有人可以帮助我实现这一目标吗?我知道这是基本知识,但我仍在学习C编程,并且不确定如何做到这一点。预先感谢。

2 个答案:

答案 0 :(得分:2)

您有一些选择。首先,如果您可以限制字符串的大小,并且不需要在函数外部使用它,那么char数组将起作用:

#define STRING_MAX_LEN 999//chux mentions this is better then just putting "1000" in the array[] - 1000 needs to make sense in terms of the program, or something you wish to enforce (and checked!)
char newString[STRING_MAX_LEN+1] = {0};//Initialize to NULL value.

尽管没有理由担心malloc-只需记住安全,自由地工作,就可以了:

char *newString = malloc(sizeof(char)*(len+1)); //Better limit on needed space - +1 for a final '\0'.
if (!newString) //Oh no! hard fail.
    //do Something
}
memset(newString,0,sizeof(char)*(len+1)); //No garbage in my new string anymore!
...
...
free(newString);
//Finish up with program

您甚至不必附加'\0'-您已经确定缓冲区中已满,因此您是有效的C字符串。注意sizeof(char)可能是多余的,但无论如何我还是想保留它,以防有一天它不等于1。

请注意,如果由于某种原因必须返回新字符串,则必须使用malloc动态分配的数组。最后,如果您一次只需要检查/保留一个子字符串,那么最好重用同一字符串。

答案 1 :(得分:0)

void print_payload(const unsigned char *payload, int len) 
{
    int i;
    char c;
    char *p;

    p = (char*)payload;
    for(i=0;i<len;i++) {
        if(!strncmp(&p[i],"\r\n\r\n",4)) {
            c = p[i+4];
            p[i+4] = 0;
            break;
        }
    }
    if(i==len) {
        return;
    }
    printf("%s\n",p);
    p[i+4] = c;
}