1 /* Copyright (C) 2007 The Written Word, Inc.
2  * Copyright (C) 2008, 2010 Simon Josefsson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  *   Redistributions of source code must retain the above
10  *   copyright notice, this list of conditions and the
11  *   following disclaimer.
12  *
13  *   Redistributions in binary form must reproduce the above
14  *   copyright notice, this list of conditions and the following
15  *   disclaimer in the documentation and/or other materials
16  *   provided with the distribution.
17  *
18  *   Neither the name of the copyright holder nor the names
19  *   of any other contributors may be used to endorse or
20  *   promote products derived from this software without
21  *   specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  */
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 
42 #include "libssh2.h"
43 
test_libssh2_base64_decode(LIBSSH2_SESSION * session)44 static int test_libssh2_base64_decode(LIBSSH2_SESSION *session)
45 {
46     char *data;
47     unsigned int datalen;
48     const char *src = "Zm5vcmQ=";
49     unsigned int src_len = strlen(src);
50     int ret;
51 
52     ret = libssh2_base64_decode(session, &data, &datalen,
53                                 src, src_len);
54     if(ret)
55         return ret;
56 
57     if(datalen != 5 || strcmp(data, "fnord") != 0) {
58         fprintf(stderr,
59                 "libssh2_base64_decode() failed (%d, %.*s)\n",
60                 datalen, datalen, data);
61         return 1;
62     }
63 
64     free(data);
65 
66     return 0;
67 }
68 
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71     LIBSSH2_SESSION *session;
72     int rc;
73     (void)argv;
74     (void)argc;
75 
76     rc = libssh2_init(LIBSSH2_INIT_NO_CRYPTO);
77     if(rc != 0) {
78         fprintf(stderr, "libssh2_init() failed: %d\n", rc);
79         return 1;
80     }
81 
82     session = libssh2_session_init();
83     if(!session) {
84         fprintf(stderr, "libssh2_session_init() failed\n");
85         return 1;
86     }
87 
88     test_libssh2_base64_decode(session);
89 
90     libssh2_session_free(session);
91 
92     libssh2_exit();
93 
94     return 0;
95 }
96