bool Connection::read_frame(Frame &frame) { if (!fill_input(2)) { return false; } char h1 = _input.next(); char h2 = _input.next(); frame.type = type_from_opcode(h1 & 0x0f); frame.last = ((h1 & 0x80) != 0); frame.payload.clear(); size_t len = (h2 & 0x7f); if (len > 125) { size_t len_bytes = (len == 127) ? 8 : 2; if (!fill_input(len_bytes)) { return false; } len = 0; for (size_t i = 0; i < len_bytes; ++i) { len = (len << 8) + (_input.next() & 0xff); } } char mask[4]; bool use_mask = ((h2 & 0x80) != 0); if (use_mask) { if (!fill_input(4)) { return false; } for (size_t i = 0; i < 4; ++i) { mask[i] = _input.next(); } } if (!fill_input(len)) { return false; } const char *src = _input.obtain(); char *dst = frame.payload.reserve(len); if (use_mask) { for (size_t i = 0; i < len; ++i) { dst[i] = (src[i] ^ mask[i & 0x3]); } } else { memcpy(dst, src, len); } frame.payload.commit(len); _input.evict(len); return true; }
int main(int argc, char *argv[]) { if (argc < 4) { fprintf(stderr, "Usage %s infile outfile threads\n", argv[0]); exit(-1); } char *src = (char *) malloc(ALIGNED_BLOCKSIZE); int fd_in = open(argv[1], O_RDONLY); int fd_out = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); unsigned long comp_array_size = atol(argv[3]); pthread_t lz4comp_thread[comp_array_size]; lz4_compression_work *comp_array; unsigned long comp_head; unsigned long comp_tail; comp_array = new lz4_compression_work[comp_array_size]; for (unsigned long i = 0; i < comp_array_size; i++) { comp_array[i].fd_out = fd_out; comp_array[i].startup(i == 0); if(pthread_create(&lz4comp_thread[i], NULL, lz4_comp_service_start, &comp_array[i]) != 0) { printf("failed to launch exec thread\n"); exit(-1); } } comp_head = comp_tail = 0; unsigned long inbytes; unsigned long total_bytes = 0; while (true) { inbytes = fill_input(fd_in, src); if (!inbytes) break; total_bytes += inbytes; unsigned char *buffer = (unsigned char *) src; while (inbytes > 0) { if (comp_array[comp_head].uncomp_bytes == ALIGNED_BLOCKSIZE) { comp_array[comp_head].busy = true; comp_head = (comp_head + 1) % comp_array_size; if (comp_head == comp_tail) { while (comp_array[comp_tail].busy); write_to_file(comp_array[comp_tail].fd_out, comp_array[comp_tail].comp_data, comp_array[comp_tail].outbytes); comp_tail = (comp_tail + 1) % comp_array_size; } } unsigned long tocopy = (inbytes > (ALIGNED_BLOCKSIZE - comp_array[comp_head].uncomp_bytes)) ? (ALIGNED_BLOCKSIZE - comp_array[comp_head].uncomp_bytes) : inbytes; memcpy(comp_array[comp_head].uncomp_data + comp_array[comp_head].uncomp_offset, buffer, tocopy); buffer += tocopy; inbytes -= tocopy; comp_array[comp_head].uncomp_offset += tocopy; comp_array[comp_head].uncomp_bytes += tocopy; } } while (comp_tail != comp_head) { while (comp_array[comp_tail].busy); write_to_file(comp_array[comp_tail].fd_out, comp_array[comp_tail].comp_data, comp_array[comp_tail].outbytes); comp_array[comp_tail].shutdown(false); comp_array[comp_tail].terminate = true; comp_tail = (comp_tail + 1) % comp_array_size; } if (comp_array[comp_head].uncomp_bytes > 0) { comp_array[comp_head].compress(); write_to_file(comp_array[comp_head].fd_out, comp_array[comp_head].comp_data, comp_array[comp_head].outbytes); } comp_array[comp_head].shutdown(true); comp_array[comp_head].terminate = true; close(fd_in); close(fd_out); for(int i=0;i<comp_array_size;i++) { pthread_join(lz4comp_thread[i], NULL); } delete[] comp_array; printf("Compressed bytes %lu\n", total_bytes); return 0; }
inline static void do_action(struct mouse_action *act, MSLLHOOKSTRUCT *msll) { switch(act->code) { case MOUSE_ACT_BUTTON_D: { INPUT in; memset(&in, 0, sizeof(in)); in.type = INPUT_MOUSE; in.mi.time = msll->time; in.mi.dwExtraInfo = msll->dwExtraInfo; fill_input(&in, MOTION_DOWN, act->conf.button); SendInput(1, &in, sizeof(INPUT)); break; } case MOUSE_ACT_BUTTON_U: { INPUT in; memset(&in, 0, sizeof(in)); in.type = INPUT_MOUSE; in.mi.time = msll->time; in.mi.dwExtraInfo = msll->dwExtraInfo; fill_input(&in, MOTION_UP, act->conf.button); SendInput(1, &in, sizeof(INPUT)); break; } case MOUSE_ACT_WHEEL: break; case MOUSE_ACT_WHEELPOST: PostMessage(ctx.main_window, WM_MOUSEHOOK_WHEELPOST, (WPARAM)HIWORD(msll->mouseData), MAKELPARAM(msll->pt.x, msll->pt.y)); case MOUSE_ACT_MOVE: break; case MOUSE_ACT_MODECH: ctx.mode_data.cur_conf = act->conf.mode_change.mode; PostMessage(ctx.main_window, WM_MOUSEHOOK_MODECH, MAKEWPARAM(msll->pt.x, msll->pt.y), (LPARAM)&act->conf.mode_change.data); break; case MOUSE_ACT_MODEMSG: PostMessage(ctx.main_window, WM_MOUSEHOOK_MODEMSG, MAKEWPARAM(msll->pt.x, msll->pt.y), (LPARAM)&act->conf.mode_msg.data); break; case MOUSE_ACT_NOTHING: /* do nothing */ break; } }