aboutsummaryrefslogtreecommitdiffstats
path: root/src/cnki_hn.c
blob: c2f76ecca5de68413b0d6064dedb06528877049d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (c) 2020-2022, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdlib.h>

#include "cnki.h"

int
cnki_hn(cnki_t **param)
{
	if (*param == NULL)
		return 1;

	if ((*param)->stat > 0)
		printf("Begin 'HN' conversion\n");

	if ((*param)->file_stat->page > 0)
		(*param)->object_hn = malloc(sizeof(object_hn_t));
	else
		return 1;

	if ((*param)->object_hn == NULL)
		return 1;

	if ((*param)->stat > 1) {
		printf("Loading page(s)\n");
		printf("\t%8s\t%8s\t%6s\t%4s\t%8s\t%8s\t%4s\t%8s\t%8s\n",
			"address",
			"text",
			"length",
			"page",
			"unknown",
			"next",
			"code",
			"address",
			"image");
	}

	object_hn_t *ptr = (*param)->object_hn;
	for (int i = 0; i < (*param)->file_stat->page; i++) {
		fread(&ptr->address, 4, 1, (*param)->fp_i);
		fread(&ptr->text_size, 4, 1, (*param)->fp_i);
		fread(&ptr->image_length, 2, 1, (*param)->fp_i);
		fread(&ptr->page, 2, 1, (*param)->fp_i);
		fread(&ptr->unknown, 4, 1, (*param)->fp_i);
		fread(&ptr->address_next, 4, 1, (*param)->fp_i);

		ptr->text = NULL;
		ptr->image_data = NULL;
		ptr->next = NULL;

		if (i < (*param)->file_stat->page - 1) {
			ptr->next = malloc(sizeof(object_hn_t));

			if (ptr->next == NULL)
				return 1;
		}

		ptr = ptr->next;
	}

	ptr = (*param)->object_hn;
	while (ptr != NULL) {
		if (ptr->text_size > 0) {
			ptr->text = malloc(ptr->text_size);

			if (ptr->text == NULL)
				return 1;

			fseek((*param)->fp_i, ptr->address, SEEK_SET);
			fread(ptr->text, ptr->text_size, 1, (*param)->fp_i);
		}

		if ((*param)->stat > 1)
			printf("\t%08x\t%8d\t%6d\t%4d\t%8d\t%08x",
				ptr->address,
				ptr->text_size,
				ptr->image_length,
				ptr->page,
				ptr->unknown,
				ptr->address_next);

		if (ptr->image_length > 0) {
			ptr->image_data = malloc(ptr->image_length * sizeof(hn_image_t));

			if (ptr->image_data == NULL)
				return 1;

			for (int i = 0; i < ptr->image_length; i++) {
				fread(&ptr->image_data[i].format, 4, 1, (*param)->fp_i);
				fread(&ptr->image_data[i].address, 4, 1, (*param)->fp_i);
				fread(&ptr->image_data[i].size, 4, 1, (*param)->fp_i);
				ptr->image_data[i].x = 0;
				ptr->image_data[i].y = 0;
				ptr->image_data[i].w = 0;
				ptr->image_data[i].h = 0;
				fseek((*param)->fp_i,
					ptr->image_data[i].address + ptr->image_data[i].size,
					SEEK_SET);
			}

			for (int i = 0; i < ptr->image_length; i++) {
				ptr->image_data[i].image = malloc(ptr->image_data[i].size);

				if (ptr->image_data[i].image == NULL)
					return 1;

				fseek((*param)->fp_i, ptr->image_data[i].address, SEEK_SET);
				fread(ptr->image_data[i].image,
					ptr->image_data[i].size, 1,
					(*param)->fp_i);

				if ((*param)->stat > 1) {
					if (i == 0) {
						printf("\t%4d\t%08x\t%8d\n",
							ptr->image_data[i].format,
							ptr->image_data[i].address,
							ptr->image_data[i].size);
					} else {
						printf("\t%8s\t%8s\t%6s\t%4s\t%8s\t%8s\t%4d\t%08x\t%8d\n",
							"",
							"",
							"",
							"",
							"",
							"",
							ptr->image_data[i].format,
							ptr->image_data[i].address,
							ptr->image_data[i].size);
					}
				}
			}
		} else if ((*param)->stat > 1) {
			printf("\t%4s\t%8s\t%8s\n",
				"",
				"",
				"");
		}

		ptr = ptr->next;
	}

	if ((*param)->stat > 0)
		printf("Loaded %d page(s)\n", (*param)->file_stat->page);

	cnki_pdf_hn(param);

	if ((*param)->stat > 0)
		printf("Conversion ended\n");

	return 0;
}