From 1a34dbe550d987793b3e380ecce8f5f9489b4c09 Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Wed, 3 Feb 2010 20:37:29 +0000 Subject: [PATCH] Broke out the base64 commands from the text module --- apps/shell/Makefile.shell | 2 +- apps/shell/shell-base64.c | 152 +++++++++++++++++++++++++ apps/shell/shell-base64.h | 48 ++++++++ apps/shell/shell-text.c | 6 +- apps/shell/shell-text.h | 4 +- apps/shell/shell.h | 3 +- examples/example-shell/example-shell.c | 3 +- examples/sky-shell/sky-shell.c | 9 +- 8 files changed, 215 insertions(+), 12 deletions(-) create mode 100644 apps/shell/shell-base64.c create mode 100644 apps/shell/shell-base64.h diff --git a/apps/shell/Makefile.shell b/apps/shell/Makefile.shell index 09c89dce7..a90b9c948 100644 --- a/apps/shell/Makefile.shell +++ b/apps/shell/Makefile.shell @@ -9,7 +9,7 @@ shell_src = shell.c shell-reboot.c \ shell-tcpsend.c shell-udpsend.c shell-ping.c shell-netstat.c \ shell-rime-sendcmd.c shell-download.c shell-rime-neighbors.c \ shell-rime-unicast.c \ - shell-tweet.c \ + shell-tweet.c shell-base64.c \ shell-netperf.c shell-memdebug.c shell_dsc = shell-dsc.c diff --git a/apps/shell/shell-base64.c b/apps/shell/shell-base64.c new file mode 100644 index 000000000..39298f041 --- /dev/null +++ b/apps/shell/shell-base64.c @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-base64.c,v 1.1 2010/02/03 20:37:29 adamdunkels Exp $ + */ + +/** + * \file + * Base64-related shell commands + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell.h" + +#include +#include +#ifndef HAVE_SNPRINTF +int snprintf(char *str, size_t size, const char *format, ...); +#endif /* HAVE_SNPRINTF */ + +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_dec64_process, "dec64"); +SHELL_COMMAND(dec64_command, + "dec64", + "dec64: decode base64 input", + &shell_dec64_process); +/*---------------------------------------------------------------------------*/ +#define BASE64_MAX_LINELEN 76 + +struct base64_decoder_state { + uint8_t data[3 * BASE64_MAX_LINELEN / 4]; + int dataptr; + unsigned long tmpdata; + int sextets; + int padding; +}; +/*---------------------------------------------------------------------------*/ +static int +base64_decode_char(char c) +{ + if(c >= 'A' && c <= 'Z') { + return c - 'A'; + } else if(c >= 'a' && c <= 'z') { + return c - 'a' + 26; + } else if(c >= '0' && c <= '9') { + return c - '0' + 52; + } else if(c == '+') { + return 62; + } else if(c == '/') { + return 63; + } else { + return 0; + } +} +/*---------------------------------------------------------------------------*/ +static int +base64_add_char(struct base64_decoder_state *s, char c) +{ + if(isspace(c)) { + return 0; + } + + if(s->dataptr >= sizeof(s->data)) { + return 0; + } + if(c == '=') { + ++s->padding; + } + + s->tmpdata = (s->tmpdata << 6) | base64_decode_char(c); + ++s->sextets; + if(s->sextets == 4) { + s->sextets = 0; + s->data[s->dataptr] = (uint8_t)(s->tmpdata >> 16); + s->data[s->dataptr + 1] = (uint8_t)(s->tmpdata >> 8); + s->data[s->dataptr + 2] = (uint8_t)(s->tmpdata); + s->dataptr += 3; + if(s->dataptr == sizeof(s->data)) { + return 0; + } else { + return 1; + } + } + return 1; +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_dec64_process, ev, data) +{ + PROCESS_BEGIN(); + + while(1) { + struct shell_input *input; + struct base64_decoder_state s; + int i; + + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + if(input->len1 + input->len2 == 0) { + PROCESS_EXIT(); + } + + s.sextets = s.dataptr = s.padding = 0; + + for(i = 0; i < input->len1; ++i) { + base64_add_char(&s, input->data1[i]); + } + for(i = 0; i < input->len2; ++i) { + base64_add_char(&s, input->data2[i]); + } + shell_output(&dec64_command, s.data, s.dataptr - s.padding, "", 0); + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_base64_init(void) +{ + shell_register_command(&dec64_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-base64.h b/apps/shell/shell-base64.h new file mode 100644 index 000000000..e9f798a3b --- /dev/null +++ b/apps/shell/shell-base64.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-base64.h,v 1.1 2010/02/03 20:37:29 adamdunkels Exp $ + */ + +/** + * \file + * Shell commands for base64 decoding + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_BASE64_H__ +#define __SHELL_BASE64_H__ + +#include "shell.h" + +void shell_base64_init(void); + +#endif /* __SHELL_BASE64_H__ */ diff --git a/apps/shell/shell-text.c b/apps/shell/shell-text.c index a86146aa5..c82d69488 100644 --- a/apps/shell/shell-text.c +++ b/apps/shell/shell-text.c @@ -28,12 +28,12 @@ * * This file is part of the Contiki operating system. * - * $Id: shell-text.c,v 1.3 2009/03/06 00:29:33 adamdunkels Exp $ + * $Id: shell-text.c,v 1.4 2010/02/03 20:37:29 adamdunkels Exp $ */ /** * \file - * A brief description of what this file is. + * Text-related shell commands * \author * Adam Dunkels */ @@ -80,7 +80,7 @@ PROCESS_THREAD(shell_echo_process, ev, data) { PROCESS_BEGIN(); - shell_output(&echo_command, data, (int)strlen(data), "\n", 1); + shell_output(&echo_command, data, (int)strlen(data), "", 0); PROCESS_END(); } diff --git a/apps/shell/shell-text.h b/apps/shell/shell-text.h index 67571516e..4606d1946 100644 --- a/apps/shell/shell-text.h +++ b/apps/shell/shell-text.h @@ -28,12 +28,12 @@ * * This file is part of the Contiki operating system. * - * $Id: shell-text.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + * $Id: shell-text.h,v 1.2 2010/02/03 20:37:29 adamdunkels Exp $ */ /** * \file - * A brief description of what this file is. + * Text-related shell commands * \author * Adam Dunkels */ diff --git a/apps/shell/shell.h b/apps/shell/shell.h index eba8f710f..934c6fb6a 100644 --- a/apps/shell/shell.h +++ b/apps/shell/shell.h @@ -48,7 +48,7 @@ * * This file is part of the Contiki operating system. * - * $Id: shell.h,v 1.21 2010/02/02 15:28:53 adamdunkels Exp $ + * $Id: shell.h,v 1.22 2010/02/03 20:40:23 adamdunkels Exp $ */ /** @@ -353,6 +353,7 @@ struct shell_input { * @} */ +#include "shell-base64.h" #include "shell-blink.h" #include "shell-checkpoint.h" #include "shell-coffee.h" diff --git a/examples/example-shell/example-shell.c b/examples/example-shell/example-shell.c index 34817550a..bf959c07e 100644 --- a/examples/example-shell/example-shell.c +++ b/examples/example-shell/example-shell.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: example-shell.c,v 1.2 2009/05/11 17:37:15 adamdunkels Exp $ + * $Id: example-shell.c,v 1.3 2010/02/03 20:37:52 adamdunkels Exp $ */ /** @@ -59,6 +59,7 @@ PROCESS_THREAD(example_shell_process, ev, data) serial_shell_init(); + shell_base64_init(); shell_blink_init(); /*shell_checkpoint_init();*/ /*shell_coffee_init();*/ diff --git a/examples/sky-shell/sky-shell.c b/examples/sky-shell/sky-shell.c index 48d9cf134..bf3913e60 100644 --- a/examples/sky-shell/sky-shell.c +++ b/examples/sky-shell/sky-shell.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: sky-shell.c,v 1.15 2010/01/15 08:51:56 adamdunkels Exp $ + * $Id: sky-shell.c,v 1.16 2010/02/03 20:37:52 adamdunkels Exp $ */ /** @@ -239,18 +239,19 @@ PROCESS_THREAD(sky_shell_process, ev, data) serial_shell_init(); shell_blink_init(); - shell_file_init(); - shell_coffee_init(); + /* shell_file_init(); + shell_coffee_init();*/ /* shell_download_init(); shell_rime_sendcmd_init();*/ shell_ps_init(); shell_reboot_init(); shell_rime_init(); shell_rime_netcmd_init(); - /* shell_rime_ping_init();*/ + shell_rime_ping_init(); /* shell_rime_debug_init(); */ /* shell_rime_sniff_init();*/ shell_sky_init(); + shell_base64_init(); shell_text_init(); shell_time_init(); /* shell_checkpoint_init();*/