util.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <string.h>
  2. #include <openssl/bio.h>
  3. #include <openssl/buffer.h>
  4. #include <openssl/evp.h>
  5. #include "util.h"
  6. /*
  7. * Base64 encodes input bytes and stores them in out
  8. */
  9. int base64_encode(uint8_t *in, size_t len, char **out){
  10. //b64 encode slitheen ID
  11. BUF_MEM *buffer_ptr;
  12. BIO *bio, *b64;
  13. b64 = BIO_new(BIO_f_base64());
  14. bio = BIO_new(BIO_s_mem());
  15. bio = BIO_push(b64, bio);
  16. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  17. BIO_write(bio, in, len);
  18. BIO_flush(bio);
  19. BIO_get_mem_ptr(bio, &buffer_ptr);
  20. BIO_set_close(bio, BIO_NOCLOSE);
  21. BIO_free_all(bio);
  22. *out = (*buffer_ptr).data;
  23. //*out[(*buffer_ptr).length] = '\0';
  24. return 1;
  25. }
  26. //malloc macro that exits on error
  27. void *emalloc(size_t size){
  28. void *ptr = malloc(size);
  29. if (ptr == NULL){
  30. fprintf(stderr, "Memory failure. Exiting...\n");
  31. exit(1);
  32. }
  33. return ptr;
  34. }
  35. //calloc macro that exits on error
  36. void *ecalloc(size_t nmemb, size_t size){
  37. void *ptr = calloc(nmemb, size);
  38. if(ptr == NULL){
  39. fprintf(stderr, "Memory failure. Exiting...\n");
  40. exit(1);
  41. }
  42. return ptr;
  43. }