Fix autoconf 2.70 compatibility
[platform/upstream/krb5.git] / src / lib / krb5 / krb / ser_cksum.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/ser_cksum.c - Serialize krb5_checksum structure */
3 /*
4  * Copyright 1995, 2008 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
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.
11  *
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.
25  */
26
27 #include "k5-int.h"
28 #include "int-proto.h"
29
30 /*
31  * Routines to deal with externalizing the krb5_checksum:
32  *      krb5_checksum_esize();
33  *      krb5_checksum_externalize();
34  *      krb5_checksum_internalize();
35  */
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 *);
42
43 /* Local data */
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  */
49 };
50
51 /*
52  * krb5_checksum_esize()        - Determine the size required to externalize
53  *                                the krb5_checksum.
54  */
55 static krb5_error_code
56 krb5_checksum_esize(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
57 {
58     krb5_error_code     kret;
59     krb5_checksum       *checksum;
60
61     /*
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
68      */
69     kret = EINVAL;
70     if ((checksum = (krb5_checksum *) arg)) {
71         *sizep += (sizeof(krb5_int32) +
72                    sizeof(krb5_int32) +
73                    sizeof(krb5_int32) +
74                    sizeof(krb5_int32) +
75                    (size_t) checksum->length);
76         kret = 0;
77     }
78     return(kret);
79 }
80
81 /*
82  * krb5_checksum_externalize()  - Externalize the krb5_checksum.
83  */
84 static krb5_error_code
85 krb5_checksum_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
86 {
87     krb5_error_code     kret;
88     krb5_checksum       *checksum;
89     size_t              required;
90     krb5_octet          *bp;
91     size_t              remain;
92
93     required = 0;
94     bp = *buffer;
95     remain = *lenremain;
96     kret = EINVAL;
97     if ((checksum = (krb5_checksum *) arg)) {
98         kret = ENOMEM;
99         if (!krb5_checksum_esize(kcontext, arg, &required) &&
100             (required <= remain)) {
101             /* Our identifier */
102             (void) krb5_ser_pack_int32(KV5M_CHECKSUM, &bp, &remain);
103
104             /* Our checksum_type */
105             (void) krb5_ser_pack_int32((krb5_int32) checksum->checksum_type,
106                                        &bp, &remain);
107
108             /* Our length */
109             (void) krb5_ser_pack_int32((krb5_int32) checksum->length,
110                                        &bp, &remain);
111
112             /* Our contents */
113             (void) krb5_ser_pack_bytes(checksum->contents,
114                                        (size_t) checksum->length,
115                                        &bp, &remain);
116
117             /* Finally, our trailer */
118             (void) krb5_ser_pack_int32(KV5M_CHECKSUM, &bp, &remain);
119
120             kret = 0;
121             *buffer = bp;
122             *lenremain = remain;
123         }
124     }
125     return(kret);
126 }
127
128 /*
129  * krb5_checksum_internalize()  - Internalize the krb5_checksum.
130  */
131 static krb5_error_code
132 krb5_checksum_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
133 {
134     krb5_error_code     kret;
135     krb5_checksum       *checksum;
136     krb5_int32          ibuf;
137     krb5_octet          *bp;
138     size_t              remain;
139
140     bp = *buffer;
141     remain = *lenremain;
142     kret = EINVAL;
143     /* Read our magic number */
144     if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
145         ibuf = 0;
146     if (ibuf == KV5M_CHECKSUM) {
147         kret = ENOMEM;
148
149         /* Get a 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;
155
156             /* Get the length */
157             (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
158             checksum->length = (int) ibuf;
159
160             /* Get the string */
161             if (!ibuf ||
162                 ((checksum->contents = (krb5_octet *)
163                   malloc((size_t) (ibuf))) &&
164                  !(kret = krb5_ser_unpack_bytes(checksum->contents,
165                                                 (size_t) ibuf,
166                                                 &bp, &remain)))) {
167
168                 /* Get the trailer */
169                 kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
170                 if (!kret && (ibuf == KV5M_CHECKSUM)) {
171                     checksum->magic = KV5M_CHECKSUM;
172                     *buffer = bp;
173                     *lenremain = remain;
174                     *argp = (krb5_pointer) checksum;
175                 }
176                 else
177                     kret = EINVAL;
178             }
179             if (kret) {
180                 if (checksum->contents)
181                     free(checksum->contents);
182                 free(checksum);
183             }
184         }
185     }
186     return(kret);
187 }
188
189 /*
190  * Register the checksum serializer.
191  */
192 krb5_error_code
193 krb5_ser_checksum_init(krb5_context kcontext)
194 {
195     return(krb5_register_serializer(kcontext, &krb5_checksum_ser_entry));
196 }