Get rid of double allocation in parse_number()
[hed.git] / libhed / expr.h
blob9313c394b22dfab774b2680f32879d8697810548
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_.
6 */
8 /*
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 /*******************************************************************
32 * BYTE STRINGS
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 /*******************************************************************
39 * EXPRESSIONS
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);
47 struct hed_expr {
48 struct hed_list_head atoms;
49 size_t len;
50 unsigned char *buf;
51 hed_expr_reg_cb reg_cb;
52 hed_expr_mark_cb mark_cb;
53 void *cb_data;
54 size_t cb_pos;
55 long flags;
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
84 * 0 0 x positive
85 * 0 1 0 positive
86 * 0 1 1 negative
87 * 1 x x negative
90 struct hed_expr *hed_expr_new(char *sexpr,
91 hed_expr_reg_cb reg_cb,
92 hed_expr_mark_cb mark_cb,
93 void *cb_data);
94 long hed_expr_eval(struct hed_expr *expr);
95 void hed_expr_free(struct hed_expr *expr);
97 static inline size_t
98 hed_expr_len(const struct hed_expr *expr)
100 return expr->len;
103 static inline unsigned char *
104 hed_expr_buf(const struct hed_expr *expr)
106 return expr->buf;
109 static inline long
110 hed_expr_flags(const struct hed_expr *expr)
112 return expr->flags;
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));
122 #endif