Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
	int ia[5] = {1,2,3,4,5};
	char hello[] = "Hello world";
	char seq[] = "ACGT";
        int word;
        char *str;

	checkInt("reverse_int(1)", 1, ia[0]);
	reverse_int(ia,5);
	checkInt("reverse_int(2)", 5, ia[0]);

	checkStr("reverse_seq(1)", "Hello world", hello);
	reverse_seq(hello);
	checkStr("reverse_seq(2)", "dlrow olleH", hello);

	word = str2word(seq, 4);
	checkInt("str2word(1)", 27, word);
	str = word2str(228, 4);
	checkStr("word2str(1)", "TGCA", str);

	return 0;
}
Ejemplo n.º 2
0
Archivo: op_.c Proyecto: kalngyk/learn
alphabet *
op_refine(alphabet * pat, alphabet * var, int op_num)
{
    return ((op_num < 1 || op_num > 3)? NULL:
        replace_char(pat, var, str2word(alphabets[op_num-1])));
}
Ejemplo n.º 3
0
/**
Process the buffer once full.
*/
static inline void perform_all_rounds(sha1_s* s, const uint8_t* buffer) {
  /* collect data */
  uint32_t a = s->digest.i[0];
  uint32_t b = s->digest.i[1];
  uint32_t c = s->digest.i[2];
  uint32_t d = s->digest.i[3];
  uint32_t e = s->digest.i[4];
  uint32_t t, w[16];
  /* copy data to words, performing byte swapping as needed */
  w[0] = str2word(buffer);
  w[1] = str2word(buffer + 4);
  w[2] = str2word(buffer + 8);
  w[3] = str2word(buffer + 12);
  w[4] = str2word(buffer + 16);
  w[5] = str2word(buffer + 20);
  w[6] = str2word(buffer + 24);
  w[7] = str2word(buffer + 28);
  w[8] = str2word(buffer + 32);
  w[9] = str2word(buffer + 36);
  w[10] = str2word(buffer + 40);
  w[11] = str2word(buffer + 44);
  w[12] = str2word(buffer + 48);
  w[13] = str2word(buffer + 52);
  w[14] = str2word(buffer + 56);
  w[15] = str2word(buffer + 60);
/* perform rounds */
#define perform_single_round(num)                                             \
  t = left_rotate32(a, 5) + e + w[num] + ((b & c) | ((~b) & d)) + 0x5A827999; \
  e = d;                                                                      \
  d = c;                                                                      \
  c = left_rotate32(b, 30);                                                   \
  b = a;                                                                      \
  a = t;

#define perform_four_rounds(i) \
  perform_single_round(i);     \
  perform_single_round(i + 1); \
  perform_single_round(i + 2); \
  perform_single_round(i + 3);

  perform_four_rounds(0);
  perform_four_rounds(4);
  perform_four_rounds(8);
  perform_four_rounds(12);

#undef perform_single_round
#define perform_single_round(i)                                      \
  w[(i)&15] = left_rotate32((w[(i - 3) & 15] ^ w[(i - 8) & 15] ^     \
                             w[(i - 14) & 15] ^ w[(i - 16) & 15]),   \
                            1);                                      \
  t = left_rotate32(a, 5) + e + w[(i)&15] + ((b & c) | ((~b) & d)) + \
      0x5A827999;                                                    \
  e = d;                                                             \
  d = c;                                                             \
  c = left_rotate32(b, 30);                                          \
  b = a;                                                             \
  a = t;

  perform_four_rounds(16);

#undef perform_single_round
#define perform_single_round(i)                                       \
  w[(i)&15] = left_rotate32((w[(i - 3) & 15] ^ w[(i - 8) & 15] ^      \
                             w[(i - 14) & 15] ^ w[(i - 16) & 15]),    \
                            1);                                       \
  t = left_rotate32(a, 5) + e + w[(i)&15] + (b ^ c ^ d) + 0x6ED9EBA1; \
  e = d;                                                              \
  d = c;                                                              \
  c = left_rotate32(b, 30);                                           \
  b = a;                                                              \
  a = t;

  perform_four_rounds(20);
  perform_four_rounds(24);
  perform_four_rounds(28);
  perform_four_rounds(32);
  perform_four_rounds(36);

#undef perform_single_round
#define perform_single_round(i)                                         \
  w[(i)&15] = left_rotate32((w[(i - 3) & 15] ^ w[(i - 8) & 15] ^        \
                             w[(i - 14) & 15] ^ w[(i - 16) & 15]),      \
                            1);                                         \
  t = left_rotate32(a, 5) + e + w[(i)&15] + ((b & (c | d)) | (c & d)) + \
      0x8F1BBCDC;                                                       \
  e = d;                                                                \
  d = c;                                                                \
  c = left_rotate32(b, 30);                                             \
  b = a;                                                                \
  a = t;

  perform_four_rounds(40);
  perform_four_rounds(44);
  perform_four_rounds(48);
  perform_four_rounds(52);
  perform_four_rounds(56);
#undef perform_single_round
#define perform_single_round(i)                                       \
  w[(i)&15] = left_rotate32((w[(i - 3) & 15] ^ w[(i - 8) & 15] ^      \
                             w[(i - 14) & 15] ^ w[(i - 16) & 15]),    \
                            1);                                       \
  t = left_rotate32(a, 5) + e + w[(i)&15] + (b ^ c ^ d) + 0xCA62C1D6; \
  e = d;                                                              \
  d = c;                                                              \
  c = left_rotate32(b, 30);                                           \
  b = a;                                                              \
  a = t;
  perform_four_rounds(60);
  perform_four_rounds(64);
  perform_four_rounds(68);
  perform_four_rounds(72);
  perform_four_rounds(76);

  /* store data */
  s->digest.i[4] += e;
  s->digest.i[3] += d;
  s->digest.i[2] += c;
  s->digest.i[1] += b;
  s->digest.i[0] += a;
}