VirtualBox

source: vbox/trunk/src/libs/libtpms-0.9.0/tests/base64decode.c@ 92405

Last change on this file since 92405 was 91612, checked in by vboxsync, 3 years ago

src/libs: Export libtpms-0.9.0, bugref:10078

File size: 1.8 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include <libtpms/tpm_types.h>
7#include <libtpms/tpm_library.h>
8#include <libtpms/tpm_error.h>
9
10static unsigned char *read_file(const char *name, size_t *len)
11{
12 long sz;
13 unsigned char *res;
14 FILE *f = fopen(name, "rb");
15
16 if (!f) {
17 printf("Could not open file %s for reading.", name);
18 exit(EXIT_FAILURE);
19 }
20
21 fseek(f, 0, SEEK_END);
22 sz = ftell(f);
23 fseek(f, 0, SEEK_SET);
24
25 res = malloc(sz + 1);
26 if (res != NULL) {
27 *len = fread(res, 1, sz, f);
28 res[sz] = 0;
29 }
30
31 fclose(f);
32
33 return res;
34}
35
36int main(int argc, char *argv[])
37{
38 int res = EXIT_SUCCESS;
39 TPM_RESULT rc;
40 unsigned char *buf_input = NULL, *buf_cmp = NULL;
41 size_t len_input, len_cmp;
42 unsigned char *result = NULL;
43 size_t result_len;
44
45 if (argc != 3) {
46 printf("Need 2 files as parameters.\n");
47 return EXIT_FAILURE;
48 }
49
50 buf_input = read_file(argv[1], &len_input);
51 buf_cmp = read_file(argv[2], &len_cmp);
52
53 rc = TPMLIB_DecodeBlob((char *)buf_input, TPMLIB_BLOB_TYPE_INITSTATE,
54 &result, &result_len);
55
56 if (rc != TPM_SUCCESS) {
57 printf("Decoding of the input file failed.\n");
58 res = EXIT_FAILURE;
59 goto cleanup;
60 }
61
62 if (result_len != len_cmp) {
63 printf("Length of decoded blob (%zu) does "
64 "not match length of 2nd file (%zu).\n",
65 result_len, len_cmp);
66 res = EXIT_FAILURE;
67 goto cleanup;
68 }
69
70 if (memcmp(result, buf_cmp, result_len) != 0) {
71 printf("Decoded blob does not match input from 2nd file.\n");
72 res = EXIT_FAILURE;
73 goto cleanup;
74 }
75
76cleanup:
77 free(result);
78 free(buf_cmp);
79 free(buf_input);
80
81 return res;
82}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette