From 4d9d48ca6d605ff7e38863233b1a3f7184ca8ce2 Mon Sep 17 00:00:00 2001 From: Stefan Pöschel Date: Tue, 2 Jun 2015 01:02:17 +0200 Subject: Data Groups refactoring: Data Group Length Indicator --- src/mot-encoder.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 18 deletions(-) (limited to 'src/mot-encoder.cpp') diff --git a/src/mot-encoder.cpp b/src/mot-encoder.cpp index afcbd86..514bb4f 100644 --- a/src/mot-encoder.cpp +++ b/src/mot-encoder.cpp @@ -238,9 +238,41 @@ void writeMotPAD(int output_fd, void create_dls_pads(const std::string& text, const int padlen, const uint8_t charset); void writeDLS(int output_fd, const std::string& dls_file, int padlen, uint8_t charset, bool raw_dls); +// PAD related +#define CRC_LEN 2 +typedef std::vector uint8_vector_t; + +struct DATA_GROUP { + uint8_vector_t data; + int apptype_start; + int apptype_cont; + size_t written; + + DATA_GROUP(size_t len, int apptype_start, int apptype_cont) { + this->data.resize(len); + this->apptype_start = apptype_start; + this->apptype_cont = apptype_cont; + written = 0; + } + + size_t Available() { + return data.size() - written; + } + + size_t WriteReversed(uint8_t *write_data, size_t len) { + size_t written_now = std::min(len, Available()); + + for(size_t off = 0; off < written_now; off++) + write_data[-off] = data[written + off]; + + written += written_now; + return written_now; + } +}; int get_xpadlengthmask(int padlen); size_t get_xpadlength(int mask); + #define SHORT_PAD 6 #define ALLOWED_PADLEN "6 (short X-PAD; only DLS), 23, 26, 34, 42, 58" @@ -254,12 +286,11 @@ static int cindex_body = 0; #define FPAD_LEN 2 #define DLS_SEG_LEN_PREFIX 2 #define DLS_SEG_LEN_CHAR_MAX 16 -#define DLS_SEG_LEN_CRC 2 -#define DLS_SEG_LEN_MAX (DLS_SEG_LEN_PREFIX + DLS_SEG_LEN_CHAR_MAX + DLS_SEG_LEN_CRC) +#define DLS_SEG_LEN_MAX (DLS_SEG_LEN_PREFIX + DLS_SEG_LEN_CHAR_MAX + CRC_LEN) CharsetConverter charset_converter; -typedef std::vector pad_t; +typedef uint8_vector_t pad_t; static std::deque dls_pads; static bool dls_toggle = false; std::string dlstext_prev = ""; @@ -562,6 +593,33 @@ int main(int argc, char *argv[]) return 1; } + +void appendCRC(uint8_vector_t &data) { + uint16_t crc = 0xFFFF; + for(size_t i = 0; i < data.size() - CRC_LEN; i++) + crc = update_crc_ccitt(crc, data[i]); + crc = ~crc; + + data[data.size() - 2] = (crc & 0xFF00) >> 8; + data[data.size() - 1] = (crc & 0x00FF); +} + + +DATA_GROUP createDataGroupLengthIndicator(size_t len) { + DATA_GROUP dg(4, 1, -1); // continuation never used + uint8_vector_t &data = dg.data; + + // Data Group length + data[0] = (len & 0x3F00) >> 8; + data[1] = (len & 0x00FF); + + // CRC + appendCRC(data); + + return dg; +} + + // Resize the image or add a black border around it // so that it is 320x240 pixels. // Automatically reduce the quality to make sure the @@ -1028,7 +1086,7 @@ size_t dls_get(const std::string& text, const uint8_t charset, const unsigned in int seg_text_offset = seg_index * DLS_SEG_LEN_CHAR_MAX; const char *seg_text_start = text.c_str() + seg_text_offset; size_t seg_text_len = MIN(text.size() - seg_text_offset, DLS_SEG_LEN_CHAR_MAX); - size_t seg_len = DLS_SEG_LEN_PREFIX + seg_text_len + DLS_SEG_LEN_CRC; + size_t seg_len = DLS_SEG_LEN_PREFIX + seg_text_len + CRC_LEN; // prefix: toggle? + first seg? + last seg? + (seg len - 1) @@ -1046,7 +1104,7 @@ size_t dls_get(const std::string& text, const uint8_t charset, const unsigned in // CRC uint16_t crc = 0xFFFF; - for (int i = 0; i < seg_len - DLS_SEG_LEN_CRC; i++) + for (int i = 0; i < seg_len - CRC_LEN; i++) crc = update_crc_ccitt(crc, seg_data[i]); crc = ~crc; #if DEBUG @@ -1143,6 +1201,7 @@ void create_dls_pads(const std::string& text, const int padlen, const uint8_t ch #endif } + void writeMotPAD(int output_fd, unsigned char* mscdg, unsigned short int mscdgsize, @@ -1151,7 +1210,6 @@ void writeMotPAD(int output_fd, unsigned char pad[padlen + 1]; int xpadlengthmask, i, j, k; - unsigned short int crc; xpadlengthmask = get_xpadlengthmask(padlen); @@ -1188,23 +1246,16 @@ void writeMotPAD(int output_fd, pad[padlen-2] = 0x20; // Write Data Group Length Indicator - crc = 0xffff; + DATA_GROUP dgli = createDataGroupLengthIndicator(mscdgsize); + // CI for data group length indicator: data length=4, Application Type=1 - pad[padlen-3]=0x01; + pad[padlen-3]=(0<<5) | dgli.apptype_start; // CI for data group length indicator: Application Type=12 (Start of MOT) pad[padlen-4]=(xpadlengthmask<<5) | 12; // End of CI list pad[padlen-5]=0x00; - // RFA+HI Data group length - pad[padlen-6]=(mscdgsize & 0x3F00)>>8; - pad[padlen-7]=(mscdgsize & 0x00FF); - crc = update_crc_ccitt(crc, pad[padlen-6]); - crc = update_crc_ccitt(crc, pad[padlen-7]); - crc = ~crc; - // HI CRC - pad[padlen-8]=(crc & 0xFF00) >> 8; - // LO CRC - pad[padlen-9]=(crc & 0x00FF); + + dgli.WriteReversed(&pad[padlen-6], 4); k=10; } else { -- cgit v1.2.3 From 8316cd67ee1f5745f4f540a1edf6bec158737295 Mon Sep 17 00:00:00 2001 From: Stefan Pöschel Date: Tue, 2 Jun 2015 11:44:07 +0200 Subject: Data Groups refactoring: MSC Data Group --- src/mot-encoder.cpp | 80 +++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 48 deletions(-) (limited to 'src/mot-encoder.cpp') diff --git a/src/mot-encoder.cpp b/src/mot-encoder.cpp index 514bb4f..356a54c 100644 --- a/src/mot-encoder.cpp +++ b/src/mot-encoder.cpp @@ -229,10 +229,10 @@ void createMscDG(MSCDG* msc, unsigned short int dgtype, int *cindex, unsigned sh unsigned short int lastseg, unsigned short int tid, unsigned char* data, unsigned short int datalen); -void packMscDG(unsigned char* mscblob, MSCDG* msc, unsigned short int *bsize); +struct DATA_GROUP; +DATA_GROUP packMscDG(MSCDG* msc); void writeMotPAD(int output_fd, - unsigned char* mscdg, - unsigned short int mscdgsize, + DATA_GROUP &mscdg, unsigned short int padlen); void create_dls_pads(const std::string& text, const int padlen, const uint8_t charset); @@ -696,8 +696,6 @@ int encodeFile(int output_fd, std::string& fname, int fidx, int padlen, bool raw unsigned char *blob = NULL; unsigned char *curseg = NULL; MSCDG msc; - unsigned char mscblob[9 + MAXSEGLEN + 2]; // headers + segment + CRC - unsigned short int mscblobsize; size_t orig_quality; char* orig_format = NULL; @@ -865,8 +863,8 @@ int encodeFile(int output_fd, std::string& fname, int fidx, int padlen, bool raw // Create the MSC Data Group C-Structure createMscDG(&msc, 3, &cindex_header, 0, 1, fidx, mothdr, mothdrlen); // Generate the MSC DG frame (Figure 9 en 300 401) - packMscDG(mscblob, &msc, &mscblobsize); - writeMotPAD(output_fd, mscblob, mscblobsize, padlen); + DATA_GROUP mscdg = packMscDG(&msc); + writeMotPAD(output_fd, mscdg, padlen); for (i = 0; i < nseg; i++) { curseg = blob + i * MAXSEGLEN; @@ -880,8 +878,8 @@ int encodeFile(int output_fd, std::string& fname, int fidx, int padlen, bool raw } createMscDG(&msc, 4, &cindex_body, i, last, fidx, curseg, curseglen); - packMscDG(mscblob, &msc, &mscblobsize); - writeMotPAD(output_fd, mscblob, mscblobsize, padlen); + mscdg = packMscDG(&msc); + writeMotPAD(output_fd, mscdg, padlen); } ret = 1; @@ -967,11 +965,12 @@ void createMscDG(MSCDG* msc, unsigned short int dgtype, } -void packMscDG(unsigned char* b, MSCDG* msc, unsigned short int* bsize) +DATA_GROUP packMscDG(MSCDG* msc) { - int i; - unsigned short int crc=0xFFFF; + DATA_GROUP dg(9 + msc->seglen + CRC_LEN, 12, 13); + uint8_vector_t &b = dg.data; + // headers b[0] = (msc->extflag<<7) | (msc->crcflag<<6) | (msc->segflag<<5) | (msc->accflag<<4) | msc->dgtype; @@ -985,22 +984,13 @@ void packMscDG(unsigned char* b, MSCDG* msc, unsigned short int* bsize) b[7] = (msc->rcount << 5) | ((msc->seglen & 0x1F00)>>8); b[8] = msc->seglen & 0x00FF; - for (i = 0; i<9; i++) { - crc = update_crc_ccitt(crc, b[i]); - } - - for(i = 0; i < msc->seglen; i++) { - b[i+9] = (msc->segdata)[i]; - crc = update_crc_ccitt(crc, b[i+9]); - } + // data field + memcpy(&b[9], msc->segdata, msc->seglen); - crc = ~crc; - b[9+msc->seglen] = (crc & 0xFF00) >> 8; // HI CRC - b[9+msc->seglen+1] = crc & 0x00FF; // LO CRC - - *bsize = 9 + msc->seglen + 2; + // CRC + appendCRC(b); - //write(1,b,9+msc->seglen+1+1); + return dg; } @@ -1203,42 +1193,36 @@ void create_dls_pads(const std::string& text, const int padlen, const uint8_t ch void writeMotPAD(int output_fd, - unsigned char* mscdg, - unsigned short int mscdgsize, + DATA_GROUP &mscdg, unsigned short int padlen) { unsigned char pad[padlen + 1]; - int xpadlengthmask, i, j, k; + int xpadlengthmask, k; + bool firstseg = true; xpadlengthmask = get_xpadlengthmask(padlen); // Write MSC Data Groups int curseglen, used_xpad_len; - for (i = 0; i < mscdgsize; i += curseglen) { - uint8_t* curseg; - uint8_t firstseg; - - curseg = &mscdg[i]; + while (mscdg.Available()) { #if DEBUG fprintf(stderr,"Segment offset %d\n",i); #endif - if (i == 0) { // First segment - firstseg = 1; + if (firstseg) { // First segment curseglen = get_xpadlength(xpadlengthmask); // size of first X-PAD = MSC-DG + DGLI-DG + End of CI list + 2x CI = size of subsequent non-CI X-PADs used_xpad_len = curseglen + 4 + 1 + 2; } else { - firstseg = 0; curseglen = used_xpad_len; } - curseglen = MIN(curseglen, mscdgsize - i); + curseglen = MIN(curseglen, mscdg.Available()); - if (firstseg == 1) { + if (firstseg) { // FF-PAD Byte L (CI=1) pad[padlen-1] = 0x02; @@ -1246,12 +1230,12 @@ void writeMotPAD(int output_fd, pad[padlen-2] = 0x20; // Write Data Group Length Indicator - DATA_GROUP dgli = createDataGroupLengthIndicator(mscdgsize); + DATA_GROUP dgli = createDataGroupLengthIndicator(mscdg.data.size()); // CI for data group length indicator: data length=4, Application Type=1 pad[padlen-3]=(0<<5) | dgli.apptype_start; - // CI for data group length indicator: Application Type=12 (Start of MOT) - pad[padlen-4]=(xpadlengthmask<<5) | 12; + // CI for MOT, start of X-PAD data group: Application Type=12 + pad[padlen-4]=(xpadlengthmask<<5) | mscdg.apptype_start; // End of CI list pad[padlen-5]=0x00; @@ -1267,12 +1251,9 @@ void writeMotPAD(int output_fd, k=3; } - for (j = 0; j < curseglen; j++) { - pad[padlen-k-j] = curseg[j]; - } - for (j = padlen-k-curseglen; j >= 0; j--) { - pad[j] = 0x00; - } + mscdg.WriteReversed(&pad[padlen-k], curseglen); + + memset(pad, 0x00, padlen-k-curseglen + 1); // set used pad len pad[padlen] = FPAD_LEN + used_xpad_len; @@ -1284,6 +1265,9 @@ void writeMotPAD(int output_fd, fprintf(stderr,"%02x ",pad[j]); fprintf(stderr,"\n"); #endif + + if (firstseg) + firstseg = false; } } -- cgit v1.2.3 From 7d791ff4755da7bfb2e3f4a3aecbe8ad0630d365 Mon Sep 17 00:00:00 2001 From: Stefan Pöschel Date: Tue, 2 Jun 2015 11:46:02 +0200 Subject: Data Groups refactoring: move CRC attachment to DATA_GROUP --- src/mot-encoder.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'src/mot-encoder.cpp') diff --git a/src/mot-encoder.cpp b/src/mot-encoder.cpp index 356a54c..91362e1 100644 --- a/src/mot-encoder.cpp +++ b/src/mot-encoder.cpp @@ -255,6 +255,16 @@ struct DATA_GROUP { written = 0; } + void AppendCRC() { + uint16_t crc = 0xFFFF; + for (size_t i = 0; i < data.size(); i++) + crc = update_crc_ccitt(crc, data[i]); + crc = ~crc; + + data.push_back((crc & 0xFF00) >> 8); + data.push_back((crc & 0x00FF)); + } + size_t Available() { return data.size() - written; } @@ -262,7 +272,7 @@ struct DATA_GROUP { size_t WriteReversed(uint8_t *write_data, size_t len) { size_t written_now = std::min(len, Available()); - for(size_t off = 0; off < written_now; off++) + for (size_t off = 0; off < written_now; off++) write_data[-off] = data[written + off]; written += written_now; @@ -594,19 +604,8 @@ int main(int argc, char *argv[]) } -void appendCRC(uint8_vector_t &data) { - uint16_t crc = 0xFFFF; - for(size_t i = 0; i < data.size() - CRC_LEN; i++) - crc = update_crc_ccitt(crc, data[i]); - crc = ~crc; - - data[data.size() - 2] = (crc & 0xFF00) >> 8; - data[data.size() - 1] = (crc & 0x00FF); -} - - DATA_GROUP createDataGroupLengthIndicator(size_t len) { - DATA_GROUP dg(4, 1, -1); // continuation never used + DATA_GROUP dg(2, 1, -1); // continuation never used uint8_vector_t &data = dg.data; // Data Group length @@ -614,7 +613,7 @@ DATA_GROUP createDataGroupLengthIndicator(size_t len) { data[1] = (len & 0x00FF); // CRC - appendCRC(data); + dg.AppendCRC(); return dg; } @@ -967,7 +966,7 @@ void createMscDG(MSCDG* msc, unsigned short int dgtype, DATA_GROUP packMscDG(MSCDG* msc) { - DATA_GROUP dg(9 + msc->seglen + CRC_LEN, 12, 13); + DATA_GROUP dg(9 + msc->seglen, 12, 13); uint8_vector_t &b = dg.data; // headers @@ -988,7 +987,7 @@ DATA_GROUP packMscDG(MSCDG* msc) memcpy(&b[9], msc->segdata, msc->seglen); // CRC - appendCRC(b); + dg.AppendCRC(); return dg; } -- cgit v1.2.3 From 26d3d3435e4417bb4b8b3015fd0fd35b36104863 Mon Sep 17 00:00:00 2001 From: Stefan Pöschel Date: Tue, 2 Jun 2015 17:53:48 +0200 Subject: Data Groups refactoring: DLS Data Group --- src/mot-encoder.cpp | 54 +++++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 31 deletions(-) (limited to 'src/mot-encoder.cpp') diff --git a/src/mot-encoder.cpp b/src/mot-encoder.cpp index 91362e1..a8efbaf 100644 --- a/src/mot-encoder.cpp +++ b/src/mot-encoder.cpp @@ -235,7 +235,7 @@ void writeMotPAD(int output_fd, DATA_GROUP &mscdg, unsigned short int padlen); -void create_dls_pads(const std::string& text, const int padlen, const uint8_t charset); +void create_dls_pads(const std::string& text, int padlen, uint8_t charset); void writeDLS(int output_fd, const std::string& dls_file, int padlen, uint8_t charset, bool raw_dls); // PAD related @@ -260,6 +260,9 @@ struct DATA_GROUP { for (size_t i = 0; i < data.size(); i++) crc = update_crc_ccitt(crc, data[i]); crc = ~crc; +#if DEBUG + fprintf(stderr, "crc=%04x ~crc=%04x\n", crc, ~crc); +#endif data.push_back((crc & 0xFF00) >> 8); data.push_back((crc & 0x00FF)); @@ -296,7 +299,6 @@ static int cindex_body = 0; #define FPAD_LEN 2 #define DLS_SEG_LEN_PREFIX 2 #define DLS_SEG_LEN_CHAR_MAX 16 -#define DLS_SEG_LEN_MAX (DLS_SEG_LEN_PREFIX + DLS_SEG_LEN_CHAR_MAX + CRC_LEN) CharsetConverter charset_converter; @@ -1064,19 +1066,16 @@ int dls_count(const std::string& text) { } -size_t dls_get(const std::string& text, const uint8_t charset, const unsigned int seg_index, uint8_t *seg_data) { - int seg_count = dls_count(text); - if (seg_index >= seg_count) - return 0; - +DATA_GROUP dls_get(const std::string& text, uint8_t charset, unsigned int seg_index) { bool first_seg = seg_index == 0; - bool last_seg = seg_index == seg_count - 1; + bool last_seg = seg_index == dls_count(text) - 1; int seg_text_offset = seg_index * DLS_SEG_LEN_CHAR_MAX; const char *seg_text_start = text.c_str() + seg_text_offset; size_t seg_text_len = MIN(text.size() - seg_text_offset, DLS_SEG_LEN_CHAR_MAX); - size_t seg_len = DLS_SEG_LEN_PREFIX + seg_text_len + CRC_LEN; + DATA_GROUP dg(DLS_SEG_LEN_PREFIX + seg_text_len, 2, 3); + uint8_vector_t &seg_data = dg.data; // prefix: toggle? + first seg? + last seg? + (seg len - 1) seg_data[0] = @@ -1089,31 +1088,22 @@ size_t dls_get(const std::string& text, const uint8_t charset, const unsigned in seg_data[1] = (first_seg ? charset : seg_index) << 4; // character field - memcpy(seg_data + DLS_SEG_LEN_PREFIX, seg_text_start, seg_text_len); + memcpy(&seg_data[DLS_SEG_LEN_PREFIX], seg_text_start, seg_text_len); // CRC - uint16_t crc = 0xFFFF; - for (int i = 0; i < seg_len - CRC_LEN; i++) - crc = update_crc_ccitt(crc, seg_data[i]); - crc = ~crc; -#if DEBUG - fprintf(stderr, "crc=%04x ~crc=%04x\n", ~crc, crc); -#endif - seg_data[seg_len - 2] = (crc & 0xFF00) >> 8; // HI CRC - seg_data[seg_len - 1] = crc & 0x00FF; // LO CRC + dg.AppendCRC(); #if DEBUG fprintf(stderr, "DL segment:"); - for (int i = 0; i < seg_len; i++) + for (int i = 0; i < seg_data.size(); i++) fprintf(stderr, " %02x", seg_data[i]); fprintf(stderr, "\n"); #endif - return seg_len; + return dg; } -void create_dls_pads(const std::string& text, const int padlen, const uint8_t charset) { - uint8_t seg_data[DLS_SEG_LEN_MAX]; +void create_dls_pads(const std::string& text, int padlen, uint8_t charset) { int xpadlengthmask = get_xpadlengthmask(padlen); dls_pads.clear(); @@ -1125,18 +1115,18 @@ void create_dls_pads(const std::string& text, const int padlen, const uint8_t ch #if DEBUG fprintf(stderr, "Segment number %d\n", seg_index + 1); #endif - int seg_len = dls_get(text, charset, seg_index, seg_data); + DATA_GROUP seg = dls_get(text, charset, seg_index); + bool ci_needed = true; // CI needed only at first data group int dg_len; int used_xpad_len; // distribute the segment over one or more PADs - for (int seg_off = 0; seg_off < seg_len;) { + while (seg.Available()) { dls_pads.push_back(pad_t(padlen + 1)); pad_t &pad = dls_pads.back(); int pad_off = padlen - 1; bool var_size_pad = padlen != SHORT_PAD; - bool ci_needed = seg_off == 0; // CI needed only at first data group if (ci_needed) { dg_len = get_xpadlength(xpadlengthmask); @@ -1146,7 +1136,7 @@ void create_dls_pads(const std::string& text, const int padlen, const uint8_t ch } else { dg_len = used_xpad_len; } - int dg_used = MIN(dg_len, seg_len - seg_off); + int dg_used = MIN(dg_len, seg.Available()); // F-PAD Byte L (CI if needed) @@ -1157,15 +1147,15 @@ void create_dls_pads(const std::string& text, const int padlen, const uint8_t ch // CI (app type 2 = DLS, start of X-PAD data group) if (ci_needed) - pad[pad_off--] = ((var_size_pad ? xpadlengthmask : 0) << 5) | 0x02; + pad[pad_off--] = ((var_size_pad ? xpadlengthmask : 0) << 5) | seg.apptype_start; // CI end marker if (ci_needed && var_size_pad) pad[pad_off--] = 0x00; // segment (part) - for (int i = 0; i < dg_used; i++) - pad[pad_off--] = seg_data[seg_off + i]; + seg.WriteReversed(&pad[pad_off], dg_used); + pad_off -= dg_used; // NULL PADDING std::fill_n(pad.begin(), pad_off + 1, 0x00); @@ -1179,7 +1169,9 @@ void create_dls_pads(const std::string& text, const int padlen, const uint8_t ch fprintf(stderr, " %02x", pad[i]); fprintf(stderr, "\n"); #endif - seg_off += dg_used; + + if (ci_needed) + ci_needed = false; } } #if DEBUG -- cgit v1.2.3 From 2b21db186bba5aca456666252026686badc12fa2 Mon Sep 17 00:00:00 2001 From: Stefan Pöschel Date: Fri, 5 Jun 2015 14:57:39 +0200 Subject: Data Groups refactoring: prevent unnecessary copies --- src/mot-encoder.cpp | 63 +++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 28 deletions(-) (limited to 'src/mot-encoder.cpp') diff --git a/src/mot-encoder.cpp b/src/mot-encoder.cpp index a8efbaf..ce61e9f 100644 --- a/src/mot-encoder.cpp +++ b/src/mot-encoder.cpp @@ -230,9 +230,9 @@ void createMscDG(MSCDG* msc, unsigned short int dgtype, int *cindex, unsigned sh unsigned short int datalen); struct DATA_GROUP; -DATA_GROUP packMscDG(MSCDG* msc); +DATA_GROUP* packMscDG(MSCDG* msc); void writeMotPAD(int output_fd, - DATA_GROUP &mscdg, + DATA_GROUP* mscdg, unsigned short int padlen); void create_dls_pads(const std::string& text, int padlen, uint8_t charset); @@ -606,16 +606,16 @@ int main(int argc, char *argv[]) } -DATA_GROUP createDataGroupLengthIndicator(size_t len) { - DATA_GROUP dg(2, 1, -1); // continuation never used - uint8_vector_t &data = dg.data; +DATA_GROUP* createDataGroupLengthIndicator(size_t len) { + DATA_GROUP* dg = new DATA_GROUP(2, 1, -1); // continuation never used + uint8_vector_t &data = dg->data; // Data Group length data[0] = (len & 0x3F00) >> 8; data[1] = (len & 0x00FF); // CRC - dg.AppendCRC(); + dg->AppendCRC(); return dg; } @@ -697,6 +697,7 @@ int encodeFile(int output_fd, std::string& fname, int fidx, int padlen, bool raw unsigned char *blob = NULL; unsigned char *curseg = NULL; MSCDG msc; + DATA_GROUP* mscdg; size_t orig_quality; char* orig_format = NULL; @@ -864,8 +865,9 @@ int encodeFile(int output_fd, std::string& fname, int fidx, int padlen, bool raw // Create the MSC Data Group C-Structure createMscDG(&msc, 3, &cindex_header, 0, 1, fidx, mothdr, mothdrlen); // Generate the MSC DG frame (Figure 9 en 300 401) - DATA_GROUP mscdg = packMscDG(&msc); + mscdg = packMscDG(&msc); writeMotPAD(output_fd, mscdg, padlen); + delete mscdg; for (i = 0; i < nseg; i++) { curseg = blob + i * MAXSEGLEN; @@ -881,6 +883,7 @@ int encodeFile(int output_fd, std::string& fname, int fidx, int padlen, bool raw createMscDG(&msc, 4, &cindex_body, i, last, fidx, curseg, curseglen); mscdg = packMscDG(&msc); writeMotPAD(output_fd, mscdg, padlen); + delete mscdg; } ret = 1; @@ -966,10 +969,10 @@ void createMscDG(MSCDG* msc, unsigned short int dgtype, } -DATA_GROUP packMscDG(MSCDG* msc) +DATA_GROUP* packMscDG(MSCDG* msc) { - DATA_GROUP dg(9 + msc->seglen, 12, 13); - uint8_vector_t &b = dg.data; + DATA_GROUP* dg = new DATA_GROUP(9 + msc->seglen, 12, 13); + uint8_vector_t &b = dg->data; // headers b[0] = (msc->extflag<<7) | (msc->crcflag<<6) | (msc->segflag<<5) | @@ -989,7 +992,7 @@ DATA_GROUP packMscDG(MSCDG* msc) memcpy(&b[9], msc->segdata, msc->seglen); // CRC - dg.AppendCRC(); + dg->AppendCRC(); return dg; } @@ -1066,7 +1069,7 @@ int dls_count(const std::string& text) { } -DATA_GROUP dls_get(const std::string& text, uint8_t charset, unsigned int seg_index) { +DATA_GROUP* dls_get(const std::string& text, uint8_t charset, unsigned int seg_index) { bool first_seg = seg_index == 0; bool last_seg = seg_index == dls_count(text) - 1; @@ -1074,8 +1077,8 @@ DATA_GROUP dls_get(const std::string& text, uint8_t charset, unsigned int seg_in const char *seg_text_start = text.c_str() + seg_text_offset; size_t seg_text_len = MIN(text.size() - seg_text_offset, DLS_SEG_LEN_CHAR_MAX); - DATA_GROUP dg(DLS_SEG_LEN_PREFIX + seg_text_len, 2, 3); - uint8_vector_t &seg_data = dg.data; + DATA_GROUP* dg = new DATA_GROUP(DLS_SEG_LEN_PREFIX + seg_text_len, 2, 3); + uint8_vector_t &seg_data = dg->data; // prefix: toggle? + first seg? + last seg? + (seg len - 1) seg_data[0] = @@ -1091,7 +1094,7 @@ DATA_GROUP dls_get(const std::string& text, uint8_t charset, unsigned int seg_in memcpy(&seg_data[DLS_SEG_LEN_PREFIX], seg_text_start, seg_text_len); // CRC - dg.AppendCRC(); + dg->AppendCRC(); #if DEBUG fprintf(stderr, "DL segment:"); @@ -1115,13 +1118,13 @@ void create_dls_pads(const std::string& text, int padlen, uint8_t charset) { #if DEBUG fprintf(stderr, "Segment number %d\n", seg_index + 1); #endif - DATA_GROUP seg = dls_get(text, charset, seg_index); + DATA_GROUP* seg = dls_get(text, charset, seg_index); bool ci_needed = true; // CI needed only at first data group int dg_len; int used_xpad_len; // distribute the segment over one or more PADs - while (seg.Available()) { + while (seg->Available()) { dls_pads.push_back(pad_t(padlen + 1)); pad_t &pad = dls_pads.back(); int pad_off = padlen - 1; @@ -1136,7 +1139,7 @@ void create_dls_pads(const std::string& text, int padlen, uint8_t charset) { } else { dg_len = used_xpad_len; } - int dg_used = MIN(dg_len, seg.Available()); + int dg_used = MIN(dg_len, seg->Available()); // F-PAD Byte L (CI if needed) @@ -1147,14 +1150,14 @@ void create_dls_pads(const std::string& text, int padlen, uint8_t charset) { // CI (app type 2 = DLS, start of X-PAD data group) if (ci_needed) - pad[pad_off--] = ((var_size_pad ? xpadlengthmask : 0) << 5) | seg.apptype_start; + pad[pad_off--] = ((var_size_pad ? xpadlengthmask : 0) << 5) | seg->apptype_start; // CI end marker if (ci_needed && var_size_pad) pad[pad_off--] = 0x00; // segment (part) - seg.WriteReversed(&pad[pad_off], dg_used); + seg->WriteReversed(&pad[pad_off], dg_used); pad_off -= dg_used; // NULL PADDING @@ -1173,6 +1176,8 @@ void create_dls_pads(const std::string& text, int padlen, uint8_t charset) { if (ci_needed) ci_needed = false; } + + delete seg; } #if DEBUG fprintf(stderr, "PAD length: %d\n", padlen); @@ -1184,7 +1189,7 @@ void create_dls_pads(const std::string& text, int padlen, uint8_t charset) { void writeMotPAD(int output_fd, - DATA_GROUP &mscdg, + DATA_GROUP* mscdg, unsigned short int padlen) { @@ -1196,7 +1201,7 @@ void writeMotPAD(int output_fd, // Write MSC Data Groups int curseglen, used_xpad_len; - while (mscdg.Available()) { + while (mscdg->Available()) { #if DEBUG fprintf(stderr,"Segment offset %d\n",i); #endif @@ -1211,7 +1216,7 @@ void writeMotPAD(int output_fd, curseglen = used_xpad_len; } - curseglen = MIN(curseglen, mscdg.Available()); + curseglen = MIN(curseglen, mscdg->Available()); if (firstseg) { // FF-PAD Byte L (CI=1) @@ -1221,16 +1226,18 @@ void writeMotPAD(int output_fd, pad[padlen-2] = 0x20; // Write Data Group Length Indicator - DATA_GROUP dgli = createDataGroupLengthIndicator(mscdg.data.size()); + DATA_GROUP* dgli = createDataGroupLengthIndicator(mscdg->data.size()); // CI for data group length indicator: data length=4, Application Type=1 - pad[padlen-3]=(0<<5) | dgli.apptype_start; + pad[padlen-3]=(0<<5) | dgli->apptype_start; // CI for MOT, start of X-PAD data group: Application Type=12 - pad[padlen-4]=(xpadlengthmask<<5) | mscdg.apptype_start; + pad[padlen-4]=(xpadlengthmask<<5) | mscdg->apptype_start; // End of CI list pad[padlen-5]=0x00; - dgli.WriteReversed(&pad[padlen-6], 4); + dgli->WriteReversed(&pad[padlen-6], 4); + delete dgli; + k=10; } else { @@ -1242,7 +1249,7 @@ void writeMotPAD(int output_fd, k=3; } - mscdg.WriteReversed(&pad[padlen-k], curseglen); + mscdg->WriteReversed(&pad[padlen-k], curseglen); memset(pad, 0x00, padlen-k-curseglen + 1); -- cgit v1.2.3