Back to index

im-sdk  12.3.91
comp-text.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_text *
00012 iiimp_text_new(
00013     IIIMP_data_s *          data_s,
00014     IIIMP_char_with_feedback *     cwf,
00015     IIIMP_annotation *             anno)
00016 {
00017     IIIMP_text *     text;
00018 
00019     text = (IIIMP_text *)malloc(sizeof (IIIMP_text));
00020     if (NULL == text) {
00021        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00022        return NULL;
00023     }
00024 
00025     text->nbyte = 4;
00026 
00027     text->char_with_feedback_nbyte = 0;
00028     text->char_with_feedback = cwf;
00029     for (; NULL != cwf; cwf = cwf->next) {
00030        text->char_with_feedback_nbyte += cwf->nbyte;
00031     }
00032     text->nbyte += text->char_with_feedback_nbyte;
00033 
00034     text->nbyte += 4;
00035 
00036     text->annotation_nbyte = 0;
00037     text->annotation = anno;
00038     for (; NULL != anno; anno = anno->next) {
00039        text->annotation_nbyte += anno->nbyte;
00040     }
00041     text->nbyte += text->annotation_nbyte;
00042 
00043     text->next = NULL;
00044 
00045     return text;
00046 }
00047 
00048 
00049 void
00050 iiimp_text_delete(IIIMP_data_s * data_s, IIIMP_text * text)
00051 {
00052     if (NULL != text) {
00053        iiimp_char_with_feedback_list_delete(data_s, text->char_with_feedback);
00054        iiimp_annotation_delete(data_s, text->annotation);
00055        free(text);
00056     }
00057     return;
00058 }
00059 
00060 
00061 void
00062 iiimp_text_list_delete(IIIMP_data_s * data_s, IIIMP_text * text)
00063 {
00064     IIIMP_text *     text_cur;
00065     IIIMP_text *     text_next;
00066     for (text_cur = text; NULL != text_cur; text_cur = text_next) {
00067        text_next = text_cur->next;
00068        iiimp_char_with_feedback_list_delete(data_s,
00069                                         text_cur->char_with_feedback);
00070        iiimp_annotation_delete(data_s, text_cur->annotation);
00071        free(text_cur);
00072     }
00073     return;
00074 }
00075 
00076 
00077 void
00078 iiimp_text_pack(
00079     IIIMP_data_s *   data_s,
00080     IIIMP_text *     m,
00081     size_t *         nbyte,
00082     uchar_t **              ptr)
00083 {
00084     uchar_t *               p;
00085     size_t                  rest;
00086     IIIMP_char_with_feedback *     cwf;
00087     IIIMP_annotation *             anno;
00088     size_t                  n;
00089 
00090     rest = *nbyte;
00091     p = *ptr;
00092 
00093     n = 0;
00094     cwf = ((NULL == m) ? NULL : m->char_with_feedback);
00095     for (; NULL != cwf; cwf = cwf->next) {
00096        n += cwf->nbyte;
00097     }
00098     PUT32(n, rest, p, data_s->byte_swap);
00099     if (0 < n) {
00100        iiimp_char_with_feedback_list_pack(data_s, m->char_with_feedback,
00101                                       &rest, &p);
00102     }
00103 
00104     n = 0;
00105     anno = ((NULL == m) ? NULL : m->annotation);
00106     for (; NULL != anno; anno = anno->next) {
00107        n += anno->nbyte;
00108     }
00109     PUT32(n, rest, p, data_s->byte_swap);
00110     if (0 < n) {
00111        iiimp_annotation_list_pack(data_s, m->annotation, &rest, &p);
00112     }
00113 
00114     *nbyte = rest;
00115     *ptr = p;
00116 
00117     return;
00118 }
00119 
00120 
00121 void
00122 iiimp_text_list_pack(
00123     IIIMP_data_s *   data_s,
00124     IIIMP_text *     text,
00125     size_t *         nbyte,
00126     uchar_t **              ptr)
00127 {
00128     size_t           rest;
00129     uchar_t *        p;
00130 
00131     rest = *nbyte;
00132     p = *ptr;
00133 
00134     for (; NULL != text; text = text->next) {
00135        iiimp_text_pack(data_s, text, &rest, &p);
00136     }
00137 
00138     *nbyte = rest;
00139     *ptr = p;
00140 
00141     return;
00142 }
00143 
00144 
00145 IIIMP_text *
00146 iiimp_text_unpack(
00147     IIIMP_data_s *   data_s,
00148     size_t *         nbyte,
00149     const uchar_t ** ptr,
00150     size_t           nbyte_max)
00151 {
00152     IIIMP_text *     text;
00153     const uchar_t *  p;
00154     size_t           rest;
00155     int                     data_size;
00156 
00157     rest = nbyte_max;
00158     p = *ptr;
00159 
00160     if ((*nbyte < rest) || (rest < (4 + 0 + 4 + 0))) {
00161        data_s->status = IIIMP_DATA_INVALID;
00162        return NULL;
00163     }
00164 
00165     text = (IIIMP_text *)malloc(sizeof (IIIMP_text));
00166     if (NULL == text) {
00167        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00168        return NULL;
00169     }
00170 
00171     text->char_with_feedback = NULL;
00172     text->annotation = NULL;
00173     text->next = NULL;
00174 
00175     GET32(data_size, rest, p, data_s->byte_swap);
00176     if ((data_size < 0) || (rest < (data_size + 4))) {
00177        iiimp_text_delete(data_s, text);
00178        data_s->status = IIIMP_DATA_INVALID;
00179        return NULL;
00180     }
00181 
00182     if (0 < data_size) {
00183        text->char_with_feedback =
00184            iiimp_char_with_feedback_list_unpack(data_s, &rest, &p, data_size);
00185        if ((NULL == text->char_with_feedback) || (rest < 4)) {
00186            iiimp_text_delete(data_s, text);
00187            return NULL;
00188        }
00189     }
00190 
00191     GET32(data_size, rest, p, data_s->byte_swap);
00192     if ((data_size < 0) || (rest < data_size)) {
00193        iiimp_text_delete(data_s, text);
00194        data_s->status = IIIMP_DATA_INVALID;
00195        return NULL;
00196     }
00197 
00198     if (0 < data_size) {
00199        text->annotation =
00200            iiimp_annotation_unpack(data_s, &rest, &p, data_size);
00201        if (NULL == text->annotation) {
00202            iiimp_text_delete(data_s, text);
00203            return NULL;
00204        }
00205     }
00206 
00207     *nbyte = rest;
00208     *ptr = p;
00209 
00210     return text;
00211 }
00212 
00213 
00214 IIIMP_text *
00215 iiimp_text_list_unpack(
00216     IIIMP_data_s *   data_s,
00217     size_t *         nbyte,
00218     const uchar_t ** ptr,
00219     size_t           nbyte_max)
00220 {
00221     IIIMP_text *     text;
00222     size_t           rest;
00223     const uchar_t *  p;
00224     IIIMP_text *     text_first;
00225     IIIMP_text *     text_last;
00226 
00227     rest = nbyte_max;
00228     p = *ptr;
00229     text_first = NULL;
00230     text_last = NULL;
00231 
00232     if (((*nbyte) < nbyte_max) || (0 != (rest & 0x01)) || (0 == rest)) {
00233        data_s->status = IIIMP_DATA_INVALID;
00234        return NULL;
00235     }
00236 
00237     while (0 < rest) {
00238        text = iiimp_text_unpack(data_s, &rest, &p, rest);
00239        if (NULL == text) {
00240            iiimp_text_list_delete(data_s, text_first);
00241            return NULL;
00242        } else {
00243            if (NULL == text_first) {
00244               text_first = text;
00245            } else {
00246               text_last->next = text;
00247            }
00248            text_last = text;
00249        }
00250     }
00251 
00252     *nbyte -= (nbyte_max - rest);
00253     *ptr = p;
00254 
00255     return text_first;
00256 }
00257 
00258 
00259 void
00260 iiimp_text_print(
00261     IIIMP_data_s *   data_s,
00262     IIIMP_text *     m)
00263 {
00264     if (NULL == m) return;
00265     iiimp_char_with_feedback_list_print(data_s, m->char_with_feedback);
00266     iiimp_annotation_print(data_s, m->annotation);
00267 }
00268 
00269 
00270 void
00271 iiimp_text_list_print(
00272     IIIMP_data_s *   data_s,
00273     IIIMP_text *     m)
00274 {
00275     for (; NULL != m; m = m->next) {
00276        iiimp_text_print(data_s, m);
00277     }
00278 }
00279 
00280 
00281 /* Local Variables: */
00282 /* c-file-style: "iiim-project" */
00283 /* End: */