1 /**
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include "../includes/common.h"
21 
22 #define REF_COUNT 1
23 
24 extern "C" {
25 #include <Tremolo/codebook.h>
26 }
27 
28 bool testInProgress = false;
29 struct sigaction new_action, old_action;
sigabrt_handler(int signum,siginfo_t * info,void * context)30 void sigabrt_handler(int signum, siginfo_t *info, void* context) {
31     if (testInProgress && info->si_signo == SIGABRT) {
32         (*old_action.sa_sigaction)(signum, info, context);
33         return;
34     }
35     _exit(EXIT_FAILURE);
36 }
37 
38 unsigned char data[] = {/* 24 bits to make sure the alignment is correct */
39                         0x42, 0x43, 0x56,
40                         /* 16 bits for codebook.dim */
41                         0x40, 0x00,
42                         /* 24 bits for codebook.entries */
43                         0x10, 0x00, 0x00,
44                         /* 1 bit for ordering which is unset for unordered */
45                         /* 1 bit set for specifying unused entries */
46                         /* 1 bit for valid length */
47                         /* 5 bits for length of entry */
48                         0x06,
49                         /* 1 bit for valid length */
50                         /* 5 bits for length of entry */
51                         /* 2 bits for specifying invalid length for next 2 entries */
52                         0x01,
53                         /* 8 bits for specifying invalid length for next 8 entries */
54                         0x00,
55                         /* 4 bits for specifying invalid length for next 4 entries */
56                         /* 4 bits for specifying the map type 1 */
57                         0x10,
58                         /* 32 bits for codebook.q_min */
59                         0x00, 0x00, 0x00, 0x00,
60                         /* 32 bits for codebook.q_del */
61                         0x00, 0x00, 0x00, 0x00,
62                         /* 4 bits for codebook.q_bits */
63                         /* 1 bit for codebook.q_seq */
64                         /* 4 bits for quantized values of codebook.q_val for quantvals = 2 */
65                         /* 7 bits remaining unused */
66                         0x01, 0x00};
67 
main()68 int main() {
69     sigemptyset(&new_action.sa_mask);
70     new_action.sa_flags = SA_SIGINFO;
71     new_action.sa_sigaction = sigabrt_handler;
72     sigaction(SIGABRT, &new_action, &old_action);
73 
74     ogg_buffer buf;
75     ogg_reference ref;
76     oggpack_buffer bits;
77     codebook book = {};
78 
79     memset(&buf, 0, sizeof(ogg_buffer));
80     memset(&ref, 0, sizeof(ogg_reference));
81     memset(&bits, 0, sizeof(oggpack_buffer));
82 
83     buf.data = (uint8_t *)data;
84     buf.size = sizeof(data);
85     buf.refcount = REF_COUNT;
86 
87     ref.buffer = &buf;
88     ref.length = sizeof(data);
89     oggpack_readinit(&bits, &ref);
90 
91     testInProgress = true;
92     FAIL_CHECK(vorbis_book_unpack(&bits, &book) == 0);
93     testInProgress = false;
94     return EXIT_SUCCESS;
95 }
96