1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.Base64;
26 
27 private import glib.Str;
28 private import gtkc.glib;
29 public  import gtkc.glibtypes;
30 
31 
32 /** */
33 public struct Base64
34 {
35 	/**
36 	 * Incrementally decode a sequence of binary data from its Base-64 stringified
37 	 * representation. By calling this function multiple times you can convert
38 	 * data in chunks to avoid having to have the full encoded data in memory.
39 	 *
40 	 * The output buffer must be large enough to fit all the data that will
41 	 * be written to it. Since base64 encodes 3 bytes in 4 chars you need
42 	 * at least: (@len / 4) * 3 + 3 bytes (+ 3 may be needed in case of non-zero
43 	 * state).
44 	 *
45 	 * Params:
46 	 *     inn = binary input data
47 	 *     len = max length of @in data to decode
48 	 *     output = output buffer
49 	 *     state = Saved state between steps, initialize to 0
50 	 *     save = Saved state between steps, initialize to 0
51 	 *
52 	 * Return: The number of bytes of output that was written
53 	 *
54 	 * Since: 2.12
55 	 */
56 	public static size_t decodeStep(string inn, ref ubyte[] output, ref int state, ref uint save)
57 	{
58 		auto p = g_base64_decode_step(Str.toStringz(inn), cast(int)inn.length, cast(char*)output.ptr, &state, &save);
59 		
60 		return p;
61 	}
62 
63 	/**
64 	 */
65 
66 	/**
67 	 * Decode a sequence of Base-64 encoded text into binary data
68 	 * by overwriting the input data.
69 	 *
70 	 * Params:
71 	 *     text = zero-terminated
72 	 *         string with base64 text to decode
73 	 *
74 	 * Returns: The binary data that @text responds. This pointer
75 	 *     is the same as the input @text.
76 	 *
77 	 * Since: 2.20
78 	 */
79 	public static char[] decodeInplace(ref char[] text)
80 	{
81 		size_t outLen = cast(size_t)text.length;
82 		
83 		auto p = g_base64_decode_inplace(text.ptr, &outLen);
84 		
85 		text = text[0..outLen];
86 		
87 		return p[0 .. outLen];
88 	}
89 
90 	/**
91 	 * Decode a sequence of Base-64 encoded text into binary data.  Note
92 	 * that the returned binary data is not necessarily zero-terminated,
93 	 * so it should not be used as a character string.
94 	 *
95 	 * Params:
96 	 *     text = zero-terminated string with base64 text to decode
97 	 *
98 	 * Returns: newly allocated buffer containing the binary data
99 	 *     that @text represents. The returned buffer must
100 	 *     be freed with g_free().
101 	 *
102 	 * Since: 2.12
103 	 */
104 	public static char[] decode(string text)
105 	{
106 		size_t outLen;
107 		
108 		auto p = g_base64_decode(Str.toStringz(text), &outLen);
109 		
110 		return cast(char[])p[0 .. outLen];
111 	}
112 
113 	/**
114 	 * Encode a sequence of binary data into its Base-64 stringified
115 	 * representation.
116 	 *
117 	 * Params:
118 	 *     data = the binary data to encode
119 	 *     len = the length of @data
120 	 *
121 	 * Returns: a newly allocated, zero-terminated Base-64
122 	 *     encoded string representing @data. The returned string must
123 	 *     be freed with g_free().
124 	 *
125 	 * Since: 2.12
126 	 */
127 	public static string encode(char[] data)
128 	{
129 		auto retStr = g_base64_encode(data.ptr, cast(size_t)data.length);
130 		
131 		scope(exit) Str.freeString(retStr);
132 		return Str.toString(retStr);
133 	}
134 
135 	/**
136 	 * Flush the status from a sequence of calls to g_base64_encode_step().
137 	 *
138 	 * The output buffer must be large enough to fit all the data that will
139 	 * be written to it. It will need up to 4 bytes, or up to 5 bytes if
140 	 * line-breaking is enabled.
141 	 *
142 	 * The @out array will not be automatically nul-terminated.
143 	 *
144 	 * Params:
145 	 *     breakLines = whether to break long lines
146 	 *     output = pointer to destination buffer
147 	 *     state = Saved state from g_base64_encode_step()
148 	 *     save = Saved state from g_base64_encode_step()
149 	 *
150 	 * Returns: The number of bytes of output that was written
151 	 *
152 	 * Since: 2.12
153 	 */
154 	public static size_t encodeClose(bool breakLines, out char[] output, ref int state, ref int save)
155 	{
156 		return g_base64_encode_close(breakLines, output.ptr, &state, &save);
157 	}
158 
159 	/**
160 	 * Incrementally encode a sequence of binary data into its Base-64 stringified
161 	 * representation. By calling this function multiple times you can convert
162 	 * data in chunks to avoid having to have the full encoded data in memory.
163 	 *
164 	 * When all of the data has been converted you must call
165 	 * g_base64_encode_close() to flush the saved state.
166 	 *
167 	 * The output buffer must be large enough to fit all the data that will
168 	 * be written to it. Due to the way base64 encodes you will need
169 	 * at least: (@len / 3 + 1) * 4 + 4 bytes (+ 4 may be needed in case of
170 	 * non-zero state). If you enable line-breaking you will need at least:
171 	 * ((@len / 3 + 1) * 4 + 4) / 72 + 1 bytes of extra space.
172 	 *
173 	 * @break_lines is typically used when putting base64-encoded data in emails.
174 	 * It breaks the lines at 72 columns instead of putting all of the text on
175 	 * the same line. This avoids problems with long lines in the email system.
176 	 * Note however that it breaks the lines with `LF` characters, not
177 	 * `CR LF` sequences, so the result cannot be passed directly to SMTP
178 	 * or certain other protocols.
179 	 *
180 	 * Params:
181 	 *     inn = the binary data to encode
182 	 *     len = the length of @in
183 	 *     breakLines = whether to break long lines
184 	 *     output = pointer to destination buffer
185 	 *     state = Saved state between steps, initialize to 0
186 	 *     save = Saved state between steps, initialize to 0
187 	 *
188 	 * Returns: The number of bytes of output that was written
189 	 *
190 	 * Since: 2.12
191 	 */
192 	public static size_t encodeStep(char[] inn, bool breakLines, out char[] output, ref int state, ref int save)
193 	{
194 		return g_base64_encode_step(inn.ptr, cast(size_t)inn.length, breakLines, output.ptr, &state, &save);
195 	}
196 }