hash_t *hash_create(const size_t max, unsigned int (*calc)(const void *, const size_t size), int (*cmp)(const void *, const void *)) { hash_t *h; size_t bits = calc_bits(max); size_t size; bits = (bits < HASH_MIN_BITS)? HASH_MIN_BITS:((bits > HASH_MAX_BITS)? HASH_MAX_BITS:bits); size = primes[bits - HASH_MIN_BITS]; h = (hash_t*)malloc(sizeof(*h) + size * sizeof(list_t)); if (h) { h->entries = (hash_entry_t*)(malloc( max * sizeof(hash_entry_t))); if (!(h->entries)) { free(h); h = NULL; } else { init_list(&(h->spares), h->entries, max); h->calc = calc; h->cmp = cmp; h->size = size; h->num = 0; h->max = max; init_tbl(h->tbl, size); } } return h; }
static gboolean parse_key_data (gchar *line, SeahorseSSHKeyData *data) { gchar* space; guchar *bytes; gboolean ret; gsize len; /* Get the type */ space = strchr (line, ' '); if (space == NULL) return FALSE; *space = '\0'; data->algo = parse_algo (line); *space = ' '; if (data->algo == SSH_ALGO_UNK) return FALSE; line = space + 1; if (!*line) return FALSE; /* Prepare for decoding */ g_strchug (line); space = strchr (line, ' '); if (space) *space = 0; g_strchomp (line); /* Decode it, and parse binary stuff */ bytes = g_base64_decode (line, &len); ret = parse_key_blob (bytes, len, data); g_free (bytes); if (!ret) return FALSE; /* The number of bits */ data->length = calc_bits (data->algo, len); /* And the rest is the comment */ if (space) { *space = ' '; ++space; /* If not utf8 valid, assume latin 1 */ if (!g_utf8_validate (space, -1, NULL)) data->comment = g_convert (space, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL); else data->comment = g_strdup (space); } return TRUE; }
struct hashmap *hashmap_new(size_t capacity, hash_equal_fn_t equal, hash_fn_t hash) { struct hashmap *hashmap; unsigned int mask_bits = calc_bits(capacity); size_t real_capacity = HASH_SIZE(mask_bits); hashmap = calloc(1, sizeof(struct hashmap) + real_capacity * sizeof(struct list *)); if (!hashmap) { abort(); return NULL; } hashmap->mask_bits = mask_bits; hashmap->hash = hash; hashmap->equal = equal; return hashmap; }