1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later 2*49cdfc7eSAndroid Build Coastguard Worker /* 3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2021 SUSE LLC <[email protected]> 4*49cdfc7eSAndroid Build Coastguard Worker * 5*49cdfc7eSAndroid Build Coastguard Worker * Convert bytes from standard input to hexadecimal representation. 6*49cdfc7eSAndroid Build Coastguard Worker * 7*49cdfc7eSAndroid Build Coastguard Worker * Parameters: 8*49cdfc7eSAndroid Build Coastguard Worker * -d Convert hexadecimal values from standard input to binary representation 9*49cdfc7eSAndroid Build Coastguard Worker * instead. 10*49cdfc7eSAndroid Build Coastguard Worker */ 11*49cdfc7eSAndroid Build Coastguard Worker 12*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h> 13*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h> 14*49cdfc7eSAndroid Build Coastguard Worker decode_hex(void)15*49cdfc7eSAndroid Build Coastguard Workerint decode_hex(void) 16*49cdfc7eSAndroid Build Coastguard Worker { 17*49cdfc7eSAndroid Build Coastguard Worker int ret; 18*49cdfc7eSAndroid Build Coastguard Worker unsigned int val; 19*49cdfc7eSAndroid Build Coastguard Worker 20*49cdfc7eSAndroid Build Coastguard Worker while ((ret = scanf("%2x", &val)) == 1) 21*49cdfc7eSAndroid Build Coastguard Worker putchar(val); 22*49cdfc7eSAndroid Build Coastguard Worker 23*49cdfc7eSAndroid Build Coastguard Worker return ret != EOF || ferror(stdin); 24*49cdfc7eSAndroid Build Coastguard Worker } 25*49cdfc7eSAndroid Build Coastguard Worker encode_hex(void)26*49cdfc7eSAndroid Build Coastguard Workerint encode_hex(void) 27*49cdfc7eSAndroid Build Coastguard Worker { 28*49cdfc7eSAndroid Build Coastguard Worker int val; 29*49cdfc7eSAndroid Build Coastguard Worker 30*49cdfc7eSAndroid Build Coastguard Worker for (val = getchar(); val >= 0 && val <= 0xff; val = getchar()) 31*49cdfc7eSAndroid Build Coastguard Worker printf("%02x", val); 32*49cdfc7eSAndroid Build Coastguard Worker 33*49cdfc7eSAndroid Build Coastguard Worker return val != EOF || ferror(stdin); 34*49cdfc7eSAndroid Build Coastguard Worker } 35*49cdfc7eSAndroid Build Coastguard Worker main(int argc,char ** argv)36*49cdfc7eSAndroid Build Coastguard Workerint main(int argc, char **argv) 37*49cdfc7eSAndroid Build Coastguard Worker { 38*49cdfc7eSAndroid Build Coastguard Worker int ret, decode = 0; 39*49cdfc7eSAndroid Build Coastguard Worker 40*49cdfc7eSAndroid Build Coastguard Worker while ((ret = getopt(argc, argv, "d"))) { 41*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0) 42*49cdfc7eSAndroid Build Coastguard Worker break; 43*49cdfc7eSAndroid Build Coastguard Worker 44*49cdfc7eSAndroid Build Coastguard Worker switch (ret) { 45*49cdfc7eSAndroid Build Coastguard Worker case 'd': 46*49cdfc7eSAndroid Build Coastguard Worker decode = 1; 47*49cdfc7eSAndroid Build Coastguard Worker break; 48*49cdfc7eSAndroid Build Coastguard Worker } 49*49cdfc7eSAndroid Build Coastguard Worker } 50*49cdfc7eSAndroid Build Coastguard Worker 51*49cdfc7eSAndroid Build Coastguard Worker if (decode) 52*49cdfc7eSAndroid Build Coastguard Worker return decode_hex(); 53*49cdfc7eSAndroid Build Coastguard Worker else 54*49cdfc7eSAndroid Build Coastguard Worker return encode_hex(); 55*49cdfc7eSAndroid Build Coastguard Worker } 56