Exemple #1
0
int grpc_msg_decompress(grpc_compression_algorithm algorithm,
                        grpc_slice_buffer* input, grpc_slice_buffer* output) {
  switch (algorithm) {
    case GRPC_COMPRESS_NONE:
      return copy(input, output);
    case GRPC_COMPRESS_DEFLATE:
      return zlib_decompress(input, output, 0);
    case GRPC_COMPRESS_GZIP:
      return zlib_decompress(input, output, 1);
    case GRPC_COMPRESS_ALGORITHMS_COUNT:
      break;
  }
  gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm);
  return 0;
}
static bool
decompression_algorithm_gzip_splice(struct decompression_algorithm *alg,
				    int fd_in, int fd_out, size_t cnt_in)
{
	struct zlib_decompression_algorithm	*zlib = tozlib(alg);
	struct z_stream_s			*stream = &zlib->stream;

	while (cnt_in > 0) {
		size_t		len = MIN(cnt_in, zlib->buf_in.iov_len);
		ssize_t		l = read(fd_in, zlib->buf_in.iov_base, len);

		if (l == 0) {
			fprintf(stderr, "EOS while reading data\n");
			break;
		} else if (l < 0 && errno == EINTR) {
			continue;
		} else if (l < 0) {
			perror("read(<zlib-stream>)");
			break;
		}

		stream->next_in   = zlib->buf_in.iov_base;
		stream->avail_in  = l;

		stream->next_out  = zlib->buf_out.iov_base;
		stream->avail_out = zlib->buf_out.iov_len;

		if (!zlib_decompress(fd_out, stream))
			break;

		cnt_in -= l;
	}

	return cnt_in == 0;
}
Exemple #3
0
int do_uncompress(void *dst, int dstlen, void *src, int srclen, int type)
{
	switch (type)
	{
	case JFFS2_COMPR_NONE:
		memcpy(dst, src, dstlen);
		return dstlen;
		break;
	case JFFS2_COMPR_ZERO:
		memset(dst, 0, dstlen);
		return dstlen;
		break;
	case JFFS2_COMPR_RTIME:
		rtime_decompress((unsigned char*)src, (unsigned char*)dst, srclen, dstlen);
		return dstlen;
	case JFFS2_COMPR_RUBINMIPS:
		break;
	case JFFS2_COMPR_COPY:
		break;
	case JFFS2_COMPR_DYNRUBIN:
		dynrubin_decompress((unsigned char*)src, (unsigned char*)dst, srclen, dstlen);
		return dstlen;
	case JFFS2_COMPR_ZLIB:
		return zlib_decompress((unsigned char*)src, (unsigned char*)dst, srclen, dstlen);
	}
	printf("  ** unknown compression type %d!\n", type);
	return -1;
}
Exemple #4
0
int zlib_test(int argc, char **argv)
{
  int zlib_test(int argc, char **argv)
    int ret;

  /* avoid end-of-line conversions */
  SET_BINARY_MODE(stdin);
  SET_BINARY_MODE(stdout);

  /* do compression if no arguments */
  if (argc == 1) {
    ret = zlib_compress(stdin, stdout, Z_DEFAULT_COMPRESSION);
    if (ret != Z_OK)
      zerr(ret);
    return ret;
  }

  /* do decompression if -d specified */
  else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
    ret = zlib_decompress(stdin, stdout);
    if (ret != Z_OK)
      zerr(ret);
    return ret;
  }

  /* otherwise, report usage */
  else {
    fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
    return 1;
  }
}
Exemple #5
0
CGOGN_IO_API std::vector<unsigned char> read_binary_xml_data(const char* data_str, bool is_compressed, DataType header_type)
{
	if (!is_compressed)
	{
		std::vector<unsigned char> decode = base64_decode(data_str, 0);
		decode.erase(decode.begin(), decode.begin() + (header_type == DataType::UINT32 ? 4u : 8u));
		return decode;
	}
	else
		return zlib_decompress(data_str, header_type);
}
Exemple #6
0
int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in, 
		     unsigned char *data_out, __u32 cdatalen, __u32 datalen)
{
/* xde */
  int i ;
  
	switch (comprtype) {
	case JFFS2_COMPR_NONE:
		/* This should be special-cased elsewhere, but we might as well deal with it */
		memcpy(data_out, cdata_in, datalen);
		break;

	case JFFS2_COMPR_ZERO:
/* xde		memset(data_out, 0, datalen);*/
    for (i = 0 ; i < datalen ; i ++)
      data_out[i] = 0 ;
		break;

	case JFFS2_COMPR_ZLIB:
		zlib_decompress(cdata_in, data_out, cdatalen, datalen);
		break;

	case JFFS2_COMPR_RTIME:
		rtime_decompress(cdata_in, data_out, cdatalen, datalen);
		break;

	case JFFS2_COMPR_RUBINMIPS:
#if 0 /* Disabled 23/9/1 */
		rubinmips_decompress(cdata_in, data_out, cdatalen, datalen);
#else
		printk(KERN_WARNING "JFFS2: Rubinmips compression encountered but support not compiled in!\n");
#endif
		break;
	case JFFS2_COMPR_DYNRUBIN:
#if 1 /* Phase this one out */
		dynrubin_decompress(cdata_in, data_out, cdatalen, datalen);
#else
		printk(KERN_WARNING "JFFS2: Dynrubin compression encountered but support not compiled in!\n");
#endif
		break;

	default:
		printk(KERN_NOTICE "Unknown JFFS2 compression type 0x%02x\n", comprtype);
		return -EIO;
	}
	return 0;
}
Exemple #7
0
int main(int argc, char **argv)
{
	FILE **infiles;
	FILE *outfile;
	
	if (argc == 1 || argc == 2) {
		usage();
		return(EXIT_FAILURE);
	}
	
	if (!strcmp(argv[1], "-d")) { // Decompress
		infiles = calloc(1, sizeof(FILE*));
		if (argc != 4) {
			usage();
			return(EXIT_FAILURE);
		} 
		infiles[0] = fopen(argv[2], "rb");
		if (infiles[0] == NULL) {
			fprintf(stderr, "Error. Cannot open input file %s", argv[2]);
			return(EXIT_FAILURE);
		}
		outfile = fopen(argv[3], "wb");
		if (outfile == NULL) {
			fprintf(stderr, "Error. Cannot open output file %s", argv[3]);
			return(EXIT_FAILURE);
		}
		return zlib_decompress(infiles[0], outfile);

	} else { // Compress

		infiles = calloc(argc-2, sizeof(FILE*));
		for (uint16_t i = 0; i < argc-2; i++) { 
			infiles[i] = fopen(argv[i+1], "rb");
			if (infiles[i] == NULL) {
				fprintf(stderr, "Error. Cannot open input file %s", argv[i+1]);
				return(EXIT_FAILURE);
			}
		}
		outfile = fopen(argv[argc-1], "wb");
		if (outfile == NULL) {
			fprintf(stderr, "Error. Cannot open output file %s", argv[argc-1]);
			return(EXIT_FAILURE);
		}
		return zlib_compress(infiles, argc-2, outfile);
	}
}
Exemple #8
0
int main(int argc, char **argv)
{
	struct deflate_decompressor *d;
	int ret;
	int fd = open(argv[1], O_RDONLY);
	struct stat stbuf;
	assert(fd >= 0);
	ret = fstat(fd, &stbuf);
	assert(!ret);

	char in[stbuf.st_size];
	ret = read(fd, in, sizeof in);
	assert(ret == sizeof in);

	char out[sizeof(in) * 3];

	d = deflate_alloc_decompressor();

	zlib_decompress(d, in, sizeof in, out, sizeof out, NULL);
	deflate_free_decompressor(d);
	return 0;
}
Exemple #9
0
int main(int argc, char **argv)
{
	FILE **infiles;
	char **infile_names;
	FILE *outfile;
	
	if (argc == 1 || argc == 2) {
		usage();
		return(EXIT_FAILURE);
	}
	
	if (!strcmp(argv[1], "-d")) { // Decompress

		infiles = calloc(1, sizeof(FILE*));
		if (argc != 4) {
			usage();
			return(EXIT_FAILURE);
		} 
		infiles[0] = fopen(argv[2], "rb");
		if (infiles[0] == NULL) {
			fprintf(stderr, "Error. Cannot open input file %s\n\n", argv[2]);
			return(EXIT_FAILURE);
		}
		outfile = fopen(argv[3], "wb");
		if (outfile == NULL) {
			fprintf(stderr, "Error. Cannot open output file %s\n\n", argv[3]);
			return(EXIT_FAILURE);
		}
		return zlib_decompress(infiles[0], outfile);

	} else { // Compress or gemerate version info

		bool hardnested_mode = false;
		bool generate_version_file = false;
		int num_input_files = 0;
		if (!strcmp(argv[1], "-t")) { 			// compress one hardnested table
			if (argc != 4) {
				usage();
				return(EXIT_FAILURE);
			}
			hardnested_mode = true;
			num_input_files = 1;
		} else if (!strcmp(argv[1], "-v")) { 	// generate version info
			generate_version_file = true;
			num_input_files = argc-3;
		} else { 								// compress 1..n fpga files
			num_input_files = argc-2;
		}
			
		infiles = calloc(num_input_files, sizeof(FILE*));
		infile_names = calloc(num_input_files, sizeof(char*));
		for (uint16_t i = 0; i < num_input_files; i++) {
			infile_names[i] = argv[i+((hardnested_mode || generate_version_file)?2:1)];
			infiles[i] = fopen(infile_names[i], "rb");
			if (infiles[i] == NULL) {
				fprintf(stderr, "Error. Cannot open input file %s\n\n", infile_names[i]);
				return(EXIT_FAILURE);
			}
		}
		outfile = fopen(argv[argc-1], "wb");
		if (outfile == NULL) {
			fprintf(stderr, "Error. Cannot open output file %s\n\n", argv[argc-1]);
			return(EXIT_FAILURE);
		}
		if (generate_version_file) {
			if (generate_fpga_version_info(infiles, infile_names, num_input_files, outfile)) {
				return(EXIT_FAILURE);
			}
		} else {
			return zlib_compress(infiles, num_input_files, outfile, hardnested_mode);
		}
	}
}
Exemple #10
0
int32 recv_parse(int8* buff, size_t* buffsize, sockaddr_in* from, map_session_data_t* map_session_data)
{
    size_t size = *buffsize;
    int32 checksumResult = -1;

#ifdef WIN32
    try
    {
        checksumResult = checksum((uint8*)(buff + FFXI_HEADER_SIZE), size - (FFXI_HEADER_SIZE + 16), buff + size - 16);
    }
    catch (...)
    {
        ShowError(CL_RED"Possible crash attempt from: %s\n" CL_RESET, ip2str(map_session_data->client_addr, nullptr));
        return -1;
    }
#else
    checksumResult = checksum((uint8*)(buff + FFXI_HEADER_SIZE), size - (FFXI_HEADER_SIZE + 16), buff + size - 16);
#endif

    if (checksumResult == 0)
    {
        if (map_session_data->PChar == nullptr)
        {
            uint32 CharID = RBUFL(buff, FFXI_HEADER_SIZE + 0x0C);

            const int8* fmtQuery = "SELECT charid FROM chars WHERE charid = %u LIMIT 1;";

            int32 ret = Sql_Query(SqlHandle, fmtQuery, CharID);

            if (ret == SQL_ERROR ||
                Sql_NumRows(SqlHandle) == 0 ||
                Sql_NextRow(SqlHandle) != SQL_SUCCESS)
            {
                ShowError(CL_RED"recv_parse: Cannot load charid %u" CL_RESET, CharID);
                return -1;
            }

            fmtQuery = "SELECT session_key FROM accounts_sessions WHERE charid = %u LIMIT 1;";

            ret = Sql_Query(SqlHandle, fmtQuery, CharID);

            if (ret == SQL_ERROR ||
                Sql_NumRows(SqlHandle) == 0 ||
                Sql_NextRow(SqlHandle) != SQL_SUCCESS)
            {
                ShowError(CL_RED"recv_parse: Cannot load session_key for charid %u" CL_RESET, CharID);
            }
            else
            {
                int8* strSessionKey = nullptr;
                Sql_GetData(SqlHandle, 0, &strSessionKey, nullptr);

                memcpy(map_session_data->blowfish.key, strSessionKey, 20);
            }

            // наверное создание персонажа лучше вынести в метод charutils::LoadChar() и загрузку инвентаря туда же сунуть
            CCharEntity* PChar = new CCharEntity();
            PChar->id = CharID;
            PChar->PBattleAI = new CAICharNormal(PChar);

            charutils::LoadChar(PChar);

            PChar->status = STATUS_DISAPPEAR;

            map_session_data->PChar = PChar;
        }
        map_session_data->client_packet_id = 0;
        map_session_data->server_packet_id = 0;
        return 0;
    }
    else
    {
        //char packets

        if (map_decipher_packet(buff, *buffsize, from, map_session_data) == -1)
        {
            *buffsize = 0;
            return -1;
        }
        // reading data size
        uint32 PacketDataSize = RBUFL(buff, *buffsize - sizeof(int32) - 16);
        // creating buffer for decompress data
        int8* PacketDataBuff = nullptr;
        CREATE(PacketDataBuff, int8, map_config.buffer_size);
        // it's decompressing data and getting new size
        PacketDataSize = zlib_decompress(buff + FFXI_HEADER_SIZE,
            PacketDataSize,
            PacketDataBuff,
            map_config.buffer_size,
            zlib_decompress_table);

        // it's making result buff
        // don't need memcpy header
        memcpy(buff + FFXI_HEADER_SIZE, PacketDataBuff, PacketDataSize);
        *buffsize = FFXI_HEADER_SIZE + PacketDataSize;

        aFree(PacketDataBuff);
        return 0;
    }
    return -1;
}
Exemple #11
0
bool load_ps3_mamba_payload()
{
    //DrawDialogOK("Label1"); 
    if(sys8_mamba() == 0x666) return true;  // MAMBA is already running
    
    if(!syscall_base)
    {
        DrawDialogOK("syscall_base is empty!");
        return false;
    }

    char payload_file[MAXPATHLEN];
    sprintf(payload_file, "%s/USRDIR/mamba/mamba_%X.lz.bin", self_path, firmware);

#ifdef LASTPLAY_LOADER
    //DrawDialogOK("Label2"); 
    if(file_exists(payload_file) == false)
        sprintf(payload_file, "/dev_hdd0/game/IRISMAN00/USRDIR/mamba/mamba_%X.lz.bin", firmware);
#endif
    //DrawDialogOK("Label3"); 
    if(file_exists(payload_file) == false) return false;

    write_htab();

    u64 *addr = (u64 *) memalign(128, 0x20000);
    //DrawDialogOK("Label4"); 
    if(!addr)
    {
        DrawDialogOK("Memory is full");
        exit(0);
    }

    memset((char *) addr, 0, 0x20000);
    int out_size;

    int file_size = 0;
    char *mamba_payload = LoadFile((char *) payload_file, &file_size);

    if(file_size < 20000)
    {
        if(mamba_payload) free(mamba_payload);

        free(addr);
        return false;
    }

    zlib_decompress((char *) mamba_payload, (char *) addr, file_size, &out_size);

    if(mamba_payload) free(mamba_payload);

    out_size = (out_size + 0x4000) & ~127;
    u64 lv2_mem = sys8_alloc(out_size, 0x27ULL); // alloc LV2 memory

    if(!lv2_mem)
    {
        free(addr);
        DrawDialogOK("LV2 memory is full!");
        exit(0);
    }

    for(int n = 0; n < 100; n++)
    {
        lv2poke(lv2_mem, lv2_mem + 0x8ULL);
        sys8_memcpy(lv2_mem + 8, (u64) addr, out_size);


        lv2poke(syscall_base + (u64) (40 * 8), lv2_mem);  // syscall management
        lv2poke(0x80000000000004E8ULL, 0);

        usleep(1000);
    }


    free(addr);
    return true;
}
Exemple #12
0
lzo_bool x_decompress(file_t *fip, file_t *fop,
                      const header_t *h, lzo_bool skip)
{
    lzo_bool ok = 0;

    if (!x_enter(h))
        e_memory();

    if (skip)
    {
        assert(opt_cmd != CMD_TEST);
        assert(fop->fd < 0);
        if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 0)
            fprintf(con_term,"skipping %s [%s]", fip->name, fop->name);
        fflush(con_term);
        set_err_nl(1);
    }
    else if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 1)
    {
        if (opt_unlink)
            fprintf(con_term,"restoring %s into %s", fip->name, fop->name);
        else
            fprintf(con_term,"decompressing %s into %s", fip->name, fop->name);
        fflush(con_term);
        set_err_nl(1);
    }
    else if (opt_cmd == CMD_TEST && opt_verbose > 0)
    {
        /* note: gzip is quiet by default when testing, lzop is not */
        if (opt_verbose > 2)
            fprintf(con_term,"testing %s [%s]", fip->name, fop->name);
        else
            fprintf(con_term,"testing %s", fip->name);
        fflush(con_term);
        set_err_nl(1);
    }

    fip->bytes_processed = fop->bytes_processed = 0;

    switch (h->method)
    {
#if defined(WITH_LZO)
    case M_LZO1X_1:
    case M_LZO1X_1_15:
    case M_LZO1X_999:
        ok = lzo_decompress(fip,fop,h,skip);
        break;
#endif
#if defined(WITH_NRV)
    case M_NRV1A:
    case M_NRV1B:
    case M_NRV2A:
    case M_NRV2B:
        ok = nrv_decompress(fip,fop,h,skip);
        break;
#endif
#if defined(WITH_ZLIB)
    case M_ZLIB:
        ok = zlib_decompress(fip,fop,h,skip);
        break;
#endif
    default:
        fatal(fip,"Internal error");
        ok = 0;
        break;
    }

    if (skip && opt_cmd == CMD_DECOMPRESS && opt_verbose > 0)
    {
        fprintf(con_term, ok ? "\n" : " FAILED\n");
        fflush(con_term);
    }
    else if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 1)
    {
        fprintf(con_term, ok ? "\n" : " FAILED\n");
        fflush(con_term);
    }
    else if (opt_cmd == CMD_TEST && opt_verbose > 0)
    {
        fprintf(con_term, ok ? " OK\n" : " FAILED\n");
        fflush(con_term);
    }
    set_err_nl(0);

    if (ok && opt_cmd == CMD_TEST)
        do_test(h,fop->bytes_processed,fip->bytes_processed);
    if (ok && opt_cmd == CMD_LIST)
        do_list(h,fop->bytes_processed,fip->bytes_processed);
    if (ok && opt_cmd == CMD_LS)
        do_ls(h,fop->bytes_processed,fip->bytes_processed);
    if (ok && opt_cmd == CMD_INFO)
        do_info(h,fop->bytes_processed,fip->bytes_processed);

    x_leave(h);
    return ok;
}
void load_ps3_mamba_payload()
{

    u64 *addr= (u64 *) memalign(128, 0x20000);

    if(!addr) {
        DrawDialogOK("Shit! full memory");
        exit(0);
    }

    if(!syscall_base) {
        DrawDialogOK("syscall_base is empty!");
        free(addr);
        return;
    }
    
    //PAYLOAD_BASE = 0x80000000007E4000ULL;

    if(sys8_mamba()==0x666) goto skip_the_load;  // MAMBA is running yet

    write_htab();

    memset((char *) addr, 0, 0x20000);
    int out_size;

    /*
    if(firmware == 0x446C)
        memcpy((char *) addr, (char *) mamba_4_46_bin, mamba_4_46_bin_size);
    else if(firmware == 0x453C)
        memcpy((char *) addr, (char *) mamba_4_53_bin, mamba_4_53_bin_size);
    else {
        DrawDialogOK("MAMBA is not supported for this CFW");
        free(addr);
        return;
    }
    */

    if(firmware == 0x446C)
        zlib_decompress((char *) mamba_4_46_lz_bin, (char *) addr, mamba_4_46_lz_bin_size, &out_size);
    else if(firmware == 0x453C)
        zlib_decompress((char *) mamba_4_53_lz_bin, (char *) addr, mamba_4_53_lz_bin_size, &out_size);
    else {
        DrawDialogOK("MAMBA is not supported for this CFW");
        free(addr);
        return;
    }

    out_size = (out_size + 0x4000) & ~127;
    u64 lv2_mem = sys8_alloc(out_size, 0x27ULL); // alloc LV2 memory

    if(!lv2_mem) {
        DrawDialogOK("Shit! LV2 full memory");
        free(addr);
        exit(0);
    }

    int n;

    for(n=0;n<2000;n++) {

        lv2poke(lv2_mem, lv2_mem + 0x8ULL);
        sys8_memcpy(lv2_mem + 8, (u64) addr, out_size);
        

        lv2poke(syscall_base + (u64) (40 * 8), lv2_mem);  // syscall management
        lv2poke(0x80000000000004E8ULL, 0);

        usleep(1000);
    }

   // sleep(1);

skip_the_load:
    free(addr);

}
Exemple #14
0
static rc_t decode_encoded(struct decoded *y, const struct encoded *x) {
    unsigned i;
    unsigned type = x->flags & 0x3;
    rc_t rc;
    const uint8_t *src;
    uint8_t *hsrc;
    unsigned elem_bits;
    unsigned n = 0;
    
    memset(y, 0, sizeof(*y));
    y->data_count = x->data_count;
    y->size_type = (x->flags >> 2) & 3;
    
    y->diff = alloc_raw_nbuf(y->data_count);
    if (y->diff == NULL)
        return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted);
    
    if (type) {
        if ((type & 1) != 0)  {
            rc = zlib_decompress(y->diff->data.u8, 8 * y->data_count, &n, x->u.zipped.data, x->u.zipped.data_size );
            if (rc)
                return rc;
            if ((type & 2) != 0)
                y->diff->min = x->u.packed.min;
        }
        else {
            y->diff->min = x->u.packed.min;
            memcpy(y->diff->data.u8, x->u.packed.data, n = x->u.packed.data_size);
        }
        elem_bits = n * 8 / x->data_count;
        if (elem_bits * x->data_count / 8 != n)
            return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid);
        BITS_TO_VARIANT(y->diff, elem_bits);
        if (type == 1 && 4 - y->diff->var != y->size_type) {
#if _DEBUGGING
            fprintf(stderr, "decode_encoded: var = %i, size_type = %u\n", (int)y->diff->var, (unsigned)y->size_type);
#endif
            return RC(rcXF, rcFunction, rcExecuting, rcRange, rcExcessive);
        }
        y->diff->used = x->data_count;
        return 0;
    }
    
    y->type = malloc(x->u.izipped.segments);
    if (y->type == NULL)
        return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted);
    
    if (x->u.izipped.outliers) {
        hsrc = NULL;
        
        if (FLAG_TYPE(x->u.izipped) == DATA_ZIPPED) {
            hsrc = malloc(x->u.izipped.segments);
            if (hsrc == NULL)
                return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted);
            rc = zlib_decompress(hsrc, x->u.izipped.segments, &i, x->u.izipped.type, x->u.izipped.type_size);
            if (rc) {
                free(hsrc);
                return rc;
            }
            src = hsrc;
        }
        else
            src = x->u.izipped.type;
        decode_types(y->type, x->u.izipped.segments, src);
        if (hsrc) free(hsrc);
        for (n = i = 0; i != x->u.izipped.segments; ++i) {
            if (y->type[i])
                ++n;
        }
        y->lines = x->u.izipped.segments - n;
        y->outliers = n;
    }
    else {
        memset(y->type, 0, x->u.izipped.segments);
        y->lines = x->u.izipped.segments;
        y->outliers = 0;
    }
    
    y->diff->min = x->u.izipped.min_diff;
    if (FLAG_DIFF(x->u.izipped) == DATA_CONSTANT) {
        y->diff->used = x->u.izipped.diff_size;
        memset(y->diff->data.raw, 0, nbuf_size(y->diff));
    }
    else {
        if (FLAG_DIFF(x->u.izipped) == DATA_ZIPPED) {
            rc = zlib_decompress(y->diff->data.u8, y->diff->size * 8, &n, x->u.izipped.diff, x->u.izipped.diff_size);
            if (rc)
                return rc;
        }
        else {
            n = x->u.izipped.diff_size;
            memcpy(y->diff->data.u8, x->u.izipped.diff, n);
        }
        elem_bits = (n * 8) / y->diff->size;
        if (elem_bits * y->diff->size / 8 != n)
            return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid);
        BITS_TO_VARIANT(y->diff, elem_bits);
        y->diff->used = n >> (4 - y->diff->var);
    }
    
    y->length = alloc_nbuf(y->lines + y->outliers, 2);
    if (y->length == NULL)
        return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted);
    
    y->length->min = x->u.izipped.min_length;
    if (FLAG_LENGTH(x->u.izipped) == DATA_CONSTANT) {
        y->length->used = y->lines + y->outliers;
        memset(y->length->data.raw, 0, nbuf_size(y->length));
    }
    else {
        if (FLAG_LENGTH(x->u.izipped) == DATA_ZIPPED) {
            rc = zlib_decompress(y->length->data.u8, y->length->size * 4, &n, x->u.izipped.length, x->u.izipped.length_size);
            if (rc)
                return rc;
        }
        else {
            n = x->u.izipped.length_size;
            memcpy(y->length->data.u8, x->u.izipped.length, n);
        }
        elem_bits = (n * 8) / (y->lines + y->outliers);
        if (elem_bits * (y->lines + y->outliers) / 8 != n)
            return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid);
        BITS_TO_VARIANT(y->length, elem_bits);
        y->length->used = n >> (4 - y->length->var);
    }
    
    y->dy = alloc_nbuf(y->lines, 1);
    if (y->dy == NULL)
        return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted);
    
    y->dy->min = x->u.izipped.min_dy;
    if (FLAG_DY(x->u.izipped) == DATA_CONSTANT) {
        y->dy->used = y->lines;
        memset(y->dy->data.raw, 0, nbuf_size(y->dy));
    }
    else {
        if (FLAG_DY(x->u.izipped) == DATA_ZIPPED) {
            rc = zlib_decompress(y->dy->data.u8, y->dy->size * 8, &n, x->u.izipped.dy, x->u.izipped.dy_size);
            if (rc)
                return rc;
        }
        else {
            n = x->u.izipped.dy_size;
            memcpy(y->dy->data.u8, x->u.izipped.dy, n);
        }
        elem_bits = (n * 8) / y->lines;
        if (elem_bits * y->lines / 8 != n)
            return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid);
        BITS_TO_VARIANT(y->dy, elem_bits);
        y->dy->used = n >> (4 - y->dy->var);
    }
    
    y->dx = alloc_nbuf(y->lines, 1);
    if (y->dx == NULL)
        return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted);
    
    y->dx->min = x->u.izipped.min_dx;
    if (FLAG_DX(x->u.izipped) == DATA_CONSTANT) {
        y->dx->used = y->lines;
        memset(y->dx->data.raw, 0, nbuf_size(y->dx));
    }
    else {
        if (FLAG_DX(x->u.izipped) == DATA_ZIPPED) {
            rc = zlib_decompress(y->dx->data.u8, y->dx->size * 8, &n, x->u.izipped.dx, x->u.izipped.dx_size);
            if (rc)
                return rc;
        }
        else {
            n = x->u.izipped.dx_size;
            memcpy(y->dx->data.u8, x->u.izipped.dx, n);
        }
        elem_bits = (n * 8) / y->lines;
        if (elem_bits * y->lines / 8 != n)
            return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid);
        BITS_TO_VARIANT(y->dx, elem_bits);
        y->dx->used = n >> (4 - y->dx->var);
    }
    
    y->a = alloc_nbuf(y->lines, 1);
    if (y->a == NULL)
        return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted);
    
    y->a->min = x->u.izipped.min_a;
    if (FLAG_A(x->u.izipped) == DATA_CONSTANT) {
        y->a->used = y->lines;
        memset(y->a->data.raw, 0, nbuf_size(y->a));
    }
    else {
        if (FLAG_A(x->u.izipped) == DATA_ZIPPED) {
            rc = zlib_decompress(y->a->data.u8, y->a->size * 8, &n, x->u.izipped.a, x->u.izipped.a_size);
            if (rc)
                return rc;
        }
        else {
            n = x->u.izipped.a_size;
            memcpy(y->a->data.u8, x->u.izipped.a, n);
        }
        elem_bits = (n * 8) / y->lines;
        if (elem_bits * y->lines / 8 != n)
            return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid);
        BITS_TO_VARIANT(y->a, elem_bits);
        y->a->used = n >> (4 - y->a->var);
    }
    
    if (y->outliers) {
        y->outlier = alloc_nbuf(x->u.izipped.outliers, 1);
        if (y->outlier == NULL)
            return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted);
        
        y->outlier->min = x->u.izipped.min_outlier;
        if (FLAG_OUTLIER(x->u.izipped) == DATA_CONSTANT) {
            y->outlier->used = y->outliers;
            memset(y->outlier->data.raw, 0, nbuf_size(y->outlier));
        }
        else {
            if (FLAG_OUTLIER(x->u.izipped) == DATA_ZIPPED) {
                rc = zlib_decompress(y->outlier->data.u8, y->outlier->size * 8, &n, x->u.izipped.outlier, x->u.izipped.outlier_size);
                if (rc)
                    return rc;
            }
            else {
                n = x->u.izipped.outlier_size;
                memcpy(y->outlier->data.u8, x->u.izipped.outlier, n);
            }
            elem_bits = (n * 8) / x->u.izipped.outliers;
            if (elem_bits * x->u.izipped.outliers / 8 != n)
                return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid);
            BITS_TO_VARIANT(y->outlier, elem_bits);
            y->outlier->used = n >> (4 - y->outlier->var);
        }
    }
