static void dumpio(struct tcb *tcp) { if (syserror(tcp)) return; int fd = tcp->u_arg[0]; if (fd < 0) return; if (is_number_in_set(fd, read_set)) { switch (tcp->s_ent->sen) { case SEN_read: case SEN_pread: case SEN_recv: case SEN_recvfrom: case SEN_mq_timedreceive: dumpstr(tcp, tcp->u_arg[1], tcp->u_rval); return; case SEN_readv: case SEN_preadv: case SEN_preadv2: dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], tcp->u_rval); return; case SEN_recvmsg: dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval); return; case SEN_recvmmsg: dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]); return; } } if (is_number_in_set(fd, write_set)) { switch (tcp->s_ent->sen) { case SEN_write: case SEN_pwrite: case SEN_send: case SEN_sendto: case SEN_mq_timedsend: dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]); break; case SEN_writev: case SEN_pwritev: case SEN_pwritev2: case SEN_vmsplice: dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1); break; case SEN_sendmsg: dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1); break; case SEN_sendmmsg: dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]); break; } } }
static void dumpio(struct tcb *tcp) { int (*func)(); if (syserror(tcp)) return; if ((unsigned long) tcp->u_arg[0] >= num_quals) return; func = tcp->s_ent->sys_func; if (func == printargs) return; if (qual_flags[tcp->u_arg[0]] & QUAL_READ) { if (func == sys_read || func == sys_pread || func == sys_recv || func == sys_recvfrom) { dumpstr(tcp, tcp->u_arg[1], tcp->u_rval); return; } else if (func == sys_readv) { dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]); return; #if HAVE_SENDMSG } else if (func == sys_recvmsg) { dumpiov_in_msghdr(tcp, tcp->u_arg[1]); return; } else if (func == sys_recvmmsg) { dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]); return; #endif } } if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) { if (func == sys_write || func == sys_pwrite || func == sys_send || func == sys_sendto) dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]); else if (func == sys_writev) dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]); #if HAVE_SENDMSG else if (func == sys_sendmsg) dumpiov_in_msghdr(tcp, tcp->u_arg[1]); else if (func == sys_sendmmsg) dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]); #endif } }
static void do_send(int fd, char *msg, unsigned int msg_size, struct timespec *tmout, bool cropped) { long rc; long saved_errno; do { rc = syscall(__NR_mq_timedsend, fd, msg, msg_size, 42, tmout); saved_errno = errno; printf("mq_timedsend(%d, ", fd); printstr(MSG_START, msg_size > MSG_MAX_UNCUT ? MSG_MAX_UNCUT : msg_size); if (cropped) printf("..."); errno = saved_errno; printf(", %u, 42, {tv_sec=%lld, tv_nsec=%llu}) = %s\n", msg_size, (long long) tmout->tv_sec, zero_extend_signed_to_ull(tmout->tv_nsec), sprintrc(rc)); errno = saved_errno; if (rc == -1) { if (errno == EINTR) continue; perror_msg_and_skip("mq_timedsend"); } # if DUMPIO_WRITE dumpstr(MSG_START, msg_size); # endif } while (rc); }
static void dumpmatch(const char *str, int len, const char *rep, int nmat, const int *ovec) { int i; printf("%s Input\n", str); printf("nmat=%d", nmat); for (i = 0; i < nmat * 2; i++) printf(" %d", ovec[i]); printf("\n"); for (i = 0; i < nmat * 2; i += 2) dumpstr(str, len, ovec[i], ovec[i+1]); printf("\n"); }
static inline int sock_recv(int sock, uint16_t *src, char *buf, int len) { //#define RECV_FLAGS MSG_DONTWAIT #define RECV_FLAGS 0 #if 0 struct msghdr hdr; struct iovec iov; hdr.msg_name = src; hdr.msg_namelen = sizeof(*src); hdr.msg_iov = &iov; hdr.msg_iovlen = 1; hdr.msg_control = NULL; hdr.msg_controllen = 0; hdr.msg_flags = 0; iov.iov_base = buf; iov.iov_len = len; /* len = recvmsg(sock, &hdr, MSG_NOSIGNAL | MSG_DONTWAIT);*/ do { len = recvmsg(sock, &hdr, RECV_FLAGS); } while ((len < 0) && (errno == EAGAIN)); if (arg_verbose > 0) { printf("Recv %d bytes from %d\n",len , *src); if ((arg_verbose > 1) && (len > 0)) { printf(":%s:\n", dumpstr(buf, MIN(len ,32))); } } if (len < 0) { perror("recvmsg"); } return len; #else struct p4s_io_recv_s r; r.Flags = RECV_FLAGS; r.iov.iov_base = buf; r.iov.iov_len = len; do { len = ioctl(sock, P4S_IO_RECV, &r); } while ((len < 0) && (errno == EAGAIN)); *src = r.SrcNode; if (len < 0) { perror("recvmsg"); } return len; #endif }
static void run_recv() { int sock; int ret; char *buf; printf("Start server on port %d. My PSID: %d\n", arg_serverport, PSP_GetNodeID()); sock = socket(PF_P4S , 0, 0); if (!sock) goto err_socket; ret = bind_port(sock, arg_serverport); if (ret) goto err_bind; buf = (char*)malloc(SERVER_BUFLEN); if (!buf) goto err_malloc; while (1) { uint16_t src; int len; len = sock_recv(sock, &src, buf, SERVER_BUFLEN); if (arg_verbose > 0) { printf("Recv %d bytes from %d\n",len ,src); if ((arg_verbose > 1) && (len > 0)) { if (arg_verbose > 2) { dump(buf, 0, len, 0, 16, "recv"); } else { printf(":%s:\n", dumpstr(buf, MIN(len,32))); } } } } return; err_socket: perror("socket()"); exit(1); err_bind: perror("bind()"); exit(1); err_malloc: perror("malloc()"); exit(1); }
static int bind_port(int sock, int portno) { struct sockaddr_p4 sp4; int ret; memset(&sp4, 0, sizeof(sp4)); sp4.sp4_family = PF_P4S; snprintf(&sp4.sp4_port[0], sizeof(sp4.sp4_port), "pp_%04x", portno); if (arg_verbose) { printf("bind address: %s\n", dumpstr(&sp4.sp4_port, sizeof(sp4.sp4_port))); } ret = bind(sock, (struct sockaddr*)&sp4, sizeof(sp4)); return ret; }
static void do_recv(int fd, char *msg, unsigned int msg_size, struct timespec *tmout, bool cropped) { long rc; long saved_errno; unsigned prio; do { rc = syscall(__NR_mq_timedreceive, fd, msg, MSG_SIZE, &prio, tmout); saved_errno = errno; printf("mq_timedreceive(%d, ", fd); if (rc >= 0) { printstr(MSG_START, rc > MSG_MAX_UNCUT ? MSG_MAX_UNCUT : rc); if (cropped) printf("..."); } else { printf("%p", msg); } errno = saved_errno; printf(", %u, [42], {tv_sec=%lld, tv_nsec=%llu}) = %s\n", MSG_SIZE, (long long) tmout->tv_sec, zero_extend_signed_to_ull(tmout->tv_nsec), sprintrc(rc)); errno = saved_errno; if (rc == -1) { if (errno == EINTR) continue; perror_msg_and_skip("mq_timedreceive"); } if ((rc >= 0) && ((unsigned long) rc != msg_size)) error_msg_and_skip("mq_timedreceive size mismatch" ": expected %u, got %ld", msg_size, rc); # if DUMPIO_READ dumpstr(MSG_START, rc); # endif } while (rc < 0); }
static void dump_ci( p4_dumpci_t *ci ) { printf("%6u %7u %6u %7u ", ci->SSeqNo, ci->SWindow, ci->RSeqNo, ci->RWindow ); printf("%7d %7d %7d ", ci->list_usr_idx, ci->list_net_idx, ci->rem_net_idx); printf("%3d %3d %4d\n", ci->SFragQN, ci->RFragQN, ci->RefCnt); if (arg_verbose <= 1) return; dump_p4_addr( &ci->sap4.sp4_port ); switch ( ci->sap4.sp4_ra.type ){ case P4REMADDR_LOCAL: printf(" LOCAL\n"); break; case P4REMADDR_ETHER: printf(" ETH: %02x:%02x:%02x:%02x:%02x:%02x %s\n", ci->sap4.sp4_ra.tec.ether.addr.mac[0], ci->sap4.sp4_ra.tec.ether.addr.mac[1], ci->sap4.sp4_ra.tec.ether.addr.mac[2], ci->sap4.sp4_ra.tec.ether.addr.mac[3], ci->sap4.sp4_ra.tec.ether.addr.mac[4], ci->sap4.sp4_ra.tec.ether.addr.mac[5], ci->sap4.sp4_ra.tec.ether.devname); break; case P4REMADDR_MYRI: printf(" MYRI: ID %d\n",ci->sap4.sp4_ra.tec.myri.nodeid); break; case 4711: printf(" Wow\n"); break; default: printf(" TYP %d: %s\n",ci->sap4.sp4_ra.type, dumpstr( &ci->sap4.sp4_ra.tec, sizeof(ci->sap4.sp4_ra.tec)) ); } }
static void dump_p4_addr( p4_addr_t *addr ) { printf( "%s", dumpstr( addr, sizeof( *addr))); }
static void run_send() { int sock; int ret; int conid; char *buf; int i; printf("Start client from port %d to port %d\n", arg_clientport, arg_serverport); sock = socket( PF_P4S , 0, 0 ); if (!sock) goto err_socket; ret = bind_port(sock, arg_clientport); if (ret) goto err_bind; buf = (char*)malloc(arg_size); if (!buf) goto err_malloc; for (i = 0; i < arg_size; i++) { buf[i] = i; } for (i = 0; i < arg_size; i++) { buf[i] = (i & ~3) >> (8*(3-(i & 3))); } // memset(buf, 0x42, arg_size); conid = connect_port(sock, arg_servernode, arg_serverport); if (conid < 0) goto err_connect; if (arg_verbose > 0) { printf("Connect to %08x:%d with id %d\n", arg_servernode, arg_serverport, conid); printf("Send %d bytes\n",arg_size); if ((arg_verbose > 1) && (arg_size > 0)) { if (arg_verbose > 2) { dump(buf, 0, arg_size, 0, 16, "send"); } else { printf(":%s:\n", dumpstr(buf, MIN(arg_size,32))); } } } { uint16_t dest = conid; int len; int offset = 0; while (offset < arg_size) { len = sock_send(sock, dest, buf + offset, arg_size - offset); if (len < 0) { perror("send"); } else { if (arg_verbose > 1) { printf("Send bytes %d - %d.\n", offset, offset + len - 1); } offset += len; } } } // sleep(10); free(buf); return; err_socket: perror("socket()"); exit(1); err_bind: perror("bind()"); exit(1); err_malloc: perror("malloc()"); exit(1); err_connect: perror("connect()"); exit(1); }
int main(int argc, char* argv[]) { if (argc != 2) { printf("BGI Text Exprot by Fuyin\n"); printf("usage: %s <input_file>\n", argv[0]); return -1; } std::string in_filename = argv[1]; FILE* fin = fopen(in_filename.c_str(), "rb"); if (!fin) { printf("fopen failed!\n"); return -1; } fseek(fin, 0, SEEK_END); dword fsize = ftell(fin); fseek(fin, 0, SEEK_SET); byte *buffer = new byte[fsize]; fread(buffer, fsize, 1, fin); fclose(fin); byte *p = buffer; const int magic_size = 0x1C; if (strncmp((char*)p, "BurikoCompiledScriptVer1.00\x00", magic_size)) { printf("format not match!\n"); return -1; } p += magic_size; dword header_size = *(dword*)p; p += header_size; //忽略文件末尾的少于4个的字节 dword find_count = (fsize - magic_size - header_size) / 4; dword baseoffset = magic_size + header_size; //用于避免导出空文本 dword *dummy; dword first_offset = baseoffset + find_and_getoffset((dword*)p, find_count, 0x00000003, dummy); find_count = (first_offset - magic_size - header_size) / 4; std::string out_filename = in_filename + ".txt"; FILE* fout = fopen(out_filename.c_str(), "wb"); fwrite("\xFF\xFE", 2, 1, fout); dword *opt_block = (dword*)p; dword text_offset = 0; dword *next_opt; while (true) { text_offset = baseoffset + find_and_getoffset(opt_block, find_count, 0x00000003, next_opt); if (next_opt == NULL) break; if (text_offset == 0) break; opt_block = next_opt; if (text_offset < fsize && text_offset > first_offset) { char *str = dumpstr(buffer, text_offset); fixstr(str); printstr(fout, str); } } fclose(fout); delete[]buffer; return 0; }