00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) University of Washington 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #include <string> 00020 #include <iostream> 00021 #include <iomanip> 00022 #include <sstream> 00023 00024 namespace ns3 { 00025 00026 /** 00027 * \brief Convert a byte buffer to a string containing a hex representation 00028 * of the buffer. Make the string pretty by adding a colon (':') between 00029 * the hex. 00030 * 00031 * \param buffer The input buffer to be converted. 00032 * \param len The length of the input buffer. 00033 * \returns A string containing a hex representation of the data in buffer. 00034 */ 00035 std::string 00036 EmuBufferToString (uint8_t *buffer, uint32_t len) 00037 { 00038 std::ostringstream oss; 00039 // 00040 // Tell the stream to make hex characters, zero-filled 00041 // 00042 oss.setf (std::ios::hex, std::ios::basefield); 00043 oss.fill('0'); 00044 00045 // 00046 // Loop through the buffer, separating the two-digit-wide hex bytes 00047 // with a colon. 00048 // 00049 for (uint8_t i = 0; i < len; i++) 00050 { 00051 oss << ":" << std::setw (2) << (uint32_t)buffer[i]; 00052 } 00053 return oss.str (); 00054 } 00055 00056 /** 00057 * \brief Convert string encoded by the inverse function (EmuBufferToString) 00058 * back into a byte buffer. 00059 * 00060 * \param s The input string. 00061 * \param buffer The buffer to initialize with the converted bits. 00062 * \param len The length of the data that is valid in the buffer. 00063 * \returns True indicates a successful conversion. 00064 */ 00065 bool 00066 EmuStringToBuffer (std::string s, uint8_t *buffer, uint32_t *len) 00067 { 00068 // 00069 // If the string was made by our inverse function, the string length must 00070 // be a multiple of three characters in length. Use this fact to do a 00071 // quick reasonableness test. 00072 // 00073 if ((s.length () % 3) != 0) 00074 { 00075 return false; 00076 } 00077 00078 std::istringstream iss; 00079 iss.str (s); 00080 00081 uint8_t n = 0; 00082 00083 while (iss.good ()) 00084 { 00085 // 00086 // The first character in the "triplet" we're working on is always the 00087 // the ':' separator. Read that into a char and make sure we're skipping 00088 // what we think we're skipping. 00089 // 00090 char c; 00091 iss.read (&c, 1); 00092 if (c != ':') 00093 { 00094 return false; 00095 } 00096 00097 // 00098 // And then read in the real bits and convert them. 00099 // 00100 uint32_t tmp; 00101 iss >> std::hex >> tmp; 00102 buffer[n] = tmp; 00103 n++; 00104 } 00105 00106 *len = n; 00107 return true; 00108 } 00109 00110 } // namespace ns3