Back to index

im-sdk  12.3.91
comp-operation.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 
00010 
00011 IIIMP_operation *
00012 iiimp_operation_new(
00013     IIIMP_data_s *   data_s,
00014     IIIMP_string *   id,
00015     size_t           value_nbyte,
00016     uchar_t *        value)
00017 {
00018     IIIMP_operation *       op;
00019 
00020     op = (IIIMP_operation *)malloc(sizeof (IIIMP_operation));
00021     if (NULL == op) {
00022        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00023        return NULL;
00024     }
00025 
00026     op->id = id;
00027     op->next = NULL;
00028 
00029     if (NULL == id) {
00030        op->nbyte = 4;
00031     } else {
00032        op->nbyte = id->nbyte;
00033     }
00034 
00035     if (0 == value_nbyte) {
00036        op->nbyte += (2 + 2);
00037        op->value.nbyte = 0;
00038        op->value.ptr = NULL;
00039     } else {
00040        op->nbyte += (2 + value_nbyte + PAD(2 + value_nbyte));
00041        op->value.nbyte = value_nbyte;
00042        op->value.ptr = (uchar_t *)malloc(value_nbyte);
00043        if (NULL == op->value.ptr) {
00044            free(op);
00045            data_s->status = IIIMP_DATA_MALLOC_ERROR;
00046            return NULL;
00047        }
00048        (void)memcpy(op->value.ptr, value, value_nbyte);
00049     }
00050 
00051     return op;
00052 }
00053 
00054 
00055 void
00056 iiimp_operation_delete(IIIMP_data_s * data_s, IIIMP_operation * op)
00057 {
00058     if (NULL == op) return;
00059     iiimp_string_delete(data_s, op->id);
00060     free(op->value.ptr);
00061     free(op);
00062     return;
00063 }
00064 
00065 
00066 void
00067 iiimp_operation_list_delete(IIIMP_data_s * data_s, IIIMP_operation * op)
00068 {
00069     IIIMP_operation *       op_next;
00070     for (; NULL != op; op = op_next) {
00071        op_next = op->next;
00072        iiimp_string_delete(data_s, op->id);
00073        free(op->value.ptr);
00074        free(op);
00075     }
00076     return;
00077 }
00078 
00079 
00080 void
00081 iiimp_operation_pack(
00082     IIIMP_data_s *   data_s,
00083     IIIMP_operation *       m,
00084     size_t *         nbyte,
00085     uchar_t **              ptr)
00086 {
00087     size_t    rest;
00088     uchar_t * p;
00089     int              pad;
00090     int              i;
00091 
00092     rest = *nbyte;
00093     p = *ptr;
00094 
00095     iiimp_string_pack(data_s, m->id, &rest, &p);
00096     PUTU16(m->value.nbyte, rest, p, data_s->byte_swap);
00097     pad = PAD(2 + m->value.nbyte);
00098     (void)memcpy(p, m->value.ptr, m->value.nbyte);
00099     p += (m->value.nbyte);
00100     rest -= (m->value.nbyte);
00101     for (i = 0; i < pad; i++) {
00102        PUT8(0, rest, p, data_s->byte_swap);
00103     }
00104 
00105     *nbyte = rest;
00106     *ptr = p;
00107 
00108     return;
00109 }
00110 
00111 
00112 void
00113 iiimp_operation_list_pack(
00114     IIIMP_data_s *   data_s,
00115     IIIMP_operation *       m,
00116     size_t *         nbyte,
00117     uchar_t **              ptr)
00118 {
00119     size_t    rest;
00120     uchar_t * p;
00121 
00122     rest = *nbyte;
00123     p = *ptr;
00124 
00125     for (; NULL != m; m = m->next) {
00126        iiimp_operation_pack(data_s, m, &rest, &p);
00127     }
00128 
00129     *nbyte = rest;
00130     *ptr = p;
00131 
00132     return;
00133 }
00134 
00135 
00136 IIIMP_operation *
00137 iiimp_operation_unpack(
00138     IIIMP_data_s *   data_s,
00139     size_t *         nbyte,
00140     const uchar_t ** ptr,
00141     size_t           nbyte_max)
00142 {
00143     IIIMP_operation *       op;
00144     size_t           rest;
00145     const uchar_t *  p;
00146     size_t           len;
00147     int                     pad;
00148 
00149     rest = nbyte_max;
00150     p = *ptr;
00151 
00152     if ((*nbyte < rest) || (rest < 4)) {
00153        data_s->status = IIIMP_DATA_INVALID;
00154        return NULL;
00155     }
00156 
00157     op = (IIIMP_operation *)malloc(sizeof (IIIMP_operation));
00158     if (NULL == op) {
00159        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00160        return NULL;
00161     }
00162 
00163     op->value.ptr = NULL;
00164     op->next = NULL;
00165 
00166     op->id = iiimp_string_unpack(data_s, &rest, &p, rest);
00167     if ((NULL == op->id) || (rest < 4)) {
00168        iiimp_operation_delete(data_s, op);
00169        return NULL;
00170     }
00171 
00172     GETU16(len, rest, p, data_s->byte_swap);
00173     if (rest <len) {
00174        iiimp_operation_delete(data_s, op);
00175        data_s->status = IIIMP_DATA_INVALID;
00176        return NULL;
00177     }
00178 
00179     op->value.nbyte = len;
00180     op->value.ptr = (uchar_t *)malloc(len);
00181     if (NULL == op->value.ptr) {
00182        iiimp_operation_delete(data_s, op);
00183        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00184        return NULL;
00185     }
00186     (void)memcpy(op->value.ptr, p, len);
00187     pad = PAD(2 + len);
00188     rest -= (len + pad);
00189     p += (len + pad);
00190 
00191     *nbyte -= (nbyte_max - rest);
00192     *ptr = p;
00193 
00194     return op;
00195 }
00196 
00197 
00198 IIIMP_operation *
00199 iiimp_operation_list_unpack(
00200     IIIMP_data_s *   data_s,
00201     size_t *         nbyte,
00202     const uchar_t ** ptr,
00203     size_t           nbyte_max)
00204 {
00205     IIIMP_operation *       op;
00206     size_t           rest;
00207     const uchar_t *  p;
00208     IIIMP_operation *       op_first;
00209     IIIMP_operation *       op_last;
00210 
00211     rest = nbyte_max;
00212     p = *ptr;
00213 
00214     if (*nbyte < rest) {
00215        data_s->status = IIIMP_DATA_INVALID;
00216        return NULL;
00217     }
00218 
00219     op_first = NULL;
00220     op_last = NULL;
00221 
00222     while (0 < rest) {
00223        op = iiimp_operation_unpack(data_s, &rest, &p, rest);
00224        if (NULL == op) {
00225            iiimp_operation_list_delete(data_s, op_first);
00226            return NULL;
00227        } else {
00228            if (NULL == op_first) {
00229               op_first = op;
00230            } else {
00231               op_last->next = op;
00232            }
00233            op_last = op;
00234        }
00235     }
00236 
00237     *nbyte -= (nbyte_max - rest);
00238     *ptr = p;
00239 
00240     return op_first;
00241 }
00242 
00243 
00244 void
00245 iiimp_operation_print(
00246     IIIMP_data_s *   data_s,
00247     IIIMP_operation *       m)
00248 {
00249     if (NULL == m) return;
00250     (void)fprintf(data_s->print_fp, "\toperation-id=");
00251     iiimp_string_print(data_s, m->id);
00252     (void)fputc('\n', data_s->print_fp);
00253     (void)fprintf(data_s->print_fp, "\toperation-value=\n");
00254     iiimp_byte_stream_print(data_s, &(m->value));
00255 }
00256 
00257 
00258 void
00259 iiimp_operation_list_print(
00260     IIIMP_data_s *   data_s,
00261     IIIMP_operation *       m)
00262 {
00263     for (; NULL != m; m = m->next) {
00264        iiimp_operation_print(data_s, m);
00265     }
00266 }
00267 
00268 
00269 /* Local Variables: */
00270 /* c-file-style: "iiim-project" */
00271 /* End: */