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