/* Initalize data compression */ int gprs_sndcp_dcomp_init(const void *ctx, struct gprs_sndcp_comp *comp_entity, const struct gprs_sndcp_comp_field *comp_field) { /* Note: This function is automatically called from * gprs_sndcp_comp.c when a new data compression * entity is created by gprs_sndcp.c */ OSMO_ASSERT(comp_entity); OSMO_ASSERT(comp_field); if (comp_entity->compclass == SNDCP_XID_DATA_COMPRESSION && comp_entity->algo == V42BIS) { OSMO_ASSERT(comp_field->v42bis_params); comp_entity->state = v42bis_init(ctx, NULL, comp_field->v42bis_params->p0, comp_field->v42bis_params->p1, comp_field->v42bis_params->p2, &tx_v42bis_frame_handler, NULL, V42BIS_MAX_OUTPUT_LENGTH, &rx_v42bis_data_handler, NULL, V42BIS_MAX_OUTPUT_LENGTH); LOGP(DSNDCP, LOGL_INFO, "V.42bis data compression initalized.\n"); return 0; } /* Just in case someone tries to initalize an unknown or unsupported * data compresson. Since everything is checked during the SNDCP * negotiation process, this should never happen! */ OSMO_ASSERT(false); }
/* Perform a full reset of the V.42bis compression state */ static void v42bis_reset(v42bis_state_t *comp) { /* This function performs a complete reset of the V.42bis compression * state by reinitalizing the state withe the previously negotiated * parameters. */ int p0, p1, p2; p0 = comp->decompress.v42bis_parm_p0 | comp->compress.v42bis_parm_p0; p1 = comp->decompress.v42bis_parm_n2; p2 = comp->decompress.v42bis_parm_n7; DEBUGP(DSNDCP, "Resetting compression state: %p, p0=%d, p1=%d, p2=%d\n", comp, p0, p1, p2); v42bis_init(NULL, comp, p0, p1, p2, &tx_v42bis_frame_handler, NULL, V42BIS_MAX_OUTPUT_LENGTH, &rx_v42bis_data_handler, NULL, V42BIS_MAX_OUTPUT_LENGTH); }
int main(int argc, char *argv[]) { int len; v42bis_state_t state_a; v42bis_state_t state_b; uint8_t buf[1024]; int in_fd; int v42bis_fd; int out_fd; int do_compression; int do_decompression; time_t now; do_compression = TRUE; do_decompression = TRUE; if (argc < 2) { fprintf(stderr, "Usage: %s <file>\n", argv[0]); exit(2); } if (do_compression) { if ((in_fd = open(argv[1], O_RDONLY)) < 0) { fprintf(stderr, "Error opening file '%s'.\n", argv[1]); exit(2); } if ((v42bis_fd = open(COMPRESSED_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { fprintf(stderr, "Error opening file '%s'.\n", COMPRESSED_FILE_NAME); exit(2); } time(&now); v42bis_init(&state_a, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, NULL, 512); v42bis_compression_control(&state_a, V42BIS_COMPRESSION_MODE_ALWAYS); in_octets_to_date = 0; out_octets_to_date = 0; while ((len = read(in_fd, buf, 1024)) > 0) { if (v42bis_compress(&state_a, buf, len)) { fprintf(stderr, "Bad return code from compression\n"); exit(2); } in_octets_to_date += len; } v42bis_compress_flush(&state_a); printf("%d bytes compressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now); close(in_fd); close(v42bis_fd); } if (do_decompression) { /* Now open the files for the decompression. */ if ((v42bis_fd = open(COMPRESSED_FILE_NAME, O_RDONLY)) < 0) { fprintf(stderr, "Error opening file '%s'.\n", COMPRESSED_FILE_NAME); exit(2); } if ((out_fd = open(OUTPUT_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { fprintf(stderr, "Error opening file '%s'.\n", OUTPUT_FILE_NAME); exit(2); } time(&now); v42bis_init(&state_b, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, (void *) (intptr_t) out_fd, 512); in_octets_to_date = 0; out_octets_to_date = 0; while ((len = read(v42bis_fd, buf, 1024)) > 0) { if (v42bis_decompress(&state_b, buf, len)) { fprintf(stderr, "Bad return code from decompression\n"); exit(2); } in_octets_to_date += len; } v42bis_decompress_flush(&state_b); printf("%d bytes decompressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now); close(v42bis_fd); close(out_fd); } return 0; }