Windows global stuff:
[krb5.git] / src / lib / krb5 / asn.1 / asn1_make.c
1 /*
2  * src/lib/krb5/asn.1/asn1_make.c
3  * 
4  * Copyright 1994 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.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  */
23
24 #include "asn1_make.h"
25
26 asn1_error_code asn1_make_etag(buf, class, tagnum, in_len, retlen)
27      asn1buf * buf;
28      const asn1_class class;
29      const asn1_tagnum tagnum;
30      const int in_len;
31      int * retlen;
32 {
33   return asn1_make_tag(buf,class,CONSTRUCTED,tagnum,in_len,retlen);
34 }
35
36 asn1_error_code asn1_make_tag(buf, class, construction, tagnum, in_len, retlen)
37      asn1buf * buf;
38      const asn1_class class;
39      const asn1_construction construction;
40      const asn1_tagnum tagnum;
41      const int in_len;
42      int * retlen;
43 {
44   asn1_error_code retval;
45   int sumlen=0, length;
46
47   if(tagnum > ASN1_TAGNUM_MAX) return ASN1_OVERFLOW;
48
49   retval = asn1_make_length(buf,in_len, &length);
50   if(retval) return retval;
51   sumlen += length;
52   retval = asn1_make_id(buf,class,construction,tagnum,&length);
53   if(retval) return retval;
54   sumlen += length;
55
56   *retlen = sumlen;
57   return 0;
58 }
59
60 asn1_error_code asn1_make_length(buf, in_len, retlen)
61      asn1buf * buf;
62      const int in_len;
63      int * retlen;
64 {
65   asn1_error_code retval;
66
67   if(in_len < 128){
68     retval = asn1buf_insert_octet(buf, (asn1_octet)(in_len&0x7F));
69     if(retval) return retval;
70     *retlen = 1;
71   }else{
72     int in_copy=in_len, length=0;
73
74     while(in_copy != 0){
75       retval = asn1buf_insert_octet(buf, (asn1_octet)(in_copy&0xFF));
76       if(retval) return retval;
77       in_copy = in_copy >> 8;
78       length++;
79     }
80     retval = asn1buf_insert_octet(buf, (asn1_octet) (0x80 | (asn1_octet)(length&0x7F)));
81     if(retval) return retval;
82     length++;
83     *retlen = length;
84   }
85
86   return 0;
87 }
88
89 asn1_error_code asn1_make_id(buf, class, construction, tagnum, retlen)
90      asn1buf * buf;
91      const asn1_class class;
92      const asn1_construction construction;
93      const asn1_tagnum tagnum;
94      int * retlen;
95 {
96   asn1_error_code retval;
97
98   if(tagnum < 31) {
99     retval = asn1buf_insert_octet(buf, (asn1_octet) (class | construction |
100                                        (asn1_octet)tagnum));
101     if(retval) return retval;
102     *retlen = 1;
103   }else{
104     asn1_tagnum tagcopy = tagnum;
105     int length = 0;
106
107     retval = asn1buf_insert_octet(buf, (asn1_octet)(tagcopy&0x7F));
108     if(retval) return retval;
109     tagcopy >>= 7;
110     length++;
111
112     for(; tagcopy != 0; tagcopy >>= 7){
113       retval = asn1buf_insert_octet(buf, (asn1_octet) (0x80 | (asn1_octet)(tagcopy&0x7F)));
114       if(retval) return retval;
115       length++;
116     }
117
118     retval = asn1buf_insert_octet(buf, (asn1_octet) (class | construction | 0x1F));
119     if(retval) return retval;
120     length++;
121     *retlen = length;
122   }
123
124   return 0;
125 }
126
127 asn1_error_code asn1_make_sequence(buf, seq_len, retlen)
128      asn1buf * buf;
129      const int seq_len;
130      int * retlen;
131 {
132   asn1_error_code retval;
133   int len, sum=0;
134
135   retval = asn1_make_length(buf,seq_len,&len);
136   if(retval) return retval;
137   sum += len;
138   retval = asn1_make_id(buf,UNIVERSAL,CONSTRUCTED,ASN1_SEQUENCE,&len);
139   if(retval) return retval;
140   sum += len;
141
142   *retlen = sum;
143   return 0;
144 }
145
146 asn1_error_code asn1_make_set(buf, set_len, retlen)
147      asn1buf * buf;
148      const int set_len;
149      int * retlen;
150 {
151   asn1_error_code retval;
152   int len, sum=0;
153
154   retval = asn1_make_length(buf,set_len,&len);
155   if(retval) return retval;
156   sum += len;
157   retval = asn1_make_id(buf,UNIVERSAL,CONSTRUCTED,ASN1_SET,&len);
158   if(retval) return retval;
159   sum += len;
160
161   *retlen = sum;
162   return 0;
163 }
164
165 asn1_error_code asn1_make_string(buf, length, string, retlen)
166      asn1buf * buf;
167      const int length;
168      const char * string;
169      int * retlen;
170 {
171   asn1_error_code retval;
172
173   retval = asn1buf_insert_charstring(buf,length,string);
174   if(retval) return retval;
175
176   *retlen = length;
177   return 0;
178 }