Example #1
0
bool xmr_base58_encode(char *b58, size_t *b58sz, const void *data, size_t binsz)
{
	if (binsz==0)
		return true;

	const char * data_bin = data;
	size_t full_block_count = binsz / full_block_size;
	size_t last_block_size = binsz % full_block_size;
	size_t res_size = full_block_count * full_encoded_block_size + encoded_block_sizes[last_block_size];

	if (b58sz){
		if (res_size >= *b58sz){
			return false;
		}
		*b58sz = res_size;
	}

	for (size_t i = 0; i < full_block_count; ++i)
	{
		encode_block(data_bin + i * full_block_size, full_block_size, b58 + i * full_encoded_block_size);
	}

	if (0 < last_block_size)
	{
		encode_block(data_bin + full_block_count * full_block_size, last_block_size, b58 + full_block_count * full_encoded_block_size);
	}

	return true;
}
Example #2
0
/**
 * \param j pointer to jpeg_enc_t structure
 *
 * This function huffman encodes one MCU, and emits the
 * resulting bitstream into the MJPEG code that is currently worked on.
 *
 * this function is a reproduction of the one in mjpeg, it includes two
 * changes, it allows for black&white encoding (it skips the U and V
 * macroblocks and it outputs the huffman code for 'no change' (dc) and
 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420)
 */
static av_always_inline void zr_mjpeg_encode_mb(jpeg_enc_t *j) {

	MJpegContext *m = j->s->mjpeg_ctx;

	encode_block(j->s, j->s->block[0], 0);
	encode_block(j->s, j->s->block[1], 1);
	if (j->bw) {
		/* U */
		put_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
				m->huff_code_dc_chrominance[0]);
		put_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
				m->huff_code_ac_chrominance[0]);
		/* V */
		put_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
				m->huff_code_dc_chrominance[0]);
		put_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
				m->huff_code_ac_chrominance[0]);
	} else {
		/* we trick encode_block here so that it uses
		 * chrominance huffman tables instead of luminance ones
		 * (see the effect of second argument of encode_block) */
		encode_block(j->s, j->s->block[2], 4);
		encode_block(j->s, j->s->block[3], 5);
	}
}
Example #3
0
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[6][64])
{
    int i;
    for(i=0;i<5;i++) {
        encode_block(s, block[i], i);
    }
    if (s->chroma_format == CHROMA_420) {
        encode_block(s, block[5], 5);
    } else {
        encode_block(s, block[6], 6);
        encode_block(s, block[5], 5);
        encode_block(s, block[7], 7);
    }

    s->i_tex_bits += get_bits_diff(s);
}
Example #4
0
std::string
base64_encode(const unsigned char *src,
              int src_len,
              bool line_breaks,
              int max_line_len) {
  int pos        = 0;
  int blocks_out = 0;

  if (4 > max_line_len)
    max_line_len = 4;

  int i, len_mod = max_line_len / 4;
  std::string out;

  while (pos < src_len) {
    unsigned char in[3];
    int len = 0;
    for (i = 0; 3 > i; ++i) {
      if (pos < src_len) {
        ++len;
        in[i] = (unsigned char)src[pos];
      } else
        in[i] = 0;
      ++pos;
    }

    encode_block(in, len, out);

    ++blocks_out;
    if (line_breaks && ((blocks_out % len_mod) == 0))
      out += "\n";
  }

  return out;
}
Example #5
0
void encode_update(ENCODE_CTX *ctx, const unsigned char *in, int inlen,
                   unsigned char *out, int *outlen)
{
     int i, j;
     unsigned int total=0;

     *outlen = 0;
     if (inlen == 0)
          return;
     assert(ctx->length <= (int)sizeof(ctx->data));
     if ((ctx->number + inlen) < ctx->length) {
          memcpy(&(ctx->data[ctx->number]), in, inlen);
          ctx->number += inlen;
          return;
     }
     if (ctx->number != 0) {
          i = ctx->length - ctx->number;
          memcpy(&(ctx->data[ctx->number]), in, i);
          in += i;
          inlen -= i;
          j = encode_block(out, ctx->data, ctx->length);
          ctx->number = 0;
          out += j;
          *(out++)='\n';
          *out='\0';
          total=j+1;
     }
     while (inlen >= ctx->length) {
          j = encode_block(out, in, ctx->length);
          in += ctx->length;
          inlen -= ctx->length;
          out += j;
          *(out++) = '\n';
          *out='\0';
          total += j+1;
     }
     if (inlen != 0)
          memcpy(&(ctx->data[0]), in, inlen);
     ctx->number = inlen;
     *outlen = total;
}
Example #6
0
    std::string encode(const std::string& data)
    {
      if (data.empty())
        return std::string();

      size_t full_block_count = data.size() / full_block_size;
      size_t last_block_size = data.size() % full_block_size;
      size_t res_size = full_block_count * full_encoded_block_size + encoded_block_sizes[last_block_size];

      std::string res(res_size, alphabet[0]);
      for (size_t i = 0; i < full_block_count; ++i)
      {
        encode_block(data.data() + i * full_block_size, full_block_size, &res[i * full_encoded_block_size]);
      }

      if (0 < last_block_size)
      {
        encode_block(data.data() + full_block_count * full_block_size, last_block_size, &res[full_block_count * full_encoded_block_size]);
      }

      return res;
    }
