File: listmem.cpp

package info (click to toggle)
recoll 1.43.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 16,892 kB
  • sloc: cpp: 104,589; python: 9,603; xml: 7,272; ansic: 6,447; sh: 1,212; perl: 166; makefile: 72
file content (151 lines) | stat: -rw-r--r-- 3,448 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "listmem.h"

#include <stdlib.h>
#include <string.h>
#include <iomanip>

using namespace std;

/*
 * Functions to list a memory buffer:
 */

/* Turn byte into Hexadecimal ascii representation */
static char *hexa(unsigned int i)
{
    int j;
    static char asc[3];

    asc[0] = (i >> 4) & 0x0f;
    asc[1] = i & 0x0f;
    asc[2] = 0;
    for (j = 0; j < 2; j++)
        if (asc[j] > 9) {
            asc[j] += 55;
        } else {
            asc[j] += 48;
        }
    return (asc);
}

static void l_swap16(unsigned char *d, const unsigned char *s, int n)
{
    if (n & 1) {
        n >>= 1;
        n++;
    } else {
        n >>= 1;
    }
    while (n--) {
        int i;
        i = 2 * n;
        d[i] = s[i + 1];
        d[i + 1] = s[i];
    }
}

static void l_swap32(unsigned char *d, const unsigned char *s, int n)
{
    if (n & 3) {
        n >>= 2;
        n++;
    } else {
        n >>= 2;
    }
    while (n--) {
        int i;
        i = 4 * n;
        d[i] = s[i + 3];
        d[i + 1] = s[i + 2];
        d[i + 2] = s[i + 1];
        d[i + 3] = s[i];
    }
}

/* Turn byte buffer into hexadecimal representation */
void charbuftohex(int len, unsigned char *dt, int maxlen, char *str)
{
    int i;
    char *bf;

    for (i = 0, bf = str; i < len; i++) {
        char *cp;
        if (bf - str >= maxlen - 4) {
            break;
        }
        cp = hexa((unsigned int)dt[i]);
        *bf++ = *cp++;
        *bf++ = *cp++;
        *bf++ = ' ';
    }
    *bf++ = 0;
}

void listmem(ostream& os, const void *_ptr, int siz, int adr, int opts)
{
    const unsigned char *ptr = (const unsigned char *)_ptr;
    int             i, j, c;
    char            lastlisted[16];
    int             alreadysame = 0;
    int         oneout = 0;
    unsigned char  *mpt;

    if (opts & (LISTMEM_SWAP16 | LISTMEM_SWAP32)) {
        if ((mpt = (unsigned char *)malloc(siz + 4)) == NULL) {
            os << "OUT OF MEMORY\n";
            return;
        }
        if (opts & LISTMEM_SWAP16) {
            l_swap16(mpt, ptr, siz);
        } else if (opts & LISTMEM_SWAP32) {
            l_swap32(mpt, ptr, siz);
        }
    } else {
        mpt = (unsigned char *)ptr;
    }

    for (i = 0; i < siz; i += 16) {
        /* Check for same data (only print first line in this case) */
        if (oneout != 0  &&
                siz - i >= 16 && memcmp(lastlisted, mpt + i, 16) == 0) {
            if (alreadysame == 0) {
                os << "*\n";
                alreadysame = 1;
            }
            continue;
        }
        alreadysame = 0;
        /* Line header */
        os << std::setw(4) << i + adr << " ";

        /* Hexadecimal representation */
        for (j = 0; j < 16; j++) {
            if ((i + j) < siz) {
                os << hexa(mpt[i + j]) << ((j & 1) ? " " : "");
            } else {
                os << "  " << ((j & 1) ? " " : "");
            }
        }
        os << "  ";

        /* Also print ascii for values that fit */
        for (j = 0; j < 16; j++) {
            if ((i + j) < siz) {
                c = mpt[i + j];
                if (c >= 0x20 && c <= 0x7f) {
                    os << char(c);
                } else {
                    os << ".";
                }
            } else {
                os << " ";
            }
        }
        os << "\n";
        memcpy(lastlisted, mpt + i, 16);
        oneout = 1;
    }
    if (mpt != ptr) {
        free(mpt);
    }
}