Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #include <math.h>
00022 #include <stdio.h>
00023 #include <strings.h>
00024 #include <unistd.h>
00025 
00026 #include <fcntl.h>
00027 #include <sys/stat.h>
00028 #include <sys/time.h>
00029 #include <sys/types.h>
00030 #include <time.h>
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif  // HAVE_CONFIG_H
00035 
00036 
00037 #include <communication.h>
00038 
00042 int encode(const unsigned char *src, int len, unsigned char *dst, int buf_max)
00043 {
00044   int pos, s_pos, w_pos;
00045   unsigned short b;
00046   pos = 0;    
00047   w_pos = 0;  
00048   s_pos = 0;
00049   b = 0;
00050 
00051   while (pos < len || s_pos >= 6)
00052   {
00053     if (s_pos >= 6)
00054     {
00055       dst[w_pos] = ((b >> 10) & 0x3f) + 0x40;
00056       w_pos++;
00057       if (w_pos >= buf_max)
00058         return (-1);
00059       b = b << 6;
00060       s_pos -= 6;
00061     }
00062     else
00063     {
00064       b |= src[pos] << (8 - s_pos);
00065       s_pos += 8;
00066       pos++;
00067       if (pos >= len)
00068         s_pos += 4;  
00069     }
00070   }
00071 
00072   if (w_pos >= buf_max)
00073     return (-1);
00074 
00075   return w_pos;
00076 }
00077 
00086 int decode(const unsigned char *src, int len, unsigned char *dst, int buf_max)
00087 {
00088   unsigned short dat, b;
00089   int pos, s_pos, w_pos;
00090   int rerr;
00091   pos = 0;    
00092   w_pos = 0;  
00093   s_pos = 0;  
00094   rerr = 0;
00095   dat = 0;
00096   b = 0;
00097   while (pos < len)
00098   {
00099     if (src[pos] >= 0x40)
00100       b = src[pos] - 0x40;
00101     else
00102       rerr++;
00103 
00104     dat |= (b << (10 - s_pos));
00105     s_pos += 6;
00106     if (s_pos >= 8)
00107     {
00108       dst[w_pos] = (dat >> 8);
00109       w_pos++;
00110       if (w_pos >= buf_max)
00111         return 0;
00112       s_pos -= 8;
00113       dat = dat << 8;
00114     }
00115     pos++;
00116   }
00117 
00118   if (rerr)
00119     return -rerr;
00120   return w_pos;
00121 }