Beispiel #1
0
void concat_encode(unsigned char * message,unsigned char* codeword) {
	unsigned char tmp_code[v*32*n1/k1],stripe[k1],stripe_code[n1];
	int index,i,j;
	
	for (index=0;index<v*32;index++) {
		tmp_code[index] = message[index];
	}
	for (i=0;i<v;i++) {
		for (j=0;j<sizeof(stripe);j++) {
			stripe[j] = message[i*k1+j];
		}
		initialize_ecc();
		encode_data(stripe,k1,stripe_code);
		for (j=0;j<n1-k1;j++) {
			tmp_code[index] = stripe_code[k1+j];
			index++;
		}
	}
	index = 0;
	for (i=0;i<v*n1/k1;i++) {
		for (j=0;j<sizeof(stripe);j++) {
			stripe[j] = tmp_code[i*k2+j];
		}
		encode_data(stripe,k2,stripe_code);
		for (j=0;j<n2;j++) {
			codeword[index] = stripe_code[j];
			index++;
		}
	}
}
Beispiel #2
0
// concatenate two RS code for encoding
void concat_encode(unsigned char * message,unsigned char* codeword) {
	unsigned char tmp_code[v*BLOCK_SIZE*n1/k1],stripe[k1],stripe_code[n1];
	int index,i,j;
	// read in the message part
	for (index=0;index<v*BLOCK_SIZE;index++) {
		tmp_code[index] = message[index];
	}
	// outer encoding on the message
	for (i=0;i<v;i++) {
		for (j=0;j<sizeof(stripe);j++) {
			stripe[j] = message[i*k1+j];
		}
		initialize_ecc();
		// encode for each outer stripe
		encode_data(stripe,k1,stripe_code);
		// append parity at the end
		for (j=0;j<n1-k1;j++) {
			tmp_code[index] = stripe_code[k1+j];
			index++;
		}
	}
	index = 0;
	// perform inner encoding
	for (i=0;i<v*n1/k1;i++) {
		for (j=0;j<sizeof(stripe);j++) {
			stripe[j] = tmp_code[i*k2+j];
		}
		// encode for each inner stripe
		encode_data(stripe,k2,stripe_code);
		for (j=0;j<n2;j++) {
			codeword[index] = stripe_code[j];
			index++;
		}
	}
}
Beispiel #3
0
void encode_file(FILE *fpc, FILE *fpd, FILE *fpo) {
	size_t n, x;
	char cont[CONTSIZE], data[DATASIZE];
	struct stash_hdr shdr = {0};

	// Write restricted area
	n = fread(cont, 1, STARTBYTE, fpc);
	fwrite(cont, 1, (int)n, fpo);

	// Write header
	shdr.len = get_filesize(fpd);
	memset(cont, 0, sizeof(cont));
	n = fread(cont, 1, sizeof(struct stash_hdr)<<3, fpc);
	encode_data(cont, (int)n, (unsigned char *)&shdr, sizeof(struct stash_hdr), 0); 
	fwrite(cont, 1, n, fpo);

	// Write data
	while((n = fread(cont, 1, sizeof(cont), fpc))) {
		if((x = fread(data, 1, n>>3, fpd)) > 0)
			encode_data(cont, (int)x<<3, data, (int)x, 1);

		fwrite(cont, 1, (int)n, fpo);
	}

	return;
}
void xpass_encode_switch_event_headers(char *ebuf, int buf_len, switch_event_t *event) {
    switch_event_header_t *hp;
    char *uuid = switch_event_get_header(event, "unique-id");
    int i;
    int left_buf_len = buf_len;
    char * tmp = ebuf;

    for (i = 0, hp = event->headers; hp; hp = hp->next, i++);

    if (event->body)
        i++;


    if (uuid) {
		char *unique_id = switch_event_get_header(event, "unique-id");
		tmp = encode_data(tmp, unique_id, strlen(unique_id));
    } else {
        tmp = encode_data(tmp, "undefined", 9);
    }
    tmp  = encode_data(tmp, "\r\n", 2);

    for (hp = event->headers; hp; hp = hp->next) {
        tmp = encode_data(tmp, hp->name, strlen(hp->name));
        tmp = encode_data(tmp, ":", 1);
        switch_url_decode(hp->value);
        tmp = encode_data(tmp, hp->value, strlen(hp->value));
        tmp = encode_data(tmp, "\r\n", 2);
    }
    tmp = encode_data(tmp, "\r\n", 2);
    if (event->body) {
        tmp = encode_data(tmp, event->body, strlen(event->body));
    }

}
Beispiel #5
0
static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* target, 
                                          bool sendBinaries) {
    if (sendBinaries) {
        SkData* encoded = image.encode(SkImageEncoder::kPNG_Type, 100);
        if (encoded == nullptr) {
            // PNG encode doesn't necessarily support all color formats, convert to a different
            // format
            size_t rowBytes = 4 * image.width();
            void* buffer = sk_malloc_throw(rowBytes * image.height());
            SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), 
                                                    kN32_SkColorType, kPremul_SkAlphaType);
            if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) {
                SkDebugf("readPixels failed\n");
                return false;
            }
            SkImage* converted = SkImage::NewRasterCopy(dstInfo, buffer, rowBytes);
            encoded = converted->encode(SkImageEncoder::kPNG_Type, 100);
            if (encoded == nullptr) {
                SkDebugf("image encode failed\n");
                return false;
            }
            free(converted);
            free(buffer);
        }
        Json::Value bytes;
        encode_data(encoded->data(), encoded->size(), &bytes);
        (*target)[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes;
        encoded->unref();
    }
    else {
        SkString description = SkStringPrintf("%dx%d pixel image", image.width(), image.height());
        (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description.c_str());
    }
    return true;
}
Beispiel #6
0
		bool encode_frame(analysis_filter* filter, int total_bits) {
			if (false == in.has_more_data())
				return false;
			this->total_bits = total_bits;
			try {
				filter_data(filter, in);
				calc_scale();
				for (int c = 0; NUMBER_CHANNELS != c; ++c)
					model[c]->calc_smr(in[c], scale[c], smr[c], NUMBER_PARTS);
				calc_allocation();
				encode_config();
				encode_sideinfo();
				encode_data();
				while (this->total_bits > 0)
					if (this->total_bits >= 8) {
						str.write_bits(0, 8);
						this->total_bits -= 8;
					}
					else {
						str.write_bits(0, this->total_bits);
						this->total_bits = 0;
					}
				return true;
			}
			catch (stream_exception) {
				return false;
			}
		}
