void change_style(SDL_Surface *src, Uint32 style) { Uint8 rm, gm, bm; int x, y; SDL_GetRGB(style, src->format, &rm, &gm, &bm); if ( SDL_MUSTLOCK(src) ) { if ( SDL_LockSurface(src) < 0 ) { return; } } for (x = 0; x < src->w; x++) { for (y = 0; y < src->h; y++) { Uint32 color; Uint8 r, g, b; int nr, ng, nb; color = get_pixel(src, x, y); SDL_GetRGB(color, src->format, &r, &g, &b); nr = r * rm / 255; ng = g * gm / 255; nb = b * bm / 255; #define check_byte(r) ((r > 0)?((r < 255)?r:255):0) nr = check_byte(nr); ng = check_byte(ng); nb = check_byte(nb); #undef check_byte color = SDL_MapRGB(src->format, (Uint8)nr, (Uint8)ng, (Uint8)nb); set_pixel(src, x, y, color); } } if ( SDL_MUSTLOCK(src) ) { SDL_UnlockSurface(src); } }
void change_spec(SDL_Surface *src, SDL_Surface *new_spec) { int x, y; if ( SDL_MUSTLOCK(src) ) { if ( SDL_LockSurface(src) < 0 ) { return; } } for (x = 0; x < src->w; x++) { for (y = 0; y < src->h; y++) { int nr, ng, nb, a; Uint8 sr, sg, sb; Uint8 nsr, nsg, nsb; SDL_GetRGB(get_pixel(src, x, y), src->format, &sr, &sg, &sb); SDL_GetRGB(get_pixel(new_spec, x, y), new_spec->format, &nsr, &nsg, &nsb); a = (nsr + nsg + nsb) / 3; nr = sr - a + nsr; ng = sg - a + nsg; nb = sb - a + nsb; #define check_byte(r) ((r > 0)?((r < 255)?r:255):0) nr = check_byte(nr); ng = check_byte(ng); nb = check_byte(nb); #undef check_byte set_pixel(src, x, y, SDL_MapRGB(src->format, nr, ng, nb)); } } if ( SDL_MUSTLOCK(src) ) { SDL_UnlockSurface(src); } }
//打印tcp时间戳和数据字段长度(区分不同的tcp目的端口) int get_tcp_info(char *filename) { int reval; struct pcap_pkthdr* header; u_char *pkt_data; pcap_t *pcap_handle; char error_content[PCAP_ERRBUF_SIZE]; char tempname[256]; FILE *fd; int good_count = 0; int count1_100 = 0; int count100_1000 = 0; int count1000more = 0; //flag & 0100 0000 == 0100 0000, 表示最高第二位标志 Bad FCS: TRUE,该数据包有误码 //if ((flags[i] & 0x40)== 0x40) // continue; strcpy(tempname,filename); strcat(tempname,"_out.txt"); printf ("输出文件:%s\n",tempname); //w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。 fd=fopen(tempname,"w+"); pcap_handle=pcap_open_offline(filename,error_content); if(!pcap_handle) { fprintf(stderr, "Error in opening savefile, %s, for reading: %s\n",filename,error_content ); exit(1); } reval = pcap_next_ex(pcap_handle, &header, (const u_char **)&pkt_data); unsigned int src,dst; inet_aton(src_ip,(struct in_addr*)&src); inet_aton(dst_ip,(struct in_addr*)&dst); double time_stamp = 0.0; //所有包的错误bit之和 double sum_error_bit = 0.0; //所有包数据域bit之和 double sum_bit = 0.0; int frame_index = 0; unsigned short radiotap_h_len = 0; while(pkt_data!=NULL && reval > 0) { radiotap_h_len = *(unsigned short*)(pkt_data+2); //ip 过滤 int offset = MONITOR_MODE_EXTRA_LEN + radiotap_h_len; if(*(pkt_data+12+offset)==0x08 && *(pkt_data+13+offset)==0x00 && *(unsigned int*)(pkt_data+26+offset)==src && *(unsigned int*)(pkt_data+30+offset)==dst) { frame_index ++; if(*(pkt_data+23+offset)==0x06) // TCP 6 { double usec = (double)header->ts.tv_usec; usec /= 1000000; time_stamp = header->ts.tv_sec + usec; //fprintf(fd,"%ld.%ld\t",header->ts.tv_sec,header->ts.tv_usec); // has a bug int data_len = header->len-*(pkt_data+46+offset)/4-34-offset-FCS_LEN; //数据字段长度大于0 if (data_len>0) { fprintf(fd, "%lf\t", time_stamp); //时间戳 fprintf (fd, "%d \t", data_len); //数据长度 unsigned char temp_flag = *(unsigned char*)(pkt_data+24); //radiotap header's flags fprintf(fd,"%2x\t",temp_flag ); //is FCS bad? if ((temp_flag & 0x40)== 0x40) fprintf(fd,"Bad\t"); else fprintf(fd,"Good\t"); //数据字段错误的bit数 int len_before_tcpdata = header->len - data_len - FCS_LEN; unsigned char* p_tcpdata = (unsigned char*)(pkt_data + len_before_tcpdata ); int error_sum = 0; int i=0; //逐个byte check for (; i<data_len; i++) { error_sum += check_byte(*p_tcpdata); p_tcpdata += 1; } fprintf(fd ,"%d\n", error_sum); //assert check // (temp_flag & 0x40)== 0x40 //error //error_sum>0 //error if ( (temp_flag & 0x40)!= 0x40 && error_sum > 0 ) { printf("%2x, %d, frame: %d\n",temp_flag, error_sum, frame_index); //assert(0); } //assert( ((temp_flag & 0x40)== 0x40) != (error_sum==0) ); if (error_sum==0) good_count++; else if (error_sum>0 && error_sum<=100) //(0,100] count1_100 ++; else if (error_sum>100 && error_sum<=1000) //(100,1000] count100_1000 ++; else if (error_sum >1000) //(1000, max) count1000more ++; else assert(0); sum_bit += data_len*8; sum_error_bit += error_sum; } } } reval = pcap_next_ex(pcap_handle, &header, (const u_char **)&pkt_data); } int total = good_count + count1_100 + count100_1000 + count1000more; printf("good: %d\n(0,100]: %d\n(100,1000]: %d\n(1000,--): %d\n", good_count, count1_100, count100_1000, count1000more); printf("packet error rate: %lf \n", 1-(double)good_count/(double)total); printf("bit error rate: %lf\n", sum_error_bit/sum_bit); printf("%lf,%lf,%lf,%lf\n", (double)good_count/(double)total, (double)count1_100/(double)total, (double)count100_1000/(double)total, (double)count1000more/(double)total); fclose(fd); }