]> git.cryptolib.org Git - arm-crypto-lib.git/commitdiff
added base64 encoding
authorbg <daniel.otte@rub.de>
Sun, 27 Feb 2011 17:18:34 +0000 (18:18 +0100)
committerbg <daniel.otte@rub.de>
Sun, 27 Feb 2011 17:18:34 +0000 (18:18 +0100)
base64/base64_dec.c [new file with mode: 0644]
base64/base64_dec.h [new file with mode: 0644]
base64/base64_enc.c [new file with mode: 0644]
base64/base64_enc.h [new file with mode: 0644]
lm3s9b90.ld
mkfiles/base64.mk [new file with mode: 0644]
noekeon/noekeon_prng.c
test_src/main-base64-test.c [new file with mode: 0644]
test_src/main-bigint-test.c
test_src/string-extras.c
test_src/string-extras.h

diff --git a/base64/base64_dec.c b/base64/base64_dec.c
new file mode 100644 (file)
index 0000000..724caf8
--- /dev/null
@@ -0,0 +1,211 @@
+/* base64_dec.c */
+/*
+ *   This file is part of the ARM-Crypto-Lib.
+ *   Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+/**
+ * base64 decoder (RFC3548)
+ * Author: Daniel Otte
+ * License: GPLv3
+ * 
+ * 
+ */
+
+#include <stdint.h>
+#include "base64_dec.h"
+
+
+/*
+#define USE_GCC_EXTENSION
+*/
+
+#ifdef USE_GCC_EXTENSION
+
+static
+int ascii2bit6(char a){
+       switch(a){
+               case 'A'...'Z':
+                       return a-'A';
+               case 'a'...'z':
+                       return a-'a'+26;
+               case '0'...'9':
+                       return a-'0'+52;
+               case '+':
+               case '-':
+                       return 62;
+               case '/':
+               case '_':
+                       return 63;
+               default:
+                       return -1;
+       }
+}
+
+#else
+
+static
+uint8_t ascii2bit6(char a){
+       int r;
+       switch(a>>4){
+               case 0x5:
+               case 0x4: 
+                       r=a-'A';
+                       if(r<0 || r>25){
+                               return -1;
+                       } else {
+                               return r;
+                       }
+               case 0x7:
+               case 0x6: 
+                       r=a-'a';
+                       if(r<0 || r>25){
+                               return -1;
+                       } else {
+                               return r+26;
+                       }
+                       break;
+               case 0x3:
+                       if(a>'9')
+                               return -1;
+                       return a-'0'+52;
+               default:
+                       break;  
+       }
+       switch (a){
+               case '+':
+               case '-':
+                       return 62;
+               case '/':
+               case '_':
+                       return 63;
+               default:
+                       return 0xff;
+       }
+}
+
+#endif
+
+int base64_binlength(char* str, uint8_t strict){
+       int l=0;
+       uint8_t term=0;
+       for(;;){
+               if(*str=='\0')
+                       break;
+               if(*str=='\n' || *str=='\r'){
+                       str++;
+                       continue;
+               }
+               if(*str=='='){
+                       term++;
+                       str++;
+                       if(term==2){
+                               break;
+                       }
+                       continue;
+               }
+               if(term)
+                       return -1;
+               if(ascii2bit6(*str)==-1){
+                       if(strict)
+                               return -1;
+               } else {
+                       l++;
+               }
+               str++;
+       }
+       switch(term){
+               case 0:
+                       if(l%4!=0)
+                               return -1;
+                       return l/4*3;
+               case 1:
+                       if(l%4!=3)
+                               return -1;
+                       return (l+1)/4*3-1;
+               case 2:
+                       if(l%4!=2)
+                               return -1;
+                       return (l+2)/4*3-2;
+               default:
+                       return -1;
+       }
+}
+
+/*
+  |543210543210543210543210|
+  |765432107654321076543210|
+
+        .      .      .     .
+  |54321054|32105432|10543210|
+  |76543210|76543210|76543210|
+
+*/
+
+int base64dec(void* dest, const char* b64str, uint8_t strict){
+       uint8_t buffer[4];
+       uint8_t idx=0;
+       uint8_t term=0;
+       for(;;){
+               buffer[idx]= ascii2bit6(*b64str);
+               
+               if(buffer[idx]==0xFF){
+                       if(*b64str=='='){
+                               term++;
+                               b64str++;
+                               if(term==2)
+                                       goto finalize; /* definitly the end */
+                       }else{
+                               if(*b64str == '\0'){
+                                       goto finalize; /* definitly the end */
+                               }else{
+                                       if(*b64str == '\r' || *b64str == '\n' || !(strict)){
+                                               b64str++; /* charcters that we simply ignore */
+                                       }else{
+                                               return -1;
+                                       }
+                               }
+                       }
+               }else{
+                       if(term)
+                               return -1; /* this happens if we get a '=' in the stream */
+                       idx++;
+                       b64str++;
+               }
+               if(idx==4){
+                       ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;
+                       ((uint8_t*)dest)[1] = buffer[1]<<4 | buffer[2]>>2;
+                       ((uint8_t*)dest)[2] = buffer[2]<<6 | buffer[3];
+                       dest = (uint8_t*)dest +3;
+                       idx=0;
+               }
+       }
+  finalize:    
+       /* the final touch */
+       if(idx==0)
+               return 0;
+       if(term==1){
+               ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;
+               ((uint8_t*)dest)[1] = buffer[1]<<4 | buffer[2]>>2;                      
+               return 0;
+       }
+       if(term==2){
+               ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;
+               return 0;
+       }
+       return -1;
+}
diff --git a/base64/base64_dec.h b/base64/base64_dec.h
new file mode 100644 (file)
index 0000000..5a75d00
--- /dev/null
@@ -0,0 +1,29 @@
+/* base64_dec.h */
+/*
+ *   This file is part of the ARM-Crypto-Lib.
+ *   Copyright (C) 2006, 2007, 2008  Daniel Otte (daniel.otte@rub.de)
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef BASE64_DEC_H_
+#define BASE64_DEC_H_
+
+#include <stdint.h>
+
+int base64_binlength(char* str, uint8_t strict);
+int base64dec(void* dest, const char* b64str, uint8_t strict);
+
+#endif /*BASE64_DEC_H_*/
diff --git a/base64/base64_enc.c b/base64/base64_enc.c
new file mode 100644 (file)
index 0000000..09aed98
--- /dev/null
@@ -0,0 +1,88 @@
+/* base64_enc.c */
+/*
+ *   This file is part of the ARM-Crypto-Lib.
+ *   Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+/**
+ * base64 encoder (RFC3548)
+ * Author: Daniel Otte
+ * License: GPLv3
+ * 
+ * 
+ */
+
+#include <stdint.h>
+#include "base64_enc.h"
+
+const char base64_alphabet[64] = {
+       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
+       'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 
+       'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
+       'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 
+       'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
+       'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
+       'w', 'x', 'y', 'z', '0', '1', '2', '3', 
+       '4', '5', '6', '7', '8', '9', '+', '/' }; 
+
+
+static 
+char bit6toAscii(uint8_t a){
+       a &= (uint8_t)0x3F;
+       return base64_alphabet[a];
+}
+
+void base64enc(char* dest,const void* src, uint16_t length){
+       uint16_t i,j;
+       uint8_t a[4];
+       for(i=0; i<length/3; ++i){
+               a[0]= (((uint8_t*)src)[i*3+0])>>2;
+               a[1]= (((((uint8_t*)src)[i*3+0])<<4) | ((((uint8_t*)src)[i*3+1])>>4)) & 0x3F;
+               a[2]= (((((uint8_t*)src)[i*3+1])<<2) | ((((uint8_t*)src)[i*3+2])>>6)) & 0x3F;
+               a[3]= (((uint8_t*)src)[i*3+2]) & 0x3F;
+               for(j=0; j<4; ++j){
+                       *dest++=bit6toAscii(a[j]);
+               }
+       }
+       /* now we do the rest */
+       switch(length%3){
+               case 0: 
+                       break;
+               case 1:
+                       a[0]=(((uint8_t*)src)[i*3+0])>>2;
+                       a[1]=((((uint8_t*)src)[i*3+0])<<4)&0x3F;
+                       *dest++ = bit6toAscii(a[0]);
+                       *dest++ = bit6toAscii(a[1]);
+                       *dest++ = '=';
+                       *dest++ = '=';
+                       break;
+               case 2:         
+                       a[0]= (((uint8_t*)src)[i*3+0])>>2;
+                       a[1]= (((((uint8_t*)src)[i*3+0])<<4) | ((((uint8_t*)src)[i*3+1])>>4)) & 0x3F;
+                       a[2]= ((((uint8_t*)src)[i*3+1])<<2) & 0x3F;
+                       *dest++ = bit6toAscii(a[0]);
+                       *dest++ = bit6toAscii(a[1]);
+                       *dest++ = bit6toAscii(a[2]);
+                       *dest++ = '=';
+                       break;
+               default: /* this will not happen! */
+                       break;  
+       }
+/*  finalize: */
+       *dest='\0';
+}
+
diff --git a/base64/base64_enc.h b/base64/base64_enc.h
new file mode 100644 (file)
index 0000000..4f3f98c
--- /dev/null
@@ -0,0 +1,28 @@
+/* base64_enc.h */
+/*
+ *   This file is part of the ARM-Crypto-Lib.
+ *   Copyright (C) 2006, 2007, 2008  Daniel Otte (daniel.otte@rub.de)
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef BASE64_ENC_H_
+#define BASE64_ENC_H_
+
+#include <stdint.h>
+
+void base64enc(char* dest, const void* src, uint16_t length);
+
+#endif /*BASE64_ENC_H_*/
index 8672a932e8e2d7d9f5cba925ad85f61897c2d4e3..36ef54162bfbc2af5270d4b7b235d0778b840c9c 100644 (file)
@@ -26,14 +26,28 @@ SECTIONS {
        .text : {
                _text = .;
                KEEP(*(.isr_vectors))
-               *(.init*)
-               *(.fini*)
                *(.text*)
                *(.rodata*)
-               _text_end = .;
        } > flash
+     /* .ARM.exidx is sorted, so has to go in its own output section.
+*/
+    .ARM.exidx : {
+        __exidx_start = .;
+        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
+        __exidx_end = .;
+    } > flash
+
+  . = ALIGN(4);
+    .ARM.extab : {
+        __extab_start = .;
+        *(.ARM.extab* .gnu.linkonce.armextab.*)
+        __extab_end = .;
+    } > flash
+
+  . = ALIGN(4);
        
-       .data : AT ( ADDR(.text) + SIZEOF(.text) ) {
+       _text_end = .;
+       .data : AT ( ADDR(.ARM.extab) + SIZEOF(.ARM.extab) ) {
                _data = .;
                *(.data*)
                _data_end = .;
@@ -41,11 +55,9 @@ SECTIONS {
        
        .bss : AT ( ADDR(.data) + SIZEOF(.data) ) {
                _bss = .;
-               __bss_start__ = .;
                *(.bss*)
                *(COMMON)
                _bss_end = .;
-               __bss_end__ = .;
        } > ram 
     end = .;
 }
diff --git a/mkfiles/base64.mk b/mkfiles/base64.mk
new file mode 100644 (file)
index 0000000..8abfbcc
--- /dev/null
@@ -0,0 +1,14 @@
+# Makefile for Base64-encoding
+ALGO_NAME := BASE64
+
+# comment out the following line for removement of base64 from the build process
+ENCODINGS += $(ALGO_NAME)
+
+$(ALGO_NAME)_DIR      := base64/
+$(ALGO_NAME)_OBJ      := base64_enc.o base64_dec.o
+$(ALGO_NAME)_INCDIR   := noekeon/ memxor/
+$(ALGO_NAME)_TEST_BIN := main-base64-test.o $(CLI_STD)  \
+                         performance_test.o noekeon.o noekeon_prng.o memxor.o
+                        
+$(ALGO_NAME)_PERFORMANCE_TEST := performance
+
index a264707a3235bd72bc14b61514c9226292b46380..3b2eca9c494d0a92661b2e7840aca167cc91af09 100644 (file)
@@ -31,7 +31,7 @@
 
 static uint8_t random_state[16];
 static uint8_t random_key[16];
-static uint8_t i=0;
+static volatile uint8_t i=0;
 
 uint8_t random8(void){
        static uint8_t sr[16];
diff --git a/test_src/main-base64-test.c b/test_src/main-base64-test.c
new file mode 100644 (file)
index 0000000..67fb730
--- /dev/null
@@ -0,0 +1,189 @@
+/* main-base64-test.c */
+/*
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2008, 2009  Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+ * base64 test-suit
+ * 
+*/
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "config.h"
+#include "cli.h"
+#include "dump.h"
+#include "uart_lowlevel.h"
+#include "sysclock.h"
+#include "hw_gptm.h"
+
+#include "noekeon.h"
+#include "noekeon_prng.h"
+#include "base64_enc.h"
+#include "base64_dec.h"
+#include "string-extras.h"
+#include "cli.h"
+#include "performance_test.h"
+
+void uart0_putc(char byte){
+       uart_putc(UART_0, byte);
+}
+
+char uart0_getc(void){
+       return uart_getc(UART_0);
+}
+
+char* algo_name = "Base64";
+
+/*****************************************************************************
+ *  additional validation-functions                                                                                     *
+ *****************************************************************************/
+#define TESTLENGTH 4096
+
+void testrun_stdtest_base64(void){
+       uint8_t fail=0;
+       unsigned l,i;
+       int     sl;
+       char str[32];
+       uint8_t bin_buffer[TESTLENGTH];
+       char    b64_buffer[TESTLENGTH*4/3+10];
+       uint8_t bin_buffer2[TESTLENGTH];
+       random_seed(bin_buffer);
+       
+       for(l=0; l<TESTLENGTH-1; ++l){
+               cli_putstr("\r\nTest ");
+               ultoa(l, str, 10);
+               cli_putstr(str);
+               
+               uart_flush(0);
+               for(i=0; i<l; ++i){
+                       bin_buffer[i] = random8();
+               }
+               cli_putstr("\r\n bin: ");
+               cli_hexdump(bin_buffer, l);
+
+               uart_flush(0);
+               base64enc(b64_buffer, bin_buffer, l);
+               cli_putstr("\r\n b64: ");
+               cli_putstr(b64_buffer);
+
+               uart_flush(0);
+               sl = base64_binlength(b64_buffer, 1);
+               
+               if(sl!=l){
+                       cli_putstr("\r\n ERROR length ");
+                       cli_putstr(str);
+                       if(sl!=-1){
+                               cli_putstr(" != ");
+                               ultoa(l, str, 10);
+                               cli_putstr(str);
+                       }else{
+                               cli_putstr(" != -1");
+                       }
+                       fail=1;
+               }else{
+                       cli_putstr("\r\n length ok");
+                       uart_flush(0);
+               }
+               uart_flush(0);
+               base64dec(bin_buffer2, b64_buffer, 1);
+               if(memcmp(bin_buffer, bin_buffer2, l)){
+                       cli_putstr("\r\n ERROR value\r\n out: ");
+                       cli_hexdump(bin_buffer2, l);
+                       fail=1;
+               }else{
+                       cli_putstr("\r\n value ok");
+                       uart_flush(0);
+               }
+               if(fail)
+                       break;
+       }       
+       cli_putstr(fail?"\r\n [FAIL]\r\n":"\r\n [OK]\r\n");
+}
+
+void testrun_performance_base64(void){
+/*
+       uint64_t t;
+       char str[16];
+       uint8_t key[16], data[16];
+       noekeon_ctx_t ctx;
+       
+       calibrateTimer();
+       print_overhead();
+       
+       memset(key,  0, 16);
+       memset(data, 0, 16);
+       
+       startTimer(1);
+       noekeon_init(key, &ctx);
+       t = stopTimer();
+       cli_putstr("\r\n\tctx-gen time: ");
+       ultoa((unsigned long)t, str, 10);
+       cli_putstr(str);        
+       
+       startTimer(1);
+       noekeon_enc(data, &ctx);
+       t = stopTimer();
+       cli_putstr("\r\n\tencrypt time: ");
+       ultoa((unsigned long)t, str, 10);
+       cli_putstr(str);        
+       
+       startTimer(1);
+       noekeon_dec(data, &ctx);
+       t = stopTimer();
+       cli_putstr("\r\n\tdecrypt time: ");
+       ultoa((unsigned long)t, str, 10);
+       cli_putstr(str);
+       
+       cli_putstr("\r\n");
+*/
+}
+/*****************************************************************************
+ *  main                                                                                                                                        *
+ *****************************************************************************/
+
+const char test_str[]        = "test";
+const char performance_str[] = "performance";
+const char echo_str[]        = "echo";
+
+const cmdlist_entry_t cmdlist[] = {
+       { test_str,        NULL, testrun_stdtest_base64},
+       { performance_str, NULL, testrun_performance_base64},
+       { echo_str,    (void*)1, (void_fpt)echo_ctrl},
+       { NULL,            NULL, NULL}
+};
+
+int main (void){
+       sysclk_set_freq(SYS_FREQ);
+       sysclk_mosc_verify_enable();
+       uart_init(UART_0, 115200, 8, UART_PARATY_NONE, UART_STOPBITS_ONE);
+       gptm_set_timer_32periodic(TIMER0);
+
+       cli_rx = uart0_getc;
+       cli_tx = uart0_putc;
+       
+       for(;;){
+               cli_putstr("\r\n\r\nARM-Crypto-Lib VS (");
+               cli_putstr(algo_name);
+               cli_putstr("; ");
+               cli_putstr(__DATE__);
+               cli_putc(' ');
+               cli_putstr(__TIME__);
+               cli_putstr(")\r\nloaded and running\r\n");
+               cmd_interface(cmdlist);
+       }
+}
index 4ec32eeaa3b17aa2df362b45a1a0f9f1f98ae078..621e70f5a6d88bddf454cf89a9ce97ef18190634 100644 (file)
 
 #include "performance_test.h"
 
-#include <stdint.h>
-#include <string.h>
-#include <stdlib.h>
-
 char* algo_name = "BigInt";
 
 void uart0_putc(char byte){
index 0c01a048c8f4486002043f90503b4380769c7ba7..41e3aecb7a7d0cc6726cda06570150adb8c7a331 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 #include <ctype.h>
 #include <string.h>
+#include <stdlib.h>
 
 uint32_t stridentcnt(const char* a, const char* b){
        uint16_t i=0;
@@ -76,63 +77,85 @@ void str_reverse(char* buffer){
                --j;
        }
 }
-
-char* ultoa(unsigned long a, char* buffer, uint8_t radix){
-       if(radix<2 || radix>36){
+char* ultoa (unsigned long value, char* buffer, uint8_t radix ){
+       if((radix>36) || (radix<2) || (buffer==NULL)){
                return NULL;
        }
-       char* ptr=buffer;
-       div_t result;
-       if(a==0){
-               ptr[0] = '0';
-               ptr[1] = '\0';
-               return buffer;
-       }
-       while(a){
-               result = div(a, radix);
-               *ptr = result.rem;
-               if(result.rem<10){
-                       *ptr += '0';
+       unsigned length=(unsigned)-1;
+       uint8_t x;
+       char c;
+       do{
+               x = value%radix;
+               value /= radix;
+               if(x<10){
+                       c = x+ '0';
                }else{
-                       *ptr += 'a'-10;
+                       c = x+ 'a';
                }
-               ++ptr;
-               a = result.quot;
+               buffer[++length] = c;
+       }while(value);
+       buffer[length+1]='\0';
+       unsigned idx=0;
+       while(idx+1<length){
+               c = buffer[idx];
+               buffer[idx++] = buffer[length];
+               buffer[length--] = c;
        }
-       *ptr = '\0';
-       str_reverse(buffer);
        return buffer;
 }
 
-char* ulltoa(unsigned long long a, char* buffer, uint8_t radix){
-       if(radix<2 || radix>36){
+char* ulltoa(unsigned long long value, char* buffer, uint8_t radix){
+       if((radix>36) || (radix<2) || (buffer==NULL)){
                return NULL;
        }
-       char* ptr=buffer;
-       uint8_t rem;
-       if(a==0){
-               ptr[0] = '0';
-               ptr[1] = '\0';
-               return buffer;
-       }
-       while(a){
-               rem = a % radix;
-               a = a / radix;
-               *ptr = rem;
-               if(rem<10){
-                       *ptr += '0';
+       unsigned length=(unsigned)-1;
+       uint8_t x;
+       char c;
+       do{
+               x = value%radix;
+               value /= radix;
+               if(x<10){
+                       c = x+ '0';
                }else{
-                       *ptr += 'a'-10;
+                       c = x+ 'a';
                }
-               ++ptr;
+               buffer[++length] = c;
+       }while(value);
+       buffer[length+1]='\0';
+       unsigned idx=0;
+       while(idx+1<length){
+               c = buffer[idx];
+               buffer[idx++] = buffer[length];
+               buffer[length--] = c;
        }
-       *ptr = '\0';
-       str_reverse(buffer);
        return buffer;
 }
 
-char* ustoa(unsigned short a, char* buffer, uint8_t radix){
-       return ultoa((unsigned long)a, buffer, radix);
+char* ustoa(unsigned short value, char* buffer, uint8_t radix){
+       if((radix>36) || (radix<2) || (buffer==NULL)){
+               return NULL;
+       }
+       unsigned length=(unsigned)-1;
+       uint8_t x;
+       char c;
+       do{
+               x = value%radix;
+               value /= radix;
+               if(x<10){
+                       c = x+ '0';
+               }else{
+                       c = x+ 'a';
+               }
+               buffer[++length] = c;
+       }while(value);
+       buffer[length+1]='\0';
+       unsigned idx=0;
+       while(idx+1<length){
+               c = buffer[idx];
+               buffer[idx++] = buffer[length];
+               buffer[length--] = c;
+       }
+       return buffer;
 }
 /*
 void strlwr(char* s){
index 066b80439607cb3a354aaaa622841e41b7dcd0ba..d982f8a86bc386beab41d6cb20b38e3b7c3c5abb 100644 (file)
@@ -61,5 +61,5 @@ char* ultoa(unsigned long a, char* buffer, uint8_t radix);
 
 char* ulltoa(unsigned long long a, char* buffer, uint8_t radix);
 
-char* ustoa(unsigned long a, char* buffer, uint8_t radix);
+char* ustoa(unsigned short a, char* buffer, uint8_t radix);
 // void strlwr(char* s);