Commit cc6bb2ce authored by Matthieu Dien's avatar Matthieu Dien
Browse files

add .c and exemple_collision.c

parent 1657c766
Loading
Loading
Loading
Loading

src/bst.c

0 → 100644
+33 −0
Original line number Diff line number Diff line
struct node_t {
  unsigned long int key;
  void* val;
  struct node_t* left;
  struct node_t* right;
};

typedef struct bst_t {
  // Use this to compute a key from a val
  // It is not really a hash function, the key must be unique
  unsigned long int (*h)(void*);

  unsigned long int size;
  struct node_t* tree;
} bst_t;

bst_t bst_empty(void){
}

int bst_is_empty(bst_t t){
}

bst_t bst_add(bst_t t, void* x){
}

void bst_destroy(bst_t t, void (*free_void)(void*)){
}

int bst_in(bst_t t, void* x){
}

unsigned long int bst_size(bst_t t){
}

src/hash_tbl.c

0 → 100644
+30 −0
Original line number Diff line number Diff line
#include <stddef.h>
#include "list.h"

#define ARRAY_CAPACITY 1 << 16

struct hash_tbl {
  size_t (*h)(void*);
  int (*eq)(void*, void*);
  void (*free)(void*);
  unsigned long int size;
  size_t capacity; // array length
  list_t* array;
};

typedef struct hash_tbl* hash_tbl;

hash_tbl htbl_empty(size_t (*h)(void*),
                    int (*eq)(void*, void*),
                    void (*free)(void*)){
}

int htbl_add(hash_tbl htbl, void* x){
}

int htbl_in(hash_tbl htbl, void* x){
}

void htbl_destroy(hash_tbl htbl){
}
  

src/list.c

0 → 100644
+36 −0
Original line number Diff line number Diff line
struct cell_t {
  void* val;
  unsigned long int id;
  struct cell_t* next;
};

typedef struct cell_t* list_t;

list_t list_empty(){
}

int list_is_empty(list_t l){
}

list_t list_push(list_t l, void* x){
}

list_t list_tail(list_t l){
}

void* list_pop(list_t* l){ 
}

void* list_top(list_t l){
}

void list_destroy(list_t l, void (*free)(void*)){
}

// return the found element or NULL
void* list_in(list_t l, void* x, int (*eq)(void*, void*)){
}

unsigned long int list_len(list_t l){
}
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ list_t list_tail(list_t);
void* list_pop(list_t*);
void* list_top(list_t);
void list_destroy(list_t, void (*free)(void*));
int list_in(list_t, void*, int (*eq)(void*, void*));
void* list_in(list_t, void*, int (*eq)(void*, void*));
unsigned long int list_len(list_t);

#endif
+50 −0
Original line number Diff line number Diff line
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <openssl/md5.h>
#include "list.h"

typedef struct pair_t {
  char x[8];
  unsigned char h;
}* pair_t;

int eq_pair(void* a, void* b){
  return ((pair_t)a)->h == ((pair_t)b)->h;
}

/* char x[8] */
unsigned char md5_first_byte(const char* x){
  unsigned char digest[MD5_DIGEST_LENGTH];
  
  MD5((const unsigned char*)x, 8, digest);
  return digest[0]; // we keep only the first byte
}

int main(){
  pair_t p, q;
  list_t l;
  unsigned int i = 0;

  srand(time(NULL));

  l = list_empty();
  
  for(;;){
    p = (pair_t)malloc(sizeof(struct pair_t));
    for(int i = 0; i < 7; i++)
      p->x[i] = rand() % (126-32-1) + 32;
    p->x[7] = 0;
    p->h = md5_first_byte(p->x);

    if((q = (pair_t)list_in(l, p, eq_pair)) != NULL){
      printf("After %d iterations, a collision happened !\n", i);
      printf("%s and %s have the same MD5 first byte.\n", p->x, q->x);

      return 0;
    } else {
      l = list_push(l, p);
    }
  }
}