0001 
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 
0015 
0016 
0017 
0018 
0019 
0020 
0021 
0022 
0023 
0024 
0025 
0026 
0027 
0028 
0029 
0030 
0031 
0032 
0033 
0034 
0035 
0036 
0037 #include <string.h>
0038 #include <stdio.h>
0039 #include <stdlib.h>
0040 #include <errno.h>
0041 #include <fcntl.h>
0042 #include <ctype.h>
0043 
0044 #include <ndrstandard.h>
0045 #include <ndebug.h>
0046 #include <ubf.h>
0047 #include <ubf_int.h>
0048 #include <ubfutil.h>
0049 #include <fdatatype.h>
0050 #include <ubfview.h>
0051 #include <nstd_int.h>
0052 
0053 
0054 #define IS_NOT_PRINTABLE(X) !(isprint(X) && !iscntrl(X))
0055 
0056 
0057 
0058 char HEX_TABLE[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
0059 
0060 
0061 
0062 
0063 
0064 
0065 
0066 
0067 
0068 
0069 
0070 
0071 expublic int ndrx_get_nonprintable_char_tmpspace(char *str, int len)
0072 {
0073     int ret=0;
0074     int i;
0075 
0076     for (i=0; i<len; i++)
0077     {
0078         if (IS_NOT_PRINTABLE(str[i]))
0079         {
0080             ret+=3;
0081         }
0082         else if ('\\'==str[i])
0083         {
0084             ret+=2;
0085         }
0086         else
0087         {
0088             ret++;
0089         }
0090     }
0091     return ret;
0092 }
0093 
0094 
0095 
0096 expublic void ndrx_build_printable_string(char *out, int out_len, char *in, int in_len)
0097 {
0098     int i;
0099     int cur = 0;
0100     
0101     for (i=0; i<in_len; i++)
0102     {
0103         if (IS_NOT_PRINTABLE(in[i]))
0104         {
0105             
0106 
0107 
0108             if (out_len - cur > 3)
0109             {
0110                 
0111                 out[cur++] = '\\';
0112                 out[cur++] = HEX_TABLE[(in[i]>>4)&0x0f];
0113                 out[cur++] = HEX_TABLE[in[i]&0x0f];
0114             }
0115             else
0116             {
0117                 break;
0118             }
0119         }
0120         else if ('\\'==in[i])
0121         {
0122             
0123             if (out_len - cur > 2)
0124             {
0125                 out[cur++] = '\\';
0126                 out[cur++] = '\\';
0127             }
0128             else
0129             {
0130                 break;
0131             }
0132         }
0133         else if (out_len - cur > 1)
0134         {
0135             
0136             out[cur++] = in[i];
0137         }
0138         else
0139         {
0140             break;
0141         }
0142     }
0143     
0144     if (cur<out_len)
0145     {
0146         out[cur] = EXEOS;
0147     }
0148 }
0149 
0150 
0151 
0152 
0153 
0154 
0155 expublic int ndrx_get_num_from_hex(char c)
0156 {
0157     int ret=EXFAIL;
0158     int i;
0159 
0160     for (i=0; i< sizeof(HEX_TABLE); i++)
0161     {
0162         if (toupper(HEX_TABLE[i])==toupper(c))
0163         {
0164             ret=i;
0165             break;
0166         }
0167     }
0168 
0169     return ret;
0170 }
0171 
0172 
0173 
0174 
0175 
0176 
0177 
0178 
0179 expublic int ndrx_normalize_string(char *str, int *out_len)
0180 {
0181     int ret=EXSUCCEED;
0182     int real=0;
0183     int data=0;
0184     int len = strlen(str);
0185     int high, low;
0186     while (data<len)
0187     {
0188         if (str[data]=='\\')
0189         {
0190             if (data+1>=len)
0191             {
0192                 UBF_LOG(log_error, "String terminates after prefix \\");
0193                 return EXFAIL; 
0194             }
0195             else if (str[data+1]=='\\') 
0196             {
0197                 str[real] = str[data];
0198                 data+=2;
0199                 real++;
0200             } 
0201             else if (data+2>=len)
0202             {
0203                 UBF_LOG(log_error, "Hex code does not follow at the"
0204                                                 " end of string for \\");
0205                 return EXFAIL; 
0206             }
0207             else
0208             {
0209                 
0210                 high = ndrx_get_num_from_hex(str[data+1]);
0211                 low = ndrx_get_num_from_hex(str[data+2]);
0212                 if (EXFAIL==high||EXFAIL==low)
0213                 {
0214                     UBF_LOG(log_error, "Invalid hex number 0x%c%c",
0215                                                     str[data+1], str[data+2]);
0216                     return EXFAIL; 
0217                 }
0218                 else
0219                 {
0220                     
0221                     str[real]= high<<4 | low;
0222                     real++;
0223                     data+=3;
0224                 }
0225             }
0226         }
0227         else 
0228         {
0229             str[real] = str[data];
0230             real++;
0231             data++;
0232         }
0233     }
0234     
0235 
0236     *out_len = real;
0237     
0238     return ret;
0239 }
0240 
0241 
0242 
0243 
0244 
0245 
0246 
0247 expublic void ndrx_debug_dump_UBF(int lev, char *title, UBFH *p_ub)
0248 {
0249     ndrx_debug_t * dbg = debug_get_ndrx_ptr();
0250     if (dbg->level>=lev)
0251     {
0252         
0253         NDRX_LOG(lev, "%s", title);
0254         
0255         ndrx_debug_lock((ndrx_debug_file_sink_t*)dbg->dbg_f_ptr);
0256         Bfprint(p_ub, ((ndrx_debug_file_sink_t*)dbg->dbg_f_ptr)->fp);
0257         ndrx_debug_unlock((ndrx_debug_file_sink_t*)dbg->dbg_f_ptr);
0258     }
0259 }
0260 
0261 
0262 
0263 
0264 
0265 
0266 
0267 expublic void ndrx_debug_dump_UBF_ubflogger(int lev, char *title, UBFH *p_ub)
0268 {
0269     ndrx_debug_t * dbg = debug_get_ubf_ptr();
0270     if (dbg->level>=lev)
0271     {
0272         UBF_LOG(lev, "%s", title);
0273         
0274         ndrx_debug_lock((ndrx_debug_file_sink_t*)dbg->dbg_f_ptr);
0275         Bfprint(p_ub,((ndrx_debug_file_sink_t*)dbg->dbg_f_ptr)->fp);
0276         ndrx_debug_unlock((ndrx_debug_file_sink_t*)dbg->dbg_f_ptr);
0277     }
0278 }
0279 
0280 
0281 
0282 
0283 
0284 expublic void ndrx_debug_dump_UBF_hdr_ubflogger(int lev, char *title, UBFH *p_ub)
0285 {
0286     UBF_header_t *hdr = (UBF_header_t*)p_ub;
0287     ndrx_debug_t * dbg = debug_get_ubf_ptr();
0288     
0289     if (dbg->level>=lev)
0290     {
0291         UBF_LOG(lev, "****************** START OF %p UBF HEADER ******************", p_ub);
0292         UBF_LOG(lev, "%s: UBF_header_t.buffer_type=[%c] offset=%d", title,
0293                 hdr->buffer_type, EXOFFSET(UBF_header_t, buffer_type));
0294         UBF_LOG(lev, "%s: UBF_header_t.version=[%d]  offset=%d", title,
0295                 (unsigned int)hdr->version, EXOFFSET(UBF_header_t, version));
0296         UBF_LOG(lev, "%s: UBF_header_t.magic=[%x%x%x%x]  offset=%d", title,
0297                 (unsigned int)hdr->magic[0], (unsigned int)hdr->magic[1], 
0298                 (unsigned int)hdr->magic[2], (unsigned int)hdr->magic[3],
0299                 EXOFFSET(UBF_header_t, magic));
0300         UBF_LOG(lev, "%s: UBF_header_t.cache_long_off=[%d] offset=%d", title,
0301                 hdr->cache_long_off, EXOFFSET(UBF_header_t, cache_long_off));
0302         UBF_LOG(lev, "%s: UBF_header_t.cache_char_off=[%d] offset=%d", title,
0303                 hdr->cache_char_off, EXOFFSET(UBF_header_t, cache_char_off));
0304         UBF_LOG(lev, "%s: UBF_header_t.cache_float_off=[%d] offset=%d", title,
0305                 hdr->cache_float_off, EXOFFSET(UBF_header_t, cache_float_off));
0306         UBF_LOG(lev, "%s: UBF_header_t.cache_double_off=[%d] offset=%d", title,
0307                 hdr->cache_double_off, EXOFFSET(UBF_header_t, cache_double_off));
0308         UBF_LOG(lev, "%s: UBF_header_t.cache_string_off=[%d] offset=%d", title,
0309                 hdr->cache_string_off, EXOFFSET(UBF_header_t, cache_string_off));
0310         UBF_LOG(lev, "%s: UBF_header_t.cache_carray_off=[%d] offset=%d", title,
0311                 hdr->cache_carray_off, EXOFFSET(UBF_header_t, cache_carray_off));
0312         UBF_LOG(lev, "%s: UBF_header_t.cache_ptr_off=[%d] offset=%d", title,
0313                 hdr->cache_ptr_off, EXOFFSET(UBF_header_t, cache_ptr_off));
0314         UBF_LOG(lev, "%s: UBF_header_t.cache_ubf_off=[%d] offset=%d", title,
0315                 hdr->cache_ubf_off, EXOFFSET(UBF_header_t, cache_ubf_off));
0316         UBF_LOG(lev, "%s: UBF_header_t.cache_view_off=[%d] offset=%d", title,
0317                 hdr->cache_view_off, EXOFFSET(UBF_header_t, cache_view_off));
0318         UBF_LOG(lev, "%s: UBF_header_t.buf_len=[%d] offset=%d", title,
0319                 hdr->buf_len, EXOFFSET(UBF_header_t, buf_len));
0320         UBF_LOG(lev, "%s: UBF_header_t.opts=[%d] offset=%d", title,
0321                 hdr->opts, EXOFFSET(UBF_header_t, opts));
0322         UBF_LOG(lev, "%s: UBF_header_t.bytes_used=[%d] offset=%d", title,
0323                 hdr->bytes_used, EXOFFSET(UBF_header_t, bytes_used));
0324 #if EX_ALIGNMENT_BYTES == 8
0325         UBF_LOG(lev, "%s: UBF_header_t.padding1=[%ld] offset=%d", title,
0326                 hdr->padding1, EXOFFSET(UBF_header_t, padding1));
0327 #endif
0328         UBF_LOG(lev, "%s: UBF_header_t.bfldid=[%d] offset=%d", title,
0329                 hdr->bfldid, EXOFFSET(UBF_header_t, buffer_type));
0330 #if EX_ALIGNMENT_BYTES == 8
0331         UBF_LOG(lev, "%s: UBF_header_t.passing2=[%d] offset=%d", title,
0332                 hdr->passing2, EXOFFSET(UBF_header_t, passing2));
0333 #endif
0334         UBF_LOG(lev, "******************** END OF %p UBF HEADER ******************", p_ub);
0335     }
0336 }
0337 
0338 
0339 
0340 
0341 
0342 
0343 
0344 
0345 expublic void ndrx_debug_dump_VIEW_ubflogger(int lev, char *title, char *cstruct, char *view)
0346 {
0347     ndrx_debug_t * dbg = debug_get_ubf_ptr();
0348     
0349     if (EXSUCCEED==ndrx_view_init() && dbg->level>=lev)
0350     {
0351         UBF_LOG(lev, "%s: VIEW [%s]", title, view);
0352         Bvfprint(cstruct, view, dbg->dbg_f_ptr);
0353     }
0354 }
0355 
0356 
0357 
0358 
0359 
0360 
0361 
0362 expublic long ndrx_Bneeded(BFLDOCC nrfields, BFLDLEN totsize)
0363 {
0364     
0365     return sizeof(UBF_header_t) + totsize 
0366             + nrfields*(EX_ALIGNMENT_BYTES+sizeof(UBF_string_t));
0367 }
0368 
0369