How to print 8 Byte void pointer (including chars) as hex

-1

I am currently trying to print out data, captured by the SSH Wireshark dissector. I debugged the void pointer array which I need and in the debugger the first index looks as follows:

    [0] 0xa9ac1405c4040000  void *
    [1] 0xc4c2f211de8a3e38  void *

However when i try to print the stuff the values I get out look like this: C4040000 DE8A3E38 Which means the data is only printed after the first 4 Bytes.

My question now is, how can I get the whole void pointer. My code looks as follows:

For clarification. The breakpoint I set to get the above data was in the last shown line.

Edit: I added the code of tvb_memcpy to the example. The length which is returned by the tvb_captured_length is the length of the packet and should be correct.

gint length = tvb_captured_length(tvb);
size_t length_2 = length;
unsigned char target[2000];

tvb_memcpy(tvb, target, offset, length_2);
g_print("data: ");
int i;
for (i = 0; i < length; ++i) {
    g_print(" %02X", (unsigned char *)target[i]);
}
g_print("\n");

void *
tvb_memcpy(tvbuff_t *tvb, void *target, const gint offset, size_t length)
{
guint   abs_offset = 0, abs_length = 0;

DISSECTOR_ASSERT(tvb && tvb->initialized);

/*
 * XXX - we should eliminate the "length = -1 means 'to the end
 * of the tvbuff'" convention, and use other means to achieve
 * that; this would let us eliminate a bunch of checks for
 * negative lengths in cases where the protocol has a 32-bit
 * length field.
 *
 * Allowing -1 but throwing an assertion on other negative
 * lengths is a bit more work with the length being a size_t;
 * instead, we check for a length <= 2^31-1.
 */
DISSECTOR_ASSERT(length <= 0x7FFFFFFF);
check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);

if (tvb->real_data) {
    return memcpy(target, tvb->real_data + abs_offset, abs_length);
}

if (tvb->ops->tvb_memcpy)
    return tvb->ops->tvb_memcpy(tvb, target, abs_offset, abs_length);

/*
 * If the length is 0, there's nothing to do.
 * (tvb->real_data could be null if it's allocated with
 * a size of length.)
 */
if (length != 0) {
    /*
     * XXX, fallback to slower method
     */
    DISSECTOR_ASSERT_NOT_REACHED();
}
return NULL;
}

Edit 2: I changed the void * to an unsigned char * pointer and it worked. Thanks to all of you!

c
pointers
printing
asked on Stack Overflow Jul 13, 2019 by JustPlayin • edited Jul 13, 2019 by JustPlayin

2 Answers

1

Why copy the data into an array of void pointers?

You should probably just use unsigned char. However I cannot tell of your use of tvb, tvb_captured_length(), tvb_memcpy... are correct.

gint length = tvb_captured_length(tvb);
size_t length_2 = length;
unsigned char target[2000];

tvb_memcpy(tvb, target, offset, length_2);
g_print("data: ");
int i;
for (i = 0; i < length; ++i) {
    g_print(" %02X", target[i]);
}
g_print("\n");
answered on Stack Overflow Jul 13, 2019 by chqrlie
1

Use the %p format specifier.

#include <stdio.h>

int main()
{
        int a;
        void *p;

        p = &a;
        printf("p=%p (as ptr), p=%02X\n", p, p);

        return (0);
}

Output (on my system):

p=0x7fffffffeb44 (as ptr), p=FFFFEB44
answered on Stack Overflow Jul 13, 2019 by gsr • edited Jul 13, 2019 by shizhen

User contributions licensed under CC BY-SA 3.0