]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
move generic encode / decode to encode.c and decode.c
authorAlan T. DeKok <aland@freeradius.org>
Sun, 24 Aug 2025 02:16:43 +0000 (22:16 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Sun, 24 Aug 2025 02:16:43 +0000 (22:16 -0400)
src/lib/util/decode.c
src/lib/util/decode.h
src/lib/util/encode.c
src/lib/util/encode.h
src/lib/util/struct.c

index 032a5dd3ca1263f8f1404818769d6867704cb545..ab6099a5e65e9184df1a4ae17e29d219b525e398 100644 (file)
@@ -322,3 +322,40 @@ ssize_t fr_pair_dns_labels_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out,
        fr_pair_list_append(out, &tmp);
        return labels_len;
 }
+
+/** Generic decode value.
+ *
+ */
+ssize_t fr_pair_decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
+                            uint8_t const *data, size_t const data_len, UNUSED void *decode_ctx)
+{
+       fr_pair_t *vp;
+       ssize_t slen;
+
+       if (!fr_type_is_leaf(parent->type)) {
+               FR_PROTO_TRACE("Cannot use generic decoder for data type %s", fr_type_to_str(parent->type));
+               fr_strerror_printf("Cannot decode data type %s", fr_type_to_str(parent->type));
+               return -1;
+       }
+
+       vp = fr_pair_afrom_da(ctx, parent);
+       if (!vp) return PAIR_DECODE_OOM;
+
+       /*
+        *      No protocol-specific data types here.
+        *
+        *      If we can't decode this field, then the entire
+        *      structure is treated as a raw blob.
+        */
+       slen = fr_value_box_from_network(vp, &vp->data, vp->vp_type, vp->da,
+                                         &FR_DBUFF_TMP(data, data_len), data_len, true);
+       if (slen <= 0) {
+               FR_PROTO_TRACE("failed decoding child VP %s", vp->da->name);
+               talloc_free(vp);
+               return fr_pair_raw_from_network(ctx, out, parent, data, data_len);
+       }
+
+       fr_pair_append(out, vp);
+
+       return slen;
+}
index 0d09a4e8059ec2c764cff87a7c1da05a8a4e68b0..ac4a3cfa517565d0ca14fd29ad6656d8215cb66b 100644 (file)
@@ -68,6 +68,9 @@ ssize_t fr_pair_dns_labels_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out,
                                        fr_dict_attr_t const *parent, uint8_t const *start,
                                        uint8_t const *data, size_t const data_len, fr_dns_labels_t *lb, bool exact);
 
+ssize_t fr_pair_decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
+                            uint8_t const *data, size_t const data_len, UNUSED void *decode_ctx) CC_HINT(nonnull(1,2,3,4));
+
 #ifdef __cplusplus
 }
 #endif
index a952b9469bc4d61199e115c48dad95202d3a43f0..a76ca0a409315b3a7f1d4faf6b891b843409a4d6 100644 (file)
@@ -155,3 +155,25 @@ ssize_t fr_pair_ref_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsig
 
        return fr_dbuff_set(dbuff, &work_dbuff);
 }
+
+/** Generic encode value.
+ *
+ */
+ssize_t fr_pair_encode_value(fr_dbuff_t *dbuff, UNUSED fr_da_stack_t *da_stack, UNUSED unsigned int depth,
+                             fr_dcursor_t *cursor, UNUSED void *encode_ctx)
+{
+       fr_pair_t const         *vp = fr_dcursor_current(cursor);
+       fr_dbuff_t              work_dbuff = FR_DBUFF(dbuff);
+
+       if (!fr_type_is_leaf(vp->vp_type)) {
+               FR_PROTO_TRACE("Cannot use generic encoder for data type %s", fr_type_to_str(vp->vp_type));
+               fr_strerror_printf("Cannot encode data type %s", fr_type_to_str(vp->vp_type));
+               return PAIR_ENCODE_FATAL_ERROR;
+       }
+
+       if (fr_value_box_to_network(&work_dbuff, &vp->data) <= 0) return PAIR_ENCODE_FATAL_ERROR;
+
+       (void) fr_dcursor_next(cursor);
+
+       return fr_dbuff_set(dbuff, &work_dbuff);        
+}
index cd61a117e083d42115ad9bb72e64b8f87b81307a..59eaf9b7718de81ad33c58aaf4bf8b9b2047f370 100644 (file)
@@ -49,6 +49,9 @@ ssize_t fr_pair_cursor_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, un
 ssize_t fr_pair_ref_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth,
                               fr_dcursor_t *cursor) CC_HINT(nonnull);
 
