Rizin
unix-like reverse engineering framework and cli tools
create_zip_err_str.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # SPDX-FileCopyrightText: 2021 ret2libc <sirmy15@gmail.com>
4 # SPDX-License-Identifier: LGPL-3.0-only
5 
6 import sys
7 
8 TEMPLATE = '''/*
9  This file was auto-generated by Meson from zip.h and zipint.h
10 */
11 
12 #include "zipint.h"
13 
14 #define L ZIP_ET_LIBZIP
15 #define N ZIP_ET_NONE
16 #define S ZIP_ET_SYS
17 #define Z ZIP_ET_ZLIB
18 
19 #define E ZIP_DETAIL_ET_ENTRY
20 #define G ZIP_DETAIL_ET_GLOBAL
21 
22 const struct _zip_err_info _zip_err_str[] = {{
23 {zip_err_strs}
24 }};
25 
26 const int _zip_err_str_count = sizeof(_zip_err_str)/sizeof(_zip_err_str[0]);
27 
28 const struct _zip_err_info _zip_err_details[] = {{
29 {zip_err_details}
30 }};
31 
32 const int _zip_err_details_count = sizeof(_zip_err_details)/sizeof(_zip_err_details[0]);
33 '''
34 
35 zip_err_str_c = sys.argv[1]
36 zip_h = sys.argv[2]
37 zipint_h = sys.argv[3]
38 
39 def getcomment(x):
40  return x[x.index('/* ') + 3:x.index('*/')]
41 
42 def def2errstr(x):
43  comment = getcomment(x)
44  val = comment.split(' ')[0]
45  comment_str = comment.split(' ')[1]
46  return f'{{{val}, "{comment_str}"}}'
47 
49  comment = getcomment(x)
50  val = comment.split(' ')[0]
51  comment_str = comment.split(' ')[1]
52  return f'{{{val}, "{comment_str}"}}'
53 
54 defines = [l for l in open(zip_h, 'r').read().split('\n') if l.startswith('#define ZIP_ER')]
55 zip_err_strs = '\n'.join([def2errstr(x) + ',' for x in defines])
56 
57 defines = [l for l in open(zipint_h, 'r').read().split('\n') if l.startswith('#define ZIP_ER_DETAIL')]
58 zip_err_details = '\n'.join([def2errdetail(x) + ',' for x in defines])
59 
60 open(zip_err_str_c, 'w').write(TEMPLATE.format(zip_err_strs=zip_err_strs, zip_err_details=zip_err_details))
static static fork write
Definition: sflib.h:33
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115