1 /*===========================================================================
2  *
3  *                            PUBLIC DOMAIN NOTICE
4  *               National Center for Biotechnology Information
5  *
6  *  This software/database is a "United States Government Work" under the
7  *  terms of the United States Copyright Act.  It was written as part of
8  *  the author's official duties as a United States Government employee and
9  *  thus cannot be copyrighted.  This software/database is freely available
10  *  to the public for use. The National Library of Medicine and the U.S.
11  *  Government have not placed any restriction on its use or reproduction.
12  *
13  *  Although all reasonable efforts have been taken to ensure the accuracy
14  *  and reliability of the software and data, the NLM and the U.S.
15  *  Government do not and cannot warrant the performance or results that
16  *  may be obtained by using this software or data. The NLM and the U.S.
17  *  Government disclaim all warranties, express or implied, including
18  *  warranties of performance, merchantability or fitness for any particular
19  *  purpose.
20  *
21  *  Please cite the author in any work or product based on this material.
22  *
23  * ===========================================================================
24  *
25  */
26 #include <vdb/extern.h>
27 #include <klib/defs.h>
28 #include <klib/rc.h>
29 #include <vdb/xform.h>
30 #include <vdb/schema.h>
31 #include <sysalloc.h>
32 #include <klib/pack.h>
33 
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <assert.h>
37 
38 typedef struct self_t self_t;
39 struct self_t
40 {
41     VTypedesc sdesc;
42     VTypedesc ddesc;
43     uint32_t sbits;
44 };
45 
46 static
unpack_func(void * Self,const VXformInfo * info,void * dst,const void * src,uint64_t elem_count)47 rc_t CC unpack_func( void *Self, const VXformInfo *info,
48     void *dst, const void *src, uint64_t elem_count )
49 {
50     size_t usize;
51     const self_t *self = ( const void* ) Self;
52     bitsz_t ssize = elem_count * VTypedescSizeof ( & self -> sdesc );
53     size_t dsize = (elem_count * VTypedescSizeof ( & self -> ddesc ) + 7) >> 3;
54 
55     return Unpack( self -> sbits, self -> ddesc.intrinsic_bits,
56         src, 0, ssize, NULL,
57         dst, dsize, &usize );
58 }
59 
60 static
vxf_unpack_wrapper(void * ptr)61 void CC vxf_unpack_wrapper( void *ptr )
62 {
63     free( ptr );
64 }
65 
66 /*
67  */
68 VTRANSFACT_IMPL ( vdb_unpack, 1, 0, 0 ) ( const void *ignore, const VXfactInfo *info,
69     VFuncDesc *rslt, const VFactoryParams *cp, const VFunctionParams *dp )
70 {
71     self_t *self = malloc ( sizeof * self );
72     if ( self == NULL )
73         return RC ( rcVDB, rcFunction, rcConstructing, rcMemory, rcExhausted );
74 
75     assert (dp->argc == 1);
76     self->sdesc = dp->argv[0].desc;
77     self->ddesc = info->fdesc.desc;
78     self->sbits = VTypedescSizeof ( & dp->argv[0].desc );
79 
80     rslt->self = self;
81     rslt->whack = vxf_unpack_wrapper;
82 
83     rslt->variant = vftArray;
84     rslt->u.af = unpack_func;
85 
86     return 0;
87 }
88 
89 VTRANSFACT_IMPL ( NCBI_unpack, 1, 0, 0 ) ( const void *ignore, const VXfactInfo *info,
90     VFuncDesc *rslt, const VFactoryParams *cp, const VFunctionParams *dp )
91 {
92     self_t *self = malloc ( sizeof * self );
93     if ( self == NULL )
94         return RC ( rcVDB, rcFunction, rcConstructing, rcMemory, rcExhausted );
95 
96     assert ( dp -> argc == 1 );
97 
98     assert ( cp -> argc == 2 );
99     assert ( cp -> argv [ 0 ] . desc . intrinsic_bits == 32 );
100     assert ( cp -> argv [ 0 ] . desc . domain == vtdUint );
101 
102     self->sdesc = dp->argv[0].desc;
103     self -> ddesc . intrinsic_bits = cp -> argv [ 1 ] . data . u32 [ 0 ];
104     self -> ddesc . intrinsic_dim = 1;
105     self -> ddesc . domain = 0;
106     self -> sbits = cp -> argv [ 0 ] . data . u32 [ 0 ];
107 
108     rslt->self = self;
109     rslt->whack = vxf_unpack_wrapper;
110 
111     rslt->variant = vftArray;
112     rslt->u.af = unpack_func;
113 
114     return 0;
115 }
116