Example #7
0
static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
                        uint8_t *buf, int buf_size, int total_gain)
{
    init_put_bits(&s->pb, buf, buf_size);

    if (s->use_bit_reservoir)
        av_assert0(0); // FIXME not implemented
    else if (encode_block(s, src_coefs, total_gain) < 0)
        return INT_MAX;

    avpriv_align_put_bits(&s->pb);

    return put_bits_count(&s->pb) / 8 - s->avctx->block_align;
}
Example #8
0
/*
 * Encodes payload of SRTP packet AES128-CTR
 * 
 * @src - packet payload
 * @dst - encoded payload
 * @key - pointer to the keys array
 * @iv - initial vector for counter mode in packet for AES
 * @length - length of payload in bytes
 *
 * returns 0 for success or error code for failure
 */
void AES::srtp_encode(BYTE* src, BYTE* dst, BYTE* key, BYTE* iv, int length){
    LOG_MSG("AES::srtp_encode()");
    BYTE counter[BLOCK_SIZE];
    memcpy(counter, iv, BLOCK_SIZE);

    unsigned char round_key[ROUND_KEY_SIZE][BLOCK_SIZE];
    expand_key(key,round_key);
    
    int i = 0, j = 0;

    for( ; i < length; i+=BLOCK_SIZE){
        encode_block(counter, dst+i, round_key);
        xor_key(dst+i,dst+i,src+i);
        update_counter(counter);
    }

    BYTE last_block[BLOCK_SIZE];
    encode_block(counter, last_block, round_key);
    for(i=i-BLOCK_SIZE; i < length; i++, j++){
        dst[i] = last_block[j] ^ src[i];
    }

}
void test_block_decode()
{
    des_block_t key;
    key.c = 0x13345779;
    key.d = 0x9BBCDFF1;
    des_block_t* key_schedule = generate_key_schedule(key);

    des_block_t message_block;
    message_block.c = 0x01234567;
    message_block.d = 0x89ABCDEF;
    des_block_t decoded = encode_block(message_block, key_schedule, DECODE);
    assert(decoded.c == 0xEE0F7C12);
    assert(decoded.d == 0xE0B09338);

    free(key_schedule);
}
void test_block_encode()
{
    des_block_t key;
    key.c = 0x13345779;
    key.d = 0x9BBCDFF1;
    des_block_t* key_schedule = generate_key_schedule(key);

    des_block_t message_block;
    message_block.c = 0x01234567;
    message_block.d = 0x89ABCDEF;
    des_block_t encoded = encode_block(message_block, key_schedule, ENCODE);
    assert(encoded.c == 0x85E81354);
    assert(encoded.d == 0x0F0AB405);

    free(key_schedule);
}
Example #11
0
int
validate (const char *dst, const void *src, int srclen)
{
    int n;
    char buf[9];

    if (encode_block (buf, &n, src, srclen) < 0)
        return (-1);
    if (n != strlen (dst))
        return (-1);
    if (strncmp (dst, buf, n))
        return (-1);

    if (decode_block (buf, &n, dst, strlen (dst)) < 0)
        return (-1);
    if (n != srclen)
        return (-1);
    if (strncmp (src, buf, n))
        return (-1);

    if (encode_context (buf, &n, src, srclen) < 0)
        return (-1);
    if (n != strlen (dst))
        return (-1);
    if (strncmp (dst, buf, n))
        return (-1);

    if (decode_context (buf, &n, dst, strlen (dst)) < 0)
        return (-1);
    if (n != srclen)
        return (-1);
    if (strncmp (src, buf, n))
        return (-1);

    return (0);
}
Example #12
0
int
validate (const unsigned char *src, const unsigned char *dst)
{
    int n;
    unsigned char buf[9];

    if (encode_block (buf, &n, src, strlen (src)) < 0)
        return (-1);
    if (n != strlen (dst))
        return (-1);
    if (strncmp (dst, buf, n))
        return (-1);

    if (decode_block (buf, &n, dst, strlen (dst)) < 0)
        return (-1);
    if (n != strlen (src))
        return (-1);
    if (strncmp (src, buf, n))
        return (-1);

    if (encode_context (buf, &n, src, strlen (src)) < 0)
        return (-1);
    if (n != strlen (dst))
        return (-1);
    if (strncmp (dst, buf, n))
        return (-1);

    if (decode_context (buf, &n, dst, strlen (dst)) < 0)
        return (-1);
    if (n != strlen (src))
        return (-1);
    if (strncmp (src, buf, n))
        return (-1);

    return (0);
}
Example #13
0
void rm_test_0(ull r, ull m, int k, int max_iter, FILE *fout) {
	ull wordsize = 0, dim = 0, gen_size = 0;
	unsigned char *G = NULL, *D = NULL, *DD = NULL, *C = NULL, *NC = NULL;
	unsigned short *masks = NULL, width = 0;
	ull accumulator = 0, n = 0;

	ull data_block_size_bytes = 0;
	ull code_block_size_bytes = 0;

	dim = rm_dim(r, m);
	wordsize = rm_wordsize(r, m);
	width = rm_codeword_size_in_bytes(r, m);

	gen_size = gen_matrix_mem_size(m, m);
	G = (unsigned char *) malloc(gen_size);
	generate_matrix(m, m, G);
	
	//print(dim, 8 * width, G);printf("\n");

	masks = (unsigned short *) malloc(wordsize * sizeof(unsigned short));
	generate_masks(m, masks);

	data_block_size_bytes = rm_dataword_size_in_bytes(r, m);
	code_block_size_bytes = rm_codeword_size_in_bytes(r, m);

	D =	(unsigned char *) malloc(data_block_size_bytes);
	DD = (unsigned char *) malloc(data_block_size_bytes);

	for (int i = 0; i < data_block_size_bytes; ++i)
		D[i] = (unsigned char) rand();

	C = (unsigned char *) malloc(code_block_size_bytes);
	NC = (unsigned char *) malloc(code_block_size_bytes);

	encode_block(r, m, dim, wordsize, C, D, masks, G);
	//printf("Data:\n");print(1, dim, D);printf("\n");
	
	memcpy(NC, C, width);
	// print(1, wordsize, C);printf("\n");
	// decode_block(r, m, dim, wordsize, NC, DD, masks, G, width);
	// print(1, dim, DD);
	srand (time(NULL));
	
	n = 2 * k / 3 + 1;
	fprintf(fout, "%lld\n", n);
	for (ull i = 0; i < n; i++) {
		accumulator = 0;
		fprintf(fout, "%1.9lf ", (double) i / (double) k);

		for (ull j = 0; j < max_iter; ++j) {
			memcpy(NC, C, code_block_size_bytes);
	
			add_noise(NC, code_block_size_bytes, (double) i / (double) k);				
			decode_block(r, m, dim, wordsize, NC, DD, masks, G, width);
			accumulator += match(data_block_size_bytes, 0, dim, D, DD);
		}

		fprintf(fout, "%1.9lf\n", (double) accumulator / (double) max_iter);
	}
	FREE_IF_ALLOCATED(C);
	FREE_IF_ALLOCATED(NC);
	FREE_IF_ALLOCATED(D);
	FREE_IF_ALLOCATED(DD);
	FREE_IF_ALLOCATED(masks);
	FREE_IF_ALLOCATED(G);
}
Example #14
0
/*XXX*/
int BvcEncoder::consume(const VideoFrame* vf)
{
	if (!samesize(vf))
		size(vf->width_, vf->height_);
	YuvFrame* p = (YuvFrame*)vf;
	tx_->flush();
	int layer = p->layer_;

	u_char* frm = (u_char*)p->bp_;
	const u_char* chm = frm + framesize_;
	blkno_ = -1;
	pktbuf* pb = getpkt(p->ts_, layer);
	const u_int8_t* crv = p->crvec_;
	memset(&b, 0, sizeof(b));

#ifdef SBC_STAT
	if (f[0] == 0) {
		f[0] = fopen("sbc0", "w");
		f[1] = fopen("sbc1", "w");
		f[2] = fopen("sbc2", "w");
		f[3] = fopen("sbc3", "w");
	}
#endif
	int cc = 0;
	int blkw = width_ >> 4;
	int blkh = height_ >> 4;
	int blkno = 0;
	for (int y = 0; y < blkh; ++y) {
		for (int x = 0; x < blkw; ++blkno, frm += 16, chm += 8,
		     ++x, ++crv) {
			int s = crv[0];
			if ((s & CR_SEND) == 0)
				continue;

#define MAXMBSIZE (16*16+2*8*8+4)
			if (bs_ + MAXMBSIZE >= es_) {
				cc += flush(pb, 0);
				pb = getpkt(p->ts_, layer);
			}

			int dblk = blkno - blkno_;
			blkno_ = blkno;
			if (dblk <= 0)
				abort();

			if (dblk == 1) {
				PUT_BITS(1, 1, nbb_, bb_, bs_);
b.mba += 1;
			} else if (dblk <= 17) {
				PUT_BITS(0x10 | (dblk - 2), 6, nbb_, bb_, bs_);
b.mba += 6;
			} else {
				PUT_BITS(dblk, 13, nbb_, bb_, bs_);
b.mba += 13;
			}
			/*
			 * Deal with boundaries of image.  This is
			 * simply ugly, but it's the price we have
			 * to pay for using a four tap filter stage.
			 * For the 1-3-3-1 filter set, we use symmetric
			 * extension at both analysis and synthesis
			 * to give perfect reconstruction.
			 * If we're at the top of the image, we simply
			 * copy the top row up (since the grabber's
			 * reserve one line of extra space).  Otherwise,
			 * we save pixels, do the symmetrization,
			 * encode the block, then restore the pixels.
			 */
			u_char ysave[16];
			u_char xsave[16];
			if (y == 0)
				memcpy(frm - width_, frm, 16);
			else if (y == blkh - 1) {
				u_char* p = frm + (width_ << 4);
				memcpy(ysave, p, 16);
				memcpy(p, p - width_, 16);
			}
			if (x == 0) {
				u_char* s = xsave;
				u_char* p = frm;
				for (int k = 0; k < 16; ++k) {
					*s++ = p[-1];
					p[-1] = p[0];
					p += width_;
				}
			} else if (x == blkw - 1) {
				u_char* s = xsave;
				u_char* p = frm + 16;
				for (int k = 0; k < 16; ++k) {
					*s++ = p[0];
					p[0] = p[-1];
					p += width_;
				}
			}
			encode_block(frm);
			encode_color(chm);
			encode_color(chm + (framesize_ >> 2));
			/*
			 * Now restore the pixels.  We don't bother
			 * with the "y = 0" case because the affected
			 * memory will not be used.
			 */
			if (y == blkh - 1) {
				u_char* p = frm + (width_ << 4);
				memcpy(p, ysave, 16);
			}
			if (x == 0) {
				u_char* s = xsave;
				u_char* p = frm;
				for (int k = 0; k < 16; ++k) {
					p[-1] = *s++;
					p += width_;
				}
			} else if (x == blkw - 1) {
				u_char* s = xsave;
				u_char* p = frm + 16;
				for (int k = 0; k < 16; ++k) {
					p[0] = *s++;
					p += width_;
				}
			}
		}
		frm += 15 * width_;
		chm += 7 * (width_ >> 1);
	}
#ifdef notdef
	double t = b.dc + b.mba;
	for (int i = 0; i < 4; ++i)
		t += b.zt[i] + b.sbc[i] + b.sbz[i];
	printf("dc\t%.3f\n", b.dc / t);
	printf("mba\t%.3f\n", b.mba / t);
	for (i = 0; i < 4; ++i) {
		printf("sbc%d\t%.3f (%.3f)\n", i, (b.sbc[i] + b.sbz[i]) / t,
		       b.sbz[i] / double(b.sbc[i] + b.sbz[i]));
		printf("zt%d\t%.3f\n", i, b.zt[i] / t);
	}
#endif
	cc += flush(pb, 1);
	return (cc);
}
Example #15
0
int
generate_key (const char *header)
{
  PUBLIC_KEY pubkey;
  PRIVATE_KEY privkey;
  unsigned char line[1024];
  int i, err, len;
  unsigned char iv[8];
  byte digest[16], ID[16], tmpbyte;
  byte des3key[24];
  BUFFER *b1, *encrypted_key;
  FILE *privring, *privlock;
#ifdef USE_RSAREF
  R_DIGEST_CTX digest_context;
  DES3_CBC_CTX context;
  R_RSA_PROTO_KEY protokey;
#else
  B_ALGORITHM_OBJ digest_obj;
  B_ALGORITHM_OBJ des_obj;
  B_ALGORITHM_OBJ gen_obj;
  B_KEY_OBJ key_obj;
  A_RSA_KEY_GEN_PARAMS keypar;
  A_PKCS_RSA_PRIVATE_KEY *keyinfo;
  unsigned char pubexpt[] =
  {1, 0, 1};
#endif

#ifdef DEBUG
  printf ("Generating key:\n%s", header);
#endif
  /* Generate a 1024 bit key with pub exponent = 65537 */
#ifdef USE_RSAREF
  protokey.bits = MIX_RSA_MOD;
  protokey.useFermat4 = 1;
  err = R_GeneratePEMKeys (&pubkey, &privkey, &protokey, &random_obj);
#else
  B_CreateAlgorithmObject (&gen_obj);
  /* Generate a 1024 bit key with pub exponent = 65537 */
  keypar.modulusBits = 1024;
  keypar.publicExponent.data = pubexpt;
  keypar.publicExponent.len = sizeof (pubexpt);
  B_SetAlgorithmInfo (gen_obj, AI_RSAKeyGen, (POINTER) & keypar);
  B_GenerateInit (gen_obj, CHOOSER, NULL);
  B_CreateKeyObject (&pubkey);
  B_CreateKeyObject (&privkey);
  err = (B_GenerateKeypair (gen_obj, pubkey, privkey, random_obj, NULL));
#endif
  if (err != 0)
    {
      fprintf (errlog, "Key generation error.\n");
      return (-1);
    }

#ifdef DEBUG
  printf ("Done.\n");
#endif
  /* put private key in a buffer */
  b1 = new_buffer ();
#ifdef USE_RSAREF
  /* Convert privkey.bits to two bytes */
  i = privkey.bits;
#else
  B_GetKeyInfo ((POINTER *) & keyinfo, privkey, KI_PKCS_RSAPrivate);
  i = keyinfo->modulus.len * 8;
#endif
  tmpbyte = i & 0xFF;
  add_to_buffer (b1, &tmpbyte, 1);	/* low byte of bits */
  tmpbyte = (i / 256) & 0xFF;
  add_to_buffer (b1, &tmpbyte, 1);	/* high byte of bits */

#ifdef USE_RSAREF
  add_to_buffer (b1, privkey.modulus, MAX_RSA_MODULUS_LEN);
  add_to_buffer (b1, privkey.publicExponent, MAX_RSA_MODULUS_LEN);

  /* Make Key ID */
  R_DigestInit (&digest_context, DA_MD5);
  R_DigestUpdate (&digest_context, pubkey.modulus, MAX_RSA_MODULUS_LEN);
  R_DigestUpdate (&digest_context, pubkey.exponent, MAX_RSA_MODULUS_LEN);
  R_DigestFinal (&digest_context, ID, &i);

  add_to_buffer (b1, privkey.exponent, MAX_RSA_MODULUS_LEN);
  add_to_buffer (b1, privkey.prime[0], MAX_RSA_PRIME_LEN);
  add_to_buffer (b1, privkey.prime[1], MAX_RSA_PRIME_LEN);
  add_to_buffer (b1, privkey.primeExponent[0], MAX_RSA_PRIME_LEN);
  add_to_buffer (b1, privkey.primeExponent[1], MAX_RSA_PRIME_LEN);
  add_to_buffer (b1, privkey.coefficient, MAX_RSA_PRIME_LEN);
#else
  i = (i + 7) / 8;

  /* Make Key ID */
  B_CreateAlgorithmObject (&digest_obj);
  B_SetAlgorithmInfo (digest_obj, AI_MD5, NULL);
  B_DigestInit (digest_obj, NULL, CHOOSER, NULL);

  add_to_buffer (b1, keyinfo->modulus.data, i);

  if (keyinfo->publicExponent.len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->publicExponent.len);
  add_to_buffer (b1, keyinfo->publicExponent.data,
		 keyinfo->publicExponent.len);

  B_DigestUpdate (digest_obj, b1->message + 2,
		  2 * i,
		  NULL);
  B_DigestFinal (digest_obj, ID, &i, 16, NULL);
  B_DestroyAlgorithmObject (&digest_obj);

  if (keyinfo->privateExponent.len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->privateExponent.len);
  add_to_buffer (b1, keyinfo->privateExponent.data,
		 keyinfo->privateExponent.len);

  i = (i + 1) / 2;

  if (keyinfo->prime[0].len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->prime[0].len);
  add_to_buffer (b1, keyinfo->prime[0].data, keyinfo->prime[0].len);

  if (keyinfo->prime[1].len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->prime[1].len);
  add_to_buffer (b1, keyinfo->prime[1].data, keyinfo->prime[1].len);

  if (keyinfo->primeExponent[0].len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->primeExponent[0].len);
  add_to_buffer (b1, keyinfo->primeExponent[0].data,
		 keyinfo->primeExponent[0].len);

  if (keyinfo->primeExponent[1].len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->primeExponent[1].len);
  add_to_buffer (b1, keyinfo->primeExponent[1].data,
		 keyinfo->primeExponent[1].len);

  if (keyinfo->coefficient.len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->coefficient.len);
  add_to_buffer (b1, keyinfo->coefficient.data, keyinfo->coefficient.len);
#endif

  /* Encrypt the secret key */
  encrypted_key = new_buffer ();
  len = b1->length;
  if (len % 8 != 0)		/* ensure length is mult of 8 */
    len += 8 - len % 8;
  add_to_buffer (encrypted_key, malloc (len), len);
  our_randombytes (iv, 8);
#ifdef USE_RSAREF
  R_DigestInit (&digest_context, DA_MD5);
  R_DigestUpdate (&digest_context, PASSPHRASE, strlen (PASSPHRASE));
  R_DigestFinal (&digest_context, digest, &i);
  memcpy (des3key, digest, 16);	/* set first 2 keys */
  memcpy (des3key + 16, digest, 8);	/* third key = first key */
  DES3_CBCInit (&context, des3key, iv, 1);
  if (DES3_CBCUpdate (&context, encrypted_key->message, b1->message,
		      encrypted_key->length))
    {
      printf ("Error: Problem encrypting key\n");
      return (-1);
    }
#else
  B_CreateAlgorithmObject (&digest_obj);
  B_SetAlgorithmInfo (digest_obj, AI_MD5, NULL);

  B_DigestInit (digest_obj, NULL, CHOOSER, NULL);
  B_DigestUpdate (digest_obj, PASSPHRASE, strlen (PASSPHRASE), NULL);
  B_DigestFinal (digest_obj, digest, &i, 16, NULL);
  B_DestroyAlgorithmObject (&digest_obj);

  memcpy (des3key, digest, 16);	/* set first 2 keys */
  memcpy (des3key + 16, digest, 8);	/* third key = first key */
  B_CreateAlgorithmObject (&des_obj);
  B_SetAlgorithmInfo (des_obj, AI_DES_EDE3_CBC_IV8, iv);
  B_CreateKeyObject (&key_obj);
  B_SetKeyInfo (key_obj, KI_DES24Strong, des3key);
  B_EncryptInit (des_obj, key_obj, CHOOSER, NULL);
  B_EncryptUpdate (des_obj, encrypted_key->message, &len,
		   encrypted_key->length,
		   b1->message, b1->length, random_obj, NULL);
  B_EncryptFinal (des_obj, encrypted_key->message + len,
		  &len, encrypted_key->length - len, random_obj, NULL);
  /* err? XXX */
#endif
  memset ((void *) digest, 0, 16);	/* zero password */

  mix_lock ("secring", &privlock);
  if ((privring = open_mix_file (SECRING, "a+")) == NULL)
    {
      mix_unlock ("secring", privlock);
      return (-1);
    }
  fprintf (privring, "%s\n", begin_key);
  if (strlen (header) > 0)
    {
      fprintf (privring, KEY_VERSION "%s\n", VERSION);
      fprintf (privring, "%s", header);
    }
  print_ID (privring, ID);
  fprintf (privring, "%d\n", len);
  encode_block (line, &i, iv, 8);
  fwrite (line, 1, i, privring);
  fprintf (privring, "\n");

  /* Armor privkey */
  armor (encrypted_key);
  write_buffer (encrypted_key, privring);
  free_buffer (encrypted_key);
  fprintf (privring, "%s\n", end_key);
  fclose (privring);
  mix_unlock ("secring", privlock);
  return 0;
}
Example #16
0
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
{
    int i;
    if (s->chroma_format == CHROMA_444) {
        encode_block(s, block[0], 0);
        encode_block(s, block[2], 2);
        encode_block(s, block[4], 4);
        encode_block(s, block[8], 8);
        encode_block(s, block[5], 5);
        encode_block(s, block[9], 9);

        if (16*s->mb_x+8 < s->width) {
            encode_block(s, block[1], 1);
            encode_block(s, block[3], 3);
            encode_block(s, block[6], 6);
            encode_block(s, block[10], 10);
            encode_block(s, block[7], 7);
            encode_block(s, block[11], 11);
        }
    } else {
        for(i=0;i<5;i++) {
            encode_block(s, block[i], i);
        }
        if (s->chroma_format == CHROMA_420) {
            encode_block(s, block[5], 5);
        } else {
            encode_block(s, block[6], 6);
            encode_block(s, block[5], 5);
            encode_block(s, block[7], 7);
        }
    }

    s->i_tex_bits += get_bits_diff(s);
}
Example #17
0
int encode_file(char *filepath) {
#ifdef DEBUG
    printf("processing %s\n", filepath);
#endif  // DEBUG

    if (!file_exists(filepath)) {
        fprintf(stderr, "Failed to decode file %s\n, file doesn't exitsts",
                filepath);
        return -1;
    }

    /**
     * Check if the file to be encoded is empty, if so, we don't need
     * to process it.
     */
    struct stat filestat;
    if (-1 == stat(filepath, &filestat)) {
        perror(NULL);
        return -1;
    }
    if (0 == filestat.st_size) return 0;

    /**
     * If the file is not empty, we encode it and add a newline sign
     * when finish encoding the file
     */
    char *input_path = clone_file(filepath);
    char *output_path = filepath;

    if (!file_exists(input_path) || !file_exists(output_path)) {
        fprintf(stderr, "Failed to make a clone of file %s\n", filepath);
        return -1;
    }

    int input_fd = open(input_path, O_RDONLY);
    int output_fd = open(output_path, O_WRONLY | O_TRUNC);

    if (-1 == input_fd || -1 == output_fd) {
        perror(NULL);
        return -1;
    }

    uint8_t read_buf[kNumRead], write_buf[kNumWrite];
    int num_read, num_write;

    while (1) {
        memset(read_buf, 0, kNumRead);
        memset(write_buf, 0, kNumWrite);
        num_read = read(input_fd, read_buf, kNumRead);

        if (0 == num_read) break;  // finish reading the file

        if (-1 == num_read) {
            fprintf(stderr, "Failed to read bytes from %s\n", input_path);
            return -1;
        }

        num_write = encode_block(read_buf, write_buf, num_read);
        if (num_write != write(output_fd, write_buf, num_write)) {
            fprintf(stderr, "Failed to write %d bytes into %s\n", num_write,
                    output_path);
            return -1;
        }
    }

    /**
     * write a newline sign to the output_fd
     */
    write_buf[0] = 0x0a;
    if (1 != write(output_fd, write_buf, 1)) {
        fprintf(stderr, "Failed to write the newline sign to %s\n",
                output_path);
        return -1;
    }

    /**
     * close everything, remove the file_clone
     */
    close(input_fd);
    close(output_fd);
    if (-1 == unlink(input_path)) {
        fprintf(stderr, "Failed to remove file %s\n", input_path);
        return -1;
    }
    return 0;
}