-
Notifications
You must be signed in to change notification settings - Fork 17
Add ability to report positions relative to root stream and add PositionInfo interface #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mingun
wants to merge
3
commits into
kaitai-io:master
Choose a base branch
from
Mingun:absolute-positions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright 2015-2021 Kaitai Project: MIT license | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package io.kaitai.struct; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Span that represents positional information of array field and each of it | ||
* elements. Spans of items is available in the {@link #items} field. | ||
* | ||
* @since 0.10 | ||
*/ | ||
public class ArraySpan extends Span { | ||
/** Individual span of the each item in the array. */ | ||
public final List<Span> items; | ||
|
||
/** | ||
* Creates a span of array that starts at the current stream offset and | ||
* ends at the unknown position. | ||
* | ||
* @param io the stream to get positional information | ||
*/ | ||
public ArraySpan(KaitaiStream io) { | ||
super(io); | ||
items = new ArrayList<Span>(); | ||
} | ||
|
||
public ArraySpan(KaitaiStream io, int size) { | ||
super(io); | ||
items = new ArrayList<Span>(size); | ||
} | ||
|
||
/** | ||
* Appends a new span of array item from current stream position to the end-of-stream | ||
* to this span | ||
* | ||
* @param io Stream used to inquire current position | ||
* @return A new span, added to the internal list of item spans | ||
*/ | ||
public Span addItem(KaitaiStream io) { | ||
final Span span = new Span(io); | ||
items.add(span); | ||
return span; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright 2015-2021 Kaitai Project: MIT license | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package io.kaitai.struct; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* This interface is implemented by each {@link KaitaiStruct} successor, if | ||
* class was generated with positional information. | ||
* <p> | ||
* If you want to work with generated structures in the generic way, you can use | ||
* following code snipped to deal with positions information: | ||
* <code> | ||
* final KaitaiStruct struct = ...; | ||
* // Generator generates classes, that implements this interface, | ||
* // if debug mode/positions-info is enabled | ||
* if (struct instanceof PositionInfo) { | ||
* final PositionInfo info = (PositionInfo)struct; | ||
* //... | ||
* } | ||
* </code> | ||
* | ||
* @since 0.10 | ||
*/ | ||
public interface PositionInfo { | ||
/** | ||
* Information about each struct field. If field is an array, then corresponding | ||
* {@code Span} will be of {@link ArraySpan} instance. Map keys is equals to the | ||
* names of the java methods/fields in the generated class. | ||
* | ||
* @return the map from field name to field span information. | ||
*/ | ||
Map<String, Span> _spans(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* Copyright 2015-2021 Kaitai Project: MIT license | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package io.kaitai.struct; | ||
|
||
/** | ||
* Information about positions of parsed value in the streams. | ||
* | ||
* @since 0.10 | ||
*/ | ||
public class Span { | ||
/** Offset from begin of the root stream, for which that span was created. */ | ||
public final long offset; | ||
/** | ||
* Offset from begin of the stream, from which value was parsed. This is relative | ||
* position, to get an absolute position (relative to the root stream) use | ||
* {@link #absoluteStart()}. That offset is always non-negative. | ||
*/ | ||
public final long start; | ||
/** | ||
* Offset from begin of the stream, from which value was parsed. This is relative | ||
* position, to get an absolute position (relative to the root stream) use | ||
* {@link #absoluteEnd()}. | ||
* <p> | ||
* If that offset is negative, then value wasn't parsed yet or exception was | ||
* thrown while parsing value. | ||
*/ | ||
public long end = -1; | ||
|
||
/** | ||
* Creates a span that starts at the current stream offset and ends at | ||
* the unknown position. | ||
* | ||
* @param io the stream to get the positional information | ||
*/ | ||
public Span(KaitaiStream io) { | ||
this(io.offset(), io.pos()); | ||
} | ||
private Span(long offset, long start) { | ||
this.offset = offset; | ||
this.start = start; | ||
} | ||
|
||
/** | ||
* Offset to the start of this span relative to the root stream. | ||
* | ||
* @return start offset from the root stream | ||
*/ | ||
public long absoluteStart() { return offset + start; } | ||
/** | ||
* Offset to the end of this span relative to the root stream. | ||
* <p> | ||
* If that offset is negative, then value wasn't parsed yet or exception was | ||
* thrown while parsing value. | ||
* | ||
* @return start offset from the root stream or negative value if value not yet parsed | ||
*/ | ||
public long absoluteEnd() { return end < 0 ? -1 : offset + end; } | ||
/** | ||
* Size of this span in bytes. | ||
* <p> | ||
* If size is negative, then value wasn't parsed yet or exception was | ||
* thrown while parsing value. | ||
* | ||
* @return size of the span in bytes | ||
*/ | ||
public long size() { return end < 0 ? -1 : end - start; } | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that you want to add support for >2GB offsets for
RandomAccessFileKaitaiStream
, but I'm afraid it's not that simple, it deserves more careful consideration. The problem is thatlong
is not automatically coercible toint
, andint
is the target type forCalcIntType
in KS.CalcIntType
is the default integer type for all expressions - in fact, almost any integer value instance will have that type.Switching
_io.pos
type tou8
(long
) requires additional changes in KSC - whenever you use_io.pos
in expression, the derived type must belong
, notint
. The same applies to_io.size
- the fact that it's declared aslong
is a bit heedless. Right now, when you reference_io.pos
in a value instance like this:despite the
_io.size
is declared askaitai_struct_java_runtime/src/main/java/io/kaitai/struct/ByteBufferKaitaiStream.java
Line 170 in e1168b0
it will be truncated to
int
anyway:Another solution would be switch target type of
CalcIntType
fromint
tolong
(see kaitai-io/kaitai_struct#510), but it also requires a careful consideration - as I understand,long
cannot be used everywhere in Java, for example in array indices. Morever, Java conventions suggest to useint
whenever possible, and it should be probably also more efficient at least on 32-bit platforms.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. All that you say is applicable to the
size
, butpos
, it seems, is not used in the array subscription expressions... At least none of the https://github.com/kaitai-io/kaitai_struct_tests fail, but I'm not known if these are ALL tests...Also, it seems that
pos()
isn't used much in the computations: if you try to find usages of thepos
in the https://github.com/kaitai-io/kaitai_struct_formats repo (RegExp:pos[^:_a-z]
) you'll find only 16 occurrences that used only with conjunction with thesize
(which is alreadylong
) or plain number. So at least that change seems to not break anything.Results from VS Code
So, if you conclusion about
pos()
type wouldn't changed, just say and I'll revert thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@generalmimon, could you comment on this?