Bug 1700051: part 46) Const-qualify `mozInlineSpellStatus::mAnchorRange`. r=smaug
[gecko.git] / dom / webauthn / cbor-cpp / src / encoder.cpp
blobceac69646866070ac68cd81117805357646ce5f8
1 /*
2 Copyright 2014-2015 Stanislav Ovsyannikov
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #include "encoder.h"
19 using namespace cbor;
22 encoder::encoder(output &out) {
23 _out = &out;
26 encoder::~encoder() {
30 void encoder::write_type_value(int major_type, unsigned int value) {
31 major_type <<= 5;
32 if(value < 24) {
33 _out->put_byte((unsigned char) (major_type | value));
34 } else if(value < 256) {
35 _out->put_byte((unsigned char) (major_type | 24));
36 _out->put_byte((unsigned char) value);
37 } else if(value < 65536) {
38 _out->put_byte((unsigned char) (major_type | 25));
39 _out->put_byte((unsigned char) (value >> 8));
40 _out->put_byte((unsigned char) value);
41 } else {
42 _out->put_byte((unsigned char) (major_type | 26));
43 _out->put_byte((unsigned char) (value >> 24));
44 _out->put_byte((unsigned char) (value >> 16));
45 _out->put_byte((unsigned char) (value >> 8));
46 _out->put_byte((unsigned char) value);
50 void encoder::write_type_value(int major_type, unsigned long long value) {
51 major_type <<= 5;
52 if(value < 24ULL) {
53 _out->put_byte((unsigned char) (major_type | value));
54 } else if(value < 256ULL) {
55 _out->put_byte((unsigned char) (major_type | 24));
56 _out->put_byte((unsigned char) value);
57 } else if(value < 65536ULL) {
58 _out->put_byte((unsigned char) (major_type | 25));
59 _out->put_byte((unsigned char) (value >> 8));
60 _out->put_byte((unsigned char) value);
61 } else if(value < 4294967296ULL) {
62 _out->put_byte((unsigned char) (major_type | 26));
63 _out->put_byte((unsigned char) (value >> 24));
64 _out->put_byte((unsigned char) (value >> 16));
65 _out->put_byte((unsigned char) (value >> 8));
66 _out->put_byte((unsigned char) value);
67 } else {
68 _out->put_byte((unsigned char) (major_type | 27));
69 _out->put_byte((unsigned char) (value >> 56));
70 _out->put_byte((unsigned char) (value >> 48));
71 _out->put_byte((unsigned char) (value >> 40));
72 _out->put_byte((unsigned char) (value >> 32));
73 _out->put_byte((unsigned char) (value >> 24));
74 _out->put_byte((unsigned char) (value >> 16));
75 _out->put_byte((unsigned char) (value >> 8));
76 _out->put_byte((unsigned char) value);
80 void encoder::write_int(unsigned int value) {
81 write_type_value(0, value);
84 void encoder::write_int(unsigned long long value) {
85 write_type_value(0, value);
88 void encoder::write_int(long long value) {
89 if(value < 0) {
90 write_type_value(1, (unsigned long long) -(value+1));
91 } else {
92 write_type_value(0, (unsigned long long) value);
96 void encoder::write_int(int value) {
97 if(value < 0) {
98 write_type_value(1, (unsigned int) -(value+1));
99 } else {
100 write_type_value(0, (unsigned int) value);
104 void encoder::write_bytes(const unsigned char *data, unsigned int size) {
105 write_type_value(2, size);
106 _out->put_bytes(data, size);
109 void encoder::write_string(const char *data, unsigned int size) {
110 write_type_value(3, size);
111 _out->put_bytes((const unsigned char *) data, size);
114 void encoder::write_string(const std::string str) {
115 write_type_value(3, (unsigned int) str.size());
116 _out->put_bytes((const unsigned char *) str.c_str(), (int) str.size());
120 void encoder::write_array(int size) {
121 write_type_value(4, (unsigned int) size);
124 void encoder::write_map(int size) {
125 write_type_value(5, (unsigned int) size);
128 void encoder::write_tag(const unsigned int tag) {
129 write_type_value(6, tag);
132 void encoder::write_special(int special) {
133 write_type_value(7, (unsigned int) special);
136 void encoder::write_bool(bool value) {
137 if (value == true) {
138 _out->put_byte((unsigned char) 0xf5);
139 } else {
140 _out->put_byte((unsigned char) 0xf4);
144 void encoder::write_null() {
145 _out->put_byte((unsigned char) 0xf6);
148 void encoder::write_undefined() {
149 _out->put_byte((unsigned char) 0xf7);