ltkc_framedecode.c
Go to the documentation of this file.
00001 
00002 /*
00003  ***************************************************************************
00004  *  Copyright 2007,2008 Impinj, Inc.
00005  *
00006  *  Licensed under the Apache License, Version 2.0 (the "License");
00007  *  you may not use this file except in compliance with the License.
00008  *  You may obtain a copy of the License at
00009  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  *  Unless required by applicable law or agreed to in writing, software
00013  *  distributed under the License is distributed on an "AS IS" BASIS,
00014  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  *  See the License for the specific language governing permissions and
00016  *  limitations under the License.
00017  *
00018  ***************************************************************************
00019  */
00020 
00021 
00022 #include <assert.h>
00023 
00024 #include "ltkc_platform.h"
00025 #include "ltkc_base.h"
00026 #include "ltkc_frame.h"
00027 
00028 
00029 /*
00030  * BEGIN forward decls
00031  */
00032 
00033 LLRP_tSFrameDecoder *
00034 LLRP_FrameDecoder_construct (
00035   const LLRP_tSTypeRegistry *   pTypeRegistry,
00036   unsigned char *               pBuffer,
00037   unsigned int                  nBuffer);
00038 
00039 static void
00040 decoderDestruct (
00041   LLRP_tSDecoder *              pBaseDecoder);
00042 
00043 static LLRP_tSMessage *
00044 topDecodeMessage (
00045   LLRP_tSDecoder *              pBaseDecoder);
00046 
00047 static llrp_u8_t
00048 next_u8 (
00049   LLRP_tSFrameDecoder *         pDecoder);
00050 
00051 static llrp_u16_t
00052 next_u16 (
00053   LLRP_tSFrameDecoder *         pDecoder);
00054 
00055 static llrp_u32_t
00056 next_u32 (
00057   LLRP_tSFrameDecoder *         pDecoder);
00058 
00059 static llrp_u64_t
00060 next_u64 (
00061   LLRP_tSFrameDecoder *         pDecoder);
00062 
00063 static llrp_u8_t
00064 get_u8 (
00065   LLRP_tSDecoderStream *        pBaseDecoderStream,
00066   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00067 
00068 static llrp_s8_t
00069 get_s8 (
00070   LLRP_tSDecoderStream *        pBaseDecoderStream,
00071   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00072 
00073 static llrp_u8v_t
00074 get_u8v (
00075   LLRP_tSDecoderStream *        pBaseDecoderStream,
00076   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00077 
00078 static llrp_s8v_t
00079 get_s8v (
00080   LLRP_tSDecoderStream *        pBaseDecoderStream,
00081   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00082 
00083 static llrp_u16_t
00084 get_u16 (
00085   LLRP_tSDecoderStream *        pBaseDecoderStream,
00086   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00087 
00088 static llrp_s16_t
00089 get_s16 (
00090   LLRP_tSDecoderStream *        pBaseDecoderStream,
00091   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00092 
00093 static llrp_u16v_t
00094 get_u16v (
00095   LLRP_tSDecoderStream *        pBaseDecoderStream,
00096   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00097 
00098 static llrp_s16v_t
00099 get_s16v (
00100   LLRP_tSDecoderStream *        pBaseDecoderStream,
00101   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00102 
00103 static llrp_u32_t
00104 get_u32 (
00105   LLRP_tSDecoderStream *        pBaseDecoderStream,
00106   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00107 
00108 static llrp_s32_t
00109 get_s32 (
00110   LLRP_tSDecoderStream *        pBaseDecoderStream,
00111   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00112 
00113 static llrp_u32v_t
00114 get_u32v (
00115   LLRP_tSDecoderStream *        pBaseDecoderStream,
00116   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00117 
00118 static llrp_s32v_t
00119 get_s32v (
00120   LLRP_tSDecoderStream *        pBaseDecoderStream,
00121   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00122 
00123 static llrp_u64_t
00124 get_u64 (
00125   LLRP_tSDecoderStream *        pBaseDecoderStream,
00126   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00127 
00128 static llrp_s64_t
00129 get_s64 (
00130   LLRP_tSDecoderStream *        pBaseDecoderStream,
00131   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00132 
00133 static llrp_u64v_t
00134 get_u64v (
00135   LLRP_tSDecoderStream *        pBaseDecoderStream,
00136   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00137 
00138 static llrp_s64v_t
00139 get_s64v (
00140   LLRP_tSDecoderStream *        pBaseDecoderStream,
00141   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00142 
00143 static llrp_u1_t
00144 get_u1 (
00145   LLRP_tSDecoderStream *        pBaseDecoderStream,
00146   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00147 
00148 static llrp_u1v_t
00149 get_u1v (
00150   LLRP_tSDecoderStream *        pBaseDecoderStream,
00151   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00152 
00153 static llrp_u2_t
00154 get_u2 (
00155   LLRP_tSDecoderStream *        pBaseDecoderStream,
00156   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00157 
00158 static llrp_u96_t
00159 get_u96 (
00160   LLRP_tSDecoderStream *        pBaseDecoderStream,
00161   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00162 
00163 static llrp_utf8v_t
00164 get_utf8v (
00165   LLRP_tSDecoderStream *        pBaseDecoderStream,
00166   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00167 
00168 static llrp_bytesToEnd_t
00169 get_bytesToEnd (
00170   LLRP_tSDecoderStream *        pBaseDecoderStream,
00171   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00172 
00173 static int
00174 get_e1 (
00175   LLRP_tSDecoderStream *        pBaseDecoderStream,
00176   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00177 
00178 static int
00179 get_e2 (
00180   LLRP_tSDecoderStream *        pBaseDecoderStream,
00181   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00182 
00183 static int
00184 get_e8 (
00185   LLRP_tSDecoderStream *        pBaseDecoderStream,
00186   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00187 
00188 static int
00189 get_e16 (
00190   LLRP_tSDecoderStream *        pBaseDecoderStream,
00191   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00192 
00193 static int
00194 get_e32 (
00195   LLRP_tSDecoderStream *        pBaseDecoderStream,
00196   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00197 
00198 static llrp_u8v_t
00199 get_e8v (
00200   LLRP_tSDecoderStream *        pBaseDecoderStream,
00201   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00202 
00203 static void
00204 get_reserved (
00205   LLRP_tSDecoderStream *        pBaseDecoderStream,
00206   unsigned int                  nBit);
00207 
00208 static void
00209 streamConstruct_outermost (
00210   LLRP_tSFrameDecoderStream *   pDecoderStream,
00211   LLRP_tSFrameDecoder *         pDecoder);
00212 
00213 static void
00214 streamConstruct_nested (
00215   LLRP_tSFrameDecoderStream *   pDecoderStream,
00216   LLRP_tSFrameDecoderStream *   pEnclosingDecoderStream);
00217 
00218 static LLRP_tSMessage *
00219 decodeMessage (
00220   LLRP_tSFrameDecoderStream *   pDecoderStream);
00221 
00222 static LLRP_tSParameter *
00223 decodeParameter (
00224   LLRP_tSFrameDecoderStream *   pDecoderStream);
00225 
00226 static unsigned int
00227 getRemainingByteCount (
00228   LLRP_tSFrameDecoderStream *   pDecoderStream);
00229 
00230 static llrp_bool_t
00231 checkAvailable (
00232   LLRP_tSFrameDecoderStream *   pDecoderStream,
00233   unsigned int                  nByte,
00234   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00235 
00236 static unsigned int
00237 getBitField (
00238   LLRP_tSFrameDecoderStream *   pDecoderStream,
00239   unsigned int                  nBit,
00240   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00241 
00242 static llrp_u16_t
00243 getVarlenCount (
00244   LLRP_tSFrameDecoderStream *   pDecoderStream,
00245   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00246 
00247 static llrp_bool_t
00248 verifyVectorAllocation (
00249   LLRP_tSFrameDecoderStream *   pDecoderStream,
00250   const void *                  pValue,
00251   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00252 
00253 /*
00254  * END forward decls
00255  */
00256 
00257 
00258 
00259 static LLRP_tSDecoderOps
00260 s_FrameDecoderOps =
00261 {
00262     .pfDestruct             = decoderDestruct,
00263     .pfDecodeMessage        = topDecodeMessage,
00264 };
00265 
00266 static LLRP_tSDecoderStreamOps
00267 s_FrameDecoderStreamOps =
00268 {
00269     .pfGet_u8               = get_u8,
00270     .pfGet_s8               = get_s8,
00271     .pfGet_u8v              = get_u8v,
00272     .pfGet_s8v              = get_s8v,
00273 
00274     .pfGet_u16              = get_u16,
00275     .pfGet_s16              = get_s16,
00276     .pfGet_u16v             = get_u16v,
00277     .pfGet_s16v             = get_s16v,
00278 
00279     .pfGet_u32              = get_u32,
00280     .pfGet_s32              = get_s32,
00281     .pfGet_u32v             = get_u32v,
00282     .pfGet_s32v             = get_s32v,
00283 
00284     .pfGet_u64              = get_u64,
00285     .pfGet_s64              = get_s64,
00286     .pfGet_u64v             = get_u64v,
00287     .pfGet_s64v             = get_s64v,
00288 
00289     .pfGet_u1               = get_u1,
00290     .pfGet_u1v              = get_u1v,
00291     .pfGet_u2               = get_u2,
00292     .pfGet_u96              = get_u96,
00293     .pfGet_utf8v            = get_utf8v,
00294     .pfGet_bytesToEnd       = get_bytesToEnd,
00295 
00296     .pfGet_e1               = get_e1,
00297     .pfGet_e2               = get_e2,
00298     .pfGet_e8               = get_e8,
00299     .pfGet_e16              = get_e16,
00300     .pfGet_e32              = get_e32,
00301     .pfGet_e8v              = get_e8v,
00302 
00303     .pfGet_reserved         = get_reserved,
00304 };
00305 
00306 LLRP_tSFrameDecoder *
00307 LLRP_FrameDecoder_construct (
00308   const LLRP_tSTypeRegistry *   pTypeRegistry,
00309   unsigned char *               pBuffer,
00310   unsigned int                  nBuffer)
00311 {
00312     LLRP_tSFrameDecoder *       pDecoder;
00313 
00314     pDecoder = malloc(sizeof *pDecoder);
00315     if(NULL == pDecoder)
00316     {
00317         return pDecoder;
00318     }
00319 
00320     memset(pDecoder, 0, sizeof *pDecoder);
00321 
00322     pDecoder->decoderHdr.pDecoderOps = &s_FrameDecoderOps;
00323     pDecoder->decoderHdr.pRegistry = pTypeRegistry;
00324 
00325     pDecoder->pBuffer        = pBuffer;
00326     pDecoder->nBuffer        = nBuffer;
00327 
00328     pDecoder->iNext          = 0;
00329     pDecoder->BitFieldBuffer = 0;
00330     pDecoder->nBitFieldResid = 0;
00331 
00332     return pDecoder;
00333 }
00334 
00335 static void
00336 decoderDestruct (
00337   LLRP_tSDecoder *              pBaseDecoder)
00338 {
00339     LLRP_tSFrameDecoder *       pDecoder = (LLRP_tSFrameDecoder*)pBaseDecoder;
00340 
00341     free(pDecoder);
00342 }
00343 
00344 LLRP_tSMessage *
00345 topDecodeMessage (
00346   LLRP_tSDecoder *              pBaseDecoder)
00347 {
00348     LLRP_tSFrameDecoder *       pDecoder;
00349     LLRP_tSFrameDecoderStream   DecoderStream;
00350     LLRP_tSMessage *            pMessage;
00351 
00352     pDecoder = (LLRP_tSFrameDecoder *) pBaseDecoder;
00353 
00354     streamConstruct_outermost(&DecoderStream, pDecoder);
00355 
00356     pMessage = decodeMessage(&DecoderStream);
00357 
00358     return pMessage;
00359 }
00360 
00361 static llrp_u8_t
00362 next_u8 (
00363   LLRP_tSFrameDecoder *         pDecoder)
00364 {
00365     llrp_u8_t                   Value;
00366 
00367     assert(pDecoder->iNext + 1u <= pDecoder->nBuffer);
00368 
00369     Value = pDecoder->pBuffer[pDecoder->iNext++];
00370 
00371     return Value;
00372 }
00373 
00374 static llrp_u16_t
00375 next_u16 (
00376   LLRP_tSFrameDecoder *         pDecoder)
00377 {
00378     llrp_u16_t                  Value;
00379 
00380     assert(pDecoder->iNext + 2u <= pDecoder->nBuffer);
00381 
00382     Value = pDecoder->pBuffer[pDecoder->iNext++];
00383     Value <<= 8u;
00384     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00385 
00386     return Value;
00387 }
00388 
00389 static llrp_u32_t
00390 next_u32 (
00391   LLRP_tSFrameDecoder *         pDecoder)
00392 {
00393     llrp_u32_t                  Value;
00394 
00395     assert(pDecoder->iNext + 4u <= pDecoder->nBuffer);
00396 
00397     Value = pDecoder->pBuffer[pDecoder->iNext++];
00398     Value <<= 8u;
00399     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00400     Value <<= 8u;
00401     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00402     Value <<= 8u;
00403     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00404 
00405     return Value;
00406 }
00407 
00408 static llrp_u64_t
00409 next_u64 (
00410   LLRP_tSFrameDecoder *         pDecoder)
00411 {
00412     llrp_u64_t                  Value;
00413 
00414     assert(pDecoder->iNext + 8u <= pDecoder->nBuffer);
00415 
00416     Value = pDecoder->pBuffer[pDecoder->iNext++];
00417     Value <<= 8u;
00418     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00419     Value <<= 8u;
00420     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00421     Value <<= 8u;
00422     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00423 
00424     Value <<= 8u;
00425     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00426     Value <<= 8u;
00427     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00428     Value <<= 8u;
00429     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00430     Value <<= 8u;
00431     Value |= pDecoder->pBuffer[pDecoder->iNext++];
00432 
00433     return Value;
00434 }
00435 
00436 
00437 static llrp_u8_t
00438 get_u8 (
00439   LLRP_tSDecoderStream *        pBaseDecoderStream,
00440   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00441 {
00442     LLRP_tSFrameDecoderStream * pDecoderStream;
00443     llrp_u8_t                   Value;
00444 
00445     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00446 
00447     if(checkAvailable(pDecoderStream, 1u, pFieldDescriptor))
00448     {
00449         Value = next_u8(pDecoderStream->pDecoder);
00450     }
00451     else
00452     {
00453         Value = 0;
00454     }
00455 
00456     return Value;
00457 }
00458 
00459 static llrp_s8_t
00460 get_s8 (
00461   LLRP_tSDecoderStream *        pBaseDecoderStream,
00462   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00463 {
00464     LLRP_tSFrameDecoderStream * pDecoderStream;
00465     llrp_s8_t                   Value;
00466 
00467     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00468 
00469     if(checkAvailable(pDecoderStream, 1u, pFieldDescriptor))
00470     {
00471         Value = next_u8(pDecoderStream->pDecoder);
00472     }
00473     else
00474     {
00475         Value = 0;
00476     }
00477 
00478     return Value;
00479 }
00480 
00481 static llrp_u8v_t
00482 get_u8v (
00483   LLRP_tSDecoderStream *        pBaseDecoderStream,
00484   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00485 {
00486     LLRP_tSFrameDecoderStream * pDecoderStream;
00487     llrp_u16_t                  nValue;
00488     llrp_u8v_t                  Value;
00489 
00490     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00491     memset(&Value, 0, sizeof Value);
00492 
00493     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
00494 
00495     if(0 < nValue)
00496     {
00497         if(checkAvailable(pDecoderStream, 1u * nValue, pFieldDescriptor))
00498         {
00499             Value = LLRP_u8v_construct(nValue);
00500             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00501                                 pFieldDescriptor))
00502             {
00503                 unsigned int    Ix;
00504 
00505                 for(Ix = 0; Ix < nValue; Ix++)
00506                 {
00507                     Value.pValue[Ix] = next_u8(pDecoderStream->pDecoder);
00508                 }
00509             }
00510         }
00511     }
00512 
00513     return Value;
00514 }
00515 
00516 static llrp_s8v_t
00517 get_s8v (
00518   LLRP_tSDecoderStream *        pBaseDecoderStream,
00519   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00520 {
00521     LLRP_tSFrameDecoderStream * pDecoderStream;
00522     llrp_u16_t                  nValue;
00523     llrp_s8v_t                  Value;
00524 
00525     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00526     memset(&Value, 0, sizeof Value);
00527 
00528     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
00529 
00530     if(0 < nValue)
00531     {
00532         if(checkAvailable(pDecoderStream, 1u * nValue, pFieldDescriptor))
00533         {
00534             Value = LLRP_s8v_construct(nValue);
00535             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00536                                 pFieldDescriptor))
00537             {
00538                 unsigned int    Ix;
00539 
00540                 for(Ix = 0; Ix < nValue; Ix++)
00541                 {
00542                     Value.pValue[Ix] = next_u8(pDecoderStream->pDecoder);
00543                 }
00544             }
00545         }
00546     }
00547 
00548     return Value;
00549 }
00550 
00551 static llrp_u16_t
00552 get_u16 (
00553   LLRP_tSDecoderStream *        pBaseDecoderStream,
00554   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00555 {
00556     LLRP_tSFrameDecoderStream * pDecoderStream;
00557     llrp_u16_t                  Value;
00558 
00559     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00560 
00561     if(checkAvailable(pDecoderStream, 2u, pFieldDescriptor))
00562     {
00563         Value = next_u16(pDecoderStream->pDecoder);
00564     }
00565     else
00566     {
00567         Value = 0;
00568     }
00569 
00570     return Value;
00571 }
00572 
00573 static llrp_s16_t
00574 get_s16 (
00575   LLRP_tSDecoderStream *        pBaseDecoderStream,
00576   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00577 {
00578     LLRP_tSFrameDecoderStream * pDecoderStream;
00579     llrp_s16_t                  Value;
00580 
00581     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00582 
00583     if(checkAvailable(pDecoderStream, 2u, pFieldDescriptor))
00584     {
00585         Value = next_u16(pDecoderStream->pDecoder);
00586     }
00587     else
00588     {
00589         Value = 0;
00590     }
00591 
00592     return Value;
00593 }
00594 
00595 static llrp_u16v_t
00596 get_u16v (
00597   LLRP_tSDecoderStream *        pBaseDecoderStream,
00598   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00599 {
00600     LLRP_tSFrameDecoderStream * pDecoderStream;
00601     llrp_u16_t                  nValue;
00602     llrp_u16v_t                 Value;
00603 
00604     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00605     memset(&Value, 0, sizeof Value);
00606 
00607     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
00608 
00609     if(0 < nValue)
00610     {
00611         if(checkAvailable(pDecoderStream, 2u * nValue, pFieldDescriptor))
00612         {
00613             Value = LLRP_u16v_construct(nValue);
00614             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00615                                 pFieldDescriptor))
00616             {
00617                 unsigned int    Ix;
00618 
00619                 for(Ix = 0; Ix < nValue; Ix++)
00620                 {
00621                     Value.pValue[Ix] = next_u16(pDecoderStream->pDecoder);
00622                 }
00623             }
00624         }
00625     }
00626 
00627     return Value;
00628 }
00629 
00630 static llrp_s16v_t
00631 get_s16v (
00632   LLRP_tSDecoderStream *        pBaseDecoderStream,
00633   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00634 {
00635     LLRP_tSFrameDecoderStream * pDecoderStream;
00636     llrp_u16_t                  nValue;
00637     llrp_s16v_t                 Value;
00638 
00639     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00640     memset(&Value, 0, sizeof Value);
00641 
00642     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
00643 
00644     if(0 < nValue)
00645     {
00646         if(checkAvailable(pDecoderStream, 2u * nValue, pFieldDescriptor))
00647         {
00648             Value = LLRP_s16v_construct(nValue);
00649             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00650                                 pFieldDescriptor))
00651             {
00652                 unsigned int    Ix;
00653 
00654                 for(Ix = 0; Ix < nValue; Ix++)
00655                 {
00656                     Value.pValue[Ix] = next_u16(pDecoderStream->pDecoder);
00657                 }
00658             }
00659         }
00660     }
00661 
00662     return Value;
00663 }
00664 
00665 static llrp_u32_t
00666 get_u32 (
00667   LLRP_tSDecoderStream *        pBaseDecoderStream,
00668   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00669 {
00670     LLRP_tSFrameDecoderStream * pDecoderStream;
00671     llrp_u32_t                  Value;
00672 
00673     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00674 
00675     if(checkAvailable(pDecoderStream, 4u, pFieldDescriptor))
00676     {
00677         Value = next_u32(pDecoderStream->pDecoder);
00678     }
00679     else
00680     {
00681         Value = 0;
00682     }
00683 
00684     return Value;
00685 }
00686 
00687 static llrp_s32_t
00688 get_s32 (
00689   LLRP_tSDecoderStream *        pBaseDecoderStream,
00690   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00691 {
00692     LLRP_tSFrameDecoderStream * pDecoderStream;
00693     llrp_s32_t                  Value;
00694 
00695     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00696 
00697     if(checkAvailable(pDecoderStream, 4u, pFieldDescriptor))
00698     {
00699         Value = next_u32(pDecoderStream->pDecoder);
00700     }
00701     else
00702     {
00703         Value = 0;
00704     }
00705 
00706     return Value;
00707 }
00708 
00709 static llrp_u32v_t
00710 get_u32v (
00711   LLRP_tSDecoderStream *        pBaseDecoderStream,
00712   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00713 {
00714     LLRP_tSFrameDecoderStream * pDecoderStream;
00715     llrp_u16_t                  nValue;
00716     llrp_u32v_t                 Value;
00717 
00718     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00719     memset(&Value, 0, sizeof Value);
00720 
00721     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
00722 
00723     if(0 < nValue)
00724     {
00725         if(checkAvailable(pDecoderStream, 4u * nValue, pFieldDescriptor))
00726         {
00727             Value = LLRP_u32v_construct(nValue);
00728             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00729                                 pFieldDescriptor))
00730             {
00731                 unsigned int    Ix;
00732 
00733                 for(Ix = 0; Ix < nValue; Ix++)
00734                 {
00735                     Value.pValue[Ix] = next_u32(pDecoderStream->pDecoder);
00736                 }
00737             }
00738         }
00739     }
00740 
00741     return Value;
00742 }
00743 
00744 static llrp_s32v_t
00745 get_s32v (
00746   LLRP_tSDecoderStream *        pBaseDecoderStream,
00747   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00748 {
00749     LLRP_tSFrameDecoderStream * pDecoderStream;
00750     llrp_u16_t                  nValue;
00751     llrp_s32v_t                 Value;
00752 
00753     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00754     memset(&Value, 0, sizeof Value);
00755 
00756     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
00757 
00758     if(0 < nValue)
00759     {
00760         if(checkAvailable(pDecoderStream, 4u * nValue, pFieldDescriptor))
00761         {
00762             Value = LLRP_s32v_construct(nValue);
00763             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00764                                 pFieldDescriptor))
00765             {
00766                 unsigned int    Ix;
00767 
00768                 for(Ix = 0; Ix < nValue; Ix++)
00769                 {
00770                     Value.pValue[Ix] = next_u32(pDecoderStream->pDecoder);
00771                 }
00772             }
00773         }
00774     }
00775 
00776     return Value;
00777 }
00778 
00779 static llrp_u64_t
00780 get_u64 (
00781   LLRP_tSDecoderStream *        pBaseDecoderStream,
00782   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00783 {
00784     LLRP_tSFrameDecoderStream * pDecoderStream;
00785     llrp_u64_t                  Value;
00786 
00787     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00788 
00789     if(checkAvailable(pDecoderStream, 8u, pFieldDescriptor))
00790     {
00791         Value = next_u64(pDecoderStream->pDecoder);
00792     }
00793     else
00794     {
00795         Value = 0;
00796     }
00797 
00798     return Value;
00799 }
00800 
00801 static llrp_s64_t
00802 get_s64 (
00803   LLRP_tSDecoderStream *        pBaseDecoderStream,
00804   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00805 {
00806     LLRP_tSFrameDecoderStream * pDecoderStream;
00807     llrp_s64_t                  Value;
00808 
00809     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00810 
00811     if(checkAvailable(pDecoderStream, 8u, pFieldDescriptor))
00812     {
00813         Value = next_u64(pDecoderStream->pDecoder);
00814     }
00815     else
00816     {
00817         Value = 0;
00818     }
00819 
00820     return Value;
00821 }
00822 
00823 static llrp_u64v_t
00824 get_u64v (
00825   LLRP_tSDecoderStream *        pBaseDecoderStream,
00826   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00827 {
00828     LLRP_tSFrameDecoderStream * pDecoderStream;
00829     llrp_u16_t                  nValue;
00830     llrp_u64v_t                 Value;
00831 
00832     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00833     memset(&Value, 0, sizeof Value);
00834 
00835     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
00836 
00837     if(0 < nValue)
00838     {
00839         if(checkAvailable(pDecoderStream, 8u * nValue, pFieldDescriptor))
00840         {
00841             Value = LLRP_u64v_construct(nValue);
00842             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00843                                 pFieldDescriptor))
00844             {
00845                 unsigned int    Ix;
00846 
00847                 for(Ix = 0; Ix < nValue; Ix++)
00848                 {
00849                     Value.pValue[Ix] = next_u64(pDecoderStream->pDecoder);
00850                 }
00851             }
00852         }
00853     }
00854 
00855     return Value;
00856 }
00857 
00858 static llrp_s64v_t
00859 get_s64v (
00860   LLRP_tSDecoderStream *        pBaseDecoderStream,
00861   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00862 {
00863     LLRP_tSFrameDecoderStream * pDecoderStream;
00864     llrp_u16_t                  nValue;
00865     llrp_s64v_t                 Value;
00866 
00867     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00868     memset(&Value, 0, sizeof Value);
00869 
00870     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
00871 
00872     if(0 < nValue)
00873     {
00874         if(checkAvailable(pDecoderStream, 8u * nValue, pFieldDescriptor))
00875         {
00876             Value = LLRP_s64v_construct(nValue);
00877             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00878                                 pFieldDescriptor))
00879             {
00880                 unsigned int    Ix;
00881 
00882                 for(Ix = 0; Ix < nValue; Ix++)
00883                 {
00884                     Value.pValue[Ix] = next_u64(pDecoderStream->pDecoder);
00885                 }
00886             }
00887         }
00888     }
00889 
00890     return Value;
00891 }
00892 
00893 static llrp_u1_t
00894 get_u1 (
00895   LLRP_tSDecoderStream *        pBaseDecoderStream,
00896   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00897 {
00898     LLRP_tSFrameDecoderStream * pDecoderStream;
00899     llrp_u1_t                   Value;
00900 
00901     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00902 
00903     Value = getBitField(pDecoderStream, 1, pFieldDescriptor);
00904 
00905     return Value;
00906 }
00907 
00908 static llrp_u1v_t
00909 get_u1v (
00910   LLRP_tSDecoderStream *        pBaseDecoderStream,
00911   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00912 {
00913     LLRP_tSFrameDecoderStream * pDecoderStream;
00914     llrp_u16_t                  nBit;
00915     llrp_u1v_t                  Value;
00916 
00917     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00918     memset(&Value, 0, sizeof Value);
00919 
00920     nBit = getVarlenCount(pDecoderStream, pFieldDescriptor);
00921 
00922     if(0 < nBit)
00923     {
00924         unsigned int            nByte = (nBit + 7u) / 8u;
00925 
00926         if(checkAvailable(pDecoderStream, nByte, pFieldDescriptor))
00927         {
00928             Value = LLRP_u1v_construct(nBit);
00929             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
00930                                 pFieldDescriptor))
00931             {
00932                 unsigned int    Ix;
00933 
00934                 for(Ix = 0; Ix < nByte; Ix++)
00935                 {
00936                     Value.pValue[Ix] = next_u8(pDecoderStream->pDecoder);
00937                 }
00938             }
00939         }
00940     }
00941 
00942     return Value;
00943 }
00944 
00945 static llrp_u2_t
00946 get_u2 (
00947   LLRP_tSDecoderStream *        pBaseDecoderStream,
00948   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00949 {
00950     LLRP_tSFrameDecoderStream * pDecoderStream;
00951     llrp_u2_t                   Value;
00952 
00953     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00954 
00955     Value = getBitField(pDecoderStream, 2, pFieldDescriptor);
00956 
00957     return Value;
00958 }
00959 
00960 static llrp_u96_t
00961 get_u96 (
00962   LLRP_tSDecoderStream *        pBaseDecoderStream,
00963   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00964 {
00965     LLRP_tSFrameDecoderStream * pDecoderStream;
00966     llrp_u96_t                  Value;
00967 
00968     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00969 
00970     if(checkAvailable(pDecoderStream, 12u, pFieldDescriptor))
00971     {
00972         unsigned int            Ix;
00973 
00974         for(Ix = 0; Ix < 12u; Ix++)
00975         {
00976             Value.aValue[Ix] = next_u8(pDecoderStream->pDecoder);
00977         }
00978     }
00979     else
00980     {
00981         memset(&Value, 0, sizeof Value);
00982     }
00983 
00984     return Value;
00985 }
00986 
00987 static llrp_utf8v_t
00988 get_utf8v (
00989   LLRP_tSDecoderStream *        pBaseDecoderStream,
00990   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00991 {
00992     LLRP_tSFrameDecoderStream * pDecoderStream;
00993     llrp_u16_t                  nValue;
00994     llrp_utf8v_t                Value;
00995 
00996     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
00997     memset(&Value, 0, sizeof Value);
00998 
00999     nValue = getVarlenCount(pDecoderStream, pFieldDescriptor);
01000 
01001     if(0 < nValue)
01002     {
01003         if(checkAvailable(pDecoderStream, 1u * nValue, pFieldDescriptor))
01004         {
01005             Value = LLRP_utf8v_construct(nValue);
01006             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
01007                                 pFieldDescriptor))
01008             {
01009                 unsigned int    Ix;
01010 
01011                 for(Ix = 0; Ix < nValue; Ix++)
01012                 {
01013                     Value.pValue[Ix] = next_u8(pDecoderStream->pDecoder);
01014                 }
01015             }
01016         }
01017     }
01018 
01019     return Value;
01020 }
01021 
01022 static llrp_bytesToEnd_t
01023 get_bytesToEnd (
01024   LLRP_tSDecoderStream *        pBaseDecoderStream,
01025   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01026 {
01027     LLRP_tSFrameDecoderStream * pDecoderStream;
01028     llrp_u16_t                  nValue;
01029     llrp_bytesToEnd_t           Value;
01030 
01031     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
01032     memset(&Value, 0, sizeof Value);
01033 
01034     nValue = getRemainingByteCount(pDecoderStream);
01035 
01036     if(0 < nValue)
01037     {
01038         if(checkAvailable(pDecoderStream, 1u * nValue, pFieldDescriptor))
01039         {
01040             Value = LLRP_bytesToEnd_construct(nValue);
01041             if(verifyVectorAllocation(pDecoderStream, Value.pValue,
01042                                 pFieldDescriptor))
01043             {
01044                 unsigned int    Ix;
01045 
01046                 for(Ix = 0; Ix < nValue; Ix++)
01047                 {
01048                     Value.pValue[Ix] = next_u8(pDecoderStream->pDecoder);
01049                 }
01050             }
01051         }
01052     }
01053 
01054     return Value;
01055 }
01056 
01057 static int
01058 get_e1 (
01059   LLRP_tSDecoderStream *        pBaseDecoderStream,
01060   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01061 {
01062     int                         eValue;
01063 
01064     eValue = (int)get_u1(pBaseDecoderStream, pFieldDescriptor);
01065 
01066     return eValue;
01067 }
01068 
01069 static int
01070 get_e2 (
01071   LLRP_tSDecoderStream *        pBaseDecoderStream,
01072   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01073 {
01074     int                         eValue;
01075 
01076     eValue = (int)get_u2(pBaseDecoderStream, pFieldDescriptor);
01077 
01078     return eValue;
01079 }
01080 
01081 static int
01082 get_e8 (
01083   LLRP_tSDecoderStream *        pBaseDecoderStream,
01084   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01085 {
01086     int                         eValue;
01087 
01088     eValue = (int)get_u8(pBaseDecoderStream, pFieldDescriptor);
01089 
01090     return eValue;
01091 }
01092 
01093 static int
01094 get_e16 (
01095   LLRP_tSDecoderStream *        pBaseDecoderStream,
01096   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01097 {
01098     int                         eValue;
01099 
01100     eValue = (int)get_u16(pBaseDecoderStream, pFieldDescriptor);
01101 
01102     return eValue;
01103 }
01104 
01105 static int
01106 get_e32 (
01107   LLRP_tSDecoderStream *        pBaseDecoderStream,
01108   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01109 {
01110     int                         eValue;
01111 
01112     eValue = (int)get_u32(pBaseDecoderStream, pFieldDescriptor);
01113 
01114     return eValue;
01115 }
01116 
01117 static llrp_u8v_t
01118 get_e8v (
01119   LLRP_tSDecoderStream *        pBaseDecoderStream,
01120   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01121 {
01122     return get_u8v(pBaseDecoderStream, pFieldDescriptor);
01123 }
01124 
01125 static void
01126 get_reserved (
01127   LLRP_tSDecoderStream *        pBaseDecoderStream,
01128   unsigned int                  nBit)
01129 {
01130     LLRP_tSFrameDecoderStream * pDecoderStream;
01131     LLRP_tSFrameDecoder *       pDecoder;
01132     LLRP_tSErrorDetails *       pError;
01133 
01134     pDecoderStream = (LLRP_tSFrameDecoderStream *) pBaseDecoderStream;
01135     pDecoder = pDecoderStream->pDecoder;
01136     pError = &pDecoder->decoderHdr.ErrorDetails;
01137 
01138     if(LLRP_RC_OK != pError->eResultCode)
01139     {
01140         return;
01141     }
01142 
01143     while(0 < nBit)
01144     {
01145         unsigned int            Step = 7u & nBit;
01146 
01147         if(0 != pDecoder->nBitFieldResid)
01148         {
01149             if(Step != pDecoder->nBitFieldResid)
01150             {
01151                 pError->eResultCode = LLRP_RC_UnalignedReservedBits;
01152                 pError->pWhatStr    = "unaligned reserved bits";
01153                 pError->pRefType    = pDecoderStream->pRefType;
01154                 pError->pRefField   = NULL;
01155                 pError->OtherDetail = pDecoder->iNext;
01156                 return;
01157             }
01158 
01159             nBit -= Step;
01160             pDecoder->nBitFieldResid = 0;
01161         }
01162         else
01163         {
01164             if(0 != Step)
01165             {
01166                 pError->eResultCode = LLRP_RC_UnalignedReservedBits;
01167                 pError->pWhatStr    = "unaligned reserved bits";
01168                 pError->pRefType    = pDecoderStream->pRefType;
01169                 pError->pRefField   = NULL;
01170                 pError->OtherDetail = pDecoder->iNext;
01171                 return;
01172             }
01173 
01174             if(pDecoder->iNext >= pDecoderStream->iLimit)
01175             {
01176                 pError->eResultCode = LLRP_RC_ReservedBitsUnderrun;
01177                 pError->pWhatStr    = "underrun at reserved bits";
01178                 pError->pRefType    = pDecoderStream->pRefType;
01179                 pError->pRefField   = NULL;
01180                 pError->OtherDetail = pDecoder->iNext;
01181                 return;
01182             }
01183 
01184             next_u8(pDecoder);
01185             nBit -= 8;
01186         }
01187     }
01188 }
01189 
01190 static void
01191 streamConstruct_outermost (
01192   LLRP_tSFrameDecoderStream *   pDecoderStream,
01193   LLRP_tSFrameDecoder *         pDecoder)
01194 {
01195     memset(pDecoderStream, 0, sizeof *pDecoderStream);
01196     pDecoderStream->decoderStreamHdr.pDecoderStreamOps =
01197                                 &s_FrameDecoderStreamOps;
01198 
01199     pDecoderStream->pDecoder                = pDecoder;
01200     pDecoderStream->pEnclosingDecoderStream = NULL;
01201     pDecoderStream->pRefType                = NULL;
01202     pDecoderStream->iBegin                  = pDecoder->iNext;
01203     pDecoderStream->iLimit                  = pDecoder->nBuffer;
01204 }
01205 
01206 static void
01207 streamConstruct_nested (
01208   LLRP_tSFrameDecoderStream *   pDecoderStream,
01209   LLRP_tSFrameDecoderStream *   pEnclosingDecoderStream)
01210 {
01211     LLRP_tSFrameDecoder *       pDecoder;
01212 
01213     pDecoder = pEnclosingDecoderStream->pDecoder;
01214 
01215     memset(pDecoderStream, 0, sizeof *pDecoderStream);
01216     pDecoderStream->decoderStreamHdr.pDecoderStreamOps =
01217                                 &s_FrameDecoderStreamOps;
01218 
01219     pDecoderStream->pDecoder                = pDecoder;
01220     pDecoderStream->pEnclosingDecoderStream = pEnclosingDecoderStream;
01221     pDecoderStream->pRefType                = NULL;
01222     pDecoderStream->iBegin                  = pDecoder->iNext;
01223     pDecoderStream->iLimit                  = pEnclosingDecoderStream->iLimit;
01224 }
01225 
01226 static LLRP_tSMessage *
01227 decodeMessage (
01228   LLRP_tSFrameDecoderStream *   pDecoderStream)
01229 {
01230     LLRP_tSFrameDecoder *       pDecoder  = pDecoderStream->pDecoder;
01231     LLRP_tSErrorDetails *       pError    = &pDecoder->decoderHdr.ErrorDetails;
01232     const LLRP_tSTypeRegistry * pRegistry = pDecoder->decoderHdr.pRegistry;
01233     LLRP_tSDecoderStream *      pBaseDecoderStream =
01234                                         &pDecoderStream->decoderStreamHdr;
01235     const LLRP_tSTypeDescriptor *pTypeDescriptor;
01236     llrp_u16_t                  Type;
01237     llrp_u16_t                  Vers;
01238     llrp_u32_t                  nLength;
01239     unsigned int                iLimit;
01240     llrp_u32_t                  MessageID;
01241     LLRP_tSElement *            pElement;
01242     LLRP_tSMessage *            pMessage;
01243 
01244     if(LLRP_RC_OK != pError->eResultCode)
01245     {
01246         return NULL;
01247     }
01248 
01249     Type = get_u16(pBaseDecoderStream, &LLRP_g_fdMessageHeader_Type);
01250     Vers = (Type >> 10u) & 3;
01251     Type &= 0x3FF;
01252 
01253     if(LLRP_RC_OK != pError->eResultCode)
01254     {
01255         return NULL;
01256     }
01257 
01258     if(1u != Vers)
01259     {
01260         pError->eResultCode = LLRP_RC_BadVersion;
01261         pError->pWhatStr    = "unsupported version";
01262         pError->pRefType    = NULL;
01263         pError->pRefField   = &LLRP_g_fdMessageHeader_Type;
01264         pError->OtherDetail = pDecoder->iNext;
01265         return NULL;
01266     }
01267 
01268     nLength = get_u32(pBaseDecoderStream, &LLRP_g_fdMessageHeader_Length);
01269 
01270     if(LLRP_RC_OK != pError->eResultCode)
01271     {
01272         return NULL;
01273     }
01274 
01275     if(10u > nLength)
01276     {
01277         pError->eResultCode = LLRP_RC_InvalidLength;
01278         pError->pWhatStr    = "message length too small";
01279         pError->pRefType    = NULL;
01280         pError->pRefField   = &LLRP_g_fdMessageHeader_Length;
01281         pError->OtherDetail = pDecoder->iNext;
01282         return NULL;
01283     }
01284 
01285     iLimit = pDecoderStream->iBegin + nLength;
01286 
01287     if(iLimit > pDecoderStream->iLimit)
01288     {
01289         pError->eResultCode = LLRP_RC_ExcessiveLength;
01290         pError->pWhatStr    = "message length exceeds enclosing length";
01291         pError->pRefType    = NULL;
01292         pError->pRefField   = &LLRP_g_fdMessageHeader_Length;
01293         pError->OtherDetail = pDecoder->iNext;
01294         return NULL;
01295     }
01296 
01297     pDecoderStream->iLimit = iLimit;
01298 
01299     MessageID = get_u32(pBaseDecoderStream, &LLRP_g_fdMessageHeader_MessageID);
01300 
01301     if(LLRP_RC_OK != pError->eResultCode)
01302     {
01303         return NULL;
01304     }
01305 
01306     /* Custom? */
01307     if(1023u == Type)
01308     {
01309         llrp_u32_t              VendorPEN;
01310         llrp_u8_t               Subtype;
01311 
01312         VendorPEN = get_u32(pBaseDecoderStream,
01313                             &LLRP_g_fdMessageHeader_VendorPEN);
01314         Subtype   = get_u8(pBaseDecoderStream,
01315                             &LLRP_g_fdMessageHeader_Subtype);
01316 
01317         if(LLRP_RC_OK != pError->eResultCode)
01318         {
01319             return NULL;
01320         }
01321 
01322         pTypeDescriptor = LLRP_TypeRegistry_lookupCustomMessage(pRegistry,
01323             VendorPEN, Subtype);
01324         if(NULL == pTypeDescriptor)
01325         {
01326             /*
01327              * If we don't have a definition for a particular
01328              * CUSTOM message, just use the generic one.
01329              */
01330             pDecoder->iNext -= 5;       /* back up to VendorPEN and SubType */
01331             pTypeDescriptor = LLRP_TypeRegistry_lookupMessage(pRegistry, Type);
01332         }
01333     }
01334     else
01335     {
01336         pTypeDescriptor = LLRP_TypeRegistry_lookupMessage(pRegistry, Type);
01337     }
01338 
01339     if(NULL == pTypeDescriptor)
01340     {
01341         pError->eResultCode = LLRP_RC_UnknownMessageType;
01342         pError->pWhatStr    = "unknown message type";
01343         pError->pRefType    = NULL;
01344         pError->pRefField   = &LLRP_g_fdMessageHeader_Type;
01345         pError->OtherDetail = 0;
01346         return NULL;
01347     }
01348 
01349     pDecoderStream->pRefType = pTypeDescriptor;
01350 
01351     pElement = LLRP_Element_construct(pTypeDescriptor);
01352 
01353     if(NULL == pElement)
01354     {
01355         pError->eResultCode = LLRP_RC_MessageAllocationFailed;
01356         pError->pWhatStr    = "message allocation failed";
01357         pError->pRefType    = pTypeDescriptor;
01358         pError->pRefField   = NULL;
01359         pError->OtherDetail = pDecoder->iNext;
01360         return NULL;
01361     }
01362 
01363     pMessage = (LLRP_tSMessage *) pElement;
01364     pMessage->MessageID = MessageID;
01365 
01366     pTypeDescriptor->pfDecodeFields(pElement, pBaseDecoderStream);
01367 
01368     if(LLRP_RC_OK != pError->eResultCode)
01369     {
01370         LLRP_Element_destruct(pElement);
01371         return NULL;
01372     }
01373 
01374     /*
01375      * Subparameters
01376      */
01377     while(0 < getRemainingByteCount(pDecoderStream) &&
01378           LLRP_RC_OK == pError->eResultCode)
01379     {
01380         LLRP_tSFrameDecoderStream       NestStream;
01381         LLRP_tSParameter *              pParameter;
01382 
01383         streamConstruct_nested(&NestStream, pDecoderStream);
01384 
01385         pParameter = decodeParameter(&NestStream);
01386 
01387         if(NULL == pParameter)
01388         {
01389             if(LLRP_RC_OK == pError->eResultCode)
01390             {
01391                 pError->eResultCode = LLRP_RC_Botch;
01392                 pError->pWhatStr    = "botch -- no param and no error";
01393                 pError->pRefType    = pTypeDescriptor;
01394                 pError->pRefField   = NULL;
01395                 pError->OtherDetail = pDecoder->iNext;
01396             }
01397             break;
01398         }
01399 
01400         pParameter->elementHdr.pParent = pElement;
01401         LLRP_Element_addSubParameterToAllList(pElement, pParameter);
01402     }
01403 
01404     if(LLRP_RC_OK == pError->eResultCode)
01405     {
01406         if(pDecoder->iNext != pDecoderStream->iLimit)
01407         {
01408             pError->eResultCode = LLRP_RC_ExtraBytes;
01409             pError->pWhatStr    = "extra bytes at end of message";
01410             pError->pRefType    = pTypeDescriptor;
01411             pError->pRefField   = NULL;
01412             pError->OtherDetail = pDecoder->iNext;
01413         }
01414     }
01415 
01416     if(LLRP_RC_OK != pError->eResultCode)
01417     {
01418         LLRP_Element_destruct(pElement);
01419         return NULL;
01420     }
01421 
01422     pTypeDescriptor->pfAssimilateSubParameters(pElement, pError);
01423 
01424     if(LLRP_RC_OK != pError->eResultCode)
01425     {
01426         LLRP_Element_destruct(pElement);
01427         return NULL;
01428     }
01429 
01430     return pMessage;
01431 }
01432 
01433 static LLRP_tSParameter *
01434 decodeParameter (
01435   LLRP_tSFrameDecoderStream *   pDecoderStream)
01436 {
01437     LLRP_tSFrameDecoder *       pDecoder  = pDecoderStream->pDecoder;
01438     LLRP_tSErrorDetails *       pError    = &pDecoder->decoderHdr.ErrorDetails;
01439     const LLRP_tSTypeRegistry * pRegistry = pDecoder->decoderHdr.pRegistry;
01440     LLRP_tSDecoderStream *      pBaseDecoderStream =
01441                                         &pDecoderStream->decoderStreamHdr;
01442     const LLRP_tSTypeDescriptor *pTypeDescriptor;
01443     llrp_u16_t                  Type;
01444     llrp_bool_t                 bIsTV;
01445     LLRP_tSElement *            pElement;
01446     LLRP_tSParameter *          pParameter;
01447 
01448     if(LLRP_RC_OK != pError->eResultCode)
01449     {
01450         return NULL;
01451     }
01452 
01453     Type = get_u8(pBaseDecoderStream, &LLRP_g_fdParameterHeader_TVType);
01454 
01455     if(LLRP_RC_OK != pError->eResultCode)
01456     {
01457         return NULL;
01458     }
01459 
01460     if(0 != (Type&0x80))
01461     {
01462         /*
01463          * Type-Value (TV).
01464          * All we can do is inherit the length from
01465          * the enclosing element.
01466          */
01467         Type &= 0x7F;
01468         bIsTV = TRUE;
01469     }
01470     else
01471     {
01472         llrp_u16_t              nLength;
01473         unsigned int            iLimit;
01474 
01475         /*
01476          * Type-Length-Value (TLV).
01477          * Back up and get the real type number,
01478          * then get the length.
01479          */
01480         pDecoder->iNext--;
01481         Type = get_u16(pBaseDecoderStream,
01482                     &LLRP_g_fdParameterHeader_TLVType);
01483         Type &= 0x3FF;
01484 
01485         if(LLRP_RC_OK != pError->eResultCode)
01486         {
01487             return NULL;
01488         }
01489 
01490         nLength = get_u16(pBaseDecoderStream,
01491                     &LLRP_g_fdParameterHeader_TLVLength);
01492 
01493         if(LLRP_RC_OK != pError->eResultCode)
01494         {
01495             return NULL;
01496         }
01497 
01498         if(4u > nLength)
01499         {
01500             pError->eResultCode = LLRP_RC_InvalidLength;
01501             pError->pWhatStr    = "TLV parameter length too small";
01502             pError->pRefType    = NULL;
01503             pError->pRefField   = &LLRP_g_fdParameterHeader_TLVLength;
01504             pError->OtherDetail = pDecoder->iNext;
01505             return NULL;
01506         }
01507 
01508         iLimit = pDecoderStream->iBegin + nLength;
01509 
01510         if(iLimit > pDecoderStream->iLimit)
01511         {
01512             pError->eResultCode = LLRP_RC_ExcessiveLength;
01513             pError->pWhatStr    =
01514                             "TLV parameter length exceeds enclosing length";
01515             pError->pRefType    = NULL;
01516             pError->pRefField   = &LLRP_g_fdParameterHeader_TLVLength;
01517             pError->OtherDetail = pDecoder->iNext;
01518             return NULL;
01519         }
01520 
01521         pDecoderStream->iLimit = iLimit;
01522 
01523         bIsTV = FALSE;
01524     }
01525 
01526     /* Custom? */
01527     if(1023u == Type)
01528     {
01529         llrp_u32_t              VendorPEN;
01530         llrp_u32_t              Subtype;
01531 
01532         VendorPEN = get_u32(pBaseDecoderStream,
01533                         &LLRP_g_fdParameterHeader_VendorPEN);
01534         Subtype   = get_u32(pBaseDecoderStream,
01535                         &LLRP_g_fdParameterHeader_Subtype);
01536 
01537         if(LLRP_RC_OK != pError->eResultCode)
01538         {
01539             return NULL;
01540         }
01541 
01542         pTypeDescriptor = LLRP_TypeRegistry_lookupCustomParameter(pRegistry,
01543             VendorPEN, Subtype);
01544         if(NULL == pTypeDescriptor)
01545         {
01546             /*
01547              * If we don't have a definition for a particular
01548              * CUSTOM parameter, just use the generic one.
01549              */
01550             pDecoder->iNext -= 8;       /* back up to VendorPEN and SubType */
01551             pTypeDescriptor =
01552                 LLRP_TypeRegistry_lookupParameter(pRegistry, Type);
01553         }
01554     }
01555     else
01556     {
01557         pTypeDescriptor = LLRP_TypeRegistry_lookupParameter(pRegistry, Type);
01558     }
01559 
01560     if(NULL == pTypeDescriptor)
01561     {
01562         pError->eResultCode = LLRP_RC_UnknownParameterType;
01563         pError->pWhatStr    = "unknown parameter type";
01564         pError->pRefType    = NULL;
01565         if(bIsTV)
01566         {
01567             pError->pRefField = &LLRP_g_fdParameterHeader_TVType;
01568         }
01569         else
01570         {
01571             pError->pRefField = &LLRP_g_fdParameterHeader_TLVType;
01572         }
01573         pError->OtherDetail = pDecoder->iNext;
01574         return NULL;
01575     }
01576 
01577     pDecoderStream->pRefType = pTypeDescriptor;
01578 
01579     pElement = LLRP_Element_construct(pTypeDescriptor);
01580 
01581     if(NULL == pElement)
01582     {
01583         pError->eResultCode = LLRP_RC_ParameterAllocationFailed;
01584         pError->pWhatStr    = "parameter allocation failed";
01585         pError->pRefType    = pTypeDescriptor;
01586         pError->pRefField   = NULL;
01587         pError->OtherDetail = pDecoder->iNext;
01588         return NULL;
01589     }
01590 
01591     pParameter = (LLRP_tSParameter *) pElement;
01592 
01593     pTypeDescriptor->pfDecodeFields(pElement, pBaseDecoderStream);
01594 
01595     if(LLRP_RC_OK != pError->eResultCode)
01596     {
01597         LLRP_Element_destruct(pElement);
01598         return NULL;
01599     }
01600 
01601     if(!bIsTV)
01602     {
01603         /*
01604          * Subparameters
01605          */
01606         while(0 < getRemainingByteCount(pDecoderStream) &&
01607               LLRP_RC_OK == pError->eResultCode)
01608         {
01609             LLRP_tSFrameDecoderStream       NestStream;
01610             LLRP_tSParameter *              pSubParameter;
01611 
01612             streamConstruct_nested(&NestStream, pDecoderStream);
01613 
01614             pSubParameter = decodeParameter(&NestStream);
01615 
01616             if(NULL == pSubParameter)
01617             {
01618                 if(LLRP_RC_OK == pError->eResultCode)
01619                 {
01620                     pError->eResultCode = LLRP_RC_Botch;
01621                     pError->pWhatStr    = "botch -- no param and no error";
01622                     pError->pRefType    = pTypeDescriptor;
01623                     pError->pRefField   = NULL;
01624                     pError->OtherDetail = pDecoder->iNext;
01625                 }
01626                 break;
01627             }
01628 
01629             pSubParameter->elementHdr.pParent = pElement;
01630             LLRP_Element_addSubParameterToAllList(pElement, pSubParameter);
01631         }
01632 
01633         if(LLRP_RC_OK == pError->eResultCode)
01634         {
01635             if(pDecoder->iNext != pDecoderStream->iLimit)
01636             {
01637                 pError->eResultCode = LLRP_RC_ExtraBytes;
01638                 pError->pWhatStr    = "extra bytes at end of TLV parameter";
01639                 pError->pRefType    = pTypeDescriptor;
01640                 pError->pRefField   = NULL;
01641                 pError->OtherDetail = pDecoder->iNext;
01642             }
01643         }
01644 
01645         if(LLRP_RC_OK != pError->eResultCode)
01646         {
01647             LLRP_Element_destruct(pElement);
01648             return NULL;
01649         }
01650 
01651         pTypeDescriptor->pfAssimilateSubParameters(pElement, pError);
01652 
01653         if(LLRP_RC_OK != pError->eResultCode)
01654         {
01655             LLRP_Element_destruct(pElement);
01656             return NULL;
01657         }
01658     }
01659 
01660     return pParameter;
01661 }
01662 
01663 static unsigned int
01664 getRemainingByteCount (
01665   LLRP_tSFrameDecoderStream *   pDecoderStream)
01666 {
01667     LLRP_tSFrameDecoder *       pDecoder = pDecoderStream->pDecoder;
01668 
01669     if(pDecoder->iNext < pDecoderStream->iLimit)
01670     {
01671         return pDecoderStream->iLimit - pDecoder->iNext;
01672     }
01673     else
01674     {
01675         return 0;
01676     }
01677 }
01678 
01679 static llrp_bool_t
01680 checkAvailable (
01681   LLRP_tSFrameDecoderStream *   pDecoderStream,
01682   unsigned int                  nByte,
01683   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01684 {
01685     LLRP_tSFrameDecoder *       pDecoder = pDecoderStream->pDecoder;
01686     LLRP_tSErrorDetails *       pError = &pDecoder->decoderHdr.ErrorDetails;
01687 
01688     if(LLRP_RC_OK != pError->eResultCode)
01689     {
01690         return FALSE;
01691     }
01692 
01693     if(pDecoder->iNext + nByte > pDecoderStream->iLimit)
01694     {
01695         pError->eResultCode = LLRP_RC_FieldUnderrun;
01696         pError->pWhatStr    = "underrun at field";
01697         pError->pRefType    = pDecoderStream->pRefType;
01698         pError->pRefField   = pFieldDescriptor;
01699         pError->OtherDetail = pDecoder->iNext;
01700         return FALSE;
01701     }
01702 
01703     if(0 != pDecoder->nBitFieldResid)
01704     {
01705         pError->eResultCode = LLRP_RC_UnalignedBitField;
01706         pError->pWhatStr    = "unaligned/incomplete bit field";
01707         pError->pRefType    = pDecoderStream->pRefType;
01708         pError->pRefField   = pFieldDescriptor;
01709         pError->OtherDetail = pDecoder->iNext;
01710         return FALSE;
01711     }
01712 
01713     return TRUE;
01714 }
01715 
01716 static unsigned int
01717 getBitField (
01718   LLRP_tSFrameDecoderStream *   pDecoderStream,
01719   unsigned int                  nBit,
01720   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01721 {
01722     LLRP_tSFrameDecoder *       pDecoder = pDecoderStream->pDecoder;
01723     LLRP_tSErrorDetails *       pError = &pDecoder->decoderHdr.ErrorDetails;
01724     unsigned int                Value;
01725 
01726     if(0 == pDecoder->nBitFieldResid)
01727     {
01728         if(checkAvailable(pDecoderStream, 1u, pFieldDescriptor))
01729         {
01730             pDecoder->BitFieldBuffer = next_u8(pDecoder);
01731             pDecoder->nBitFieldResid = 8u;
01732         }
01733         else
01734         {
01735             return 0;
01736         }
01737     }
01738 
01739     if(pDecoder->nBitFieldResid < nBit)
01740     {
01741         pError->eResultCode = LLRP_RC_UnalignedBitField;
01742         pError->pWhatStr    = "unaligned/incomplete bit field";
01743         pError->pRefType    = pDecoderStream->pRefType;
01744         pError->pRefField   = pFieldDescriptor;
01745         pError->OtherDetail = pDecoder->iNext;
01746         return 0;
01747     }
01748 
01749     pDecoder->nBitFieldResid -= nBit;
01750 
01751     Value = pDecoder->BitFieldBuffer >> pDecoder->nBitFieldResid;
01752     Value &= (1u << nBit) - 1u;
01753 
01754     return Value;
01755 }
01756 
01757 static llrp_u16_t
01758 getVarlenCount (
01759   LLRP_tSFrameDecoderStream *   pDecoderStream,
01760   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01761 {
01762     llrp_u16_t                  nValue;
01763 
01764     if(checkAvailable(pDecoderStream, 2u, pFieldDescriptor))
01765     {
01766         nValue = next_u16(pDecoderStream->pDecoder);
01767     }
01768     else
01769     {
01770         nValue = 0;
01771     }
01772 
01773     return nValue;
01774 }
01775 
01776 static llrp_bool_t
01777 verifyVectorAllocation (
01778   LLRP_tSFrameDecoderStream *   pDecoderStream,
01779   const void *                  pValue,
01780   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01781 {
01782     if(NULL == pValue)
01783     {
01784         LLRP_tSFrameDecoder *   pDecoder = pDecoderStream->pDecoder;
01785         LLRP_tSErrorDetails *   pError = &pDecoder->decoderHdr.ErrorDetails;
01786 
01787         pError->eResultCode = LLRP_RC_FieldAllocationFailed;
01788         pError->pWhatStr    = "field allocation failed";
01789         pError->pRefType    = pDecoderStream->pRefType;
01790         pError->pRefField   = pFieldDescriptor;
01791         pError->OtherDetail = pDecoder->iNext;
01792 
01793         return FALSE;
01794     }
01795     else
01796     {
01797         return TRUE;
01798     }
01799 }
01800 


thingmagic_rfid
Author(s): Brian Bingham
autogenerated on Thu May 16 2019 03:01:23