/** ** Show selected units with circle. ** ** @param color Color to draw and fill circle. ** @param x1,y1 Coordinates of the top left corner. ** @param x2,y2 Coordinates of the bottom right corner. */ void DrawSelectionCircleWithTrans(Uint32 color, int x1, int y1, int x2, int y2) { Video.FillTransCircleClip(color, (x1 + x2) / 2, (y1 + y2) / 2, s_min((x2 - x1) / 2, (y2 - y1) / 2), 95); Video.DrawCircleClip(color, (x1 + x2) / 2, (y1 + y2) / 2, s_min((x2 - x1) / 2, (y2 - y1) / 2)); }
void sort_template(xptr_sequence *xs, int off, int len) { Comparator comp; // Insertion sort on smallest arrays if (len < 7) { for (int i=off; i<len+off; i++) for (int j=i; j>off && comp.on_less(xs, j,j-1)<0; j--) xs->swap(j, j-1); return; } // Choose a partition element, v int m = off + (len >> 1); // Small arrays, middle element if (len > 7) { int l = off; int n = off + len - 1; if (len > 40) { // Big arrays, pseudomedian of 9 int s = len/8; l = comp.med3(xs, l, l+s, l+2*s); m = comp.med3(xs, m-s, m, m+s); n = comp.med3(xs, n-2*s, n-s, n); } m = comp.med3(xs, l, m, n); // Mid-size, med of 3 } xptr v = xs->get(m); // Establish Invariant: v* (<v)* (>v)* v* int a = off, b = a, c = off + len - 1, d = c; while(true) { while (b <= c && comp.on_less_lt(xs, b, v)<=0) { if (xs->get(b) == v) xs->swap(a++, b); b++; } while (c >= b && comp.on_less_rt(xs,v,c)<=0) { if (xs->get(c) == v) xs->swap(c, d--); c--; } if (b > c) break; xs->swap(b++, c--); } // Swap partition elements back to middle int s, n = off + len; s = s_min(a-off, b-a ); xs->vecswap( off, b-s, s); s = s_min(d-c, n-d-1); xs->vecswap( b, n-s, s); // Recursively sort non-partition-elements if ((s = b-a) > 1) sort_template<Comparator>(xs, off, s); if ((s = d-c) > 1) sort_template<Comparator>(xs, n-s, s); }
NOEXPORT char *ntlm3(char *username, char *password, char *phase2) { MD4_CTX md4; char *decoded; /* decoded reply from proxy */ char phase3[146]; unsigned char md4_hash[21]; unsigned int userlen=strlen(username); unsigned int phase3len=s_min(88+userlen, sizeof phase3); /* setup phase3 structure */ memset(phase3, 0, sizeof phase3); strcpy(phase3, "NTLMSSP"); phase3[8]=3; /* type: 3 */ phase3[16]=phase3len; /* LM-resp off */ phase3[20]=24; /* NT-resp len */ phase3[22]=24; /* NT-Resp len */ phase3[24]=64; /* NT-resp off */ phase3[32]=phase3len; /* domain offset */ phase3[36]=userlen; /* user length */ phase3[38]=userlen; /* user length */ phase3[40]=88; /* user offset */ phase3[48]=phase3len; /* host offset */ phase3[56]=phase3len; /* message len */ phase3[60]=2; /* flag: negotiate OEM */ phase3[61]=2; /* flag: negotiate NTLM */ /* calculate MD4 of UTF-16 encoded password */ MD4_Init(&md4); while(*password) { MD4_Update(&md4, password++, 1); MD4_Update(&md4, "", 1); /* UTF-16 */ } MD4_Final(md4_hash, &md4); memset(md4_hash+16, 0, 5); /* pad to 21 bytes */ /* decode challenge and calculate response */ decoded=base64(0, phase2, strlen(phase2)); /* decode */ if(!decoded) return NULL; crypt_DES((unsigned char *)phase3+64, (unsigned char *)decoded+24, md4_hash); crypt_DES((unsigned char *)phase3+72, (unsigned char *)decoded+24, md4_hash+7); crypt_DES((unsigned char *)phase3+80, (unsigned char *)decoded+24, md4_hash+14); str_free(decoded); strncpy(phase3+88, username, sizeof phase3-88); return base64(1, phase3, phase3len); /* encode */ }
int power_eigensolution(const MT& Ap, MT& Q, MT& L, unsigned int max_sol) { L = MT(0); typedef typename MT::VectorType VT; MT A = Ap; unsigned int n = s_min(MT::get_v_dim(), max_sol); for(unsigned int i=0;i<n;++i) { // Seed the eigenvector estimate VT q(1); q.normalize(); double l=123,l_old; // As long as we haven't reached the max iterations and the // eigenvalue has not converged, do unsigned int k=0; do { const VT z = A * q; double z_len = length(z); if(z_len < EV_THRESH) return i; l_old = l; l = dot(q, z)>0 ? z_len : -z_len; q = z/z_len; if(++k==KMAX) return i; } while((fabs(l-l_old) > fabs(EV_THRESH * l)) || k<2); // Update the solution by adding the eigenvector to Q and // the eigenvalue to the diagonal of L. Q[i] = q; L[i][i] = l; // Update A by subtracting the subspace represented by the // eigensolution just found. This is called the method of // deflation. MT B; outer_product(q,q,B); A = A - l * B; } return n; }
/** ** Show selected units with circle. ** ** @param color Color to draw circle ** @param x1,y1 Coordinates of the top left corner. ** @param x2,y2 Coordinates of the bottom right corner. */ void DrawSelectionCircle(Uint32 color, int x1, int y1, int x2, int y2) { Video.DrawCircleClip(color, (x1 + x2) / 2, (y1 + y2) / 2, s_min((x2 - x1) / 2, (y2 - y1) / 2) + 2); }
NOEXPORT void ntlm(CLI *c) { char *line, buf[BUFSIZ], *ntlm1_txt, *ntlm2_txt, *ntlm3_txt, *tmpstr; long content_length=0; /* no HTTP content */ /* send Proxy-Authorization (phase 1) */ fd_printf(c, c->remote_fd.fd, "Proxy-Connection: keep-alive"); ntlm1_txt=ntlm1(); if(!ntlm1_txt) { s_log(LOG_ERR, "Proxy-Authenticate: Failed to build NTLM request"); longjmp(c->err, 1); } fd_printf(c, c->remote_fd.fd, "Proxy-Authorization: NTLM %s", ntlm1_txt); str_free(ntlm1_txt); fd_putline(c, c->remote_fd.fd, ""); /* empty line */ line=fd_getline(c, c->remote_fd.fd); /* receive Proxy-Authenticate (phase 2) */ if(!is_prefix(line, "HTTP/1.0 407") && !is_prefix(line, "HTTP/1.1 407")) { s_log(LOG_ERR, "Proxy-Authenticate: NTLM authorization request rejected"); do { /* read all headers */ str_free(line); line=fd_getline(c, c->remote_fd.fd); } while(*line); str_free(line); longjmp(c->err, 1); } ntlm2_txt=NULL; do { /* read all headers */ str_free(line); line=fd_getline(c, c->remote_fd.fd); if(is_prefix(line, "Proxy-Authenticate: NTLM ")) ntlm2_txt=str_dup(line+25); else if(is_prefix(line, "Content-Length: ")) { content_length=strtol(line+16, &tmpstr, 10); if(tmpstr==line+16 || *tmpstr || content_length<0) { s_log(LOG_ERR, "Proxy-Authenticate: Invalid Content-Length"); str_free(line); longjmp(c->err, 1); } } } while(*line); if(!ntlm2_txt) { /* no Proxy-Authenticate: NTLM header */ s_log(LOG_ERR, "Proxy-Authenticate: NTLM header not found"); str_free(line); longjmp(c->err, 1); } /* read and ignore HTTP content (if any) */ while(content_length>0) { s_read(c, c->remote_fd.fd, buf, s_min(content_length, BUFSIZ)); content_length-=s_min(content_length, BUFSIZ); } /* send Proxy-Authorization (phase 3) */ fd_printf(c, c->remote_fd.fd, "CONNECT %s HTTP/1.1", c->opt->protocol_host); fd_printf(c, c->remote_fd.fd, "Host: %s", c->opt->protocol_host); ntlm3_txt=ntlm3(c->opt->protocol_username, c->opt->protocol_password, ntlm2_txt); str_free(ntlm2_txt); if(!ntlm3_txt) { s_log(LOG_ERR, "Proxy-Authenticate: Failed to build NTLM response"); longjmp(c->err, 1); } fd_printf(c, c->remote_fd.fd, "Proxy-Authorization: NTLM %s", ntlm3_txt); str_free(ntlm3_txt); }
static struct s_file_meta_data * icreate_fmd(struct s_core * core, struct s_file_meta_meta_data * mmd, struct s_core_mcreating * creating) { struct s_mserver * mserv = s_core_mserv(core); /* 1. find top N min-mem data-serv : By now N == 2 */ int ndserv = 0; #define MAX_D 2 unsigned int dserv[MAX_D] = {0}; unsigned int dmem[MAX_D] = {0}; int id = -1; struct s_server * serv = s_servg_next_active_serv(core->servg, S_SERV_TYPE_D, &id); while(serv) { unsigned int mem = s_servg_get_mem(serv); if(dmem[0] == 0) { ndserv++; dmem[0] = mem; dserv[0] = s_servg_get_id(serv); } else if (dmem[1] == 0 || mem < dmem[1]) { ndserv++; int i = 1; if(mem < dmem[0]) { dmem[1] = dmem[0]; dserv[1] = dserv[0]; i = 0; } dmem[i] = mem; dserv[i] = s_servg_get_id(serv); } serv = s_servg_next_active_serv(core->servg, S_SERV_TYPE_D, &id); } ndserv = s_min(ndserv, MAX_D); if(ndserv == 0) { s_log("[Error] create file:%s, no dserver!", s_string_data_p(mmd->fname)); } /* 2. create file-meta-data */ int nblocks = s_core_fsize_to_block_num(&creating->size); struct s_file_meta_data * fmd = s_file_meta_data_create( core, nblocks ); fmd->fname = s_string_grab(mmd->fname); fmd->fdesc = mmd->fdesc; fmd->fsize = creating->size; fmd->nblocks = nblocks; s_log("[LOG] create file-meta-data: size:%u-%u, nblocks:%d", fmd->fsize.high, fmd->fsize.low, fmd->nblocks); int i; for(i = 0; i < nblocks; ++i) { fmd->blocks[i].locate[0] = dserv[0]; fmd->blocks[i].locate[1] = dserv[1]; } /* 3. save file-meta-data in this mserv */ struct s_file_meta_data ** ppmd = s_hash_set_str(mserv->file_metadata, mmd->fname); *ppmd = fmd; /* 4. send to related mserv & dserv */ struct s_packet * pkt = s_core_pkt_from_fmd(core, fmd); s_packet_set_fun(pkt, S_PKT_TYPE_CREATE_METADATA); for(i = 0; i < mmd->nmserv; ++i) { struct s_server * serv = s_servg_get_active_serv(core->servg, S_SERV_TYPE_M, mmd->mservs[i]); if(serv && s_servg_get_id(serv) != core->id) { s_servg_rpc_call(serv, pkt, s_string_grab(creating->fname), &irpc_send_md_back, 3000); creating->ncheck++; } } for(i = 0; i < ndserv; ++i) { struct s_server * serv = s_servg_get_active_serv(core->servg, S_SERV_TYPE_D, dserv[i]); if(serv) { s_servg_rpc_call(serv, pkt, s_string_grab(creating->fname), &irpc_send_md_back, 3000); creating->ncheck++; } } if(creating->ncheck == 0) { s_log("[Warning] send file-meta-data error : none serv!"); } return fmd; }