blob: 286233e639c00c4a8c166060398058111116f05d [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin523de9e2006-08-18 01:34:18 +00002 * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
6/*
Lev Walkin41635d32006-03-18 05:06:57 +00007 * Read the NativeInteger.h for the explanation wrt. differences between
Lev Walkinf15320b2004-06-03 03:38:44 +00008 * INTEGER and NativeInteger.
9 * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
10 * implementation deals with the standard (machine-specific) representation
11 * of them instead of using the platform-independent buffer.
12 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000013#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000014#include <NativeInteger.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * NativeInteger basic type description.
18 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070019static const ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000020 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
21};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080022asn_TYPE_operation_t asn_OP_NativeInteger = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000023 NativeInteger_free,
24 NativeInteger_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070025 NativeInteger_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000026 NativeInteger_decode_ber,
27 NativeInteger_encode_der,
Lev Walkin435cb282004-10-21 14:13:48 +000028 NativeInteger_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000029 NativeInteger_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070030#ifdef ASN_DISABLE_OER_SUPPORT
31 0,
32 0,
33#else
Lev Walkin9ae018e2017-07-24 01:45:57 +040034 NativeInteger_decode_oer, /* OER decoder */
35 NativeInteger_encode_oer, /* Canonical OER encoder */
Lev Walkincc159472017-07-06 08:26:36 -070036#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040037#ifdef ASN_DISABLE_PER_SUPPORT
38 0,
39 0,
40#else
41 NativeInteger_decode_uper, /* Unaligned PER decoder */
42 NativeInteger_encode_uper, /* Unaligned PER encoder */
43#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080044 0 /* Use generic outmost tag fetcher */
45};
46asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
47 "INTEGER", /* The ASN.1 type is still INTEGER */
48 "INTEGER",
49 &asn_OP_NativeInteger,
50 asn_generic_no_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000051 asn_DEF_NativeInteger_tags,
52 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
53 asn_DEF_NativeInteger_tags, /* Same as above */
54 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070055 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000056 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000057 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000058 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000059};
60
61/*
62 * Decode INTEGER type.
63 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000064asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070065NativeInteger_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin5e033762004-09-29 13:26:15 +000066 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000067 void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkind62a6622017-08-22 02:31:02 -070068 const asn_INTEGER_specifics_t *specs =
69 (const asn_INTEGER_specifics_t *)td->specifics;
70 long *native = (long *)*nint_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000071 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000072 ber_tlv_len_t length;
73
74 /*
75 * If the structure is not there, allocate it.
76 */
Lev Walkine0b56e02005-02-25 12:10:27 +000077 if(native == NULL) {
78 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
79 if(native == NULL) {
Lev Walkinf15320b2004-06-03 03:38:44 +000080 rval.code = RC_FAIL;
81 rval.consumed = 0;
82 return rval;
83 }
84 }
85
86 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
87 td->name, tag_mode);
88
89 /*
90 * Check tags.
91 */
Lev Walkin5e033762004-09-29 13:26:15 +000092 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
93 tag_mode, 0, &length, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000094 if(rval.code != RC_OK)
95 return rval;
96
97 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
98
99 /*
100 * Make sure we have this length.
101 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000102 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +0000104 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 rval.code = RC_WMORE;
106 rval.consumed = 0;
107 return rval;
108 }
109
110 /*
111 * ASN.1 encoded INTEGER: buf_ptr, length
Lev Walkine0b56e02005-02-25 12:10:27 +0000112 * Fill the native, at the same time checking for overflow.
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 * If overflow occured, return with RC_FAIL.
114 */
115 {
116 INTEGER_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +0000117 union {
118 const void *constbuf;
119 void *nonconstbuf;
120 } unconst_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 long l;
Lev Walkin7e033b52005-07-02 08:19:17 +0000122
123 unconst_buf.constbuf = buf_ptr;
124 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 tmp.size = length;
126
Lev Walkin8bb57a22007-12-03 13:41:36 +0000127 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700128 ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000129 : asn_INTEGER2long(&tmp, &l)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 rval.code = RC_FAIL;
131 rval.consumed = 0;
132 return rval;
133 }
134
Lev Walkine0b56e02005-02-25 12:10:27 +0000135 *native = l;
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 }
137
138 rval.code = RC_OK;
139 rval.consumed += length;
140
Lev Walkine0b56e02005-02-25 12:10:27 +0000141 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
142 (long)rval.consumed, (long)length, td->name, (long)*native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000143
144 return rval;
145}
146
147/*
148 * Encode the NativeInteger using the standard INTEGER type DER encoder.
149 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000150asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000151NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 int tag_mode, ber_tlv_tag_t tag,
153 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000154 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000155 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 INTEGER_t tmp;
157
158#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
159
Lev Walkine0b56e02005-02-25 12:10:27 +0000160 tmp.buf = (uint8_t *)&native;
161 tmp.size = sizeof(native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000162
163#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
Lev Walkine0b56e02005-02-25 12:10:27 +0000164 uint8_t buf[sizeof(native)];
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 uint8_t *p;
166
Lev Walkind54f2552004-10-27 13:24:51 +0000167 /* Prepare a fake INTEGER */
Lev Walkine0b56e02005-02-25 12:10:27 +0000168 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
Lev Walkinf3c089e2007-11-13 23:53:13 +0000169 *p = (uint8_t)native;
Lev Walkinf15320b2004-06-03 03:38:44 +0000170
171 tmp.buf = buf;
172 tmp.size = sizeof(buf);
173#endif /* WORDS_BIGENDIAN */
174
175 /* Encode fake INTEGER */
176 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
177 if(erval.encoded == -1) {
178 assert(erval.structure_ptr == &tmp);
179 erval.structure_ptr = ptr;
180 }
181 return erval;
182}
183
Lev Walkin435cb282004-10-21 14:13:48 +0000184/*
185 * Decode the chunk of XML text encoding INTEGER.
186 */
187asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700188NativeInteger_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin435cb282004-10-21 14:13:48 +0000189 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000190 const void *buf_ptr, size_t size) {
Lev Walkind62a6622017-08-22 02:31:02 -0700191 const asn_INTEGER_specifics_t *specs =
192 (const asn_INTEGER_specifics_t *)td->specifics;
193 asn_dec_rval_t rval;
Lev Walkin59b176e2005-11-26 11:25:14 +0000194 INTEGER_t st;
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000195 void *st_ptr = (void *)&st;
Lev Walkine0b56e02005-02-25 12:10:27 +0000196 long *native = (long *)*sptr;
Lev Walkin435cb282004-10-21 14:13:48 +0000197
Lev Walkine0b56e02005-02-25 12:10:27 +0000198 if(!native) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000199 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700200 if(!native) ASN__DECODE_FAILED;
Lev Walkin435cb282004-10-21 14:13:48 +0000201 }
202
Lev Walkin59b176e2005-11-26 11:25:14 +0000203 memset(&st, 0, sizeof(st));
204 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000205 opt_mname, buf_ptr, size);
Lev Walkin435cb282004-10-21 14:13:48 +0000206 if(rval.code == RC_OK) {
207 long l;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000208 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700209 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000210 : asn_INTEGER2long(&st, &l)) {
Lev Walkin435cb282004-10-21 14:13:48 +0000211 rval.code = RC_FAIL;
212 rval.consumed = 0;
213 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000214 *native = l;
Lev Walkin435cb282004-10-21 14:13:48 +0000215 }
216 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000217 /*
218 * Cannot restart from the middle;
219 * there is no place to save state in the native type.
220 * Request a continuation from the very beginning.
221 */
Lev Walkin435cb282004-10-21 14:13:48 +0000222 rval.consumed = 0;
223 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000224 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
Lev Walkin435cb282004-10-21 14:13:48 +0000225 return rval;
226}
227
228
Lev Walkina9cc46e2004-09-22 16:06:28 +0000229asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000230NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000231 int ilevel, enum xer_encoder_flags_e flags,
232 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind62a6622017-08-22 02:31:02 -0700233 const asn_INTEGER_specifics_t *specs =
234 (const asn_INTEGER_specifics_t *)td->specifics;
235 char scratch[32]; /* Enough for 64-bit int */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000236 asn_enc_rval_t er;
Lev Walkine0b56e02005-02-25 12:10:27 +0000237 const long *native = (const long *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000238
239 (void)ilevel;
240 (void)flags;
241
Lev Walkin7c1dc052016-03-14 03:08:15 -0700242 if(!native) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000243
Lev Walkin8bb57a22007-12-03 13:41:36 +0000244 er.encoded = snprintf(scratch, sizeof(scratch),
245 (specs && specs->field_unsigned)
246 ? "%lu" : "%ld", *native);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000247 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
248 || cb(scratch, er.encoded, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700249 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000250
Lev Walkin7c1dc052016-03-14 03:08:15 -0700251 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000252}
253
Lev Walkin62d76872017-08-05 22:49:42 -0700254#ifndef ASN_DISABLE_PER_SUPPORT
255
Lev Walkin59b176e2005-11-26 11:25:14 +0000256asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700257NativeInteger_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -0700258 asn_TYPE_descriptor_t *td,
259 const asn_per_constraints_t *constraints, void **sptr,
260 asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -0700261 const asn_INTEGER_specifics_t *specs =
262 (const asn_INTEGER_specifics_t *)td->specifics;
263 asn_dec_rval_t rval;
Lev Walkin59b176e2005-11-26 11:25:14 +0000264 long *native = (long *)*sptr;
265 INTEGER_t tmpint;
266 void *tmpintptr = &tmpint;
267
268 (void)opt_codec_ctx;
269 ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
270
271 if(!native) {
272 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700273 if(!native) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000274 }
275
276 memset(&tmpint, 0, sizeof tmpint);
277 rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
278 &tmpintptr, pd);
Lev Walkinb2bed822005-11-27 12:40:43 +0000279 if(rval.code == RC_OK) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000280 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700281 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
Lev Walkin8bb57a22007-12-03 13:41:36 +0000282 : asn_INTEGER2long(&tmpint, native))
Lev Walkin59b176e2005-11-26 11:25:14 +0000283 rval.code = RC_FAIL;
284 else
285 ASN_DEBUG("NativeInteger %s got value %ld",
286 td->name, *native);
Lev Walkinb2bed822005-11-27 12:40:43 +0000287 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000288 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
Lev Walkin59b176e2005-11-26 11:25:14 +0000289
290 return rval;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000291}
292
Lev Walkin523de9e2006-08-18 01:34:18 +0000293asn_enc_rval_t
294NativeInteger_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700295 const asn_per_constraints_t *constraints, void *sptr,
296 asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700297 const asn_INTEGER_specifics_t *specs =
298 (const asn_INTEGER_specifics_t *)td->specifics;
299 asn_enc_rval_t er;
Lev Walkin523de9e2006-08-18 01:34:18 +0000300 long native;
301 INTEGER_t tmpint;
302
Lev Walkin7c1dc052016-03-14 03:08:15 -0700303 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000304
305 native = *(long *)sptr;
306
307 ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
308
309 memset(&tmpint, 0, sizeof(tmpint));
Lev Walkin8bb57a22007-12-03 13:41:36 +0000310 if((specs&&specs->field_unsigned)
311 ? asn_ulong2INTEGER(&tmpint, native)
312 : asn_long2INTEGER(&tmpint, native))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700313 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000314 er = INTEGER_encode_uper(td, constraints, &tmpint, po);
315 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
316 return er;
317}
318
Lev Walkin62d76872017-08-05 22:49:42 -0700319#endif /* ASN_DISABLE_PER_SUPPORT */
320
Lev Walkinf15320b2004-06-03 03:38:44 +0000321/*
322 * INTEGER specific human-readable output.
323 */
324int
Lev Walkin5e033762004-09-29 13:26:15 +0000325NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind62a6622017-08-22 02:31:02 -0700327 const asn_INTEGER_specifics_t *specs =
328 (const asn_INTEGER_specifics_t *)td->specifics;
329 const long *native = (const long *)sptr;
Lev Walkindb13f512004-07-19 17:30:25 +0000330 char scratch[32]; /* Enough for 64-bit int */
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 int ret;
332
Lev Walkind9bd7752004-06-05 08:17:50 +0000333 (void)td; /* Unused argument */
334 (void)ilevel; /* Unused argument */
335
Lev Walkine0b56e02005-02-25 12:10:27 +0000336 if(native) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000337 ret = snprintf(scratch, sizeof(scratch),
338 (specs && specs->field_unsigned)
339 ? "%lu" : "%ld", *native);
Lev Walkine0b56e02005-02-25 12:10:27 +0000340 assert(ret > 0 && (size_t)ret < sizeof(scratch));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000341 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000342 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000343 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000344 }
345}
346
347void
Lev Walkinf6853ce2017-08-11 00:50:27 -0700348NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700349 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700350 if(!td || !ptr)
Lev Walkinf15320b2004-06-03 03:38:44 +0000351 return;
352
353 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700354 td->name, method, ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000355
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700356 switch(method) {
357 case ASFM_FREE_EVERYTHING:
358 FREEMEM(ptr);
359 break;
360 case ASFM_FREE_UNDERLYING:
361 break;
362 case ASFM_FREE_UNDERLYING_AND_RESET:
363 memset(ptr, 0, sizeof(long));
364 break;
365 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000366}
367
Lev Walkincd2f48e2017-08-10 02:14:59 -0700368int
369NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
370 (void)td;
371
372 if(aptr && bptr) {
373 const asn_INTEGER_specifics_t *specs =
374 (const asn_INTEGER_specifics_t *)td->specifics;
Lev Walkinf6853ce2017-08-11 00:50:27 -0700375 if(specs && specs->field_unsigned) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700376 const unsigned long *a = aptr;
377 const unsigned long *b = bptr;
378 if(*a < *b) {
379 return -1;
380 } else if(*a > *b) {
381 return 1;
382 } else {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700383 return 0;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700384 }
385 } else {
386 const long *a = aptr;
387 const long *b = bptr;
388 if(*a < *b) {
389 return -1;
390 } else if(*a > *b) {
391 return 1;
392 } else {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700393 return 0;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700394 }
395 }
396 } else if(!aptr) {
397 return -1;
398 } else {
399 return 1;
400 }
401}