| 1 | /* |
| 2 | * particle_string.c |
| 3 | * |
| 4 | * Copyright (C) 2015 Aerospike, Inc. |
| 5 | * |
| 6 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 7 | * license agreements. |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify it under |
| 10 | * the terms of the GNU Affero General Public License as published by the Free |
| 11 | * Software Foundation, either version 3 of the License, or (at your option) any |
| 12 | * later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 17 | * details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include <stddef.h> |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #include "aerospike/as_string.h" |
| 28 | #include "aerospike/as_val.h" |
| 29 | |
| 30 | #include "fault.h" |
| 31 | |
| 32 | #include "base/datamodel.h" |
| 33 | #include "base/particle.h" |
| 34 | #include "base/particle_blob.h" |
| 35 | |
| 36 | |
| 37 | //========================================================== |
| 38 | // STRING particle interface - function declarations. |
| 39 | // |
| 40 | |
| 41 | // Most STRING particle table functions just use the equivalent BLOB particle |
| 42 | // functions. Here are the differences... |
| 43 | |
| 44 | // Handle as_val translation. |
| 45 | uint32_t string_size_from_asval(const as_val *val); |
| 46 | void string_from_asval(const as_val *val, as_particle **pp); |
| 47 | as_val *string_to_asval(const as_particle *p); |
| 48 | uint32_t string_asval_wire_size(const as_val *val); |
| 49 | uint32_t string_asval_to_wire(const as_val *val, uint8_t *wire); |
| 50 | |
| 51 | |
| 52 | //========================================================== |
| 53 | // STRING particle interface - vtable. |
| 54 | // |
| 55 | |
| 56 | const as_particle_vtable string_vtable = { |
| 57 | blob_destruct, |
| 58 | blob_size, |
| 59 | |
| 60 | blob_concat_size_from_wire, |
| 61 | blob_append_from_wire, |
| 62 | blob_prepend_from_wire, |
| 63 | blob_incr_from_wire, |
| 64 | blob_size_from_wire, |
| 65 | blob_from_wire, |
| 66 | blob_compare_from_wire, |
| 67 | blob_wire_size, |
| 68 | blob_to_wire, |
| 69 | |
| 70 | string_size_from_asval, |
| 71 | string_from_asval, |
| 72 | string_to_asval, |
| 73 | string_asval_wire_size, |
| 74 | string_asval_to_wire, |
| 75 | |
| 76 | blob_size_from_msgpack, |
| 77 | blob_from_msgpack, |
| 78 | |
| 79 | blob_skip_flat, |
| 80 | blob_cast_from_flat, |
| 81 | blob_from_flat, |
| 82 | blob_flat_size, |
| 83 | blob_to_flat |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | //========================================================== |
| 88 | // Typedefs & constants. |
| 89 | // |
| 90 | |
| 91 | // Same as related BLOB struct. TODO - just expose BLOB structs? |
| 92 | |
| 93 | typedef struct string_mem_s { |
| 94 | uint8_t type; |
| 95 | uint32_t sz; |
| 96 | uint8_t data[]; |
| 97 | } __attribute__ ((__packed__)) string_mem; |
| 98 | |
| 99 | |
| 100 | //========================================================== |
| 101 | // STRING particle interface - function definitions. |
| 102 | // |
| 103 | |
| 104 | // Most STRING particle table functions just use the equivalent BLOB particle |
| 105 | // functions. Here are the differences... |
| 106 | |
| 107 | //------------------------------------------------ |
| 108 | // Handle as_val translation. |
| 109 | // |
| 110 | |
| 111 | uint32_t |
| 112 | string_size_from_asval(const as_val *val) |
| 113 | { |
| 114 | return (uint32_t)(sizeof(string_mem) + as_string_len(as_string_fromval(val))); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | string_from_asval(const as_val *val, as_particle **pp) |
| 119 | { |
| 120 | string_mem *p_string_mem = (string_mem *)*pp; |
| 121 | |
| 122 | as_string *string = as_string_fromval(val); |
| 123 | |
| 124 | p_string_mem->type = AS_PARTICLE_TYPE_STRING; |
| 125 | p_string_mem->sz = (uint32_t)as_string_len(string); |
| 126 | memcpy(p_string_mem->data, as_string_tostring(string), p_string_mem->sz); |
| 127 | } |
| 128 | |
| 129 | as_val * |
| 130 | string_to_asval(const as_particle *p) |
| 131 | { |
| 132 | string_mem *p_string_mem = (string_mem *)p; |
| 133 | |
| 134 | uint8_t *value = cf_malloc(p_string_mem->sz + 1); |
| 135 | |
| 136 | memcpy(value, p_string_mem->data, p_string_mem->sz); |
| 137 | value[p_string_mem->sz] = 0; |
| 138 | |
| 139 | return (as_val *)as_string_new_wlen((char *)value, p_string_mem->sz, true); |
| 140 | } |
| 141 | |
| 142 | uint32_t |
| 143 | string_asval_wire_size(const as_val *val) |
| 144 | { |
| 145 | return as_string_len(as_string_fromval(val)); |
| 146 | } |
| 147 | |
| 148 | uint32_t |
| 149 | string_asval_to_wire(const as_val *val, uint8_t *wire) |
| 150 | { |
| 151 | as_string *string = as_string_fromval(val); |
| 152 | uint32_t size = (uint32_t)as_string_len(string); |
| 153 | |
| 154 | memcpy(wire, as_string_tostring(string), size); |
| 155 | |
| 156 | return size; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | //========================================================== |
| 161 | // as_bin particle functions specific to STRING. |
| 162 | // |
| 163 | |
| 164 | uint32_t |
| 165 | as_bin_particle_string_ptr(const as_bin *b, char **p_value) |
| 166 | { |
| 167 | // Caller must ensure this is called only for STRING particles. |
| 168 | string_mem *p_string_mem = (string_mem *)b->particle; |
| 169 | |
| 170 | *p_value = (char *)p_string_mem->data; |
| 171 | |
| 172 | return p_string_mem->sz; |
| 173 | } |
| 174 | |