+ssize_t fr_pair_encode_value(fr_dbuff_t *dbuff, UNUSED fr_da_stack_t *da_stack, UNUSED unsigned int depth,
+                            fr_dcursor_t *cursor, UNUSED void *encode_ctx) CC_HINT(nonnull(1,4));
+
 #ifdef __cplusplus
 }
 #endif
index 7f51fcdc7c786958f82aa59cb3ec8035a2654482..641ebb01a440e5e5849839f8acc2690e1af151fd 100644 (file)
@@ -26,47 +26,6 @@ RCSID("$Id$")
 #include <freeradius-devel/util/struct.h>
 #include <freeradius-devel/io/pair.h>
 
-/** Generic decode value.
- *
- */
-static ssize_t generic_decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out,
-                                   fr_dict_attr_t const *parent,
-                                   uint8_t const *data, size_t const data_len, UNUSED void *decode_ctx)
-{
-       fr_pair_t *vp;
-       ssize_t slen;
-
-       if (!fr_type_is_leaf(parent->type)) {
-               FR_PROTO_TRACE("Cannot use generic decoder for data type %s", fr_type_to_str(parent->type));
-               fr_strerror_printf("Cannot decode data type %s", fr_type_to_str(parent->type));
-               return -1;
-       }
-
-       vp = fr_pair_afrom_da(ctx, parent);
-       if (!vp) {
-               FR_PROTO_TRACE("fr_struct_from_network - failed allocating child VP");
-               return PAIR_DECODE_OOM;
-       }
-
-       /*
-        *      No protocol-specific data types here (yet).
-        *
-        *      If we can't decode this field, then the entire
-        *      structure is treated as a raw blob.
-        */
-       slen = fr_value_box_from_network(vp, &vp->data, vp->vp_type, vp->da,
-                                         &FR_DBUFF_TMP(data, data_len), data_len, true);
-       if (slen < 0) {
-               FR_PROTO_TRACE("fr_struct_from_network - failed decoding child VP %s", vp->da->name);
-               talloc_free(vp);
-               return slen;
-       }
-
-       fr_pair_append(out, vp);
-
-       return slen;
-}
-
 /** Convert a STRUCT to one or more VPs
  *
  */
@@ -111,7 +70,7 @@ ssize_t fr_struct_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out,
        /*
         *      Simplify the code by having a generic decode routine.
         */
-       if (!decode_value) decode_value = generic_decode_value;
+       if (!decode_value) decode_value = fr_pair_decode_value;
 
        /*
         *      Decode structs with length prefixes.
@@ -524,26 +483,6 @@ static void *struct_next_encodable(fr_dcursor_t *cursor, void *current, void *uc
        return c;
 }
 
-static ssize_t generic_encode_value(fr_dbuff_t *dbuff, UNUSED fr_da_stack_t *da_stack, UNUSED unsigned int depth,
-                                   fr_dcursor_t *cursor, UNUSED void *encode_ctx)
-{
-       fr_pair_t const         *vp = fr_dcursor_current(cursor);
-       fr_dbuff_t              work_dbuff = FR_DBUFF(dbuff);
-
-       if (!fr_type_is_leaf(vp->vp_type)) {
-               FR_PROTO_TRACE("Cannot use generic encoder for data type %s", fr_type_to_str(vp->vp_type));
-               fr_strerror_printf("Cannot encode data type %s", fr_type_to_str(vp->vp_type));
-               return PAIR_ENCODE_FATAL_ERROR;
-       }
-
-
-       if (fr_value_box_to_network(&work_dbuff, &vp->data) <= 0) return PAIR_ENCODE_FATAL_ERROR;
-
-       (void) fr_dcursor_next(cursor);
-
-       return fr_dbuff_set(dbuff, &work_dbuff);        
-}
-
 
 ssize_t fr_struct_to_network(fr_dbuff_t *dbuff,
                             fr_da_stack_t *da_stack, unsigned int depth,
@@ -642,7 +581,7 @@ ssize_t fr_struct_to_network(fr_dbuff_t *dbuff,
                do_length = true;
        }
 
-       if (!encode_value) encode_value = generic_encode_value;
+       if (!encode_value) encode_value = fr_pair_encode_value;
 
        /*
         *      Loop over all children.