Back to index

im-sdk  12.3.91
comp-jarfile-object.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_jarfile_object *
00012 iiimp_jarfile_object_new(
00013     IIIMP_data_s *   data_s,
00014     IIIMP_string *   class_names,
00015     size_t           value_nbyte,
00016     const uchar_t *  value)
00017 {
00018     IIIMP_jarfile_object *  jar;
00019 
00020     jar = (IIIMP_jarfile_object *)malloc(sizeof (IIIMP_jarfile_object));
00021     if (NULL == jar) {
00022        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00023        return NULL;
00024     }
00025 
00026     jar->nbyte = 4;
00027     jar->class_names_nbyte = 0;
00028 
00029     jar->class_names = class_names;
00030     for (; NULL != class_names; class_names = class_names->next) {
00031        jar->class_names_nbyte += class_names->nbyte;
00032     }
00033     jar->nbyte += jar->class_names_nbyte;
00034 
00035     jar->nbyte += 4;
00036     jar->value.nbyte = value_nbyte;
00037 
00038     jar->value.ptr = (uchar_t*) malloc(value_nbyte * sizeof(uchar_t));
00039     if (!jar->value.ptr) {
00040        free(jar);
00041        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00042        return NULL;
00043     }
00044     memcpy(jar->value.ptr, value, value_nbyte);
00045     jar->nbyte += (value_nbyte + PAD(value_nbyte));
00046 
00047     return jar;
00048 }
00049 
00050 
00051 void
00052 iiimp_jarfile_object_delete(
00053     IIIMP_data_s *          data_s,
00054     IIIMP_jarfile_object *  jarfile)
00055 {
00056     if (NULL == jarfile) return;
00057     iiimp_string_list_delete(data_s, jarfile->class_names);
00058     free(jarfile->value.ptr);
00059     free(jarfile);
00060     return;
00061 }
00062 
00063 
00064 void
00065 iiimp_jarfile_object_pack(
00066     IIIMP_data_s *          data_s,
00067     IIIMP_jarfile_object *  m,
00068     size_t *                nbyte,
00069     uchar_t **                     ptr)
00070 {
00071     size_t    rest;
00072     uchar_t * p;
00073     size_t    pad;
00074 
00075     rest = *nbyte;
00076     p = *ptr;
00077 
00078     PUTU32(m->class_names_nbyte, rest, p, data_s->byte_swap);
00079     iiimp_string_list_pack(data_s, m->class_names, &rest, &p);
00080     PUTU32(m->value.nbyte, rest, p, data_s->byte_swap);
00081     (void)memcpy(p, m->value.ptr, m->value.nbyte);
00082     p += m->value.nbyte;
00083     rest -= m->value.nbyte;
00084     for (pad = PAD(m->value.nbyte); 0 < pad; --pad) {
00085        PUTU8(0, rest, p, data_s->byte_swap);
00086     }
00087 
00088     *nbyte = rest;
00089     *ptr = p;
00090 
00091     return;
00092 }
00093 
00094 
00095 IIIMP_jarfile_object *
00096 iiimp_jarfile_object_unpack(
00097     IIIMP_data_s *   data_s,
00098     size_t *         nbyte,
00099     const uchar_t ** ptr,
00100     size_t           nbyte_max)
00101 {
00102     IIIMP_jarfile_object *  jar;
00103     size_t                  rest;
00104     const uchar_t *         p;
00105     size_t                  len;
00106     int                            i;
00107 
00108     rest = nbyte_max;
00109     p = *ptr;
00110 
00111     if ((*nbyte < rest) || (rest < 4)) {
00112        data_s->status = IIIMP_DATA_INVALID;
00113        return NULL;
00114     }
00115 
00116     GETU32(len, rest, p, data_s->byte_swap);
00117     if (rest < len) {
00118        data_s->status = IIIMP_DATA_INVALID;
00119        return NULL;
00120     }
00121 
00122     jar = (IIIMP_jarfile_object *)malloc(sizeof (IIIMP_jarfile_object));
00123     if (NULL == jar) {
00124        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00125        return NULL;
00126     }
00127 
00128     jar->class_names = iiimp_string_list_unpack(data_s, &rest, &p, len);
00129     if (NULL == jar->class_names) {
00130        free(jar);
00131        return NULL;
00132     }
00133 
00134     GETU32(len, rest, p, data_s->byte_swap);
00135     if (rest < len) {
00136        iiimp_string_list_delete(data_s, jar->class_names);
00137        free(jar);
00138        data_s->status = IIIMP_DATA_INVALID;
00139        return NULL;
00140     }
00141 
00142     jar->value.nbyte = len;
00143     jar->value.ptr = (uchar_t *)malloc(len);
00144     if (NULL == jar->value.ptr) {
00145        iiimp_string_list_delete(data_s, jar->class_names);
00146        free(jar);
00147        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00148        return NULL;
00149     }
00150     (void)memcpy(jar->value.ptr, p, len);
00151 
00152     i = PAD(len);
00153     if ((len + i) < rest) {
00154        rest -= (len + i);
00155        p += (len + i);
00156     } else {
00157        p += rest;
00158        rest = 0;
00159     }
00160 
00161     *nbyte = rest;
00162     *ptr = p;
00163 
00164     return jar;
00165 }
00166 
00167 
00168 void
00169 iiimp_jarfile_object_print(
00170     IIIMP_data_s *          data_s,
00171     IIIMP_jarfile_object *  m)
00172 {
00173     IIIMP_string *   s;
00174 
00175     if (NULL == m) return;
00176 
00177     (void)fprintf(data_s->print_fp, "\tname=");
00178     for (s = m->class_names; NULL != s; s = s->next) {
00179        if (s != m->class_names) {
00180            (void)fprintf(data_s->print_fp, "\t     ");
00181        }
00182        iiimp_string_print(data_s, s);
00183        (void)fputc('\n', data_s->print_fp);
00184     }
00185     (void)fprintf(data_s->print_fp, "\tjarfile object=");
00186     if (IIIMP_PRINT_JARFILE & data_s->print_flag) {
00187        (void)fputc('\n', data_s->print_fp);
00188        iiimp_byte_stream_print(data_s, &(m->value));
00189     } else {
00190        (void)fprintf(data_s->print_fp, "(%d)\n", m->value.nbyte);
00191     }
00192 }
00193 
00194 
00195 /* Local Variables: */
00196 /* c-file-style: "iiim-project" */
00197 /* End: */