summaryrefslogtreecommitdiffstats
path: root/lib/libjove/syscall/path-fromobj.c
blob: 1c0c5d9358f3737888cff15632ef955413377932 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <path-fromobj.h>

int
path_fromobj_s(KernelObjectTyped *obj, uint8_t *buffer, int size)
{
    size_t depth = 0, odepth = 0;
    for(KernelObjectTyped *o = obj->parent; o; o = o->parent, depth++);

    odepth = depth;
    if(depth > size) depth = size;

    for(int i = 0; i < depth; i++) {
        buffer[depth - i - 1] = obj->membi;
        obj = obj->parent;
    }
    return odepth;
}

int 
path_tobuf(KernelObjectTyped *obj, uint8_t *buffer, size_t at, size_t bufferw)
{
    if(at + sizeof(size_t) > bufferw) return -1;

    size_t *buffer_pathW = (size_t*)(&buffer[at]);
    uint8_t *buffer_path = (uint8_t*)(&buffer_pathW[1]);

    size_t maxDepth = bufferw - (buffer_path - buffer);
    size_t objDepth = path_fromobj_s(obj, buffer_path, bufferw - (size_t)(buffer_path - buffer));
    if(objDepth > maxDepth) return -1;

    *buffer_pathW = objDepth;
    return (int)(buffer_path - buffer) + objDepth;
}