1 /* The expression evaluator */
3 /* This is a libhed PUBLIC file.
4 * This header file will be installed on the target system.
5 * When you add new things here, make sure they start with hed_.
9 * hed - Hexadecimal editor
10 * Copyright (C) 2004 Petr Baudis <pasky@ucw.cz>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #ifndef LIBHED__EXPR_H
27 #define LIBHED__EXPR_H
29 #include <libhed/types.h>
31 /*******************************************************************
35 hed_off_t
hed_bytestr2off(unsigned char *bytestr
, size_t len
, long flags
);
36 void hed_off2bytestr(unsigned char *bytestr
, size_t len
, hed_off_t num
);
38 /*******************************************************************
42 typedef long (*hed_expr_reg_cb
)(void *data
, char reg
, hed_off_t ofs
, /* */
43 unsigned char *scramble
, size_t len
);
44 typedef long (*hed_expr_mark_cb
)(void *data
, char mark
,
45 unsigned char *scramble
, size_t len
);
48 struct hed_list_head atoms
;
51 hed_expr_reg_cb reg_cb
;
52 hed_expr_mark_cb mark_cb
;
58 /* Return flags for callbacks */
59 #define HED_AEF_NEGATIVE 1 // the result should be treated as negative
60 #define HED_AEF_SIGNED 2 // take the sign bit from the result
61 #define HED_AEF_DYNAMIC 4 // result is not constant
63 #define HED_AEF_ENDIANMASK (3<<3) // endianity mask
64 #define HED_AEF_KEEPENDIAN (0<<3) // keep endianity (no change)
65 #define HED_AEF_FORCELE (1<<3) // force little-endian
66 #define HED_AEF_FORCEBE (2<<3) // force big-endian
67 #define HED_AEF_SWAPENDIAN (3<<3) // always swap endianity
69 #define HED_AEF_ERROR (~(~0UL>>1))
71 /* NB: The HED_AEF_NEGATIVE flag behaves as an extra "hidden" MSB bit,
72 * which is used in calculations, but not included in the final result.
73 * This allows to fit unsigned values in the range 0..2^n-1 as well as
74 * signed values from -2^(n-1)..2^(n-1)-1 into only n bits.
76 * In particular, a zero with HED_AEF_NEGATIVE set is not treated as
77 * a negative zero, but rather as the largest negative signed value,
78 * e.g. "00" byte string with HED_AEF_NEGATIVE represents -256.
80 * HED_AEF_NEGATIVE takes precedence over HED_AEF_SIGNED. The following
81 * table shows the relation betwen the various bits and the resulting sign:
83 * HED_AEF_NEGATIVE HED_AEF_SIGNED MSB result
90 struct hed_expr
*hed_expr_new(char *sexpr
,
91 hed_expr_reg_cb reg_cb
,
92 hed_expr_mark_cb mark_cb
,
94 long hed_expr_eval(struct hed_expr
*expr
);
95 void hed_expr_free(struct hed_expr
*expr
);
98 hed_expr_len(const struct hed_expr
*expr
)
103 static inline unsigned char *
104 hed_expr_buf(const struct hed_expr
*expr
)
110 hed_expr_flags(const struct hed_expr
*expr
)
115 static inline hed_off_t
116 hed_expr2off(const struct hed_expr
*expr
)
118 return hed_bytestr2off(hed_expr_buf(expr
), hed_expr_len(expr
),
119 hed_expr_flags(expr
));