Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dump.h
Go to the documentation of this file.
1 /*
2 ** mruby/dump.h - mruby binary dumper (mrbc binary format)
3 **
4 ** See Copyright Notice in mruby.h
5 */
6 
7 #ifndef MRUBY_DUMP_H
8 #define MRUBY_DUMP_H
9 
10 #if defined(__cplusplus)
11 extern "C" {
12 #endif
13 
14 #include "mruby.h"
15 
16 #ifdef ENABLE_STDIO
17 int mrb_dump_irep_binary(mrb_state*, size_t, int, FILE*);
18 int mrb_dump_irep_cfunc(mrb_state *mrb, size_t n, int, FILE *f, const char *initname);
19 int32_t mrb_read_irep_file(mrb_state*, FILE*);
20 #endif
21 int32_t mrb_read_irep(mrb_state*, const uint8_t*);
22 
23 #ifdef ENABLE_STDIO
24 mrb_value mrb_load_irep_file(mrb_state*,FILE*);
25 #endif
26 
27 /* dump/load error code
28  *
29  * NOTE: MRB_DUMP_GENERAL_FAILURE is caused by
30  * unspecified issues like malloc failed.
31  */
32 #define MRB_DUMP_OK 0
33 #define MRB_DUMP_GENERAL_FAILURE -1
34 #define MRB_DUMP_WRITE_FAULT -2
35 #define MRB_DUMP_READ_FAULT -3
36 #define MRB_DUMP_CRC_ERROR -4
37 #define MRB_DUMP_INVALID_FILE_HEADER -5
38 #define MRB_DUMP_INVALID_IREP -6
39 #define MRB_DUMP_INVALID_ARGUMENT -7
40 
41 /* null symbol length */
42 #define MRB_DUMP_NULL_SYM_LEN 0xFFFF
43 
44 /* Rite Binary File header */
45 #define RITE_BINARY_IDENTIFIER "RITE"
46 #define RITE_BINARY_FORMAT_VER "0001"
47 #define RITE_COMPILER_NAME "MATZ"
48 #define RITE_COMPILER_VERSION "0000"
49 
50 #define RITE_VM_VER "0000"
51 
52 #define RITE_BINARY_EOF "END\0"
53 #define RITE_SECTION_IREP_IDENTIFIER "IREP"
54 #define RITE_SECTION_LINENO_IDENTIFIER "LINE"
55 #define RITE_SECTION_DEBUG_IDENTIFIER "DBG\0"
56 
57 #define MRB_DUMP_DEFAULT_STR_LEN 128
58 
59 // binary header
61  uint8_t binary_identify[4]; // Binary Identifier
62  uint8_t binary_version[4]; // Binary Format Version
63  uint8_t binary_crc[2]; // Binary CRC
64  uint8_t binary_size[4]; // Binary Size
65  uint8_t compiler_name[4]; // Compiler name
66  uint8_t compiler_version[4];
67 };
68 
69 // section header
70 #define RITE_SECTION_HEADER \
71  uint8_t section_identify[4]; \
72  uint8_t section_size[4]
73 
76 };
77 
80 
81  uint8_t rite_version[4]; // Rite Instruction Specification Version
82  uint8_t nirep[2]; // Number of ireps
83  uint8_t sirep[2]; // Start index
84 };
85 
88 
89  uint8_t nirep[2]; // Number of ireps
90  uint8_t sirep[2]; // Start index
91 };
92 
95 
96  uint8_t nirep[2]; // Number of ireps
97  uint8_t sirep[2]; // Start index
98 };
99 
102 };
103 
104 static inline int
105 uint8_to_bin(uint8_t s, uint8_t *bin)
106 {
107  *bin = s;
108  return sizeof(uint8_t);
109 }
110 
111 static inline int
112 uint16_to_bin(uint16_t s, uint8_t *bin)
113 {
114  *bin++ = (s >> 8) & 0xff;
115  *bin = s & 0xff;
116  return sizeof(uint16_t);
117 }
118 
119 static inline int
120 uint32_to_bin(uint32_t l, uint8_t *bin)
121 {
122  *bin++ = (l >> 24) & 0xff;
123  *bin++ = (l >> 16) & 0xff;
124  *bin++ = (l >> 8) & 0xff;
125  *bin = l & 0xff;
126  return sizeof(uint32_t);
127 }
128 
129 static inline uint32_t
130 bin_to_uint32(const uint8_t *bin)
131 {
132  return (uint32_t)bin[0] << 24 |
133  (uint32_t)bin[1] << 16 |
134  (uint32_t)bin[2] << 8 |
135  (uint32_t)bin[3];
136 }
137 
138 static inline uint16_t
139 bin_to_uint16(const uint8_t *bin)
140 {
141  return (uint16_t)bin[0] << 8 |
142  (uint16_t)bin[1];
143 }
144 
145 static inline uint8_t
146 bin_to_uint8(const uint8_t *bin)
147 {
148  return (uint8_t)bin[0];
149 }
150 
151 #if defined(__cplusplus)
152 } /* extern "C" { */
153 #endif
154 
155 /* crc.c */
156 uint16_t
157 calc_crc_16_ccitt(const uint8_t *src, size_t nbytes, uint16_t crc);
158 
159 #endif /* MRUBY_DUMP_H */