Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Unified Buffer Format utils
0003  *
0004  * @file ubfutil.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdint.h>
0037 #include <stdlib.h>
0038 #include <memory.h>
0039 #include <errno.h>
0040 #include <dlfcn.h>
0041 
0042 #include <ndrstandard.h>
0043 #include <ndebug.h>
0044 #include <ndrxdcmn.h>
0045 #include <userlog.h>
0046 #include <ubf.h>
0047 #include <ubfutil.h>
0048 /*---------------------------Externs------------------------------------*/
0049 /*---------------------------Macros-------------------------------------*/
0050 /*---------------------------Enums--------------------------------------*/
0051 /*---------------------------Typedefs-----------------------------------*/
0052 /*---------------------------Globals------------------------------------*/
0053 /*---------------------------Statics------------------------------------*/
0054 
0055 /**
0056  * Convert c structure to UBF buffer
0057  * @param map buffer/c mapping
0058  * @param p_ub UBF buffer
0059  * @param c_struct c struct
0060  * @return SUCCEED/FAIL
0061  */
0062 expublic int atmi_cvt_c_to_ubf(ubf_c_map_t *map, void *c_struct, UBFH *p_ub, long *rules)
0063 {
0064     int ret = EXSUCCEED;
0065     
0066     while (BBADFLDID!=map->fld)
0067     {
0068         if (*rules & UBFUTIL_EXPORT)
0069         {
0070             char *data_ptr = (char *)(c_struct+map->offset);
0071             
0072             if (BFLD_INT==map->ftype)
0073             {
0074                 long l;
0075                 int *i = (int *)data_ptr;
0076                 
0077                 l = *i;
0078                 
0079                 if (EXSUCCEED!=CBchg(p_ub, map->fld, map->occ, (char *)&l, map->buf_size, 
0080                     BFLD_LONG))
0081                 {
0082                     int be = Berror;
0083                     NDRX_LOG(log_error, "Failed to install mapped long "
0084                             "field %d:[%s] to UBF buffer: %s", 
0085                             Bfname(map->fld), Bstrerror(be));
0086                     EXFAIL_OUT(ret);
0087                 }
0088             }
0089             else if (EXSUCCEED!=CBchg(p_ub, map->fld, map->occ, data_ptr, map->buf_size, 
0090                     map->ftype))
0091             {
0092                 int be = Berror;
0093                 NDRX_LOG(log_error, "Failed to install field %d:[%s] to UBF buffer: %s", 
0094                         Bfname(map->fld), Bstrerror(be));
0095                 EXFAIL_OUT(ret);
0096             }
0097         }
0098         rules++;
0099         map++;
0100     }
0101 out:
0102     return ret;
0103 }
0104 
0105 /**
0106  * Convert UBF buffer data to c structure
0107  * @param map buffer/c mapping
0108  * @param p_ub UBF buffer
0109  * @param c_struct c struct
0110  * @return SUCCEED/FAIL
0111  */
0112 expublic int atmi_cvt_ubf_to_c(ubf_c_map_t *map, UBFH *p_ub, void *c_struct, long *rules)
0113 {
0114     int ret = EXSUCCEED;
0115     BFLDLEN len;
0116     while (BBADFLDID!=map->fld)
0117     {
0118         if (*rules & UBFUTIL_EXPORT)
0119         {
0120             char *data_ptr = (char *)(c_struct+map->offset);
0121             len = map->buf_size; /* have the buffer size */
0122             
0123             if (BFLD_INT==map->ftype)
0124             {
0125                 long l;
0126                 int *i = (int *)data_ptr;
0127                 if (EXSUCCEED!=CBget(p_ub, map->fld, map->occ, (char *)&l, 0, BFLD_LONG))
0128                 {
0129                     int be = Berror;
0130                     NDRX_LOG(log_error, "Failed to get mapped in field %d:[%s"
0131                             "] from UBF buffer: %s", 
0132                             map->fld, Bfname(map->fld), Bstrerror(be));
0133 
0134                     if (*rules & UBFUTIL_OPTIONAL)
0135                     {
0136                         NDRX_LOG(log_warn, "Field %d:[%s] is optional - continue");
0137                     }
0138                     else
0139                     {
0140                         EXFAIL_OUT(ret);
0141                     }
0142                 }
0143                 else
0144                 {
0145                     *i = (int)l;
0146                 }
0147             }
0148             else if (EXSUCCEED!=CBget(p_ub, map->fld, map->occ, data_ptr, &len, map->ftype))
0149             {
0150                 int be = Berror;
0151                 NDRX_LOG(log_error, "Failed to get field %d:[%s] from UBF buffer: %s", 
0152                         map->fld, Bfname(map->fld), Bstrerror(be));
0153 
0154                 if (*rules & UBFUTIL_OPTIONAL)
0155                 {
0156                     NDRX_LOG(log_warn, "Field %d:[%s] is optional - continue");
0157                 }
0158                 else
0159                 {
0160                     EXFAIL_OUT(ret);
0161                 }
0162             }
0163         }
0164         rules++;
0165         map++;
0166     }
0167     
0168 out:
0169     return ret;
0170 }
0171 /* vim: set ts=4 sw=4 et smartindent: */