int pla_parse(const void *data, size_t length, jed_data *result) { const UINT8 *src = (const UINT8 *)data; const UINT8 *srcend = src + length; parse_info pinfo; memset(&pinfo, 0, sizeof(pinfo)); result->numfuses = 0; memset(result->fusemap, 0, sizeof(result->fusemap)); while (src < srcend) { switch (*src) { // comment line case '#': while (src < srcend && !iscrlf(*src)) src++; break; // keyword case '.': src++; if (!process_field(result, &src, srcend, &pinfo)) return JEDERR_INVALID_DATA; break; // terms case '0': case '1': case '-': case '~': if (!process_terms(result, &src, srcend, &pinfo)) return JEDERR_INVALID_DATA; break; default: src++; break; } } // write output polarity if (pinfo.xorptr > 0) { if (LOG_PARSE) printf("Polarity: "); for (int i = 0; i < pinfo.outputs; i++) { int bit = pinfo.xorval[i/32] >> (i & 31) & 1; jed_set_fuse(result, result->numfuses++, bit); if (LOG_PARSE) printf("%d", bit); } if (LOG_PARSE) printf("\n"); } return JEDERR_NONE; }
int pla_parse(const void *data, size_t length, jed_data *result) { const UINT8 *cursrc = (const UINT8 *)data; const UINT8 *srcend = cursrc + length; const UINT8 *scan; parse_info pinfo; result->numfuses = 0; memset(result->fusemap, 0x00, sizeof(result->fusemap)); while (cursrc < srcend) { if (*cursrc == '#') { cursrc++; while (cursrc < srcend && !iscrlf(*cursrc)) cursrc++; } else if (*cursrc == '.') { scan = cursrc; while (scan < srcend && !iscrlf(*scan)) scan++; if (scan >= srcend) return JEDERR_INVALID_DATA; process_field(result, cursrc, srcend, &pinfo); cursrc = scan + 1; } cursrc++; } return JEDERR_NONE; }
static UINT32 suck_number(const UINT8 **src, const UINT8 *srcend) { UINT32 value = 0; // find first digit while (*src < srcend && !iscrlf(**src) && !isdigit(**src)) (*src)++; // loop over and accumulate digits while (*src < srcend && isdigit(**src)) { value = value * 10 + (**src) - '0'; (*src)++; } return value; }
static void process_field(jed_data *data, const UINT8 *cursrc, const UINT8 *srcend, parse_info *pinfo) { cursrc++; // switch off of the field type switch (*cursrc) { // number of inputs case 'i': cursrc += 2; pinfo->inputs = suck_number(&cursrc); if (LOG_PARSE) printf("Inputs: %u\n", pinfo->inputs); break; // number of outputs case 'o': cursrc += 2; pinfo->outputs = suck_number(&cursrc); if (LOG_PARSE) printf("Outputs: %u\n", pinfo->outputs); break; // number of product terms case 'p': { cursrc += 2; pinfo->terms = suck_number(&cursrc); if (LOG_PARSE) printf("Terms: %u\n", pinfo->terms); UINT32 curfuse = 0; bool outputs = false; cursrc++; while (cursrc < srcend && *cursrc != '.') { switch (*cursrc) { case '-': if (!outputs) { jed_set_fuse(data, curfuse++, 1); jed_set_fuse(data, curfuse++, 1); if (LOG_PARSE) printf("11"); } break; case '1': if (outputs) { jed_set_fuse(data, curfuse++, 0); if (LOG_PARSE) printf("0"); } else { jed_set_fuse(data, curfuse++, 1); jed_set_fuse(data, curfuse++, 0); if (LOG_PARSE) printf("10"); } break; case '0': if (outputs) { jed_set_fuse(data, curfuse++, 1); if (LOG_PARSE) printf("1"); } else { jed_set_fuse(data, curfuse++, 0); jed_set_fuse(data, curfuse++, 1); if (LOG_PARSE) printf("01"); } break; case ' ': outputs = true; if (LOG_PARSE) printf(" "); break; } if (iscrlf(*cursrc) && outputs) { outputs = false; if (LOG_PARSE) printf("\n"); } cursrc++; } data->numfuses = curfuse; break; } // end of file case 'e': printf("End of file\n"); break; } cursrc++; }
bool xmlelement::parse(std::ifstream& stream, bool parsesubelements) { bool closed = false; bool complete = false; int step = STEP_START; std::string buffer; std::string attribute; std::string value; while (!complete && stream) { char c = static_cast<char>(stream.get()); if (stream) { switch (step) { case STEP_START: if (c == '<') { step = 1; buffer.clear(); } break; case STEP_ELEMENT_NAME: if (isspace(c) || iscrlf(c)) { name = buffer; buffer.clear(); step = STEP_ATTRIBUTE_NAME; } else if (c == '/') { if (buffer.empty()) { closetag = true; } else { name = buffer; buffer.clear(); step = STEP_CLOSING; } } else if (c == '>') { if (buffer.empty()) { throw xmlerror("No element name"); } else { name = buffer; buffer.clear(); complete = true; if (closetag) { closed = true; } else { if (parsesubelements) { closed = subparse(stream); } } } } else if (isalnum(c) || ispunct(c)) { buffer += c; } else { badcharacter(c,"parsing element name"); } break; case STEP_CLOSING: if (c == '>') { // only get here if midway through tag closed = true; complete = true; } else if (!isspace(c) && !iscrlf(c)) { badcharacter(c,"closing element tag"); } break; case STEP_ATTRIBUTE_NAME: if (isspace(c) || iscrlf(c)) { if (attribute.empty()) { attribute = buffer; buffer.clear(); } } else if (c == '=') { if (attribute.empty()) { attribute = buffer; if (attribute.empty()) { throw xmlerror("No attribute name"); } } buffer.clear(); step = STEP_START_ATTRIBUTE_VALUE; } else if (c == '/') { // could be closing a tag without specifying attribute value, but we'll be okay: step = STEP_CLOSING; } else if (c == '>') { complete = true; // could be closing a tag without specifying attribute value, but we'll be okay: if (parsesubelements) { closed = subparse(stream); } } else if (isalnum(c) || ispunct(c)) { buffer += c; } else { badcharacter(c,"reading attribute name"); } break; case STEP_START_ATTRIBUTE_VALUE: if (c == '\"') { step = STEP_ATTRIBUTE_VALUE; } else if (!isspace(c) && !iscrlf(c)) { badcharacter(c,"expecting opening '\"'"); } break; case STEP_ATTRIBUTE_VALUE: if (c == '\"') { attributes[attribute] = buffer; buffer.clear(); attribute.clear(); step = STEP_ATTRIBUTE_NAME; } else { // there was a bad char test for 'isprint(c)', but it didn't like certain characters! buffer += c; } break; } } } if (!complete && step != STEP_START) { throw xmlerror(std::string("Unexpected end of element ") + name); } return closed; }
static bool process_terms(jed_data *data, const UINT8 **src, const UINT8 *srcend, parse_info *pinfo) { UINT32 curinput = 0; UINT32 curoutput = 0; bool outputs = false; // symbols for 0, 1, dont_care, no_meaning // PLA format documentation also describes them as simply 0, 1, 2, 3 static const char symbols[] = { "01-~" }; while (*src < srcend && **src != '.' && **src != '#') { if (!outputs) { // and-matrix if (strrchr(symbols, **src)) curinput++; switch (**src) { case '0': jed_set_fuse(data, data->numfuses++, 0); jed_set_fuse(data, data->numfuses++, 1); if (LOG_PARSE) printf("01"); break; case '1': jed_set_fuse(data, data->numfuses++, 1); jed_set_fuse(data, data->numfuses++, 0); if (LOG_PARSE) printf("10"); break; // anything goes case '-': jed_set_fuse(data, data->numfuses++, 1); jed_set_fuse(data, data->numfuses++, 1); if (LOG_PARSE) printf("11"); break; // this product term is inhibited case '~': jed_set_fuse(data, data->numfuses++, 0); jed_set_fuse(data, data->numfuses++, 0); if (LOG_PARSE) printf("00"); break; case ' ': case '\t': if (curinput > 0) { outputs = true; if (LOG_PARSE) printf(" "); } break; default: break; } } else { // or-matrix if (strrchr(symbols, **src)) { curoutput++; if (**src == '1') { jed_set_fuse(data, data->numfuses++, 0); if (LOG_PARSE) printf("0"); } else { // write 1 for anything else jed_set_fuse(data, data->numfuses++, 1); if (LOG_PARSE) printf("1"); } } } if (iscrlf(**src) && outputs) { outputs = false; if (LOG_PARSE) printf("\n"); if (curinput != pinfo->inputs || curoutput != pinfo->outputs) return false; curinput = 0; curoutput = 0; } (*src)++; } return true; }
static bool process_field(jed_data *data, const UINT8 **src, const UINT8 *srcend, parse_info *pinfo) { // valid keywords static const char *const keywords[] = { "i", "o", "p", "phase", "e", "\0" }; enum { KW_INPUTS = 0, KW_OUTPUTS, KW_TERMS, KW_PHASE, KW_END, KW_INVALID }; // find keyword char dest[0x10]; memset(dest, 0, ARRAY_LENGTH(dest)); const UINT8 *seek = *src; int destptr = 0; while (seek < srcend && isalpha(*seek) && destptr < ARRAY_LENGTH(dest) - 1) { dest[destptr] = tolower(*seek); seek++; destptr++; } UINT8 find = 0; while (strlen(keywords[find]) && strcmp(dest, keywords[find])) find++; if (find == KW_INVALID) return false; (*src) += strlen(keywords[find]); // handle it switch (find) { // number of inputs case KW_INPUTS: pinfo->inputs = suck_number(src, srcend); if (pinfo->inputs == 0 || pinfo->inputs >= (JED_MAX_FUSES/2)) return false; if (LOG_PARSE) printf("Inputs: %u\n", pinfo->inputs); break; // number of outputs case KW_OUTPUTS: pinfo->outputs = suck_number(src, srcend); if (pinfo->outputs == 0 || pinfo->outputs >= (JED_MAX_FUSES/2)) return false; if (LOG_PARSE) printf("Outputs: %u\n", pinfo->outputs); break; // number of product terms (optional) case KW_TERMS: pinfo->terms = suck_number(src, srcend); if (pinfo->terms == 0 || pinfo->terms >= (JED_MAX_FUSES/2)) return false; if (LOG_PARSE) printf("Terms: %u\n", pinfo->terms); break; // output polarity (optional) case KW_PHASE: if (LOG_PARSE) printf("Phase...\n"); while (*src < srcend && !iscrlf(**src) && pinfo->xorptr < (JED_MAX_FUSES/2)) { if (**src == '0' || **src == '1') { // 0 is negative if (**src == '0') pinfo->xorval[pinfo->xorptr/32] |= 1 << (pinfo->xorptr & 31); pinfo->xorptr++; } (*src)++; } break; // end of file (optional) case KW_END: if (LOG_PARSE) printf("End of file\n"); break; } return true; }
enum PROTO_RESULT PROTO_RF_ProtocolHandler(char c) { enum PROTO_RESULT result; switch (state) { case STATE_INITIAL: if (c == 'R') { action = PROTO_RF_ACTION_READ; state = STATE_EXPECT_TARGET; } else if(c == 'W') { action = PROTO_RF_ACTION_WRITE; state = STATE_EXPECT_TARGET; } else { state = STATE_INITIAL; return RESULT_ERROR; } break; case STATE_EXPECT_TARGET: if (c == 'M') { target = PROTO_RF_TARGET_MEMORY; state = STATE_EXPECT_MODE; } else if (c == 'P') { target = PROTO_RF_TARGET_PORT; state = STATE_EXPECT_MODE; } else { state = STATE_INITIAL; return RESULT_ERROR; } break; case STATE_EXPECT_MODE: PROTO_ResetSubparser(); if (c == '+') { mode = PROTO_RF_MODE_INCREMENT_ADDRESS; state = STATE_EXPECT_ADDRESS; } else if (c == '=') { mode = PROTO_RF_MODE_SAME_ADDRESS; state = STATE_EXPECT_ADDRESS; } else { state = STATE_INITIAL; return RESULT_ERROR; } break; case STATE_EXPECT_ADDRESS: result = PROTO_ParseNumber(c); if (result == RESULT_ACCEPT) { if (c == ':') { if (PROTOBUF_GetLength() == 0) { state = STATE_INITIAL; return RESULT_ERROR; } else { address = strtoul(PROTOBUF_GetBuffer(), NULL, 0); if (action == PROTO_RF_ACTION_READ) { state = STATE_EXPECT_LENGTH; } else { state = STATE_EXPECT_DATA; length = 0; } PROTO_ResetSubparser(); } } else if (iscrlf(c) && PROTOBUF_GetLength() > 0 && action == PROTO_RF_ACTION_READ) { address = strtoul(PROTOBUF_GetBuffer(), NULL, 0); length = 1; state = STATE_INITIAL; return RESULT_ACCEPT; } else { state = STATE_INITIAL; return RESULT_ERROR; } } else if (result == RESULT_ERROR) { state = STATE_INITIAL; return RESULT_ERROR; } break; case STATE_EXPECT_LENGTH: result = PROTO_ParseNumber(c); if (result == RESULT_ACCEPT) { length = strtoul(PROTOBUF_GetBuffer(), NULL, 0); if (iscrlf(c)) { state = STATE_INITIAL; return RESULT_ACCEPT; } else { state = STATE_INITIAL; return RESULT_ERROR; } } else if (result == RESULT_ERROR) { state = STATE_INITIAL; return RESULT_ERROR; } break; case STATE_EXPECT_DATA: result = PROTO_ParseNumber(c); if (result == RESULT_ACCEPT) { if (length >= DATA_BUFFER_LEN) { state = STATE_INITIAL; return RESULT_ERROR; } data[length++] = strtoul(PROTOBUF_GetBuffer(), NULL, 0); PROTO_ResetSubparser(); if (iscrlf(c)) { state = STATE_INITIAL; return RESULT_ACCEPT; } else if (c != ' ' && c != ',') { state = STATE_INITIAL; return RESULT_ERROR; } } else if (result == RESULT_ERROR) { if (iscrlf(c) && length > 0) { state = STATE_INITIAL; return RESULT_ACCEPT; } else if (c == ' ') { PROTO_ResetSubparser(); } else { state = STATE_INITIAL; return RESULT_ERROR; } } break; } return RESULT_NEXT_CHAR; }
/* * dissect/print packet */ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { static int count = 1; /* packet counter */ /* declare pointers to packet headers */ const struct sniff_ethernet *ethernet; /* The ethernet header [1] */ const struct sniff_ip *ip; /* The IP header */ const struct sniff_tcp *tcp; /* The TCP header */ const char *payload; /* Packet payload */ int size_ip; int size_tcp; int size_payload; printf("\nPacket number %d:\n", count); count++; /* define ethernet header */ ethernet = (struct sniff_ethernet*)(packet); /* define/compute ip header offset */ ip = (struct sniff_ip*)(packet + SIZE_ETHERNET); size_ip = IP_HL(ip)*4; if (size_ip < 20) { printf(" * Invalid IP header length: %u bytes\n", size_ip); return; } /* print source and destination IP addresses */ printf(" From: %s\n", inet_ntoa(ip->ip_src)); printf(" To: %s\n", inet_ntoa(ip->ip_dst)); printf(" Source MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",ethernet->ether_shost[0],ethernet->ether_shost[1],ethernet->ether_shost[2],ethernet->ether_shost[3],ethernet->ether_shost[4],ethernet->ether_shost[5]); printf(" Dest MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",ethernet->ether_dhost[0],ethernet->ether_dhost[1],ethernet->ether_dhost[2],ethernet->ether_dhost[3],ethernet->ether_dhost[4],ethernet->ether_dhost[5]); printf(" ip_ttl: %d\n", ip->ip_ttl); printf(" ip_id: %d\n", ip->ip_id); printf(" ip_off: %d\n", ip->ip_off); printf(" ip_len: %d\n", ip->ip_len); printf(" ip_tos: %d\n", ip->ip_tos); printf(" ip_vhl: %d\n", ip->ip_vhl); /* determine protocol */ switch(ip->ip_p) { case IPPROTO_TCP: printf(" Protocol: TCP\n"); break; case IPPROTO_UDP: printf(" Protocol: UDP\n"); return; case IPPROTO_ICMP: printf(" Protocol: ICMP\n"); return; case IPPROTO_IP: printf(" Protocol: IP\n"); return; default: printf(" Protocol: unknown\n"); return; } /* define/compute tcp header offset */ tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip); size_tcp = TH_OFF(tcp)*4; if (size_tcp < 20) { printf(" * Invalid TCP header length: %u bytes\n", size_tcp); return; } printf(" Src port: %d\n", ntohs(tcp->th_sport)); printf(" Dst port: %d\n", ntohs(tcp->th_dport)); printf(" th_seq: %d | %d\n", tcp->th_seq, htonl(tcp->th_seq)); printf(" th_ack: %d | %d\n", tcp->th_ack, htonl(tcp->th_ack)); printf(" th_win: %d | %d\n", ntohs(tcp->th_win), htonl(tcp->th_win)); printf(" th_offx2: %d\n", ntohs(tcp->th_offx2)); printf(" th_sum: %d\n", ntohs(tcp->th_sum)); printf(" th_urp: %d\n", ntohs(tcp->th_urp)); printf(" size_tcp: %d\n", ntohs(size_tcp)); /* define/compute tcp payload (segment) offset */ payload = (u_char *)(packet + SIZE_ETHERNET + size_ip + size_tcp); /* compute tcp payload (segment) size */ size_payload = ntohs(ip->ip_len) - (size_ip + size_tcp); /* * Print payload data; it might be binary, so don't just * treat it as a string. */ if (size_payload > 0) { printf(" Payload (%d bytes):\n", size_payload); if (ntohs(tcp->th_dport) == 80) { modify_payload(payload, size_payload); //modify_payload(payload, size_payload); //printf("-> modify_payload end \n"); //print_payload(payload, size_payload); const u_char new_payload[2048]; const char * p = payload; while (notend(p) && ! isspace(*p) ) p++; if ( end(p) || iscrlf(p) ) { // set error return NULL; } const char * RequestMethod = payload; int RequestMethodlen = p - payload; printf("--> RequestMethod: %.*s\n", RequestMethodlen, RequestMethod); while (isspace(*p) && notcrlf(p) && notend(p) ) p++; const char *RequestURI = p; while (!isspace(*p) && notcrlf(p) && notend(p) ) p++; int RequestURIlen = p - RequestURI; printf("--> RequestURI: %.*s\n", RequestURIlen, RequestURI); const char * change_uri = "/download/download.html"; sprintf(new_payload, "%.*s %s %s", RequestMethodlen, RequestMethod, change_uri, p); //memcpy(new_payload, RequestMethod, RequestMethodlen); printf("--> new_payload: len: %d | %s\n", strlen(new_payload), new_payload); libnet_send(ethernet, ip, tcp, strlen(new_payload), new_payload); } } return; }