communication.c
Go to the documentation of this file.
1 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include <math.h>
22 #include <stdio.h>
23 #include <strings.h>
24 #include <unistd.h>
25 
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <time.h>
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif // HAVE_CONFIG_H
35 
36 /* yp-spur用 */
37 #include <communication.h>
38 
42 int encode(const unsigned char *src, int len, unsigned char *dst, int buf_max)
43 {
44  int pos, s_pos, w_pos;
45  unsigned short b;
46  pos = 0; // read position
47  w_pos = 0; // write_position
48  s_pos = 0;
49  b = 0;
50 
51  while (pos < len || s_pos >= 6)
52  {
53  if (s_pos >= 6)
54  {
55  dst[w_pos] = ((b >> 10) & 0x3f) + 0x40;
56  w_pos++;
57  if (w_pos >= buf_max)
58  return (-1);
59  b = b << 6;
60  s_pos -= 6;
61  }
62  else
63  {
64  b |= src[pos] << (8 - s_pos);
65  s_pos += 8;
66  pos++;
67  if (pos >= len)
68  s_pos += 4; // 最後
69  }
70  }
71 
72  if (w_pos >= buf_max)
73  return (-1);
74 
75  return w_pos;
76 }
77 
86 int decode(const unsigned char *src, int len, unsigned char *dst, int buf_max)
87 {
88  unsigned short dat, b;
89  int pos, s_pos, w_pos;
90  int rerr;
91  pos = 0; // read position
92  w_pos = 0; // write_position
93  s_pos = 0; // shift position
94  rerr = 0;
95  dat = 0;
96  b = 0;
97  while (pos < len)
98  {
99  if (src[pos] >= 0x40)
100  b = src[pos] - 0x40;
101  else
102  rerr++;
103 
104  dat |= (b << (10 - s_pos));
105  s_pos += 6;
106  if (s_pos >= 8)
107  {
108  dst[w_pos] = (dat >> 8);
109  w_pos++;
110  if (w_pos >= buf_max)
111  return 0;
112  s_pos -= 8;
113  dat = dat << 8;
114  }
115  pos++;
116  }
117 
118  if (rerr)
119  return -rerr;
120  return w_pos;
121 }
int decode(const unsigned char *src, int len, unsigned char *dst, int buf_max)
デコード
Definition: communication.c:86
int encode(const unsigned char *src, int len, unsigned char *dst, int buf_max)
エンコード
Definition: communication.c:42


yp-spur
Author(s):
autogenerated on Sat May 11 2019 02:08:24