Skip to content

Commit

Permalink
imgeditor: Export more API
Browse files Browse the repository at this point in the history
Signed-off-by: qianfan Zhao <qianfanguijin@163.com>
  • Loading branch information
qianfan-Zhao committed Sep 26, 2024
1 parent f1c7ef6 commit 325981d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions imgeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ find_registed_partition(uint64_t start_addr, const char **disk_type);

void free_registed_disk_partitions(void);

int imgeditor_editor_detect(const char *name, int fd);
int imgeditor_editor_main(const char *name, const char *args);
int imgeditor_editor_unpack(const char *name, int fd, const char *out,
const char *args);
int imgeditor_editor_unpack2fd(const char *name, int fd, int fd_out,
const char *args);

#endif
51 changes: 51 additions & 0 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ static void imgeditor_editor_release(struct imgeditor *editor)
editor->init(editor->private_data);
}

int imgeditor_editor_detect(const char *name, int fd)
{
struct imgeditor *editor = gd_get_imgeditor(name);
int ret;

if (!editor)
return -1;

ret = editor->detect(editor->private_data, 0 /* force_type */, fd);
imgeditor_editor_release(editor);

return ret;
}

int imgeditor_editor_main(const char *name, const char *args)
{
struct imgeditor *editor = imgeditor_editor_request(name);
Expand All @@ -72,3 +86,40 @@ int imgeditor_editor_main(const char *name, const char *args)
imgeditor_editor_release(editor);
return ret;
}

#define editor_argv_helper(func, fd, ...) \
struct imgeditor *editor = imgeditor_editor_request(name); \
char args_nonconst[8192], *argv[128] = { NULL }; \
off64_t cur_offset; \
int argc, ret = -1; \
\
if (!editor || !editor->func) \
return ret; \
\
cur_offset = lseek64(fd, 0, SEEK_CUR); \
ret = editor->detect(editor->private_data, 1, fd); \
if (ret < 0) \
goto done; \
\
snprintf(args_nonconst, sizeof(args_nonconst), "%s", args); \
argc = argv_buffer_split_with_delim(args_nonconst, " \t", argv, 128); \
\
/* restore file offset */ \
lseek64(fd, cur_offset, SEEK_SET); \
ret = editor->func(editor->private_data, fd, ##__VA_ARGS__, argc, argv);\
\
done: \
imgeditor_editor_release(editor); \
return ret;

int imgeditor_editor_unpack(const char *name, int fd, const char *out,
const char *args)
{
editor_argv_helper(unpack, fd, out);
}

int imgeditor_editor_unpack2fd(const char *name, int fd, int fd_out,
const char *args)
{
editor_argv_helper(unpack2fd, fd, fd_out);
}

0 comments on commit 325981d

Please sign in to comment.