diff --git a/src/lib_ccx/ccx_decoders_708_output.c b/src/lib_ccx/ccx_decoders_708_output.c index 08446150..d2717ae3 100644 --- a/src/lib_ccx/ccx_decoders_708_output.c +++ b/src/lib_ccx/ccx_decoders_708_output.c @@ -191,13 +191,13 @@ void _dtvcc_write_row(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decoder *d "conversion failed: %s\n", strerror(errno)); - write(fd, encoded_buf_start, encoded_buf - encoded_buf_start); + write_wrapped(fd, encoded_buf_start, encoded_buf - encoded_buf_start); free(encoded_buf_start); } else { - write(fd, buf, buf_len); + write_wrapped(fd, buf, buf_len); } } @@ -221,19 +221,19 @@ void ccx_dtvcc_write_srt(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decoder "%02u:%02u:%02u,%03u", buf + strlen(buf)); sprintf(buf + strlen(buf), "%s", (char *)encoder->encoded_crlf); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); for (int i = 0; i < CCX_DTVCC_SCREENGRID_ROWS; i++) { if (!_dtvcc_is_row_empty(tv, i)) { _dtvcc_write_row(writer, decoder, i, encoder, 1); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, - encoder->encoded_crlf, encoder->encoded_crlf_length); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, + encoder->encoded_crlf, encoder->encoded_crlf_length); } } - write(encoder->dtvcc_writers[tv->service_number - 1].fd, - encoder->encoded_crlf, encoder->encoded_crlf_length); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, + encoder->encoded_crlf, encoder->encoded_crlf_length); } void ccx_dtvcc_write_debug(dtvcc_tv_screen *tv) @@ -289,12 +289,13 @@ void ccx_dtvcc_write_transcript(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_ if (encoder->transcript_settings->showMode) sprintf(buf + strlen(buf), "POP|"); //TODO caption mode(pop, rollup, etc.) - if (strlen(buf)) - write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); + const size_t buf_len = strlen(buf); + if (buf_len != 0) + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, buf_len); _dtvcc_write_row(writer, decoder, i, encoder, 0); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, - encoder->encoded_crlf, encoder->encoded_crlf_length); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, + encoder->encoded_crlf, encoder->encoded_crlf_length); } } } @@ -321,16 +322,16 @@ void _dtvcc_write_sami_header(dtvcc_tv_screen *tv, struct encoder_ctx *encoder) buf_len += sprintf(buf + buf_len, "%s%s", encoder->encoded_crlf, encoder->encoded_crlf); buf_len += sprintf(buf + buf_len, "%s", encoder->encoded_crlf); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, buf_len); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, buf_len); } void _dtvcc_write_sami_footer(dtvcc_tv_screen *tv, struct encoder_ctx *encoder) { char *buf = (char *)encoder->buffer; sprintf(buf, ""); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, - encoder->encoded_crlf, encoder->encoded_crlf_length); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, + encoder->encoded_crlf, encoder->encoded_crlf_length); } void ccx_dtvcc_write_sami(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decoder *decoder, struct encoder_ctx *encoder) @@ -351,24 +352,24 @@ void ccx_dtvcc_write_sami(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decode sprintf(buf, "

%s", (unsigned long long)tv->time_ms_show + encoder->subs_delay, encoder->encoded_crlf); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); for (int i = 0; i < CCX_DTVCC_SCREENGRID_ROWS; i++) { if (!_dtvcc_is_row_empty(tv, i)) { _dtvcc_write_row(writer, decoder, i, encoder, 1); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, - encoder->encoded_br, encoder->encoded_br_length); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, - encoder->encoded_crlf, encoder->encoded_crlf_length); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, + encoder->encoded_br, encoder->encoded_br_length); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, + encoder->encoded_crlf, encoder->encoded_crlf_length); } } sprintf(buf, "

 

%s%s", (unsigned long long)tv->time_ms_hide + encoder->subs_delay, encoder->encoded_crlf, encoder->encoded_crlf); - write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); + write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf)); } void _ccx_dtvcc_write(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decoder *decoder, struct encoder_ctx *encoder) @@ -488,7 +489,7 @@ void ccx_dtvcc_writer_output(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_dec CCX_COMMON_EXIT_FILE_CREATION_FAILED, "[CEA-708] Failed to open a file\n"); } if (!encoder->no_bom) - write(writer->fd, UTF8_BOM, sizeof(UTF8_BOM)); + write_wrapped(writer->fd, UTF8_BOM, sizeof(UTF8_BOM)); } _ccx_dtvcc_write(writer, decoder, encoder); diff --git a/src/lib_ccx/ccx_encoders_g608.c b/src/lib_ccx/ccx_encoders_g608.c index 2316723c..bd23ec6c 100644 --- a/src/lib_ccx/ccx_encoders_g608.c +++ b/src/lib_ccx/ccx_encoders_g608.c @@ -16,25 +16,25 @@ int write_cc_buffer_as_g608(struct eia608_screen *data, struct encoder_ctx *cont context->srt_counter++; sprintf(timeline, "%u%s", context->srt_counter, context->encoded_crlf); used = encode_line(context, context->buffer, (unsigned char *)timeline); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); sprintf(timeline, "%02u:%02u:%02u,%03u --> %02u:%02u:%02u,%03u%s", h1, m1, s1, ms1, h2, m2, s2, ms2, context->encoded_crlf); used = encode_line(context, context->buffer, (unsigned char *)timeline); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); for (int i = 0; i < 15; i++) { int length = get_line_encoded(context, context->subline, i, data); - write(context->out->fh, context->subline, length); + write_wrapped(context->out->fh, context->subline, length); length = get_color_encoded(context, context->subline, i, data); - write(context->out->fh, context->subline, length); + write_wrapped(context->out->fh, context->subline, length); length = get_font_encoded(context, context->subline, i, data); - write(context->out->fh, context->subline, length); - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, context->subline, length); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); wrote_something = 1; } - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); return wrote_something; } diff --git a/src/lib_ccx/ccx_encoders_mcc.c b/src/lib_ccx/ccx_encoders_mcc.c index a936c387..56a6b5f1 100644 --- a/src/lib_ccx/ccx_encoders_mcc.c +++ b/src/lib_ccx/ccx_encoders_mcc.c @@ -156,7 +156,7 @@ boolean mcc_encode_cc_data(struct encoder_ctx *enc_ctx, struct lib_cc_decode *de strcat(compressed_data_buffer, "\n"); - write(enc_ctx->out->fh, compressed_data_buffer, strlen(compressed_data_buffer)); + write_wrapped(enc_ctx->out->fh, compressed_data_buffer, strlen(compressed_data_buffer)); free(compressed_data_buffer); @@ -721,5 +721,5 @@ static void byte_to_ascii(uint8 hex_byte, uint8 *msn, uint8 *lsn) static void write_string(int fh, char *string) { - write(fh, string, strlen(string)); + write_wrapped(fh, string, strlen(string)); } diff --git a/src/lib_ccx/ccx_encoders_sami.c b/src/lib_ccx/ccx_encoders_sami.c index 4b445f6a..eb756749 100644 --- a/src/lib_ccx/ccx_encoders_sami.c +++ b/src/lib_ccx/ccx_encoders_sami.c @@ -24,17 +24,14 @@ int write_stringz_as_sami(char *string, struct encoder_ctx *context, LLONG ms_st used = encode_line(context, context->buffer, (unsigned char *)str); ret = write(context->out->fh, context->buffer, used); if (ret != used) - { return ret; - } len = strlen(string); unescaped = (unsigned char *)malloc(len + 1); - if (!unescaped) + if (unescaped == NULL) { mprint("In write_stringz_as_sami() - not enough memory for len %d.\n", len); - ret = -1; - goto end; + return -1; } el = (unsigned char *)malloc(len * 3 + 1); // Be generous @@ -132,7 +129,7 @@ int write_cc_bitmap_as_sami(struct cc_subtitle *sub, struct encoder_ctx *context { sprintf(buf, "

\r\n", (unsigned long long)sub->start_time); - write(context->out->fh, buf, strlen(buf)); + write_wrapped(context->out->fh, buf, strlen(buf)); for (int i = sub->nb_data - 1; i >= 0; i--) { if (rect[i].ocr_text && *(rect[i].ocr_text)) @@ -142,21 +139,21 @@ int write_cc_bitmap_as_sami(struct cc_subtitle *sub, struct encoder_ctx *context token = strtok(rect[i].ocr_text, "\r\n"); sprintf(buf, "%s", token); token = strtok(NULL, "\r\n"); - write(context->out->fh, buf, strlen(buf)); + write_wrapped(context->out->fh, buf, strlen(buf)); if (i != 0) - write(context->out->fh, context->encoded_br, context->encoded_br_length); - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, context->encoded_br, context->encoded_br_length); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); } } } sprintf(buf, "

\r\n"); - write(context->out->fh, buf, strlen(buf)); + write_wrapped(context->out->fh, buf, strlen(buf)); } else //we write an empty subtitle to clear the old one { sprintf(buf, "

 

\r\n\r\n", (unsigned long long)sub->start_time); - write(context->out->fh, buf, strlen(buf)); + write_wrapped(context->out->fh, buf, strlen(buf)); } #endif @@ -204,7 +201,7 @@ int write_cc_buffer_as_sami(struct eia608_screen *data, struct encoder_ctx *cont dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str); } used = encode_line(context, context->buffer, (unsigned char *)str); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); for (int i = 0; i < 15; i++) { if (data->row_used[i]) @@ -215,11 +212,11 @@ int write_cc_buffer_as_sami(struct eia608_screen *data, struct encoder_ctx *cont dbg_print(CCX_DMT_DECODER_608, "\r"); dbg_print(CCX_DMT_DECODER_608, "%s\n", context->subline); } - write(context->out->fh, context->subline, length); + write_wrapped(context->out->fh, context->subline, length); wrote_something = 1; if (i != 14) - write(context->out->fh, context->encoded_br, context->encoded_br_length); - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, context->encoded_br, context->encoded_br_length); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); } } sprintf((char *)str, "

\r\n"); @@ -228,7 +225,7 @@ int write_cc_buffer_as_sami(struct eia608_screen *data, struct encoder_ctx *cont dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str); } used = encode_line(context, context->buffer, (unsigned char *)str); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); sprintf((char *)str, "

 

\r\n\r\n", (unsigned long long)data->end_time - 1); // - 1 to prevent overlap @@ -237,6 +234,6 @@ int write_cc_buffer_as_sami(struct eia608_screen *data, struct encoder_ctx *cont dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str); } used = encode_line(context, context->buffer, (unsigned char *)str); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); return wrote_something; } diff --git a/src/lib_ccx/ccx_encoders_scc.c b/src/lib_ccx/ccx_encoders_scc.c index 9639b3ad..1bf2cde5 100644 --- a/src/lib_ccx/ccx_encoders_scc.c +++ b/src/lib_ccx/ccx_encoders_scc.c @@ -419,11 +419,12 @@ void add_padding(int fd, const char disassemble) if (disassemble) { - write(fd, "_", 1); + write_wrapped(fd, "_", 1); } else { - write(fd, "80", 2); // 0x80 == odd_parity(0x00) + // 0x80 == odd_parity(0x00) + write_wrapped(fd, "80", 2); } } @@ -440,12 +441,12 @@ void write_character(const int fd, const unsigned char character, const bool dis { if (disassemble) { - write(fd, &character, 1); + write_wrapped(fd, &character, 1); } else { if (*bytes_written % 2 == 0) - write(fd, " ", 1); + write_wrapped(fd, " ", 1); fdprintf(fd, "%02x", odd_parity(character)); } @@ -466,12 +467,12 @@ void write_control_code(const int fd, const unsigned char channel, const enum co { unsigned int length; const char *assembly_code = disassemble_code(code, &length); - write(fd, assembly_code, length); + write_wrapped(fd, assembly_code, length); } else { if (*bytes_written % 2 == 0) - write(fd, " ", 1); + write_wrapped(fd, " ", 1); fdprintf(fd, "%02x%02x", odd_parity(get_first_byte(channel, code)), odd_parity(get_second_byte(code))); } @@ -520,9 +521,9 @@ enum control_code get_font_code(enum font_bits font, enum ccx_decoder_608_color_ void add_timestamp(const struct encoder_ctx *context, LLONG time, const bool disassemble) { - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); if (!disassemble) - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); unsigned hour, minute, second, milli; millis_to_time(time, &hour, &minute, &second, &milli); diff --git a/src/lib_ccx/ccx_encoders_smptett.c b/src/lib_ccx/ccx_encoders_smptett.c index 8daf0cb1..3a7fe9b5 100644 --- a/src/lib_ccx/ccx_encoders_smptett.c +++ b/src/lib_ccx/ccx_encoders_smptett.c @@ -53,7 +53,7 @@ void write_stringz_as_smptett(char *string, struct encoder_ctx *context, LLONG m dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str); } used = encode_line(context, context->buffer, (unsigned char *)str); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); // Scan for \n in the string and replace it with a 0 while (pos_r < len) { @@ -80,10 +80,10 @@ void write_stringz_as_smptett(char *string, struct encoder_ctx *context, LLONG m dbg_print(CCX_DMT_DECODER_608, "\r"); dbg_print(CCX_DMT_DECODER_608, "%s\n", context->subline); } - write(context->out->fh, el, u); + write_wrapped(context->out->fh, el, u); //write (wb->fh, encoded_br, encoded_br_length); - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); begin += strlen((const char *)begin) + 1; } @@ -93,14 +93,14 @@ void write_stringz_as_smptett(char *string, struct encoder_ctx *context, LLONG m dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str); } used = encode_line(context, context->buffer, (unsigned char *)str); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); sprintf((char *)str, "

\n\n", h2, m2, s2, ms2); if (context->encoding != CCX_ENC_UNICODE) { dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str); } used = encode_line(context, context->buffer, (unsigned char *)str); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); sprintf((char *)str, "

\n"); free(el); free(unescaped); @@ -134,12 +134,12 @@ int write_cc_bitmap_as_smptett(struct cc_subtitle *sub, struct encoder_ctx *cont millis_to_time(sub->start_time, &h1, &m1, &s1, &ms1); millis_to_time(sub->end_time - 1, &h2, &m2, &s2, &ms2); // -1 To prevent overlapping with next line. sprintf((char *)context->buffer, "

\n", h1, m1, s1, ms1, h2, m2, s2, ms2); - write(context->out->fh, buf, strlen(buf)); + write_wrapped(context->out->fh, buf, strlen(buf)); len = strlen(rect[i].ocr_text); - write(context->out->fh, rect[i].ocr_text, len); - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, rect[i].ocr_text, len); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); sprintf(buf, "

\n"); - write(context->out->fh, buf, strlen(buf)); + write_wrapped(context->out->fh, buf, strlen(buf)); } } } @@ -231,7 +231,7 @@ int write_cc_buffer_as_smptett(struct eia608_screen *data, struct encoder_ctx *c dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str); } used = encode_line(context, context->buffer, (unsigned char *)str); - write(context->out->fh, context->buffer, used); + write_wrapped(context->out->fh, context->buffer, used); // Trimming subs because the position is defined by "tts:origin" int old_trim_subs = context->trim_subs; context->trim_subs = 1; @@ -388,9 +388,9 @@ int write_cc_buffer_as_smptett(struct eia608_screen *data, struct encoder_ctx *c } } - write(context->out->fh, final, strlen(final)); + write_wrapped(context->out->fh, final, strlen(final)); - write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); + write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length); context->trim_subs = old_trim_subs; sprintf(str, "