Debian Codegraph @

terps/tads/tads3/include/bytearr.h

raw symbols SCIP
1 #charset "us-ascii"
2 #pragma once
3
4 /*
5 * Copyright (c) 2001, 2006 Michael J. Roberts
6 *
7 * This file is part of TADS 3.
8 *
9 * This header defines the ByteArray intrinsic class.
10 */
11
12 /* include our base class definition */
13 #include "systype.h"
14
15 /* we need the CharacterSet class for mapToString */
16 #include "charset.h"
17
18 /*
19 * 'ByteArray' intrinsic class. This class provides a fixed-size array of
20 * unsigned 8-bit byte values; each array element is an integer in the
21 * range 0-255.
22 *
23 * ByteArray is particularly useful for reading and writing binary files,
24 * since it lets you manipulate the raw bytes in a file directly.
25 */
26 intrinsic class ByteArray 'bytearray/030002': Object
27 {
28 /*
29 * Constructors:
30 *
31 * new ByteArray(length) - create a byte array with the given number of
32 * bytes. All elements in the new array are initialized to zero.
33 *
34 * new ByteArray(byteArray, startIndex?, length?) - create a new byte
35 * array as a copy of the given byte range of the given byte array,
36 * which must be an object of intrinsic class ByteArray. If the
37 * starting index and length are not given, the new object is a
38 * complete copy of the source byte array.
39 *
40 * new ByteArray(string, charset?) - create a new byte array by
41 * mapping the given string to bytes. If 'charset' is provided,
42 * it's a CharacterSet object, or a string giving the name of a
43 * character set; the characters are mapped to bytes using this
44 * character set mapping. If 'charset' is missing or nil, the
45 * Unicode character codes of the characters are used as the byte
46 * values for the new array; in this mode, the character codes
47 * must each fit into a byte, meaning that they're in the range 0
48 * to 255, or a numeric overflow error will occur.
49 */
50
51 /*
52 * Get the number of bytes in the array. The length is fixed at
53 * creation time.
54 */
55 length();
56
57 /*
58 * create a new ByteArray as a copy of the given range of this array;
59 * if the length is not given, bytes from the starting index to the
60 * end of this array are included in the new array
61 */
62 subarray(startIndex, length?);
63
64 /*
65 * Copy bytes from the source array into this array. Bytes are copied
66 * into this array starting at the given index. The specified number
67 * of bytes are copied from the source array starting at the given
68 * index.
69 */
70 copyFrom(src, srcStartIndex, dstStartIndex, length);
71
72 /*
73 * Fill bytes in this array with the given value. If no starting
74 * index or length values are given, the entire array is filled with
75 * the given byte value. The byte value must be an integer in the
76 * range 0 to 255.
77 */
78 fillValue(val, startIndex?, length?);
79
80 /*
81 * Convert a range of bytes in the array to a string, interpreting the
82 * bytes in the array as characters in the given character set.
83 *
84 * If the starting index and length are not given, the entire byte
85 * array is converted to a string. 'charset' must be an object of
86 * intrinsic class CharacterSet.
87 */
88 mapToString(charset, startIndex?, length?);
89
90 /*
91 * Read an integer value from the byte array. Reads bytes from the
92 * starting index; the number of bytes read depends on the format.
93 * Returns an integer giving the value read.
94 *
95 * 'format' gives the format of the integer to be read. This is a
96 * combination (using '|' operators) of three constants, giving the
97 * size, byte order, and signedness of the value.
98 *
99 * First, choose the SIZE of the value: FmtSize8, FmtSize16, FmtSize32,
100 * for 8-bit, 16-bit, and 32-bit integers, respectively.
101 *
102 * Second, choose the BYTE ORDER of the value, as represented in the
103 * byte array: FmtLittleEndian or FmtBigEndian. The standard T3
104 * portable data interchange format uses little-endian values; this is
105 * a format in which the least-significant byte of a value comes first,
106 * followed by the remaining bytes in ascending order of significance.
107 * The big-endian format uses the opposite order. The byte order
108 * obviously is irrelevant for 8-bit values.
109 *
110 * Third, choose the SIGNEDNESS of the value: FmtSigned or FmtUnsigned.
111 * Note that FmtUnsigned cannot be used with FmtSize32, because T3
112 * itself doesn't have an unsigned 32-bit integer type.
113 *
114 * For example, to read a 16-bit unsigned integer in the standard T3
115 * portable interchange format, you'd use
116 *
117 *. FmtUnsigned | FmtSize16 | FmtLittleEndian
118 *
119 * For convenience, individual macros are also defined that pre-compose
120 * all of the meaningful combinations; see below.
121 *
122 * The byte array must be large enough to read the required number of
123 * bytes starting at the given index. An "index out of range"
124 * exception is thrown if there aren't enough bytes in the array to
125 * satisfy the request.
126 */
127 readInt(startIndex, format);
128
129 /*
130 * Write an integer value to the byte array. Writes bytes starting at
131 * the given index; the number of bytes written depends on the format.
132 * The 'format' parameter gives the format, using the same codes as in
133 * readInt(). 'val' is the integer value to be written. If 'val' is
134 * outside of the bounds of the format to be written, the written
135 * value is truncated.
136 *
137 * The byte array must be large enough to hold the number of bytes
138 * required by the format starting at the starting index. An "index
139 * out of range" exception is thrown if the byte array doesn't have
140 * enough space to store the value.
141 *
142 * The value is not checked for range. If the value is outside of the
143 * range that the format is capable of storing, the value stored will
144 * be truncated to its least significant bits that fit the format.
145 * For example, attempting to store 0xABCD in an 8-bit format will
146 * store only 0xCD.
147 *
148 * Note that the signedness doesn't matter when writing a value. The
149 * signedness is important only when reading the value back in.
150 */
151 writeInt(startIndex, format, val);
152
153 /*
154 * Pack data values into bytes according to a format definition string,
155 * and store the packed bytes in the byte array starting at the given
156 * index.
157 *
158 * 'idx' is the starting index in the array for the packed bytes.
159 * 'format' is the format string, which specifies the binary
160 * representations to use for the argument values. The remaining
161 * arguments after 'format' are the data values to pack.
162 *
163 * Returns the number of bytes written to the array. (More precisely,
164 * returns the final write pointer as a byte offset from 'idx'. If a
165 * positioning code like @ or X is used in the string, it's possible
166 * that more bytes were actually written.)
167 *
168 * You can also call packBytes a static method, on the ByteArray class
169 * itself:
170 *
171 *. ByteArray.packBytes(format, ...)
172 *
173 * The static version of the method packs the data into bytes the same
174 * way the regular method does, but returns a new ByteArray object
175 * containing the packed bytes. Note that there's no index argument
176 * with the static version.
177 *
178 * Refer to Byte Packing in the System Manual for details.
179 */
180 packBytes(idx, format, ...);
181
182 /*
183 * Unpack bytes from the byte array starting at the given index, and
184 * translate the bytes into data values according to the given format
185 * string.
186 *
187 * 'idx' is the starting index in the array for the unpacking, and
188 * 'format' is the format string. Returns a list of the unpacked
189 * values.
190 *
191 * Refer to Byte Packing in the System Manual for details.
192 */
193 unpackBytes(idx, format);
194
195 /*
196 * Get the SHA-256 hash of the bytes in the array. This calculates the
197 * 256-bit Secure Hash Algorithm 2 hash value, returning the hash as a
198 * 64-character string of hex digits. The hash value is computed on
199 * the UTF-8 representation of the string. If 'idx' and 'len' are
200 * specified, they give the range of bytes to include in the hash; the
201 * default is to hash the whole array.
202 */
203 sha256(idx?, len?);
204
205 /*
206 * Get the MD5 digest of the string. This calculates the 128-bit RSA
207 * MD5 digest value, returning the digest as a 32-character string of
208 * hex digits. The hash value is computed on the UTF-8 representation
209 * of the string. If 'idx' and 'len' are specified, the give the range
210 * of bytes to include in the hash; the default is to hash the whole
211 * array.
212 */
213 digestMD5();
214 }
215
216 /* ------------------------------------------------------------------------ */
217 /*
218 * Integer format codes, for readInt() and writeInt()
219 */
220
221 /* integer sizes */
222 #define FmtSize8 0x0000
223 #define FmtSize16 0x0001
224 #define FmtSize32 0x0002
225
226 /* integer byte orders */
227 #define FmtLittleEndian 0x0000
228 #define FmtBigEndian 0x0010
229
230 /* integer signedness */
231 #define FmtSigned 0x0000
232 #define FmtUnsigned 0x0100
233
234 /* pre-composed integer formats */
235 #define FmtInt8 (FmtSize8 | FmtSigned)
236 #define FmtUInt8 (FmtSize8 | FmtUnsigned)
237
238 #define FmtInt16LE (FmtSize16 | FmtLittleEndian | FmtSigned)
239 #define FmtUInt16LE (FmtSize16 | FmtLittleEndian | FmtUnsigned)
240 #define FmtInt16BE (FmtSize16 | FmtBigEndian | FmtSigned)
241 #define FmtUInt16BE (FmtSize16 | FmtBigEndian | FmtUnsigned)
242
243 #define FmtInt32LE (FmtSize32 | FmtLittleEndian | FmtSigned)
244 #define FmtInt32BE (FmtSize32 | FmtBigEndian | FmtSigned)
245