BRL-CAD
bu_b64.c
Go to the documentation of this file.
1 /* B U _ B 6 4 . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "bu.h"
27 
28 
29 /* Test for bu_b64_encode/bu_b64_decode for reversibility:
30  *
31  * 1. encode the input string
32  * 2. decode the encoded string
33  * 3. decoded string should be the same as input
34  *
35  */
36 static void
37 test_encode(const signed char *str)
38 {
39  int passed = 1;
40  int decoded_size = 0;
41  signed char *encoded = NULL;
42  signed char *decoded = NULL;
43 
44  encoded = bu_b64_encode(str);
45  decoded_size = bu_b64_decode(&decoded, encoded);
46 
47  if (BU_STR_EQUAL((const char *)str, (const char *)decoded) && decoded_size == (int)strlen((const char *)str)) {
48  printf("%s -> %s -> %s [PASS]\n", str, encoded, decoded);
49  } else {
50  printf("%s -> %s -> %s [FAIL]\n", str, encoded, decoded);
51  passed = 0;
52  }
53  bu_free(encoded, "free encoded");
54  bu_free(decoded, "free decoded");
55 
56  if (!passed) {
57  bu_exit(1, "");
58  }
59 }
60 
61 
62 int
63 main(int ac, char *av[])
64 {
65  if (ac != 1)
66  fprintf(stderr,"Usage: %s \n", av[0]);
67 
68  test_encode((const signed char *)"hello world!");
69  test_encode((const signed char *)"!@#&#$%@&#$^@(*&^%(#$@&^#*$nasty_string!<>?");
70 
71  return 0;
72 }
73 
74 
75 /*
76  * Local Variables:
77  * mode: C
78  * tab-width: 8
79  * indent-tabs-mode: t
80  * c-file-style: "stroustrup"
81  * End:
82  * ex: shiftwidth=4 tabstop=8
83  */
int bu_b64_decode(signed char **output_buffer, const signed char *input)
Definition: b64.c:274
Header file for the BRL-CAD common definitions.
int main(int ac, char *av[])
Definition: bu_b64.c:63
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
signed char * bu_b64_encode(const signed char *input)
Definition: b64.c:240
int test_encode(const char *str)
Definition: bu_encode.c:37
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126