Ejemplo n.º 1
0
int WLDLoader::Open(char *base_path, char *zone_name, Archive *archive) {
  uchar *buffer;
  int buf_len;
  int i, j, vc, pc, bc, mlen, *pmap;
  Zone_Model *zm;
  Model *m;

  Archive *obj_archive;

  char *filename, *model_name;

  struct_wld_header *header;
  struct_wld_basic_frag *frag;

#ifdef DEBUGWLD
  printf("Zone: %s\n", zone_name);
#endif
  this->model_data.zone_model = nullptr;

  this->model_data.plac_count = 0;
  this->model_data.placeable = 0;

  this->obj_loader = this->plac_loader = nullptr;

  this->clear_plac = 0;

  filename = new char[strlen(zone_name) + 5];
  sprintf(filename, "%s.wld", zone_name);

  if(!GetFile(&buffer, &buf_len, nullptr, filename, archive)) {
    delete[] filename;
    return 0;
  }
  delete[] filename;

  header = (struct_wld_header *) buffer;

  if(header->magic != 0x54503d02)
    return 0;

  buffer += sizeof(struct_wld_header);
  
  this->sHash = buffer;
  decode(this->sHash, header->stringHashSize);
  buffer += header->stringHashSize;

  if(header->version == 0x00015500)
    this->old = 1;
  else
    this->old = 0;

  this->fragcount = header->fragmentCount;
  this->frags = new Fragment *[header->fragmentCount];


  for(i = 0; i < this->fragcount; ++i) {
    frag = (struct_wld_basic_frag *) buffer;

    buffer += sizeof(struct_wld_basic_frag);

    switch(frag->id) {
    case 0x03: FRAGMENT(Data03); break;
    case 0x04: FRAGMENT(Data04); break;
    case 0x05: FRAGMENT(Data05); break;
    case 0x15: FRAGMENT(Data15); break;
    case 0x1B: FRAGMENT(Data1B); break;
    case 0x1C: FRAGMENT(Data1C); break;
    case 0x21: FRAGMENT(Data21); break;
    case 0x22: FRAGMENT(Data22); break;
    case 0x29: FRAGMENT(Data29); break;
    case 0x28: FRAGMENT(Data28); break;
    case 0x30: FRAGMENT(Data30); break;
    case 0x31: FRAGMENT(Data31); break;
    case 0x36: FRAGMENT(Data36); break;
			   
    default: this->frags[i] = new Fragment; break;
    }

    this->frags[i]->type = frag->id;
    this->frags[i]->name = frag->nameRef;

    buffer += frag->size - 4;
  }

  


  if(!strcmp(&zone_name[strlen(zone_name) - 4], "_obj")) {
    this->model_data.plac_count = 0;
    this->model_data.model_count = 0;
    // for an obj_s3d file, find out how many placeabale objects there are (each 0x36 Frag is a placeable object in an obj_s3d file
    for(i = 0; i < this->fragcount; ++i) {
      if(this->frags[i]->type == 0x36)
        ++this->model_data.model_count;
    }
    // allocate space for this many models
    this->model_data.models = new Model *[this->model_data.model_count];
    this->model_data.model_count = 0;
    for(i = 0; i < this->fragcount; ++i) {
      if(this->frags[i]->type == 0x36)
        this->model_data.models[this->model_data.model_count++] = (Model *) this->frags[i]->frag;
    }
  }
  else if(!strcmp(&zone_name[strlen(zone_name) - 4], "_chr")) {
  }
  else if(!strcmp(zone_name, "objects")) {
    this->clear_plac = 1;
  }
  else {
    // We are procesing the zone mesh S3D
    zm = this->model_data.zone_model = new Zone_Model;
    zm->vert_count = 0;
    zm->poly_count = 0;
    for(i = 0; i < this->fragcount; ++i) {
      if(this->frags[i]->type != 0x36)
        continue;
      m = (Model *) this->frags[i]->frag;
      zm->vert_count += m->vert_count;
      zm->poly_count += m->poly_count;
      zm->tex_count = m->tex_count; // The texcount and tex fields are the same for every 0x36 frag in the zone mesh S3D
      zm->tex = m->tex;
    }


    
    zm->verts = new Vertex *[zm->vert_count];
    zm->polys = new Polygon *[zm->poly_count];
    
    vc = pc = 0;
    
    for(i = 0; i < this->fragcount; ++i) {
      if(this->frags[i]->type != 0x36)
        continue;
      m = (Model *) this->frags[i]->frag;
      bc = vc;
      // Copy the vertexes from the 0x36 frag into our zone model
      for(j = 0; j < m->vert_count; ++j)
        zm->verts[vc++] = m->verts[j];
      // Adjust the polygon vertices indices to match the indices in our zone model vertex array
      for(j = 0; j < m->poly_count; ++j) {
        m->polys[j]->v1 += bc;
        m->polys[j]->v2 += bc;
        m->polys[j]->v3 += bc;
#ifdef DEBUGWLD
	printf("Polygon: %5d  Vertices %5d, %5d, %5d\n", j,
	        m->polys[j]->v1, m->polys[j]->v2, m->polys[j]->v3);
#endif

#ifdef DEBUGWLD
        printf("Zone Frag36 No. %5d: Poly %5d V3 = %4.3f, %4.3f, %4.3f\n", i, j, zm->verts[m->polys[j]->v3]->x, zm->verts[m->polys[j]->v3]->y, zm->verts[m->polys[j]->v3]->z);
	fflush(stdout);
#endif
        zm->polys[pc++] = m->polys[j];
      }
    }

    this->model_data.plac_count = 0;
    this->model_data.model_count = 0;

    filename = new char[strlen(zone_name) + 10];
    sprintf(filename, "%s_obj.s3d",zone_name);

    obj_archive = new PFSLoader;
    if(!obj_archive->Open(fopen(filename, "rb"))) {
//    	printf("_obj.s3d file not found.\n");
	return 1;
    }

    delete[] filename;

    filename = new char[strlen(zone_name) + 5];
    sprintf(filename, "%s_obj", zone_name);

    this->obj_loader = new WLDLoader();
    this->obj_loader->Open(nullptr, filename, obj_archive);

    delete[] filename;

    this->model_data.model_count = this->obj_loader->model_data.model_count;
    this->model_data.models = this->obj_loader->model_data.models;

    this->plac_loader = new WLDLoader();
    this->plac_loader->Open(nullptr, "objects", archive);

    if(this->model_data.plac_count) {
      delete this->model_data.placeable[0];
      delete[] this->model_data.placeable;
    }

    this->model_data.plac_count = this->plac_loader->model_data.plac_count;
    this->model_data.placeable = this->plac_loader->model_data.placeable;


    for(i = 0; i < this->model_data.plac_count; ++i) {
      mlen = strlen((char *) this->model_data.placeable[i]->model) - 8;
      model_name = new char[mlen + 12];
      memcpy(model_name, (char *) this->model_data.placeable[i]->model, mlen);
      model_name[mlen] = 0;
      sprintf(model_name, "%sDMSPRITEDEF", model_name);
      this->model_data.placeable[i]->model = -1;

      for(j = 0; j < this->model_data.model_count; ++j) {
        if(!strcmp(this->model_data.models[j]->name, model_name)) {
          this->model_data.placeable[i]->model = j;
//	    printf("Placeable object %d name is %s\n", j, model_name);
          break;
        }
      }


      delete[] model_name;
    }

    for(i = 0; i < this->model_data.model_count; ++i) {
      for(j = 0; j < this->model_data.models[i]->tex_count; ++j)
        this->model_data.models[i]->tex[j]->archive = obj_archive;
    }

    for(i = 0; i < this->model_data.model_count; ++i) {
      pmap = new int[this->model_data.models[i]->poly_count];
      memset(pmap, 0, sizeof(int) * this->model_data.models[i]->poly_count);
      for(j = 0; j < this->model_data.models[i]->tex_count; ++j) {
        filename = this->model_data.models[i]->tex[j]->filenames[0];
        for(bc = 0; bc < this->model_data.zone_model->tex_count; ++bc) {
          if(!strcmp(filename, this->model_data.zone_model->tex[bc]->filenames[0])) {
            for(pc = 0; pc < this->model_data.models[i]->poly_count; ++pc) {
              if(!pmap[pc] && this->model_data.models[i]->polys[pc]->tex == j) {
                this->model_data.models[i]->polys[pc]->tex = bc;
                pmap[pc] = 1;
              }
            }
            break;
          }
        }
      }
      delete[] pmap;
    }
  }

  return 1;
}
Ejemplo n.º 2
0
/*
 *	Write captured packet to file
 *
 *	uint8_t *flag : filter is set or not
 *	const uint8_t *packet : captured packet
 *	char *fltrload : filter for payload
 *	cosnt int payloadlen : length of captured payload
 */
