move repository from m6w6 to awesomized
[m6w6/libmemcached] / src / libmemcached / key.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17
18 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool) {
19 if (key_length == 0) {
20 return MEMCACHED_BAD_KEY_PROVIDED;
21 }
22
23 // No one ever reimplemented MEMCACHED to use keys longer then the original ascii length
24 #if 0
25 if (binary)
26 {
27 if (key_length > 0xffff)
28 {
29 return MEMCACHED_BAD_KEY_PROVIDED;
30 }
31 }
32 else
33 #endif
34 {
35 if (key_length >= MEMCACHED_MAX_KEY) {
36 return MEMCACHED_BAD_KEY_PROVIDED;
37 }
38 }
39
40 return MEMCACHED_SUCCESS;
41 }
42
43 memcached_return_t memcached_key_test(memcached_st &memc, const char *const *keys,
44 const size_t *key_length, size_t number_of_keys) {
45 if (number_of_keys == 0) {
46 return memcached_set_error(memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
47 memcached_literal_param("Numbers of keys provided was zero"));
48 }
49
50 if (keys == NULL or key_length == NULL) {
51 return memcached_set_error(memc, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT,
52 memcached_literal_param("Key was NULL or length of key was zero."));
53 }
54
55 const bool is_binary = memcached_flag(memc, MEMCACHED_FLAG_BINARY_PROTOCOL);
56
57 // If we don't need to verify the key, or we are using the binary protoocol,
58 // we just check the size of the key
59 for (size_t x = 0; x < number_of_keys; ++x) {
60 // We should set binary key, but the memcached server is broken for
61 // longer keys at the moment.
62 memcached_return_t rc =
63 memcached_validate_key_length(*(key_length + x), false /* memc.flags.binary_protocol */);
64 if (memcached_failed(rc)) {
65 return memcached_set_error(memc, rc, MEMCACHED_AT,
66 memcached_literal_param("Key provided was too long."));
67 }
68
69 if (memc.flags.verify_key and is_binary == false) {
70 for (size_t y = 0; y < *(key_length + x); ++y) {
71 if ((isgraph(keys[x][y])) == 0) {
72 return memcached_set_error(
73 memc, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT,
74 memcached_literal_param("Key provided had invalid character."));
75 }
76 }
77 }
78 }
79
80 return MEMCACHED_SUCCESS;
81 }