Exemple #15
0
static void on_msg_recv_cb(wslay_event_context_ptr ev,
                           const struct wslay_event_on_msg_recv_arg *arg,
                           void *user_data)
{
    struct transaction_t *txn = (struct transaction_t *) user_data;
    struct ws_context *ctx = (struct ws_context *) txn->ws_ctx;
    struct buf inbuf = BUF_INITIALIZER, outbuf = BUF_INITIALIZER;
    struct wslay_event_msg msgarg = { arg->opcode, NULL, 0 };
    uint8_t rsv = WSLAY_RSV_NONE;
    double cmdtime, nettime;
    const char *err_msg;
    int r, err_code = 0;

    /* Place client request into a buf */
    buf_init_ro(&inbuf, (const char *) arg->msg, arg->msg_length);

    /* Decompress request, if necessary */
    if (wslay_get_rsv1(arg->rsv)) {
        /* Add trailing 4 bytes */
        buf_appendmap(&inbuf, "\x00\x00\xff\xff", 4);

        r = zlib_decompress(txn, buf_base(&inbuf), buf_len(&inbuf));
        if (r) {
            syslog(LOG_ERR, "on_msg_recv_cb(): zlib_decompress() failed");

            err_code = WSLAY_CODE_PROTOCOL_ERROR;
            err_msg = DECOMP_FAILED_ERR;
            goto err;
        }

        buf_move(&inbuf, &txn->zbuf);
    }

    /* Log the uncompressed client request */
    buf_truncate(&ctx->log, ctx->log_tail);
    buf_appendcstr(&ctx->log, " (");
    if (txn->strm_ctx) {
        buf_printf(&ctx->log, "stream-id=%d; ",
                   http2_get_streamid(txn->strm_ctx));
    }
    buf_printf(&ctx->log, "opcode=%s; rsv=0x%x; length=%ld",
               wslay_str_opcode(arg->opcode), arg->rsv, arg->msg_length);

    switch (arg->opcode) {
    case WSLAY_CONNECTION_CLOSE:
        buf_printf(&ctx->log, "; status=%d; msg='%s'", arg->status_code,
                   buf_len(&inbuf) ? buf_cstring(&inbuf)+2 : "");
        txn->flags.conn = CONN_CLOSE;
        break;

    case WSLAY_TEXT_FRAME:
    case WSLAY_BINARY_FRAME:
        if (txn->conn->logfd != -1) {
            /* Telemetry logging */
            struct iovec iov[2];
            int niov = 0;

            assert(!buf_len(&txn->buf));
            buf_printf(&txn->buf, "<%ld<", time(NULL));  /* timestamp */
            WRITEV_ADD_TO_IOVEC(iov, niov,
                                buf_base(&txn->buf), buf_len(&txn->buf));
            WRITEV_ADD_TO_IOVEC(iov, niov, buf_base(&inbuf), buf_len(&inbuf));
            writev(txn->conn->logfd, iov, niov);
            buf_reset(&txn->buf);
        }

        /* Process the request */
        r = ctx->data_cb(&inbuf, &outbuf, &ctx->log, &ctx->cb_rock);
        if (r) {

            err_code = (r == HTTP_SERVER_ERROR ?
                        WSLAY_CODE_INTERNAL_SERVER_ERROR :
                        WSLAY_CODE_INVALID_FRAME_PAYLOAD_DATA);
            err_msg = error_message(r);
            goto err;
        }

        if (txn->conn->logfd != -1) {
            /* Telemetry logging */
            struct iovec iov[2];
            int niov = 0;

            assert(!buf_len(&txn->buf));
            buf_printf(&txn->buf, ">%ld>", time(NULL));  /* timestamp */
            WRITEV_ADD_TO_IOVEC(iov, niov,
                                buf_base(&txn->buf), buf_len(&txn->buf));
            WRITEV_ADD_TO_IOVEC(iov, niov, buf_base(&outbuf), buf_len(&outbuf));
            writev(txn->conn->logfd, iov, niov);
            buf_reset(&txn->buf);
        }

        /* Compress the server response, if supported by the client */
        if (ctx->ext & EXT_PMCE_DEFLATE) {
            r = zlib_compress(txn,
                              ctx->pmce.deflate.no_context ? COMPRESS_START : 0,
                              buf_base(&outbuf), buf_len(&outbuf));
            if (r) {
                syslog(LOG_ERR, "on_msg_recv_cb(): zlib_compress() failed");

                err_code = WSLAY_CODE_INTERNAL_SERVER_ERROR;
                err_msg = COMP_FAILED_ERR;
                goto err;
            }

            /* Trim the trailing 4 bytes */
            buf_truncate(&txn->zbuf, buf_len(&txn->zbuf) - 4);
            buf_move(&outbuf, &txn->zbuf);

            rsv |= WSLAY_RSV1_BIT;
        }

        /* Queue the server response */
        msgarg.msg = (const uint8_t *) buf_base(&outbuf);
        msgarg.msg_length = buf_len(&outbuf);
        wslay_event_queue_msg_ex(ev, &msgarg, rsv);

        /* Log the server response */
        buf_printf(&ctx->log,
                   ") => \"Success\" (opcode=%s; rsv=0x%x; length=%ld",
                   wslay_str_opcode(msgarg.opcode), rsv, msgarg.msg_length);
        break;
    }

  err:
    if (err_code) {
        size_t err_msg_len = strlen(err_msg);

        syslog(LOG_DEBUG, "wslay_event_queue_close()");
        wslay_event_queue_close(ev, err_code, (uint8_t *) err_msg, err_msg_len);

        /* Log the server response */
        buf_printf(&ctx->log,
                   ") => \"Fail\" (opcode=%s; rsv=0x%x; length=%ld"
                   "; status=%d; msg='%s'",
                   wslay_str_opcode(WSLAY_CONNECTION_CLOSE), rsv, err_msg_len,
                   err_code, err_msg);
    }

    /* Add timing stats */
    cmdtime_endtimer(&cmdtime, &nettime);
    buf_printf(&ctx->log, ") [timing: cmd=%f net=%f total=%f]",
               cmdtime, nettime, cmdtime + nettime);

    syslog(LOG_INFO, "%s", buf_cstring(&ctx->log));

    buf_free(&inbuf);
    buf_free(&outbuf);
}