static void printfile(uint8_t *flag, const uint8_t *packet, char *fltrload, const int payloadlen)
{
	FILE *fp = fopen(LOGFILE, "a");
	const uint8_t *http = NULL;
	char *timestamp;
	struct ether_header *eth;
	struct ip *iphdr;
	struct tcphdr *tcphdr;
	struct udphdr *udphdr;
	uint8_t prot;
	int hdrlen, i;

	flag += 2;  // remove unused member

	eth = (struct ether_header *)packet;
	hdrlen = sizeof(struct ether_header);

	iphdr = (struct ip *)(packet + hdrlen);
	hdrlen += sizeof(struct ip);

	prot = iphdr->ip_p;
	if( prot == 17 ) {
		udphdr = (struct udphdr *)(packet + hdrlen);
		hdrlen += sizeof(struct udphdr);
	}else {
		tcphdr = (struct tcphdr *)(packet + hdrlen);
		hdrlen += sizeof(struct tcphdr);

		// HTTP request check 
		if( PROTOCOL(flag) ) {
			if( prot == 3 ) {
				if( HTTPTEST("GET",3) == false )
					return;

			}else if( prot == 5 ) {
				if( HTTPTEST("PUT",3) == false )
					return;

			}else if(prot == 7) {
				if( HTTPTEST("POST",4) == false )
					return;
			}
		}
	}

	
	

	// Timestamp
	timestamp = (char *)gettime();
	fprintf(fp, "%s ------\n", timestamp);
	free(timestamp);

	/* Ethernet Header */
	i = 0;
	if( DSTMAC(flag) ) {
		fprintf(fp, "\x1b[45mDestination MAC\x1b[0m\t: \x1b[45m");
		while( i < 5 )
			fprintf(fp, "%02X:", eth->ether_dhost[i++]);
		fprintf(fp, "%02X\x1b[0m\n", eth->ether_dhost[i]);
	}else {
		fprintf(fp, "Destination MAC\t: ");
		while( i < 5 )
			fprintf(fp, "%02X:", eth->ether_dhost[i++]);
		fprintf(fp, "%02X\n", eth->ether_dhost[i]);
	}

	i = 0;
	if( SRCMAC(flag) ) {
		fprintf(fp, "\x1b[45mSource MAC\x1b[0m\t: \x1b[45m");
		while( i < 5 )
			fprintf(fp, "%02X:", eth->ether_shost[i++]);
		fprintf(fp, "%02X\x1b[0m\n", eth->ether_shost[i]);
	}else {
		fprintf(fp, "Source MAC\t: ");
		while( i < 5 )
			fprintf(fp, "%02X:", eth->ether_shost[i++]);
		fprintf(fp, "%02X\n", eth->ether_shost[i]);
	}

	if( ETHERTYPE(flag) )
		fprintf(fp, "\x1b[45mEthernet Type\x1b[0m\t: \x1b[45m%s\x1b[0m\n", gettype(eth->ether_type));
	else
		fprintf(fp, "Ethernet Type\t: %s\n", gettype(eth->ether_type));

	/* IP Header */
	if( VERSION(flag) & 0xf0 )
		fprintf(fp, "\x1b[44mVersion\x1b[0m\t\t: \x1b[44m%d\x1b[0m\n", iphdr->ip_v);
	else
		fprintf(fp, "Version\t\t: %d\n", iphdr->ip_v);

	if( IPHLEN(flag) & 0x0f )
		fprintf(fp, "\x1b[44mIP Header length\x1b[0m: \x1b[44m%d\x1b[0m\n", iphdr->ip_hl);
	else
		fprintf(fp, "IP Header length: %d\n", iphdr->ip_hl);

	if( TOS(flag) )
		fprintf(fp, "\x1b[44mType of Service\x1b[0m\t: \x1b[44m%s\x1b[0m\n", gettos(iphdr->ip_tos));
	else
		fprintf(fp, "Type of Service\t: %s\n", gettos(iphdr->ip_tos));

	if( IPLEN(flag) )
		fprintf(fp, "\x1b[44mTotal length\x1b[0m\t: \x1b[44m%d\x1b[0m\n", ntohs(iphdr->ip_len));
	else
		fprintf(fp, "Total length\t: %d\n", ntohs(iphdr->ip_len));

	if( IPID(flag) )
		fprintf(fp, "\x1b[44mIdentification\x1b[0m\t: \x1b[44m%d\x1b[0m\n", ntohs(iphdr->ip_id));
	else
		fprintf(fp, "Identification\t: %d\n", ntohs(iphdr->ip_id));

	if( FRAGMENT(flag) )
		fprintf(fp, "\x1b[44mFragment\x1b[0m\t: \x1b[44m%d\x1b[0m\n", iphdr->ip_off);
	else
		fprintf(fp, "Fragment\t: %d\n", iphdr->ip_off);

	if( TTL(flag) )
		fprintf(fp, "\x1b[44mTime to live\x1b[0m\t: \x1b[44m%d\x1b[0m\n", iphdr->ip_ttl);
	else
		fprintf(fp, "Time to live\t: %d\n", iphdr->ip_ttl);

	if( PROTOCOL(flag) )
		fprintf(fp, "\x1b[44mProtocol\x1b[0m\t: \x1b[44m%s\x1b[0m\n", getprot(iphdr->ip_p));
	else
		fprintf(fp, "Protocol\t: %s\n", getprot(iphdr->ip_p));

	if( IPCKSUM(flag) )
		fprintf(fp, "\x1b[44mChecksum\x1b[0m\t: \x1b[44m%d\x1b[0m\n", ntohs(iphdr->ip_sum));
	else
		fprintf(fp, "Checksum\t: %d\n", ntohs(iphdr->ip_sum));

	if( SRCIP(flag) )
		fprintf(fp, "\x1b[44mSource IP\x1b[0m\t: \x1b[44m%s\x1b[0m\n", inet_ntoa(iphdr->ip_src));
	else
		fprintf(fp, "Source IP\t: %s\n", inet_ntoa(iphdr->ip_src));

	if( DSTIP(flag) )
		fprintf(fp, "\x1b[44mDestination IP\x1b[0m\t: \x1b[44m%s\x1b[0m\n", inet_ntoa(iphdr->ip_dst));
	else
		fprintf(fp, "Destination IP\t: %s\n", inet_ntoa(iphdr->ip_dst));

	if( iphdr->ip_p == 17 ) {
	/* UDP Header */
		if( SRCPORT(flag) )
			fprintf(fp, "\x1b[42mSource Port\x1b[0m\t: \x1b[42m%d\x1b[0m\n", ntohs(udphdr->source));
		else
			fprintf(fp, "Source Port\t: %d\n", ntohs(udphdr->source));

		if( DSTPORT(flag) )
			fprintf(fp, "\x1b[42mDestination Port\x1b[0m: \x1b[42m%d\x1b[0m\n", ntohs(udphdr->dest));
		else
			fprintf(fp, "Destination Port: %d\n", ntohs(udphdr->dest));

		if( UDPLEN(flag) )
			fprintf(fp, "\x1b[42mTotal length\x1b[0m\t: \x1b[42m%d\x1b[0m\n", ntohs(udphdr->len));
		else
			fprintf(fp, "Total length\t: %d\n", ntohs(udphdr->len));

		if( UDPCKSUM(flag) )
			fprintf(fp, "\x1b[42mCheckSum\x1b[0m\t: \x1b[42m%d\x1b[0m\n", ntohs(udphdr->check));
		else
			fprintf(fp, "Checksum\t: %d\n", ntohs(udphdr->check));
	}else {
	/* TCP Header */
		if( SRCPORT(flag) )
			fprintf(fp, "\x1b[31;43mSource Port\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->source));
		else
			fprintf(fp, "Source Port\t: %d\n", ntohs(tcphdr->source));

		if( DSTPORT(flag) )
			fprintf(fp, "\x1b[31;43mDestination Port\x1b[0m: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->dest));
		else
			fprintf(fp, "Destination Port: %d\n", ntohs(tcphdr->dest));

		if( SEQ(flag) )
			fprintf(fp, "\x1b[31;43mSequence Number\x1b[0m\t: \x1b[31;43m%u\x1b[0m\n", ntohl(tcphdr->seq));
		else
			fprintf(fp, "Sequence Number\t: %u\n", ntohl(tcphdr->seq));

		if( ACK(flag) )
			fprintf(fp, "\x1b[31;43mAcknowledgement\x1b[0m\t: \x1b[31;43m%u\x1b[0m\n", ntohl(tcphdr->ack));
		else
			fprintf(fp, "Acknowledgement\t: %u\n", ntohl(tcphdr->ack));

		if( TCPOFF(flag) & 0x0f )
			fprintf(fp, "\x1b[31;43mOffset\x1b[0m\t\t: \x1b[31;43m%d\x1b[0m\n", tcphdr->doff);
		else
			fprintf(fp, "Offset\t\t: %d\n", tcphdr->doff);

		if( TCPRES(flag) & 0xf0 )
			fprintf(fp, "\x1b[31;43mReserved\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", tcphdr->res1);
		else
			fprintf(fp, "Reserved\t: %d\n", tcphdr->res1);

		char *tmp = getflag(*(packet + 47), tcphdr->res1, tcphdr->res2);
		if( TCPFLAG(flag) )
			fprintf(fp, "\x1b[31;43mFlags\x1b[0m\t\t: \x1b[31;43m%s\x1b[0m\n", tmp);
		else
			fprintf(fp, "Flags\t\t: %s\n", tmp);
		free(tmp);

		if( WINDOW(flag) )
			fprintf(fp, "\x1b[31;43mWindow size\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->window));
		else
			fprintf(fp, "Window size\t: %d\n", ntohs(tcphdr->window));

		if( TCPCKSUM(flag) )
			fprintf(fp, "\x1b[31;43mChecksum\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->check));
		else
			fprintf(fp, "Checksum\t: %d\n", ntohs(tcphdr->check));

		if( URGPTR(flag) )
			fprintf(fp, "\x1b[31;43mUrgent Pointer\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->urg_ptr));
		else
			fprintf(fp, "Urgent Pointer\t: %d\n", ntohs(tcphdr->urg_ptr));
	}

	/* Payload */
	if( fltrload != NULL ) {
		const uint8_t *match = NULL;
		uint8_t highlight[MAXPAYLOAD];
		int fltrloadlen, unmatchlen = 0;
		int colorhex = 0, colorstr = 0;
		int i = 0, str = 0, colorlen = 0, highlen = 0;
		char ch;

		memset(highlight, 0, MAXPAYLOAD);

		packet += hdrlen;
		fltrloadlen = strlen(fltrload);

		fprintf(fp, "*** Payload ***\n");

		if( http != NULL ) {
			if( prot == 3 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "GET\x1b[0m", 7);
				highlen += 12;
				i += 3;

			}else if( prot == 5 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "PUT\x1b[0m", 7);
				highlen += 12;
				i += 3;

			}else if( prot == 7 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "POST\x1b[0m", 8);
				highlen += 13;
				i += 4;
			}
		}

		while( i < payloadlen ) {
			// move to next matching string
			match = memcmp_cont(packet + i, fltrload, fltrloadlen, payloadlen - i);
			if( match == NULL ) {
				unmatchlen = payloadlen - i;

				memcpy(highlight + highlen, packet + i, unmatchlen);
				highlen += unmatchlen;

				break;
			}else {

				if( http != NULL && match - packet > http - packet ) {
				// HTTP/1.1
					unmatchlen = http - (packet + i);

					memcpy(highlight + highlen, packet + i, unmatchlen);
					highlen += unmatchlen;
					i += unmatchlen;

					memcpy(highlight + highlen, "\x1b[46mHTTP/1.1\x1b[0m", 17);
					highlen += 17;
					i += 8;
					http = NULL;
				}else {
					unmatchlen = match - (packet + i);

					memcpy(highlight + highlen, packet + i, unmatchlen);
					highlen += unmatchlen;
					i += unmatchlen;

					memcpy(highlight + highlen, PAYLOADCOLOR, 5);
					highlen += 5;

					memcpy(highlight + highlen, packet + i, fltrloadlen);
					highlen += fltrloadlen;
					i += fltrloadlen;

					memcpy(highlight + highlen, NORMALCOLOR, 4);
					highlen += 4;
				}
			}
		}

		i = 0;
		while( i < highlen ) {
			ch = *(highlight + i);

			if( ch == '\x1b' ) {
				if( COLORTEST(i, PAYLOADCOLOR) == true ) {
					fprintf(fp, PAYLOADCOLOR);
					colorhex = 41;
					i += 5;
					colorlen += 5;
					continue;

				}else if( COLORTEST(i, HTTPCOLOR) == true ) {
					fprintf(fp, HTTPCOLOR);
					colorhex = 46;
					i += 5;
					colorlen += 5;
					continue;

				}else if( COLORTEST(i, NORMALCOLOR) == true ) {
					fprintf(fp, NORMALCOLOR);
					colorhex = 0;
					i += 4;
					colorlen += 4;
					continue;
				}
			}

			fprintf(fp, "%02X ", ch);
			i++;

			if( ++str == 16 ) {
				if( colorhex != 0 )
					fprintf(fp, NORMALCOLOR);

				fprintf(fp, "   ");

				if( colorstr == 41 ) {
					fprintf(fp, PAYLOADCOLOR);
					colorstr = 0;

				}else if( colorstr == 46 ) {
					fprintf(fp, HTTPCOLOR);
					colorstr = 0;
				}

				while( colorlen > 0 ) {
					ch = *(highlight + i - colorlen - str);
					if( ch > 126 || ch < 32 ) {
						if( ch == 27 ) {
							if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
								colorstr = 41;

							else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
								colorstr = 46;

							else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
								colorstr = 0;

							else
								ch = '.';
						}else
							ch = '.';
					}
					fprintf(fp, "%c", ch);
					colorlen--;
				}

				while( str > 0 ) {
					ch = *(highlight + i - str);
					if( ch > 126 || ch < 32 ) {
						if( ch == 27 ) {
							if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
								colorstr = 41;
							else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
								colorstr = 46;
							else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
								colorstr = 0;
							else
								ch = '.';
						}else
							ch = '.';
					}
					fprintf(fp, "%c", ch);
					str--;
				}

				if( colorstr != 0 )
					fprintf(fp, NORMALCOLOR);

				fprintf(fp, "\n");

				if( colorhex == 41 ) {
					fprintf(fp, PAYLOADCOLOR);
					colorhex = 0;

				}else if( colorhex == 46 ) {
					fprintf(fp, HTTPCOLOR);
					colorhex = 0;
				}
			}
		}

		if( str != 0 ) {
			int padd = 17;
			if( colorhex != 0 )
				fprintf(fp, NORMALCOLOR);

			while( str < padd-- )
				fprintf(fp, "   ");

			if( colorstr == 41 ) {
				fprintf(fp, PAYLOADCOLOR);
				colorstr = 0;

			}else if( colorstr == 46 ) {
				fprintf(fp, HTTPCOLOR);
				colorstr = 0;
			}

			while( colorlen > 0 ) {
				ch = *(highlight + i - colorlen - str);
				if( ch > 126 || ch < 32 ) {
					if( ch == 27 ) {
						if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
							colorstr = 41;
						else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
							colorstr = 46;
						else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
							colorstr = 0;
						else
							ch = '.';
					}else
						ch = '.';
				}
				fprintf(fp, "%c", ch);
				colorlen--;
			}

			while( str > 0 ) {
				ch = *(highlight + i - str);
				if( ch > 126 || ch < 32 ) {
					if( ch == 27 ) {
						if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
							colorstr = 41;
						else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
							colorstr = 46;
						else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
							colorstr = 0;
						else
							ch = '.';
					}else
						ch = '.';
				}
				fprintf(fp, "%c", ch);
				str--;
			}
			fprintf(fp, "\x1b[0m\n");
		}
	}else { // fltrload == NULL
		uint8_t highlight[MAXPAYLOAD];
		int unmatchlen = 0;
		int colorhex = 0, colorstr = 0;
		int i = 0, str = 0, colorlen = 0, highlen = 0;
		char ch;

		memset(highlight, 0, MAXPAYLOAD);

		packet += hdrlen;

		fprintf(fp, "*** Payload ***\n");

		if( http != NULL ) {
			if( prot == 3 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "GET\x1b[0m", 7);
				highlen += 12;
				i += 3;

			}else if( prot == 5 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "PUT\x1b[0m", 7);
				highlen += 12;
				i += 3;

			}else if( prot == 7 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "POST\x1b[0m", 8);
				highlen += 13;
				i += 4;
			}
		}

		while( i < payloadlen ) {
		// move to next matching string
			if( http != NULL ) {
			// HTTP/1.1
				unmatchlen = http - (packet + i);

				memcpy(highlight + highlen, packet + i, unmatchlen);
				highlen += unmatchlen;
				i += unmatchlen;

				memcpy(highlight + highlen, "\x1b[46mHTTP/1.1\x1b[0m", 17);
				highlen += 17;
				i += 8;
				http = NULL;
			}else {
				memcpy(highlight + highlen, packet + i, payloadlen - i);
				highlen += unmatchlen;
				i += unmatchlen;
			}
		}

		i = 0;
		while( i < highlen ) {
			ch = *(highlight + i);

			if( ch == '\x1b' ) {
				if( COLORTEST(i, PAYLOADCOLOR) == true ) {
					fprintf(fp, PAYLOADCOLOR);
					colorhex = 41;
					i += 5;
					colorlen += 5;
					continue;

				}else if( COLORTEST(i, HTTPCOLOR) == true ) {
					fprintf(fp, HTTPCOLOR);
					colorhex = 46;
					i += 5;
					colorlen += 5;
					continue;

				}else if( COLORTEST(i, NORMALCOLOR) == true ) {
					fprintf(fp, NORMALCOLOR);
					colorhex = 0;
					i += 4;
					colorlen += 4;
					continue;
				}
			}

			fprintf(fp, "%02X ", ch);
			i++;

			if( ++str == 16 ) {
				if( colorhex != 0 )
					fprintf(fp, NORMALCOLOR);

				fprintf(fp, "   ");

				if( colorstr == 41 ) {
					fprintf(fp, PAYLOADCOLOR);
					colorstr = 0;

				}else if( colorstr == 46 ) {
					fprintf(fp, HTTPCOLOR);
					colorstr = 0;
				}

				while( colorlen > 0 ) {
					ch = *(highlight + i - colorlen - str);
					if( ch > 126 || ch < 32 ) {
						if( ch == 27 ) {
							if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
								colorstr = 41;
							else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
								colorstr = 46;
							else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
								colorstr = 0;
							else
								ch = '.';
						}else
							ch = '.';
					}
					fprintf(fp, "%c", ch);
					colorlen--;
				}

				while( str > 0 ) {
					ch = *(highlight + i - str);
					if( ch > 126 || ch < 32 ) {
						if( ch == 27 ) {
							if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
								colorstr = 41;
							else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
								colorstr = 46;
							else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
								colorstr = 0;
							else
								ch = '.';
						}else
							ch = '.';
					}
					fprintf(fp, "%c", ch);
					str--;
				}

				if( colorstr != 0 )
					fprintf(fp, NORMALCOLOR);

				fprintf(fp, "\n");

				if( colorhex == 41 ) {
					fprintf(fp, PAYLOADCOLOR);
					colorhex = 0;

				}else if( colorhex == 46 ) {
					fprintf(fp, HTTPCOLOR);
					colorhex = 0;
				}
			}
		}

		if( str != 0 ) {
			int padd = 17;
			if( colorhex != 0 )
				fprintf(fp, NORMALCOLOR);

			while( str < padd-- )
				fprintf(fp, "   ");

			if( colorstr == 41 ) {
				fprintf(fp, PAYLOADCOLOR);
				colorstr = 0;

			}else if( colorstr == 46 ) {
				fprintf(fp, HTTPCOLOR);
				colorstr = 0;
			}

			while( colorlen > 0 ) {
				ch = *(highlight + i - colorlen - str);
				if( ch > 126 || ch < 32 ) {
					if( ch == 27 ) {
						if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
							colorstr = 41;
						else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
							colorstr = 46;
						else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
							colorstr = 0;
						else
							ch = '.';
					}else
						ch = '.';
				}
				fprintf(fp, "%c", ch);
				colorlen--;
			}

			while( str > 0 ) {
				ch = *(highlight + i - str);
				if( ch > 126 || ch < 32 ) {
					if( ch == 27 ) {
						if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
							colorstr = 41;
						else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
							colorstr = 46;
						else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
							colorstr = 0;
						else
							ch = '.';
					}else
						ch = '.';
				}
				fprintf(fp, "%c", ch);
				str--;
			}
			fprintf(fp, "\x1b[0m\n");
		}
	}
	fprintf(fp, "----------------------------\n");
	fclose(fp);
}