Rizin
unix-like reverse engineering framework and cli tools
randomart.c File Reference
#include <rz_hash.h>
#include <rz_util.h>

Go to the source code of this file.

Macros

#define FLDBASE   8
 
#define FLDSIZE_Y   (FLDBASE + 1)
 
#define FLDSIZE_X   (FLDBASE * 2 + 1)
 

Functions

RZ_API RZ_OWN char * rz_hash_cfg_randomart (RZ_NONNULL const ut8 *buffer, ut32 length, ut64 address)
 Generates a randomart that is meant to be an easily validate buffers. More...
 

Macro Definition Documentation

◆ FLDBASE

#define FLDBASE   8

Definition at line 43 of file randomart.c.

◆ FLDSIZE_X

#define FLDSIZE_X   (FLDBASE * 2 + 1)

Definition at line 45 of file randomart.c.

◆ FLDSIZE_Y

#define FLDSIZE_Y   (FLDBASE + 1)

Definition at line 44 of file randomart.c.

Function Documentation

◆ rz_hash_cfg_randomart()

RZ_API RZ_OWN char* rz_hash_cfg_randomart ( RZ_NONNULL const ut8 buffer,
ut32  length,
ut64  address 
)

Generates a randomart that is meant to be an easily validate buffers.

Parameters
bufferThe buffer to use to generate the
lengthThe buffer length
addrThe location of the hash (when unknown can be set to 0).
Returns

Definition at line 55 of file randomart.c.

55  {
57  /*
58  * Chars to be used after each other every time the worm
59  * intersects with itself. Matter of taste.
60  */
61  char *augmentation_string = " .o+=*BOX@%&#/^SE";
62  char *retval, *p;
63  ut8 field[FLDSIZE_X][FLDSIZE_Y];
64  ut32 i, b;
65  int x, y;
66  size_t len = strlen(augmentation_string) - 1;
67 
68  // 2*(FLDSIZE_X+3) there are two for loops that iterate over this
69  // FLDSIZE_Y * (FLDSIZE_X+3) there is a loop that for each y iterates over the whole FLDSIZE_X
70  // The rest is counting the +--[0x%08"PFMT64x"]- and '\0'
71  retval = calloc(1, 2 * (FLDSIZE_X + 3) + (FLDSIZE_Y * (FLDSIZE_X + 3)) + 7 + sizeof(PFMT64x));
72  if (!retval) {
73  return NULL;
74  }
75 
76  /* initialize field */
77  memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
78  x = FLDSIZE_X / 2;
79  y = FLDSIZE_Y / 2;
80 
81  /* process raw key */
82  for (i = 0; i < length; i++) {
83  int input;
84  /* each byte conveys four 2-bit move commands */
85  input = buffer[i];
86  for (b = 0; b < 4; b++) {
87  /* evaluate 2 bit, rest is shifted later */
88  x += (input & 0x1) ? 1 : -1;
89  y += (input & 0x2) ? 1 : -1;
90 
91  /* assure we are still in bounds */
92  x = RZ_MAX(x, 0);
93  y = RZ_MAX(y, 0);
94  x = RZ_MIN(x, FLDSIZE_X - 1);
95  y = RZ_MIN(y, FLDSIZE_Y - 1);
96 
97  /* augment the field */
98  if (field[x][y] < len - 2) {
99  field[x][y]++;
100  }
101  input = input >> 2;
102  }
103  }
104 
105  /* mark starting point and end point*/
106  field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
107  field[x][y] = len;
108 
109  /* fill in retval */
110  sprintf(retval, "+--[0x%08" PFMT64x "]-", address);
111  p = strchr(retval, '\0');
112 
113  /* output upper border */
114  for (i = p - retval - 1; i < FLDSIZE_X; i++) {
115  *p++ = '-';
116  }
117  *p++ = '+';
118  *p++ = '\n';
119 
120  /* output content */
121  for (y = 0; y < FLDSIZE_Y; y++) {
122  *p++ = '|';
123  for (x = 0; x < FLDSIZE_X; x++) {
124  *p++ = augmentation_string[RZ_MIN(field[x][y], len)];
125  }
126  *p++ = '|';
127  *p++ = '\n';
128  }
129 
130  /* output lower border */
131  *p++ = '+';
132  for (i = 0; i < FLDSIZE_X; i++) {
133  *p++ = '-';
134  }
135  *p++ = '+';
136  *p++ = 0;
137 
138  return retval;
139 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
uint32_t ut32
sprintf
Definition: kernel.h:365
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
void * p
Definition: libc.cpp:67
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
int x
Definition: mipsasm.c:20
#define FLDSIZE_X
Definition: randomart.c:45
#define FLDSIZE_Y
Definition: randomart.c:44
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define PFMT64x
Definition: rz_types.h:393
#define RZ_MIN(x, y)
#define RZ_MAX(x, y)
#define b(i)
Definition: sha256.c:42
Definition: buffer.h:15
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)

References b, calloc(), FLDSIZE_X, FLDSIZE_Y, i, input(), len, length, memset(), NULL, p, PFMT64x, RZ_MAX, RZ_MIN, rz_return_val_if_fail, sprintf, and x.

Referenced by hash_print_digest(), and rz_cmd_print().