File: test_kms_util.c

package info (click to toggle)
libmongocrypt 1.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,572 kB
  • sloc: ansic: 70,067; python: 4,547; cpp: 615; sh: 460; makefile: 44; awk: 8
file content (92 lines) | stat: -rw-r--r-- 2,447 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright 2021-present 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 "test_kms_util.h"

#include "test_kms_assert.h"
#include "hexlify.h"

#include <ctype.h> /* tolower */

char *
copy_and_filter_hex (const char *unfiltered_hex)
{
   size_t i, j;

   char *filtered = malloc (strlen (unfiltered_hex) + 1);
   j = 0;
   for (i = 0; i < strlen (unfiltered_hex); i++) {
      if (unfiltered_hex[i] != ' ' && unfiltered_hex[i] != '|') {
         filtered[j] = (char) tolower (unfiltered_hex[i]);
         j++;
      }
   }
   filtered[j] = '\0';
   return filtered;
}

uint8_t *
hex_to_data (const char *unfiltered_hex, size_t *outlen)
{
   char *filtered_hex;
   uint8_t *bytes;
   size_t i;

   filtered_hex = copy_and_filter_hex (unfiltered_hex);
   *outlen = strlen (filtered_hex) / 2;
   bytes = malloc (*outlen);
   for (i = 0; i < *outlen; i++) {
      bytes[i] = unhexlify (filtered_hex + (i * 2), 2);
   }

   free (filtered_hex);
   return bytes;
}

char *
data_to_hex (const uint8_t *buf, size_t len) {
   return hexlify (buf, len);
}

char *
replace_all (const char *input, const char *match, const char *replacement)
{
   ASSERT (input);
   ASSERT (match);
   ASSERT (replacement);
   kms_request_str_t *replaced = kms_request_str_new ();
   const char *start = input;
   const char *iter = strstr (input, match);
   while (iter != NULL) {
      kms_request_str_append_chars (replaced, start, (ssize_t) (iter - start));
      kms_request_str_append_chars (
         replaced, replacement, (ssize_t) strlen (replacement));
      iter += strlen (match);
      start = iter;
      iter = strstr (iter, match);
   }
   // Append the remainder of input.
   kms_request_str_append_chars (replaced, start, -1);
   return kms_request_str_detach (replaced);
}

void
test_kms_util (void)
{
   char *got = replace_all ("foo bar baz", "bar", "baz");
   ASSERT_CMPSTR (got, "foo baz baz");
   free (got);
}