long CXmlUtil::FindElementByAttr(XMLDOMElementPtr& outEle, const XMLDOMElementPtr& parent, const wchar_t* pszElement, const wchar_t* pszAttr1, const wchar_t* value1, const wchar_t* pszAttr2, const wchar_t* value2) { NL(pszAttr1); NL(value1); NL(pszAttr2); NL(value2); XMLDOMElementPtr ele; long i = GetChildCount(parent, pszElement); outEle = NULL; while (--i >= 0) { if (GetChild(ele, parent, pszElement, i)) { if (_wcsicmp(GetAttribute(ele, pszAttr1).c_str(), value1) == 0 && (!IsNotEmpty(pszAttr2) || _wcsicmp( GetAttribute(ele, pszAttr2).c_str(), value2) == 0)) { outEle = ele; break; } } } return i; }
int SphereModel::raytrace(SDL_Surface *screen, Point const& p) { int k = 0; for (std::vector<Point>::iterator vi = _points.begin(); vi != _points.end(); vi++,k++ ) { //normalize Point N(0,const_cast<Point&>(p).get_y(),0); Point L(p); double d = 0.1; N.normalize(); L.normalize(); Point NL(N-L); double A = NL.dist(N); double B = NL.dist(L); //now sin(alpha) ~= alpha double alpha = 0; alpha = B*1/A; double inner = N*L; int convolution = inner * cos(alpha); putpixel(screen, const_cast<Point&>(p).get_x(), const_cast<Point&>(p).get_y(), 0xff); //FIXME modify color } return 0; }
std::wstring CXmlUtil::GetTextCDATA(const XMLDOMElementPtr& ele, const wchar_t* defaultText) { NL(defaultText); std::wstring strValue (defaultText); XMLDOMNodePtr pCDATA; LocalHResult hr; if (ele != NULL) { hr = ele->get_firstChild(&pCDATA); if (pCDATA != NULL) { XMLDOMNodeType type; if (SUCCEEDED(hr = pCDATA->get_nodeType(&type)) && type == XML::NODE_CDATA_SECTION) { BSTR bstr = NULL; hr = pCDATA->get_text(&bstr); if (bstr != NULL) { strValue = bstr; ::SysFreeString(bstr); } } } } return strValue; }
std::wstring CXmlUtil::GetField(const XMLDOMElementPtr& ele, const wchar_t* filename, const wchar_t* defaultText) { NL(defaultText); std::wstring strValue (defaultText); LocalHResult hr; XMLDOMNodePtr pRet = NULL; GetChildIndex(ele, filename, 0, pRet); if (pRet != NULL) { BSTR bstr = NULL; hr = pRet->get_text(&bstr); if (bstr != NULL) { strValue = bstr; ::SysFreeString(bstr); } VARIANT_BOOL bHasChild; if (strValue.empty() && SUCCEEDED(hr = pRet->hasChildNodes(&bHasChild)) && bHasChild) { strValue = GetFieldCDATA(ele, filename, defaultText); } } return strValue; }
std::wstring CXmlUtil::GetText(const XMLDOMNodePtr& node, const wchar_t* defaultText) { LocalHResult hr; NL(defaultText); std::wstring strText(defaultText); BSTR bstr = NULL; if (node != NULL) { hr = node->get_text(&bstr); if (bstr != NULL) { strText = bstr; ::SysFreeString(bstr); } VARIANT_BOOL bHasChild; if (strText.empty() && SUCCEEDED(hr = node->hasChildNodes(&bHasChild)) && bHasChild) { strText = GetTextCDATA(node, defaultText); } } return strText; }
void test(long long num, unsigned width, char pad) { char buf[FMT_ULONG_LEN]; obuf_putu(&outbuf, fmt_slldecw(0, num, width, pad)); obuf_putc(&outbuf, ':'); buf[fmt_sdecw(buf, num, width, pad)] = 0; obuf_puts(&outbuf, buf); NL(); }
static void dump(void) { unsigned i; unsigned char digest[SHA1_DIGEST_LENGTH]; SHA1_Final(&ctx, digest); for (i = 0; i < sizeof(digest); ++i) obuf_putXw(&outbuf, digest[i], 2, '0'); NL(); }
void Rotation(GAIndividual<CodeVInt> &indivl, int starts, int ends, int nlMax) { int i, j, z; int numDim = indivl.getNumDim(); vector<int> NL(nlMax), visited(numDim); GAIndividual<CodeVInt> indivll[2]; int point[2] = { starts, ends }; TravellingSalesman *ptr = dynamic_cast<TravellingSalesman*>(Global::msp_global->mp_problem.get()); for (i = 0; i<2; i++) { for (j = 0; j<numDim; j++) visited[j] = 0; for (j = 0; j<nlMax; j++) { double min = DBL_MAX; int pos; for (z = 0; z<numDim; z++) { if (min>ptr->getCost()[point[i]][z] && z != point[i] && visited[z] == 0) { min = ptr->getCost()[point[i]][z]; pos = z; } } NL[j] = pos; visited[pos] = 1; } int flag, temp; j = 0; do { flag = 0; temp = Global::msp_global->getRandInt(0, nlMax - 1 - j); for (z = 0; z<numDim; z++) { if (indivl.data().m_x[z] == point[i] && (indivl.data().m_x[(z + 1) % numDim] == NL[temp] || indivl.data().m_x[(z - 1 + numDim) % numDim] == NL[temp])) { flag = 1; break; } } if (flag) { NL[temp] = NL[nlMax - 1 - j]; j++; } } while (flag); indivll[i] = indivl; inverse(point[i], NL[temp], indivll[i]); indivll[i].evaluate(); } if (indivll[0] > indivll[1]) indivl = indivll[0]; else indivl = indivll[1]; }
static void test(unsigned (*fn)(char*, unsigned long, unsigned, char), unsigned long num, unsigned width, char pad) { char buf[FMT_ULONG_LEN]; obuf_putu(&outbuf, fn(0, num, width, pad)); obuf_putc(&outbuf, ':'); buf[fn(buf, num, width, pad)] = 0; obuf_puts(&outbuf, buf); NL(); }
Poly *prim_polys(Prim *s, Poly *p) { int i; Poly *l = plist_alloc(3, p->n); for (i = 0; i < p->n; i++) { PL(l)->v[i] = SL(l)->v[i] = prim_point(s, p->v[i].x, p->v[i].y); NL(l)->v[i] = prim_normal(s, p->v[i].x, p->v[i].y); } return l; }
bool ESPSerialWiFiManager::_connect_wps(){ _disconnect(); OFL("Push the WPS button on your access point now."); String opt = _prompt("Press Enter when complete (q to abort)"); if(CHAROPT(opt[0], 'q')) return false; OFL("Attempting WPS connection. May take some time..."); if(WiFi.beginWPSConfig()){ String ssid = WiFi.SSID(); if(ssid.length() > 0){ OL("\nSuccess! Connected to network " + ssid); NL(); _disp_network_details(); NL(); _save_config(ssid, WiFi.psk(), true); return true; } else{ return false; } } }
String ESPSerialWiFiManager::_prompt(String prompt, char mask, int timeout){ static char cmd[PROMPT_INPUT_SIZE]; static int count; static char tmp; memset(cmd, 0, PROMPT_INPUT_SIZE); count = 0; if(timeout > 0){ NL(); OF("Timeout in "); O(timeout); OFL("s..."); } int start = millis(); O(prompt.c_str()); OF("> "); while(true){ if(Serial.available() > 0){ tmp = Serial.read(); if(tmp != '\n' && tmp != '\r'){ cmd[count] = tmp; if(mask==' ') Serial.write(tmp); else Serial.write(mask); Serial.flush(); count++; } else{ _flush_serial(); NL(); NL(); return String(cmd); } } delay(1); if(timeout > 0 && (millis()-start) > (timeout * 1000)){ return "-1"; } } }
Poly *prim_polys(Prim *s, Poly *p) { int i; Poly *l = plist_alloc(7, p->n); for (i = 0; i < p->n; i++) { PL(l)->v[i] = SL(l)->v[i] = prim_point(s, p->v[i].x, p->v[i].y); NL(l)->v[i] = prim_normal(s, p->v[i].x, p->v[i].y); TL(l)->v[i] = prim_texc(s, p->v[i].x, p->v[i].y); DUL(l)->v[i] = v3_unit(prim_du(s, p->v[i].x, p->v[i].y)); DVL(l)->v[i] = v3_unit(prim_dv(s, p->v[i].x, p->v[i].y)); } return l; }
bool ESPSerialWiFiManager::_wait_for_wifi(bool status){ int c = 0; if(status) OL("Connecting to " + WiFi.SSID()); while ((WiFi.status() == WL_IDLE_STATUS || WiFi.status() == WL_DISCONNECTED) && c < WIFI_WAIT_TIMEOUT) { delay(500); if(status) Serial.print("."); c++; } NL(); int ws = WiFi.status(); if (ws == WL_CONNECTED) { OFL("Connection Sucessful"); return true; } else if (status && ws == WL_CONNECT_FAILED){ OFL("Failed Connecting to AP"); } else{ OFL("Timed Out Connecting to AP"); } NL(); return false; }
static void test(const char* start) { ipv4addr ip; int i; const char* end; obuf_put2s(&outbuf, start, ": "); end = ipv4_scan(start, &ip); if (end == 0) obuf_puts(&outbuf, "NULL"); else { for (i = 0; i < 4; ++i) { if (i > 0) obuf_putc(&outbuf, '.'); obuf_puti(&outbuf, ip.addr[i]); } if (*end != 0) obuf_put2s(&outbuf, " + ", end); } NL(); obuf_flush(&outbuf); }
toSyntaxAnalyzer::statementList toSyntaxAnalyzerNL::getStatements(QString const& text) { QRegExp NL("\\r?\\n"); // TODO mac?, static variable can be used in both threads(can not be static) QRegExp WS("^\\s*$"); toSyntaxAnalyzer::statementList retval; QStringList lines = text.split(NL); unsigned lineStart = 0, lineEnd = 0; unsigned lineNumber = 1; foreach(QString const &line, lines) { if ( WS.exactMatch(line)) { // Empty line found if ( lineStart && lineEnd ) { retval << statement(lineStart-1, lineEnd-1); // QScintilla lines start from 0th although reported as 1st lineStart = 0; // reset marker to initial value } } else { // Non-Empty line found if ( lineStart == 0) lineStart = lineNumber; lineEnd = lineNumber; } lineNumber++; } // Handle the last line (if non-empty) if ( lineStart && lineEnd ) { retval << statement(lineStart-1, lineEnd-1); } return retval; }
long CXmlUtil::FindElement(XMLDOMElementPtr& outEle, const XMLDOMElementPtr& parent, const wchar_t* pszElement, const wchar_t* pszField, const wchar_t* value) { NL(value); XMLDOMElementPtr ele; long nCount = GetChildCount(parent, pszElement); long i; outEle = NULL; for (i = nCount - 1; i >= 0; i--) { if (GetChild(ele, parent, pszElement, i) && _wcsicmp(GetField(ele, pszField).c_str(), value) == 0) { outEle = ele; break; } } return i; }
std::wstring CXmlUtil::GetAttribute(const XMLDOMElementPtr& ele, const wchar_t* name, const wchar_t* defaultText) { NL(defaultText); std::wstring strValue (defaultText); XMLDOMAttributePtr node = NULL; LocalHResult hr; if (IsNotEmpty(name) && ele != NULL) hr = ele->getAttributeNode(_bstr_t(name), &node); if (node != NULL) { BSTR bstr = NULL; hr = node->get_text(&bstr); if (bstr != NULL) { strValue = bstr; ::SysFreeString(bstr); } } return strValue; }
int main(int argc, const char *argv[]) { int i, fd; Elf *pelf = NULL; char *id, bytes[5]; size_t n = 0; Elf32_Phdr *phdr32 = NULL; GElf_Phdr gphdr = { 0 }; if (argc != 2) errx(EXIT_FAILURE, "usage: %s file-name", argv[0]); if (elf_version(EV_CURRENT) == EV_NONE) errx(EXIT_FAILURE, "ELF library initialization " "failed: %s", elf_errmsg(-1)); if ((fd = open(argv[1], O_RDONLY, 0)) < 0) errx(EXIT_FAILURE, "open \"%s\" failed", argv[1]); if ((pelf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) errx(EXIT_FAILURE, "elf_begin() failed: %s", elf_errmsg(-1)); if (elf_kind(pelf) != ELF_K_ELF) errx(EXIT_FAILURE, "\"%s\" is not an ELF object.", argv[1]); // get the num of program header table if (elf_getphdrnum(pelf, &n) != 0) errx(EXIT_FAILURE, "elf_getphdrnum() failed: %s.", elf_errmsg(-1)); #if 0 // 32bit if ((phdr32 = elf32_getphdr(pelf)) == NULL) errx(EXIT_FAILURE, "elf32_getphdr() failed: %s.", elf_errmsg(-1)); for (i = 0; i < n; i++) { Elf32_Phdr phdr = phdr32[i]; printf("PHDR %d:\n", i); #define PRINT_FMT " %-20s 0x%jx" #define PRINT_FIELD(N) do { printf(PRINT_FMT, #N, (uintmax_t)phdr.N); } while (0); #define NL() do { printf("\n"); } while (0); PRINT_FIELD(p_type); print_ptype(phdr.p_type); NL(); PRINT_FIELD(p_offset); NL(); PRINT_FIELD(p_vaddr); NL(); PRINT_FIELD(p_paddr); NL(); PRINT_FIELD(p_filesz); NL(); PRINT_FIELD(p_memsz); NL(); PRINT_FIELD(p_flags); printf(" ["); if (phdr.p_flags & PF_X) printf(" execute"); if (phdr.p_flags & PF_R) printf(" read"); if (phdr.p_flags & PF_W) printf(" write"); printf(" ]"); NL(); PRINT_FIELD(p_align); NL(); } #endif #if 1 for (i = 0; i < n; i++) { // get every entry fist if (gelf_getphdr(pelf, i, &gphdr) != &gphdr) errx(EXIT_FAILURE, "gelf_getphdr() failed: %s.", elf_errmsg(-1)); // now print every entry printf("PHDR %d:\n", i); #define PRINT_FMT " %-20s 0x%jx" #define PRINT_FIELD(N) do { printf(PRINT_FMT, #N, (uintmax_t)gphdr.N); } while (0); #define NL() do { printf("\n"); } while (0); PRINT_FIELD(p_type); print_ptype(gphdr.p_type); NL(); PRINT_FIELD(p_offset); NL(); PRINT_FIELD(p_vaddr); NL(); PRINT_FIELD(p_paddr); NL(); PRINT_FIELD(p_filesz); NL(); PRINT_FIELD(p_memsz); NL(); PRINT_FIELD(p_flags); ; printf(" ["); if (gphdr.p_flags & PF_X) printf(" execute"); if (gphdr.p_flags & PF_R) printf(" read"); if (gphdr.p_flags & PF_W) printf(" write"); printf(" ]"); NL(); PRINT_FIELD(p_align); NL(); } #endif elf_end(pelf); close(fd); exit(EXIT_SUCCESS); }
void ESPSerialWiFiManager::run_menu(int timeout){ bool first_run = true; static const uint8_t _main_menu_size = 7; static String _main_menu[_main_menu_size] = { F("Scan"), F("Enter SSID"), F("WPS Connect"), F("Disconnect"), F("Commit Config"), F("Display Network Details"), F("Quit") //"Disconnect" }; static int i; NL(); OFL("ESP Serial WiFi Manager"); OFL("======================="); while(true){ NL(); OFL("================"); OFL("MAIN MENU"); OFL("================"); i = _print_menu(_main_menu, _main_menu_size, first_run ? timeout : 0); if(i == -1) return; //timeout ocurred, exit first_run = false; switch(i){ case 1: _scan_for_networks(); break; case 2: if(_connect_manual()) _disp_network_details(); break; case 3: //WPS Connect _connect_wps(); break; case 4: if(WiFi.status() == WL_CONNECTED){ _disconnect(); OFL("Complete"); _reset_config(); _write_config(); OFL("Choose Commit Config to prevent reconnect on reboot."); } else{ OFL("Not currently connected..."); } break; case 5: _commit_config(); break; case 6: _disp_network_details(); break; case 7: if(_dirty_config){ OFL("Your current config is unsaved."); String opt = _prompt("Save Before Quit? y/n"); if(CHAROPT(opt[0], 'y')) _commit_config(); } return; } delay(1); } }
void ESPSerialWiFiManager::_scan_for_networks(){ int i, opt, n; String opt_s; bool inv_opt = false; while(true){ if(!inv_opt){ OFL("Starting network scan...\n"); n = WiFi.scanNetworks(); } inv_opt = false; if(n == 0){ OFL("\nNo Avaliable Networks!\nChoose Option Below."); } else{ O(n); OFL(" networks found:"); OFL("=================="); for(i=0; i < n; i++){ O(i + 1);O(": ");O(WiFi.SSID(i)); O(" (");O(WiFi.RSSI(i));O(")"); OL((WiFi.encryptionType(i) == ENC_TYPE_NONE)?"":"*"); } OFL("=================="); } NL(); OFL("s: Scan Again"); OFL("q: Quit Network Scan"); opt_s = _prompt(""); if(CHAROPT(opt_s[0], 'q')){ return; } //exit scan else if(CHAROPT(opt_s[0], 's')){ continue; } else{ opt = opt_s.toInt(); if(opt < 1 || opt >= n + 2){ OFL("Invalid Menu Option!"); inv_opt = true; } else{ opt = opt - 1; switch (WiFi.encryptionType(opt)) { case ENC_TYPE_WEP: case ENC_TYPE_TKIP: case ENC_TYPE_CCMP: case ENC_TYPE_AUTO: if(_connect_enc(WiFi.SSID(opt))){ _disp_network_details(); return; } break; case ENC_TYPE_NONE: if(_connect_noenc(WiFi.SSID(opt))){ _disp_network_details(); return; } break; opt_s = _prompt("Scan Again? y/n"); if(CHAROPT(opt_s[0], 'y')){ continue; } else{ return; } } } } } }
char * tr_httpParse( const char * data, int len, tr_http_header_t *headers ) { const char * body, * begin; int ii, jj, full; /* find the end of the http headers */ body = slice( data, &len, CR LF CR LF ); if( NULL == body ) { body = slice( data, &len, LF LF ); if( NULL == body ) { body = slice( data, &len, CR CR ); if( NULL == body ) { return NULL; } } } /* return if no headers were requested */ if( NULL == headers || NULL == headers[0].name ) { return (char*) body; } /* NULL out all the header's data pointers */ for( ii = 0; NULL != headers[ii].name; ii++ ) { headers[ii].data = NULL; headers[ii].len = 0; } /* skip the http request or response line */ ii = 0; SKIP( ii, len, !NL( data[ii] ) ); SKIP( ii, len, NL( data[ii] ) ); /* find the requested headers */ while(ii < len ) { /* skip leading spaces and find the header name */ SKIP( ii, len, SP( data[ii] ) ); begin = data + ii; SKIP( ii, len, ':' != data[ii] && !NL( data[ii] ) ); if( ':' == data[ii] ) { full = 1; /* try to match the found header with one of the requested */ for( jj = 0; NULL != headers[jj].name; jj++ ) { if( NULL == headers[jj].data ) { full = 0; if( 0 == tr_strncasecmp( headers[jj].name, begin, ( data + ii ) - begin ) ) { ii++; /* skip leading whitespace and save the header value */ SKIP( ii, len, SP( data[ii] ) ); headers[jj].data = data + ii; SKIP( ii, len, !NL( data[ii] ) ); headers[jj].len = ( data + ii ) - headers[jj].data; break; } } } if( full ) { break; } /* skip to the end of the header */ SKIP( ii, len, !NL( data[ii] ) ); } /* skip past the newline */ SKIP( ii, len, NL( data[ii] ) ); } return (char*)body; }
int main(int argc, char *argv[]) { int i, g; /* generation and parent counters */ int parent1, parent2; int gop; fastf_t total_fitness = 0.0f; struct fitness_state fstate; struct population pop = {NULL, NULL, NULL, NULL, NULL, 0}; char dbname[256] = {0}; struct beset_options opts = {DEFAULT_POP_SIZE, DEFAULT_GENS, DEFAULT_RES, 0, 0, 0, 0}; struct individual *tmp = NULL; int ac; struct db_i *source_db; ac = parse_args(argc, argv, &opts); if (argc - ac != 3) usage(); /* read source model into fstate.rays */ fit_prep(&fstate, opts.res, opts.res); fit_store(argv[ac+2], argv[ac+1], &fstate); /* initialize population and spawn initial individuals */ pop_init(&pop, opts.pop_size); pop_spawn(&pop); source_db = db_open(argv[ac+1], DB_OPEN_READWRITE); db_dirbuild(source_db); pop.db_c = db_create("testdb", 5); db_close(pop.db_c); for (g = 1; g < opts.gens; g++ ) { #ifdef VERBOSE printf("\nGeneration %d:\n" "--------------\n", g); #endif total_fitness = 0.0f; snprintf(dbname, 256, "gen%.3d", g); pop.db_c = db_create(dbname, 5); pop_gop(REPRODUCE, argv[ac+2], NULL, argv[ac+2], NULL, source_db, pop.db_c, &rt_uniresource); /* calculate sum of all fitnesses and find * the most fit individual in the population * note: need to calculate outside of main pop * loop because it's needed for pop_wrand_ind()*/ for (i = 0; i < pop.size; i++) { fit_diff(NL(pop.parent[i].id), pop.db_p, &fstate); pop.parent[i].fitness = fstate.fitness; total_fitness += FITNESS; } /* sort population - used for keeping top N and dropping bottom M */ bu_sort((void *)pop.parent, pop.size, sizeof(struct individual), cmp_ind, NULL); /* remove lower M of individuals */ for (i = 0; i < opts.kill_lower; i++) { total_fitness -= pop.parent[i].fitness; } printf("Most fit from %s was %s with a fitness of %g\n", dbname, NL(pop.parent[pop.size-1].id), pop.parent[pop.size-1].fitness); printf("%6.8g\t%6.8g\t%6.8g\n", total_fitness/pop.size, pop.parent[0].fitness, pop.parent[pop.size-1].fitness); for (i = 0; i < pop.size; i++) { pop.child[i].id = i; /* keep upper N */ if (i >= pop.size - opts.keep_upper) { pop_gop(REPRODUCE, NL(pop.parent[i].id), NULL, NL(pop.child[i].id), NULL, pop.db_p, pop.db_c, &rt_uniresource); continue; } /* Choose a random genetic operation and * a parent which the op will be performed on*/ gop = pop_wrand_gop(); parent1 = pop_wrand_ind(pop.parent, pop.size, total_fitness, opts.kill_lower); /* only need 1 more individual, can't crossover, so reproduce */ if (gop == CROSSOVER && i >= pop.size-opts.keep_upper-1)gop=REPRODUCE; if (gop & (REPRODUCE | MUTATE)) { #ifdef VERBOSE printf("r(%s)\t ---------------> (%s)\n", NL(pop.parent[parent1].id), NL(pop.child[i].id)); #endif /* perform the genetic operation and output the child to the child database */ pop_gop(gop, NL(pop.parent[parent1].id), NULL, NL(pop.child[i].id), NULL, pop.db_p, pop.db_c, &rt_uniresource); } else { /* If we're performing crossover, we need a second parent */ parent2 = pop_wrand_ind(pop.parent, pop.size, total_fitness, opts.kill_lower); ++i; pop.child[i].id = i; #ifdef VERBOSE printf("x(%s, %s) --> (%s, %s)\n", NL(pop.parent[parent1].id), NL(pop.parent[parent2].id), pop.child[i-1].id, pop.child[i].id); #endif /* perform the genetic operation and output the children to the child database */ pop_gop(gop, NL(pop.parent[parent1].id), NL(pop.parent[parent2].id), NL(pop.child[i-1].id), NL(pop.child[i].id), pop.db_p, pop.db_c, &rt_uniresource); } } /* Close parent db and move children * to parent database and population * Note: pop size is constant so we * can keep the storage from the previous * pop.parent for the next pop.child*/ db_close(pop.db_p); pop.db_p = pop.db_c; tmp = pop.child; pop.child = pop.parent; pop.parent = tmp; } db_close(pop.db_p); #ifdef VERBOSE printf("\nFINAL POPULATION\n" "----------------\n"); for (i = 0; i < pop.size; i++) printf("%s\tf:%.5g\n", NL(pop.child[i].id), pop.child[i].fitness); #endif pop_clean(&pop); fit_clean(&fstate); return 0; }
/* Return > 0 Total packet length.in bytes * = 0 Length unknown, need more data. * < 0 Error, invalid format. */ int packet_get_length(enum PacketParseType htype, const char* ptr, unsigned n, /* Bytes read so far */ unsigned max_plen, /* Max packet length, 0=no limit */ unsigned trunc_len, /* Truncate (lines) if longer, 0=no limit */ int* statep) /* Protocol specific state */ { unsigned hlen, plen; switch (htype) { case TCP_PB_RAW: if (n == 0) goto more; else { DEBUGF((" => nothing remain packet=%d\r\n", n)); return n; } case TCP_PB_1: /* TCP_PB_1: [L0 | Data] */ hlen = 1; if (n < hlen) goto more; plen = get_int8(ptr); goto remain; case TCP_PB_2: /* TCP_PB_2: [L1,L0 | Data] */ hlen = 2; if (n < hlen) goto more; plen = get_int16(ptr); goto remain; case TCP_PB_4: /* TCP_PB_4: [L3,L2,L1,L0 | Data] */ hlen = 4; if (n < hlen) goto more; plen = get_int32(ptr); goto remain; case TCP_PB_RM: /* TCP_PB_RM: [L3,L2,L1,L0 | Data] ** where MSB (bit) is used to signal end of record */ hlen = 4; if (n < hlen) goto more; plen = get_int32(ptr) & 0x7fffffff; goto remain; case TCP_PB_LINE_LF: { /* TCP_PB_LINE_LF: [Data ... \n] */ const char* ptr2; if ((ptr2 = memchr(ptr, '\n', n)) == NULL) { if (n > max_plen && max_plen != 0) { /* packet full */ DEBUGF((" => packet full (no NL)=%d\r\n", n)); goto error; } else if (n >= trunc_len && trunc_len!=0) { /* buffer full */ DEBUGF((" => line buffer full (no NL)=%d\r\n", n)); return trunc_len; } goto more; } else { int len = (ptr2 - ptr) + 1; /* including newline */ if (len > max_plen && max_plen!=0) { DEBUGF((" => packet_size %d exceeded\r\n", max_plen)); goto error; } if (len > trunc_len && trunc_len!=0) { DEBUGF((" => truncated line=%d\r\n", trunc_len)); return trunc_len; } DEBUGF((" => nothing remain packet=%d\r\n", len)); return len; } } case TCP_PB_ASN1: { /* TCP_PB_ASN1: handles long (4 bytes) or short length format */ const char* tptr = ptr; int length; int nn = n; if (n < 2) goto more; nn--; if ((*tptr++ & 0x1f) == 0x1f) { /* Long tag format */ while (nn && ((*tptr & 0x80) == 0x80)) { tptr++; nn--; } if (nn < 2) goto more; tptr++; nn--; } /* tptr now point to length field and nn characters remain */ length = *tptr & 0x7f; if ((*tptr & 0x80) == 0x80) { /* Long length format */ tptr++; nn--; if (nn < length) goto more; switch (length) { case 0: plen = 0; break; case 1: plen = get_int8(tptr); tptr += 1; break; case 2: plen = get_int16(tptr); tptr += 2; break; case 3: plen = get_int24(tptr); tptr += 3; break; case 4: plen = get_int32(tptr); tptr += 4; break; default: goto error; /* error */ } } else { tptr++; plen = length; } hlen = (tptr-ptr); goto remain; } case TCP_PB_CDR: { const struct cdr_head* hp; hlen = sizeof(struct cdr_head); if (n < hlen) goto more; hp = (struct cdr_head*) ptr; if (sys_memcmp(hp->magic, CDR_MAGIC, 4) != 0) goto error; if (hp->flags & 0x01) /* Byte ordering flag */ plen = get_little_int32(hp->message_size); else plen = get_int32(hp->message_size); goto remain; } case TCP_PB_FCGI: { const struct fcgi_head* hp; hlen = sizeof(struct fcgi_head); if (n < hlen) goto more; hp = (struct fcgi_head*) ptr; if (hp->version != FCGI_VERSION_1) goto error; plen = ((hp->contentLengthB1 << 8) | hp->contentLengthB0) + hp->paddingLength; goto remain; } case TCP_PB_HTTPH: case TCP_PB_HTTPH_BIN: *statep = !0; case TCP_PB_HTTP: case TCP_PB_HTTP_BIN: /* TCP_PB_HTTP: data \r\n(SP data\r\n)* */ plen = n; if (((plen == 1) && NL(ptr)) || ((plen == 2) && CRNL(ptr))) goto done; else { const char* ptr1 = ptr; int len = plen; if (!max_plen) { /* This is for backward compatibility with old user of decode_packet * that might use option 'line_length' to limit accepted length of * http lines. */ max_plen = trunc_len; } while (1) { const char* ptr2 = memchr(ptr1, '\n', len); if (ptr2 == NULL) { if (max_plen != 0) { if (n >= max_plen) /* packet full */ goto error; } goto more; } else { plen = (ptr2 - ptr) + 1; if (*statep == 0) { if (max_plen != 0 && plen > max_plen) goto error; goto done; } if (plen < n) { if (SP(ptr2+1) && plen>2) { /* header field value continue on next line */ ptr1 = ptr2+1; len = n - plen; } else { if (max_plen != 0 && plen > max_plen) goto error; goto done; } } else { if (max_plen != 0 && plen > max_plen) goto error; goto more; } } } } case TCP_PB_TPKT: { const struct tpkt_head* hp; hlen = sizeof(struct tpkt_head); if (n < hlen) goto more; hp = (struct tpkt_head*) ptr; if (hp->vrsn == TPKT_VRSN) { plen = get_int16(hp->packet_length) - hlen; } else { goto error; } goto remain; } case TCP_PB_SSL_TLS: hlen = 5; if (n < hlen) goto more; if ((ptr[0] & 0x80) && ptr[2] == 1) { /* Ssl-v2 Client hello <<1:1, Len:15, 1:8, Version:16>> */ plen = (get_int16(&ptr[0]) & 0x7fff) - 3; } else { /* <<ContentType:8, Version:16, Length:16>> */ plen = get_int16(&ptr[3]); } goto remain; default: DEBUGF((" => case error\r\n")); return -1; } more: return 0; remain: { int tlen = hlen + plen; if ((max_plen != 0 && plen > max_plen) || tlen < (int)hlen) { /* wrap-around protection */ return -1; } return tlen; } done: return plen; error: return -1; }
if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user, sizeof cyg_privsep_user) != 0) #endif strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user)); } return cyg_privsep_user; } #define NL(x) x, (sizeof (x) - 1) #define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0])) static struct wenv { const char *name; size_t namelen; } wenv_arr[] = { { NL("ALLUSERSPROFILE=") }, { NL("COMPUTERNAME=") }, { NL("COMSPEC=") }, { NL("CYGWIN=") }, { NL("OS=") }, { NL("PATH=") }, { NL("PATHEXT=") }, { NL("PROGRAMFILES=") }, { NL("SYSTEMDRIVE=") }, { NL("SYSTEMROOT=") }, { NL("WINDIR=") } }; char ** fetch_windows_environment(void) {
#include "usb_ftdi_protocol.h" void reserved() { LOG(STR("reserved\n")); usb_stall_endpoint0(); } void get_descriptor() { uint8_t desc = device_request.value >> 8; uint8_t index = device_request.value; LOG(STR("get descriptor: des="), HEX8(desc), STR(" idx="), HEX8(index), NL()); if (desc == USB_DEVICE_DESCRIPTOR_TYPE) { LOG(STR("device descriptor\n")); //code_transmit((unsigned char code *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR)); } else if (desc == USB_CONFIGURATION_DESCRIPTOR_TYPE) { LOG(STR("config descriptor\n")); //code_transmit((unsigned char code *)&ConfigDescr, sizeof(CFG01)); } else if(desc == USB_STRING_DESCRIPTOR_TYPE) { LOG(STR("string descriptor\n")); /* i=0; switch(index)
static int checklength( tr_http_t * http ) { char * buf; int num, ii, len, lastnum; switch( http->lengthtype ) { case HTTP_LENGTH_UNKNOWN: if( learnlength( http ) ) { return checklength( http ); } break; case HTTP_LENGTH_EOF: break; case HTTP_LENGTH_FIXED: if( http->header.used >= http->chunkoff + http->chunklen ) { http->header.used = http->chunkoff + http->chunklen; return 1; } break; case HTTP_LENGTH_CHUNKED: buf = http->header.buf; lastnum = -1; while( http->header.used > http->chunkoff + http->chunklen ) { num = http->chunkoff + http->chunklen; if( lastnum == num ) { /* ugh, some trackers send Transfer-encoding: chunked and then don't encode the body */ http->lengthtype = HTTP_LENGTH_EOF; return checklength( http ); } lastnum = num; while( http->header.used > num && NL( buf[num] ) ) { num++; } ii = num; while( http->header.used > ii && !NL( buf[ii] ) ) { ii++; } if( http->header.used > ii ) { /* strtol should stop at the newline */ len = strtol( buf + num, NULL, 16 ); if( 0 == len ) { /* XXX should handle invalid length differently than 0 length chunk */ http->header.used = http->chunkoff + http->chunklen; return 1; } if( http->header.used > ii + 1 ) { ii += ( 0 == memcmp( buf + ii, CR LF, 2 ) ? 2 : 1 ); if( http->header.used > ii ) { memmove( buf + http->chunkoff + http->chunklen, buf + ii, http->header.used - ii ); } http->header.used -= ii - ( http->chunkoff + http->chunklen ); http->chunkoff += http->chunklen; http->chunklen = len; } } } break; } return 0; }
int tr_httpRequestType( const char * data, int len, char ** method, char ** uri ) { const char * words[6]; int ii, ret; const char * end; /* find the end of the line */ for( end = data; data + len > end; end++ ) { if( NL( *data) ) { break; } } /* find the first three "words" in the line */ for( ii = 0; ALEN( words ) > ii && data < end; ii++ ) { /* find the next space or non-space */ while( data < end && ( ii % 2 ? !SP( *data ) : SP( *data ) ) ) { data++; } /* save the beginning of the word */ words[ii] = data; } /* check for missing words */ if( ALEN( words) > ii ) { return -1; } /* parse HTTP version */ ret = -1; if( 8 <= words[5] - words[4] ) { if( 0 == tr_strncasecmp( words[4], "HTTP/1.1", 8 ) ) { ret = 11; } else if( 0 == tr_strncasecmp( words[4], "HTTP/1.0", 8 ) ) { ret = 10; } } /* copy the method */ if( 0 <= ret && NULL != method ) { *method = tr_dupstr( words[0], words[1] - words[0] ); if( NULL == *method ) { ret = -1; } } /* copy uri */ if( 0 <= ret && NULL != uri ) { *uri = tr_dupstr( words[2], words[3] - words[2] ); if( NULL == *uri ) { free( *method ); ret = -1; } } return ret; }
void put(const char* s) { obuf_puts(&outbuf, s); NL(); }
if (!(kerneldll = LoadLibrary("KERNEL32.DLL"))) return; if (!(RegisterServiceProcess = (DWORD (*)(DWORD, DWORD)) GetProcAddress(kerneldll, "RegisterServiceProcess"))) return; RegisterServiceProcess(0, 1); } #define NL(x) x, (sizeof (x) - 1) #define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0])) static struct wenv { const char *name; size_t namelen; } wenv_arr[] = { { NL("ALLUSERSPROFILE=") }, { NL("COMMONPROGRAMFILES=") }, { NL("COMPUTERNAME=") }, { NL("COMSPEC=") }, { NL("CYGWIN=") }, { NL("NUMBER_OF_PROCESSORS=") }, { NL("OS=") }, { NL("PATH=") }, { NL("PATHEXT=") }, { NL("PROCESSOR_ARCHITECTURE=") }, { NL("PROCESSOR_IDENTIFIER=") }, { NL("PROCESSOR_LEVEL=") }, { NL("PROCESSOR_REVISION=") }, { NL("PROGRAMFILES=") }, { NL("SYSTEMDRIVE=") }, { NL("SYSTEMROOT=") },