ltkc_xmltextencode.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 <stdio.h>
00023 #include <stdarg.h>
00024 #include <time.h>
00025 
00026 #include "ltkc_platform.h"
00027 #include "ltkc_base.h"
00028 #include "ltkc_xmltext.h"
00029 
00030 
00031 /*
00032  * BEGIN forward declarations
00033  */
00034 
00035 LLRP_tSXMLTextEncoder *
00036 LLRP_XMLTextEncoder_construct (
00037   unsigned char *               pBuffer,
00038   unsigned int                  nBuffer);
00039 
00040 static void
00041 encoderDestruct (
00042   LLRP_tSEncoder *              pBaseEncoder);
00043 
00044 static void
00045 encodeElement (
00046   LLRP_tSEncoder *              pBaseEncoder,
00047   const LLRP_tSElement *        pElement);
00048 
00049 static void
00050 putRequiredSubParameter (
00051   LLRP_tSEncoderStream *        pBaseEncoderStream,
00052   const LLRP_tSParameter *      pParameter,
00053   const LLRP_tSTypeDescriptor * pRefType);
00054 
00055 static void
00056 putOptionalSubParameter (
00057   LLRP_tSEncoderStream *        pBaseEncoderStream,
00058   const LLRP_tSParameter *      pParameter,
00059   const LLRP_tSTypeDescriptor * pRefType);
00060 
00061 static void
00062 putRequiredSubParameterList (
00063   LLRP_tSEncoderStream *        pBaseEncoderStream,
00064   const LLRP_tSParameter *      pParameterList,
00065   const LLRP_tSTypeDescriptor * pRefType);
00066 
00067 static void
00068 putOptionalSubParameterList (
00069   LLRP_tSEncoderStream *        pBaseEncoderStream,
00070   const LLRP_tSParameter *      pParameterList,
00071   const LLRP_tSTypeDescriptor * pRefType);
00072 
00073 static void
00074 put_u8 (
00075   LLRP_tSEncoderStream *        pBaseEncoderStream,
00076   const llrp_u8_t               Value,
00077   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00078 
00079 static void
00080 put_s8 (
00081   LLRP_tSEncoderStream *        pBaseEncoderStream,
00082   const llrp_s8_t               Value,
00083   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00084 
00085 static void
00086 put_u8v (
00087   LLRP_tSEncoderStream *        pBaseEncoderStream,
00088   const llrp_u8v_t              Value,
00089   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00090 
00091 static void
00092 put_s8v (
00093   LLRP_tSEncoderStream *        pBaseEncoderStream,
00094   const llrp_s8v_t              Value,
00095   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00096 
00097 static void
00098 put_u16 (
00099   LLRP_tSEncoderStream *        pBaseEncoderStream,
00100   const llrp_u16_t              Value,
00101   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00102 
00103 static void
00104 put_s16 (
00105   LLRP_tSEncoderStream *        pBaseEncoderStream,
00106   const llrp_s16_t              Value,
00107   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00108 
00109 static void
00110 put_u16v (
00111   LLRP_tSEncoderStream *        pBaseEncoderStream,
00112   const llrp_u16v_t             Value,
00113   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00114 
00115 static void
00116 put_s16v (
00117   LLRP_tSEncoderStream *        pBaseEncoderStream,
00118   const llrp_s16v_t             Value,
00119   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00120 
00121 static void
00122 put_u32 (
00123   LLRP_tSEncoderStream *        pBaseEncoderStream,
00124   const llrp_u32_t              Value,
00125   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00126 
00127 static void
00128 put_s32 (
00129   LLRP_tSEncoderStream *        pBaseEncoderStream,
00130   const llrp_s32_t              Value,
00131   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00132 
00133 static void
00134 put_u32v (
00135   LLRP_tSEncoderStream *        pBaseEncoderStream,
00136   const llrp_u32v_t             Value,
00137   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00138 
00139 static void
00140 put_s32v (
00141   LLRP_tSEncoderStream *        pBaseEncoderStream,
00142   const llrp_s32v_t             Value,
00143   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00144 
00145 static void
00146 put_u64 (
00147   LLRP_tSEncoderStream *        pBaseEncoderStream,
00148   const llrp_u64_t              Value,
00149   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00150 
00151 static void
00152 put_s64 (
00153   LLRP_tSEncoderStream *        pBaseEncoderStream,
00154   const llrp_s64_t              Value,
00155   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00156 
00157 static void
00158 put_u64v (
00159   LLRP_tSEncoderStream *        pBaseEncoderStream,
00160   const llrp_u64v_t             Value,
00161   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00162 
00163 static void
00164 put_s64v (
00165   LLRP_tSEncoderStream *        pBaseEncoderStream,
00166   const llrp_s64v_t             Value,
00167   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00168 
00169 static void
00170 put_u1 (
00171   LLRP_tSEncoderStream *        pBaseEncoderStream,
00172   const llrp_u1_t               Value,
00173   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00174 
00175 static void
00176 put_u1v (
00177   LLRP_tSEncoderStream *        pBaseEncoderStream,
00178   const llrp_u1v_t              Value,
00179   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00180 
00181 static void
00182 put_u2 (
00183   LLRP_tSEncoderStream *        pBaseEncoderStream,
00184   const llrp_u2_t               Value,
00185   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00186 
00187 static void
00188 put_u96 (
00189   LLRP_tSEncoderStream *        pBaseEncoderStream,
00190   const llrp_u96_t              Value,
00191   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00192 
00193 static void
00194 put_utf8v (
00195   LLRP_tSEncoderStream *        pBaseEncoderStream,
00196   const llrp_utf8v_t            Value,
00197   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00198 
00199 static void
00200 put_bytesToEnd (
00201   LLRP_tSEncoderStream *        pBaseEncoderStream,
00202   const llrp_bytesToEnd_t       Value,
00203   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00204 
00205 static void
00206 put_e1 (
00207   LLRP_tSEncoderStream *        pBaseEncoderStream,
00208   const int                     Value,
00209   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00210 
00211 static void
00212 put_e2 (
00213   LLRP_tSEncoderStream *        pBaseEncoderStream,
00214   const int                     Value,
00215   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00216 
00217 static void
00218 put_e8 (
00219   LLRP_tSEncoderStream *        pBaseEncoderStream,
00220   const int                     Value,
00221   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00222 
00223 static void
00224 put_e16 (
00225   LLRP_tSEncoderStream *        pBaseEncoderStream,
00226   const int                     Value,
00227   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00228 
00229 static void
00230 put_e32 (
00231   LLRP_tSEncoderStream *        pBaseEncoderStream,
00232   const int                     Value,
00233   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00234 
00235 static void
00236 put_e8v (
00237   LLRP_tSEncoderStream *        pBaseEncoderStream,
00238   const llrp_u8v_t              Value,
00239   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00240 
00241 static void
00242 put_enum (
00243   LLRP_tSEncoderStream *        pBaseEncoderStream,
00244   int                           eValue,
00245   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00246 
00247 static void
00248 put_reserved (
00249   LLRP_tSEncoderStream *        pBaseEncoderStream,
00250   unsigned int                  nBits);
00251 
00252 static void
00253 streamConstruct_outermost (
00254   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00255   LLRP_tSXMLTextEncoder *       pEncoder);
00256 
00257 static void
00258 streamConstruct_nested (
00259   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00260   LLRP_tSXMLTextEncoderStream * pEnclosingEncoderStream);
00261 
00262 static void
00263 putElement (
00264   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00265   const LLRP_tSElement *        pElement);
00266 
00267 static int
00268 discoverNamespaces (
00269   const LLRP_tSElement *        pElement,
00270   void *                        pArg);
00271 
00272 static void
00273 nestSubParameter (
00274   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00275   const LLRP_tSParameter *      pParameter);
00276 
00277 static void
00278 indent (
00279   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00280   int                           adjust);
00281 
00282 static void
00283 appendOpenTag (
00284   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00285   const char *                  pName);
00286 
00287 static void
00288 appendCloseTag (
00289   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00290   const char *                  pName);
00291 
00292 static void
00293 appendPrefixedTagName (
00294   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00295   const char *                  pName);
00296 
00297 static void
00298 appendFormat (
00299   LLRP_tSXMLTextEncoderStream * pEncoderStream,
00300   char *                        pFmtStr,
00301                                 ...);
00302 /*
00303  * END forward declarations
00304  */
00305 
00306 /*
00307  * Structures used by discoverNamespaces() and putElement().
00308  */
00309 #define MAX_NS      10u
00310 struct NamespaceList
00311 {
00312     const LLRP_tSNamespaceDescriptor *apNamespaceDescriptor[MAX_NS];
00313     unsigned int                nNamespaceDescriptor;
00314 };
00315 typedef struct NamespaceList    tNamespaceList;
00316 
00317 
00318 
00319 
00320 static LLRP_tSEncoderOps
00321 s_XMLTextEncoderOps =
00322 {
00323     .pfDestruct                 = encoderDestruct,
00324     .pfEncodeElement            = encodeElement,
00325 };
00326 
00327 static LLRP_tSEncoderStreamOps
00328 s_XMLTextEncoderStreamOps =
00329 {
00330     .pfPutRequiredSubParameter      = putRequiredSubParameter,
00331     .pfPutOptionalSubParameter      = putOptionalSubParameter,
00332     .pfPutRequiredSubParameterList  = putRequiredSubParameterList,
00333     .pfPutOptionalSubParameterList  = putOptionalSubParameterList,
00334 
00335     .pfPut_u8                       = put_u8,
00336     .pfPut_s8                       = put_s8,
00337     .pfPut_u8v                      = put_u8v,
00338     .pfPut_s8v                      = put_s8v,
00339 
00340     .pfPut_u16                      = put_u16,
00341     .pfPut_s16                      = put_s16,
00342     .pfPut_u16v                     = put_u16v,
00343     .pfPut_s16v                     = put_s16v,
00344 
00345     .pfPut_u32                      = put_u32,
00346     .pfPut_s32                      = put_s32,
00347     .pfPut_u32v                     = put_u32v,
00348     .pfPut_s32v                     = put_s32v,
00349 
00350     .pfPut_u64                      = put_u64,
00351     .pfPut_s64                      = put_s64,
00352     .pfPut_u64v                     = put_u64v,
00353     .pfPut_s64v                     = put_s64v,
00354 
00355     .pfPut_u1                       = put_u1,
00356     .pfPut_u1v                      = put_u1v,
00357     .pfPut_u2                       = put_u2,
00358     .pfPut_u96                      = put_u96,
00359     .pfPut_utf8v                    = put_utf8v,
00360     .pfPut_bytesToEnd               = put_bytesToEnd,
00361 
00362     .pfPut_e1                       = put_e1,
00363     .pfPut_e2                       = put_e2,
00364     .pfPut_e8                       = put_e8,
00365     .pfPut_e16                      = put_e16,
00366     .pfPut_e32                      = put_e32,
00367     .pfPut_e8v                      = put_e8v,
00368 
00369     .pfPut_reserved                 = put_reserved,
00370 };
00371 
00372 
00373 LLRP_tSXMLTextEncoder *
00374 LLRP_XMLTextEncoder_construct (
00375   unsigned char *               pBuffer,
00376   unsigned int                  nBuffer)
00377 {
00378     LLRP_tSXMLTextEncoder *     pEncoder;
00379 
00380     pEncoder = malloc(sizeof *pEncoder);
00381     if(NULL == pEncoder)
00382     {
00383         return pEncoder;
00384     }
00385 
00386     memset(pEncoder, 0, sizeof *pEncoder);
00387 
00388     pEncoder->encoderHdr.pEncoderOps = &s_XMLTextEncoderOps;
00389     pEncoder->pBuffer = pBuffer;
00390     pEncoder->nBuffer = nBuffer;
00391     pEncoder->iNext   = 0;
00392     pEncoder->bOverflow = 0;
00393 
00394     return pEncoder;
00395 }
00396 
00397 static void
00398 encoderDestruct (
00399   LLRP_tSEncoder *              pBaseEncoder)
00400 {
00401     LLRP_tSXMLTextEncoder *     pEncoder =
00402                                     (LLRP_tSXMLTextEncoder *) pBaseEncoder;
00403 
00404     free(pEncoder);
00405 }
00406 
00407 static void
00408 encodeElement (
00409   LLRP_tSEncoder *              pBaseEncoder,
00410   const LLRP_tSElement *        pElement)
00411 {
00412     LLRP_tSXMLTextEncoder *     pEncoder =
00413                                     (LLRP_tSXMLTextEncoder *) pBaseEncoder;
00414     LLRP_tSXMLTextEncoderStream EncoderStream;
00415 
00416     if(NULL == pElement)
00417     {
00418         return;
00419     }
00420 
00421     streamConstruct_outermost(&EncoderStream, pEncoder);
00422 
00423     putElement(&EncoderStream, pElement);
00424 }
00425 
00426 static void
00427 putRequiredSubParameter (
00428   LLRP_tSEncoderStream *        pBaseEncoderStream,
00429   const LLRP_tSParameter *      pParameter,
00430   const LLRP_tSTypeDescriptor * pRefType)
00431 {
00432     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00433                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00434 
00435     if(NULL == pParameter)
00436     {
00437         appendFormat(pEncoderStream, "warning: missing %s\n",
00438             (NULL == pRefType) ? "<something>" : pRefType->pName);
00439         return;
00440     }
00441 
00442     nestSubParameter(pEncoderStream, pParameter);
00443 }
00444 
00445 static void
00446 putOptionalSubParameter (
00447   LLRP_tSEncoderStream *        pBaseEncoderStream,
00448   const LLRP_tSParameter *      pParameter,
00449   const LLRP_tSTypeDescriptor * pRefType)
00450 {
00451     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00452                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00453 
00454     nestSubParameter(pEncoderStream, pParameter);
00455 }
00456 
00457 static void
00458 putRequiredSubParameterList (
00459   LLRP_tSEncoderStream *        pBaseEncoderStream,
00460   const LLRP_tSParameter *      pParameterList,
00461   const LLRP_tSTypeDescriptor * pRefType)
00462 {
00463     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00464                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00465     const LLRP_tSParameter *    pParameter;
00466 
00467     if(NULL == pParameterList)
00468     {
00469         appendFormat(pEncoderStream, "warning: missing list of %s\n",
00470             (NULL == pRefType) ? "<something>" : pRefType->pName);
00471         return;
00472     }
00473 
00474     for(
00475         pParameter = pParameterList;
00476         NULL != pParameter;
00477         pParameter = pParameter->pNextSubParameter)
00478     {
00479         nestSubParameter(pEncoderStream, pParameter);
00480     }
00481 }
00482 
00483 static void
00484 putOptionalSubParameterList (
00485   LLRP_tSEncoderStream *        pBaseEncoderStream,
00486   const LLRP_tSParameter *      pParameterList,
00487   const LLRP_tSTypeDescriptor * pRefType)
00488 {
00489     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00490                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00491     const LLRP_tSParameter *    pParameter;
00492 
00493     for(
00494         pParameter = pParameterList;
00495         NULL != pParameter;
00496         pParameter = pParameter->pNextSubParameter)
00497     {
00498         nestSubParameter(pEncoderStream, pParameter);
00499     }
00500 }
00501 
00502 static void
00503 put_u8 (
00504   LLRP_tSEncoderStream *        pBaseEncoderStream,
00505   const llrp_u8_t               Value,
00506   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00507 {
00508     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00509                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00510     const char *                pFieldName = pFieldDescriptor->pName;
00511 
00512     appendOpenTag(pEncoderStream, pFieldName);
00513     switch(pFieldDescriptor->eFieldFormat)
00514     {
00515     case LLRP_FMT_NORMAL:
00516     case LLRP_FMT_DEC:
00517     default:
00518         appendFormat(pEncoderStream, "%u", Value);
00519         break;
00520 
00521     case LLRP_FMT_HEX:
00522         appendFormat(pEncoderStream, "%02X", Value);
00523         break;
00524     }
00525     appendCloseTag(pEncoderStream, pFieldName);
00526 }
00527 
00528 static void
00529 put_s8 (
00530   LLRP_tSEncoderStream *        pBaseEncoderStream,
00531   const llrp_s8_t               Value,
00532   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00533 {
00534     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00535                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00536     const char *                pFieldName = pFieldDescriptor->pName;
00537 
00538     appendOpenTag(pEncoderStream, pFieldName);
00539     switch(pFieldDescriptor->eFieldFormat)
00540     {
00541     case LLRP_FMT_NORMAL:
00542     case LLRP_FMT_DEC:
00543     default:
00544         appendFormat(pEncoderStream, "%d", Value);
00545         break;
00546 
00547     case LLRP_FMT_HEX:
00548         appendFormat(pEncoderStream, "%02X", 0xFF & Value);
00549         break;
00550     }
00551     appendCloseTag(pEncoderStream, pFieldName);
00552 }
00553 
00554 static void
00555 put_u8v (
00556   LLRP_tSEncoderStream *        pBaseEncoderStream,
00557   const llrp_u8v_t              Value,
00558   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00559 {
00560     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00561                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00562     const char *                pFieldName = pFieldDescriptor->pName;
00563     int                         i;
00564 
00565     appendOpenTag(pEncoderStream, pFieldName);
00566     for(i = 0; i < Value.nValue; i++)
00567     {
00568         switch(pFieldDescriptor->eFieldFormat)
00569         {
00570         case LLRP_FMT_DEC:
00571         case LLRP_FMT_NORMAL:
00572         default:
00573             if(0 < i)
00574             {
00575                 appendFormat(pEncoderStream, " ");
00576             }
00577             appendFormat(pEncoderStream, "%u", Value.pValue[i]);
00578             break;
00579 
00580         case LLRP_FMT_HEX:
00581             appendFormat(pEncoderStream, "%02X", Value.pValue[i]);
00582             break;
00583         }
00584     }
00585     appendCloseTag(pEncoderStream, pFieldName);
00586 }
00587 
00588 static void
00589 put_s8v (
00590   LLRP_tSEncoderStream *        pBaseEncoderStream,
00591   const llrp_s8v_t              Value,
00592   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00593 {
00594     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00595                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00596     const char *                pFieldName = pFieldDescriptor->pName;
00597     int                         i;
00598 
00599     appendOpenTag(pEncoderStream, pFieldName);
00600     for(i = 0; i < Value.nValue; i++)
00601     {
00602         switch(pFieldDescriptor->eFieldFormat)
00603         {
00604         case LLRP_FMT_DEC:
00605         case LLRP_FMT_NORMAL:
00606         default:
00607             if(0 < i)
00608             {
00609                 appendFormat(pEncoderStream, " ");
00610             }
00611             appendFormat(pEncoderStream, "%d", Value.pValue[i]);
00612             break;
00613 
00614         case LLRP_FMT_HEX:
00615             appendFormat(pEncoderStream, "%02X", 0xFF & Value.pValue[i]);
00616             break;
00617         }
00618     }
00619     appendCloseTag(pEncoderStream, pFieldName);
00620 }
00621 
00622 static void
00623 put_u16 (
00624   LLRP_tSEncoderStream *        pBaseEncoderStream,
00625   const llrp_u16_t              Value,
00626   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00627 {
00628     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00629                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00630     const char *                pFieldName = pFieldDescriptor->pName;
00631 
00632     appendOpenTag(pEncoderStream, pFieldName);
00633     switch(pFieldDescriptor->eFieldFormat)
00634     {
00635     case LLRP_FMT_NORMAL:
00636     case LLRP_FMT_DEC:
00637     default:
00638         appendFormat(pEncoderStream, "%u", Value);
00639         break;
00640 
00641     case LLRP_FMT_HEX:
00642         appendFormat(pEncoderStream, "%04X", Value);
00643         break;
00644     }
00645     appendCloseTag(pEncoderStream, pFieldName);
00646 }
00647 
00648 static void
00649 put_s16 (
00650   LLRP_tSEncoderStream *        pBaseEncoderStream,
00651   const llrp_s16_t              Value,
00652   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00653 {
00654     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00655                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00656     const char *                pFieldName = pFieldDescriptor->pName;
00657 
00658     appendOpenTag(pEncoderStream, pFieldName);
00659     switch(pFieldDescriptor->eFieldFormat)
00660     {
00661     case LLRP_FMT_NORMAL:
00662     case LLRP_FMT_DEC:
00663     default:
00664         appendFormat(pEncoderStream, "%d", Value);
00665         break;
00666 
00667     case LLRP_FMT_HEX:
00668         appendFormat(pEncoderStream, "%04X", 0xFFFF & Value);
00669         break;
00670     }
00671     appendCloseTag(pEncoderStream, pFieldName);
00672 }
00673 
00674 static void
00675 put_u16v (
00676   LLRP_tSEncoderStream *        pBaseEncoderStream,
00677   const llrp_u16v_t             Value,
00678   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00679 {
00680     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00681                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00682     const char *                pFieldName = pFieldDescriptor->pName;
00683     int                         i;
00684 
00685     appendOpenTag(pEncoderStream, pFieldName);
00686     for(i = 0; i < Value.nValue; i++)
00687     {
00688         if(0 < i)
00689         {
00690             appendFormat(pEncoderStream, " ");
00691         }
00692         switch(pFieldDescriptor->eFieldFormat)
00693         {
00694         case LLRP_FMT_DEC:
00695         case LLRP_FMT_NORMAL:
00696         default:
00697             appendFormat(pEncoderStream, "%u", Value.pValue[i]);
00698             break;
00699 
00700         case LLRP_FMT_HEX:
00701             appendFormat(pEncoderStream, "%04X", Value.pValue[i]);
00702             break;
00703         }
00704     }
00705     appendCloseTag(pEncoderStream, pFieldName);
00706 }
00707 
00708 static void
00709 put_s16v (
00710   LLRP_tSEncoderStream *        pBaseEncoderStream,
00711   const llrp_s16v_t             Value,
00712   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00713 {
00714     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00715                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00716     const char *                pFieldName = pFieldDescriptor->pName;
00717     int                         i;
00718 
00719     appendOpenTag(pEncoderStream, pFieldName);
00720     for(i = 0; i < Value.nValue; i++)
00721     {
00722         if(0 < i)
00723         {
00724             appendFormat(pEncoderStream, " ");
00725         }
00726         switch(pFieldDescriptor->eFieldFormat)
00727         {
00728         case LLRP_FMT_DEC:
00729         case LLRP_FMT_NORMAL:
00730         default:
00731             appendFormat(pEncoderStream, "%d", Value.pValue[i]);
00732             break;
00733 
00734         case LLRP_FMT_HEX:
00735             appendFormat(pEncoderStream, "%04X", 0xFFFF & Value.pValue[i]);
00736             break;
00737         }
00738     }
00739     appendCloseTag(pEncoderStream, pFieldName);
00740 }
00741 
00742 static void
00743 put_u32 (
00744   LLRP_tSEncoderStream *        pBaseEncoderStream,
00745   const llrp_u32_t              Value,
00746   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00747 {
00748     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00749                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00750     const char *                pFieldName = pFieldDescriptor->pName;
00751 
00752     appendOpenTag(pEncoderStream, pFieldName);
00753     switch(pFieldDescriptor->eFieldFormat)
00754     {
00755     case LLRP_FMT_NORMAL:
00756     case LLRP_FMT_DEC:
00757     default:
00758         appendFormat(pEncoderStream, "%u", Value);
00759         break;
00760 
00761     case LLRP_FMT_HEX:
00762         appendFormat(pEncoderStream, "%08X", Value);
00763         break;
00764     }
00765     appendCloseTag(pEncoderStream, pFieldName);
00766 }
00767 
00768 static void
00769 put_s32 (
00770   LLRP_tSEncoderStream *        pBaseEncoderStream,
00771   const llrp_s32_t              Value,
00772   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00773 {
00774     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00775                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00776     const char *                pFieldName = pFieldDescriptor->pName;
00777 
00778     appendOpenTag(pEncoderStream, pFieldName);
00779     switch(pFieldDescriptor->eFieldFormat)
00780     {
00781     case LLRP_FMT_NORMAL:
00782     case LLRP_FMT_DEC:
00783     default:
00784         appendFormat(pEncoderStream, "%d", Value);
00785         break;
00786 
00787     case LLRP_FMT_HEX:
00788         appendFormat(pEncoderStream, "%08X", Value);
00789         break;
00790     }
00791     appendCloseTag(pEncoderStream, pFieldName);
00792 }
00793 
00794 static void
00795 put_u32v (
00796   LLRP_tSEncoderStream *        pBaseEncoderStream,
00797   const llrp_u32v_t             Value,
00798   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00799 {
00800     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00801                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00802     const char *                pFieldName = pFieldDescriptor->pName;
00803     int                         i;
00804 
00805     appendOpenTag(pEncoderStream, pFieldName);
00806     for(i = 0; i < Value.nValue; i++)
00807     {
00808         if(0 < i)
00809         {
00810             appendFormat(pEncoderStream, " ");
00811         }
00812         switch(pFieldDescriptor->eFieldFormat)
00813         {
00814         case LLRP_FMT_DEC:
00815         case LLRP_FMT_NORMAL:
00816         default:
00817             appendFormat(pEncoderStream, "%u", Value.pValue[i]);
00818             break;
00819 
00820         case LLRP_FMT_HEX:
00821             appendFormat(pEncoderStream, "%08X", Value.pValue[i]);
00822             break;
00823         }
00824     }
00825     appendCloseTag(pEncoderStream, pFieldName);
00826 }
00827 
00828 static void
00829 put_s32v (
00830   LLRP_tSEncoderStream *        pBaseEncoderStream,
00831   const llrp_s32v_t             Value,
00832   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00833 {
00834     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00835                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00836     const char *                pFieldName = pFieldDescriptor->pName;
00837     int                         i;
00838 
00839     appendOpenTag(pEncoderStream, pFieldName);
00840     for(i = 0; i < Value.nValue; i++)
00841     {
00842         if(0 < i)
00843         {
00844             appendFormat(pEncoderStream, " ");
00845         }
00846         switch(pFieldDescriptor->eFieldFormat)
00847         {
00848         case LLRP_FMT_DEC:
00849         case LLRP_FMT_NORMAL:
00850         default:
00851             appendFormat(pEncoderStream, "%d", Value.pValue[i]);
00852             break;
00853 
00854         case LLRP_FMT_HEX:
00855             appendFormat(pEncoderStream, "%08X", Value.pValue[i]);
00856             break;
00857         }
00858     }
00859     appendCloseTag(pEncoderStream, pFieldName);
00860 }
00861 
00862 static void
00863 put_u64 (
00864   LLRP_tSEncoderStream *        pBaseEncoderStream,
00865   const llrp_u64_t              Value,
00866   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00867 {
00868     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00869                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00870     const char *                pFieldName = pFieldDescriptor->pName;
00871 
00872     appendOpenTag(pEncoderStream, pFieldName);
00873     switch(pFieldDescriptor->eFieldFormat)
00874     {
00875     case LLRP_FMT_NORMAL:
00876     case LLRP_FMT_DEC:
00877     default:
00878         appendFormat(pEncoderStream, "%llu", Value);
00879         break;
00880 
00881     case LLRP_FMT_HEX:
00882         appendFormat(pEncoderStream, "%016llX", Value);
00883         break;
00884 
00885     case LLRP_FMT_DATETIME:
00886         {
00887             char                aBuf[64];
00888             time_t              CurSec  = Value / 1000000u;
00889             llrp_u32_t          CurUSec = Value % 1000000u;
00890             struct tm *         pLclTime;
00891 
00892             pLclTime = localtime(&CurSec);
00893             strftime(aBuf, sizeof aBuf, "%Y-%m-%dT%H:%M:%S", pLclTime);
00894             appendFormat(pEncoderStream, "%s.%06d", aBuf, CurUSec);
00895         }
00896         break;
00897     }
00898     appendCloseTag(pEncoderStream, pFieldName);
00899 }
00900 
00901 static void
00902 put_s64 (
00903   LLRP_tSEncoderStream *        pBaseEncoderStream,
00904   const llrp_s64_t              Value,
00905   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00906 {
00907     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00908                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00909     const char *                pFieldName = pFieldDescriptor->pName;
00910 
00911     appendOpenTag(pEncoderStream, pFieldName);
00912     switch(pFieldDescriptor->eFieldFormat)
00913     {
00914     case LLRP_FMT_NORMAL:
00915     case LLRP_FMT_DEC:
00916     default:
00917         appendFormat(pEncoderStream, "%lld", Value);
00918         break;
00919 
00920     case LLRP_FMT_HEX:
00921         appendFormat(pEncoderStream, "%016llX", Value);
00922         break;
00923     }
00924     appendCloseTag(pEncoderStream, pFieldName);
00925 }
00926 
00927 static void
00928 put_u64v (
00929   LLRP_tSEncoderStream *        pBaseEncoderStream,
00930   const llrp_u64v_t             Value,
00931   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00932 {
00933     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00934                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00935     const char *                pFieldName = pFieldDescriptor->pName;
00936     int                         i;
00937 
00938     appendOpenTag(pEncoderStream, pFieldName);
00939     for(i = 0; i < Value.nValue; i++)
00940     {
00941         if(0 < i)
00942         {
00943             appendFormat(pEncoderStream, " ");
00944         }
00945         switch(pFieldDescriptor->eFieldFormat)
00946         {
00947         case LLRP_FMT_DEC:
00948         case LLRP_FMT_NORMAL:
00949         default:
00950             appendFormat(pEncoderStream, "%llu", Value.pValue[i]);
00951             break;
00952 
00953         case LLRP_FMT_HEX:
00954             appendFormat(pEncoderStream, "%016X", Value.pValue[i]);
00955             break;
00956         }
00957     }
00958     appendCloseTag(pEncoderStream, pFieldName);
00959 }
00960 
00961 static void
00962 put_s64v (
00963   LLRP_tSEncoderStream *        pBaseEncoderStream,
00964   const llrp_s64v_t             Value,
00965   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00966 {
00967     LLRP_tSXMLTextEncoderStream * pEncoderStream =
00968                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
00969     const char *                pFieldName = pFieldDescriptor->pName;
00970     int                         i;
00971 
00972     appendOpenTag(pEncoderStream, pFieldName);
00973     for(i = 0; i < Value.nValue; i++)
00974     {
00975         if(0 < i)
00976         {
00977             appendFormat(pEncoderStream, " ");
00978         }
00979         switch(pFieldDescriptor->eFieldFormat)
00980         {
00981         case LLRP_FMT_DEC:
00982         case LLRP_FMT_NORMAL:
00983         default:
00984             appendFormat(pEncoderStream, "%lld", Value.pValue[i]);
00985             break;
00986 
00987         case LLRP_FMT_HEX:
00988             appendFormat(pEncoderStream, "%016X", Value.pValue[i]);
00989             break;
00990         }
00991     }
00992     appendCloseTag(pEncoderStream, pFieldName);
00993 }
00994 
00995 static void
00996 put_u1 (
00997   LLRP_tSEncoderStream *        pBaseEncoderStream,
00998   const llrp_u1_t               Value,
00999   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01000 {
01001     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01002                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01003     const char *                pFieldName = pFieldDescriptor->pName;
01004 
01005     appendOpenTag(pEncoderStream, pFieldName);
01006     switch(pFieldDescriptor->eFieldFormat)
01007     {
01008     case LLRP_FMT_NORMAL:
01009     default:
01010         appendFormat(pEncoderStream, "%s", (Value & 1) ? "true" : "false");
01011         break;
01012 
01013     case LLRP_FMT_DEC:
01014     case LLRP_FMT_HEX:
01015         appendFormat(pEncoderStream, "%u", Value & 1);
01016         break;
01017     }
01018     appendCloseTag(pEncoderStream, pFieldName);
01019 }
01020 
01021 static void
01022 put_u1v (
01023   LLRP_tSEncoderStream *        pBaseEncoderStream,
01024   const llrp_u1v_t              Value,
01025   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01026 {
01027     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01028                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01029     const char *                pFieldName = pFieldDescriptor->pName;
01030     int                         nByte = (Value.nBit + 7u) / 8u;
01031     int                         i;
01032 
01033     indent(pEncoderStream, 0);
01034     appendFormat(pEncoderStream, "<");
01035     appendPrefixedTagName(pEncoderStream, pFieldName);
01036     appendFormat(pEncoderStream, " Count='%d'>", Value.nBit);
01037 
01038     for(i = 0; i < nByte; i++)
01039     {
01040         appendFormat(pEncoderStream, "%02X", Value.pValue[i]);
01041     }
01042 
01043     appendCloseTag(pEncoderStream, pFieldName);
01044 }
01045 
01046 static void
01047 put_u2 (
01048   LLRP_tSEncoderStream *        pBaseEncoderStream,
01049   const llrp_u2_t               Value,
01050   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01051 {
01052     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01053                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01054     const char *                pFieldName = pFieldDescriptor->pName;
01055 
01056     appendOpenTag(pEncoderStream, pFieldName);
01057     appendFormat(pEncoderStream, "%u", Value & 3);
01058     appendCloseTag(pEncoderStream, pFieldName);
01059 }
01060 
01061 static void
01062 put_u96 (
01063   LLRP_tSEncoderStream *        pBaseEncoderStream,
01064   const llrp_u96_t              Value,
01065   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01066 {
01067     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01068                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01069     const char *                pFieldName = pFieldDescriptor->pName;
01070     int                         i;
01071 
01072     appendOpenTag(pEncoderStream, pFieldName);
01073     for(i = 0; i < 12; i++)
01074     {
01075         appendFormat(pEncoderStream, "%02X", Value.aValue[i]);
01076     }
01077     appendCloseTag(pEncoderStream, pFieldName);
01078 }
01079 
01080 static void
01081 put_utf8v (
01082   LLRP_tSEncoderStream *        pBaseEncoderStream,
01083   const llrp_utf8v_t            Value,
01084   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01085 {
01086     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01087                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01088     const char *                pFieldName = pFieldDescriptor->pName;
01089     int                         i;
01090 
01091     appendOpenTag(pEncoderStream, pFieldName);
01092     for(i = 0; i < Value.nValue; i++)
01093     {
01094         int                     c = 0xFF & Value.pValue[i];
01095 
01096         if(0 == c && i+1 == Value.nValue)
01097         {
01098             continue;
01099         }
01100         if(' ' <= c && c < 0x7F)
01101         {
01102             appendFormat(pEncoderStream, "%c", c);
01103         }
01104         else
01105         {
01106             appendFormat(pEncoderStream, "&#%02x;", c);
01107         }
01108     }
01109     appendCloseTag(pEncoderStream, pFieldName);
01110 }
01111 
01112 static void
01113 put_bytesToEnd (
01114   LLRP_tSEncoderStream *        pBaseEncoderStream,
01115   const llrp_bytesToEnd_t       Value,
01116   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01117 {
01118     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01119                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01120     const char *                pFieldName = pFieldDescriptor->pName;
01121     int                         i;
01122 
01123     appendOpenTag(pEncoderStream, pFieldName);
01124     for(i = 0; i < Value.nValue; i++)
01125     {
01126         appendFormat(pEncoderStream, "%02X", Value.pValue[i]);
01127     }
01128     appendCloseTag(pEncoderStream, pFieldName);
01129 }
01130 
01131 static void
01132 put_e1 (
01133   LLRP_tSEncoderStream *        pBaseEncoderStream,
01134   const int                     Value,
01135   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01136 {
01137     put_enum(pBaseEncoderStream, Value, pFieldDescriptor);
01138 }
01139 
01140 static void
01141 put_e2 (
01142   LLRP_tSEncoderStream *        pBaseEncoderStream,
01143   const int                     Value,
01144   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01145 {
01146     put_enum(pBaseEncoderStream, Value, pFieldDescriptor);
01147 }
01148 
01149 static void
01150 put_e8 (
01151   LLRP_tSEncoderStream *        pBaseEncoderStream,
01152   const int                     Value,
01153   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01154 {
01155     put_enum(pBaseEncoderStream, Value, pFieldDescriptor);
01156 }
01157 
01158 static void
01159 put_e16 (
01160   LLRP_tSEncoderStream *        pBaseEncoderStream,
01161   const int                     Value,
01162   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01163 {
01164     put_enum(pBaseEncoderStream, Value, pFieldDescriptor);
01165 }
01166 
01167 static void
01168 put_e32 (
01169   LLRP_tSEncoderStream *        pBaseEncoderStream,
01170   const int                     Value,
01171   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01172 {
01173     put_enum(pBaseEncoderStream, Value, pFieldDescriptor);
01174 }
01175 
01176 static void
01177 put_e8v (
01178   LLRP_tSEncoderStream *        pBaseEncoderStream,
01179   const llrp_u8v_t              Value,
01180   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01181 {
01182     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01183                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01184     const char *                pFieldName = pFieldDescriptor->pName;
01185     int                         i;
01186 
01187     appendOpenTag(pEncoderStream, pFieldName);
01188 
01189     for(i = 0; i < Value.nValue; i++)
01190     {
01191         int                     eValue = Value.pValue[i];
01192         const LLRP_tSEnumTableEntry *pEntry;
01193 
01194         for(
01195             pEntry = pFieldDescriptor->pEnumTable;
01196             NULL != pEntry->pName;
01197             pEntry++)
01198         {
01199             if(pEntry->Value == eValue)
01200             {
01201                 break;
01202             }
01203         }
01204 
01205         if(0 < i)
01206         {
01207             appendFormat(pEncoderStream, " ");
01208         }
01209 
01210         if(NULL != pEntry->pName)
01211         {
01212             appendFormat(pEncoderStream, "%s", pEntry->pName);
01213         }
01214         else
01215         {
01216             appendFormat(pEncoderStream, "%d", eValue);
01217         }
01218     }
01219 
01220     appendCloseTag(pEncoderStream, pFieldName);
01221 }
01222 
01223 static void
01224 put_enum (
01225   LLRP_tSEncoderStream *        pBaseEncoderStream,
01226   int                           eValue,
01227   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01228 {
01229     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01230                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01231     const char *                pFieldName = pFieldDescriptor->pName;
01232     const LLRP_tSEnumTableEntry *pEntry;
01233 
01234     appendOpenTag(pEncoderStream, pFieldName);
01235 
01236     for(
01237         pEntry = pFieldDescriptor->pEnumTable;
01238         NULL != pEntry->pName;
01239         pEntry++)
01240     {
01241         if(pEntry->Value == eValue)
01242         {
01243             break;
01244         }
01245     }
01246 
01247     if(NULL != pEntry->pName)
01248     {
01249         appendFormat(pEncoderStream, "%s", pEntry->pName);
01250     }
01251     else
01252     {
01253         appendFormat(pEncoderStream, "%d", eValue);
01254     }
01255 
01256     appendCloseTag(pEncoderStream, pFieldName);
01257 }
01258 
01259 static void
01260 put_reserved (
01261   LLRP_tSEncoderStream *        pBaseEncoderStream,
01262   unsigned int                  nBits)
01263 {
01264     LLRP_tSXMLTextEncoderStream * pEncoderStream =
01265                             (LLRP_tSXMLTextEncoderStream *) pBaseEncoderStream;
01266 
01267     indent(pEncoderStream, 0);
01268     appendFormat(pEncoderStream, "<!-- reserved %d bits -->\n", nBits);
01269 }
01270 
01271 static void
01272 streamConstruct_outermost (
01273   LLRP_tSXMLTextEncoderStream *   pEncoderStream,
01274   LLRP_tSXMLTextEncoder *         pEncoder)
01275 {
01276     memset(pEncoderStream, 0, sizeof *pEncoderStream);
01277     pEncoderStream->encoderStreamHdr.pEncoderStreamOps =
01278                                 &s_XMLTextEncoderStreamOps;
01279 
01280     pEncoderStream->pEncoder                = pEncoder;
01281     pEncoderStream->pEnclosingEncoderStream = NULL;
01282     pEncoderStream->pRefType                = NULL;
01283     pEncoderStream->nDepth                  = 1;
01284 }
01285 
01286 
01287 static void
01288 streamConstruct_nested (
01289   LLRP_tSXMLTextEncoderStream *   pEncoderStream,
01290   LLRP_tSXMLTextEncoderStream *   pEnclosingEncoderStream)
01291 {
01292     LLRP_tSXMLTextEncoder *       pEncoder;
01293 
01294     pEncoder = pEnclosingEncoderStream->pEncoder;
01295 
01296     memset(pEncoderStream, 0, sizeof *pEncoderStream);
01297     pEncoderStream->encoderStreamHdr.pEncoderStreamOps =
01298                                 &s_XMLTextEncoderStreamOps;
01299 
01300     pEncoderStream->pEncoder                = pEncoder;
01301     pEncoderStream->pEnclosingEncoderStream = pEnclosingEncoderStream;
01302     pEncoderStream->pRefType                = NULL;
01303     pEncoderStream->nDepth                  =
01304                             pEnclosingEncoderStream->nDepth+1;
01305 }
01306 
01307 
01308 static void
01309 putElement (
01310   LLRP_tSXMLTextEncoderStream * pEncoderStream,
01311   const LLRP_tSElement *        pElement)
01312 {
01313     const LLRP_tSTypeDescriptor *pRefType = pElement->pType;
01314 
01315     pEncoderStream->pRefType = pRefType;
01316 
01317     indent(pEncoderStream, -1);
01318     appendFormat(pEncoderStream, "<");
01319     appendPrefixedTagName(pEncoderStream, pRefType->pName);
01320     if(pRefType->bIsMessage)
01321     {
01322         appendFormat(pEncoderStream, " MessageID='%u'",
01323             ((LLRP_tSMessage *)pElement)->MessageID);
01324     }
01325 
01326     if(NULL == pEncoderStream->pEnclosingEncoderStream)
01327     {
01328         tNamespaceList          NamespaceList;
01329         const LLRP_tSNamespaceDescriptor *pNamespaceDescriptor;
01330         int                     iNSD;
01331 
01332         memset(&NamespaceList, 0, sizeof NamespaceList);
01333 
01334         LLRP_Element_walk(pElement,
01335             discoverNamespaces, (void*)&NamespaceList,
01336             0, 12);
01337 
01338         /* Emit the namespace cookie for each */
01339         for(iNSD = 0; iNSD < NamespaceList.nNamespaceDescriptor; iNSD++)
01340         {
01341             pNamespaceDescriptor = NamespaceList.apNamespaceDescriptor[iNSD];
01342 
01343             appendFormat(pEncoderStream, "\n");
01344             indent(pEncoderStream, 0);
01345             appendFormat(pEncoderStream, "xmlns:%s='%s'",
01346                 pNamespaceDescriptor->pPrefix,
01347                 pNamespaceDescriptor->pURI);
01348             /*
01349              * If this is the default namespace then emit the assigment.
01350              */
01351             if(0 == strcmp(pNamespaceDescriptor->pPrefix, "llrp"))
01352             {
01353                 appendFormat(pEncoderStream, "\n");
01354                 indent(pEncoderStream, 0);
01355                 appendFormat(pEncoderStream, "xmlns='%s'",
01356                     pNamespaceDescriptor->pURI);
01357             }
01358         }
01359     }
01360     appendFormat(pEncoderStream, ">\n");
01361 
01362     pRefType->pfEncode(pElement, &pEncoderStream->encoderStreamHdr);
01363 
01364     indent(pEncoderStream, -1);
01365     appendCloseTag(pEncoderStream, pRefType->pName);
01366 }
01367 
01368 
01369 static int
01370 discoverNamespaces (
01371   const LLRP_tSElement *        pElement,
01372   void *                        pArg)
01373 {
01374     tNamespaceList *            pNSL = (tNamespaceList *) pArg;
01375     const LLRP_tSNamespaceDescriptor *pNamespaceDescriptor;
01376     int                         iNSD;
01377 
01378     pNamespaceDescriptor = pElement->pType->pNamespaceDescriptor;
01379 
01380     for(iNSD = 0; iNSD < pNSL->nNamespaceDescriptor; iNSD++)
01381     {
01382         if(pNSL->apNamespaceDescriptor[iNSD] == pNamespaceDescriptor)
01383         {
01384             /* Already have it */
01385             return 0;
01386         }
01387     }
01388 
01389     /* if we get here this namespace isn't already in the list */
01390     if(MAX_NS > pNSL->nNamespaceDescriptor)
01391     {
01392         iNSD = pNSL->nNamespaceDescriptor++;
01393         pNSL->apNamespaceDescriptor[iNSD] = pNamespaceDescriptor;
01394     }
01395 
01396     return 0;
01397 }
01398 
01399 
01400 static void
01401 nestSubParameter (
01402   LLRP_tSXMLTextEncoderStream * pEncoderStream,
01403   const LLRP_tSParameter *      pParameter)
01404 {
01405     const LLRP_tSElement *      pElement = (const LLRP_tSElement *)pParameter;
01406     LLRP_tSXMLTextEncoderStream NestEncoderStream;
01407 
01408     if(NULL == pParameter)
01409     {
01410         return;
01411     }
01412 
01413     streamConstruct_nested(&NestEncoderStream, pEncoderStream);
01414 
01415     putElement(&NestEncoderStream, pElement);
01416 }
01417 
01418 static void
01419 indent (
01420   LLRP_tSXMLTextEncoderStream * pEncoderStream,
01421   int                           adjust)
01422 {
01423     int                         n = pEncoderStream->nDepth + adjust;
01424     int                         i;
01425 
01426     for(i = 0; i < n; i++)
01427     {
01428         appendFormat(pEncoderStream, "  ");
01429     }
01430 }
01431 
01432 static void
01433 appendOpenTag (
01434   LLRP_tSXMLTextEncoderStream * pEncoderStream,
01435   const char *                  pName)
01436 {
01437     indent(pEncoderStream, 0);
01438     appendFormat(pEncoderStream, "<");
01439     appendPrefixedTagName(pEncoderStream, pName);
01440     appendFormat(pEncoderStream, ">");
01441 }
01442 
01443 static void
01444 appendCloseTag (
01445   LLRP_tSXMLTextEncoderStream * pEncoderStream,
01446   const char *                  pName)
01447 {
01448     appendFormat(pEncoderStream, "</");
01449     appendPrefixedTagName(pEncoderStream, pName);
01450     appendFormat(pEncoderStream, ">\n");
01451 }
01452 
01453 static void
01454 appendPrefixedTagName (
01455   LLRP_tSXMLTextEncoderStream * pEncoderStream,
01456   const char *                  pName)
01457 {
01458     const LLRP_tSTypeDescriptor *pRefType = pEncoderStream->pRefType;
01459     const char *                pPrefix =
01460                                     pRefType->pNamespaceDescriptor->pPrefix;
01461 
01462     if(0 != strcmp("llrp", pPrefix))
01463     {
01464         appendFormat(pEncoderStream, "%s:%s", pPrefix, pName);
01465     }
01466     else
01467     {
01468         appendFormat(pEncoderStream, "%s", pName);
01469     }
01470 }
01471 
01472 static void
01473 appendFormat (
01474   LLRP_tSXMLTextEncoderStream * pEncoderStream,
01475   char *                        pFmtStr,
01476                                 ...)
01477 {
01478     LLRP_tSXMLTextEncoder *     pEncoder = pEncoderStream->pEncoder;
01479     char                        aHoldBuf[256u];
01480     int                         nHoldBuf;
01481     va_list                     ap;
01482 
01483     /* If overflow already detected, bail */
01484     if(pEncoder->bOverflow)
01485     {
01486         return;
01487     }
01488 
01489     va_start(ap, pFmtStr);
01490     vsnprintf(aHoldBuf, sizeof aHoldBuf, pFmtStr, ap);
01491     va_end(ap);
01492 
01493     nHoldBuf = strlen(aHoldBuf);
01494 
01495     if(pEncoder->iNext + nHoldBuf >= pEncoder->nBuffer)
01496     {
01497         pEncoder->bOverflow = 1;
01498         return;
01499     }
01500 
01501     strcpy((char *)&pEncoder->pBuffer[pEncoder->iNext], aHoldBuf);
01502 
01503     pEncoder->iNext += nHoldBuf;
01504 }
01505 
01506 
01523 LLRP_tResultCode
01524 LLRP_toXMLString (
01525   const LLRP_tSElement *        pElement,
01526   char *                        pBuffer,
01527   int                           nBuffer)
01528 {
01529     LLRP_tSXMLTextEncoder *     pXMLEncoder;
01530     LLRP_tSEncoder *            pEncoder;
01531     const LLRP_tSErrorDetails * pError;
01532 
01533     /*
01534      * Make sure the element is not NULL.
01535      */
01536     if(NULL == pElement)
01537     {
01538         strcpy(pBuffer, "ERROR: NULL pMessage to printXMLMessage\n");
01539         return LLRP_RC_MiscError;
01540     }
01541 
01542     /*
01543      * Construct an XML encoder
01544      */
01545     pXMLEncoder = LLRP_XMLTextEncoder_construct((unsigned char *)pBuffer, nBuffer);
01546     if(NULL == pXMLEncoder)
01547     {
01548         strcpy(pBuffer, "ERROR: XMLTextEncoder_construct failed\n");
01549         return LLRP_RC_MiscError;
01550     }
01551 
01552     /*
01553      * Essentially cast the XMLEncoder as a generic encoder.
01554      */
01555     pEncoder = &pXMLEncoder->encoderHdr;
01556 
01557     /*
01558      * Now let the encoding mechanism do its thing.
01559      */
01560     LLRP_Encoder_encodeElement(pEncoder, pElement);
01561 
01562     /*
01563      * Check the outcome in the error details.
01564      * If there is a problem, return the error rather
01565      * than the assumed to be useless string.
01566      */
01567     pError = &pEncoder->ErrorDetails;
01568 
01569     if(LLRP_RC_OK != pError->eResultCode)
01570     {
01571         sprintf(pBuffer, "ERROR: %s XML text failed, %s\n",
01572             pElement->pType->pName,
01573             pError->pWhatStr ? pError->pWhatStr : "no reason given");
01574 
01575         LLRP_Encoder_destruct(pEncoder);
01576 
01577         return pError->eResultCode;
01578     }
01579 
01580     /*
01581      * Check if the XML fit in the buffer.
01582      */
01583     if(pXMLEncoder->bOverflow)
01584     {
01585         strcpy(pBuffer, "ERROR: Buffer overflow\n");
01586         LLRP_Encoder_destruct(pEncoder);
01587         return LLRP_RC_MiscError;
01588     }
01589 
01590     /*
01591      * Done with the XML encoder.
01592      */
01593     LLRP_Encoder_destruct(pEncoder);
01594 
01595     return LLRP_RC_OK;
01596 }
01597 


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