Back to index

im-sdk  12.3.91
comp-imeinfo.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 #define IMEINFO_OBJECT_SIZE 4 * 9
00011 
00012 IIIMP_imeinfo *
00013 iiimp_imeinfo_new(
00014     IIIMP_data_s *   data_s,
00015     IIIMP_card32     enable,
00016     IIIMP_string *   ime_id,
00017     IIIMP_string *   imename,
00018     IIIMP_string *   version,
00019     IIIMP_string *   description,
00020     IIIMP_string *   author,
00021     IIIMP_string *   copyright,
00022     IIIMP_string *   reserved1,
00023     IIIMP_string *   reserved2)
00024 {
00025     IIIMP_imeinfo *  imeinfo;
00026 
00027     imeinfo = (IIIMP_imeinfo *)malloc(sizeof (IIIMP_imeinfo));
00028     if (NULL == imeinfo) {
00029        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00030        return NULL;
00031     }
00032 
00033     imeinfo->nbyte = 4; /* for enable */
00034     imeinfo->next = NULL;
00035 
00036     if (NULL == ime_id) {
00037        imeinfo->nbyte += 4;
00038     } else {
00039        imeinfo->nbyte += ime_id->nbyte;
00040     }
00041     if (NULL == imename) {
00042        imeinfo->nbyte += 4;
00043     } else {
00044        imeinfo->nbyte += imename->nbyte;
00045     }
00046     if (NULL == version) {
00047        imeinfo->nbyte += 4;
00048     } else {
00049        imeinfo->nbyte += version->nbyte;
00050     }
00051     if (NULL == description) {
00052        imeinfo->nbyte += 4;
00053     } else {
00054        imeinfo->nbyte += description->nbyte;
00055     }
00056     if (NULL == author) {
00057         imeinfo->nbyte += 4;
00058     } else {
00059         imeinfo->nbyte += author->nbyte;
00060     }
00061     if (NULL == copyright) {
00062         imeinfo->nbyte += 4;
00063     } else {
00064         imeinfo->nbyte += copyright->nbyte;
00065     }
00066     if (NULL == reserved1) {
00067         imeinfo->nbyte += 4;
00068     } else {
00069         imeinfo->nbyte += reserved1->nbyte;
00070     }
00071     if (NULL == reserved2) {
00072         imeinfo->nbyte += 4;
00073     } else {
00074         imeinfo->nbyte += reserved2->nbyte;
00075     }
00076 
00077     imeinfo->enable = enable;
00078     imeinfo->ime_id = ime_id;
00079     imeinfo->imename = imename;
00080     imeinfo->version = version;
00081     imeinfo->description = description;
00082     imeinfo->author = author;
00083     imeinfo->copyright = copyright;
00084     imeinfo->reserved1 = reserved1;
00085     imeinfo->reserved2 = reserved2;
00086 
00087     return imeinfo;
00088 }
00089 
00090 void
00091 iiimp_imeinfo_delete(IIIMP_data_s * data_s, IIIMP_imeinfo * imeinfo)
00092 {
00093     if (NULL == imeinfo) return;
00094     iiimp_string_delete(data_s, imeinfo->ime_id);
00095     iiimp_string_delete(data_s, imeinfo->imename);
00096     iiimp_string_delete(data_s, imeinfo->version);
00097     iiimp_string_delete(data_s, imeinfo->description);
00098     iiimp_string_delete(data_s, imeinfo->author);
00099     iiimp_string_delete(data_s, imeinfo->copyright);
00100     iiimp_string_delete(data_s, imeinfo->reserved1);
00101     iiimp_string_delete(data_s, imeinfo->reserved2);
00102     free(imeinfo);
00103     return;
00104 }
00105 
00106 void
00107 iiimp_imeinfo_list_delete(IIIMP_data_s * data_s, IIIMP_imeinfo * imeinfo)
00108 {
00109     IIIMP_imeinfo *  imeinfo_next;
00110     for (; NULL != imeinfo; imeinfo = imeinfo_next) {
00111        imeinfo_next = imeinfo->next;
00112        iiimp_imeinfo_delete(data_s, imeinfo);
00113     }
00114     return;
00115 }
00116 
00117 void
00118 iiimp_imeinfo_pack(
00119     IIIMP_data_s *   data_s,
00120     IIIMP_imeinfo *  m,
00121     size_t *         nbyte,
00122     uchar_t **              ptr)
00123 {
00124     size_t    rest;
00125     uchar_t * p;
00126 
00127     rest = *nbyte;
00128     p = *ptr;
00129 
00130     PUT32(m->enable, rest, p, data_s->byte_swap);
00131     iiimp_string_pack(data_s, m->ime_id, &rest, &p);
00132     iiimp_string_pack(data_s, m->imename, &rest, &p);
00133     iiimp_string_pack(data_s, m->version, &rest, &p);
00134     iiimp_string_pack(data_s, m->description, &rest, &p);
00135     iiimp_string_pack(data_s, m->author, &rest, &p);
00136     iiimp_string_pack(data_s, m->copyright, &rest, &p);
00137     iiimp_string_pack(data_s, m->reserved1, &rest, &p);
00138     iiimp_string_pack(data_s, m->reserved2, &rest, &p);
00139 
00140     *nbyte -= rest;
00141     *ptr = p;
00142 
00143     return;
00144 }
00145 
00146 
00147 void
00148 iiimp_imeinfo_list_pack(
00149     IIIMP_data_s *   data_s,
00150     IIIMP_imeinfo *  m,
00151     size_t *         nbyte,
00152     uchar_t **              ptr)
00153 {
00154     size_t    rest;
00155     uchar_t * p;
00156 
00157     rest = *nbyte;
00158     p = *ptr;
00159 
00160     for (; NULL != m; m = m->next) {
00161        iiimp_imeinfo_pack(data_s, m, &rest, &p);
00162     }
00163 
00164     *nbyte -= rest;
00165     *ptr = p;
00166 
00167     return;
00168 }
00169 
00170 
00171 IIIMP_imeinfo *
00172 iiimp_imeinfo_unpack(
00173     IIIMP_data_s *      data_s,
00174     size_t *            nbyte,
00175     const uchar_t **    ptr,
00176     size_t              nbyte_max)
00177 {
00178     IIIMP_imeinfo *    imeinfo;
00179     size_t              rest;
00180     const uchar_t *     p;
00181 
00182     if (nbyte_max == 0) return NULL;
00183 
00184     rest = nbyte_max;
00185     p = *ptr;
00186 
00187     if ((*nbyte < rest) || (rest < IMEINFO_OBJECT_SIZE)) {
00188        data_s->status = IIIMP_DATA_INVALID;
00189        return NULL;
00190     }
00191 
00192     imeinfo = (IIIMP_imeinfo *)malloc(sizeof (IIIMP_imeinfo));
00193     if (NULL == imeinfo) {
00194        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00195        return NULL;
00196     }
00197 
00198     memset (imeinfo, 0, sizeof (IIIMP_imeinfo));
00199 
00200     /*
00201      * the imeinfo is optional, so we don't need to check the following members 
00202      * are NULL or not.
00203      */
00204     GET32(imeinfo->enable, rest, p, data_s->byte_swap);
00205     imeinfo->ime_id = iiimp_string_unpack(data_s, &rest, &p, rest);
00206     imeinfo->imename = iiimp_string_unpack(data_s, &rest, &p, rest);
00207     imeinfo->version = iiimp_string_unpack(data_s, &rest, &p, rest);
00208     imeinfo->description = iiimp_string_unpack(data_s, &rest, &p, rest);
00209     imeinfo->author = iiimp_string_unpack(data_s, &rest, &p, rest);
00210     imeinfo->copyright = iiimp_string_unpack(data_s, &rest, &p, rest);
00211     imeinfo->reserved1 = iiimp_string_unpack(data_s, &rest, &p, rest);
00212     imeinfo->reserved2 = iiimp_string_unpack(data_s, &rest, &p, rest);
00213 
00214     *nbyte -= (nbyte_max - rest);
00215     *ptr = p;
00216 
00217     return imeinfo;
00218 }
00219 
00220 
00221 IIIMP_imeinfo *
00222 iiimp_imeinfo_list_unpack(
00223     IIIMP_data_s *      data_s,
00224     size_t *            nbyte,
00225     const uchar_t **    ptr,
00226     size_t              nbyte_max)
00227 {
00228     IIIMP_imeinfo *    imeinfo;
00229     size_t              rest;
00230     const uchar_t *     p;
00231     IIIMP_imeinfo *    imeinfo_first;
00232     IIIMP_imeinfo *    imeinfo_last;
00233 
00234     if (nbyte_max == 0) return NULL;
00235 
00236     rest = nbyte_max;
00237     p = *ptr;
00238     imeinfo = NULL;
00239     imeinfo_first = NULL;
00240     imeinfo_last = NULL;
00241 
00242     if ((*nbyte < rest) || (rest < IMEINFO_OBJECT_SIZE)) {
00243        data_s->status = IIIMP_DATA_INVALID;
00244        return NULL;
00245     }
00246 
00247     while (0 < rest) {
00248        imeinfo = iiimp_imeinfo_unpack(data_s, &rest, &p, rest);
00249        if (NULL == imeinfo) {
00250           /* imeinfo is optional, so it is ok if imeinfo is NULL */
00251           break;
00252        } else {
00253            if (NULL == imeinfo_first) {
00254               imeinfo_first = imeinfo;
00255            } else {
00256               imeinfo_last->next = imeinfo;
00257            }
00258            imeinfo_last = imeinfo;
00259        }
00260     }
00261 
00262     *nbyte -= (nbyte_max - rest);
00263     *ptr = p;
00264 
00265     return imeinfo_first;
00266 }
00267 
00268 /* Local Variables: */
00269 /* c-file-style: "iiim-project" */
00270 /* End: */