Rizin
unix-like reverse engineering framework and cli tools
lines_diff.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 RizinOrg <info@rizin.re>
2 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 /* Helpers for handling lines */
6 #define DIFF_IS_LINES_METHOD(x) (x.elem_at == methods_lines.elem_at)
7 
8 static RzList *tokenize_lines(const char *string) {
9  RzList *lines = NULL;
10  size_t last = 0;
11  size_t size = 0;
12  char *line = NULL;
13 
15  if (!lines) {
16  RZ_LOG_ERROR("rz_diff_line_new: cannot allocate list of lines\n");
17  goto tokenize_newlines_fail;
18  }
19 
20  size = strlen(string);
21  for (size_t i = 0; i < size; ++i) {
22  if (string[i] == '\n') {
23  line = rz_str_ndup(string + last, (i + 1) - last);
24  if (!line || !rz_list_append(lines, line)) {
25  RZ_LOG_ERROR("rz_diff_line_new: cannot allocate line or add it to the list\n");
26  free(line);
27  goto tokenize_newlines_fail;
28  }
29  last = i + 1;
30  }
31  }
32 
33  if (last < size) {
34  line = rz_str_ndup(string + last, size - last);
35  if (!line || !rz_list_append(lines, line)) {
36  RZ_LOG_ERROR("rz_diff_line_new: cannot allocate last line or add it to the list\n");
37  free(line);
38  goto tokenize_newlines_fail;
39  }
40  }
41 
42  return lines;
43 
44 tokenize_newlines_fail:
46  return NULL;
47 }
48 
49 static const void *line_elem_at(const RzList *array, ut32 index) {
50  return rz_list_get_n(array, index);
51 }
52 
53 static int line_compare(const char *a_elem, const char *b_elem) {
54  return strcmp(a_elem, b_elem);
55 }
56 
57 static ut32 line_hash(const char *elem) {
58  ut32 size = strlen(elem);
59  return rz_diff_hash_data((const ut8 *)elem, size);
60 }
61 
62 static void line_stringify(const char *a_elem, RzStrBuf *sb) {
63  rz_strbuf_set(sb, a_elem);
64 }
65 
66 static void line_free(RzList *array) {
67  rz_list_free(array);
68 }
69 
72  .elem_hash = (RzDiffMethodElemHash)line_hash,
73  .compare /* */ = (RzDiffMethodCompare)line_compare,
75  .ignore /* */ = fake_ignore,
76  .free /* */ = (RzDiffMethodFree)line_free,
77 };
lzma_index ** i
Definition: index.h:629
static SblHeader sb
Definition: bin_mbn.c:26
static RzBinSourceLineInfo * lines(RzBinFile *bf)
Definition: bin_symbols.c:427
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
RZ_API ut32 rz_diff_hash_data(RZ_NULLABLE const ut8 *buffer, ut32 size)
Calculates the hash of any given data.
Definition: diff.c:103
static bool fake_ignore(const void *value)
Definition: diff.c:118
void(* RzDiffMethodFree)(const void *array)
Definition: diff.c:78
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_BORROW void * rz_list_get_n(RZ_NONNULL const RzList *list, ut32 n)
Returns the N-th element of the list.
Definition: list.c:574
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static int line_compare(const char *a_elem, const char *b_elem)
Definition: lines_diff.c:53
static const MethodsInternal methods_lines
Definition: lines_diff.c:70
static void line_free(RzList *array)
Definition: lines_diff.c:66
static void line_stringify(const char *a_elem, RzStrBuf *sb)
Definition: lines_diff.c:62
static ut32 line_hash(const char *elem)
Definition: lines_diff.c:57
static const void * line_elem_at(const RzList *array, ut32 index)
Definition: lines_diff.c:49
static RzList * tokenize_lines(const char *string)
Definition: lines_diff.c:8
line
Definition: setup.py:34
const void *(* RzDiffMethodElemAt)(RZ_BORROW const void *array, ut32 index)
Definition: rz_diff.h:36
ut32(* RzDiffMethodElemHash)(RZ_BORROW const void *elem)
Definition: rz_diff.h:37
void(* RzDiffMethodStringify)(RZ_BORROW const void *elem, RZ_BORROW RzStrBuf *sb)
Definition: rz_diff.h:40
int(* RzDiffMethodCompare)(RZ_BORROW const void *a_elem, RZ_BORROW const void *b_elem)
Definition: rz_diff.h:38
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
RzDiffMethodElemAt elem_at
Definition: diff.c:81