Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-only */
2 : /*
3 : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #include "core/response.h"
7 :
8 : #include <errno.h>
9 : #include <stddef.h>
10 : #include <stdio.h>
11 : #include <stdlib.h>
12 :
13 : #include "core/helper.h"
14 :
15 2 : int bf_response_new_raw(struct bf_response **response, size_t data_len)
16 : {
17 2 : bf_assert(response);
18 :
19 2 : *response = malloc(sizeof(**response) + data_len);
20 2 : if (!*response)
21 : return -ENOMEM;
22 :
23 2 : (*response)->type = BF_RES_SUCCESS;
24 :
25 2 : return 0;
26 : }
27 :
28 0 : int bf_response_new_success(struct bf_response **response, const char *data,
29 : size_t data_len)
30 : {
31 0 : _cleanup_bf_response_ struct bf_response *_response = NULL;
32 :
33 0 : bf_assert(response);
34 0 : bf_assert(!(!!data ^ !!data_len));
35 :
36 0 : _response = calloc(1, sizeof(*_response) + data_len);
37 0 : if (!_response)
38 : return -ENOMEM;
39 :
40 0 : _response->type = BF_RES_SUCCESS;
41 0 : _response->data_len = data_len;
42 0 : bf_memcpy(_response->data, data, data_len);
43 :
44 0 : *response = TAKE_PTR(_response);
45 :
46 0 : return 0;
47 : }
48 :
49 0 : int bf_response_new_failure(struct bf_response **response, int error)
50 : {
51 0 : _cleanup_bf_response_ struct bf_response *_response = NULL;
52 :
53 0 : bf_assert(response);
54 :
55 0 : _response = calloc(1, sizeof(*_response));
56 0 : if (!_response)
57 : return -ENOMEM;
58 :
59 0 : _response->type = BF_RES_FAILURE;
60 0 : _response->error = error;
61 :
62 0 : *response = TAKE_PTR(_response);
63 :
64 0 : return 0;
65 : }
66 :
67 4 : void bf_response_free(struct bf_response **response)
68 : {
69 4 : free(*response);
70 4 : *response = NULL;
71 4 : }
72 :
73 0 : int bf_response_copy(struct bf_response **dest, const struct bf_response *src)
74 : {
75 0 : _cleanup_bf_response_ struct bf_response *_response = NULL;
76 :
77 0 : bf_assert(dest);
78 0 : bf_assert(src);
79 :
80 0 : _response = bf_memdup(src, bf_response_size(src));
81 0 : if (!_response)
82 : return -ENOMEM;
83 :
84 0 : *dest = TAKE_PTR(_response);
85 :
86 0 : return 0;
87 : }
|