communication.c
Go to the documentation of this file.
00001 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
00002 //
00003 // Permission is hereby granted, free of charge, to any person obtaining a copy
00004 // of this software and associated documentation files (the "Software"), to
00005 // deal in the Software without restriction, including without limitation the
00006 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00007 // sell copies of the Software, and to permit persons to whom the Software is
00008 // furnished to do so, subject to the following conditions:
00009 //
00010 // The above copyright notice and this permission notice shall be included in
00011 // all copies or substantial portions of the Software.
00012 //
00013 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00016 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00019 // SOFTWARE.
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 /* yp-spur用 */
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;    // read position
00047   w_pos = 0;  // write_position
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;    // read position
00092   w_pos = 0;  // write_position
00093   s_pos = 0;  // shift position
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 }


yp-spur
Author(s):
autogenerated on Fri May 10 2019 02:52:19