Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright 2011, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "helper.h"
     18 #include "raw_ostream.h"
     19 
     20 #include <stdlib.h>
     21 #include <ctype.h>
     22 
     23 using namespace llvm;
     24 
     25 void dump_hex(unsigned char const *data,
     26               size_t size, size_t begin, size_t end) {
     27   if (end <= begin) {
     28     // Nothing to print now.  Return directly.
     29     return;
     30   }
     31 
     32   size_t lower = begin & (~0xfUL);
     33   size_t upper = (end & (~0xfUL)) ? end : ((end + 16UL) & (~0xfUL));
     34 
     35   for (size_t i = lower; i < upper; i += 16) {
     36     out() << format("%08x", i) << ':';
     37 
     38     if (i < begin) {
     39       out().changeColor(raw_ostream::MAGENTA);
     40     }
     41 
     42     for (size_t j = i, k = i + 16; j < k; ++j) {
     43       if (j == begin) {
     44         out().resetColor();
     45       }
     46 
     47       if (j == end) {
     48         out().changeColor(raw_ostream::MAGENTA);
     49       }
     50 
     51       if (j < size) {
     52         out() << ' ' << format("%02x", (unsigned)data[j]);
     53       }
     54     }
     55 
     56     out().resetColor();
     57     out() << "  ";
     58 
     59     for (size_t j = i, k = i + 16; j < k; ++j) {
     60       if (data[j] > 0x20 && data[j] < 0x7f) {
     61         out() << (char)data[j];
     62       } else {
     63         out() << '.';
     64       }
     65     }
     66 
     67     out() << '\n';
     68   }
     69 }
     70