1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/serialize.c - Base serialization routines */
3 /*
4 * Copyright 1995 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
29 /*
30 * krb5_ser_pack_int32() - Pack a 4-byte integer if space is available.
31 * Update buffer pointer and remaining space.
32 */
33 krb5_error_code KRB5_CALLCONV
krb5_ser_pack_int32(krb5_int32 iarg,krb5_octet ** bufp,size_t * remainp)34 krb5_ser_pack_int32(krb5_int32 iarg, krb5_octet **bufp, size_t *remainp)
35 {
36 if (*remainp >= sizeof(krb5_int32)) {
37 store_32_be(iarg, *bufp);
38 *bufp += sizeof(krb5_int32);
39 *remainp -= sizeof(krb5_int32);
40 return(0);
41 }
42 else
43 return(ENOMEM);
44 }
45
46 /*
47 * krb5_ser_pack_int64() - Pack an 8-byte integer if space is available.
48 * Update buffer pointer and remaining space.
49 */
50 krb5_error_code KRB5_CALLCONV
krb5_ser_pack_int64(int64_t iarg,krb5_octet ** bufp,size_t * remainp)51 krb5_ser_pack_int64(int64_t iarg, krb5_octet **bufp, size_t *remainp)
52 {
53 if (*remainp >= sizeof(int64_t)) {
54 store_64_be(iarg, (unsigned char *)*bufp);
55 *bufp += sizeof(int64_t);
56 *remainp -= sizeof(int64_t);
57 return(0);
58 }
59 else
60 return(ENOMEM);
61 }
62
63 /*
64 * krb5_ser_pack_bytes() - Pack a string of bytes.
65 */
66 krb5_error_code KRB5_CALLCONV
krb5_ser_pack_bytes(krb5_octet * ostring,size_t osize,krb5_octet ** bufp,size_t * remainp)67 krb5_ser_pack_bytes(krb5_octet *ostring, size_t osize, krb5_octet **bufp, size_t *remainp)
68 {
69 if (*remainp >= osize) {
70 memcpy(*bufp, ostring, osize);
71 *bufp += osize;
72 *remainp -= osize;
73 return(0);
74 }
75 else
76 return(ENOMEM);
77 }
78
79 /*
80 * krb5_ser_unpack_int32() - Unpack a 4-byte integer if it's there.
81 */
82 krb5_error_code KRB5_CALLCONV
krb5_ser_unpack_int32(krb5_int32 * intp,krb5_octet ** bufp,size_t * remainp)83 krb5_ser_unpack_int32(krb5_int32 *intp, krb5_octet **bufp, size_t *remainp)
84 {
85 if (*remainp >= sizeof(krb5_int32)) {
86 *intp = load_32_be(*bufp);
87 *bufp += sizeof(krb5_int32);
88 *remainp -= sizeof(krb5_int32);
89 return(0);
90 }
91 else
92 return(ENOMEM);
93 }
94
95 /*
96 * krb5_ser_unpack_int64() - Unpack an 8-byte integer if it's there.
97 */
98 krb5_error_code KRB5_CALLCONV
krb5_ser_unpack_int64(int64_t * intp,krb5_octet ** bufp,size_t * remainp)99 krb5_ser_unpack_int64(int64_t *intp, krb5_octet **bufp, size_t *remainp)
100 {
101 if (*remainp >= sizeof(int64_t)) {
102 *intp = load_64_be((unsigned char *)*bufp);
103 *bufp += sizeof(int64_t);
104 *remainp -= sizeof(int64_t);
105 return(0);
106 }
107 else
108 return(ENOMEM);
109 }
110
111 /*
112 * krb5_ser_unpack_bytes() - Unpack a byte string if it's there.
113 */
114 krb5_error_code KRB5_CALLCONV
krb5_ser_unpack_bytes(krb5_octet * istring,size_t isize,krb5_octet ** bufp,size_t * remainp)115 krb5_ser_unpack_bytes(krb5_octet *istring, size_t isize, krb5_octet **bufp, size_t *remainp)
116 {
117 if (*remainp >= isize) {
118 memcpy(istring, *bufp, isize);
119 *bufp += isize;
120 *remainp -= isize;
121 return(0);
122 }
123 else
124 return(ENOMEM);
125 }
126