Main Page   Class Hierarchy   Compound List   File List   Compound Members  

BufferUtils.cpp

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #include "BufferUtils.hpp"
00021 #include "Utils.hpp"
00022 #include <cstring>
00023 
00024 #include "GeneratorTrace.hpp"
00025 
00026 T_UnsignedInteger32 convert_char_to_ul(char *P_data) {
00027   char                *L_tmp ;
00028   T_UnsignedInteger32  L_result ;
00029   L_result = strtoul_f(P_data, &L_tmp, 10);
00030   return (L_result);
00031 }
00032 
00033 void convert_ul_to_bin_network(unsigned char      *P_buf, 
00034                                size_t              P_size, 
00035                                T_UnsignedInteger32 P_val) {
00036   T_UnsignedInteger32 L_val = P_val ;
00037   unsigned char      *L_ul = (unsigned char*) &L_val ;
00038   unsigned char      *L_res = P_buf ;
00039 
00040   GEN_DEBUG(1, "convert_ul_to_bin_network ("
00041         << &P_buf << ", size: " << P_size << ", Val: 0x" 
00042         << iostream_hex << P_val << iostream_dec
00043         << ")");
00044   
00045 #ifdef DEBUG_MODE
00046   static char   L_hexa_buf [50] ;
00047   unsigned char*L_cur ;
00048 
00049   L_cur = (unsigned char*)&P_val;
00050   pretty_binary_buffer(L_cur, sizeof(T_UnsignedInteger32), L_hexa_buf);
00051   GEN_DEBUG(1, "host val: " << L_hexa_buf);
00052 #endif
00053 
00054   GEN_DEBUG(1, "decal size: "
00055                << (sizeof(T_UnsignedInteger32) - P_size));
00056 
00057   L_val = htonl_f (L_val) ;
00058 
00059 #ifdef DEBUG_MODE
00060   L_cur = (unsigned char*)&L_val;
00061   pretty_binary_buffer(L_cur, sizeof(T_UnsignedInteger32), L_hexa_buf);
00062   GEN_DEBUG(1, "htonl   : " << L_hexa_buf);
00063 #endif
00064 
00065   L_ul  += (sizeof(T_UnsignedInteger32) - P_size);
00066 
00067   memcpy ( (void *) L_res,
00068            (void *) L_ul,
00069            P_size) ;
00070 
00071 #ifdef DEBUG_MODE
00072   L_cur = L_res;
00073   pretty_binary_buffer(L_cur, P_size, L_hexa_buf);
00074   GEN_DEBUG(1, "net val: " << L_hexa_buf);
00075 #endif
00076 
00077   GEN_DEBUG(1, "convert_ul_to_bin_network() end");
00078 }
00079 
00080 void convert_l_to_bin_network(unsigned char   *P_buf, 
00081                               size_t           P_size, 
00082                               T_Integer32      P_val) {
00083   T_Integer32         L_val = P_val ;
00084   unsigned char      *L_ul = (unsigned char*) &L_val ;
00085   unsigned char      *L_res = P_buf ;
00086 
00087   GEN_DEBUG(1, "convert_l_to_bin_network ("
00088         << &P_buf << ", size: " << P_size << ", Val: 0x" 
00089         << iostream_hex << P_val << iostream_dec
00090         << ")");
00091   
00092 #ifdef DEBUG_MODE
00093   static char   L_hexa_buf [50] ;
00094   unsigned char*L_cur ;
00095 
00096   L_cur = (unsigned char*)&P_val;
00097   pretty_binary_buffer(L_cur, sizeof(T_Integer32), L_hexa_buf);
00098   GEN_DEBUG(1, "host val: " << L_hexa_buf);
00099 #endif
00100 
00101   GEN_DEBUG(1, "decal size: "
00102                << (sizeof(T_UnsignedInteger32) - P_size));
00103 
00104   L_val = htonl_f (L_val) ;
00105 
00106 #ifdef DEBUG_MODE
00107   L_cur = (unsigned char*)&L_val;
00108   pretty_binary_buffer(L_cur, sizeof(T_Integer32), L_hexa_buf);
00109   GEN_DEBUG(1, "htonl   : " << L_hexa_buf);
00110 #endif
00111 
00112   L_ul  += (sizeof(T_UnsignedInteger32) - P_size);
00113 
00114   memcpy ( (void *) L_res,
00115            (void *) L_ul,
00116            P_size) ;
00117 
00118 #ifdef DEBUG_MODE
00119   L_cur = L_res;
00120   pretty_binary_buffer(L_cur, P_size, L_hexa_buf);
00121   GEN_DEBUG(1, "net val: " << L_hexa_buf);
00122 #endif
00123 
00124   GEN_DEBUG(1, "convert_l_to_bin_network() end");
00125 }
00126 
00127 T_UnsignedInteger32 convert_bin_network_to_ul(unsigned char*P_buf, size_t P_size) {
00128 
00129   T_UnsignedInteger32 L_ret = 0 ;
00130   unsigned char      *L_ptr     ;
00131 
00132   GEN_DEBUG(1, "convert_bin_network_to_ul ("
00133         << &P_buf << ", size: " << P_size
00134         << ")");
00135 
00136 #ifdef DEBUG_MODE
00137   static char   L_hexa_buf [50] ;
00138   unsigned char*L_cur ;
00139 
00140   L_cur = P_buf;
00141   pretty_binary_buffer(L_cur, P_size, L_hexa_buf);
00142   GEN_DEBUG(1, "net val: " << L_hexa_buf);
00143 #endif
00144 
00145   GEN_DEBUG(1, "decal size: "
00146                << (sizeof(T_UnsignedInteger32) - P_size));
00147 
00148   if (P_size > sizeof(T_UnsignedInteger32)) {
00149      GEN_ERROR(E_GEN_FATAL_ERROR,"Current Size: "
00150                << P_size << " is greater than expecting one: "
00151                <<sizeof(T_UnsignedInteger32));
00152   } else {
00153     // from network => MSB representation
00154     L_ptr = (unsigned char*) &L_ret ;
00155     L_ptr += (sizeof(T_UnsignedInteger32) - P_size) ;
00156     memcpy( (void*)  L_ptr,
00157             (void*)  P_buf,
00158             (size_t) P_size ) ;
00159     // convert to local representation
00160     L_ret = ntohl_f (L_ret) ;
00161   }
00162 
00163 #ifdef DEBUG_MODE
00164   L_cur = (unsigned char*)&L_ret;
00165   pretty_binary_buffer(L_cur, sizeof(T_UnsignedInteger32), L_hexa_buf);
00166   GEN_DEBUG(1, "host val: " << L_hexa_buf);
00167 #endif
00168 
00169   return (L_ret) ;
00170 }
00171 
00172 T_Integer32 convert_bin_network_to_l(unsigned char*P_buf, size_t P_size) {
00173 
00174   T_Integer32      L_ret = 0 ;
00175   unsigned char   *L_ptr     ;
00176 #if LONG_MAX==9223372036854775807L
00177   bool             L_neg_value = false ;
00178 #endif
00179   unsigned int     L_decal = sizeof(T_Integer32) - P_size;
00180 
00181   GEN_DEBUG(1, "convert_bin_network_to_l ("
00182         << &P_buf << ", size: " << P_size
00183         << ")");
00184 
00185 #ifdef DEBUG_MODE
00186   static char   L_hexa_buf [50] ;
00187   unsigned char*L_cur ;
00188 
00189   L_cur = P_buf;
00190   pretty_binary_buffer(L_cur, P_size, L_hexa_buf);
00191   GEN_DEBUG(1, "net val: " << L_hexa_buf);
00192 #endif
00193 
00194   GEN_DEBUG(1, "decal size: " << L_decal);
00195 
00196   if (P_size > sizeof(T_Integer32)) {
00197      GEN_ERROR(E_GEN_FATAL_ERROR,"Current Size: "
00198                << P_size << " is greater than expecting one: "
00199                <<sizeof(T_Integer32));
00200   } else {
00201 #if LONG_MAX==9223372036854775807L
00202     // check if value < 0
00203     if (P_buf[0] & 128) { 
00204       L_neg_value = true ;
00205     }
00206 #endif
00207     
00208     // from network => MSB representation
00209     L_ptr = (unsigned char*) &L_ret ;
00210     L_ptr += L_decal;
00211     memcpy( (void*)  L_ptr,
00212             (void*)  P_buf,
00213             (size_t) P_size ) ;
00214     // convert to local representation
00215     L_ret = ntohl_f (L_ret) ;
00216 
00217 #if LONG_MAX==9223372036854775807L
00218     if (L_neg_value == true) { 
00219       if (L_decal > 0) { 
00220         memset(L_ptr, 0xff, L_decal);
00221       }
00222     }
00223 #endif
00224   }
00225 
00226 #ifdef DEBUG_MODE
00227   L_cur = (unsigned char*)&L_ret;
00228   pretty_binary_buffer(L_cur, sizeof(T_Integer32), L_hexa_buf);
00229   GEN_DEBUG(1, "host val: " << L_hexa_buf);
00230 #endif
00231 
00232   return (L_ret) ;
00233 }
00234 
00235 
00236 void convert_bin_to_hexa_char(unsigned char*P_buf, size_t P_size, char* P_res) {
00237   
00238   unsigned long  L_i ;
00239   char          *L_res = P_res ;
00240 
00241   for(L_i = 0; L_i < P_size; L_i++) {
00242     (void)sprintf(L_res,"%02x", (int)P_buf[L_i]);
00243     L_res += 2 ;
00244   }
00245 
00246 }
00247 
00248 
00249 unsigned char *convert_hexa_char_to_bin(char* P_data, size_t *P_bufSize) {
00250 
00251   char           L_value[3]        ;
00252   unsigned char *L_buf      = NULL ;
00253   int            L_i, L_k          ;
00254   unsigned int   L_binValue        ;
00255   
00256   *P_bufSize = (size_t) 0 ;
00257 
00258   if ((strlen(P_data) % 2) == 0) {
00259     *P_bufSize = (size_t) strlen(P_data) / 2 ;
00260     ALLOC_TABLE(L_buf, unsigned char*, 
00261                 sizeof(unsigned char), (*P_bufSize));
00262     L_k = 0 ;
00263     L_value[2] = '\0' ;
00264     for (L_i=0 ; L_i < (int)(*P_bufSize); L_i++) {
00265       L_value[0]=P_data[L_k] ;
00266       L_value[1]=P_data[L_k+1] ;
00267       sscanf(L_value, "%x", &L_binValue);
00268       L_buf[L_i] = (unsigned char) L_binValue ;
00269       L_k += 2 ;
00270     }
00271     
00272   } 
00273 
00274   return (L_buf) ;
00275 }
00276 
00277 void pretty_binary_buffer (unsigned char*P_buf, size_t P_size, char* P_res) {
00278   
00279   unsigned long  L_i ;
00280   char          *L_res = P_res ;
00281 
00282   for(L_i = 0; L_i < P_size; L_i++) {
00283     (void)sprintf(L_res,"%02x ", (int)P_buf[L_i]);
00284     L_res += 3 ;
00285   }
00286 
00287 }
00288 
00289 void pretty_binary_printable_buffer (unsigned char*P_buf, size_t P_size, char* P_res, char *P_print) {
00290   
00291   unsigned long  L_i ;
00292   char          *L_res = P_res ;
00293   char          *L_print = P_print ;
00294 
00295   for(L_i = 0; L_i < P_size; L_i++) {
00296     (void)sprintf(L_res,"%02x ", (int)P_buf[L_i]);
00297     (void)sprintf(L_print,"%c", ((((int)P_buf[L_i] >= 32) && ((int)P_buf[L_i] <= 126)) ? P_buf[L_i] : '.'));
00298     L_res += 3 ;
00299     L_print += 1 ;
00300   }
00301 
00302 }
00303 T_UnsignedInteger64 convert_bin_network_to_ull(unsigned char*P_buf, 
00304                                                size_t        P_size) {
00305 
00306   T_UnsignedInteger64 L_ret = 0 ;
00307   unsigned char      *L_ptr     ;
00308 
00309   GEN_DEBUG(1, "convert_bin_network_to_ull ("
00310         << &P_buf << ", size: " << P_size
00311         << ")");
00312 
00313 #ifdef DEBUG_MODE
00314   static char   L_hexa_buf [50] ;
00315   unsigned char*L_cur ;
00316 
00317   L_cur = P_buf;
00318   pretty_binary_buffer(L_cur, P_size, L_hexa_buf);
00319   GEN_DEBUG(1, "net val: " << L_hexa_buf);
00320 #endif
00321 
00322   GEN_DEBUG(1, "decal size: "
00323                << (sizeof(T_UnsignedInteger64) - P_size));
00324 
00325   if (P_size > sizeof(T_UnsignedInteger64)) {
00326      GEN_ERROR(E_GEN_FATAL_ERROR,"Current Size: "
00327                << P_size << " is greater than expecting one: "
00328                <<sizeof(T_UnsignedInteger64));
00329   } else {
00330     // from network => MSB representation
00331     L_ptr = (unsigned char*) &L_ret ;
00332     L_ptr += (sizeof(T_UnsignedInteger64) - P_size) ;
00333     memcpy( (void*)  L_ptr,
00334             (void*)  P_buf,
00335             (size_t) P_size ) ;
00336     // convert to local representation
00337     L_ret = ntohll_f (L_ret) ;
00338   }
00339 
00340 #ifdef DEBUG_MODE
00341   L_cur = (unsigned char*) &L_ret;
00342   pretty_binary_buffer(L_cur, sizeof(T_UnsignedInteger64), L_hexa_buf);
00343   GEN_DEBUG(1, "host val: " << L_hexa_buf);
00344 #endif
00345 
00346   return (L_ret) ;
00347 }
00348 
00349 T_Integer64 convert_bin_network_to_ll(unsigned char*P_buf, size_t P_size) {
00350 
00351   T_Integer64      L_ret = 0 ;
00352   unsigned char   *L_ptr     ;
00353 
00354   GEN_DEBUG(1, "convert_bin_network_to_ll ("
00355         << &P_buf << ", size: " << P_size
00356         << ")");
00357   
00358 #ifdef DEBUG_MODE
00359   static char   L_hexa_buf [50] ;
00360   unsigned char*L_cur ;
00361 
00362   L_cur = P_buf;
00363   pretty_binary_buffer(L_cur, P_size, L_hexa_buf);
00364   GEN_DEBUG(1, "net val: " << L_hexa_buf);
00365 #endif
00366 
00367   GEN_DEBUG(1, "decal size: "
00368                << (sizeof(T_UnsignedInteger64) - P_size));
00369 
00370   if (P_size > sizeof(T_UnsignedInteger64)) {
00371      GEN_ERROR(E_GEN_FATAL_ERROR,"Current Size: "
00372                << P_size << " is greater than expecting one: "
00373                <<sizeof(T_UnsignedInteger64));
00374   } else {
00375     // from network => MSB representation
00376     L_ptr = (unsigned char*) &L_ret ;
00377     L_ptr += (sizeof(T_UnsignedInteger64) - P_size) ;
00378     memcpy( (void*)  L_ptr,
00379             (void*)  P_buf,
00380             (size_t) P_size ) ;
00381     // convert to local representation
00382     L_ret = ntohll_f (L_ret) ;
00383   }
00384 
00385 #ifdef DEBUG_MODE
00386   L_cur = (unsigned char *)&L_ret;
00387   pretty_binary_buffer(L_cur, sizeof(T_Integer64), L_hexa_buf);
00388   GEN_DEBUG(1, "host val: " << L_hexa_buf);
00389 #endif
00390 
00391   return (L_ret) ;
00392 }
00393 
00394 void convert_ull_to_bin_network(unsigned char      *P_buf, 
00395                                 size_t              P_size, 
00396                                 T_UnsignedInteger64 P_val) {
00397   T_UnsignedInteger64 L_val = P_val ;
00398   unsigned char      *L_ul = (unsigned char*) &L_val ;
00399   unsigned char      *L_res = P_buf ;
00400 
00401   GEN_DEBUG(1, "convert_ull_to_bin_network ("
00402         << &P_buf << ", size: " << P_size << ", Val: 0x" 
00403         << iostream_hex << P_val << iostream_dec
00404         << ")");
00405   
00406 #ifdef DEBUG_MODE
00407   static char   L_hexa_buf [50] ;
00408   unsigned char*L_cur ;
00409 
00410   L_cur = (unsigned char *)&P_val;
00411   pretty_binary_buffer(L_cur, sizeof(T_UnsignedInteger64), L_hexa_buf);
00412   GEN_DEBUG(1, "host val: " << L_hexa_buf);
00413 #endif
00414 
00415   GEN_DEBUG(1, "decal size: "
00416                << (sizeof(T_UnsignedInteger64) - P_size));
00417 
00418   L_val = htonll_f (L_val) ;
00419 
00420 #ifdef DEBUG_MODE
00421   L_cur = (unsigned char *)&L_val;
00422   pretty_binary_buffer(L_cur, sizeof(T_UnsignedInteger64), L_hexa_buf);
00423   GEN_DEBUG(1, "htonll  : " << L_hexa_buf);
00424 #endif
00425 
00426   L_ul  += (sizeof(T_UnsignedInteger64) - P_size);
00427 
00428   memcpy ( (void *) L_res,
00429            (void *) L_ul,
00430            P_size) ;
00431 
00432 #ifdef DEBUG_MODE
00433   L_cur = L_res;
00434   pretty_binary_buffer(L_cur, P_size, L_hexa_buf);
00435   GEN_DEBUG(1, "net val: " << L_hexa_buf);
00436 #endif
00437 
00438   GEN_DEBUG(1, "convert_ull_to_bin_network() end");
00439 }
00440 
00441 void convert_ll_to_bin_network(unsigned char   *P_buf, 
00442                                size_t           P_size, 
00443                                T_Integer64      P_val) {
00444   T_Integer64         L_val = P_val ;
00445   unsigned char      *L_ul = (unsigned char*) &L_val ;
00446   unsigned char      *L_res = P_buf ;
00447 
00448   GEN_DEBUG(1, "convert_ll_to_bin_network ("
00449         << &P_buf << ", size: " << P_size << ", Val: 0x" 
00450         << iostream_hex << P_val << iostream_dec
00451         << ")");
00452 
00453 #ifdef DEBUG_MODE
00454   static char   L_hexa_buf [50] ;
00455   unsigned char*L_cur ;
00456 
00457   L_cur = (unsigned char*)&P_val;
00458   pretty_binary_buffer(L_cur, sizeof(T_Integer64), L_hexa_buf);
00459   GEN_DEBUG(1, "host val: " << L_hexa_buf);
00460 #endif
00461 
00462   GEN_DEBUG(1, "decal size: "
00463                << (sizeof(T_UnsignedInteger64) - P_size));
00464 
00465   L_val = htonll_f (L_val) ;
00466 
00467 #ifdef DEBUG_MODE
00468   L_cur = (unsigned char *)&L_val;
00469   pretty_binary_buffer(L_cur, sizeof(T_Integer64), L_hexa_buf);
00470   GEN_DEBUG(1, "htonll  : " << L_hexa_buf);
00471 #endif
00472 
00473   L_ul  += (sizeof(T_UnsignedInteger64) - P_size);
00474 
00475   memcpy ( (void *) L_res,
00476            (void *) L_ul,
00477            P_size) ;
00478 
00479 #ifdef DEBUG_MODE
00480   L_cur = L_res;
00481   pretty_binary_buffer(L_cur, P_size, L_hexa_buf);
00482   GEN_DEBUG(1, "net val: " << L_hexa_buf);
00483 #endif
00484 
00485   GEN_DEBUG(1, "convert_ll_to_bin_network() end");
00486 }
00487 

Generated on Wed Mar 7 14:44:49 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002