Contiki 3.x
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  */
32 
33 /**
34  * \file
35  * Implementation of GCR coding/decoding
36  * \author
37  * Joakim Eriksson <joakime@sics.se>
38  *
39  */
40 
41 /* GCR conversion table - used for converting ordinary byte to 10-bits */
42 /* (or 4 bits to 5) */
43 static const unsigned char GCR_encode[16] = {
44  0x0a, 0x0b, 0x12, 0x13,
45  0x0e, 0x0f, 0x16, 0x17,
46  0x09, 0x19, 0x1a, 0x1b,
47  0x0d, 0x1d, 0x1e, 0x15
48 };
49 
50 /* 5 bits > 4 bits (0xff => invalid) */
51 static const unsigned char GCR_decode[32] = {
52  0xff, 0xff, 0xff, 0xff, // 0 - 3invalid...
53  0xff, 0xff, 0xff, 0xff, // 4 - 7 invalid...
54  0xff, 0x08, 0x00, 0x01, // 8 invalid... 9 = 8, a = 0, b = 1
55  0xff, 0x0c, 0x04, 0x05, // c invalid... d = c, e = 4, f = 5
56 
57  0xff, 0xff, 0x02, 0x03, // 10-11 invalid...
58  0xff, 0x0f, 0x06, 0x07, // 14 invalid...
59  0xff, 0x09, 0x0a, 0x0b, // 18 invalid...
60  0xff, 0x0d, 0x0e, 0xff, // 1c, 1f invalid...
61  };
62 
63 static unsigned char gcr_bits = 0;
64 static unsigned short gcr_val = 0;
65 
66 /* Call before starting encoding or decoding */
67 void gcr_init() {
68  gcr_val = 0;
69  gcr_bits = 0;
70 }
71 
72 /* Use this to check if encoding / decoding is complete for now */
73 unsigned char gcr_finished() {
74  return gcr_bits == 0;
75 }
76 
77 /* Encode one character - and store in bits - get encoded with get_encoded */
78 void gcr_encode(unsigned char raw_data) {
79  gcr_val |=
80  ((GCR_encode[raw_data >> 4u] << 5u ) |
81  GCR_encode[raw_data & 0xf]) << gcr_bits;
82  gcr_bits += 10;
83 }
84 
85 /* Gets the current char of the encoded stream */
86 unsigned char gcr_get_encoded(unsigned char *raw_data) {
87  if (gcr_bits >= 8) {
88  *raw_data = (unsigned char) (gcr_val & 0xff);
89  gcr_val = gcr_val >> 8u;
90  gcr_bits = gcr_bits - 8;
91  return 1;
92  }
93  return 0;
94 }
95 
96 /* Decode one char - result can be get from get_decoded */
97 void gcr_decode(unsigned char gcr_data) {
98  gcr_val |= gcr_data << gcr_bits;
99  gcr_bits += 8;
100 }
101 
102 /* check if the current decoded stream is correct */
103 unsigned char gcr_valid() {
104  if (gcr_bits >= 10) {
105  unsigned short val = gcr_val & 0x3ff;
106  if ((GCR_decode[val >> 5u] << 4u) == 0xff ||
107  (GCR_decode[val & 0x1f]) == 0xff) {
108  return 0;
109  }
110  }
111  return 1;
112 }
113 
114 /* gets the decoded stream - if any char is available */
115 unsigned char gcr_get_decoded(unsigned char *raw_data) {
116  if (gcr_bits >= 10) {
117  unsigned short val = gcr_val & 0x3ff;
118  *raw_data = (unsigned char) ((GCR_decode[val >> 5] << 4) |
119  (GCR_decode[val & 0x1f]));
120  gcr_val = gcr_val >> 10;
121  gcr_bits = gcr_bits - 10;
122  return 1;
123  }
124  return 0;
125 }
126 
127 /*
128 static const char encoded[] = {
129  0x4a, 0x25, 0xa5, 0xfc, 0x96, 0xff, 0xff, 0xb5, 0xd4, 0x5a, 0xea, 0xff, 0xff, 0xaa, 0xd3, 0xff
130 };
131 
132 int main(int argc, char **argv) {
133  // unsigned char c[] = "testing gcr 1 2 3 4 5 6...";
134  unsigned char c[] = { 0, 8, 0xe0, 0x2b, 0xac, 0x10, 0x01, 0x11, 0x50, 0xff, 0xf4, 0xa4, 0x00 };
135  unsigned char c2[200];
136  int pos = 0, pos2 = 0, i = 0;
137 
138  printf("Testing GCR on: %s \n", c);
139 
140  gcr_init();
141  for (i = 0; i < sizeof(c); i++) {
142  gcr_encode(c[i]);
143  while(gcr_get_encoded(&c2[pos])) {
144  printf("%02x=>%02x ", c[i], c2[pos]);
145  pos++;
146  }
147  }
148  printf("\n");
149  printf("Encoded result %d chars (from %d) \n", pos, i);
150  gcr_init();
151  for (i = 0; i < pos; i++) {
152  gcr_decode(c2[i]);
153  if(!gcr_valid()) {
154  printf("GCR: not valid\n");
155  }
156  while(gcr_get_decoded(&c[pos2])) {
157  pos2++;
158  }
159  }
160  printf("GCR: %s\n",c);
161 }
162 */