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 glib.c.functions; 29 public import glib.c.types; 30 public import gtkc.glibtypes; 31 32 33 /** */ 34 public struct Base64 35 { 36 /** 37 * Incrementally decode a sequence of binary data from its Base-64 stringified 38 * representation. By calling this function multiple times you can convert 39 * data in chunks to avoid having to have the full encoded data in memory. 40 * 41 * The output buffer must be large enough to fit all the data that will 42 * be written to it. Since base64 encodes 3 bytes in 4 chars you need 43 * at least: (@len / 4) * 3 + 3 bytes (+ 3 may be needed in case of non-zero 44 * state). 45 * 46 * Params: 47 * inn = binary input data 48 * len = max length of @in data to decode 49 * output = output buffer 50 * state = Saved state between steps, initialize to 0 51 * save = Saved state between steps, initialize to 0 52 * 53 * Return: The number of bytes of output that was written 54 * 55 * Since: 2.12 56 */ 57 public static size_t decodeStep(string inn, ref ubyte[] output, ref int state, ref uint save) 58 { 59 auto p = g_base64_decode_step(Str.toStringz(inn), cast(int)inn.length, cast(char*)output.ptr, &state, &save); 60 61 return p; 62 } 63 64 /** 65 */ 66 67 /** 68 * Decode a sequence of Base-64 encoded text into binary data 69 * by overwriting the input data. 70 * 71 * Params: 72 * text = zero-terminated 73 * string with base64 text to decode 74 * 75 * Returns: The binary data that @text responds. This pointer 76 * is the same as the input @text. 77 * 78 * Since: 2.20 79 */ 80 public static char[] decodeInplace(ref char[] text) 81 { 82 size_t outLen = cast(size_t)text.length; 83 84 auto __p = g_base64_decode_inplace(text.ptr, &outLen); 85 86 text = text[0..outLen]; 87 88 return __p[0 .. outLen]; 89 } 90 91 /** 92 * Decode a sequence of Base-64 encoded text into binary data. Note 93 * that the returned binary data is not necessarily zero-terminated, 94 * so it should not be used as a character string. 95 * 96 * Params: 97 * text = zero-terminated string with base64 text to decode 98 * 99 * Returns: newly allocated buffer containing the binary data 100 * that @text represents. The returned buffer must 101 * be freed with g_free(). 102 * 103 * Since: 2.12 104 */ 105 public static char[] decode(string text) 106 { 107 size_t outLen; 108 109 auto __p = g_base64_decode(Str.toStringz(text), &outLen); 110 111 return cast(char[])__p[0 .. outLen]; 112 } 113 114 /** 115 * Encode a sequence of binary data into its Base-64 stringified 116 * representation. 117 * 118 * Params: 119 * data = the binary data to encode 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 * out_ = 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[] out_, ref int state, ref int save) 155 { 156 return g_base64_encode_close(breakLines, out_.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) / 76 + 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 76 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 * in_ = the binary data to encode 182 * breakLines = whether to break long lines 183 * out_ = pointer to destination buffer 184 * state = Saved state between steps, initialize to 0 185 * save = Saved state between steps, initialize to 0 186 * 187 * Returns: The number of bytes of output that was written 188 * 189 * Since: 2.12 190 */ 191 public static size_t encodeStep(char[] in_, bool breakLines, out char[] out_, ref int state, ref int save) 192 { 193 return g_base64_encode_step(in_.ptr, cast(size_t)in_.length, breakLines, out_.ptr, &state, &save); 194 } 195 }