nmsg  0.9.0
payload.c
1 /*
2  * Copyright (c) 2008-2013 by Farsight Security, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* Import. */
18 
19 #include "private.h"
20 
21 /* Export. */
22 
23 void
24 _nmsg_payload_free_all(Nmsg__Nmsg *nc) {
25  unsigned i;
26 
27  for (i = 0; i < nc->n_payloads; i++) {
28  nmsg__nmsg_payload__free_unpacked(nc->payloads[i], NULL);
29  nc->payloads[i] = NULL;
30  }
31  nc->n_payloads = 0;
32 }
33 
34 void
35 _nmsg_payload_calc_crcs(Nmsg__Nmsg *nc) {
36  unsigned i;
37 
38  if (nc->payload_crcs != NULL)
39  free(nc->payload_crcs);
40 
41  nc->payload_crcs = malloc(nc->n_payloads * sizeof(*(nc->payload_crcs)));
42  assert(nc->payload_crcs != NULL);
43 
44  for (i = 0; i < nc->n_payloads; i++)
45  nc->payload_crcs[i] = htonl(my_crc32c(nc->payloads[i]->payload.data,
46  nc->payloads[i]->payload.len));
47 
48  nc->n_payload_crcs = nc->n_payloads;
49 }
50 
51 void
52 _nmsg_payload_free(Nmsg__NmsgPayload **np) {
53  nmsg__nmsg_payload__free_unpacked(*np, NULL);
54  *np = NULL;
55 }
56 
57 size_t
58 _nmsg_payload_size(const Nmsg__NmsgPayload *np) {
59  size_t sz;
60  Nmsg__NmsgPayload copy;
61 
62  copy = *np;
63  copy.payload.len = 0;
64  copy.payload.data = NULL;
65  sz = nmsg__nmsg_payload__get_packed_size(&copy);
66 
67  sz += np->payload.len;
68 
69  /* varint encoded length */
70  if (np->payload.len >= (1 << 7))
71  sz += 1;
72  if (np->payload.len >= (1 << 14))
73  sz += 1;
74 
75  return (sz);
76 }