static int64_t SPICE_GNUC_UNUSED consume_int64(uint8_t **ptr) { int64_t val; val = read_int64(*ptr); *ptr += 8; return val; }
int32_t execute_segment_stat_handle(struct data_buffer* dbuf,int socket_fd,MEM_POOL* mem_pool) { struct data_buffer rbuf; //发数据包 if(socket_write(socket_fd,dbuf->data,dbuf->data_len) <= 0) return -1; //接受命名 memset(&rbuf,0,sizeof(struct data_buffer)); rbuf.data_len = sizeof(uint32_t); rbuf.size = rbuf.data_len; rbuf.data = (uint8_t *)mem_pool_malloc(mem_pool,sizeof(uint32_t)); if(socket_read(socket_fd,rbuf.data,rbuf.data_len) <= 0) return -1; rbuf.data_len = read_int32(&rbuf); rbuf.data = (uint8_t *)mem_pool_malloc(mem_pool,rbuf.data_len); rbuf.rpos = 0; rbuf.size = rbuf.data_len; if(socket_read(socket_fd,rbuf.data,rbuf.data_len) <= 0) return -1; rbuf.rpos += MESSAGE_HEAD_LEN; uint16_t type = message_type(&rbuf); if (type != MT_DC_STAT_RS) return -1; uint16_t segment_count = read_int16(&rbuf); uint16_t i; fprintf(stderr,"%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n","SEGMENT_ID","CREATE_TIME","MODIFY_TIME","CHECKPOINT_TIME","FLAG","ROW_COUNT","DEL_COUNT"); for(i=0; i<segment_count; i++) { fprintf(stderr,"%-20u",read_int16(&rbuf)); fprintf(stderr,"%-20llu",read_int64(&rbuf)); fprintf(stderr,"%-20llu",read_int64(&rbuf)); fprintf(stderr,"%-20llu",read_int64(&rbuf)); fprintf(stderr,"%-20u",read_int8(&rbuf)); fprintf(stderr,"%-20u",read_int32(&rbuf)); fprintf(stderr,"%-20u\n",read_int32(&rbuf)); } return 0; }
void SSDBMultiSetWaitReply::mergeAndSend(ClientLogicSession* client) { if (mIsError) { SSDBProtocolRequest errorResponse; errorResponse.writev("error"); errorResponse.endl(); client->send(errorResponse.getResult(), errorResponse.getResultLen()); } else { if (mWaitResponses.size() == 1) { client->send(mWaitResponses.front().reply->c_str(), mWaitResponses.front().reply->size()); } else { string* errorReply = nullptr; int64_t num = 0; SSDBProtocolRequest megreResponse; for (auto& v : mWaitResponses) { int64_t tmp; if (read_int64(v.ssdbReply, &tmp).ok()) { num += tmp; } else { errorReply = v.reply; break; } } if (errorReply != nullptr) { client->send(errorReply->c_str(), errorReply->size()); } else { SSDBProtocolRequest megreResponse; megreResponse.writev("ok", num); megreResponse.endl(); client->send(megreResponse.getResult(), megreResponse.getResultLen()); } } } }
void SSDBMultiSetWaitReply::mergeAndSend(std::shared_ptr<ClientSession>& client) { if (mErrorCode != nullptr) { syncSSDBStrList(client, { SSDB_ERROR, *mErrorCode }); } else { if (mWaitResponses.size() == 1) { client->sendPacket(mWaitResponses.front().responseBinary); } else { std::shared_ptr<std::string> errorReply = nullptr; int64_t num = 0; for (auto& v : mWaitResponses) { int64_t tmp; if (read_int64(v.ssdbReply, &tmp).ok()) { num += tmp; } else { errorReply = v.responseBinary; break; } } if (errorReply != nullptr) { client->sendPacket(errorReply); } else { SSDBProtocolRequest& response = client->getCacheSSDBProtocol(); response.init(); response.writev(SSDB_OK, num); response.endl(); client->sendPacket(response.getResult(), response.getResultLen()); } } } }
TEST(DATA_BUFFER_TEST, READ_AND_WRITE){ MEM_POOL_PTR pMemPool = mem_pool_init(MB_SIZE); struct data_buffer* buffer = init_data_buffer(); buffer->data_len = 256; buffer->data = (uint8*) mem_pool_malloc(pMemPool, 256); uint8 tmp8; uint16 tmp16; uint32 tmp32; uint64 tmp64; char* str = (char*) mem_pool_malloc(pMemPool, 100); char* result = (char*) mem_pool_malloc(pMemPool, 100); strcpy(str, "hello world!"); write_int8(122, buffer); write_int16(1111, buffer); write_int32(324, buffer); write_int64(2321, buffer); write_bytes((uint8*)str, strlen(str)+1, buffer); tmp8 = read_int8(buffer); ASSERT_EQ(tmp8, 122); tmp16 = read_int16(buffer); ASSERT_EQ(tmp16, 1111); tmp32 = read_int32(buffer); ASSERT_EQ(tmp32, 324); tmp64 = read_int64(buffer); ASSERT_EQ(tmp64, 2321); read_bytes(buffer, (uint8*)result, strlen(str)+1); ASSERT_EQ(0, strcmp(str, result)); destroy_data_buffer(buffer); mem_pool_destroy(pMemPool); }
int read_pc_from_file(ap_handler* handler, FILE *fp) { int ret = MATCH_FAILED; perf_counter *pc; if(handler->len == PERF_COUNT_MAX) { handler->err = AP_ERR_PC_BUF_OVERFLOW; goto EXIT; } pc = &handler->buf[handler->len]; ret = read_int(fp, &pc->counter_typer); if(ret == MATCH_EOF) { goto EXIT; } if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_COUNTER_TYPE); goto EXIT; } ret = read_str(fp, pc->type_name, TYPE_NAME_MAX); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_TYPE_NAME); goto EXIT; } ret = read_str(fp, pc->property_name, PROPERTY_NAME_MAX); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_PROPERTY_NAME); goto EXIT; } ret = read_str(fp, pc->instance_name, INSTANCE_NAME_MAX); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_INSTANCE_NAME); goto EXIT; } ret = read_int(fp, &pc->is_empty); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_IS_EMPTY_FLAG); goto EXIT; } if(!pc->is_empty) { switch(pc->counter_typer) { case PERF_COUNTER_TYPE_INT: ret = read_int(fp, &pc->val_int); break; case PERF_COUNTER_TYPE_LARGE: ret = read_int64(fp, &pc->val_large); break; case PERF_COUNTER_TYPE_DOUBLE: ret = read_double(fp, &pc->val_double); break; case PERF_COUNTER_TYPE_STRING: ret = read_str(fp, pc->val_str, STRING_VALUE_MAX); break; } if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_VALUE); goto EXIT; } } else { ret = read_sperator(fp, NON_STRICT_MATCH); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_VALUE); goto EXIT; } } ret = read_str(fp, pc->unit_name, UNIT_NAME_MAX); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_UNIT_NAME); goto EXIT; } ret = read_int(fp, &pc->refresh_interval); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_REFRESH_INTERVAL); goto EXIT; } ret = read_int64(fp, &pc->timestamp); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_TIMESTAMP); goto EXIT; } ret = read_str(fp, pc->machine_name, MACHINE_NAME_MAX); if(ret != MATCH_SUCCESS) { set_handler_err(handler, AP_ERR_INVALID_MACHINE_NAME); goto EXIT; } handler->len++; //Discard line end if exits. fscanf(fp, "\n"); EXIT: return ret; }
static int reader_int64(lua_State* L) { reader_t* reader = lua_touserdata(L, 1); lua_pushinteger(L, (lua_Integer)read_int64(reader)); return 1; }
static int restore_data(const char* srcPath) { int res = -1; FILE* fh = fopen(srcPath, "r"); if (fh == NULL) { fprintf(stderr, "Unable to open source '%s': %s\n", srcPath, strerror(errno)); return -1; } inputFileVersion = read_int32(fh, 0); if (inputFileVersion < FILE_VERSION_1 || inputFileVersion > FILE_VERSION) { fprintf(stderr, "Restore file has bad version: 0x%x\n", inputFileVersion); goto done; } if (inputFileVersion >= FILE_VERSION_2) { opt_backupAll = read_int32(fh, 0); } else { opt_backupAll = 0; } printf("Wiping contents of /data...\n"); if (!wipe("/data")) { goto done; } printf("Restoring from %s to /data...\n", srcPath); while (1) { int type; char* path = NULL; if (read_header(fh, &type, &path, &statBuffer) == 0) { goto done; } if (type == 0) { break; } const char* typeName = "?"; if (type == TYPE_DIR) { typeName = "dir"; printf("Restoring dir %s...\n", path); if (mkdir(path, statBuffer.st_mode) != 0) { if (errno != EEXIST) { fprintf(stderr, "unable to create directory '%s': %s\n", path, strerror(errno)); free(path); goto done; } } } else if (type == TYPE_FILE) { typeName = "file"; off_t size = read_int64(fh, -1); if (size < 0) { fprintf(stderr, "bad file size %ld in restore file\n", size); free(path); goto done; } printf("Restoring file %s...\n", path); FILE* dest = fopen(path, "w"); if (dest == NULL) { fprintf(stderr, "unable to open destination file '%s': %s\n", path, strerror(errno)); free(path); goto done; } int copyres = copy_file(dest, fh, size, path, NULL); fclose(dest); if (!copyres) { free(path); goto done; } } else { fprintf(stderr, "unknown node type %d\n", type); goto done; } // Do this even for directories, since the dir may have already existed // so we need to make sure it gets the correct mode. if (chmod(path, statBuffer.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO)) != 0) { fprintf(stderr, "unable to chmod destination %s '%s' to 0x%x: %s\n", typeName, path, statBuffer.st_mode, strerror(errno)); free(path); goto done; } if (chown(path, statBuffer.st_uid, statBuffer.st_gid) != 0) { fprintf(stderr, "unable to chown destination %s '%s' to uid %d / gid %d: %s\n", typeName, path, (int)statBuffer.st_uid, (int)statBuffer.st_gid, strerror(errno)); free(path); goto done; } struct utimbuf timbuf; timbuf.actime = statBuffer.st_atime; timbuf.modtime = statBuffer.st_mtime; if (utime(path, &timbuf) != 0) { fprintf(stderr, "unable to utime destination %s '%s': %s\n", typeName, path, strerror(errno)); free(path); goto done; } free(path); } res = 0; done: fclose(fh); return res; }
static int read_header(FILE* fh, int* type, char** path, struct stat* st) { *type = read_int32(fh, -1); if (*type == TYPE_END) { return 1; } if (*type < 0) { fprintf(stderr, "bad token %d in restore file\n", *type); return 0; } int32_t pathLen = read_int32(fh, -1); if (pathLen <= 0) { fprintf(stderr, "bad path length %d in restore file\n", pathLen); return 0; } char* readPath = (char*)malloc(pathLen+1); if (fread(readPath, 1, pathLen, fh) != (size_t)pathLen) { fprintf(stderr, "truncated path in restore file\n"); free(readPath); return 0; } readPath[pathLen] = 0; *path = readPath; st->st_uid = read_int32(fh, -1); if (st->st_uid == (uid_t)-1) { fprintf(stderr, "bad uid in restore file at '%s'\n", readPath); return 0; } st->st_gid = read_int32(fh, -1); if (st->st_gid == (gid_t)-1) { fprintf(stderr, "bad gid in restore file at '%s'\n", readPath); return 0; } st->st_mode = read_int32(fh, -1); if (st->st_mode == (mode_t)-1) { fprintf(stderr, "bad mode in restore file at '%s'\n", readPath); return 0; } int64_t ltime = read_int64(fh, -1); if (ltime < 0) { fprintf(stderr, "bad atime in restore file at '%s'\n", readPath); return 0; } st->st_atime = (time_t)(ltime/1000/1000/1000); ltime = read_int64(fh, -1); if (ltime < 0) { fprintf(stderr, "bad mtime in restore file at '%s'\n", readPath); return 0; } st->st_mtime = (time_t)(ltime/1000/1000/1000); ltime = read_int64(fh, -1); if (ltime < 0) { fprintf(stderr, "bad ctime in restore file at '%s'\n", readPath); return 0; } st->st_ctime = (time_t)(ltime/1000/1000/1000); st->st_mode &= (S_IRWXU|S_IRWXG|S_IRWXO); return 1; }
int bson_decoder_next(bson_decoder_t decoder) { int8_t tbyte; int32_t tint; unsigned nlen; CODER_CHECK(decoder); /* if the previous node was EOO, pop a nesting level */ if (decoder->node.type == BSON_EOO) { if (decoder->nesting > 0) { decoder->nesting--; } /* if the nesting level is now zero, the top-level document is done */ if (decoder->nesting == 0) { /* like kill but not an error */ debug("nesting is zero, document is done"); decoder->fd = -1; /* return end-of-file to the caller */ return 0; } } /* if there are unread bytes pending in the stream, discard them */ while (decoder->pending > 0) { if (read_int8(decoder, &tbyte)) { CODER_KILL(decoder, "read error discarding pending bytes"); } decoder->pending--; } /* get the type byte */ if (read_int8(decoder, &tbyte)) { CODER_KILL(decoder, "read error on type byte"); } decoder->node.type = tbyte; decoder->pending = 0; debug("got type byte 0x%02x", decoder->node.type); /* EOO is special; it has no name/data following */ if (decoder->node.type == BSON_EOO) { decoder->node.name[0] = '\0'; } else { /* get the node name */ nlen = 0; for (;;) { if (nlen >= BSON_MAXNAME) { CODER_KILL(decoder, "node name overflow"); } if (read_int8(decoder, (int8_t *)&decoder->node.name[nlen])) { CODER_KILL(decoder, "read error on node name"); } if (decoder->node.name[nlen] == '\0') { break; } nlen++; } debug("got name '%s'", decoder->node.name); switch (decoder->node.type) { case BSON_BOOL: if (read_int8(decoder, &tbyte)) { CODER_KILL(decoder, "read error on BSON_BOOL"); } decoder->node.b = (tbyte != 0); break; case BSON_INT32: if (read_int32(decoder, &tint)) { CODER_KILL(decoder, "read error on BSON_INT"); } decoder->node.i = tint; break; case BSON_INT64: if (read_int64(decoder, &decoder->node.i)) { CODER_KILL(decoder, "read error on BSON_INT"); } break; case BSON_DOUBLE: if (read_double(decoder, &decoder->node.d)) { CODER_KILL(decoder, "read error on BSON_DOUBLE"); } break; case BSON_STRING: if (read_int32(decoder, &decoder->pending)) { CODER_KILL(decoder, "read error on BSON_STRING length"); } break; case BSON_BINDATA: if (read_int32(decoder, &decoder->pending)) { CODER_KILL(decoder, "read error on BSON_BINDATA size"); } if (read_int8(decoder, &tbyte)) { CODER_KILL(decoder, "read error on BSON_BINDATA subtype"); } decoder->node.subtype = tbyte; break; /* XXX currently not supporting other types */ default: CODER_KILL(decoder, "unsupported node type"); } } /* call the callback and pass its results back */ return decoder->callback(decoder, decoder->priv, &decoder->node); }
double read_num2(unsigned char *buf) { int words; unsigned char *limit; double i, result, denom; switch (buf[0]) { case S2_FIXNUM32: return (double)read_int32(buf, 1); case S2_FIXNUM64: return (double)read_int64(buf, 1); case S2_SYMBOL_ID: return (double)read_int(buf, 1); case S2_CHAR: return (double)read_uint32(buf, 1); case S2_SHORT_FLOAT: return (double)read_float(buf, 1); case S2_SINGLE_FLOAT: return (double)read_float(buf, 1); case S2_DOUBLE_FLOAT: return read_double(buf, 1); case S2_NEGATIVE_BIGNUM: result = 0; buf += 5; words = read_uint32(buf, -4) / 4; for(i=0 ; i < words; i++, buf = buf+4) { result -= exp2(i*32) * read_uint32(buf, 0); } return result; case S2_POSITIVE_BIGNUM: result = 0; buf += 5; words = read_uint32(buf, -4) / 4; for(i=0 ; i < words; i++, buf = buf+4) { result += exp2(i*32) * read_uint32(buf, 0); } return result; case S2_RATIONAL: case S2_COMPLEX: default: switch ((++buf)[0]) { case S2_FIXNUM32: case S2_FIXNUM64: result = (double)read_int(++buf, 0); buf += 4; break; case S2_NEGATIVE_BIGNUM: result = 0; buf += 5; limit = buf + read_uint(buf, -4); for(i=0 ; buf < limit; i++, buf = buf+4) { result -= exp2(i*32) - read_uint(buf, 0); } break; case S2_POSITIVE_BIGNUM: default: result = 0; buf += 5; limit = buf + read_uint(buf, -4); for(i=0 ; buf < limit; i++, buf = buf+4) { result += exp2(i*32) * read_uint(buf, 0); } break; } switch (buf[0]) { case S2_FIXNUM32: case S2_FIXNUM64: return result / read_int(++buf, 0); case S2_NEGATIVE_BIGNUM: denom = 0; buf += 5; limit = buf + read_uint(buf, -4); for(i=0 ; buf < limit; i++, buf = buf+4) { denom -= exp2(i*32) * read_uint(buf, 0); } return result / denom; case S2_POSITIVE_BIGNUM: default: denom = 0; buf += 5; limit = buf + read_uint(buf, -4); for(i=0 ; buf < limit; i++, buf = buf+4) { denom += exp2(i*32) * read_uint(buf, 0); } return result / denom; } } }