]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
generic/pack: support for lookup
authorMarek Vavruša <marek.vavrusa@nic.cz>
Fri, 29 May 2015 16:15:08 +0000 (18:15 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Fri, 29 May 2015 16:15:08 +0000 (18:15 +0200)
lib/generic/pack.h
tests/test_pack.c

index 5bad8f0c196dcf5ed19374970fd9671fe1fefa37..613505e580eb729195d512788bdfd52f8293bd64 100644 (file)
@@ -129,24 +129,37 @@ static inline int pack_obj_push(pack_t *pack, const uint8_t *obj, pack_objlen_t
        return 0;
 }
 
+/** Returns a pointer to packed object.
+  * @return pointer to packed object or NULL
+  */
+static inline uint8_t *pack_obj_find(pack_t *pack, const uint8_t *obj, pack_objlen_t len)
+{
+               uint8_t *endp = pack_tail(*pack);
+               uint8_t *it = pack_head(*pack);
+               while (it != endp) {
+                       uint8_t *val = pack_obj_val(it);
+                       if (pack_obj_len(it) == len && memcmp(obj, val, len) == 0) {
+                               return it;
+                       }
+                       it = pack_obj_next(it);
+               }
+               return NULL;
+}
+
 /** Delete object from the pack
   * @return 0 on success, negative number on failure
   */
 static inline int pack_obj_del(pack_t *pack, const uint8_t *obj, pack_objlen_t len)
 {
        uint8_t *endp = pack_tail(*pack);
-       uint8_t *it = pack_head(*pack);
-       while (it != endp) {
-               uint8_t *val = pack_obj_val(it);
-               if (pack_obj_len(it) == len && memcmp(obj, val, len) == 0) {
-                       size_t packed_len = len + sizeof(len);
-                       memmove(it, it + packed_len, endp - it - packed_len);
-                       pack->len -= packed_len;
-                       return 0;
-               }
-               it = pack_obj_next(it);
-       }
-       return -1;
+       uint8_t *it = pack_obj_find(pack, obj, len);
+       if (it) {
+               size_t packed_len = len + sizeof(len);
+               memmove(it, it + packed_len, endp - it - packed_len);
+               pack->len -= packed_len;
+               return 0;
+       }
+       return -1;
 }
 
 #ifdef __cplusplus
index f5ad501cd28580b1d0062e4c377407a7442917d4..c2a470b4a46719adeb04fcdaada5c9dd5c2f957b 100644 (file)
@@ -49,6 +49,12 @@ static void test_pack_std(void **state)
                count += 1;
        }
 
+       /* Find */
+       it = pack_obj_find(&pack, U8("de"), 2);
+       assert_non_null(it);
+       it = pack_obj_find(&pack, U8("ed"), 2);
+       assert_null(it);
+
        /* Delete */
        assert_int_not_equal(pack_obj_del(&pack, U8("be"), 2), 0);
        assert_int_equal(pack_obj_del(&pack, U8("de"), 2), 0);