Beispiel #7
0
TEST_F(EncodeDecode, EmptyEncode) {
  char p[4] = {};
  encode_data((unsigned char *)p, 0, (unsigned char *)p);
  EXPECT_EQ(0, p[0]);
  EXPECT_EQ(0, p[1]);
  EXPECT_EQ(0, p[2]);
  EXPECT_EQ(0, p[3]);
};
Beispiel #8
0
		void Base64EncoderImpl::feed(const void *_data, int size, bool append_result)
		{
			int pos = 0;
			const unsigned char *data = (const unsigned char *)_data;

			if (!append_result)
				_result->set_size(0);

			// Handle any left-over data from last encode:

			if (chunk_filled > 0)
			{
				int needed = 3 - chunk_filled;
				if (size >= needed)
				{
					memcpy(chunk + chunk_filled, data, needed);
					int out_pos = _result->size();
					_result->set_size(out_pos + 4);
					encode_data((unsigned char *)_result->data() + out_pos, chunk, 3);
					pos += needed;
					chunk_filled = 0;
				}
				else
				{
					memcpy(chunk + chunk_filled, data, size);
					chunk_filled += size;
					return;
				}
			}

			// Base64 encode what's available to us now:

			int blocks = (size - pos) / 3;
			int out_pos = _result->size();
			_result->set_size(out_pos + blocks * 4);
			encode_data((unsigned char *)_result->data() + out_pos, data + pos, blocks * 3);
			pos += blocks * 3;

			// Save data for last incomplete block:

			int leftover = size - pos;
			if (leftover > 3)
				throw Exception("Base64 encoder is broken!");
			chunk_filled = leftover;
			memcpy(chunk, data + pos, leftover);
		}
Beispiel #9
0
TEST_F(EncodeDecode, CorrectEncode) {
  unsigned char p[10] = {'a', 'b', 'c', 'd', 'e', 'f'};
  encode_data(p, 6, p);
  //fprintf(stdout, "%d %d %d %d\n", p[6], p[7], p[8], p[9]);
  EXPECT_EQ(0x1f, p[6]);
  EXPECT_EQ(0xa3, p[7]);
  EXPECT_EQ(0x9a, p[8]);
  EXPECT_EQ(0x3b, p[9]);
};
Beispiel #10
0
int
main (int argc, char *argv[])
{
 
  int erasures[16];
  int nerasures = 0;

  /* Initialization the ECC library */
 
  initialize_ecc ();
 
  /* ************** */
 
  /* Encode data into codeword, adding NPAR parity bytes */
  encode_data(msg, sizeof(msg), codeword);
 
  printf("Encoded data is: \"%s\"\n", codeword);
  printf("Encoded data length: \"%d\"\n", sizeof(codeword));
  printf("msg length: \"%d\"\n", sizeof(msg));
  int i;
  		for (i = 0; (i < sizeof(msg)); i++) {
			printf("%02x", msg[i]);
		}
		printf("\n");
		for (i = 0; (i < sizeof(codeword)); i++) {
			printf("%02x", codeword[i]);
		}
		printf("\n");
 
#define ML (sizeof (msg) + NPAR)


  /* We need to indicate the position of the erasures.  Eraseure
     positions are indexed (1 based) from the end of the message... */

  erasures[nerasures++] = ML-17;
  erasures[nerasures++] = ML-19;

 
  /* Now decode -- encoded codeword size must be passed */
  decode_data(codeword, ML);

  /* check if syndrome is all zeros */
  if (check_syndrome () != 0) {
    correct_errors_erasures (codeword, 
			     ML,
			     nerasures, 
			     erasures);
 
    printf("Corrected codeword: \"%s\"\n", codeword);
  }
 
  exit(0);
}
Beispiel #11
0
/**
 * Transmit a packet from the transmit packet buffer window.
 * \param[in] data The packet handler instance data pointer.
 * \param[in] p A pointer to the packet buffer.
 * \return 1 Success
 * \return 0 Failure
 */
