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  * Conversion parameters:
26  * inFile  = glib-Base64-Encoding.html
27  * outPack = glib
28  * outFile = Base64
29  * strct   = 
30  * realStrct=
31  * ctorStrct=
32  * clss    = Base64
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_base64_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Str
47  * structWrap:
48  * module aliases:
49  * local aliases:
50  * overrides:
51  */
52 
53 module glib.Base64;
54 
55 public  import gtkc.glibtypes;
56 
57 private import gtkc.glib;
58 private import glib.ConstructionException;
59 
60 
61 private import glib.Str;
62 
63 
64 
65 
66 /**
67  * Description
68  * Base64 is an encoding that allows a sequence of arbitrary bytes to be
69  * encoded as a sequence of printable ASCII characters. For the definition
70  * of Base64, see RFC
71  * 1421 or RFC
72  * 2045. Base64 is most commonly used as a MIME transfer encoding
73  * for email.
74  * GLib supports incremental encoding using g_base64_encode_step() and
75  * g_base64_encode_close(). Incremental decoding can be done with
76  * g_base64_decode_step(). To encode or decode data in one go, use
77  * g_base64_encode() or g_base64_decode(). To avoid memory allocation when
78  * decoding, you can use g_base64_decode_inplace().
79  * Support for Base64 encoding has been added in GLib 2.12.
80  */
81 public class Base64
82 {
83 	
84 	/**
85 	 */
86 	
87 	/**
88 	 * Incrementally encode a sequence of binary data into its Base-64 stringified
89 	 * representation. By calling this function multiple times you can convert
90 	 * data in chunks to avoid having to have the full encoded data in memory.
91 	 * When all of the data has been converted you must call
92 	 * g_base64_encode_close() to flush the saved state.
93 	 * The output buffer must be large enough to fit all the data that will
94 	 * be written to it. Due to the way base64 encodes you will need
95 	 * at least: (len / 3 + 1) * 4 + 4 bytes (+ 4 may be needed in case of
96 	 * Since 2.12
97 	 * Params:
98 	 * in = the binary data to encode
99 	 * len = the length of in
100 	 * breakLines = whether to break long lines
101 	 * out = pointer to destination buffer
102 	 * state = Saved state between steps, initialize to 0
103 	 * save = Saved state between steps, initialize to 0
104 	 * Returns: The number of bytes of output that was written
105 	 */
106 	public static gsize encodeStep(char* inn, gsize len, int breakLines, string f_out, ref int state, ref int save)
107 	{
108 		// gsize g_base64_encode_step (const guchar *in,  gsize len,  gboolean break_lines,  gchar *out,  gint *state,  gint *save);
109 		return g_base64_encode_step(inn, len, breakLines, Str.toStringz(f_out), &state, &save);
110 	}
111 	
112 	/**
113 	 * Flush the status from a sequence of calls to g_base64_encode_step().
114 	 * The output buffer must be large enough to fit all the data that will
115 	 * be written to it. It will need up to 4 bytes, or up to 5 bytes if
116 	 * line-breaking is enabled.
117 	 * Since 2.12
118 	 * Params:
119 	 * breakLines = whether to break long lines
120 	 * out = pointer to destination buffer
121 	 * state = Saved state from g_base64_encode_step()
122 	 * save = Saved state from g_base64_encode_step()
123 	 * Returns: The number of bytes of output that was written
124 	 */
125 	public static gsize encodeClose(int breakLines, string f_out, ref int state, ref int save)
126 	{
127 		// gsize g_base64_encode_close (gboolean break_lines,  gchar *out,  gint *state,  gint *save);
128 		return g_base64_encode_close(breakLines, Str.toStringz(f_out), &state, &save);
129 	}
130 	
131 	/**
132 	 * Encode a sequence of binary data into its Base-64 stringified
133 	 * representation.
134 	 * Since 2.12
135 	 * Params:
136 	 * data = the binary data to encode
137 	 * len = the length of data
138 	 * Returns: a newly allocated, zero-terminated Base-64 encoded string representing data. The returned string must be freed with g_free().
139 	 */
140 	public static string encode(char* data, gsize len)
141 	{
142 		// gchar * g_base64_encode (const guchar *data,  gsize len);
143 		return Str.toString(g_base64_encode(data, len));
144 	}
145 	
146 	/**
147 	 * Incrementally decode a sequence of binary data from its Base-64 stringified
148 	 * representation. By calling this function multiple times you can convert
149 	 * data in chunks to avoid having to have the full encoded data in memory.
150 	 * The output buffer must be large enough to fit all the data that will
151 	 * be written to it. Since base64 encodes 3 bytes in 4 chars you need
152 	 * at least: (len / 4) * 3 + 3 bytes (+ 3 may be needed in case of non-zero
153 	 * state).
154 	 * Since 2.12
155 	 * Params:
156 	 * in = binary input data
157 	 * len = max length of in data to decode
158 	 * out = output buffer
159 	 * state = Saved state between steps, initialize to 0
160 	 * save = Saved state between steps, initialize to 0
161 	 * Returns: The number of bytes of output that was written
162 	 */
163 	public static gsize decodeStep(string inn, gsize len, char* f_out, ref int state, ref uint save)
164 	{
165 		// gsize g_base64_decode_step (const gchar *in,  gsize len,  guchar *out,  gint *state,  guint *save);
166 		return g_base64_decode_step(Str.toStringz(inn), len, f_out, &state, &save);
167 	}
168 	
169 	/**
170 	 * Decode a sequence of Base-64 encoded text into binary data
171 	 * Since 2.12
172 	 * Params:
173 	 * text = zero-terminated string with base64 text to decode
174 	 * Returns: a newly allocated buffer containing the binary data that text represents. The returned buffer must be freed with g_free().
175 	 */
176 	public static char[] decode(string text)
177 	{
178 		// guchar * g_base64_decode (const gchar *text,  gsize *out_len);
179 		gsize outLen;
180 		auto p = g_base64_decode(Str.toStringz(text), &outLen);
181 		
182 		if(p is null)
183 		{
184 			return null;
185 		}
186 		
187 		return p[0 .. outLen];
188 	}
189 	
190 	/**
191 	 * Decode a sequence of Base-64 encoded text into binary data
192 	 * by overwriting the input data.
193 	 * Since 2.20
194 	 * Params:
195 	 * text = zero-terminated string with base64 text to decode
196 	 * Returns: The binary data that text responds. This pointer is the same as the input text.
197 	 */
198 	public static char[] decodeInplace(string text)
199 	{
200 		// guchar * g_base64_decode_inplace (gchar *text,  gsize *out_len);
201 		gsize outLen;
202 		auto p = g_base64_decode_inplace(Str.toStringz(text), &outLen);
203 		
204 		if(p is null)
205 		{
206 			return null;
207 		}
208 		
209 		return p[0 .. outLen];
210 	}
211 }