1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/ser_cksum.c - Serialize krb5_checksum structure */
4 * Copyright 1995, 2008 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
28 #include "int-proto.h"
31 * Routines to deal with externalizing the krb5_checksum:
32 * krb5_checksum_esize();
33 * krb5_checksum_externalize();
34 * krb5_checksum_internalize();
36 static krb5_error_code krb5_checksum_esize
37 (krb5_context, krb5_pointer, size_t *);
38 static krb5_error_code krb5_checksum_externalize
39 (krb5_context, krb5_pointer, krb5_octet **, size_t *);
40 static krb5_error_code krb5_checksum_internalize
41 (krb5_context,krb5_pointer *, krb5_octet **, size_t *);
44 static const krb5_ser_entry krb5_checksum_ser_entry = {
45 KV5M_CHECKSUM, /* Type */
46 krb5_checksum_esize, /* Sizer routine */
47 krb5_checksum_externalize, /* Externalize routine */
48 krb5_checksum_internalize /* Internalize routine */
52 * krb5_checksum_esize() - Determine the size required to externalize
55 static krb5_error_code
56 krb5_checksum_esize(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
59 krb5_checksum *checksum;
62 * krb5_checksum requires:
63 * krb5_int32 for KV5M_CHECKSUM
64 * krb5_int32 for checksum_type
65 * krb5_int32 for length
66 * krb5_int32 for KV5M_CHECKSUM
67 * checksum->length for contents
70 if ((checksum = (krb5_checksum *) arg)) {
71 *sizep += (sizeof(krb5_int32) +
75 (size_t) checksum->length);
82 * krb5_checksum_externalize() - Externalize the krb5_checksum.
84 static krb5_error_code
85 krb5_checksum_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
88 krb5_checksum *checksum;
97 if ((checksum = (krb5_checksum *) arg)) {
99 if (!krb5_checksum_esize(kcontext, arg, &required) &&
100 (required <= remain)) {
102 (void) krb5_ser_pack_int32(KV5M_CHECKSUM, &bp, &remain);
104 /* Our checksum_type */
105 (void) krb5_ser_pack_int32((krb5_int32) checksum->checksum_type,
109 (void) krb5_ser_pack_int32((krb5_int32) checksum->length,
113 (void) krb5_ser_pack_bytes(checksum->contents,
114 (size_t) checksum->length,
117 /* Finally, our trailer */
118 (void) krb5_ser_pack_int32(KV5M_CHECKSUM, &bp, &remain);
129 * krb5_checksum_internalize() - Internalize the krb5_checksum.
131 static krb5_error_code
132 krb5_checksum_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
134 krb5_error_code kret;
135 krb5_checksum *checksum;
143 /* Read our magic number */
144 if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
146 if (ibuf == KV5M_CHECKSUM) {
150 if ((remain >= (2*sizeof(krb5_int32))) &&
151 (checksum = (krb5_checksum *) calloc(1, sizeof(krb5_checksum)))) {
152 /* Get the checksum_type */
153 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
154 checksum->checksum_type = (krb5_cksumtype) ibuf;
157 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
158 checksum->length = (int) ibuf;
162 ((checksum->contents = (krb5_octet *)
163 malloc((size_t) (ibuf))) &&
164 !(kret = krb5_ser_unpack_bytes(checksum->contents,
168 /* Get the trailer */
169 kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
170 if (!kret && (ibuf == KV5M_CHECKSUM)) {
171 checksum->magic = KV5M_CHECKSUM;
174 *argp = (krb5_pointer) checksum;
180 if (checksum->contents)
181 free(checksum->contents);
190 * Register the checksum serializer.
193 krb5_ser_checksum_init(krb5_context kcontext)
195 return(krb5_register_serializer(kcontext, &krb5_checksum_ser_entry));