luasandbox  1.4.0
Generic Lua sandbox for dynamic data analysis
luasandbox_output.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /** @brief Lua sandbox output generation/retrieval functions @file */
8 
9 #ifndef luasandbox_output_h_
10 #define luasandbox_output_h_
11 
12 #include <stdio.h>
13 
14 #include "luasandbox.h"
15 
16 #ifdef __cplusplus
17 extern "C"
18 {
19 #endif
20 
21 #include "luasandbox/lua.h"
22 
23 /**
24  * Add a output function to the environment table. The environment table must be
25  * on the top of the stack. This function will receive the userdata and
26  * lsb_output_buffer struct as pointers on the Lua stack.
27  *
28  * lsb_output_buffer* output = (output_data*)lua_touserdata(lua, -1);
29  * ud_object* ud = (ud_object*)lua_touserdata(lua, -2);
30  *
31  * @param lua Pointer the Lua state.
32  * @param fp Function pointer to the outputter.
33  *
34  * @return int Zero on success, non-zero on failure.
35  */
37 
38 /**
39  * Utility function to retrieve a user data output function
40  *
41  * @param lua
42  * @param index
43  *
44  * @return lua_CFunction
45  */
47 
48 /**
49  * Add a zero copy function to the environment table. The environment table must
50  * be on the top of the stack. This function will receive the userdata as a
51  * pointer on the Lua stack.
52  *
53  * ud_object* ud = (ud_object*)lua_touserdata(lua, -1);
54  *
55  * @param lua Pointer the Lua state.
56  * @param fp Function pointer to the zero copy function.
57  *
58  * @return int Number of segments (pointer and length for each)
59  */
61 
62 
63 /**
64  * Utility function to retrieve a user data zero copy function
65  *
66  * @param lua
67  * @param index
68  *
69  * @return lua_CFunction
70  */
72 
73 /**
74  * Write an array of variables on the Lua stack to the output buffer.
75  *
76  * @param lsb Pointer to the sandbox.
77  * @param start Lua stack index of first variable.
78  * @param end Lua stack index of the last variable.
79  * @param append 0 to overwrite the output buffer, 1 to append the output to it
80  *
81  */
82 LSB_EXPORT void
83 lsb_output(lsb_lua_sandbox *lsb, int start, int end, int append);
84 
85 /**
86  * Write an array of variables on the Lua stack to the output buffer. After
87  * adding support for coroutines we need an extra variable to specify the
88  * correct Lua state.
89  *
90  * @param lsb Pointer to the sandbox.
91  * @param lua Pointer the Lua state
92  * @param start Lua stack index of first variable.
93  * @param end Lua stack index of the last variable.
94  * @param append 0 to overwrite the output buffer, 1 to append the output to it
95  *
96  */
97 LSB_EXPORT void
98 lsb_output_coroutine(lsb_lua_sandbox *lsb, lua_State *lua, int start,
99  int end, int append);
100 
101 /**
102  * Retrieve the data in the output buffer and reset the buffer. The returned
103  * output string will remain valid until additional sandbox output is performed.
104  * The output should be copied if the application needs to hold onto it.
105  *
106  * @param lsb Pointer to the sandbox.
107  * @param len If len is not NULL, it will be set to the length of the string.
108  *
109  * @return const char* Pointer to the output buffer.
110  */
111 LSB_EXPORT const char* lsb_get_output(lsb_lua_sandbox *lsb, size_t *len);
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif
struct lsb_lua_sandbox lsb_lua_sandbox
Definition: luasandbox.h:65
LSB_EXPORT void lsb_add_output_function(lua_State *lua, lua_CFunction fp)
Add a output function to the environment table.
LSB_EXPORT lua_CFunction lsb_get_output_function(lua_State *lua, int index)
Utility function to retrieve a user data output function.
LSB_EXPORT void lsb_output_coroutine(lsb_lua_sandbox *lsb, lua_State *lua, int start, int end, int append)
Write an array of variables on the Lua stack to the output buffer.
int(* lua_CFunction)(lua_State *L)
Definition: lua.h:52
Generic Lua sandbox for dynamic data analysis.
LSB_EXPORT void lsb_output(lsb_lua_sandbox *lsb, int start, int end, int append)
Write an array of variables on the Lua stack to the output buffer.
struct lua_State lua_State
Definition: lua.h:50
LSB_EXPORT void lsb_add_zero_copy_function(lua_State *lua, lua_CFunction fp)
Add a zero copy function to the environment table.
LSB_EXPORT lua_CFunction lsb_get_zero_copy_function(lua_State *lua, int index)
Utility function to retrieve a user data zero copy function.
LSB_EXPORT const char * lsb_get_output(lsb_lua_sandbox *lsb, size_t *len)
Retrieve the data in the output buffer and reset the buffer.
#define LSB_EXPORT
Definition: luasandbox.h:24