Back to index

im-sdk  12.3.91
comp-object-descriptor.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <sys/types.h>
00005 
00006 #include <iiimp-data.h>
00007 
00008 #include "iiimp-dataP.h"
00009 #include "print-misc.h"
00010 
00011 
00012 IIIMP_object_descriptor *
00013 iiimp_object_descriptor_new(
00014     IIIMP_data_s *   data_s,
00015     IIIMP_card16     category,
00016     IIIMP_card32     object_size,
00017     IIIMP_card16     id_pre,
00018     IIIMP_card16     id_dyn,
00019     IIIMP_string *   rdun,
00020     IIIMP_string *   hrn,
00021     IIIMP_string *   signature,
00022     IIIMP_string *   user)
00023 {
00024     IIIMP_object_descriptor *      od;
00025 
00026     od = (IIIMP_object_descriptor *)malloc(sizeof (IIIMP_object_descriptor));
00027     if (NULL == od) {
00028        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00029        return NULL;
00030     }
00031 
00032     od->nbyte = (2 + 2 + 4 + 2 + 2);
00033     if (NULL == rdun) {
00034        od->nbyte += 4;
00035     } else {
00036        od->nbyte += rdun->nbyte;
00037     }
00038     if (NULL == hrn) {
00039        od->nbyte += 4;
00040     } else {
00041        od->nbyte += hrn->nbyte;
00042     }
00043     if (NULL == signature) {
00044        od->nbyte += 4;
00045     } else {
00046        od->nbyte += signature->nbyte;
00047     }
00048     if (NULL == user) {
00049        od->nbyte += 4;
00050     } else {
00051        od->nbyte += user->nbyte;
00052     }
00053 
00054     od->category = category;
00055     od->size = object_size;
00056     od->id_pre = id_pre;
00057     od->id_dyn = id_dyn;
00058     od->rdun = rdun;
00059     od->hrn = hrn;
00060     od->signature = signature;
00061     od->user = user;
00062     od->next = NULL;
00063 
00064     return od;
00065 }
00066 
00067 
00068 void
00069 iiimp_object_descriptor_delete(
00070     IIIMP_data_s *          data_s,
00071     IIIMP_object_descriptor *      od)
00072 {
00073     if (NULL == od) return;
00074     iiimp_string_delete(data_s, od->rdun);
00075     iiimp_string_delete(data_s, od->hrn);
00076     iiimp_string_delete(data_s, od->signature);
00077     iiimp_string_delete(data_s, od->user);
00078     free(od);
00079     return;
00080 }
00081 
00082 
00083 void
00084 iiimp_object_descriptor_list_delete(
00085     IIIMP_data_s *          data_s,
00086     IIIMP_object_descriptor *      od)
00087 {
00088     IIIMP_object_descriptor *      od_next;
00089     for (; NULL != od; od = od_next) {
00090        od_next = od->next;
00091        iiimp_object_descriptor_delete(data_s, od);
00092     }
00093     return;
00094 }
00095 
00096 
00097 void
00098 iiimp_object_descriptor_pack(
00099     IIIMP_data_s *          data_s,
00100     IIIMP_object_descriptor *      m,
00101     size_t *                nbyte,
00102     uchar_t **                     ptr)
00103 {
00104     size_t    rest;
00105     uchar_t * p;
00106 
00107     rest = *nbyte;
00108     p = *ptr;
00109 
00110     PUTU16(m->category, rest, p, data_s->byte_swap);
00111     PUTU16(0, rest, p, data_s->byte_swap);
00112     PUTU32(m->size, rest, p, data_s->byte_swap);
00113     PUTU16(m->id_pre, rest, p, data_s->byte_swap);
00114     PUTU16(m->id_dyn, rest, p, data_s->byte_swap);
00115     iiimp_string_pack(data_s, m->rdun, &rest, &p);
00116     iiimp_string_pack(data_s, m->hrn, &rest, &p);
00117     iiimp_string_pack(data_s, m->signature, &rest, &p);
00118     iiimp_string_pack(data_s, m->user, &rest, &p);
00119 
00120     *nbyte = rest;
00121     *ptr = p;
00122 
00123     return;
00124 }
00125 
00126 
00127 void
00128 iiimp_object_descriptor_list_pack(
00129     IIIMP_data_s *          data_s,
00130     IIIMP_object_descriptor *      m,
00131     size_t *                nbyte,
00132     uchar_t **                     ptr)
00133 {
00134     size_t    rest;
00135     uchar_t * p;
00136 
00137     rest = *nbyte;
00138     p = *ptr;
00139 
00140     for (; NULL != m; m = m->next) {
00141        iiimp_object_descriptor_pack(data_s, m, &rest, &p);
00142     }
00143 
00144     *nbyte = rest;
00145     *ptr = p;
00146 
00147     return;
00148 }
00149 
00150 
00151 IIIMP_object_descriptor *
00152 iiimp_object_descriptor_unpack(
00153     IIIMP_data_s *   data_s,
00154     size_t *         nbyte,
00155     const uchar_t ** ptr,
00156     size_t           nbyte_max)
00157 {
00158     IIIMP_object_descriptor *      od;
00159     size_t                  rest;
00160     const uchar_t *         p;
00161 
00162     rest = nbyte_max;
00163     p = *ptr;
00164 
00165     if ((*nbyte < rest) || (rest < (4 + 4 + 4 + 4))) {
00166        data_s->status = IIIMP_DATA_INVALID;
00167        return NULL;
00168     }
00169 
00170     od = (IIIMP_object_descriptor *)malloc(sizeof (IIIMP_object_descriptor));
00171     if (NULL == od) {
00172        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00173        return NULL;
00174     }
00175 
00176     GETU16(od->category, rest, p, data_s->byte_swap);
00177     SKIP16(rest, p);
00178     GETU32(od->size, rest, p, data_s->byte_swap);
00179     GETU16(od->id_pre, rest, p, data_s->byte_swap);
00180     GETU16(od->id_dyn, rest, p, data_s->byte_swap);
00181     if (od->id_dyn >= (1 << 15)) {
00182        iiimp_attribute_id_update(data_s, od->id_pre, od->id_dyn);
00183     }
00184 
00185     od->rdun = NULL;
00186     od->hrn = NULL;
00187     od->signature = NULL;
00188     od->user = NULL;
00189     od->next = NULL;
00190 
00191     od->rdun = iiimp_string_unpack(data_s, &rest, &p, rest);
00192     if (NULL == od->rdun) {
00193        iiimp_object_descriptor_delete(data_s, od);
00194        return NULL;
00195     }
00196 
00197     od->hrn = iiimp_string_unpack(data_s, &rest, &p, rest);
00198     if (NULL == od->hrn) {
00199        iiimp_object_descriptor_delete(data_s, od);
00200        return NULL;
00201     }
00202 
00203     od->signature = iiimp_string_unpack(data_s, &rest, &p, rest);
00204     if (NULL == od->signature) {
00205        iiimp_object_descriptor_delete(data_s, od);
00206        return NULL;
00207     }
00208 
00209     od->user = iiimp_string_unpack(data_s, &rest, &p, rest);
00210     if (NULL == od->user) {
00211        iiimp_object_descriptor_delete(data_s, od);
00212        return NULL;
00213     }
00214 
00215     od->nbyte = (2 + 2 + 4 + 2 + 2);
00216     od->nbyte += od->rdun->nbyte;
00217     od->nbyte += od->hrn->nbyte;
00218     od->nbyte += od->signature->nbyte;
00219     od->nbyte += od->user->nbyte;
00220 
00221     *nbyte = rest;
00222     *ptr = p;
00223 
00224     return od;
00225 }
00226 
00227 
00228 IIIMP_object_descriptor *
00229 iiimp_object_descriptor_list_unpack(
00230     IIIMP_data_s *          data_s,
00231     size_t *                nbyte,
00232     const uchar_t **        ptr,
00233     size_t                  nbyte_max)
00234 {
00235     IIIMP_object_descriptor *      od;
00236     size_t                  rest;
00237     const uchar_t *         p;
00238     IIIMP_object_descriptor *      od_first;
00239     IIIMP_object_descriptor *      od_last;
00240 
00241     rest = nbyte_max;
00242     p = *ptr;
00243     od_first = NULL;
00244     od_last = NULL;
00245 
00246     if (((*nbyte) < nbyte_max) || (0 != (rest & 0x01))) {
00247        data_s->status = IIIMP_DATA_INVALID;
00248        return NULL;
00249     }
00250 
00251     while (0 < rest) {
00252        od = iiimp_object_descriptor_unpack(data_s, &rest, &p, rest);
00253        if (NULL == od) {
00254            iiimp_object_descriptor_list_delete(data_s, od_first);
00255            return NULL;
00256        } else {
00257            if (NULL == od_first) {
00258               od_first = od;
00259            } else {
00260               od_last->next = od;
00261            }
00262            od_last = od;
00263        }
00264     }
00265 
00266     *nbyte -= (nbyte_max - rest);
00267     *ptr = p;
00268 
00269     return od_first;
00270 }
00271 
00272 
00273 void
00274 iiimp_object_descriptor_print(
00275     IIIMP_data_s *          data_s,
00276     IIIMP_object_descriptor *      m)
00277 {
00278     if (NULL == m) return;
00279 
00280     (void)fputc('\t', data_s->print_fp);
00281 
00282     (void)fprintf(data_s->print_fp, "%s (%d)",
00283                 object_descriptor_category_string_get(m->category),
00284                 m->category);
00285     (void)fputc(' ', data_s->print_fp);
00286     (void)fprintf(data_s->print_fp, "size=%d ", m->size);
00287     (void)fprintf(data_s->print_fp, "predefined=0x%x ", m->id_pre);
00288     (void)fprintf(data_s->print_fp, "dynamic=0x%x ", m->id_dyn);
00289     (void)fputc('\n', data_s->print_fp);
00290 
00291     (void)fprintf(data_s->print_fp, "\tname=");
00292     iiimp_string_print(data_s, m->rdun);
00293     (void)fputc('\n', data_s->print_fp);
00294 
00295     (void)fprintf(data_s->print_fp, "\tdescription=");
00296     iiimp_string_print(data_s, m->hrn);
00297     (void)fputc('\n', data_s->print_fp);
00298 
00299     (void)fprintf(data_s->print_fp, "\tsignature=");
00300     iiimp_string_print(data_s, m->signature);
00301     (void)fputc('\n', data_s->print_fp);
00302 
00303     (void)fprintf(data_s->print_fp, "\tuser=");
00304     iiimp_string_print(data_s, m->user);
00305     (void)fputc('\n', data_s->print_fp);
00306 }
00307 
00308 
00309 void
00310 iiimp_object_descriptor_list_print(
00311     IIIMP_data_s *          data_s,
00312     IIIMP_object_descriptor *      m)
00313 {
00314     for (; NULL != m; m = m->next) {
00315        iiimp_object_descriptor_print(data_s, m);
00316     }
00317 }
00318 
00319 
00320 /* Local Variables: */
00321 /* c-file-style: "iiim-project" */
00322 /* End: */