LCOV - code coverage report
Current view: top level - libavcodec - mlz.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 129 0.0 %
Date: 2017-12-16 13:57:32 Functions: 0 6 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 Umair Khan <omerjerk@gmail.com>
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include "mlz.h"
      22             : 
      23           0 : av_cold void ff_mlz_init_dict(void* context, MLZ *mlz) {
      24           0 :     mlz->dict = av_mallocz_array(TABLE_SIZE, sizeof(*mlz->dict));
      25             : 
      26           0 :     mlz->flush_code            = FLUSH_CODE;
      27           0 :     mlz->current_dic_index_max = DIC_INDEX_INIT;
      28           0 :     mlz->dic_code_bit          = CODE_BIT_INIT;
      29           0 :     mlz->bump_code             = (DIC_INDEX_INIT - 1);
      30           0 :     mlz->next_code             = FIRST_CODE;
      31           0 :     mlz->freeze_flag           = 0;
      32           0 :     mlz->context               = context;
      33           0 : }
      34             : 
      35           0 : av_cold void ff_mlz_flush_dict(MLZ *mlz) {
      36           0 :     MLZDict *dict = mlz->dict;
      37             :     int i;
      38           0 :     for ( i = 0; i < TABLE_SIZE; i++ ) {
      39           0 :         dict[i].string_code = CODE_UNSET;
      40           0 :         dict[i].parent_code = CODE_UNSET;
      41           0 :         dict[i].match_len = 0;
      42             :     }
      43           0 :     mlz->current_dic_index_max = DIC_INDEX_INIT;
      44           0 :     mlz->dic_code_bit          = CODE_BIT_INIT;  // DicCodeBitInit;
      45           0 :     mlz->bump_code             = mlz->current_dic_index_max - 1;
      46           0 :     mlz->next_code             = FIRST_CODE;
      47           0 :     mlz->freeze_flag           = 0;
      48           0 : }
      49             : 
      50           0 : static void set_new_entry_dict(MLZDict* dict, int string_code, int parent_code, int char_code) {
      51           0 :     dict[string_code].parent_code = parent_code;
      52           0 :     dict[string_code].string_code = string_code;
      53           0 :     dict[string_code].char_code   = char_code;
      54           0 :     if (parent_code < FIRST_CODE) {
      55           0 :         dict[string_code].match_len = 2;
      56             :     } else {
      57           0 :         dict[string_code].match_len = (dict[parent_code].match_len) + 1;
      58             :     }
      59           0 : }
      60             : 
      61           0 : static int decode_string(MLZ* mlz, unsigned char *buff, int string_code, int *first_char_code, unsigned long bufsize) {
      62           0 :     MLZDict* dict = mlz->dict;
      63             :     unsigned long count, offset;
      64             :     int current_code, parent_code, tmp_code;
      65             : 
      66           0 :     count            = 0;
      67           0 :     current_code     = string_code;
      68           0 :     *first_char_code = CODE_UNSET;
      69             : 
      70           0 :     while (count < bufsize) {
      71           0 :         switch (current_code) {
      72           0 :         case CODE_UNSET:
      73           0 :             return count;
      74             :             break;
      75           0 :         default:
      76           0 :             if (current_code < FIRST_CODE) {
      77           0 :                 *first_char_code = current_code;
      78           0 :                 buff[0] = current_code;
      79           0 :                 count++;
      80           0 :                 return count;
      81             :             } else {
      82           0 :                 offset  = dict[current_code].match_len - 1;
      83           0 :                 tmp_code = dict[current_code].char_code;
      84           0 :                 if (offset >= bufsize) {
      85           0 :                     av_log(mlz->context, AV_LOG_ERROR, "MLZ offset error.\n");
      86           0 :                     return count;
      87             :                 }
      88           0 :                 buff[offset] = tmp_code;
      89           0 :                 count++;
      90             :             }
      91           0 :             current_code = dict[current_code].parent_code;
      92           0 :             if ((current_code < 0) || (current_code > (DIC_INDEX_MAX - 1))) {
      93           0 :                 av_log(mlz->context, AV_LOG_ERROR, "MLZ dic index error.\n");
      94           0 :                 return count;
      95             :             }
      96           0 :             if (current_code > FIRST_CODE) {
      97           0 :                 parent_code = dict[current_code].parent_code;
      98           0 :                 offset = (dict[current_code].match_len) - 1;
      99           0 :                 if (parent_code < 0 || parent_code > DIC_INDEX_MAX-1) {
     100           0 :                     av_log(mlz->context, AV_LOG_ERROR, "MLZ dic index error.\n");
     101           0 :                     return count;
     102             :                 }
     103           0 :                 if (( offset > (DIC_INDEX_MAX - 1))) {
     104           0 :                     av_log(mlz->context, AV_LOG_ERROR, "MLZ dic offset error.\n");
     105           0 :                     return count;
     106             :                 }
     107             :             }
     108           0 :             break;
     109             :         }
     110             :     }
     111           0 :     return count;
     112             : }
     113             : 
     114           0 : static int input_code(GetBitContext* gb, int len) {
     115           0 :     int tmp_code = 0;
     116             :     int i;
     117           0 :     for (i = 0; i < len; ++i) {
     118           0 :         tmp_code |= get_bits1(gb) << i;
     119             :     }
     120           0 :     return tmp_code;
     121             : }
     122             : 
     123           0 : int ff_mlz_decompression(MLZ* mlz, GetBitContext* gb, int size, unsigned char *buff) {
     124           0 :     MLZDict *dict = mlz->dict;
     125             :     unsigned long output_chars;
     126             :     int string_code, last_string_code, char_code;
     127             : 
     128           0 :     string_code = 0;
     129           0 :     char_code   = -1;
     130           0 :     last_string_code = -1;
     131           0 :     output_chars = 0;
     132             : 
     133           0 :     while (output_chars < size) {
     134           0 :         string_code = input_code(gb, mlz->dic_code_bit);
     135           0 :         switch (string_code) {
     136           0 :             case FLUSH_CODE:
     137             :             case MAX_CODE:
     138           0 :                 ff_mlz_flush_dict(mlz);
     139           0 :                 char_code = -1;
     140           0 :                 last_string_code = -1;
     141           0 :                 break;
     142           0 :             case FREEZE_CODE:
     143           0 :                 mlz->freeze_flag = 1;
     144           0 :                 break;
     145           0 :             default:
     146           0 :                 if (string_code > mlz->current_dic_index_max) {
     147           0 :                     av_log(mlz->context, AV_LOG_ERROR, "String code %d exceeds maximum value of %d.\n", string_code, mlz->current_dic_index_max);
     148           0 :                     return output_chars;
     149             :                 }
     150           0 :                 if (string_code == (int) mlz->bump_code) {
     151           0 :                     ++mlz->dic_code_bit;
     152           0 :                     mlz->current_dic_index_max *= 2;
     153           0 :                     mlz->bump_code = mlz->current_dic_index_max - 1;
     154             :                 } else {
     155           0 :                     if (string_code >= mlz->next_code) {
     156           0 :                         int ret = decode_string(mlz, &buff[output_chars], last_string_code, &char_code, size - output_chars);
     157           0 :                         if (ret < 0 || ret > size - output_chars) {
     158           0 :                             av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
     159           0 :                             return output_chars;
     160             :                         }
     161           0 :                         output_chars += ret;
     162           0 :                         ret = decode_string(mlz, &buff[output_chars], char_code, &char_code, size - output_chars);
     163           0 :                         if (ret < 0 || ret > size - output_chars) {
     164           0 :                             av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
     165           0 :                             return output_chars;
     166             :                         }
     167           0 :                         output_chars += ret;
     168           0 :                         set_new_entry_dict(dict, mlz->next_code, last_string_code, char_code);
     169           0 :                         if (mlz->next_code >= TABLE_SIZE - 1) {
     170           0 :                             av_log(mlz->context, AV_LOG_ERROR, "Too many MLZ codes\n");
     171           0 :                             return output_chars;
     172             :                         }
     173           0 :                         mlz->next_code++;
     174             :                     } else {
     175           0 :                         int ret = decode_string(mlz, &buff[output_chars], string_code, &char_code, size - output_chars);
     176           0 :                         if (ret < 0 || ret > size - output_chars) {
     177           0 :                             av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
     178           0 :                             return output_chars;
     179             :                         }
     180           0 :                         output_chars += ret;
     181           0 :                         if (output_chars <= size && !mlz->freeze_flag) {
     182           0 :                             if (last_string_code != -1) {
     183           0 :                                 set_new_entry_dict(dict, mlz->next_code, last_string_code, char_code);
     184           0 :                                 if (mlz->next_code >= TABLE_SIZE - 1) {
     185           0 :                                     av_log(mlz->context, AV_LOG_ERROR, "Too many MLZ codes\n");
     186           0 :                                     return output_chars;
     187             :                                 }
     188           0 :                                 mlz->next_code++;
     189             :                             }
     190             :                         } else {
     191             :                             break;
     192             :                         }
     193             :                     }
     194           0 :                     last_string_code = string_code;
     195             :                 }
     196           0 :                 break;
     197             :         }
     198           0 :     }
     199           0 :     return output_chars;
     200             : }

Generated by: LCOV version 1.13