1 /*
2  *    Copyright (c) 2016, The OpenThread Authors.
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 are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  *    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20  *    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  *    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  *    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  *    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  *    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /**
29  * @file
30  *   This file implements an HDLC-lite encoder and decoder.
31  */
32 
33 #include "hdlc.hpp"
34 
35 #include <stdlib.h>
36 
37 #include "common/code_utils.hpp"
38 
39 namespace ot {
40 namespace Hdlc {
41 
42 /**
43  * This method updates an FCS.
44  *
45  * @param[in]  aFcs   The FCS to update.
46  * @param[in]  aByte  The input byte value.
47  *
48  * @returns The updated FCS.
49  *
50  */
51 static uint16_t UpdateFcs(uint16_t aFcs, uint8_t aByte);
52 
53 enum
54 {
55     kFlagXOn        = 0x11,
56     kFlagXOff       = 0x13,
57     kFlagSequence   = 0x7e, ///< HDLC Flag value
58     kEscapeSequence = 0x7d, ///< HDLC Escape value
59     kFlagSpecial    = 0xf8,
60 };
61 
62 /**
63  * FCS lookup table
64  *
65  */
66 enum
67 {
68     kInitFcs = 0xffff, ///< Initial FCS value.
69     kGoodFcs = 0xf0b8, ///< Good FCS value.
70     kFcsSize = 2,      ///< FCS size (number of bytes).
71 };
72 
UpdateFcs(uint16_t aFcs,uint8_t aByte)73 uint16_t UpdateFcs(uint16_t aFcs, uint8_t aByte)
74 {
75     static const uint16_t sFcsTable[256] = {
76         0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5,
77         0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb, 0xae52,
78         0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3,
79         0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
80         0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9,
81         0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x430c, 0x7197, 0x601e,
82         0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 0x6306, 0x728f,
83         0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
84         0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862,
85         0x9af9, 0x8b70, 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb,
86         0x4e64, 0x5fed, 0x6d76, 0x7cff, 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948,
87         0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
88         0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226,
89         0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 0xc60c, 0xd785, 0xe51e, 0xf497,
90         0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 0xd68d, 0xc704,
91         0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
92         0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb,
93         0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c,
94         0x3de3, 0x2c6a, 0x1ef1, 0x0f78};
95     return (aFcs >> 8) ^ sFcsTable[(aFcs ^ aByte) & 0xff];
96 }
97 
HdlcByteNeedsEscape(uint8_t aByte)98 static bool HdlcByteNeedsEscape(uint8_t aByte)
99 {
100     bool rval;
101 
102     switch (aByte)
103     {
104     case kFlagXOn:
105     case kFlagXOff:
106     case kEscapeSequence:
107     case kFlagSequence:
108     case kFlagSpecial:
109         rval = true;
110         break;
111 
112     default:
113         rval = false;
114         break;
115     }
116 
117     return rval;
118 }
119 
Encoder(FrameWritePointer & aWritePointer)120 Encoder::Encoder(FrameWritePointer &aWritePointer)
121     : mWritePointer(aWritePointer)
122     , mFcs(0)
123 {
124 }
125 
BeginFrame(void)126 otError Encoder::BeginFrame(void)
127 {
128     mFcs = kInitFcs;
129 
130     return mWritePointer.WriteByte(kFlagSequence);
131 }
132 
Encode(uint8_t aByte)133 otError Encoder::Encode(uint8_t aByte)
134 {
135     otError error = OT_ERROR_NONE;
136 
137     if (HdlcByteNeedsEscape(aByte))
138     {
139         VerifyOrExit(mWritePointer.CanWrite(2), error = OT_ERROR_NO_BUFS);
140 
141         IgnoreError(mWritePointer.WriteByte(kEscapeSequence));
142         IgnoreError(mWritePointer.WriteByte(aByte ^ 0x20));
143     }
144     else
145     {
146         SuccessOrExit(error = mWritePointer.WriteByte(aByte));
147     }
148 
149     mFcs = UpdateFcs(mFcs, aByte);
150 
151 exit:
152     return error;
153 }
154 
Encode(const uint8_t * aData,uint16_t aLength)155 otError Encoder::Encode(const uint8_t *aData, uint16_t aLength)
156 {
157     otError           error      = OT_ERROR_NONE;
158     uint16_t          oldFcs     = mFcs;
159     FrameWritePointer oldPointer = mWritePointer;
160 
161     while (aLength--)
162     {
163         SuccessOrExit(error = Encode(*aData++));
164     }
165 
166 exit:
167 
168     if (error != OT_ERROR_NONE)
169     {
170         mWritePointer = oldPointer;
171         mFcs          = oldFcs;
172     }
173 
174     return error;
175 }
176 
EndFrame(void)177 otError Encoder::EndFrame(void)
178 {
179     otError           error      = OT_ERROR_NONE;
180     FrameWritePointer oldPointer = mWritePointer;
181     uint16_t          oldFcs     = mFcs;
182     uint16_t          fcs        = mFcs;
183 
184     fcs ^= 0xffff;
185 
186     SuccessOrExit(error = Encode(fcs & 0xff));
187     SuccessOrExit(error = Encode(fcs >> 8));
188 
189     SuccessOrExit(error = mWritePointer.WriteByte(kFlagSequence));
190 
191 exit:
192 
193     if (error != OT_ERROR_NONE)
194     {
195         mWritePointer = oldPointer;
196         mFcs          = oldFcs;
197     }
198 
199     return error;
200 }
201 
Decoder(FrameWritePointer & aFrameWritePointer,FrameHandler aFrameHandler,void * aContext)202 Decoder::Decoder(FrameWritePointer &aFrameWritePointer, FrameHandler aFrameHandler, void *aContext)
203     : mState(kStateNoSync)
204     , mWritePointer(aFrameWritePointer)
205     , mFrameHandler(aFrameHandler)
206     , mContext(aContext)
207     , mFcs(0)
208     , mDecodedLength(0)
209 {
210 }
211 
Reset(void)212 void Decoder::Reset(void)
213 {
214     mState         = kStateNoSync;
215     mFcs           = 0;
216     mDecodedLength = 0;
217 }
218 
Decode(const uint8_t * aData,uint16_t aLength)219 void Decoder::Decode(const uint8_t *aData, uint16_t aLength)
220 {
221     while (aLength--)
222     {
223         uint8_t byte = *aData++;
224 
225         switch (mState)
226         {
227         case kStateNoSync:
228             if (byte == kFlagSequence)
229             {
230                 mState         = kStateSync;
231                 mDecodedLength = 0;
232                 mFcs           = kInitFcs;
233             }
234 
235             break;
236 
237         case kStateSync:
238             switch (byte)
239             {
240             case kEscapeSequence:
241                 mState = kStateEscaped;
242                 break;
243 
244             case kFlagSequence:
245 
246                 if (mDecodedLength > 0)
247                 {
248                     otError error = OT_ERROR_PARSE;
249 
250                     if ((mDecodedLength >= kFcsSize)
251 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
252                         && (mFcs == kGoodFcs)
253 #endif
254                     )
255                     {
256                         // Remove the FCS from the frame.
257                         mWritePointer.UndoLastWrites(kFcsSize);
258                         error = OT_ERROR_NONE;
259                     }
260 
261                     mFrameHandler(mContext, error);
262                 }
263 
264                 mDecodedLength = 0;
265                 mFcs           = kInitFcs;
266                 break;
267 
268             default:
269                 if (mWritePointer.CanWrite(sizeof(uint8_t)))
270                 {
271                     mFcs = UpdateFcs(mFcs, byte);
272                     IgnoreError(mWritePointer.WriteByte(byte));
273                     mDecodedLength++;
274                 }
275                 else
276                 {
277                     mFrameHandler(mContext, OT_ERROR_NO_BUFS);
278                     mState = kStateNoSync;
279                 }
280 
281                 break;
282             }
283 
284             break;
285 
286         case kStateEscaped:
287             if (mWritePointer.CanWrite(sizeof(uint8_t)))
288             {
289                 byte ^= 0x20;
290                 mFcs = UpdateFcs(mFcs, byte);
291                 IgnoreError(mWritePointer.WriteByte(byte));
292                 mDecodedLength++;
293                 mState = kStateSync;
294             }
295             else
296             {
297                 mFrameHandler(mContext, OT_ERROR_NO_BUFS);
298                 mState = kStateNoSync;
299             }
300 
301             break;
302         }
303     }
304 }
305 
306 } // namespace Hdlc
307 } // namespace ot
308