1package cbor
2
3import (
4	"fmt"
5	"math"
6	"net"
7)
8
9// AppendNil inserts a 'Nil' object into the dst byte array.
10func (Encoder) AppendNil(dst []byte) []byte {
11	return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeNull))
12}
13
14// AppendBeginMarker inserts a map start into the dst byte array.
15func (Encoder) AppendBeginMarker(dst []byte) []byte {
16	return append(dst, byte(majorTypeMap|additionalTypeInfiniteCount))
17}
18
19// AppendEndMarker inserts a map end into the dst byte array.
20func (Encoder) AppendEndMarker(dst []byte) []byte {
21	return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak))
22}
23
24// AppendObjectData takes an object in form of a byte array and appends to dst.
25func (Encoder) AppendObjectData(dst []byte, o []byte) []byte {
26	// BeginMarker is present in the dst, which
27	// should not be copied when appending to existing data.
28	return append(dst, o[1:]...)
29}
30
31// AppendArrayStart adds markers to indicate the start of an array.
32func (Encoder) AppendArrayStart(dst []byte) []byte {
33	return append(dst, byte(majorTypeArray|additionalTypeInfiniteCount))
34}
35
36// AppendArrayEnd adds markers to indicate the end of an array.
37func (Encoder) AppendArrayEnd(dst []byte) []byte {
38	return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak))
39}
40
41// AppendArrayDelim adds markers to indicate end of a particular array element.
42func (Encoder) AppendArrayDelim(dst []byte) []byte {
43	//No delimiters needed in cbor
44	return dst
45}
46
47// AppendLineBreak is a noop that keep API compat with json encoder.
48func (Encoder) AppendLineBreak(dst []byte) []byte {
49	// No line breaks needed in binary format.
50	return dst
51}
52
53// AppendBool encodes and inserts a boolean value into the dst byte array.
54func (Encoder) AppendBool(dst []byte, val bool) []byte {
55	b := additionalTypeBoolFalse
56	if val {
57		b = additionalTypeBoolTrue
58	}
59	return append(dst, byte(majorTypeSimpleAndFloat|b))
60}
61
62// AppendBools encodes and inserts an array of boolean values into the dst byte array.
63func (e Encoder) AppendBools(dst []byte, vals []bool) []byte {
64	major := majorTypeArray
65	l := len(vals)
66	if l == 0 {
67		return e.AppendArrayEnd(e.AppendArrayStart(dst))
68	}
69	if l <= additionalMax {
70		lb := byte(l)
71		dst = append(dst, byte(major|lb))
72	} else {
73		dst = appendCborTypePrefix(dst, major, uint64(l))
74	}
75	for _, v := range vals {
76		dst = e.AppendBool(dst, v)
77	}
78	return dst
79}
80
81// AppendInt encodes and inserts an integer value into the dst byte array.
82func (Encoder) AppendInt(dst []byte, val int) []byte {
83	major := majorTypeUnsignedInt
84	contentVal := val
85	if val < 0 {
86		major = majorTypeNegativeInt
87		contentVal = -val - 1
88	}
89	if contentVal <= additionalMax {
90		lb := byte(contentVal)
91		dst = append(dst, byte(major|lb))
92	} else {
93		dst = appendCborTypePrefix(dst, major, uint64(contentVal))
94	}
95	return dst
96}
97
98// AppendInts encodes and inserts an array of integer values into the dst byte array.
99func (e Encoder) AppendInts(dst []byte, vals []int) []byte {
100	major := majorTypeArray
101	l := len(vals)
102	if l == 0 {
103		return e.AppendArrayEnd(e.AppendArrayStart(dst))
104	}
105	if l <= additionalMax {
106		lb := byte(l)
107		dst = append(dst, byte(major|lb))
108	} else {
109		dst = appendCborTypePrefix(dst, major, uint64(l))
110	}
111	for _, v := range vals {
112		dst = e.AppendInt(dst, v)
113	}
114	return dst
115}
116
117// AppendInt8 encodes and inserts an int8 value into the dst byte array.
118func (e Encoder) AppendInt8(dst []byte, val int8) []byte {
119	return e.AppendInt(dst, int(val))
120}
121
122// AppendInts8 encodes and inserts an array of integer values into the dst byte array.
123func (e Encoder) AppendInts8(dst []byte, vals []int8) []byte {
124	major := majorTypeArray
125	l := len(vals)
126	if l == 0 {
127		return e.AppendArrayEnd(e.AppendArrayStart(dst))
128	}
129	if l <= additionalMax {
130		lb := byte(l)
131		dst = append(dst, byte(major|lb))
132	} else {
133		dst = appendCborTypePrefix(dst, major, uint64(l))
134	}
135	for _, v := range vals {
136		dst = e.AppendInt(dst, int(v))
137	}
138	return dst
139}
140
141// AppendInt16 encodes and inserts a int16 value into the dst byte array.
142func (e Encoder) AppendInt16(dst []byte, val int16) []byte {
143	return e.AppendInt(dst, int(val))
144}
145
146// AppendInts16 encodes and inserts an array of int16 values into the dst byte array.
147func (e Encoder) AppendInts16(dst []byte, vals []int16) []byte {
148	major := majorTypeArray
149	l := len(vals)
150	if l == 0 {
151		return e.AppendArrayEnd(e.AppendArrayStart(dst))
152	}
153	if l <= additionalMax {
154		lb := byte(l)
155		dst = append(dst, byte(major|lb))
156	} else {
157		dst = appendCborTypePrefix(dst, major, uint64(l))
158	}
159	for _, v := range vals {
160		dst = e.AppendInt(dst, int(v))
161	}
162	return dst
163}
164
165// AppendInt32 encodes and inserts a int32 value into the dst byte array.
166func (e Encoder) AppendInt32(dst []byte, val int32) []byte {
167	return e.AppendInt(dst, int(val))
168}
169
170// AppendInts32 encodes and inserts an array of int32 values into the dst byte array.
171func (e Encoder) AppendInts32(dst []byte, vals []int32) []byte {
172	major := majorTypeArray
173	l := len(vals)
174	if l == 0 {
175		return e.AppendArrayEnd(e.AppendArrayStart(dst))
176	}
177	if l <= additionalMax {
178		lb := byte(l)
179		dst = append(dst, byte(major|lb))
180	} else {
181		dst = appendCborTypePrefix(dst, major, uint64(l))
182	}
183	for _, v := range vals {
184		dst = e.AppendInt(dst, int(v))
185	}
186	return dst
187}
188
189// AppendInt64 encodes and inserts a int64 value into the dst byte array.
190func (Encoder) AppendInt64(dst []byte, val int64) []byte {
191	major := majorTypeUnsignedInt
192	contentVal := val
193	if val < 0 {
194		major = majorTypeNegativeInt
195		contentVal = -val - 1
196	}
197	if contentVal <= additionalMax {
198		lb := byte(contentVal)
199		dst = append(dst, byte(major|lb))
200	} else {
201		dst = appendCborTypePrefix(dst, major, uint64(contentVal))
202	}
203	return dst
204}
205
206// AppendInts64 encodes and inserts an array of int64 values into the dst byte array.
207func (e Encoder) AppendInts64(dst []byte, vals []int64) []byte {
208	major := majorTypeArray
209	l := len(vals)
210	if l == 0 {
211		return e.AppendArrayEnd(e.AppendArrayStart(dst))
212	}
213	if l <= additionalMax {
214		lb := byte(l)
215		dst = append(dst, byte(major|lb))
216	} else {
217		dst = appendCborTypePrefix(dst, major, uint64(l))
218	}
219	for _, v := range vals {
220		dst = e.AppendInt64(dst, v)
221	}
222	return dst
223}
224
225// AppendUint encodes and inserts an unsigned integer value into the dst byte array.
226func (e Encoder) AppendUint(dst []byte, val uint) []byte {
227	return e.AppendInt64(dst, int64(val))
228}
229
230// AppendUints encodes and inserts an array of unsigned integer values into the dst byte array.
231func (e Encoder) AppendUints(dst []byte, vals []uint) []byte {
232	major := majorTypeArray
233	l := len(vals)
234	if l == 0 {
235		return e.AppendArrayEnd(e.AppendArrayStart(dst))
236	}
237	if l <= additionalMax {
238		lb := byte(l)
239		dst = append(dst, byte(major|lb))
240	} else {
241		dst = appendCborTypePrefix(dst, major, uint64(l))
242	}
243	for _, v := range vals {
244		dst = e.AppendUint(dst, v)
245	}
246	return dst
247}
248
249// AppendUint8 encodes and inserts a unsigned int8 value into the dst byte array.
250func (e Encoder) AppendUint8(dst []byte, val uint8) []byte {
251	return e.AppendUint(dst, uint(val))
252}
253
254// AppendUints8 encodes and inserts an array of uint8 values into the dst byte array.
255func (e Encoder) AppendUints8(dst []byte, vals []uint8) []byte {
256	major := majorTypeArray
257	l := len(vals)
258	if l == 0 {
259		return e.AppendArrayEnd(e.AppendArrayStart(dst))
260	}
261	if l <= additionalMax {
262		lb := byte(l)
263		dst = append(dst, byte(major|lb))
264	} else {
265		dst = appendCborTypePrefix(dst, major, uint64(l))
266	}
267	for _, v := range vals {
268		dst = e.AppendUint8(dst, v)
269	}
270	return dst
271}
272
273// AppendUint16 encodes and inserts a uint16 value into the dst byte array.
274func (e Encoder) AppendUint16(dst []byte, val uint16) []byte {
275	return e.AppendUint(dst, uint(val))
276}
277
278// AppendUints16 encodes and inserts an array of uint16 values into the dst byte array.
279func (e Encoder) AppendUints16(dst []byte, vals []uint16) []byte {
280	major := majorTypeArray
281	l := len(vals)
282	if l == 0 {
283		return e.AppendArrayEnd(e.AppendArrayStart(dst))
284	}
285	if l <= additionalMax {
286		lb := byte(l)
287		dst = append(dst, byte(major|lb))
288	} else {
289		dst = appendCborTypePrefix(dst, major, uint64(l))
290	}
291	for _, v := range vals {
292		dst = e.AppendUint16(dst, v)
293	}
294	return dst
295}
296
297// AppendUint32 encodes and inserts a uint32 value into the dst byte array.
298func (e Encoder) AppendUint32(dst []byte, val uint32) []byte {
299	return e.AppendUint(dst, uint(val))
300}
301
302// AppendUints32 encodes and inserts an array of uint32 values into the dst byte array.
303func (e Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
304	major := majorTypeArray
305	l := len(vals)
306	if l == 0 {
307		return e.AppendArrayEnd(e.AppendArrayStart(dst))
308	}
309	if l <= additionalMax {
310		lb := byte(l)
311		dst = append(dst, byte(major|lb))
312	} else {
313		dst = appendCborTypePrefix(dst, major, uint64(l))
314	}
315	for _, v := range vals {
316		dst = e.AppendUint32(dst, v)
317	}
318	return dst
319}
320
321// AppendUint64 encodes and inserts a uint64 value into the dst byte array.
322func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
323	major := majorTypeUnsignedInt
324	contentVal := val
325	if contentVal <= additionalMax {
326		lb := byte(contentVal)
327		dst = append(dst, byte(major|lb))
328	} else {
329		dst = appendCborTypePrefix(dst, major, uint64(contentVal))
330	}
331	return dst
332}
333
334// AppendUints64 encodes and inserts an array of uint64 values into the dst byte array.
335func (e Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
336	major := majorTypeArray
337	l := len(vals)
338	if l == 0 {
339		return e.AppendArrayEnd(e.AppendArrayStart(dst))
340	}
341	if l <= additionalMax {
342		lb := byte(l)
343		dst = append(dst, byte(major|lb))
344	} else {
345		dst = appendCborTypePrefix(dst, major, uint64(l))
346	}
347	for _, v := range vals {
348		dst = e.AppendUint64(dst, v)
349	}
350	return dst
351}
352
353// AppendFloat32 encodes and inserts a single precision float value into the dst byte array.
354func (Encoder) AppendFloat32(dst []byte, val float32) []byte {
355	switch {
356	case math.IsNaN(float64(val)):
357		return append(dst, "\xfa\x7f\xc0\x00\x00"...)
358	case math.IsInf(float64(val), 1):
359		return append(dst, "\xfa\x7f\x80\x00\x00"...)
360	case math.IsInf(float64(val), -1):
361		return append(dst, "\xfa\xff\x80\x00\x00"...)
362	}
363	major := majorTypeSimpleAndFloat
364	subType := additionalTypeFloat32
365	n := math.Float32bits(val)
366	var buf [4]byte
367	for i := uint(0); i < 4; i++ {
368		buf[i] = byte(n >> ((3 - i) * 8))
369	}
370	return append(append(dst, byte(major|subType)), buf[0], buf[1], buf[2], buf[3])
371}
372
373// AppendFloats32 encodes and inserts an array of single precision float value into the dst byte array.
374func (e Encoder) AppendFloats32(dst []byte, vals []float32) []byte {
375	major := majorTypeArray
376	l := len(vals)
377	if l == 0 {
378		return e.AppendArrayEnd(e.AppendArrayStart(dst))
379	}
380	if l <= additionalMax {
381		lb := byte(l)
382		dst = append(dst, byte(major|lb))
383	} else {
384		dst = appendCborTypePrefix(dst, major, uint64(l))
385	}
386	for _, v := range vals {
387		dst = e.AppendFloat32(dst, v)
388	}
389	return dst
390}
391
392// AppendFloat64 encodes and inserts a double precision float value into the dst byte array.
393func (Encoder) AppendFloat64(dst []byte, val float64) []byte {
394	switch {
395	case math.IsNaN(val):
396		return append(dst, "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00"...)
397	case math.IsInf(val, 1):
398		return append(dst, "\xfb\x7f\xf0\x00\x00\x00\x00\x00\x00"...)
399	case math.IsInf(val, -1):
400		return append(dst, "\xfb\xff\xf0\x00\x00\x00\x00\x00\x00"...)
401	}
402	major := majorTypeSimpleAndFloat
403	subType := additionalTypeFloat64
404	n := math.Float64bits(val)
405	dst = append(dst, byte(major|subType))
406	for i := uint(1); i <= 8; i++ {
407		b := byte(n >> ((8 - i) * 8))
408		dst = append(dst, b)
409	}
410	return dst
411}
412
413// AppendFloats64 encodes and inserts an array of double precision float values into the dst byte array.
414func (e Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
415	major := majorTypeArray
416	l := len(vals)
417	if l == 0 {
418		return e.AppendArrayEnd(e.AppendArrayStart(dst))
419	}
420	if l <= additionalMax {
421		lb := byte(l)
422		dst = append(dst, byte(major|lb))
423	} else {
424		dst = appendCborTypePrefix(dst, major, uint64(l))
425	}
426	for _, v := range vals {
427		dst = e.AppendFloat64(dst, v)
428	}
429	return dst
430}
431
432// AppendInterface takes an arbitrary object and converts it to JSON and embeds it dst.
433func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
434	marshaled, err := JSONMarshalFunc(i)
435	if err != nil {
436		return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
437	}
438	return AppendEmbeddedJSON(dst, marshaled)
439}
440
441// AppendIPAddr encodes and inserts an IP Address (IPv4 or IPv6).
442func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
443	dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
444	dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
445	dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
446	return e.AppendBytes(dst, ip)
447}
448
449// AppendIPPrefix encodes and inserts an IP Address Prefix (Address + Mask Length).
450func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
451	dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
452	dst = append(dst, byte(additionalTypeTagNetworkPrefix>>8))
453	dst = append(dst, byte(additionalTypeTagNetworkPrefix&0xff))
454
455	// Prefix is a tuple (aka MAP of 1 pair of elements) -
456	// first element is prefix, second is mask length.
457	dst = append(dst, byte(majorTypeMap|0x1))
458	dst = e.AppendBytes(dst, pfx.IP)
459	maskLen, _ := pfx.Mask.Size()
460	return e.AppendUint8(dst, uint8(maskLen))
461}
462
463// AppendMACAddr encodes and inserts an Hardware (MAC) address.
464func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
465	dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
466	dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
467	dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
468	return e.AppendBytes(dst, ha)
469}
470
471// AppendHex adds a TAG and inserts a hex bytes as a string.
472func (e Encoder) AppendHex(dst []byte, val []byte) []byte {
473	dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
474	dst = append(dst, byte(additionalTypeTagHexString>>8))
475	dst = append(dst, byte(additionalTypeTagHexString&0xff))
476	return e.AppendBytes(dst, val)
477}
478