2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 using System.Collections.Generic;
31 internal long ID { get; private set; }
32 internal int Address { get; set; }
42 internal ConstData(T0Comp ctx)
44 ID = ctx.NextBlobID();
51 int tlen = len + elen;
52 if (tlen > buf.Length) {
53 int nlen = Math.Max(buf.Length << 1, tlen);
54 byte[] nbuf = new byte[nlen];
55 Array.Copy(buf, 0, nbuf, 0, len);
60 internal void Add8(byte b)
66 internal void Add16(int x)
69 buf[len ++] = (byte)(x >> 8);
70 buf[len ++] = (byte)x;
73 internal void Add24(int x)
76 buf[len ++] = (byte)(x >> 16);
77 buf[len ++] = (byte)(x >> 8);
78 buf[len ++] = (byte)x;
81 internal void Add32(int x)
84 buf[len ++] = (byte)(x >> 24);
85 buf[len ++] = (byte)(x >> 16);
86 buf[len ++] = (byte)(x >> 8);
87 buf[len ++] = (byte)x;
90 internal void AddString(string s)
92 byte[] sd = Encoding.UTF8.GetBytes(s);
93 Expand(sd.Length + 1);
94 Array.Copy(sd, 0, buf, len, sd.Length);
95 buf[len + sd.Length] = 0;
99 void CheckIndex(int off, int dlen)
101 if (off < 0 || off > (len - dlen)) {
102 throw new IndexOutOfRangeException();
106 internal void Set8(int off, byte v)
112 internal byte Read8(int off)
118 internal int Read16(int off)
121 return (buf[off] << 8) | buf[off + 1];
124 internal int Read24(int off)
127 return (buf[off] << 16) | (buf[off + 1] << 8) | buf[off + 2];
130 internal int Read32(int off)
133 return (buf[off] << 24) | (buf[off + 1] << 16)
134 | (buf[off + 2] << 8) | buf[off + 3];
137 internal string ToString(int off)
139 StringBuilder sb = new StringBuilder();
141 int x = DecodeUTF8(ref off);
143 return sb.ToString();
149 sb.Append((char)(0xD800 + (x >> 10)));
150 sb.Append((char)(0xDC00 + (x & 0x3FF)));
155 int DecodeUTF8(ref int off)
158 throw new IndexOutOfRangeException();
161 if (x < 0xC0 || x > 0xF7) {
168 } else if (x >= 0xE0) {
175 if (off + elen > len) {
178 for (int i = 0; i < elen; i ++) {
179 int y = buf[off + i];
180 if (y < 0x80 || y >= 0xC0) {
183 acc = (acc << 6) + (y & 0x3F);
185 if (acc > 0x10FFFF) {
192 internal void Encode(BlobWriter bw)
194 for (int i = 0; i < len; i ++) {