Home | History | Annotate | Download | only in foundation
      1 /*
      2  * Copyright (C) 2010 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 //#define LOG_NDEBUG 0
     18 #define LOG_TAG "hexdump"
     19 #include <utils/Log.h>
     20 
     21 #include "hexdump.h"
     22 
     23 #include "ADebug.h"
     24 #include "AString.h"
     25 
     26 #include <ctype.h>
     27 #include <stdint.h>
     28 #include <stdio.h>
     29 
     30 namespace android {
     31 
     32 void hexdump(const void *_data, size_t size) {
     33     const uint8_t *data = (const uint8_t *)_data;
     34 
     35     size_t offset = 0;
     36     while (offset < size) {
     37         AString line;
     38 
     39         char tmp[32];
     40         sprintf(tmp, "%08lx:  ", (unsigned long)offset);
     41 
     42         line.append(tmp);
     43 
     44         for (size_t i = 0; i < 16; ++i) {
     45             if (i == 8) {
     46                 line.append(' ');
     47             }
     48             if (offset + i >= size) {
     49                 line.append("   ");
     50             } else {
     51                 sprintf(tmp, "%02x ", data[offset + i]);
     52                 line.append(tmp);
     53             }
     54         }
     55 
     56         line.append(' ');
     57 
     58         for (size_t i = 0; i < 16; ++i) {
     59             if (offset + i >= size) {
     60                 break;
     61             }
     62 
     63             if (isprint(data[offset + i])) {
     64                 line.append((char)data[offset + i]);
     65             } else {
     66                 line.append('.');
     67             }
     68         }
     69 
     70         LOGI("%s", line.c_str());
     71 
     72         offset += 16;
     73     }
     74 }
     75 
     76 }  // namespace android
     77 
     78