-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipWatermarker.kt
73 lines (64 loc) · 2.56 KB
/
ZipWatermarker.kt
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
/*
* Copyright (c) 2023-2024 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
*
* This work is licensed under the Fraunhofer License (on the basis of the MIT license)
* that can be found in the LICENSE file.
*/
package de.fraunhofer.isst.trend.watermarker.fileWatermarker
import de.fraunhofer.isst.trend.watermarker.files.ZipFile
import de.fraunhofer.isst.trend.watermarker.returnTypes.Result
import de.fraunhofer.isst.trend.watermarker.returnTypes.Status
import de.fraunhofer.isst.trend.watermarker.watermarks.Watermark
import kotlin.js.JsExport
const val ZIP_WATERMARK_ID: UShort = 0x8777u
@JsExport
object ZipWatermarker : FileWatermarker<ZipFile> {
const val SOURCE = "ZipWatermarker"
/** Returns the name of the FileWatermark. Used in Event messages. */
override fun getSource(): String = SOURCE
/**
* Adds a [watermark] to [file]
* Returns an error if the size of the ExtraFields exceed UShort::MAX
*/
override fun addWatermark(
file: ZipFile,
watermark: List<Byte>,
): Status {
return file.header.addExtraField(ZIP_WATERMARK_ID, watermark)
}
/** Checks if [file] contains a watermark */
override fun containsWatermark(file: ZipFile): Boolean {
for (extraField in file.header.extraFields) {
if (extraField.id == ZIP_WATERMARK_ID) return true
}
return false
}
/** Returns all watermarks in [file] */
override fun getWatermarks(file: ZipFile): Result<List<Watermark>> {
val watermarks = ArrayList<Watermark>()
for (extraField in file.header.extraFields) {
if (extraField.id == ZIP_WATERMARK_ID) {
watermarks.add(Watermark(extraField.data))
}
}
return Result.success(watermarks)
}
/** Removes all watermarks in [file] and returns them */
override fun removeWatermarks(file: ZipFile): Result<List<Watermark>> {
val watermarks = ArrayList<Watermark>()
for (extraField in file.header.removeExtraFields(ZIP_WATERMARK_ID)) {
watermarks.add(Watermark(extraField.data))
}
return Result.success(watermarks)
}
/**
* Parses [bytes] as zip file.
* Parsing includes separating the header from the content and parsing the header.
*
* Returns errors if it cannot parse [bytes] as zip file.
* Returns warnings if the parser finds unexpected structures but is still able to parse it
*/
override fun parseBytes(bytes: List<Byte>): Result<ZipFile> {
return ZipFile.fromBytes(bytes.toByteArray())
}
}