Piotr Nowicki | 9370f90 | 2020-03-13 14:43:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * MbedTLS SSL context deserializer from base64 code |
| 3 | * |
| 4 | * Copyright (C) 2006-2020, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
Piotr Nowicki | 88ebbbf | 2020-03-13 16:26:08 +0100 | [diff] [blame^] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | /* |
| 28 | * This program version |
| 29 | */ |
| 30 | #define PROG_NAME "ssl_base64_dump" |
| 31 | #define VER_MAJOR 0 |
| 32 | #define VER_MINOR 1 |
| 33 | |
| 34 | /* |
| 35 | * Global values |
| 36 | */ |
| 37 | FILE *b64_file = NULL; /* file with base64 codes to deserialize */ |
| 38 | char debug = 0; /* flag for debug messages */ |
| 39 | |
| 40 | /* |
| 41 | * Basic printing functions |
| 42 | */ |
| 43 | void print_version( ) |
| 44 | { |
| 45 | printf( "%s v%d.%d\n", PROG_NAME, VER_MAJOR, VER_MINOR ); |
| 46 | } |
| 47 | |
| 48 | void print_usage( ) |
| 49 | { |
| 50 | print_version(); |
| 51 | printf( |
| 52 | "Usage:\n" |
| 53 | "\t-f path - Path to the file with base64 code\n" |
| 54 | "\t-v - Show version\n" |
| 55 | "\t-h - Show this usage\n" |
| 56 | "\t-d - Print more information\n" |
| 57 | "\n" |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | void printf_dbg( const char *str, ... ) |
| 62 | { |
| 63 | if( debug ) |
| 64 | { |
| 65 | va_list args; |
| 66 | va_start( args, str ); |
| 67 | printf( "debug: " ); |
| 68 | vprintf( str, args ); |
| 69 | fflush( stdout ); |
| 70 | va_end( args ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void printf_err( const char *str, ... ) |
| 75 | { |
| 76 | va_list args; |
| 77 | va_start( args, str ); |
| 78 | fprintf( stderr, "ERROR: " ); |
| 79 | vfprintf( stderr, str, args ); |
| 80 | fflush( stderr ); |
| 81 | va_end( args ); |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Exit from the program in case of error |
| 86 | */ |
| 87 | void error_exit() |
| 88 | { |
| 89 | if( NULL != b64_file ) |
| 90 | { |
| 91 | fclose( b64_file ); |
| 92 | } |
| 93 | exit( -1 ); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * This function takes the input arguments of this program |
| 98 | */ |
| 99 | void parse_arguments( int argc, char *argv[] ) |
| 100 | { |
| 101 | int i = 1; |
| 102 | |
| 103 | if( argc < 2 ) |
| 104 | { |
| 105 | print_usage(); |
| 106 | error_exit(); |
| 107 | } |
| 108 | |
| 109 | while( i < argc ) |
| 110 | { |
| 111 | if( strcmp( argv[i], "-d" ) == 0 ) |
| 112 | { |
| 113 | debug = 1; |
| 114 | } |
| 115 | else if( strcmp( argv[i], "-h" ) == 0 ) |
| 116 | { |
| 117 | print_usage(); |
| 118 | } |
| 119 | else if( strcmp( argv[i], "-v" ) == 0 ) |
| 120 | { |
| 121 | print_version(); |
| 122 | } |
| 123 | else if( strcmp( argv[i], "-f" ) == 0 ) |
| 124 | { |
| 125 | if( ++i >= argc ) |
| 126 | { |
| 127 | printf_err( "File path is empty\n" ); |
| 128 | error_exit(); |
| 129 | } |
| 130 | |
| 131 | if( ( b64_file = fopen( argv[i], "r" ) ) == NULL ) |
| 132 | { |
| 133 | printf_err( "Cannot find file \"%s\"\n", argv[i] ); |
| 134 | error_exit(); |
| 135 | } |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | print_usage(); |
| 140 | error_exit(); |
| 141 | } |
| 142 | |
| 143 | i++; |
| 144 | } |
| 145 | } |
| 146 | |
Piotr Nowicki | 9370f90 | 2020-03-13 14:43:22 +0100 | [diff] [blame] | 147 | int main( int argc, char *argv[] ) |
| 148 | { |
Piotr Nowicki | 88ebbbf | 2020-03-13 16:26:08 +0100 | [diff] [blame^] | 149 | parse_arguments( argc, argv ); |
Piotr Nowicki | 9370f90 | 2020-03-13 14:43:22 +0100 | [diff] [blame] | 150 | |
| 151 | return 0; |
| 152 | } |