File: bson-native.h

package info (click to toggle)
ruby-bson 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,828 kB
  • sloc: ruby: 11,712; ansic: 1,427; java: 514; makefile: 8
file content (135 lines) | stat: -rw-r--r-- 5,119 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2009-2020 MongoDB Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <ruby.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "bson-endian.h"

void
rb_bson_utf8_validate (const char *utf8, /* IN */
                    size_t utf8_len,  /* IN */
                    bool allow_null, /* IN */
                    const char *data_type);  /* IN */

#define BSON_BYTE_BUFFER_SIZE 1024

#ifndef HOST_NAME_HASH_MAX
#define HOST_NAME_HASH_MAX 256
#endif

/* See the type list in http://bsonspec.org/spec.html. */
#define BSON_TYPE_DOUBLE        1
#define BSON_TYPE_STRING        2
#define BSON_TYPE_DOCUMENT      3
#define BSON_TYPE_ARRAY         4
#define BSON_TYPE_BOOLEAN       8
#define BSON_TYPE_SYMBOL        0x0E
#define BSON_TYPE_INT32         0x10
#define BSON_TYPE_INT64         0x12

typedef struct {
  size_t size;
  size_t write_position;
  size_t read_position;
  char   buffer[BSON_BYTE_BUFFER_SIZE];
  char   *b_ptr;
} byte_buffer_t;

#define READ_PTR(byte_buffer_ptr) \
  (byte_buffer_ptr->b_ptr + byte_buffer_ptr->read_position)

#define READ_SIZE(byte_buffer_ptr) \
  (byte_buffer_ptr->write_position - byte_buffer_ptr->read_position)

#define WRITE_PTR(byte_buffer_ptr) \
  (byte_buffer_ptr->b_ptr + byte_buffer_ptr->write_position)

#define ENSURE_BSON_WRITE(buffer_ptr, length) \
  { if (buffer_ptr->write_position + length > buffer_ptr->size) rb_bson_expand_buffer(buffer_ptr, length); }

#define ENSURE_BSON_READ(buffer_ptr, length) \
  { if (buffer_ptr->read_position + length > buffer_ptr->write_position) \
    rb_raise(rb_eRangeError, "Attempted to read %zu bytes, but only %zu bytes remain", (size_t)length, READ_SIZE(buffer_ptr)); }

VALUE rb_bson_byte_buffer_allocate(VALUE klass);
VALUE rb_bson_byte_buffer_initialize(int argc, VALUE *argv, VALUE self);
VALUE rb_bson_byte_buffer_length(VALUE self);
VALUE rb_bson_byte_buffer_get_byte(VALUE self);
VALUE rb_bson_byte_buffer_get_bytes(VALUE self, VALUE i);
VALUE rb_bson_byte_buffer_get_cstring(VALUE self);
VALUE rb_bson_byte_buffer_get_decimal128_bytes(VALUE self);
VALUE rb_bson_byte_buffer_get_double(VALUE self);
VALUE rb_bson_byte_buffer_get_int32(VALUE self);
VALUE rb_bson_byte_buffer_get_uint32(VALUE self);
VALUE rb_bson_byte_buffer_get_int64(VALUE self);
VALUE rb_bson_byte_buffer_get_string(VALUE self);
VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self);
VALUE rb_bson_byte_buffer_get_array(int argc, VALUE *argv, VALUE self);
VALUE rb_bson_byte_buffer_put_byte(VALUE self, VALUE byte);
VALUE rb_bson_byte_buffer_put_bytes(VALUE self, VALUE bytes);
VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE string);
VALUE rb_bson_byte_buffer_put_decimal128(VALUE self, VALUE low, VALUE high);
VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f);
VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i);
VALUE rb_bson_byte_buffer_put_uint32(VALUE self, VALUE i);
VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i);
VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string);
VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol);
VALUE rb_bson_byte_buffer_put_hash(VALUE self, VALUE hash);
VALUE rb_bson_byte_buffer_put_array(VALUE self, VALUE array);
VALUE rb_bson_byte_buffer_read_position(VALUE self);
VALUE rb_bson_byte_buffer_replace_int32(VALUE self, VALUE index, VALUE i);
VALUE rb_bson_byte_buffer_rewind(VALUE self);
VALUE rb_bson_byte_buffer_write_position(VALUE self);
VALUE rb_bson_byte_buffer_to_s(VALUE self);

VALUE rb_bson_object_id_generator_next(int argc, VALUE* args, VALUE self);
VALUE rb_bson_object_id_generator_reset_counter(int argc, VALUE* args, VALUE self);

size_t rb_bson_byte_buffer_memsize(const void *ptr);
void rb_bson_byte_buffer_free(void *ptr);
void rb_bson_expand_buffer(byte_buffer_t* buffer_ptr, size_t length);
void rb_bson_generate_machine_id(VALUE rb_md5_class, char *rb_bson_machine_id);

VALUE pvt_const_get_2(const char *c1, const char *c2);
VALUE pvt_const_get_3(const char *c1, const char *c2, const char *c3);

#define BSON_MODE_DEFAULT       0
#define BSON_MODE_BSON          1

int pvt_get_mode_option(int argc, VALUE *argv);

#define BSON_OBJECT_ID_RANDOM_VALUE_LENGTH  ( 5 )

uint8_t* pvt_get_object_id_random_value();
void pvt_init_rand();
void pvt_rand_buf(uint8_t* bytes, int len, int pid);
int pvt_rand();

/**
 * The counter for incrementing object ids.
 */
extern uint32_t rb_bson_object_id_counter;

extern VALUE rb_bson_registry;

extern const rb_data_type_t rb_byte_buffer_data_type;

extern VALUE _ref_str, _id_str, _db_str;