cryptopals_c

cryptopals crypto challenges solutions in pure c
git clone git://git.superpozycja.net/cryptopals_c
Log | Files | Refs | README

ba.c (2922B)


      1 #include "ba.h"
      2 
      3 ba* ba_from_hex(char* hex) 
      4 {
      5 	uint8_t* b_val;
      6 	size_t b_len;
      7 	ba* b;
      8 	int i;
      9 
     10 	b_len = strlen(hex);
     11 	if (b_len%2 != 0) {
     12 		fprintf(stderr, "can't create ba from string %.10s", hex);
     13 		if (b_len > 10)
     14 			fprintf(stderr, "...");
     15 		fprintf(stderr, " - odd number of hex digits\n");
     16 		return NULL;
     17 	}
     18 	b_len /= 2;
     19 
     20 	b_val = (uint8_t *) malloc(sizeof(uint8_t) * b_len);
     21 	b = (ba *) malloc(sizeof(ba));
     22 
     23 	if (b_val == NULL || b == NULL) {
     24 		fprintf(stderr, "can't create ba from string %.10s", hex);
     25 		if(b_len > 10)
     26 			fprintf(stderr, "...");
     27 		fprintf(stderr, " - malloc() error\n");
     28 	}
     29 
     30 	for (i = 0; i < b_len; i++) {
     31 		char tmp[3];
     32 		strncpy(tmp, hex + 2 * i, 2);
     33 		tmp[2] = '\0';
     34 		b_val[i] = strtol(tmp, NULL, 16);
     35 	}
     36 
     37 	b->len = b_len;
     38 	b->val = b_val;
     39 	return b;
     40 }
     41 
     42 ba* ba_from_string(char *s)
     43 {
     44 	uint8_t *b_val;
     45 	size_t b_len;
     46 	ba *b;
     47 	int i;
     48 
     49 	b_len = strlen(s);
     50 	b_val = (uint8_t *) malloc(sizeof(uint8_t) * b_len);
     51 
     52 	for (i = 0; s[i] != '\0'; i++)
     53 		b_val[i] = s[i];
     54 
     55 	b = (ba *) malloc(sizeof(ba));
     56 	b->len = b_len;
     57 	b->val = b_val;
     58 	return b;
     59 }
     60 
     61 ba* ba_from_hex_n(char* hex, unsigned int n) 
     62 {
     63 	uint8_t* b_val;
     64 	size_t offset;
     65 	size_t b_len;
     66 	ba* b;
     67 	int o;
     68 
     69 	b_len = strlen(hex) * n;
     70 	offset = strlen(hex);
     71 	if (b_len%2 != 0) {
     72 		fprintf(stderr, "can't create ba from string %.10s", hex);
     73 		if (b_len > 10)
     74 			fprintf(stderr, "...");
     75 		fprintf(stderr, " - odd number of hex digits\n");
     76 		return NULL;
     77 	}
     78 	b_len /= 2;
     79 
     80 	b_val = (uint8_t*) malloc(sizeof(uint8_t) * b_len);
     81 	b = (ba*) malloc(sizeof(ba));
     82 
     83 	if (b_val == NULL || b == NULL) {
     84 		fprintf(stderr, "can't create ba from string %.10s", hex);
     85 		if(b_len > 10)
     86 			fprintf(stderr, "...");
     87 		fprintf(stderr, " - malloc() error\n");
     88 	}
     89 
     90 	for (o = 0; o < b_len * 2; o += offset) {
     91 		for (int i = 0; i < offset; i++) {
     92 			char tmp[3];
     93 			strncpy(tmp, hex + 2 * i, 2);
     94 			tmp[2] = '\0';
     95 			b_val[i+o/2] = strtol(tmp, NULL, 16);
     96 		}
     97 	}
     98 
     99 	b->len = b_len;
    100 	b->val = b_val;
    101 	return b;
    102 }
    103 
    104 void ba_fprint(ba* b, FILE* stream, int prec) 
    105 {
    106 	int len;
    107 	int i;	
    108 
    109 	len = (prec > b->len) || prec == 0 ? b->len : prec;
    110 	for (i = 0; i < len; i++)
    111 		fprintf(stream, "%02x", (b->val)[i]);
    112 }
    113 
    114 void ba_fprint_ascii(ba* b, FILE* stream, int prec) 
    115 {
    116 	int len;
    117 	int i;
    118 
    119 	len = (prec > b->len) || prec == 0 ? b->len : prec;
    120 	for (i = 0; i < len; i++)
    121 		if (isprint((b->val)[i])) 
    122 			fprintf(stream, "%c", (b->val)[i]);
    123 		else if (b->val[i] == 0x0a)
    124 			fprintf(stream, "\n");
    125 		else
    126 			fprintf(stream, "\\b%02x", (b->val)[i]);
    127 }
    128 
    129 void ba_xor(ba* a, ba* b) 
    130 {
    131 	int len;
    132 	int i;
    133 
    134 	len = a->len < b->len ? a->len : b->len;
    135 	for (i = 0; i < len; i++)
    136 		(a->val)[i] = (a->val)[i] ^ (b->val)[i];
    137 }
    138 
    139 int ba_copy(ba *dst, ba *src)
    140 {
    141 	if (!dst)
    142 		return -1;
    143 	if (dst->val)
    144 		free(dst->val);
    145 	dst->len = src->len;
    146 	dst->val = (uint8_t *) malloc(sizeof(uint8_t) * src->len);
    147 	memcpy(dst->val, src->val, src->len);
    148 	return 0;
    149 
    150 }
    151 
    152 void ba_free(ba* b) 
    153 {
    154 	free(b->val);
    155 	free(b);
    156 }
    157