/** * A simple GNUTLS NOP program that contains helper functions * to be used in the subsequent examples. It also calls GNUTLS * initialization function. That way it is possible that it is * possible to compile and execute programs that use GNUTLS * library. * * Written by Stjepan Gros . Use this * code as it suites you. */ #include #include #include #include /** * Load a given file into memory buffer * * @param file File name and path to load * * \return Pointer to buffer, or NULL if any error occured */ gnutls_datum_t *load_file(const char *file) { FILE *f; size_t filelen, size; void *ptr = NULL; gnutls_datum_t *loaded_file = NULL; if ((f = fopen(file, "r")) != NULL) { if (fseek(f, 0, SEEK_END) != 0) { perror("fseek"); goto out; } if ((filelen = ftell(f)) < 0) { perror("ftell"); goto out; } if (fseek(f, 0, SEEK_SET) != 0) { perror("fseek"); goto out; } ptr = g_malloc(filelen); if ((size = fread(ptr, 1, filelen, f)) < filelen) { if (size < 0) perror("fread"); goto out; } loaded_file = g_malloc0(sizeof(gnutls_datum_t)); loaded_file->data = ptr; loaded_file->size = filelen; out: fclose(f); } else perror ("fopen"); return loaded_file; } /** * Free GNUTLS specific memory buffer * * @param buffer Buffer to be free'd */ void unload_file(gnutls_datum_t *buffer) { g_free(buffer->data); g_free(buffer); } /** * Given a buffer and it's size dump hex represetation to stdout * * @param buf Pointer to a buffer * @param size Size of the buffer in bytes * * This function prints 16 bytes per row of the buffer */ void dump_hex(unsigned char *buf, size_t size) { int i, cols; unsigned int ch; for (i = 0, cols=0; i < size; i++, cols++) { if (cols == 16) { printf("\n"); cols = 0; } ch = (unsigned int)((unsigned char)buf[i] & 0xF0) >> 4; if (ch < 10) ch += '0'; else ch += 'A' - 10; printf("%c", ch); ch = (unsigned char)buf[i] & 0x0F; if (ch < 10) ch += '0'; else ch += 'A' - 10; printf("%c ", ch); } printf ("\n"); } int main(int argc, char **argv) { gnutls_global_init(); return 0; }