Contiki 2.5
gcr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: gcr.c,v 1.2 2011/01/07 11:55:36 nifi Exp $
32  */
33 
34 /**
35  * \file
36  * Implementation of GCR coding/decoding
37  * \author
38  * Joakim Eriksson <joakime@sics.se>
39  *
40  */
41 
42 /* GCR conversion table - used for converting ordinary byte to 10-bits */
43 /* (or 4 bits to 5) */
44 static const unsigned char GCR_encode[16] = {
45  0x0a, 0x0b, 0x12, 0x13,
46  0x0e, 0x0f, 0x16, 0x17,
47  0x09, 0x19, 0x1a, 0x1b,
48  0x0d, 0x1d, 0x1e, 0x15
49 };
50 
51 /* 5 bits > 4 bits (0xff => invalid) */
52 static const unsigned char GCR_decode[32] = {
53  0xff, 0xff, 0xff, 0xff, // 0 - 3invalid...
54  0xff, 0xff, 0xff, 0xff, // 4 - 7 invalid...
55  0xff, 0x08, 0x00, 0x01, // 8 invalid... 9 = 8, a = 0, b = 1
56  0xff, 0x0c, 0x04, 0x05, // c invalid... d = c, e = 4, f = 5
57 
58  0xff, 0xff, 0x02, 0x03, // 10-11 invalid...
59  0xff, 0x0f, 0x06, 0x07, // 14 invalid...
60  0xff, 0x09, 0x0a, 0x0b, // 18 invalid...
61  0xff, 0x0d, 0x0e, 0xff, // 1c, 1f invalid...
62  };
63 
64 static unsigned char gcr_bits = 0;
65 static unsigned short gcr_val = 0;
66 
67 /* Call before starting encoding or decoding */
68 void gcr_init() {
69  gcr_val = 0;
70  gcr_bits = 0;
71 }
72 
73 /* Use this to check if encoding / decoding is complete for now */
74 unsigned char gcr_finished() {
75  return gcr_bits == 0;
76 }
77 
78 /* Encode one character - and store in bits - get encoded with get_encoded */
79 void gcr_encode(unsigned char raw_data) {
80  gcr_val |=
81  ((GCR_encode[raw_data >> 4u] << 5u ) |
82  GCR_encode[raw_data & 0xf]) << gcr_bits;
83  gcr_bits += 10;
84 }
85 
86 /* Gets the current char of the encoded stream */
87 unsigned char gcr_get_encoded(unsigned char *raw_data) {
88  if (gcr_bits >= 8) {
89  *raw_data = (unsigned char) (gcr_val & 0xff);
90  gcr_val = gcr_val >> 8u;
91  gcr_bits = gcr_bits - 8;
92  return 1;
93  }
94  return 0;
95 }
96 
97 /* Decode one char - result can be get from get_decoded */
98 void gcr_decode(unsigned char gcr_data) {
99  gcr_val |= gcr_data << gcr_bits;
100  gcr_bits += 8;
101 }
102 
103 /* check if the current decoded stream is correct */
104 unsigned char gcr_valid() {
105  if (gcr_bits >= 10) {
106  unsigned short val = gcr_val & 0x3ff;
107  if ((GCR_decode[val >> 5u] << 4u) == 0xff ||
108  (GCR_decode[val & 0x1f]) == 0xff) {
109  return 0;
110  }
111  }
112  return 1;
113 }
114 
115 /* gets the decoded stream - if any char is available */
116 unsigned char gcr_get_decoded(unsigned char *raw_data) {
117  if (gcr_bits >= 10) {
118  unsigned short val = gcr_val & 0x3ff;
119  *raw_data = (unsigned char) ((GCR_decode[val >> 5] << 4) |
120  (GCR_decode[val & 0x1f]));
121  gcr_val = gcr_val >> 10;
122  gcr_bits = gcr_bits - 10;
123  return 1;
124  }
125  return 0;
126 }
127 
128 /*
129 static const char encoded[] = {
130  0x4a, 0x25, 0xa5, 0xfc, 0x96, 0xff, 0xff, 0xb5, 0xd4, 0x5a, 0xea, 0xff, 0xff, 0xaa, 0xd3, 0xff
131 };
132 
133 int main(int argc, char **argv) {
134  // unsigned char c[] = "testing gcr 1 2 3 4 5 6...";
135  unsigned char c[] = { 0, 8, 0xe0, 0x2b, 0xac, 0x10, 0x01, 0x11, 0x50, 0xff, 0xf4, 0xa4, 0x00 };
136  unsigned char c2[200];
137  int pos = 0, pos2 = 0, i = 0;
138 
139  printf("Testing GCR on: %s \n", c);
140 
141  gcr_init();
142  for (i = 0; i < sizeof(c); i++) {
143  gcr_encode(c[i]);
144  while(gcr_get_encoded(&c2[pos])) {
145  printf("%02x=>%02x ", c[i], c2[pos]);
146  pos++;
147  }
148  }
149  printf("\n");
150  printf("Encoded result %d chars (from %d) \n", pos, i);
151  gcr_init();
152  for (i = 0; i < pos; i++) {
153  gcr_decode(c2[i]);
154  if(!gcr_valid()) {
155  printf("GCR: not valid\n");
156  }
157  while(gcr_get_decoded(&c[pos2])) {
158  pos2++;
159  }
160  }
161  printf("GCR: %s\n",c);
162 }
163 */