static ulong parse_pktfile()

in wb/wb.c [1170:1296]


static ulong parse_pktfile(char *pkt_data, struct _g_pkt_array_ *pkt_array)
{
    ulong pkt_count = 0;
    apr_size_t parsed_bytes = 0;
    char *p = pkt_data, *p2 = pkt_data, *p_digits;
    struct _g_pkt_array_ _pkt, *pkt;

    /*
     * if it starts with a NUMBER, it consists of multiple packets
     * if not, it's raw HTTP packet, (http methods at the beginning)
     * but we can still use '\0' for seperating multiple packets
     */
    if ( *p < '0' || *p > '9' ) { // not a digit
        // this is raw http packet, save them to pkt_array directly 
        parsed_bytes = 0;
        do { 
            if (pkt_array) 
                pkt = &pkt_array[pkt_count];
            else
                pkt = &_pkt;
            
            // save the start pointer of each packet
            pkt->pkt_data = pkt_data + parsed_bytes;
            // move to next packet by increment pkt_count
            // '\0' is seperator, use strlen to get the position of next packet
            pkt->pkt_length = strlen(pkt->pkt_data);

            // remove '\0' by +1
            parsed_bytes += pkt->pkt_length + 1;
            if (pkt->pkt_length < 4){
                if (!pkt_array && pkt->pkt_data[0] != '\0' && pkt->pkt_data[0] != '\r' && pkt->pkt_data[0] != '\n' ) 
                    fprintf(stderr, "Warning: this packet (%s) does not have a valid packet size(%d), ignore it!\n", 
                            pkt->pkt_data,pkt->pkt_length);
            }
                
            else 
                pkt_count ++;
        } while (parsed_bytes < g_pkt_length && (pkt_count < g_MAX_PKT_COUNT || g_MAX_PKT_COUNT == 0));

    } else { // chunked file, each packet has a PKT_SIZE number in the first line   
        parsed_bytes = 0;
        ulong l_pkt_size = 0;
        long time_sec = 0, time_usec = 0;
        char c = 0;
        do {
            /*
            //  p points to first non-space char, skip the leading space
            while (parsed_bytes < g_pkt_length && apr_isspace(*p)) {
                p++;
                parsed_bytes ++;
            }
            
            // p_digits points to first non-digit char to fetch the digits for packet size; 
            p_digits = p; 
            while (parsed_bytes < g_pkt_length  && *p_digits >= '0' && *p_digits <= '9') {
                p_digits++;
                parsed_bytes ++;
            }

            if ( parsed_bytes >= g_pkt_length ) // end of file
                break; 
            
            // it's wrong if it does not have such digits and file does not end
            if ( p_digits == p) {
                fprintf(stderr, "Error: packets file does not have a valid packet size (%s)!\n", p);
                return 0;
            }
            
            *p_digits = 0; // so we can use string functions

            // end the processing if reads a "0"
            if ( (l_pkt_size = atoi(p)) <= 0)
                break;

            // skip the ending space,  starting from end of number
            parsed_bytes ++;
            p = p_digits + 1;
            //  p points to first non-CRLN char
            while (parsed_bytes < g_pkt_length && apr_isspace(*p)) {
                p++;
                parsed_bytes ++;
            }
            */

            // get the line feed and make it a c-based string
            for (p2 = p; *p2 && *p2 != '\r' && *p2 != '\n'; p2++);
            if (!*p2)
                break;
            c = *p2; 
            *p2 = 0;
            // use string scanf to fetch numbers
            l_pkt_size = 0;
            time_sec = 0, time_usec = 0;
            sscanf(p,"%lu %lu.%lu",&l_pkt_size, &time_sec, &time_usec);
            *p2 = c;

            if (l_pkt_size > 0) {
                // save the start pointer of each packet
                if (pkt_array) 
                    pkt = &pkt_array[pkt_count];
                else
                    pkt = &_pkt;
                p = p2 + 1;
                // for (p=p2+1; *p && apr_isspace(*p); p++);
                
                pkt->pkt_data = p;
                pkt->pkt_length = l_pkt_size;
                pkt->pkt_time_to_send = time_sec * 1000000 + time_usec;

                parsed_bytes = p - g_pkt_data;

                // move to next packet by increment pkt_count
                // use l_pkt_size to get the position of next packet
                parsed_bytes += l_pkt_size;

                if ( parsed_bytes > g_pkt_length ) {// end of file, wrong!
                    fprintf(stderr, "Error: packets file needs a body (%s)!\n", p);
                    return 0;
                }
                
                pkt_count ++;
                for (p = p + l_pkt_size; *p && apr_isspace(*p); p++);
            }
        } while (l_pkt_size > 0 && *p && (pkt_count < g_MAX_PKT_COUNT || g_MAX_PKT_COUNT == 0));
    }
    return pkt_count;
} // end of parse_pktfile