Skip to content

Commit

Permalink
Add r_strbuf_copy API
Browse files Browse the repository at this point in the history
  • Loading branch information
pelijah committed Oct 3, 2019
1 parent c06cdc4 commit 71dc854
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libr/anal/op.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ R_API RAnalOp *r_anal_op_copy(RAnalOp *op) {
nop->src[2] = r_anal_value_copy (op->src[2]);
nop->dst = r_anal_value_copy (op->dst);
r_strbuf_init (&nop->esil);
r_strbuf_set (&nop->esil, r_strbuf_get (&op->esil));
r_strbuf_copy (&nop->esil, &op->esil);
return nop;
}

Expand Down
1 change: 1 addition & 0 deletions libr/include/r_util/r_strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ R_API int r_strbuf_length(RStrBuf *sb);
R_API void r_strbuf_free(RStrBuf *sb);
R_API void r_strbuf_fini(RStrBuf *sb);
R_API void r_strbuf_init(RStrBuf *sb);
R_API bool r_strbuf_copy(RStrBuf *dst, RStrBuf *src);
R_API bool r_strbuf_equals(RStrBuf *sa, RStrBuf *sb);
R_API bool r_strbuf_reserve(RStrBuf *sb, int len);

Expand Down
19 changes: 19 additions & 0 deletions libr/util/strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ R_API void r_strbuf_init(RStrBuf *sb) {
memset (sb, 0, sizeof (RStrBuf));
}

R_API bool r_strbuf_copy(RStrBuf *dst, RStrBuf *src) {
r_return_val_if_fail (dst && src, false);
if (src->ptr) {
char *p = malloc (src->ptrlen);
if (!p) {
return false;
}
memcpy (p, src->ptr, src->ptrlen);
free (dst->ptr);
dst->ptr = p;
dst->ptrlen = src->ptrlen;
} else {
R_FREE (dst->ptr);
memcpy (dst->buf, src->buf, sizeof (dst->buf));
}
dst->len = src->len;
return true;
}

R_API bool r_strbuf_reserve(RStrBuf *sb, int len) {
r_return_val_if_fail (sb && len > 0, false);

Expand Down

0 comments on commit 71dc854

Please sign in to comment.