u16.c - vx32 - Local 9vx git repository for patches.
 (HTM) git clone git://r-36.net/vx32
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       u16.c (822B)
       ---
            1 #include "u.h"
            2 #include "lib.h"
            3 static char t16e[] = "0123456789ABCDEF";
            4 
            5 int
            6 dec16(uchar *out, int lim, char *in, int n)
            7 {
            8         int c, w = 0, i = 0;
            9         uchar *start = out;
           10         uchar *eout = out + lim;
           11 
           12         while(n-- > 0){
           13                 c = *in++;
           14                 if('0' <= c && c <= '9')
           15                         c = c - '0';
           16                 else if('a' <= c && c <= 'z')
           17                         c = c - 'a' + 10;
           18                 else if('A' <= c && c <= 'Z')
           19                         c = c - 'A' + 10;
           20                 else
           21                         continue;
           22                 w = (w<<4) + c;
           23                 i++;
           24                 if(i == 2){
           25                         if(out + 1 > eout)
           26                                 goto exhausted;
           27                         *out++ = w;
           28                         w = 0;
           29                         i = 0;
           30                 }
           31         }
           32 exhausted:
           33         return out - start;
           34 }
           35 
           36 int
           37 enc16(char *out, int lim, uchar *in, int n)
           38 {
           39         uint c;
           40         char *eout = out + lim;
           41         char *start = out;
           42 
           43         while(n-- > 0){
           44                 c = *in++;
           45                 if(out + 2 >= eout)
           46                         goto exhausted;
           47                 *out++ = t16e[c>>4];
           48                 *out++ = t16e[c&0xf];
           49         }
           50 exhausted:
           51         *out = 0;
           52         return out - start;
           53 }