static uint8_t PHLTransmitPacket(PHPacketDataHandle data, PHPacketHandle p)
{

	if(!data->output_stream)
		return 0;

	// Set the sequence ID to the current ID.
	p->header.tx_seq = data->tx_seq_id++;

	// Add the error correcting code.
	encode_data((unsigned char*)p, PHPacketSize(p), (unsigned char*)p);

	// Transmit the packet using the output stream.
	if(data->output_stream(p) == -1)
		return 0;

	return 1;
}
Beispiel #12
0
static void apply_paint_typeface(const SkPaint& paint, Json::Value* target, 
                                 bool sendBinaries) {
    SkTypeface* typeface = paint.getTypeface();
    if (typeface != nullptr) {
        if (sendBinaries) {
            Json::Value jsonTypeface;
            SkDynamicMemoryWStream buffer;
            typeface->serialize(&buffer);
            void* data = sk_malloc_throw(buffer.bytesWritten());
            buffer.copyTo(data);
            Json::Value bytes;
            encode_data(data, buffer.bytesWritten(), &bytes);
            jsonTypeface[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes;
            free(data);
            (*target)[SKJSONCANVAS_ATTRIBUTE_TYPEFACE] = jsonTypeface;
        }
    }
}
Beispiel #13
0
static void flatten(const SkFlattenable* flattenable, Json::Value* target, bool sendBinaries) {
    if (sendBinaries) {
        SkWriteBuffer buffer;
        flattenable->flatten(buffer);
        void* data = sk_malloc_throw(buffer.bytesWritten());
        buffer.writeToMemory(data);
        Json::Value bytes;
        encode_data(data, buffer.bytesWritten(), &bytes);
        Json::Value jsonFlattenable;
        jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_NAME] = Json::Value(flattenable->getTypeName());
        jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes;
        (*target) = jsonFlattenable;
        free(data);
    }
    else {
        (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(flattenable->getTypeName());
    }
}
Beispiel #14
0
TEST_F(EncodeDecode, Recover) {
  unsigned char p[10] = {'a', 'b', 'c', 'd', 'e', 'f'};
  encode_data(p, 6, p);
  unsigned char p2[10];
  for (int i = 0; i < 10; i++)
    p2[i] = p[i];
  p2[4] = 30;

  // verify it flags the error
  decode_data(p2, 6 + RS_ECC_NPARITY);
  EXPECT_EQ(1, check_syndrome());

  // verify it is corrected
  EXPECT_EQ(1, correct_errors_erasures(p2, 10, 0, 0));

  for (int i = 0; i < 6; i++)
    EXPECT_EQ(p[i], p2[i]);

};
Beispiel #15
0
void continuous_test(double ebn0) {
  uint8_t data[256] = {0};
  uint8_t encoded[650] = {0};
  uint8_t bits[5200] = {0};
  uint8_t dec_data[256] = {0};
  int8_t error[2] = {0};
  FILE *fp;
  uint16_t i;

  // Es/No in dB - Energy per symbol (symbol, like channel bit)
  double esn0 = ebn0 + 10*log10(1. / 2.);

  // Compute noise voltage. The 0.5 factor accounts for BPSK seeing
  // only half the noise power, and the sqrt() converts power to
  // voltage.
  double gain = 1./sqrt(0.5/pow(10.,esn0/10.));

  gettimeofday(&start, NULL);
  while (run) {
    generate_random_data(&data);
#if (DEBUG >= 1)
    fp = fopen("data", "wb");
    fwrite(data, 256, 1, fp);
    fclose(fp);
#endif

    encode_data(&data, &encoded);

    // AWGN channel simulation
    for (i=0; i<5200; ++i) {
      bits[i] = addnoise(encoded[i>>3] & (1 << (7 - (i & 7))), gain, 32.0);
    }

    decode_data(&bits, &dec_data, &error);
#if (DEBUG >= 1)
    fp = fopen("dec_data", "wb");
    fwrite(dec_data, 256, 1, fp);
    fclose(fp);
#endif

    stat_refresh(&error, is_equal(&data, &dec_data));
  }
}
Beispiel #16
0
/*! \brief encode a TRAU frame from the decoded bits
 *  \param[out] trau_bits output buffer, will contain encoded bits
 *  \param[in] fr decoded trau frame structure
 *  \returns 0 in case of success, < 0 in case of error
 */
int encode_trau_frame(uint8_t *trau_bits, const struct decoded_trau_frame *fr)
{
	uint8_t cbits5 = get_bits(fr->c_bits, 0, 5);

	/* 16 bits of sync header */
	memset(trau_bits, 0, 16);

	switch (cbits5) {
	case TRAU_FT_FR_UP:
	case TRAU_FT_FR_DOWN:
	case TRAU_FT_IDLE_UP:
	case TRAU_FT_IDLE_DOWN:
	case TRAU_FT_EFR:
		encode_fr(trau_bits, fr);
		break;
	case TRAU_FT_DATA_UP:
	case TRAU_FT_DATA_DOWN:
		encode_data(trau_bits, fr);
		break;
	case TRAU_FT_AMR:
	case TRAU_FT_OM_UP:
	case TRAU_FT_OM_DOWN:
	case TRAU_FT_D145_SYNC:
	case TRAU_FT_EDATA:
		LOGP(DLMUX, LOGL_NOTICE, "unimplemented TRAU Frame Type "
			"0x%02x\n", cbits5);
		return -1;
		break;
	default:
		LOGP(DLMUX, LOGL_NOTICE, "unknown TRAU Frame Type "
			"0x%02x\n", cbits5);
		return -1;
		break;
	}

	return 0;
}
Beispiel #17
0
int flush_queue(unsigned char pid) {
	log_fine("flushing %d ...", pid);

	// return, if the queue is empty
	if(outQueueSize == 0) {
		log_fine("empty");
		return 0;
	}

	log_fine("count: %d", outQueueSize);

	// otherwise, create a data message and send it
	// FIXME ensure, that outQueueSize < proto_data_size
	// or rather: split into several packages, if this is the case
	struct Message *m = encode_data(pid, outQueueSize);
	message_payload(m, outQueue, outQueueSize);
    print_message(m);
	int rslt = medium_send(m);

	message_free(m);
	outQueueSize = 0;

	return rslt;
}
Beispiel #18
0
/* Process a buffer (including reencoding or encoding, if desired).
 * Returns: >0 - success
 *           0 - shout error occurred
 *          -1 - no data produced
 *          -2 - fatal error occurred
 */
int process_and_send_buffer(stream_description *sdsc, ref_buffer *buffer)
{
    if(sdsc->reenc)
    {
        unsigned char *buf;
        int buflen,ret;

        ret = reencode_page(sdsc->reenc, buffer, &buf, &buflen);
        if(ret > 0) 
        {
            ret = stream_send_data(sdsc, buf, buflen);
            free(buf);
            return ret;
        }
        else if(ret==0) /* No data produced by reencode */
            return -1;
        else
        {
            LOG_ERROR0("Fatal reencoding error encountered");
            return -2;
        }
    }
    else if (sdsc->enc)
    {
        ogg_page og;
        int be = (sdsc->input->subtype == INPUT_PCM_BE_16)?1:0;
        int ret=1;

        /* We use critical as a flag to say 'start a new stream' */
        if(buffer->critical)
        {
            if(sdsc->resamp) {
                resample_finish(sdsc->resamp);
                encode_data_float(sdsc->enc, sdsc->resamp->buffers,
                        sdsc->resamp->buffill);
                resample_clear(sdsc->resamp);
                sdsc->resamp = resample_initialise (sdsc->stream->channels,
                        sdsc->stream->resampleinrate, sdsc->stream->resampleoutrate);
            }
            encode_finish(sdsc->enc);
            while(encode_flush(sdsc->enc, &og) != 0)
            {
                if ((ret = stream_send_data(sdsc, og.header, og.header_len)) == 0)
                    return 0;
                if ((ret = stream_send_data(sdsc, og.body, og.body_len)) == 0)
                    return 0;
            }
            encode_clear(sdsc->enc);

            if(sdsc->input->metadata_update)
            {
                vorbis_comment_clear(&sdsc->vc);
                vorbis_comment_init(&sdsc->vc);

                sdsc->input->metadata_update(sdsc->input->internal, &sdsc->vc);
            }

            sdsc->enc = encode_initialise(sdsc->stream->channels,
                    sdsc->stream->samplerate, sdsc->stream->managed, 
                    sdsc->stream->min_br, sdsc->stream->nom_br, 
                    sdsc->stream->max_br, sdsc->stream->quality,
                    &sdsc->vc);
            if(!sdsc->enc) {
                LOG_ERROR0("Failed to initialise encoder");
                return -2;
            }
            sdsc->enc->max_samples_ppage = sdsc->stream->max_samples_ppage;
        }

        if(sdsc->downmix) {
            downmix_buffer(sdsc->downmix, (signed char *)buffer->buf, buffer->len, be);
            if(sdsc->resamp) {
                resample_buffer_float(sdsc->resamp, &sdsc->downmix->buffer, 
                        buffer->len/4);
                encode_data_float(sdsc->enc, sdsc->resamp->buffers, 
                        sdsc->resamp->buffill);
            }
            else
                encode_data_float(sdsc->enc, &sdsc->downmix->buffer,
                       buffer->len/4);
        }
        else if(sdsc->resamp) {
            resample_buffer(sdsc->resamp, (signed char *)buffer->buf, 
                    buffer->len, be);
            encode_data_float(sdsc->enc, sdsc->resamp->buffers,
                    sdsc->resamp->buffill);
        }
        else {
            encode_data(sdsc->enc, (signed char *)(buffer->buf), 
                    buffer->len, be);
        }

        while(encode_dataout(sdsc->enc, &og) > 0)
        {
            if ((ret = stream_send_data(sdsc, og.header, og.header_len)) == 0)
                return 0;
            if ((ret = stream_send_data(sdsc, og.body, og.body_len)) == 0)
                return 0;
        }
                        
        return ret;
    }
    else    
        return stream_send_data(sdsc, buffer->buf, buffer->len);
}
Beispiel #19
0
void submit_post(const char *host, const char *port, const char *method, const char *url, const char *postdata, uint32_t timeout)
{
    int sockfd, n;
    unsigned int i;
    char *buf, *encoded=NULL;
    size_t bufsz;
    ssize_t recvsz;
    char chunkedlen[21];
    fd_set readfds;
    struct timeval tv;
    char *acceptable_methods[] = {
        "GET",
        "PUT",
        "POST",
        NULL
    };

    for (i=0; acceptable_methods[i] != NULL; i++)
        if (!strcmp(method, acceptable_methods[i]))
            break;

    if (acceptable_methods[i] == NULL)
        return;

    bufsz = strlen(method);
    bufsz += sizeof("   HTTP/1.1") + 2; /* Yes. Three blank spaces. +1 for the \n */
    bufsz += strlen(url);
    bufsz += sizeof("Host: \r\n");
    bufsz += strlen(host);
    bufsz += sizeof("Connection: Close\r\n");
    bufsz += 4; /* +4 for \r\n\r\n */

    if (!strcmp(method, "POST") || !strcmp(method, "PUT")) {
        encoded = encode_data(postdata);
        if (!(encoded))
            return;
        snprintf(chunkedlen, sizeof(chunkedlen), "%zu", strlen(encoded));
        bufsz += sizeof("Content-Type: application/x-www-form-urlencoded\r\n");
        bufsz += sizeof("Content-Length: \r\n");
        bufsz += strlen(chunkedlen);
        bufsz += strlen(encoded);
    }

    buf = cli_calloc(1, bufsz);
    if (!(buf)) {
        if ((encoded))
            free(encoded);

        return;
    }

    snprintf(buf, bufsz, "%s %s HTTP/1.1\r\n", method, url);
    snprintf(buf+strlen(buf), bufsz-strlen(buf), "Host: %s\r\n", host);
    snprintf(buf+strlen(buf), bufsz-strlen(buf), "Connection: Close\r\n");

    if (!strcmp(method, "POST") || !strcmp(method, "PUT")) {
        snprintf(buf+strlen(buf), bufsz-strlen(buf), "Content-Type: application/x-www-form-urlencoded\r\n");
        snprintf(buf+strlen(buf), bufsz-strlen(buf), "Content-Length: %s\r\n", chunkedlen);
        snprintf(buf+strlen(buf), bufsz-strlen(buf), "\r\n");
        snprintf(buf+strlen(buf), bufsz-strlen(buf), "%s", encoded);
        free(encoded);
    }
#if defined(_WIN32)
	sockfd = connect_host(host, port, timeout, 0);
#else
    sockfd = connect_host(host, port, timeout, 1);
#endif
    if (sockfd < 0) {
        free(buf);
        return;
    }

    cli_dbgmsg("stats - Connected to %s:%s\n", host, port);

    if ((size_t)send(sockfd, buf, strlen(buf), 0) != (size_t)strlen(buf)) {
        closesocket(sockfd);
        free(buf);
        return;
    }

    cli_dbgmsg("stats - Sending %s\n", buf);

    while (1) {
        FD_ZERO(&readfds);
        FD_SET(sockfd, &readfds);

        /*
         * Check to make sure the stats submitted okay (so that we don't kill the HTTP request
         * while it's being processed). Give a ten-second timeout so we don't have a major
         * impact on scanning.
         */
        tv.tv_sec = timeout;
        tv.tv_usec = 0;
        if ((n = select(sockfd+1, &readfds, NULL, NULL, &tv)) <= 0)
            break;

        if (FD_ISSET(sockfd, &readfds)) {
            memset(buf, 0x00, bufsz);
            if ((recvsz = recv(sockfd, buf, bufsz-1, 0) <= 0))
                break;

            buf[bufsz-1] = '\0';

            cli_dbgmsg("stats - received: %s\n", buf);

            if (strstr(buf, "STATOK")) {
                cli_dbgmsg("stats - Data received okay\n");
                break;
            }
        }
    }

    closesocket(sockfd);
    free(buf);
}
Beispiel #20
0
TEST_F(EncodeDecode, PassEncode) {
  unsigned char p[10] = {'a', 'b', 'c', 'd', 'e', 'f'};
  encode_data(p, 6, p);
  decode_data(p, 6 + RS_ECC_NPARITY);
  EXPECT_EQ(0, check_syndrome());
};
Beispiel #21
0
int main()
{

    // Set the number of symbols (i.e. the generation size in RLNC
    // terminology) and the size of a symbol in bytes
    uint32_t max_symbols = 42;
    uint32_t max_symbol_size = 64;

    std::string encode_filename = "encode-file.bin";

    // Create a test file for encoding.
    std::ofstream encode_file;
    encode_file.open (encode_filename, std::ios::binary);

    uint32_t file_size = 50000;
    std::vector<char> encode_data(file_size);
    std::vector<char> decode_data;

    // Just write some bytes to the file
    for(uint32_t i = 0; i < file_size; ++i)
    {
        encode_data[i] = rand() % 255;
    }
    encode_file.write(&encode_data[0], file_size);
    encode_file.close();

    // Select the encoding and decoding algorithms
    typedef kodo::full_rlnc_encoder<fifi::binary>
        encoder_t;

    typedef kodo::full_rlnc_decoder<fifi::binary>
        decoder_t;

    // Now for the encoder we use a file_encoder with the chosen
    // encoding algorithm
    typedef kodo::file_encoder<encoder_t>
        file_encoder_t;

    // For decoding we use an object_decoder with the chosen
    // decoding algorithm
    typedef kodo::object_decoder<decoder_t>
        object_decoder_t;

    // Create the encoder factory - builds the individual encoders used
    file_encoder_t::factory encoder_factory(max_symbols, max_symbol_size);

    // Create the actual file encoder using the encoder factory and
    // the filename of the file to be encoded
    file_encoder_t file_encoder(encoder_factory, encode_filename);

    // Create the decoder factory - build the individual decoders used
    object_decoder_t::factory decoder_factory(max_symbols, max_symbol_size);

    // Create the object decoder using the decoder factory and the
    // size of the file to be decoded
    object_decoder_t object_decoder(decoder_factory, file_size);

    // Now in the following loop we go through all the encoders
    // needed to encode the entire file. We the build the corresponding
    // decoder and decode the chunk immediately. In practice where
    // encoders and decoders are on different devices e.g. connected
    // over a network, we would have to pass also the encoder and decoder
    // index between the source and sink to allow the correct data would
    // passed from encoder to corresponding decoder.
    for(uint32_t i = 0; i < file_encoder.encoders(); ++i)
    {
        auto encoder = file_encoder.build(i);
        auto decoder = object_decoder.build(i);

        // Set the encoder non-systematic
        if(kodo::has_systematic_encoder<encoder_t>::value)
            kodo::set_systematic_off(encoder);

        std::vector<uint8_t> payload(encoder->payload_size());

        while( !decoder->is_complete() )
        {
            // Encode a packet into the payload buffer
            encoder->encode( &payload[0] );

            // In practice send the payload over a network, save it to
            // a file etc. Then when needed build and pass it to the decoder

            // Pass that packet to the decoder
            decoder->decode( &payload[0] );
        }

        std::vector<uint8_t> data_out(decoder->block_size());
        decoder->copy_symbols(sak::storage(data_out));
        data_out.resize(decoder->bytes_used());

        decode_data.insert(decode_data.end(),
                           data_out.begin(),
                           data_out.end());
    }

    // Check we properly decoded the data
    if (std::equal(decode_data.begin(),
                   decode_data.end(), encode_data.begin()))
    {
        std::cout << "Data decoded correctly" << std::endl;
    }
    else
    {
        std::cout << "Unexpected failure to decode "
                  << "please file a bug report :)" << std::endl;
    }
}
Beispiel #22
0
int
main (int argc, char *argv[])
{
    int erasures[16];
    int nerasures = 0;
    char* filename = argv[1];
    char command[100];
    printf("reading the original file %s\n",argv[1]);

    //filename = argv[1];
    sprintf(command,"md5 %s",argv[1]);
    system(command);

    /* Initialization the ECC library */
    
    initialize_ecc ();
    
    //Open file
    file = fopen(argv[1], "r");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", argv[1]);
        exit(1);
    }
    
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    fseek(file, 0, SEEK_SET);
    
    //Allocate memory
    buffer=(unsigned char *)malloc(fileLen+1);
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
        exit(2);
    }
    
    //Read file contents into buffer
    fread(buffer, fileLen, 1, file);

    fclose(file);

    long i = fileLen;
    long pos = 0;
    int writeFlag = TRUE;
    strcat(filename,".encode");
    printf("----------------------------------------------\n");
    printf("encoding the original file into %s\n",filename);

    while (i>0)
    {
        //system(command);

        int msgLen;
        if (i>256-NPAR) {
            msgLen = 256-NPAR;
        }
        else {
            msgLen = i;
        }
        unsigned char msg[msgLen];
        int k;

        for(k=0;k<msgLen;k++) {
            msg[k] = buffer[pos];
            pos++;
        }
        encode_data(msg, sizeof(msg), codeword);

        if (writeFlag) {
            file = fopen(filename, "w+");
            writeFlag = FALSE;
        }
        else {
            file = fopen(filename, "a+");
        }
        fwrite(codeword, msgLen+NPAR, 1, file);

        fclose(file);
        i = i - msgLen;
    }
    free(buffer);

    sprintf(command,"md5 %s",filename);
    system(command);

    file = fopen(filename, "r");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", filename);
        exit(1);
    }
    
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    fseek(file, 0, SEEK_SET);
    //Allocate memory
    buffer=(unsigned char *)malloc(fileLen+1);
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
        exit(2);
    }
    
    //Read file contents into buffer
    fread(buffer, fileLen, 1, file);
    
    fclose(file);
    i = fileLen;
    pos = 0;
    writeFlag = TRUE;
    strcat(filename,".corrupt");
    printf("----------------------------------------------\n");
    printf("making some errors and write into the file %s\n",filename);

    int k;
    for(k = 0;k<10;k++) {
        long h = rand()%fileLen;
        buffer[h] = buffer[h] % 20;
    }
    file = fopen(filename, "w+");
    fwrite(buffer, fileLen, 1, file);
    fclose(file);

    free(buffer);

    sprintf(command,"md5 %s",filename);
    system(command);
    
    file = fopen(filename, "r");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", filename);
        exit(1);
    }
    
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    fseek(file, 0, SEEK_SET);
    //Allocate memory
    buffer=(unsigned char *)malloc(fileLen+1);
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
        exit(2);
    }
    
    //Read file contents into buffer
    fread(buffer, fileLen, 1, file);
    
    fclose(file);
    i = fileLen;
    pos = 0;
    writeFlag = TRUE;
    strcat(filename,".recover");
    printf("----------------------------------------------\n");
    printf("recovering corrupted file into %s\n",filename);
    while (i>0)
    {
        int msgLen;
        if (i>256) {
            msgLen = 256;
        }
        else {
            msgLen = i;
        }
        unsigned char msg[msgLen];
        int k;
        
        for(k=0;k<msgLen;k++) {
            msg[k] = buffer[pos];
            pos++;
        }
        decode_data(msg, msgLen);

        if (check_syndrome () != 0) {
            correct_errors_erasures (msg,msgLen,nerasures,erasures);
        }
        if (writeFlag) {
            file = fopen(filename, "w+");
            writeFlag = FALSE;
        }
        else {
            file = fopen(filename, "a+");
        }
        fwrite(msg, msgLen-NPAR, 1, file);
        
        fclose(file);
        i = i - msgLen;
    }
    sprintf(command,"md5 %s",filename);
    system(command);
    
    free(buffer);
    exit(0);
}
Beispiel #23
0
// outer layer ECC using incremental encoding
int inc_encoding (FILE* fp,int* prptable)
{
	printf("\nIncremental encoding starts...\n");
	int i,j,enc_blocks,d=n-k;
	// get file length
	fseek(fp,0,SEEK_END);
	fileLen = ftell(fp);
	// divide by message length k, get number of encoding blocks
	if(fileLen % k==0) 
		enc_blocks = fileLen/k;
	else
		enc_blocks = fileLen/k+1;
	printf("There are %d encoding blocks\n",enc_blocks);
	unsigned char message[k];
	unsigned char codeword[n];
	unsigned char ** code; // used to store parity part
	
	long filecounter = 0;
	int blockcounter = 0;
	int round = 0;

	// code is enc_blocks * d
	code = (unsigned char **) malloc(enc_blocks*sizeof(unsigned char *));  
	for (i = 0; i < enc_blocks; i++) {
   	code[i] = (unsigned char *) malloc(d*sizeof(unsigned char)); 
   	int ii;
   	for (ii=0;ii<d;ii++)
   		code[i][ii]=0; 
   	}
   	

   rewind(fp);
	while (!feof(fp))
	{
		unsigned char * buf; 
		if ((buf = malloc(sizeof(unsigned char)*readLen))==NULL) {
			printf("malloc error: inc_encoding\n");
			exit(1);
		}
		// incremental encoding, read reaLen each time
		readStartTime = getCPUTime();
		clock_gettime(CLOCK_MONOTONIC, &start);
		printf("max read in %d bytes\n",readLen);
		size_t br = fread(buf, 1, readLen, fp);
		printf("Read in %lu bytes\n",br);
		fflush(stdout);
		clock_gettime(CLOCK_MONOTONIC, &finish);
		double addTime = finish.tv_sec - start.tv_sec;
		addTime += (finish.tv_nsec - start.tv_nsec)/1000000000.0;
		readTime += getCPUTime() - readStartTime+addTime;
		// keep a counter to know where the file pointer up to
		filecounter = filecounter + br;
		if (br!=0) {
			printf("round %d\n",round);
			printf("filecounter = %lu\n",filecounter);
			for(i=0;i<enc_blocks;i++) {
				for(j=0;j<k;j++) {
					// for each byte in each message, compute index
					int index = i*k+j;
					// get block and byte index
					int block_index = index/BLOCK_SIZE;
					int byte_index = index%BLOCK_SIZE;
					// if reach the end, padding 0s
					if (index>=fileLen) {
						int a;
						for(a=j;a<k;a++)
							message[a]=0;
						break;
					}
					// compute the PRPed index in the file
					unsigned long file_index = prptable[block_index]*BLOCK_SIZE+byte_index;
					
					// check if this byte is read in the memory or not, copy if yes, put 0 otherwise
					if(file_index<filecounter && file_index>=(filecounter-br)) {
						unsigned long newind = file_index-filecounter+br;
						message[j] = buf[newind];
					}
					else 
						message[j] = 0;
				}
				//printf("msg for block %d: ",i);
				//displayCharArray(message,k);
				// do a partial encoding on the message
				encode_data(message,k,codeword);
				// concatenate with previous code to get a whole
				/*printf("code for block %d: ",i);
				displayCharArray(codeword,n);
				printf("parity (before) for block %d: ",i);
				displayCharArray(code[i],n-k);*/
				for(j=0;j<d;j++)
					code[i][j] = code[i][j] ^ codeword[k+j];
				//printf("parity for block %d: ",i);
				//displayCharArray(code[i],n-k);
				//printf("\n");
			}
			round = round + 1;
		}
		free(buf);
	}
	/*// ------------- for debugging
	unsigned char a[fileLen],r[fileLen];
	unsigned char newc[n],newm[k];
	rewind(fp);
	fread(a, 1, fileLen, fp);
	printf("original:\n");
	for (i=0;i<fileLen;i++) {
		printf("%02x",a[i]);
	}
	printf("\n");
	for (i=0;i<fileLen/32;i++) {
		for (j=0;j<32;j++) {
			r[i*32+j] = a[prptable[i]*32+j];
		}
	}
	printf("prped:\n");
	for (i=0;i<fileLen;i++) {
		printf("%02x",r[i]);
	}
	printf("\n");
	for (i=0;i<enc_blocks;i++) {
		printf("parity part %d: ",i);
		displayCharArray(code[i],d);

		unsigned char newcode[n];
		int iii;
		int ii;
		for(ii=0;ii<k;ii++) {
			if (i*k+ii>=fileLen)
				break;
			newcode[ii] = r[i*k+ii];
			newm[ii] = r[i*k+ii];
		}
		if (i==enc_blocks-1) {
			for(ii=0;ii<k-fileLen%k;ii++){
				newm[fileLen%k+ii]=0;
				newcode[fileLen%k+ii] = 0;
			}
		}
		encode_data(newm,k,newc);
		printf("actual code %d: ",i);
		displayCharArray(newc,n);
		for(iii=0;iii<d;iii++) {
			newcode[k+iii] = code[i][iii];
		}
		newcode[0] = 99;
		printf("whole code %d: ",i);
		displayCharArray(newcode,n);
		decode_data(newcode, n);
		int erasure[1];
		int syn = check_syndrome ();
		printf("syndrome: %d\n",syn);
		if (syn != 0) {
			correct_errors_erasures(newcode,n,0,erasure);
		}
		printf("decode %d: ",i);
		displayCharArray(newcode,n);
	}
	//--------------- for debugging */
	free(prptable);
	prptable = NULL;
	// perform another PRP for parity part
	prptable = malloc(sizeof(int)*(enc_blocks));
	printf("\nSRF PRP for the outer layer ECC...\n");
   prpStartTime = getCPUTime();
	prptable = prp(enc_blocks, k_ecc_perm);
   prpTime += getCPUTime() - prpStartTime;
   
   // encrypt parity part and append to the file with PRPed order
	enc_init(k_ecc_enc);
	for (i=0;i<enc_blocks;i++) {
		unsigned char ct[d];

    	clock_gettime(CLOCK_MONOTONIC, &start);
    	encStartTime = getCPUTime();
		encrypt(ct,code[prptable[i]],sizeof(ct));
		clock_gettime(CLOCK_MONOTONIC, &finish);
		double addTime = finish.tv_sec - start.tv_sec;
		addTime += (finish.tv_nsec - start.tv_nsec)/1000000000.0;
		encTime += getCPUTime()-encStartTime+addTime;

		//printf("encrypted for %d: ",i);
		//displayCharArray(ct,sizeof(ct));
		//unsigned char pt[d];
		//decrypt(ct,pt,sizeof(ct));
		//printf("decrypted for %d: ",i);
		//displayCharArray(pt,sizeof(ct));
    	clock_gettime(CLOCK_MONOTONIC, &start);
		write1StartTime = getCPUTime();
		fwrite(ct,d,1,fp);
		clock_gettime(CLOCK_MONOTONIC, &finish);
		addTime = finish.tv_sec - start.tv_sec;
		addTime += (finish.tv_nsec - start.tv_nsec)/1000000000.0;
    	write1Time += getCPUTime()-write1StartTime;
	}
	// update t for later challenge computation
	t = t+enc_blocks;
	printf("\nIncremental encoding finishes...\n");
	free(prptable);
	for (i = 0; i < enc_blocks; i++){  
   	free(code[i]);  
	}  
	free(code); 
	return 0;
}